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