]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - jpeg.c
made packfile_t (describes a file in a pack) use fs_offset_t
[xonotic/darkplaces.git] / jpeg.c
diff --git a/jpeg.c b/jpeg.c
index 063a86f427c216331b26647bb3f68bb3e0356bcf..420fda737a3e411161a79293161d4c276edb7e80 100644 (file)
--- a/jpeg.c
+++ b/jpeg.c
@@ -23,6 +23,7 @@
 
 
 #include "quakedef.h"
+#include "image.h"
 #include "jpeg.h"
 
 
@@ -394,40 +395,31 @@ Try to load the JPEG DLL
 */
 qboolean JPEG_OpenLibrary (void)
 {
-       const char* dllname;
-       const dllfunction_t *func;
-
-       // Already loaded?
-       if (jpeg_dll)
-               return true;
-
+       const char* dllnames [] =
+       {
 #ifdef WIN32
-       dllname = "libjpeg.dll";
+               "libjpeg.dll",
+#elif defined(MACOSX)
+               "libjpeg.62.dylib",
 #else
-       dllname = "libjpeg.so.62";
+               "libjpeg.so.62",
+               "libjpeg.so",
 #endif
+               NULL
+       };
 
-       // Initializations
-       for (func = jpegfuncs; func && func->name != NULL; func++)
-               *func->funcvariable = NULL;
+       // Already loaded?
+       if (jpeg_dll)
+               return true;
 
        // Load the DLL
-       if (! (jpeg_dll = Sys_LoadLibrary (dllname)))
+       if (! Sys_LoadLibrary (dllnames, &jpeg_dll, jpegfuncs))
        {
-               Con_Printf ("Can't find %s. JPEG support disabled\n", dllname);
+               Con_Printf ("JPEG support disabled\n");
                return false;
        }
 
-       // Get the function adresses
-       for (func = jpegfuncs; func && func->name != NULL; func++)
-               if (!(*func->funcvariable = (void *) Sys_GetProcAddress (jpeg_dll, func->name)))
-               {
-                       Con_Printf ("missing function \"%s\" - broken JPEG library!\n", func->name);
-                       JPEG_CloseLibrary ();
-                       return false;
-               }
-
-       Con_Printf ("%s loaded. JPEG support enabled\n", dllname);
+       Con_Printf ("JPEG support enabled\n");
        return true;
 }
 
@@ -441,11 +433,7 @@ Unload the JPEG DLL
 */
 void JPEG_CloseLibrary (void)
 {
-       if (!jpeg_dll)
-               return;
-
-       Sys_UnloadLibrary (jpeg_dll);
-       jpeg_dll = NULL;
+       Sys_UnloadLibrary (&jpeg_dll);
 }
 
 
@@ -480,7 +468,7 @@ static void JPEG_SkipInputData (j_decompress_ptr cinfo, long num_bytes)
     cinfo->src->bytes_in_buffer -= num_bytes;
 }
 
-static void JPEG_MemSrc (j_decompress_ptr cinfo, qbyte *buffer)
+static void JPEG_MemSrc (j_decompress_ptr cinfo, const qbyte *buffer)
 {
        cinfo->src = cinfo->mem->alloc_small ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (struct jpeg_source_mgr));
 
@@ -508,7 +496,7 @@ JPEG_LoadImage
 Load a JPEG image into a RGBA buffer
 ====================
 */
-qbyte* JPEG_LoadImage (qbyte *f, int matchwidth, int matchheight)
+qbyte* JPEG_LoadImage (const qbyte *f, int matchwidth, int matchheight)
 {
        struct jpeg_decompress_struct cinfo;
        struct jpeg_error_mgr jerr;
@@ -534,6 +522,11 @@ qbyte* JPEG_LoadImage (qbyte *f, int matchwidth, int matchheight)
                qjpeg_destroy_decompress (&cinfo);
                return NULL;
        }
+       if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
+       {
+               Con_Printf("JPEG_LoadImage: invalid image size %ix%i\n", image_width, image_height);
+               return NULL;
+       }
 
        image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
        scanline = Mem_Alloc(tempmempool, image_width * cinfo.output_components);
@@ -635,7 +628,7 @@ static void JPEG_TermDestination (j_compress_ptr cinfo)
 
        // Write any data remaining in the buffer
        if (datacount > 0)
-               if (FS_Write (dest->outfile, dest->buffer, datacount) != datacount)
+               if (FS_Write (dest->outfile, dest->buffer, datacount) != (fs_offset_t)datacount)
                        error_in_jpeg = true;
 }
 
@@ -673,12 +666,12 @@ qboolean JPEG_SaveImage_preflipped (const char *filename, int width, int height,
        // No DLL = no JPEGs
        if (!jpeg_dll)
        {
-               Con_Print("You need the libjpeg library to save JPEG images\n");
+               Con_Print("You need the libjpeg library to save JPEG images\n");
                return false;
        }
 
        // Open the file
-       file = FS_Open (filename, "wb", true);
+       file = FS_Open (filename, "wb", true, false);
        if (!file)
                return false;
 
@@ -695,7 +688,7 @@ qboolean JPEG_SaveImage_preflipped (const char *filename, int width, int height,
        cinfo.in_color_space = JCS_RGB;
        cinfo.input_components = 3;
        qjpeg_set_defaults (&cinfo);
-       qjpeg_set_quality (&cinfo, 90, TRUE);  // 90% quality; FIXME: use a cvar
+       qjpeg_set_quality (&cinfo, scr_screenshot_jpeg_quality.value * 100, TRUE);
        qjpeg_start_compress (&cinfo, true);
 
        // Compress each scanline