]> icculus.org git repositories - divverent/darkplaces.git/blob - jpeg.c
Fixed Win32 dedicated server input
[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 "jpeg.h"
27
28
29 /*
30 =================================================================
31
32   Minimal set of definitions from the JPEG lib
33
34   WARNING: for a matter of simplicity, several pointer types are
35   casted to "void*", and most enumerated values are not included
36
37 =================================================================
38 */
39
40 // jboolean is qbyte instead of int on Win32
41 #ifdef WIN32
42 typedef qbyte jboolean;
43 #else
44 typedef int jboolean;
45 #endif
46
47 #define JPEG_LIB_VERSION  62  // Version 6b
48
49 typedef void *j_common_ptr;
50 typedef struct jpeg_compress_struct *j_compress_ptr;
51 typedef struct jpeg_decompress_struct *j_decompress_ptr;
52 typedef enum
53 {
54         JCS_UNKNOWN,
55         JCS_GRAYSCALE,
56         JCS_RGB,
57         JCS_YCbCr,
58         JCS_CMYK,
59         JCS_YCCK
60 } J_COLOR_SPACE;
61 typedef enum {JPEG_DUMMY1} J_DCT_METHOD;
62 typedef enum {JPEG_DUMMY2} J_DITHER_MODE;
63 typedef unsigned int JDIMENSION;
64
65 #define JPOOL_PERMANENT 0       // lasts until master record is destroyed
66 #define JPOOL_IMAGE             1       // lasts until done with image/datastream
67
68 #define JPEG_EOI        0xD9  // EOI marker code
69
70 #define JMSG_STR_PARM_MAX  80
71
72 #define DCTSIZE2 64
73 #define NUM_QUANT_TBLS 4
74 #define NUM_HUFF_TBLS 4
75 #define NUM_ARITH_TBLS 16
76 #define MAX_COMPS_IN_SCAN 4
77 #define C_MAX_BLOCKS_IN_MCU 10
78 #define D_MAX_BLOCKS_IN_MCU 10
79
80 struct jpeg_memory_mgr
81 {
82   void* (*alloc_small) (j_common_ptr cinfo, int pool_id, size_t sizeofobject);
83   void (*alloc_large) ();
84   void (*alloc_sarray) ();
85   void (*alloc_barray) ();
86   void (*request_virt_sarray) ();
87   void (*request_virt_barray) ();
88   void (*realize_virt_arrays) ();
89   void (*access_virt_sarray) ();
90   void (*access_virt_barray) ();
91   void (*free_pool) ();
92   void (*self_destruct) ();
93
94   long max_memory_to_use;
95   long max_alloc_chunk;
96 };
97
98 struct jpeg_error_mgr
99 {
100         void (*error_exit) (j_common_ptr cinfo);
101         void (*emit_message) (j_common_ptr cinfo, int msg_level);
102         void (*output_message) (j_common_ptr cinfo);
103         void (*format_message) (j_common_ptr cinfo, char * buffer);
104         void (*reset_error_mgr) (j_common_ptr cinfo);
105         int msg_code;
106         union {
107                 int i[8];
108                 char s[JMSG_STR_PARM_MAX];
109         } msg_parm;
110         int trace_level;
111         long num_warnings;
112         const char * const * jpeg_message_table;
113         int last_jpeg_message;
114         const char * const * addon_message_table;
115         int first_addon_message;
116         int last_addon_message;
117 };
118
119 struct jpeg_source_mgr
120 {
121         const qbyte *next_input_byte;
122         size_t bytes_in_buffer;
123
124         void (*init_source) (j_decompress_ptr cinfo);
125         jboolean (*fill_input_buffer) (j_decompress_ptr cinfo);
126         void (*skip_input_data) (j_decompress_ptr cinfo, long num_bytes);
127         jboolean (*resync_to_restart) (j_decompress_ptr cinfo, int desired);
128         void (*term_source) (j_decompress_ptr cinfo);
129 };
130
131 struct jpeg_decompress_struct
132 {
133         struct jpeg_error_mgr *err;             // USED
134         struct jpeg_memory_mgr *mem;    // USED
135
136         void *progress;
137         void *client_data;
138         jboolean is_decompressor;
139         int global_state;
140
141         struct jpeg_source_mgr *src;    // USED
142         JDIMENSION image_width;                 // USED
143         JDIMENSION image_height;                // USED
144
145         int num_components;
146         J_COLOR_SPACE jpeg_color_space;
147         J_COLOR_SPACE out_color_space;
148         unsigned int scale_num, scale_denom;
149         double output_gamma;
150         jboolean buffered_image;
151         jboolean raw_data_out;
152         J_DCT_METHOD dct_method;
153         jboolean do_fancy_upsampling;
154         jboolean do_block_smoothing;
155         jboolean quantize_colors;
156         J_DITHER_MODE dither_mode;
157         jboolean two_pass_quantize;
158         int desired_number_of_colors;
159         jboolean enable_1pass_quant;
160         jboolean enable_external_quant;
161         jboolean enable_2pass_quant;
162         JDIMENSION output_width;
163
164         JDIMENSION output_height;       // USED
165
166         int out_color_components;
167
168         int output_components;          // USED
169
170         int rec_outbuf_height;
171         int actual_number_of_colors;
172         void *colormap;
173
174         JDIMENSION output_scanline;     // USED
175
176         int input_scan_number;
177         JDIMENSION input_iMCU_row;
178         int output_scan_number;
179         JDIMENSION output_iMCU_row;
180         int (*coef_bits)[DCTSIZE2];
181         void *quant_tbl_ptrs[NUM_QUANT_TBLS];
182         void *dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
183         void *ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
184         int data_precision;
185         void *comp_info;
186         jboolean progressive_mode;
187         jboolean arith_code;
188         qbyte arith_dc_L[NUM_ARITH_TBLS];
189         qbyte arith_dc_U[NUM_ARITH_TBLS];
190         qbyte arith_ac_K[NUM_ARITH_TBLS];
191         unsigned int restart_interval;
192         jboolean saw_JFIF_marker;
193         qbyte JFIF_major_version;
194         qbyte JFIF_minor_version;
195         qbyte density_unit;
196         unsigned short X_density;
197         unsigned short Y_density;
198         jboolean saw_Adobe_marker;
199         qbyte Adobe_transform;
200         jboolean CCIR601_sampling;
201         void *marker_list;
202         int max_h_samp_factor;
203         int max_v_samp_factor;
204         int min_DCT_scaled_size;
205         JDIMENSION total_iMCU_rows;
206         void *sample_range_limit;
207         int comps_in_scan;
208         void *cur_comp_info[MAX_COMPS_IN_SCAN];
209         JDIMENSION MCUs_per_row;
210         JDIMENSION MCU_rows_in_scan;
211         int blocks_in_MCU;
212         int MCU_membership[D_MAX_BLOCKS_IN_MCU];
213         int Ss, Se, Ah, Al;
214         int unread_marker;
215         void *master;
216         void *main;
217         void *coef;
218         void *post;
219         void *inputctl;
220         void *marker;
221         void *entropy;
222         void *idct;
223         void *upsample;
224         void *cconvert;
225         void *cquantize;
226 };
227
228
229 struct jpeg_compress_struct
230 {
231         struct jpeg_error_mgr *err;
232         struct jpeg_memory_mgr *mem;
233         void *progress;
234         void *client_data;
235         jboolean is_decompressor;
236         int global_state;
237
238         void *dest;
239         JDIMENSION image_width;
240         JDIMENSION image_height;
241         int input_components;
242         J_COLOR_SPACE in_color_space;
243         double input_gamma;
244         int data_precision;
245
246         int num_components;
247         J_COLOR_SPACE jpeg_color_space;
248         void *comp_info;
249         void *quant_tbl_ptrs[NUM_QUANT_TBLS];
250         void *dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
251         void *ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
252         qbyte arith_dc_L[NUM_ARITH_TBLS];
253         qbyte arith_dc_U[NUM_ARITH_TBLS];
254         qbyte arith_ac_K[NUM_ARITH_TBLS];
255
256         int num_scans;
257         const void *scan_info;
258         jboolean raw_data_in;
259         jboolean arith_code;
260         jboolean optimize_coding;
261         jboolean CCIR601_sampling;
262         int smoothing_factor;
263         J_DCT_METHOD dct_method;
264
265         unsigned int restart_interval;
266         int restart_in_rows;
267
268         jboolean write_JFIF_header;
269         qbyte JFIF_major_version;
270         qbyte JFIF_minor_version;
271         qbyte density_unit;
272         unsigned short X_density;
273         unsigned short Y_density;
274         jboolean write_Adobe_marker;
275         JDIMENSION next_scanline;
276
277         jboolean progressive_mode;
278         int max_h_samp_factor;
279         int max_v_samp_factor;
280         JDIMENSION total_iMCU_rows;
281         int comps_in_scan;
282         void *cur_comp_info[MAX_COMPS_IN_SCAN];
283         JDIMENSION MCUs_per_row;
284         JDIMENSION MCU_rows_in_scan;
285         int blocks_in_MCU;
286         int MCU_membership[C_MAX_BLOCKS_IN_MCU];
287         int Ss, Se, Ah, Al;
288
289         void *master;
290         void *main;
291         void *prep;
292         void *coef;
293         void *marker;
294         void *cconvert;
295         void *downsample;
296         void *fdct;
297         void *entropy;
298         void *script_space;
299         int script_space_size;
300 };
301
302 struct jpeg_destination_mgr
303 {
304         qbyte* next_output_byte;
305         size_t free_in_buffer;
306
307         void (*init_destination) (j_compress_ptr cinfo);
308         jboolean (*empty_output_buffer) (j_compress_ptr cinfo);
309         void (*term_destination) (j_compress_ptr cinfo);
310 };
311
312
313 /*
314 =================================================================
315
316   DarkPlaces definitions
317
318 =================================================================
319 */
320
321 // Functions exported from libjpeg
322 #define qjpeg_create_compress(cinfo) \
323         qjpeg_CreateCompress((cinfo), JPEG_LIB_VERSION, (size_t) sizeof(struct jpeg_compress_struct))
324 #define qjpeg_create_decompress(cinfo) \
325         qjpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, (size_t) sizeof(struct jpeg_decompress_struct))
326
327 static void (*qjpeg_CreateCompress) (j_compress_ptr cinfo, int version, size_t structsize);
328 static void (*qjpeg_CreateDecompress) (j_decompress_ptr cinfo, int version, size_t structsize);
329 static void (*qjpeg_destroy_compress) (j_compress_ptr cinfo);
330 static void (*qjpeg_destroy_decompress) (j_decompress_ptr cinfo);
331 static void (*qjpeg_finish_compress) (j_compress_ptr cinfo);
332 static jboolean (*qjpeg_finish_decompress) (j_decompress_ptr cinfo);
333 static jboolean (*qjpeg_resync_to_restart) (j_decompress_ptr cinfo, int desired);
334 static int (*qjpeg_read_header) (j_decompress_ptr cinfo, jboolean require_image);
335 static JDIMENSION (*qjpeg_read_scanlines) (j_decompress_ptr cinfo, qbyte** scanlines, JDIMENSION max_lines);
336 static void (*qjpeg_set_defaults) (j_compress_ptr cinfo);
337 static void (*qjpeg_set_quality) (j_compress_ptr cinfo, int quality, jboolean force_baseline);
338 static jboolean (*qjpeg_start_compress) (j_compress_ptr cinfo, jboolean write_all_tables);
339 static jboolean (*qjpeg_start_decompress) (j_decompress_ptr cinfo);
340 static struct jpeg_error_mgr* (*qjpeg_std_error) (struct jpeg_error_mgr *err);
341 static JDIMENSION (*qjpeg_write_scanlines) (j_compress_ptr cinfo, qbyte** scanlines, JDIMENSION num_lines);
342
343 static dllfunction_t jpegfuncs[] =
344 {
345         {"jpeg_CreateCompress",         (void **) &qjpeg_CreateCompress},
346         {"jpeg_CreateDecompress",       (void **) &qjpeg_CreateDecompress},
347         {"jpeg_destroy_compress",       (void **) &qjpeg_destroy_compress},
348         {"jpeg_destroy_decompress",     (void **) &qjpeg_destroy_decompress},
349         {"jpeg_finish_compress",        (void **) &qjpeg_finish_compress},
350         {"jpeg_finish_decompress",      (void **) &qjpeg_finish_decompress},
351         {"jpeg_resync_to_restart",      (void **) &qjpeg_resync_to_restart},
352         {"jpeg_read_header",            (void **) &qjpeg_read_header},
353         {"jpeg_read_scanlines",         (void **) &qjpeg_read_scanlines},
354         {"jpeg_set_defaults",           (void **) &qjpeg_set_defaults},
355         {"jpeg_set_quality",            (void **) &qjpeg_set_quality},
356         {"jpeg_start_compress",         (void **) &qjpeg_start_compress},
357         {"jpeg_start_decompress",       (void **) &qjpeg_start_decompress},
358         {"jpeg_std_error",                      (void **) &qjpeg_std_error},
359         {"jpeg_write_scanlines",        (void **) &qjpeg_write_scanlines},
360         {NULL, NULL}
361 };
362
363 // Handle for JPEG DLL
364 dllhandle_t jpeg_dll = NULL;
365
366 static qbyte jpeg_eoi_marker [2] = {0xFF, JPEG_EOI};
367 static qboolean error_in_jpeg;
368
369 // Our own output manager for JPEG compression
370 typedef struct
371 {
372         struct jpeg_destination_mgr pub;
373
374         qfile_t* outfile;
375         qbyte* buffer;
376 } my_destination_mgr;
377 typedef my_destination_mgr* my_dest_ptr;
378
379
380 /*
381 =================================================================
382
383   DLL load & unload
384
385 =================================================================
386 */
387
388 /*
389 ====================
390 JPEG_OpenLibrary
391
392 Try to load the JPEG DLL
393 ====================
394 */
395 qboolean JPEG_OpenLibrary (void)
396 {
397         const char* dllname;
398
399         // Already loaded?
400         if (jpeg_dll)
401                 return true;
402
403 #ifdef WIN32
404         dllname = "libjpeg.dll";
405 #else
406         dllname = "libjpeg.so.62";
407 #endif
408
409         // Load the DLL
410         if (! Sys_LoadLibrary (dllname, &jpeg_dll, jpegfuncs))
411         {
412                 Con_Printf ("JPEG support disabled\n");
413                 return false;
414         }
415
416         Con_Printf ("JPEG support enabled\n");
417         return true;
418 }
419
420
421 /*
422 ====================
423 JPEG_CloseLibrary
424
425 Unload the JPEG DLL
426 ====================
427 */
428 void JPEG_CloseLibrary (void)
429 {
430         Sys_UnloadLibrary (&jpeg_dll);
431 }
432
433
434 /*
435 =================================================================
436
437         JPEG decompression
438
439 =================================================================
440 */
441
442 static void JPEG_Noop (j_decompress_ptr cinfo) {}
443
444 static jboolean JPEG_FillInputBuffer (j_decompress_ptr cinfo)
445 {
446     // Insert a fake EOI marker
447     cinfo->src->next_input_byte = jpeg_eoi_marker;
448     cinfo->src->bytes_in_buffer = 2;
449
450         return TRUE;
451 }
452
453 static void JPEG_SkipInputData (j_decompress_ptr cinfo, long num_bytes)
454 {
455     if (cinfo->src->bytes_in_buffer <= (unsigned long)num_bytes)
456         {
457                 cinfo->src->bytes_in_buffer = 0;
458                 return;
459         }
460
461     cinfo->src->next_input_byte += num_bytes;
462     cinfo->src->bytes_in_buffer -= num_bytes;
463 }
464
465 static void JPEG_MemSrc (j_decompress_ptr cinfo, qbyte *buffer)
466 {
467         cinfo->src = cinfo->mem->alloc_small ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (struct jpeg_source_mgr));
468
469         cinfo->src->next_input_byte = buffer;
470         cinfo->src->bytes_in_buffer = fs_filesize;
471
472         cinfo->src->init_source = JPEG_Noop;
473         cinfo->src->fill_input_buffer = JPEG_FillInputBuffer;
474         cinfo->src->skip_input_data = JPEG_SkipInputData;
475         cinfo->src->resync_to_restart = qjpeg_resync_to_restart; // use the default method
476         cinfo->src->term_source = JPEG_Noop;
477 }
478
479 static void JPEG_ErrorExit (j_common_ptr cinfo)
480 {
481         ((struct jpeg_decompress_struct*)cinfo)->err->output_message (cinfo);
482         error_in_jpeg = true;
483 }
484
485
486 /*
487 ====================
488 JPEG_LoadImage
489
490 Load a JPEG image into a RGBA buffer
491 ====================
492 */
493 qbyte* JPEG_LoadImage (qbyte *f, int matchwidth, int matchheight)
494 {
495         struct jpeg_decompress_struct cinfo;
496         struct jpeg_error_mgr jerr;
497         qbyte *image_rgba, *scanline;
498         unsigned int line;
499
500         // No DLL = no JPEGs
501         if (!jpeg_dll)
502                 return NULL;
503
504         cinfo.err = qjpeg_std_error (&jerr);
505         qjpeg_create_decompress (&cinfo);
506         JPEG_MemSrc (&cinfo, f);
507         qjpeg_read_header (&cinfo, TRUE);
508         qjpeg_start_decompress (&cinfo);
509
510         image_width = cinfo.image_width;
511         image_height = cinfo.image_height;
512
513         if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
514         {
515                 qjpeg_finish_decompress (&cinfo);
516                 qjpeg_destroy_decompress (&cinfo);
517                 return NULL;
518         }
519         if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
520         {
521                 Con_Printf("JPEG_LoadImage: invalid image size %ix%i\n", image_width, image_height);
522                 return NULL;
523         }
524
525         image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
526         scanline = Mem_Alloc(tempmempool, image_width * cinfo.output_components);
527         if (!image_rgba || !scanline)
528         {
529                 if (!image_rgba)
530                         Mem_Free (image_rgba);
531
532                 Con_Printf("JPEG_LoadImage: not enough memory for %i by %i image\n", image_width, image_height);
533                 qjpeg_finish_decompress (&cinfo);
534                 qjpeg_destroy_decompress (&cinfo);
535                 return NULL;
536         }
537
538         // Decompress the image, line by line
539         line = 0;
540         while (cinfo.output_scanline < cinfo.output_height)
541         {
542                 qbyte *buffer_ptr;
543                 int ind;
544
545                 qjpeg_read_scanlines (&cinfo, &scanline, 1);
546
547                 // Convert the image to RGBA
548                 switch (cinfo.output_components)
549                 {
550                         // RGB images
551                         case 3:
552                                 buffer_ptr = &image_rgba[image_width * line * 4];
553                                 for (ind = 0; ind < image_width * 3; ind += 3, buffer_ptr += 4)
554                                 {
555                                         buffer_ptr[0] = scanline[ind];
556                                         buffer_ptr[1] = scanline[ind + 1];
557                                         buffer_ptr[2] = scanline[ind + 2];
558                                         buffer_ptr[3] = 255;
559                                 }
560                                 break;
561
562                         // Greyscale images (default to it, just in case)
563                         case 1:
564                         default:
565                                 buffer_ptr = &image_rgba[image_width * line * 4];
566                                 for (ind = 0; ind < image_width; ind++, buffer_ptr += 4)
567                                 {
568                                         buffer_ptr[0] = scanline[ind];
569                                         buffer_ptr[1] = scanline[ind];
570                                         buffer_ptr[2] = scanline[ind];
571                                         buffer_ptr[3] = 255;
572                                 }
573                 }
574
575                 line++;
576         }
577         Mem_Free (scanline);
578
579         qjpeg_finish_decompress (&cinfo);
580         qjpeg_destroy_decompress (&cinfo);
581
582         return image_rgba;
583 }
584
585
586 /*
587 =================================================================
588
589   JPEG compression
590
591 =================================================================
592 */
593
594 #define JPEG_OUTPUT_BUF_SIZE 4096
595 static void JPEG_InitDestination (j_compress_ptr cinfo)
596 {
597         my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
598         dest->buffer = (qbyte*)cinfo->mem->alloc_small ((j_common_ptr) cinfo, JPOOL_IMAGE, JPEG_OUTPUT_BUF_SIZE * sizeof(qbyte));
599         dest->pub.next_output_byte = dest->buffer;
600         dest->pub.free_in_buffer = JPEG_OUTPUT_BUF_SIZE;
601 }
602
603 static jboolean JPEG_EmptyOutputBuffer (j_compress_ptr cinfo)
604 {
605         my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
606
607         if (FS_Write (dest->outfile, dest->buffer, JPEG_OUTPUT_BUF_SIZE) != (size_t) JPEG_OUTPUT_BUF_SIZE)
608         {
609                 error_in_jpeg = true;
610                 return false;
611         }
612
613         dest->pub.next_output_byte = dest->buffer;
614         dest->pub.free_in_buffer = JPEG_OUTPUT_BUF_SIZE;
615         return true;
616 }
617
618 static void JPEG_TermDestination (j_compress_ptr cinfo)
619 {
620         my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
621         size_t datacount = JPEG_OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
622
623         // Write any data remaining in the buffer
624         if (datacount > 0)
625                 if (FS_Write (dest->outfile, dest->buffer, datacount) != datacount)
626                         error_in_jpeg = true;
627 }
628
629 static void JPEG_MemDest (j_compress_ptr cinfo, qfile_t* outfile)
630 {
631         my_dest_ptr dest;
632
633         // First time for this JPEG object?
634         if (cinfo->dest == NULL)
635                 cinfo->dest = (struct jpeg_destination_mgr *)(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(my_destination_mgr));
636
637         dest = (my_dest_ptr)cinfo->dest;
638         dest->pub.init_destination = JPEG_InitDestination;
639         dest->pub.empty_output_buffer = JPEG_EmptyOutputBuffer;
640         dest->pub.term_destination = JPEG_TermDestination;
641         dest->outfile = outfile;
642 }
643
644
645 /*
646 ====================
647 JPEG_SaveImage_preflipped
648
649 Save a preflipped JPEG image to a file
650 ====================
651 */
652 qboolean JPEG_SaveImage_preflipped (const char *filename, int width, int height, qbyte *data)
653 {
654         struct jpeg_compress_struct cinfo;
655         struct jpeg_error_mgr jerr;
656         qbyte *scanline;
657         unsigned int offset, linesize;
658         qfile_t* file;
659
660         // No DLL = no JPEGs
661         if (!jpeg_dll)
662         {
663                 Con_Print("You need the libjpeg library to save JPEG images\n");
664                 return false;
665         }
666
667         // Open the file
668         file = FS_Open (filename, "wb", true);
669         if (!file)
670                 return false;
671
672         cinfo.err = qjpeg_std_error (&jerr);
673         cinfo.err->error_exit = JPEG_ErrorExit;
674         error_in_jpeg = false;
675
676         qjpeg_create_compress (&cinfo);
677         JPEG_MemDest (&cinfo, file);
678
679         // Set the parameters for compression
680         cinfo.image_width = width;
681         cinfo.image_height = height;
682         cinfo.in_color_space = JCS_RGB;
683         cinfo.input_components = 3;
684         qjpeg_set_defaults (&cinfo);
685         qjpeg_set_quality (&cinfo, scr_screenshot_jpeg_quality.value * 100, TRUE);
686         qjpeg_start_compress (&cinfo, true);
687
688         // Compress each scanline
689         linesize = cinfo.image_width * 3;
690         offset = linesize * (cinfo.image_height - 1);
691         while (cinfo.next_scanline < cinfo.image_height)
692         {
693                 scanline = &data[offset - cinfo.next_scanline * linesize];
694
695                 qjpeg_write_scanlines (&cinfo, &scanline, 1);
696                 if (error_in_jpeg)
697                         break;
698         }
699
700         qjpeg_finish_compress (&cinfo);
701         qjpeg_destroy_compress (&cinfo);
702
703         FS_Close (file);
704         return true;
705 }