change a few WIN64 checks to _WIN64 (which actually works), and remove
[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 (*_reserve_space_for_alloc_large) (void *dummy, ...);
106   void (*_reserve_space_for_alloc_sarray) (void *dummy, ...);
107   void (*_reserve_space_for_alloc_barray) (void *dummy, ...);
108   void (*_reserve_space_for_request_virt_sarray) (void *dummy, ...);
109   void (*_reserve_space_for_request_virt_barray) (void *dummy, ...);
110   void (*_reserve_space_for_realize_virt_arrays) (void *dummy, ...);
111   void (*_reserve_space_for_access_virt_sarray) (void *dummy, ...);
112   void (*_reserve_space_for_access_virt_barray) (void *dummy, ...);
113   void (*_reserve_space_for_free_pool) (void *dummy, ...);
114   void (*_reserve_space_for_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(WIN32)
494                 "libjpeg.dll",
495 #elif defined(MACOSX)
496                 "libjpeg.62.dylib",
497 #else
498                 "libjpeg.so.62",
499                 "libjpeg.so",
500 #endif
501                 NULL
502         };
503
504         // Already loaded?
505         if (jpeg_dll)
506                 return true;
507
508         if (jpeg_tried_loading) // only try once
509                 return false;
510
511         jpeg_tried_loading = true;
512
513         // Load the DLL
514         return Sys_LoadLibrary (dllnames, &jpeg_dll, jpegfuncs);
515 #endif
516 }
517
518
519 /*
520 ====================
521 JPEG_CloseLibrary
522
523 Unload the JPEG DLL
524 ====================
525 */
526 void JPEG_CloseLibrary (void)
527 {
528 #ifndef LINK_TO_LIBJPEG
529         Sys_UnloadLibrary (&jpeg_dll);
530         jpeg_tried_loading = false; // allow retry
531 #endif
532 }
533
534
535 /*
536 =================================================================
537
538         JPEG decompression
539
540 =================================================================
541 */
542
543 static void JPEG_Noop (j_decompress_ptr cinfo) {}
544
545 static jboolean JPEG_FillInputBuffer (j_decompress_ptr cinfo)
546 {
547     // Insert a fake EOI marker
548     cinfo->src->next_input_byte = jpeg_eoi_marker;
549     cinfo->src->bytes_in_buffer = 2;
550
551         return TRUE;
552 }
553
554 static void JPEG_SkipInputData (j_decompress_ptr cinfo, long num_bytes)
555 {
556     if (cinfo->src->bytes_in_buffer <= (unsigned long)num_bytes)
557         {
558                 cinfo->src->bytes_in_buffer = 0;
559                 return;
560         }
561
562     cinfo->src->next_input_byte += num_bytes;
563     cinfo->src->bytes_in_buffer -= num_bytes;
564 }
565
566 static void JPEG_MemSrc (j_decompress_ptr cinfo, const unsigned char *buffer, size_t filesize)
567 {
568         cinfo->src = (struct jpeg_source_mgr *)cinfo->mem->alloc_small ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (struct jpeg_source_mgr));
569
570         cinfo->src->next_input_byte = buffer;
571         cinfo->src->bytes_in_buffer = filesize;
572
573         cinfo->src->init_source = JPEG_Noop;
574         cinfo->src->fill_input_buffer = JPEG_FillInputBuffer;
575         cinfo->src->skip_input_data = JPEG_SkipInputData;
576         cinfo->src->resync_to_restart = qjpeg_resync_to_restart; // use the default method
577         cinfo->src->term_source = JPEG_Noop;
578 }
579
580 static void JPEG_ErrorExit (j_common_ptr cinfo)
581 {
582         ((struct jpeg_decompress_struct*)cinfo)->err->output_message (cinfo);
583         longjmp(error_in_jpeg, 1);
584 }
585
586
587 /*
588 ====================
589 JPEG_LoadImage
590
591 Load a JPEG image into a BGRA buffer
592 ====================
593 */
594 unsigned char* JPEG_LoadImage_BGRA (const unsigned char *f, int filesize)
595 {
596         struct jpeg_decompress_struct cinfo;
597         struct jpeg_error_mgr jerr;
598         unsigned char *image_buffer = NULL, *scanline = NULL;
599         unsigned int line;
600
601         // No DLL = no JPEGs
602         if (!jpeg_dll)
603                 return NULL;
604
605         cinfo.err = qjpeg_std_error (&jerr);
606         qjpeg_create_decompress (&cinfo);
607         if(setjmp(error_in_jpeg))
608                 goto error_caught;
609         cinfo.err = qjpeg_std_error (&jerr);
610         cinfo.err->error_exit = JPEG_ErrorExit;
611         JPEG_MemSrc (&cinfo, f, filesize);
612         qjpeg_read_header (&cinfo, TRUE);
613         qjpeg_start_decompress (&cinfo);
614
615         image_width = cinfo.image_width;
616         image_height = cinfo.image_height;
617
618         if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
619         {
620                 Con_Printf("JPEG_LoadImage: invalid image size %ix%i\n", image_width, image_height);
621                 return NULL;
622         }
623
624         image_buffer = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
625         scanline = (unsigned char *)Mem_Alloc(tempmempool, image_width * cinfo.output_components);
626         if (!image_buffer || !scanline)
627         {
628                 if (image_buffer)
629                         Mem_Free (image_buffer);
630                 if (scanline)
631                         Mem_Free (scanline);
632
633                 Con_Printf("JPEG_LoadImage: not enough memory for %i by %i image\n", image_width, image_height);
634                 qjpeg_finish_decompress (&cinfo);
635                 qjpeg_destroy_decompress (&cinfo);
636                 return NULL;
637         }
638
639         // Decompress the image, line by line
640         line = 0;
641         while (cinfo.output_scanline < cinfo.output_height)
642         {
643                 unsigned char *buffer_ptr;
644                 int ind;
645
646                 qjpeg_read_scanlines (&cinfo, &scanline, 1);
647
648                 // Convert the image to BGRA
649                 switch (cinfo.output_components)
650                 {
651                         // RGB images
652                         case 3:
653                                 buffer_ptr = &image_buffer[image_width * line * 4];
654                                 for (ind = 0; ind < image_width * 3; ind += 3, buffer_ptr += 4)
655                                 {
656                                         buffer_ptr[2] = scanline[ind];
657                                         buffer_ptr[1] = scanline[ind + 1];
658                                         buffer_ptr[0] = scanline[ind + 2];
659                                         buffer_ptr[3] = 255;
660                                 }
661                                 break;
662
663                         // Greyscale images (default to it, just in case)
664                         case 1:
665                         default:
666                                 buffer_ptr = &image_buffer[image_width * line * 4];
667                                 for (ind = 0; ind < image_width; ind++, buffer_ptr += 4)
668                                 {
669                                         buffer_ptr[0] = scanline[ind];
670                                         buffer_ptr[1] = scanline[ind];
671                                         buffer_ptr[2] = scanline[ind];
672                                         buffer_ptr[3] = 255;
673                                 }
674                 }
675
676                 line++;
677         }
678         Mem_Free (scanline);
679
680         qjpeg_finish_decompress (&cinfo);
681         qjpeg_destroy_decompress (&cinfo);
682
683         return image_buffer;
684
685 error_caught:
686         if(scanline)
687                 Mem_Free (scanline);
688         if(image_buffer)
689                 Mem_Free (image_buffer);
690         qjpeg_destroy_decompress (&cinfo);
691         return NULL;
692 }
693
694
695 /*
696 =================================================================
697
698   JPEG compression
699
700 =================================================================
701 */
702
703 #define JPEG_OUTPUT_BUF_SIZE 4096
704 static void JPEG_InitDestination (j_compress_ptr cinfo)
705 {
706         my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
707         dest->buffer = (unsigned char*)cinfo->mem->alloc_small ((j_common_ptr) cinfo, JPOOL_IMAGE, JPEG_OUTPUT_BUF_SIZE * sizeof(unsigned char));
708         dest->pub.next_output_byte = dest->buffer;
709         dest->pub.free_in_buffer = JPEG_OUTPUT_BUF_SIZE;
710 }
711
712 static jboolean JPEG_EmptyOutputBuffer (j_compress_ptr cinfo)
713 {
714         my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
715
716         if (FS_Write (dest->outfile, dest->buffer, JPEG_OUTPUT_BUF_SIZE) != (size_t) JPEG_OUTPUT_BUF_SIZE)
717                 longjmp(error_in_jpeg, 1);
718
719         dest->pub.next_output_byte = dest->buffer;
720         dest->pub.free_in_buffer = JPEG_OUTPUT_BUF_SIZE;
721         return true;
722 }
723
724 static void JPEG_TermDestination (j_compress_ptr cinfo)
725 {
726         my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
727         size_t datacount = JPEG_OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
728
729         // Write any data remaining in the buffer
730         if (datacount > 0)
731                 if (FS_Write (dest->outfile, dest->buffer, datacount) != (fs_offset_t)datacount)
732                         longjmp(error_in_jpeg, 1);
733 }
734
735 static void JPEG_FileDest (j_compress_ptr cinfo, qfile_t* outfile)
736 {
737         my_dest_ptr dest;
738
739         // First time for this JPEG object?
740         if (cinfo->dest == NULL)
741                 cinfo->dest = (struct jpeg_destination_mgr *)(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(my_destination_mgr));
742
743         dest = (my_dest_ptr)cinfo->dest;
744         dest->pub.init_destination = JPEG_InitDestination;
745         dest->pub.empty_output_buffer = JPEG_EmptyOutputBuffer;
746         dest->pub.term_destination = JPEG_TermDestination;
747         dest->outfile = outfile;
748 }
749
750 static void JPEG_Mem_InitDestination (j_compress_ptr cinfo)
751 {
752         my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
753         dest->pub.next_output_byte = dest->buffer;
754         dest->pub.free_in_buffer = dest->bufsize;
755 }
756
757 static jboolean JPEG_Mem_EmptyOutputBuffer (j_compress_ptr cinfo)
758 {
759         my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
760         jpeg_toolarge = true;
761         dest->pub.next_output_byte = dest->buffer;
762         dest->pub.free_in_buffer = dest->bufsize;
763         return true;
764 }
765
766 static void JPEG_Mem_TermDestination (j_compress_ptr cinfo)
767 {
768         my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
769         dest->bufsize = dest->pub.next_output_byte - dest->buffer;
770 }
771 static void JPEG_MemDest (j_compress_ptr cinfo, void* buf, size_t bufsize)
772 {
773         my_dest_ptr dest;
774
775         // First time for this JPEG object?
776         if (cinfo->dest == NULL)
777                 cinfo->dest = (struct jpeg_destination_mgr *)(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(my_destination_mgr));
778
779         dest = (my_dest_ptr)cinfo->dest;
780         dest->pub.init_destination = JPEG_Mem_InitDestination;
781         dest->pub.empty_output_buffer = JPEG_Mem_EmptyOutputBuffer;
782         dest->pub.term_destination = JPEG_Mem_TermDestination;
783         dest->outfile = NULL;
784
785         dest->buffer = (unsigned char *) buf;
786         dest->bufsize = bufsize;
787 }
788
789
790 /*
791 ====================
792 JPEG_SaveImage_preflipped
793
794 Save a preflipped JPEG image to a file
795 ====================
796 */
797 qboolean JPEG_SaveImage_preflipped (const char *filename, int width, int height, unsigned char *data)
798 {
799         struct jpeg_compress_struct cinfo;
800         struct jpeg_error_mgr jerr;
801         unsigned char *scanline;
802         unsigned int offset, linesize;
803         qfile_t* file;
804
805         // No DLL = no JPEGs
806         if (!jpeg_dll)
807         {
808                 Con_Print("You need the libjpeg library to save JPEG images\n");
809                 return false;
810         }
811
812         // Open the file
813         file = FS_OpenRealFile(filename, "wb", true);
814         if (!file)
815                 return false;
816
817         if(setjmp(error_in_jpeg))
818                 goto error_caught;
819         cinfo.err = qjpeg_std_error (&jerr);
820         cinfo.err->error_exit = JPEG_ErrorExit;
821
822         qjpeg_create_compress (&cinfo);
823         JPEG_FileDest (&cinfo, file);
824
825         // Set the parameters for compression
826         cinfo.image_width = width;
827         cinfo.image_height = height;
828         cinfo.in_color_space = JCS_RGB;
829         cinfo.input_components = 3;
830         qjpeg_set_defaults (&cinfo);
831         qjpeg_set_quality (&cinfo, (int)(scr_screenshot_jpeg_quality.value * 100), TRUE);
832
833         // turn off subsampling (to make text look better)
834         cinfo.optimize_coding = 1;
835         cinfo.comp_info[0].h_samp_factor = 1;
836         cinfo.comp_info[0].v_samp_factor = 1;
837         cinfo.comp_info[1].h_samp_factor = 1;
838         cinfo.comp_info[1].v_samp_factor = 1;
839         cinfo.comp_info[2].h_samp_factor = 1;
840         cinfo.comp_info[2].v_samp_factor = 1;
841
842         qjpeg_start_compress (&cinfo, true);
843
844         // Compress each scanline
845         linesize = cinfo.image_width * 3;
846         offset = linesize * (cinfo.image_height - 1);
847         while (cinfo.next_scanline < cinfo.image_height)
848         {
849                 scanline = &data[offset - cinfo.next_scanline * linesize];
850
851                 qjpeg_write_scanlines (&cinfo, &scanline, 1);
852         }
853
854         qjpeg_finish_compress (&cinfo);
855         qjpeg_destroy_compress (&cinfo);
856
857         FS_Close (file);
858         return true;
859
860 error_caught:
861         qjpeg_destroy_compress (&cinfo);
862         FS_Close (file);
863         return false;
864 }
865
866 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)
867 {
868         unsigned char *scanline;
869         unsigned int linesize;
870
871         jpeg_toolarge = false;
872         JPEG_MemDest (cinfo, jpegbuf, jpegsize);
873
874         // Set the parameters for compression
875         cinfo->image_width = width;
876         cinfo->image_height = height;
877         cinfo->in_color_space = JCS_RGB;
878         cinfo->input_components = 3;
879         qjpeg_set_defaults (cinfo);
880         qjpeg_set_quality (cinfo, quality, FALSE);
881
882         cinfo->comp_info[0].h_samp_factor = 2;
883         cinfo->comp_info[0].v_samp_factor = 2;
884         cinfo->comp_info[1].h_samp_factor = 1;
885         cinfo->comp_info[1].v_samp_factor = 1;
886         cinfo->comp_info[2].h_samp_factor = 1;
887         cinfo->comp_info[2].v_samp_factor = 1;
888         cinfo->optimize_coding = 1;
889
890         qjpeg_start_compress (cinfo, true);
891
892         // Compress each scanline
893         linesize = width * 3;
894         while (cinfo->next_scanline < cinfo->image_height)
895         {
896                 scanline = &data[cinfo->next_scanline * linesize];
897
898                 qjpeg_write_scanlines (cinfo, &scanline, 1);
899         }
900
901         qjpeg_finish_compress (cinfo);
902
903         if(jpeg_toolarge)
904                 return 0;
905
906         return ((my_dest_ptr) cinfo->dest)->bufsize;
907 }
908
909 size_t JPEG_SaveImage_to_Buffer (char *jpegbuf, size_t jpegsize, int width, int height, unsigned char *data)
910 {
911         struct jpeg_compress_struct cinfo;
912         struct jpeg_error_mgr jerr;
913
914         int quality;
915         int quality_guess;
916         size_t result;
917
918         // No DLL = no JPEGs
919         if (!jpeg_dll)
920         {
921                 Con_Print("You need the libjpeg library to save JPEG images\n");
922                 return false;
923         }
924
925         if(setjmp(error_in_jpeg))
926                 goto error_caught;
927         cinfo.err = qjpeg_std_error (&jerr);
928         cinfo.err->error_exit = JPEG_ErrorExit;
929
930         qjpeg_create_compress (&cinfo);
931
932 #if 0
933         // used to get the formula below
934         {
935                 char buf[1048576];
936                 unsigned char *img;
937                 int i;
938
939                 img = Mem_Alloc(tempmempool, width * height * 3);
940                 for(i = 0; i < width * height * 3; ++i)
941                         img[i] = rand() & 0xFF;
942
943                 for(i = 0; i <= 100; ++i)
944                 {
945                         Con_Printf("! %d %d %d %d\n", width, height, i, (int) JPEG_try_SaveImage_to_Buffer(&cinfo, buf, sizeof(buf), i, width, height, img));
946                 }
947
948                 Mem_Free(img);
949         }
950 #endif
951
952         //quality_guess = (100 * jpegsize - 41000) / (width*height) + 2; // fits random data
953         quality_guess   = (256 * jpegsize - 81920) / (width*height) - 8; // fits Nexuiz's map pictures
954
955         quality_guess = bound(0, quality_guess, 100);
956         quality = bound(0, quality_guess + sv_writepicture_quality.integer, 100); // assume it can do 10 failed attempts
957
958         while(!(result = JPEG_try_SaveImage_to_Buffer(&cinfo, jpegbuf, jpegsize, quality, width, height, data)))
959         {
960                 --quality;
961                 if(quality < 0)
962                 {
963                         Con_Printf("couldn't write image at all, probably too big\n");
964                         return 0;
965                 }
966         }
967         qjpeg_destroy_compress (&cinfo);
968         Con_DPrintf("JPEG_SaveImage_to_Buffer: guessed quality/size %d/%d, actually got %d/%d\n", quality_guess, (int)jpegsize, quality, (int)result);
969
970         return result;
971
972 error_caught:
973         qjpeg_destroy_compress (&cinfo);
974         return 0;
975 }
976
977 typedef struct CompressedImageCacheItem
978 {
979         char imagename[MAX_QPATH];
980         size_t maxsize;
981         void *compressed;
982         size_t compressed_size;
983         struct CompressedImageCacheItem *next;
984 }
985 CompressedImageCacheItem;
986 #define COMPRESSEDIMAGECACHE_SIZE 4096
987 static CompressedImageCacheItem *CompressedImageCache[COMPRESSEDIMAGECACHE_SIZE];
988
989 static void CompressedImageCache_Add(const char *imagename, size_t maxsize, void *compressed, size_t compressed_size)
990 {
991         const char *hashkey = va("%s:%d", imagename, (int) maxsize);
992         int hashindex = CRC_Block((unsigned char *) hashkey, strlen(hashkey)) % COMPRESSEDIMAGECACHE_SIZE;
993         CompressedImageCacheItem *i;
994
995         if(strlen(imagename) >= MAX_QPATH)
996                 return; // can't add this
997         
998         i = (CompressedImageCacheItem*) Z_Malloc(sizeof(CompressedImageCacheItem));
999         strlcpy(i->imagename, imagename, sizeof(i->imagename));
1000         i->maxsize = maxsize;
1001         i->compressed = compressed;
1002         i->compressed_size = compressed_size;
1003         i->next = CompressedImageCache[hashindex];
1004         CompressedImageCache[hashindex] = i;
1005 }
1006
1007 static CompressedImageCacheItem *CompressedImageCache_Find(const char *imagename, size_t maxsize)
1008 {
1009         const char *hashkey = va("%s:%d", imagename, (int) maxsize);
1010         int hashindex = CRC_Block((unsigned char *) hashkey, strlen(hashkey)) % COMPRESSEDIMAGECACHE_SIZE;
1011         CompressedImageCacheItem *i = CompressedImageCache[hashindex];
1012
1013         while(i)
1014         {
1015                 if(i->maxsize == maxsize)
1016                         if(!strcmp(i->imagename, imagename))
1017                                 return i;
1018                 i = i->next;
1019         }
1020         return NULL;
1021 }
1022
1023 qboolean Image_Compress(const char *imagename, size_t maxsize, void **buf, size_t *size)
1024 {
1025         unsigned char *imagedata, *newimagedata;
1026         int maxPixelCount;
1027         int components[3] = {2, 1, 0};
1028         CompressedImageCacheItem *i;
1029
1030         JPEG_OpenLibrary (); // for now; LH had the idea of replacing this by a better format
1031
1032         // No DLL = no JPEGs
1033         if (!jpeg_dll)
1034         {
1035                 Con_Print("You need the libjpeg library to save JPEG images\n");
1036                 return false;
1037         }
1038
1039         i = CompressedImageCache_Find(imagename, maxsize);
1040         if(i)
1041         {
1042                 *size = i->compressed_size;
1043                 *buf = i->compressed;
1044         }
1045
1046         // load the image
1047         imagedata = loadimagepixelsbgra(imagename, true, false);
1048         if(!imagedata)
1049                 return false;
1050
1051         // find an appropriate size for somewhat okay compression
1052         if(maxsize <= 768)
1053                 maxPixelCount = 32 * 32;
1054         else if(maxsize <= 1024)
1055                 maxPixelCount = 64 * 64;
1056         else if(maxsize <= 4096)
1057                 maxPixelCount = 128 * 128;
1058         else
1059                 maxPixelCount = 256 * 256;
1060
1061         while(image_width * image_height > maxPixelCount)
1062         {
1063                 int one = 1;
1064                 Image_MipReduce32(imagedata, imagedata, &image_width, &image_height, &one, image_width/2, image_height/2, 1);
1065         }
1066
1067         newimagedata = (unsigned char *) Mem_Alloc(tempmempool, image_width * image_height * 3);
1068
1069         // convert the image from BGRA to RGB
1070         Image_CopyMux(newimagedata, imagedata, image_width, image_height, false, false, false, 3, 4, components);
1071         Mem_Free(imagedata);
1072
1073         // try to compress it to JPEG
1074         *buf = Z_Malloc(maxsize);
1075         *size = JPEG_SaveImage_to_Buffer((char *) *buf, maxsize, image_width, image_height, newimagedata);
1076         Mem_Free(newimagedata);
1077
1078         if(!*size)
1079         {
1080                 Z_Free(*buf);
1081                 *buf = NULL;
1082                 Con_Printf("could not compress image %s to %d bytes\n", imagename, (int)maxsize);
1083                 // return false;
1084                 // also cache failures!
1085         }
1086
1087         // store it in the cache
1088         CompressedImageCache_Add(imagename, maxsize, *buf, *size);
1089         return (*buf != NULL);
1090 }