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