shut up jpeg warnings another way
[divverent/darkplaces.git] / jpeg.c
1 /*
2         Copyright (C) 2002  Mathieu Olivier
3
4         This program is free software; you can redistribute it and/or
5         modify it under the terms of the GNU General Public License
6         as published by the Free Software Foundation; either version 2
7         of the License, or (at your option) any later version.
8
9         This program is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13         See the GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to:
17
18                 Free Software Foundation, Inc.
19                 59 Temple Place - Suite 330
20                 Boston, MA  02111-1307, USA
21
22 */
23
24
25 #include "quakedef.h"
26 #include "image.h"
27 #include "jpeg.h"
28
29 cvar_t sv_writepicture_quality = {CVAR_SAVE, "sv_writepicture_quality", "10", "WritePicture quality offset (higher means better quality, but slower)"};
30
31 // jboolean is unsigned char instead of int on Win32
32 #ifdef WIN32
33 typedef unsigned char jboolean;
34 #else
35 typedef int jboolean;
36 #endif
37
38 #ifdef LINK_TO_LIBJPEG
39 #include <jpeglib.h>
40 #define qjpeg_create_compress jpeg_create_compress
41 #define qjpeg_create_decompress jpeg_create_decompress
42 #define qjpeg_destroy_compress jpeg_destroy_compress
43 #define qjpeg_destroy_decompress jpeg_destroy_decompress
44 #define qjpeg_finish_compress jpeg_finish_compress
45 #define qjpeg_finish_decompress jpeg_finish_decompress
46 #define qjpeg_resync_to_restart jpeg_resync_to_restart
47 #define qjpeg_read_header jpeg_read_header
48 #define qjpeg_read_scanlines jpeg_read_scanlines
49 #define qjpeg_set_defaults jpeg_set_defaults
50 #define qjpeg_set_quality jpeg_set_quality
51 #define qjpeg_start_compress jpeg_start_compress
52 #define qjpeg_start_decompress jpeg_start_decompress
53 #define qjpeg_std_error jpeg_std_error
54 #define qjpeg_write_scanlines jpeg_write_scanlines
55 #define jpeg_dll true
56 #else
57 /*
58 =================================================================
59
60   Minimal set of definitions from the JPEG lib
61
62   WARNING: for a matter of simplicity, several pointer types are
63   casted to "void*", and most enumerated values are not included
64
65 =================================================================
66 */
67
68 typedef void *j_common_ptr;
69 typedef struct jpeg_compress_struct *j_compress_ptr;
70 typedef struct jpeg_decompress_struct *j_decompress_ptr;
71
72 #define JPEG_LIB_VERSION  62  // Version 6b
73
74 typedef enum
75 {
76         JCS_UNKNOWN,
77         JCS_GRAYSCALE,
78         JCS_RGB,
79         JCS_YCbCr,
80         JCS_CMYK,
81         JCS_YCCK
82 } J_COLOR_SPACE;
83 typedef enum {JPEG_DUMMY1} J_DCT_METHOD;
84 typedef enum {JPEG_DUMMY2} J_DITHER_MODE;
85 typedef unsigned int JDIMENSION;
86
87 #define JPOOL_PERMANENT 0       // lasts until master record is destroyed
88 #define JPOOL_IMAGE             1       // lasts until done with image/datastream
89
90 #define JPEG_EOI        0xD9  // EOI marker code
91
92 #define JMSG_STR_PARM_MAX  80
93
94 #define DCTSIZE2 64
95 #define NUM_QUANT_TBLS 4
96 #define NUM_HUFF_TBLS 4
97 #define NUM_ARITH_TBLS 16
98 #define MAX_COMPS_IN_SCAN 4
99 #define C_MAX_BLOCKS_IN_MCU 10
100 #define D_MAX_BLOCKS_IN_MCU 10
101
102 struct jpeg_memory_mgr
103 {
104   void* (*alloc_small) (j_common_ptr cinfo, int pool_id, size_t sizeofobject);
105   void (*alloc_large) (void *dummy, ...);
106   void (*alloc_sarray) (void *dummy, ...);
107   void (*alloc_barray) (void *dummy, ...);
108   void (*request_virt_sarray) (void *dummy, ...);
109   void (*request_virt_barray) (void *dummy, ...);
110   void (*realize_virt_arrays) (void *dummy, ...);
111   void (*access_virt_sarray) (void *dummy, ...);
112   void (*access_virt_barray) (void *dummy, ...);
113   void (*free_pool) (void *dummy, ...);
114   void (*self_destruct) (void *dummy, ...);
115
116   long max_memory_to_use;
117   long max_alloc_chunk;
118 };
119
120 struct jpeg_error_mgr
121 {
122         void (*error_exit) (j_common_ptr cinfo);
123         void (*emit_message) (j_common_ptr cinfo, int msg_level);
124         void (*output_message) (j_common_ptr cinfo);
125         void (*format_message) (j_common_ptr cinfo, char * buffer);
126         void (*reset_error_mgr) (j_common_ptr cinfo);
127         int msg_code;
128         union {
129                 int i[8];
130                 char s[JMSG_STR_PARM_MAX];
131         } msg_parm;
132         int trace_level;
133         long num_warnings;
134         const char * const * jpeg_message_table;
135         int last_jpeg_message;
136         const char * const * addon_message_table;
137         int first_addon_message;
138         int last_addon_message;
139 };
140
141 struct jpeg_source_mgr
142 {
143         const unsigned char *next_input_byte;
144         size_t bytes_in_buffer;
145
146         void (*init_source) (j_decompress_ptr cinfo);
147         jboolean (*fill_input_buffer) (j_decompress_ptr cinfo);
148         void (*skip_input_data) (j_decompress_ptr cinfo, long num_bytes);
149         jboolean (*resync_to_restart) (j_decompress_ptr cinfo, int desired);
150         void (*term_source) (j_decompress_ptr cinfo);
151 };
152
153 typedef struct {
154   /* These values are fixed over the whole image. */
155   /* For compression, they must be supplied by parameter setup; */
156   /* for decompression, they are read from the SOF marker. */
157   int component_id;             /* identifier for this component (0..255) */
158   int component_index;          /* its index in SOF or cinfo->comp_info[] */
159   int h_samp_factor;            /* horizontal sampling factor (1..4) */
160   int v_samp_factor;            /* vertical sampling factor (1..4) */
161   int quant_tbl_no;             /* quantization table selector (0..3) */
162   /* These values may vary between scans. */
163   /* For compression, they must be supplied by parameter setup; */
164   /* for decompression, they are read from the SOS marker. */
165   /* The decompressor output side may not use these variables. */
166   int dc_tbl_no;                /* DC entropy table selector (0..3) */
167   int ac_tbl_no;                /* AC entropy table selector (0..3) */
168   
169   /* Remaining fields should be treated as private by applications. */
170   
171   /* These values are computed during compression or decompression startup: */
172   /* Component's size in DCT blocks.
173    * Any dummy blocks added to complete an MCU are not counted; therefore
174    * these values do not depend on whether a scan is interleaved or not.
175    */
176   JDIMENSION width_in_blocks;
177   JDIMENSION height_in_blocks;
178   /* Size of a DCT block in samples.  Always DCTSIZE for compression.
179    * For decompression this is the size of the output from one DCT block,
180    * reflecting any scaling we choose to apply during the IDCT step.
181    * Values of 1,2,4,8 are likely to be supported.  Note that different
182    * components may receive different IDCT scalings.
183    */
184   int DCT_scaled_size;
185   /* The downsampled dimensions are the component's actual, unpadded number
186    * of samples at the main buffer (preprocessing/compression interface), thus
187    * downsampled_width = ceil(image_width * Hi/Hmax)
188    * and similarly for height.  For decompression, IDCT scaling is included, so
189    * downsampled_width = ceil(image_width * Hi/Hmax * DCT_scaled_size/DCTSIZE)
190    */
191   JDIMENSION downsampled_width;  /* actual width in samples */
192   JDIMENSION downsampled_height; /* actual height in samples */
193   /* This flag is used only for decompression.  In cases where some of the
194    * components will be ignored (eg grayscale output from YCbCr image),
195    * we can skip most computations for the unused components.
196    */
197   jboolean component_needed;     /* do we need the value of this component? */
198
199   /* These values are computed before starting a scan of the component. */
200   /* The decompressor output side may not use these variables. */
201   int MCU_width;                /* number of blocks per MCU, horizontally */
202   int MCU_height;               /* number of blocks per MCU, vertically */
203   int MCU_blocks;               /* MCU_width * MCU_height */
204   int MCU_sample_width;         /* MCU width in samples, MCU_width*DCT_scaled_size */
205   int last_col_width;           /* # of non-dummy blocks across in last MCU */
206   int last_row_height;          /* # of non-dummy blocks down in last MCU */
207
208   /* Saved quantization table for component; NULL if none yet saved.
209    * See jdinput.c comments about the need for this information.
210    * This field is currently used only for decompression.
211    */
212   void *quant_table;
213
214   /* Private per-component storage for DCT or IDCT subsystem. */
215   void * dct_table;
216 } jpeg_component_info;
217
218 struct jpeg_decompress_struct
219 {
220         struct jpeg_error_mgr *err;             // USED
221         struct jpeg_memory_mgr *mem;    // USED
222
223         void *progress;
224         void *client_data;
225         jboolean is_decompressor;
226         int global_state;
227
228         struct jpeg_source_mgr *src;    // USED
229         JDIMENSION image_width;                 // USED
230         JDIMENSION image_height;                // USED
231
232         int num_components;
233         J_COLOR_SPACE jpeg_color_space;
234         J_COLOR_SPACE out_color_space;
235         unsigned int scale_num, scale_denom;
236         double output_gamma;
237         jboolean buffered_image;
238         jboolean raw_data_out;
239         J_DCT_METHOD dct_method;
240         jboolean do_fancy_upsampling;
241         jboolean do_block_smoothing;
242         jboolean quantize_colors;
243         J_DITHER_MODE dither_mode;
244         jboolean two_pass_quantize;
245         int desired_number_of_colors;
246         jboolean enable_1pass_quant;
247         jboolean enable_external_quant;
248         jboolean enable_2pass_quant;
249         JDIMENSION output_width;
250
251         JDIMENSION output_height;       // USED
252
253         int out_color_components;
254
255         int output_components;          // USED
256
257         int rec_outbuf_height;
258         int actual_number_of_colors;
259         void *colormap;
260
261         JDIMENSION output_scanline;     // USED
262
263         int input_scan_number;
264         JDIMENSION input_iMCU_row;
265         int output_scan_number;
266         JDIMENSION output_iMCU_row;
267         int (*coef_bits)[DCTSIZE2];
268         void *quant_tbl_ptrs[NUM_QUANT_TBLS];
269         void *dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
270         void *ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
271         int data_precision;
272         jpeg_component_info *comp_info;
273         jboolean progressive_mode;
274         jboolean arith_code;
275         unsigned char arith_dc_L[NUM_ARITH_TBLS];
276         unsigned char arith_dc_U[NUM_ARITH_TBLS];
277         unsigned char arith_ac_K[NUM_ARITH_TBLS];
278         unsigned int restart_interval;
279         jboolean saw_JFIF_marker;
280         unsigned char JFIF_major_version;
281         unsigned char JFIF_minor_version;
282         unsigned char density_unit;
283         unsigned short X_density;
284         unsigned short Y_density;
285         jboolean saw_Adobe_marker;
286         unsigned char Adobe_transform;
287         jboolean CCIR601_sampling;
288         void *marker_list;
289         int max_h_samp_factor;
290         int max_v_samp_factor;
291         int min_DCT_scaled_size;
292         JDIMENSION total_iMCU_rows;
293         void *sample_range_limit;
294         int comps_in_scan;
295         jpeg_component_info *cur_comp_info[MAX_COMPS_IN_SCAN];
296         JDIMENSION MCUs_per_row;
297         JDIMENSION MCU_rows_in_scan;
298         int blocks_in_MCU;
299         int MCU_membership[D_MAX_BLOCKS_IN_MCU];
300         int Ss, Se, Ah, Al;
301         int unread_marker;
302         void *master;
303         void *main;
304         void *coef;
305         void *post;
306         void *inputctl;
307         void *marker;
308         void *entropy;
309         void *idct;
310         void *upsample;
311         void *cconvert;
312         void *cquantize;
313 };
314
315
316 struct jpeg_compress_struct
317 {
318         struct jpeg_error_mgr *err;
319         struct jpeg_memory_mgr *mem;
320         void *progress;
321         void *client_data;
322         jboolean is_decompressor;
323         int global_state;
324
325         void *dest;
326         JDIMENSION image_width;
327         JDIMENSION image_height;
328         int input_components;
329         J_COLOR_SPACE in_color_space;
330         double input_gamma;
331         int data_precision;
332
333         int num_components;
334         J_COLOR_SPACE jpeg_color_space;
335         jpeg_component_info *comp_info;
336         void *quant_tbl_ptrs[NUM_QUANT_TBLS];
337         void *dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
338         void *ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
339         unsigned char arith_dc_L[NUM_ARITH_TBLS];
340         unsigned char arith_dc_U[NUM_ARITH_TBLS];
341         unsigned char arith_ac_K[NUM_ARITH_TBLS];
342
343         int num_scans;
344         const void *scan_info;
345         jboolean raw_data_in;
346         jboolean arith_code;
347         jboolean optimize_coding;
348         jboolean CCIR601_sampling;
349         int smoothing_factor;
350         J_DCT_METHOD dct_method;
351
352         unsigned int restart_interval;
353         int restart_in_rows;
354
355         jboolean write_JFIF_header;
356         unsigned char JFIF_major_version;
357         unsigned char JFIF_minor_version;
358         unsigned char density_unit;
359         unsigned short X_density;
360         unsigned short Y_density;
361         jboolean write_Adobe_marker;
362         JDIMENSION next_scanline;
363
364         jboolean progressive_mode;
365         int max_h_samp_factor;
366         int max_v_samp_factor;
367         JDIMENSION total_iMCU_rows;
368         int comps_in_scan;
369         jpeg_component_info *cur_comp_info[MAX_COMPS_IN_SCAN];
370         JDIMENSION MCUs_per_row;
371         JDIMENSION MCU_rows_in_scan;
372         int blocks_in_MCU;
373         int MCU_membership[C_MAX_BLOCKS_IN_MCU];
374         int Ss, Se, Ah, Al;
375
376         void *master;
377         void *main;
378         void *prep;
379         void *coef;
380         void *marker;
381         void *cconvert;
382         void *downsample;
383         void *fdct;
384         void *entropy;
385         void *script_space;
386         int script_space_size;
387 };
388
389 struct jpeg_destination_mgr
390 {
391         unsigned char* next_output_byte;
392         size_t free_in_buffer;
393
394         void (*init_destination) (j_compress_ptr cinfo);
395         jboolean (*empty_output_buffer) (j_compress_ptr cinfo);
396         void (*term_destination) (j_compress_ptr cinfo);
397 };
398
399
400 /*
401 =================================================================
402
403   DarkPlaces definitions
404
405 =================================================================
406 */
407
408 // Functions exported from libjpeg
409 #define qjpeg_create_compress(cinfo) \
410         qjpeg_CreateCompress((cinfo), JPEG_LIB_VERSION, (size_t) sizeof(struct jpeg_compress_struct))
411 #define qjpeg_create_decompress(cinfo) \
412         qjpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, (size_t) sizeof(struct jpeg_decompress_struct))
413
414 static void (*qjpeg_CreateCompress) (j_compress_ptr cinfo, int version, size_t structsize);
415 static void (*qjpeg_CreateDecompress) (j_decompress_ptr cinfo, int version, size_t structsize);
416 static void (*qjpeg_destroy_compress) (j_compress_ptr cinfo);
417 static void (*qjpeg_destroy_decompress) (j_decompress_ptr cinfo);
418 static void (*qjpeg_finish_compress) (j_compress_ptr cinfo);
419 static jboolean (*qjpeg_finish_decompress) (j_decompress_ptr cinfo);
420 static jboolean (*qjpeg_resync_to_restart) (j_decompress_ptr cinfo, int desired);
421 static int (*qjpeg_read_header) (j_decompress_ptr cinfo, jboolean require_image);
422 static JDIMENSION (*qjpeg_read_scanlines) (j_decompress_ptr cinfo, unsigned char** scanlines, JDIMENSION max_lines);
423 static void (*qjpeg_set_defaults) (j_compress_ptr cinfo);
424 static void (*qjpeg_set_quality) (j_compress_ptr cinfo, int quality, jboolean force_baseline);
425 static jboolean (*qjpeg_start_compress) (j_compress_ptr cinfo, jboolean write_all_tables);
426 static jboolean (*qjpeg_start_decompress) (j_decompress_ptr cinfo);
427 static struct jpeg_error_mgr* (*qjpeg_std_error) (struct jpeg_error_mgr *err);
428 static JDIMENSION (*qjpeg_write_scanlines) (j_compress_ptr cinfo, unsigned char** scanlines, JDIMENSION num_lines);
429
430 static dllfunction_t jpegfuncs[] =
431 {
432         {"jpeg_CreateCompress",         (void **) &qjpeg_CreateCompress},
433         {"jpeg_CreateDecompress",       (void **) &qjpeg_CreateDecompress},
434         {"jpeg_destroy_compress",       (void **) &qjpeg_destroy_compress},
435         {"jpeg_destroy_decompress",     (void **) &qjpeg_destroy_decompress},
436         {"jpeg_finish_compress",        (void **) &qjpeg_finish_compress},
437         {"jpeg_finish_decompress",      (void **) &qjpeg_finish_decompress},
438         {"jpeg_resync_to_restart",      (void **) &qjpeg_resync_to_restart},
439         {"jpeg_read_header",            (void **) &qjpeg_read_header},
440         {"jpeg_read_scanlines",         (void **) &qjpeg_read_scanlines},
441         {"jpeg_set_defaults",           (void **) &qjpeg_set_defaults},
442         {"jpeg_set_quality",            (void **) &qjpeg_set_quality},
443         {"jpeg_start_compress",         (void **) &qjpeg_start_compress},
444         {"jpeg_start_decompress",       (void **) &qjpeg_start_decompress},
445         {"jpeg_std_error",                      (void **) &qjpeg_std_error},
446         {"jpeg_write_scanlines",        (void **) &qjpeg_write_scanlines},
447         {NULL, NULL}
448 };
449
450 // Handle for JPEG DLL
451 dllhandle_t jpeg_dll = NULL;
452 qboolean jpeg_tried_loading = 0;
453 #endif
454
455 static unsigned char jpeg_eoi_marker [2] = {0xFF, JPEG_EOI};
456 static jmp_buf error_in_jpeg;
457 static qboolean jpeg_toolarge;
458
459 // Our own output manager for JPEG compression
460 typedef struct
461 {
462         struct jpeg_destination_mgr pub;
463
464         qfile_t* outfile;
465         unsigned char* buffer;
466         size_t bufsize; // used if outfile is NULL
467 } my_destination_mgr;
468 typedef my_destination_mgr* my_dest_ptr;
469
470
471 /*
472 =================================================================
473
474   DLL load & unload
475
476 =================================================================
477 */
478
479 /*
480 ====================
481 JPEG_OpenLibrary
482
483 Try to load the JPEG DLL
484 ====================
485 */
486 qboolean JPEG_OpenLibrary (void)
487 {
488 #ifdef LINK_TO_LIBJPEG
489         return true;
490 #else
491         const char* dllnames [] =
492         {
493 #if defined(WIN64)
494                 "libjpeg64.dll",
495 #elif defined(WIN32)
496                 "libjpeg.dll",
497 #elif defined(MACOSX)
498                 "libjpeg.62.dylib",
499 #else
500                 "libjpeg.so.62",
501                 "libjpeg.so",
502 #endif
503                 NULL
504         };
505
506         // Already loaded?
507         if (jpeg_dll)
508                 return true;
509
510         if (jpeg_tried_loading) // only try once
511                 return false;
512
513         jpeg_tried_loading = true;
514
515         // Load the DLL
516         return Sys_LoadLibrary (dllnames, &jpeg_dll, jpegfuncs);
517 #endif
518 }
519
520
521 /*
522 ====================
523 JPEG_CloseLibrary
524
525 Unload the JPEG DLL
526 ====================
527 */
528 void JPEG_CloseLibrary (void)
529 {
530 #ifndef LINK_TO_LIBJPEG
531         Sys_UnloadLibrary (&jpeg_dll);
532         jpeg_tried_loading = false; // allow retry
533 #endif
534 }
535
536
537 /*
538 =================================================================
539
540         JPEG decompression
541
542 =================================================================
543 */
544
545 static void JPEG_Noop (j_decompress_ptr cinfo) {}
546
547 static jboolean JPEG_FillInputBuffer (j_decompress_ptr cinfo)
548 {
549     // Insert a fake EOI marker
550     cinfo->src->next_input_byte = jpeg_eoi_marker;
551     cinfo->src->bytes_in_buffer = 2;
552
553         return TRUE;
554 }
555
556 static void JPEG_SkipInputData (j_decompress_ptr cinfo, long num_bytes)
557 {
558     if (cinfo->src->bytes_in_buffer <= (unsigned long)num_bytes)
559         {
560                 cinfo->src->bytes_in_buffer = 0;
561                 return;
562         }
563
564     cinfo->src->next_input_byte += num_bytes;
565     cinfo->src->bytes_in_buffer -= num_bytes;
566 }
567
568 static void JPEG_MemSrc (j_decompress_ptr cinfo, const unsigned char *buffer, size_t filesize)
569 {
570         cinfo->src = (struct jpeg_source_mgr *)cinfo->mem->alloc_small ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (struct jpeg_source_mgr));
571
572         cinfo->src->next_input_byte = buffer;
573         cinfo->src->bytes_in_buffer = filesize;
574
575         cinfo->src->init_source = JPEG_Noop;
576         cinfo->src->fill_input_buffer = JPEG_FillInputBuffer;
577         cinfo->src->skip_input_data = JPEG_SkipInputData;
578         cinfo->src->resync_to_restart = qjpeg_resync_to_restart; // use the default method
579         cinfo->src->term_source = JPEG_Noop;
580 }
581
582 static void JPEG_ErrorExit (j_common_ptr cinfo)
583 {
584         ((struct jpeg_decompress_struct*)cinfo)->err->output_message (cinfo);
585         longjmp(error_in_jpeg, 1);
586 }
587
588
589 /*
590 ====================
591 JPEG_LoadImage
592
593 Load a JPEG image into a BGRA buffer
594 ====================
595 */
596 unsigned char* JPEG_LoadImage_BGRA (const unsigned char *f, int filesize)
597 {
598         struct jpeg_decompress_struct cinfo;
599         struct jpeg_error_mgr jerr;
600         unsigned char *image_buffer = NULL, *scanline = NULL;
601         unsigned int line;
602
603         // No DLL = no JPEGs
604         if (!jpeg_dll)
605                 return NULL;
606
607         cinfo.err = qjpeg_std_error (&jerr);
608         qjpeg_create_decompress (&cinfo);
609         if(setjmp(error_in_jpeg))
610                 goto error_caught;
611         cinfo.err = qjpeg_std_error (&jerr);
612         cinfo.err->error_exit = JPEG_ErrorExit;
613         JPEG_MemSrc (&cinfo, f, filesize);
614         qjpeg_read_header (&cinfo, TRUE);
615         qjpeg_start_decompress (&cinfo);
616
617         image_width = cinfo.image_width;
618         image_height = cinfo.image_height;
619
620         if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
621         {
622                 Con_Printf("JPEG_LoadImage: invalid image size %ix%i\n", image_width, image_height);
623                 return NULL;
624         }
625
626         image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
627         scanline = (unsigned char *)Mem_Alloc(tempmempool, image_width * cinfo.output_components);
628         if (!image_buffer || !scanline)
629         {
630                 if (image_buffer)
631                         Mem_Free (image_buffer);
632                 if (scanline)
633                         Mem_Free (scanline);
634
635                 Con_Printf("JPEG_LoadImage: not enough memory for %i by %i image\n", image_width, image_height);
636                 qjpeg_finish_decompress (&cinfo);
637                 qjpeg_destroy_decompress (&cinfo);
638                 return NULL;
639         }
640
641         // Decompress the image, line by line
642         line = 0;
643         while (cinfo.output_scanline < cinfo.output_height)
644         {
645                 unsigned char *buffer_ptr;
646                 int ind;
647
648                 qjpeg_read_scanlines (&cinfo, &scanline, 1);
649
650                 // Convert the image to BGRA
651                 switch (cinfo.output_components)
652                 {
653                         // RGB images
654                         case 3:
655                                 buffer_ptr = &image_buffer[image_width * line * 4];
656                                 for (ind = 0; ind < image_width * 3; ind += 3, buffer_ptr += 4)
657                                 {
658                                         buffer_ptr[2] = scanline[ind];
659                                         buffer_ptr[1] = scanline[ind + 1];
660                                         buffer_ptr[0] = scanline[ind + 2];
661                                         buffer_ptr[3] = 255;
662                                 }
663                                 break;
664
665                         // Greyscale images (default to it, just in case)
666                         case 1:
667                         default:
668                                 buffer_ptr = &image_buffer[image_width * line * 4];
669                                 for (ind = 0; ind < image_width; ind++, buffer_ptr += 4)
670                                 {
671                                         buffer_ptr[0] = scanline[ind];
672                                         buffer_ptr[1] = scanline[ind];
673                                         buffer_ptr[2] = scanline[ind];
674                                         buffer_ptr[3] = 255;
675                                 }
676                 }
677
678                 line++;
679         }
680         Mem_Free (scanline);
681
682         qjpeg_finish_decompress (&cinfo);
683         qjpeg_destroy_decompress (&cinfo);
684
685         return image_buffer;
686
687 error_caught:
688         if(scanline)
689                 Mem_Free (scanline);
690         if(image_buffer)
691                 Mem_Free (image_buffer);
692         qjpeg_destroy_decompress (&cinfo);
693         return NULL;
694 }
695
696
697 /*
698 =================================================================
699
700   JPEG compression
701
702 =================================================================
703 */
704
705 #define JPEG_OUTPUT_BUF_SIZE 4096
706 static void JPEG_InitDestination (j_compress_ptr cinfo)
707 {
708         my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
709         dest->buffer = (unsigned char*)cinfo->mem->alloc_small ((j_common_ptr) cinfo, JPOOL_IMAGE, JPEG_OUTPUT_BUF_SIZE * sizeof(unsigned char));
710         dest->pub.next_output_byte = dest->buffer;
711         dest->pub.free_in_buffer = JPEG_OUTPUT_BUF_SIZE;
712 }
713
714 static jboolean JPEG_EmptyOutputBuffer (j_compress_ptr cinfo)
715 {
716         my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
717
718         if (FS_Write (dest->outfile, dest->buffer, JPEG_OUTPUT_BUF_SIZE) != (size_t) JPEG_OUTPUT_BUF_SIZE)
719                 longjmp(error_in_jpeg, 1);
720
721         dest->pub.next_output_byte = dest->buffer;
722         dest->pub.free_in_buffer = JPEG_OUTPUT_BUF_SIZE;
723         return true;
724 }
725
726 static void JPEG_TermDestination (j_compress_ptr cinfo)
727 {
728         my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
729         size_t datacount = JPEG_OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
730
731         // Write any data remaining in the buffer
732         if (datacount > 0)
733                 if (FS_Write (dest->outfile, dest->buffer, datacount) != (fs_offset_t)datacount)
734                         longjmp(error_in_jpeg, 1);
735 }
736
737 static void JPEG_FileDest (j_compress_ptr cinfo, qfile_t* outfile)
738 {
739         my_dest_ptr dest;
740
741         // First time for this JPEG object?
742         if (cinfo->dest == NULL)
743                 cinfo->dest = (struct jpeg_destination_mgr *)(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(my_destination_mgr));
744
745         dest = (my_dest_ptr)cinfo->dest;
746         dest->pub.init_destination = JPEG_InitDestination;
747         dest->pub.empty_output_buffer = JPEG_EmptyOutputBuffer;
748         dest->pub.term_destination = JPEG_TermDestination;
749         dest->outfile = outfile;
750 }
751
752 static void JPEG_Mem_InitDestination (j_compress_ptr cinfo)
753 {
754         my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
755         dest->pub.next_output_byte = dest->buffer;
756         dest->pub.free_in_buffer = dest->bufsize;
757 }
758
759 static jboolean JPEG_Mem_EmptyOutputBuffer (j_compress_ptr cinfo)
760 {
761         my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
762         jpeg_toolarge = true;
763         dest->pub.next_output_byte = dest->buffer;
764         dest->pub.free_in_buffer = dest->bufsize;
765         return true;
766 }
767
768 static void JPEG_Mem_TermDestination (j_compress_ptr cinfo)
769 {
770         my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
771         dest->bufsize = dest->pub.next_output_byte - dest->buffer;
772 }
773 static void JPEG_MemDest (j_compress_ptr cinfo, void* buf, size_t bufsize)
774 {
775         my_dest_ptr dest;
776
777         // First time for this JPEG object?
778         if (cinfo->dest == NULL)
779                 cinfo->dest = (struct jpeg_destination_mgr *)(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(my_destination_mgr));
780
781         dest = (my_dest_ptr)cinfo->dest;
782         dest->pub.init_destination = JPEG_Mem_InitDestination;
783         dest->pub.empty_output_buffer = JPEG_Mem_EmptyOutputBuffer;
784         dest->pub.term_destination = JPEG_Mem_TermDestination;
785         dest->outfile = NULL;
786
787         dest->buffer = (unsigned char *) buf;
788         dest->bufsize = bufsize;
789 }
790
791
792 /*
793 ====================
794 JPEG_SaveImage_preflipped
795
796 Save a preflipped JPEG image to a file
797 ====================
798 */
799 qboolean JPEG_SaveImage_preflipped (const char *filename, int width, int height, unsigned char *data)
800 {
801         struct jpeg_compress_struct cinfo;
802         struct jpeg_error_mgr jerr;
803         unsigned char *scanline;
804         unsigned int offset, linesize;
805         qfile_t* file;
806
807         // No DLL = no JPEGs
808         if (!jpeg_dll)
809         {
810                 Con_Print("You need the libjpeg library to save JPEG images\n");
811                 return false;
812         }
813
814         // Open the file
815         file = FS_OpenRealFile(filename, "wb", true);
816         if (!file)
817                 return false;
818
819         if(setjmp(error_in_jpeg))
820                 goto error_caught;
821         cinfo.err = qjpeg_std_error (&jerr);
822         cinfo.err->error_exit = JPEG_ErrorExit;
823
824         qjpeg_create_compress (&cinfo);
825         JPEG_FileDest (&cinfo, file);
826
827         // Set the parameters for compression
828         cinfo.image_width = width;
829         cinfo.image_height = height;
830         cinfo.in_color_space = JCS_RGB;
831         cinfo.input_components = 3;
832         qjpeg_set_defaults (&cinfo);
833         qjpeg_set_quality (&cinfo, (int)(scr_screenshot_jpeg_quality.value * 100), TRUE);
834
835         // turn off subsampling (to make text look better)
836         cinfo.optimize_coding = 1;
837         cinfo.comp_info[0].h_samp_factor = 1;
838         cinfo.comp_info[0].v_samp_factor = 1;
839         cinfo.comp_info[1].h_samp_factor = 1;
840         cinfo.comp_info[1].v_samp_factor = 1;
841         cinfo.comp_info[2].h_samp_factor = 1;
842         cinfo.comp_info[2].v_samp_factor = 1;
843
844         qjpeg_start_compress (&cinfo, true);
845
846         // Compress each scanline
847         linesize = cinfo.image_width * 3;
848         offset = linesize * (cinfo.image_height - 1);
849         while (cinfo.next_scanline < cinfo.image_height)
850         {
851                 scanline = &data[offset - cinfo.next_scanline * linesize];
852
853                 qjpeg_write_scanlines (&cinfo, &scanline, 1);
854         }
855
856         qjpeg_finish_compress (&cinfo);
857         qjpeg_destroy_compress (&cinfo);
858
859         FS_Close (file);
860         return true;
861
862 error_caught:
863         qjpeg_destroy_compress (&cinfo);
864         FS_Close (file);
865         return false;
866 }
867
868 static size_t JPEG_try_SaveImage_to_Buffer (struct jpeg_compress_struct *cinfo, char *jpegbuf, size_t jpegsize, int quality, int width, int height, unsigned char *data)
869 {
870         unsigned char *scanline;
871         unsigned int linesize;
872
873         jpeg_toolarge = false;
874         JPEG_MemDest (cinfo, jpegbuf, jpegsize);
875
876         // Set the parameters for compression
877         cinfo->image_width = width;
878         cinfo->image_height = height;
879         cinfo->in_color_space = JCS_RGB;
880         cinfo->input_components = 3;
881         qjpeg_set_defaults (cinfo);
882         qjpeg_set_quality (cinfo, quality, FALSE);
883
884         cinfo->comp_info[0].h_samp_factor = 2;
885         cinfo->comp_info[0].v_samp_factor = 2;
886         cinfo->comp_info[1].h_samp_factor = 1;
887         cinfo->comp_info[1].v_samp_factor = 1;
888         cinfo->comp_info[2].h_samp_factor = 1;
889         cinfo->comp_info[2].v_samp_factor = 1;
890         cinfo->optimize_coding = 1;
891
892         qjpeg_start_compress (cinfo, true);
893
894         // Compress each scanline
895         linesize = width * 3;
896         while (cinfo->next_scanline < cinfo->image_height)
897         {
898                 scanline = &data[cinfo->next_scanline * linesize];
899
900                 qjpeg_write_scanlines (cinfo, &scanline, 1);
901         }
902
903         qjpeg_finish_compress (cinfo);
904
905         if(jpeg_toolarge)
906                 return 0;
907
908         return ((my_dest_ptr) cinfo->dest)->bufsize;
909 }
910
911 size_t JPEG_SaveImage_to_Buffer (char *jpegbuf, size_t jpegsize, int width, int height, unsigned char *data)
912 {
913         struct jpeg_compress_struct cinfo;
914         struct jpeg_error_mgr jerr;
915
916         int quality;
917         int quality_guess;
918         size_t result;
919
920         // No DLL = no JPEGs
921         if (!jpeg_dll)
922         {
923                 Con_Print("You need the libjpeg library to save JPEG images\n");
924                 return false;
925         }
926
927         if(setjmp(error_in_jpeg))
928                 goto error_caught;
929         cinfo.err = qjpeg_std_error (&jerr);
930         cinfo.err->error_exit = JPEG_ErrorExit;
931
932         qjpeg_create_compress (&cinfo);
933
934 #if 0
935         // used to get the formula below
936         {
937                 char buf[1048576];
938                 unsigned char *img;
939                 int i;
940
941                 img = Mem_Alloc(tempmempool, width * height * 3);
942                 for(i = 0; i < width * height * 3; ++i)
943                         img[i] = rand() & 0xFF;
944
945                 for(i = 0; i <= 100; ++i)
946                 {
947                         Con_Printf("! %d %d %d %d\n", width, height, i, (int) JPEG_try_SaveImage_to_Buffer(&cinfo, buf, sizeof(buf), i, width, height, img));
948                 }
949
950                 Mem_Free(img);
951         }
952 #endif
953
954         //quality_guess = (100 * jpegsize - 41000) / (width*height) + 2; // fits random data
955         quality_guess   = (256 * jpegsize - 81920) / (width*height) - 8; // fits Nexuiz's map pictures
956
957         quality_guess = bound(0, quality_guess, 100);
958         quality = bound(0, quality_guess + sv_writepicture_quality.integer, 100); // assume it can do 10 failed attempts
959
960         while(!(result = JPEG_try_SaveImage_to_Buffer(&cinfo, jpegbuf, jpegsize, quality, width, height, data)))
961         {
962                 --quality;
963                 if(quality < 0)
964                 {
965                         Con_Printf("couldn't write image at all, probably too big\n");
966                         return 0;
967                 }
968         }
969         qjpeg_destroy_compress (&cinfo);
970         Con_DPrintf("JPEG_SaveImage_to_Buffer: guessed quality/size %d/%d, actually got %d/%d\n", quality_guess, (int)jpegsize, quality, (int)result);
971
972         return result;
973
974 error_caught:
975         qjpeg_destroy_compress (&cinfo);
976         return 0;
977 }
978
979 typedef struct CompressedImageCacheItem
980 {
981         char imagename[MAX_QPATH];
982         size_t maxsize;
983         void *compressed;
984         size_t compressed_size;
985         struct CompressedImageCacheItem *next;
986 }
987 CompressedImageCacheItem;
988 #define COMPRESSEDIMAGECACHE_SIZE 4096
989 static CompressedImageCacheItem *CompressedImageCache[COMPRESSEDIMAGECACHE_SIZE];
990
991 static void CompressedImageCache_Add(const char *imagename, size_t maxsize, void *compressed, size_t compressed_size)
992 {
993         const char *hashkey = va("%s:%d", imagename, (int) maxsize);
994         int hashindex = CRC_Block((unsigned char *) hashkey, strlen(hashkey)) % COMPRESSEDIMAGECACHE_SIZE;
995         CompressedImageCacheItem *i;
996
997         if(strlen(imagename) >= MAX_QPATH)
998                 return; // can't add this
999         
1000         i = (CompressedImageCacheItem*) Z_Malloc(sizeof(CompressedImageCacheItem));
1001         strlcpy(i->imagename, imagename, sizeof(i->imagename));
1002         i->maxsize = maxsize;
1003         i->compressed = compressed;
1004         i->compressed_size = compressed_size;
1005         i->next = CompressedImageCache[hashindex];
1006         CompressedImageCache[hashindex] = i;
1007 }
1008
1009 static CompressedImageCacheItem *CompressedImageCache_Find(const char *imagename, size_t maxsize)
1010 {
1011         const char *hashkey = va("%s:%d", imagename, (int) maxsize);
1012         int hashindex = CRC_Block((unsigned char *) hashkey, strlen(hashkey)) % COMPRESSEDIMAGECACHE_SIZE;
1013         CompressedImageCacheItem *i = CompressedImageCache[hashindex];
1014
1015         while(i)
1016         {
1017                 if(i->maxsize == maxsize)
1018                         if(!strcmp(i->imagename, imagename))
1019                                 return i;
1020                 i = i->next;
1021         }
1022         return NULL;
1023 }
1024
1025 qboolean Image_Compress(const char *imagename, size_t maxsize, void **buf, size_t *size)
1026 {
1027         unsigned char *imagedata, *newimagedata;
1028         int maxPixelCount;
1029         int components[3] = {2, 1, 0};
1030         CompressedImageCacheItem *i;
1031
1032         JPEG_OpenLibrary (); // for now; LH had the idea of replacing this by a better format
1033
1034         // No DLL = no JPEGs
1035         if (!jpeg_dll)
1036         {
1037                 Con_Print("You need the libjpeg library to save JPEG images\n");
1038                 return false;
1039         }
1040
1041         i = CompressedImageCache_Find(imagename, maxsize);
1042         if(i)
1043         {
1044                 *size = i->compressed_size;
1045                 *buf = i->compressed;
1046         }
1047
1048         // load the image
1049         imagedata = loadimagepixelsbgra(imagename, true, false);
1050         if(!imagedata)
1051                 return false;
1052
1053         // find an appropriate size for somewhat okay compression
1054         if(maxsize <= 768)
1055                 maxPixelCount = 32 * 32;
1056         else if(maxsize <= 1024)
1057                 maxPixelCount = 64 * 64;
1058         else if(maxsize <= 4096)
1059                 maxPixelCount = 128 * 128;
1060         else
1061                 maxPixelCount = 256 * 256;
1062
1063         while(image_width * image_height > maxPixelCount)
1064         {
1065                 int one = 1;
1066                 Image_MipReduce32(imagedata, imagedata, &image_width, &image_height, &one, image_width/2, image_height/2, 1);
1067         }
1068
1069         newimagedata = (unsigned char *) Mem_Alloc(tempmempool, image_width * image_height * 3);
1070
1071         // convert the image from BGRA to RGB
1072         Image_CopyMux(newimagedata, imagedata, image_width, image_height, false, false, false, 3, 4, components);
1073         Mem_Free(imagedata);
1074
1075         // try to compress it to JPEG
1076         *buf = Z_Malloc(maxsize);
1077         *size = JPEG_SaveImage_to_Buffer((char *) *buf, maxsize, image_width, image_height, newimagedata);
1078         Mem_Free(newimagedata);
1079
1080         if(!*size)
1081         {
1082                 Z_Free(*buf);
1083                 *buf = NULL;
1084                 Con_Printf("could not compress image %s to %d bytes\n", imagename, (int)maxsize);
1085                 // return false;
1086                 // also cache failures!
1087         }
1088
1089         // store it in the cache
1090         CompressedImageCache_Add(imagename, maxsize, *buf, *size);
1091         return (*buf != NULL);
1092 }