2 Copyright (C) 2002 Mathieu Olivier
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.
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.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to:
18 Free Software Foundation, Inc.
19 59 Temple Place - Suite 330
20 Boston, MA 02111-1307, USA
31 =================================================================
33 Minimal set of definitions from the JPEG lib
35 WARNING: for a matter of simplicity, several pointer types are
36 casted to "void*", and most enumerated values are not included
38 =================================================================
41 // jboolean is unsigned char instead of int on Win32
43 typedef unsigned char jboolean;
48 #define JPEG_LIB_VERSION 62 // Version 6b
50 typedef void *j_common_ptr;
51 typedef struct jpeg_compress_struct *j_compress_ptr;
52 typedef struct jpeg_decompress_struct *j_decompress_ptr;
62 typedef enum {JPEG_DUMMY1} J_DCT_METHOD;
63 typedef enum {JPEG_DUMMY2} J_DITHER_MODE;
64 typedef unsigned int JDIMENSION;
66 #define JPOOL_PERMANENT 0 // lasts until master record is destroyed
67 #define JPOOL_IMAGE 1 // lasts until done with image/datastream
69 #define JPEG_EOI 0xD9 // EOI marker code
71 #define JMSG_STR_PARM_MAX 80
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
81 struct jpeg_memory_mgr
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) ();
93 void (*self_destruct) ();
95 long max_memory_to_use;
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);
109 char s[JMSG_STR_PARM_MAX];
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;
120 struct jpeg_source_mgr
122 const unsigned char *next_input_byte;
123 size_t bytes_in_buffer;
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);
132 struct jpeg_decompress_struct
134 struct jpeg_error_mgr *err; // USED
135 struct jpeg_memory_mgr *mem; // USED
139 jboolean is_decompressor;
142 struct jpeg_source_mgr *src; // USED
143 JDIMENSION image_width; // USED
144 JDIMENSION image_height; // USED
147 J_COLOR_SPACE jpeg_color_space;
148 J_COLOR_SPACE out_color_space;
149 unsigned int scale_num, scale_denom;
151 jboolean buffered_image;
152 jboolean raw_data_out;
153 J_DCT_METHOD dct_method;
154 jboolean do_fancy_upsampling;
155 jboolean do_block_smoothing;
156 jboolean quantize_colors;
157 J_DITHER_MODE dither_mode;
158 jboolean two_pass_quantize;
159 int desired_number_of_colors;
160 jboolean enable_1pass_quant;
161 jboolean enable_external_quant;
162 jboolean enable_2pass_quant;
163 JDIMENSION output_width;
165 JDIMENSION output_height; // USED
167 int out_color_components;
169 int output_components; // USED
171 int rec_outbuf_height;
172 int actual_number_of_colors;
175 JDIMENSION output_scanline; // USED
177 int input_scan_number;
178 JDIMENSION input_iMCU_row;
179 int output_scan_number;
180 JDIMENSION output_iMCU_row;
181 int (*coef_bits)[DCTSIZE2];
182 void *quant_tbl_ptrs[NUM_QUANT_TBLS];
183 void *dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
184 void *ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
187 jboolean progressive_mode;
189 unsigned char arith_dc_L[NUM_ARITH_TBLS];
190 unsigned char arith_dc_U[NUM_ARITH_TBLS];
191 unsigned char arith_ac_K[NUM_ARITH_TBLS];
192 unsigned int restart_interval;
193 jboolean saw_JFIF_marker;
194 unsigned char JFIF_major_version;
195 unsigned char JFIF_minor_version;
196 unsigned char density_unit;
197 unsigned short X_density;
198 unsigned short Y_density;
199 jboolean saw_Adobe_marker;
200 unsigned char Adobe_transform;
201 jboolean CCIR601_sampling;
203 int max_h_samp_factor;
204 int max_v_samp_factor;
205 int min_DCT_scaled_size;
206 JDIMENSION total_iMCU_rows;
207 void *sample_range_limit;
209 void *cur_comp_info[MAX_COMPS_IN_SCAN];
210 JDIMENSION MCUs_per_row;
211 JDIMENSION MCU_rows_in_scan;
213 int MCU_membership[D_MAX_BLOCKS_IN_MCU];
230 struct jpeg_compress_struct
232 struct jpeg_error_mgr *err;
233 struct jpeg_memory_mgr *mem;
236 jboolean is_decompressor;
240 JDIMENSION image_width;
241 JDIMENSION image_height;
242 int input_components;
243 J_COLOR_SPACE in_color_space;
248 J_COLOR_SPACE jpeg_color_space;
250 void *quant_tbl_ptrs[NUM_QUANT_TBLS];
251 void *dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
252 void *ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
253 unsigned char arith_dc_L[NUM_ARITH_TBLS];
254 unsigned char arith_dc_U[NUM_ARITH_TBLS];
255 unsigned char arith_ac_K[NUM_ARITH_TBLS];
258 const void *scan_info;
259 jboolean raw_data_in;
261 jboolean optimize_coding;
262 jboolean CCIR601_sampling;
263 int smoothing_factor;
264 J_DCT_METHOD dct_method;
266 unsigned int restart_interval;
269 jboolean write_JFIF_header;
270 unsigned char JFIF_major_version;
271 unsigned char JFIF_minor_version;
272 unsigned char density_unit;
273 unsigned short X_density;
274 unsigned short Y_density;
275 jboolean write_Adobe_marker;
276 JDIMENSION next_scanline;
278 jboolean progressive_mode;
279 int max_h_samp_factor;
280 int max_v_samp_factor;
281 JDIMENSION total_iMCU_rows;
283 void *cur_comp_info[MAX_COMPS_IN_SCAN];
284 JDIMENSION MCUs_per_row;
285 JDIMENSION MCU_rows_in_scan;
287 int MCU_membership[C_MAX_BLOCKS_IN_MCU];
300 int script_space_size;
303 struct jpeg_destination_mgr
305 unsigned char* next_output_byte;
306 size_t free_in_buffer;
308 void (*init_destination) (j_compress_ptr cinfo);
309 jboolean (*empty_output_buffer) (j_compress_ptr cinfo);
310 void (*term_destination) (j_compress_ptr cinfo);
315 =================================================================
317 DarkPlaces definitions
319 =================================================================
322 // Functions exported from libjpeg
323 #define qjpeg_create_compress(cinfo) \
324 qjpeg_CreateCompress((cinfo), JPEG_LIB_VERSION, (size_t) sizeof(struct jpeg_compress_struct))
325 #define qjpeg_create_decompress(cinfo) \
326 qjpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, (size_t) sizeof(struct jpeg_decompress_struct))
328 static void (*qjpeg_CreateCompress) (j_compress_ptr cinfo, int version, size_t structsize);
329 static void (*qjpeg_CreateDecompress) (j_decompress_ptr cinfo, int version, size_t structsize);
330 static void (*qjpeg_destroy_compress) (j_compress_ptr cinfo);
331 static void (*qjpeg_destroy_decompress) (j_decompress_ptr cinfo);
332 static void (*qjpeg_finish_compress) (j_compress_ptr cinfo);
333 static jboolean (*qjpeg_finish_decompress) (j_decompress_ptr cinfo);
334 static jboolean (*qjpeg_resync_to_restart) (j_decompress_ptr cinfo, int desired);
335 static int (*qjpeg_read_header) (j_decompress_ptr cinfo, jboolean require_image);
336 static JDIMENSION (*qjpeg_read_scanlines) (j_decompress_ptr cinfo, unsigned char** scanlines, JDIMENSION max_lines);
337 static void (*qjpeg_set_defaults) (j_compress_ptr cinfo);
338 static void (*qjpeg_set_quality) (j_compress_ptr cinfo, int quality, jboolean force_baseline);
339 static jboolean (*qjpeg_start_compress) (j_compress_ptr cinfo, jboolean write_all_tables);
340 static jboolean (*qjpeg_start_decompress) (j_decompress_ptr cinfo);
341 static struct jpeg_error_mgr* (*qjpeg_std_error) (struct jpeg_error_mgr *err);
342 static JDIMENSION (*qjpeg_write_scanlines) (j_compress_ptr cinfo, unsigned char** scanlines, JDIMENSION num_lines);
344 static dllfunction_t jpegfuncs[] =
346 {"jpeg_CreateCompress", (void **) &qjpeg_CreateCompress},
347 {"jpeg_CreateDecompress", (void **) &qjpeg_CreateDecompress},
348 {"jpeg_destroy_compress", (void **) &qjpeg_destroy_compress},
349 {"jpeg_destroy_decompress", (void **) &qjpeg_destroy_decompress},
350 {"jpeg_finish_compress", (void **) &qjpeg_finish_compress},
351 {"jpeg_finish_decompress", (void **) &qjpeg_finish_decompress},
352 {"jpeg_resync_to_restart", (void **) &qjpeg_resync_to_restart},
353 {"jpeg_read_header", (void **) &qjpeg_read_header},
354 {"jpeg_read_scanlines", (void **) &qjpeg_read_scanlines},
355 {"jpeg_set_defaults", (void **) &qjpeg_set_defaults},
356 {"jpeg_set_quality", (void **) &qjpeg_set_quality},
357 {"jpeg_start_compress", (void **) &qjpeg_start_compress},
358 {"jpeg_start_decompress", (void **) &qjpeg_start_decompress},
359 {"jpeg_std_error", (void **) &qjpeg_std_error},
360 {"jpeg_write_scanlines", (void **) &qjpeg_write_scanlines},
364 // Handle for JPEG DLL
365 dllhandle_t jpeg_dll = NULL;
367 static unsigned char jpeg_eoi_marker [2] = {0xFF, JPEG_EOI};
368 static qboolean error_in_jpeg;
370 // Our own output manager for JPEG compression
373 struct jpeg_destination_mgr pub;
376 unsigned char* buffer;
377 } my_destination_mgr;
378 typedef my_destination_mgr* my_dest_ptr;
382 =================================================================
386 =================================================================
393 Try to load the JPEG DLL
396 qboolean JPEG_OpenLibrary (void)
398 const char* dllnames [] =
404 #elif defined(MACOSX)
418 if (! Sys_LoadLibrary (dllnames, &jpeg_dll, jpegfuncs))
420 Con_Printf ("JPEG support disabled\n");
424 Con_Printf ("JPEG support enabled\n");
436 void JPEG_CloseLibrary (void)
438 Sys_UnloadLibrary (&jpeg_dll);
443 =================================================================
447 =================================================================
450 static void JPEG_Noop (j_decompress_ptr cinfo) {}
452 static jboolean JPEG_FillInputBuffer (j_decompress_ptr cinfo)
454 // Insert a fake EOI marker
455 cinfo->src->next_input_byte = jpeg_eoi_marker;
456 cinfo->src->bytes_in_buffer = 2;
461 static void JPEG_SkipInputData (j_decompress_ptr cinfo, long num_bytes)
463 if (cinfo->src->bytes_in_buffer <= (unsigned long)num_bytes)
465 cinfo->src->bytes_in_buffer = 0;
469 cinfo->src->next_input_byte += num_bytes;
470 cinfo->src->bytes_in_buffer -= num_bytes;
473 static void JPEG_MemSrc (j_decompress_ptr cinfo, const unsigned char *buffer, size_t filesize)
475 cinfo->src = (struct jpeg_source_mgr *)cinfo->mem->alloc_small ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (struct jpeg_source_mgr));
477 cinfo->src->next_input_byte = buffer;
478 cinfo->src->bytes_in_buffer = filesize;
480 cinfo->src->init_source = JPEG_Noop;
481 cinfo->src->fill_input_buffer = JPEG_FillInputBuffer;
482 cinfo->src->skip_input_data = JPEG_SkipInputData;
483 cinfo->src->resync_to_restart = qjpeg_resync_to_restart; // use the default method
484 cinfo->src->term_source = JPEG_Noop;
487 static void JPEG_ErrorExit (j_common_ptr cinfo)
489 ((struct jpeg_decompress_struct*)cinfo)->err->output_message (cinfo);
490 error_in_jpeg = true;
498 Load a JPEG image into a RGBA buffer
501 unsigned char* JPEG_LoadImage (const unsigned char *f, int filesize, int matchwidth, int matchheight)
503 struct jpeg_decompress_struct cinfo;
504 struct jpeg_error_mgr jerr;
505 unsigned char *image_rgba, *scanline;
512 cinfo.err = qjpeg_std_error (&jerr);
513 qjpeg_create_decompress (&cinfo);
514 JPEG_MemSrc (&cinfo, f, filesize);
515 qjpeg_read_header (&cinfo, TRUE);
516 qjpeg_start_decompress (&cinfo);
518 image_width = cinfo.image_width;
519 image_height = cinfo.image_height;
521 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
523 qjpeg_finish_decompress (&cinfo);
524 qjpeg_destroy_decompress (&cinfo);
527 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
529 Con_Printf("JPEG_LoadImage: invalid image size %ix%i\n", image_width, image_height);
533 image_rgba = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
534 scanline = (unsigned char *)Mem_Alloc(tempmempool, image_width * cinfo.output_components);
535 if (!image_rgba || !scanline)
538 Mem_Free (image_rgba);
542 Con_Printf("JPEG_LoadImage: not enough memory for %i by %i image\n", image_width, image_height);
543 qjpeg_finish_decompress (&cinfo);
544 qjpeg_destroy_decompress (&cinfo);
548 // Decompress the image, line by line
550 while (cinfo.output_scanline < cinfo.output_height)
552 unsigned char *buffer_ptr;
555 qjpeg_read_scanlines (&cinfo, &scanline, 1);
557 // Convert the image to RGBA
558 switch (cinfo.output_components)
562 buffer_ptr = &image_rgba[image_width * line * 4];
563 for (ind = 0; ind < image_width * 3; ind += 3, buffer_ptr += 4)
565 buffer_ptr[0] = scanline[ind];
566 buffer_ptr[1] = scanline[ind + 1];
567 buffer_ptr[2] = scanline[ind + 2];
572 // Greyscale images (default to it, just in case)
575 buffer_ptr = &image_rgba[image_width * line * 4];
576 for (ind = 0; ind < image_width; ind++, buffer_ptr += 4)
578 buffer_ptr[0] = scanline[ind];
579 buffer_ptr[1] = scanline[ind];
580 buffer_ptr[2] = scanline[ind];
589 qjpeg_finish_decompress (&cinfo);
590 qjpeg_destroy_decompress (&cinfo);
597 =================================================================
601 =================================================================
604 #define JPEG_OUTPUT_BUF_SIZE 4096
605 static void JPEG_InitDestination (j_compress_ptr cinfo)
607 my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
608 dest->buffer = (unsigned char*)cinfo->mem->alloc_small ((j_common_ptr) cinfo, JPOOL_IMAGE, JPEG_OUTPUT_BUF_SIZE * sizeof(unsigned char));
609 dest->pub.next_output_byte = dest->buffer;
610 dest->pub.free_in_buffer = JPEG_OUTPUT_BUF_SIZE;
613 static jboolean JPEG_EmptyOutputBuffer (j_compress_ptr cinfo)
615 my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
617 if (FS_Write (dest->outfile, dest->buffer, JPEG_OUTPUT_BUF_SIZE) != (size_t) JPEG_OUTPUT_BUF_SIZE)
619 error_in_jpeg = true;
623 dest->pub.next_output_byte = dest->buffer;
624 dest->pub.free_in_buffer = JPEG_OUTPUT_BUF_SIZE;
628 static void JPEG_TermDestination (j_compress_ptr cinfo)
630 my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
631 size_t datacount = JPEG_OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
633 // Write any data remaining in the buffer
635 if (FS_Write (dest->outfile, dest->buffer, datacount) != (fs_offset_t)datacount)
636 error_in_jpeg = true;
639 static void JPEG_MemDest (j_compress_ptr cinfo, qfile_t* outfile)
643 // First time for this JPEG object?
644 if (cinfo->dest == NULL)
645 cinfo->dest = (struct jpeg_destination_mgr *)(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(my_destination_mgr));
647 dest = (my_dest_ptr)cinfo->dest;
648 dest->pub.init_destination = JPEG_InitDestination;
649 dest->pub.empty_output_buffer = JPEG_EmptyOutputBuffer;
650 dest->pub.term_destination = JPEG_TermDestination;
651 dest->outfile = outfile;
657 JPEG_SaveImage_preflipped
659 Save a preflipped JPEG image to a file
662 qboolean JPEG_SaveImage_preflipped (const char *filename, int width, int height, unsigned char *data)
664 struct jpeg_compress_struct cinfo;
665 struct jpeg_error_mgr jerr;
666 unsigned char *scanline;
667 unsigned int offset, linesize;
673 Con_Print("You need the libjpeg library to save JPEG images\n");
678 file = FS_Open (filename, "wb", true, false);
682 cinfo.err = qjpeg_std_error (&jerr);
683 cinfo.err->error_exit = JPEG_ErrorExit;
684 error_in_jpeg = false;
686 qjpeg_create_compress (&cinfo);
687 JPEG_MemDest (&cinfo, file);
689 // Set the parameters for compression
690 cinfo.image_width = width;
691 cinfo.image_height = height;
692 cinfo.in_color_space = JCS_RGB;
693 cinfo.input_components = 3;
694 qjpeg_set_defaults (&cinfo);
695 qjpeg_set_quality (&cinfo, scr_screenshot_jpeg_quality.value * 100, TRUE);
696 qjpeg_start_compress (&cinfo, true);
698 // Compress each scanline
699 linesize = cinfo.image_width * 3;
700 offset = linesize * (cinfo.image_height - 1);
701 while (cinfo.next_scanline < cinfo.image_height)
703 scanline = &data[offset - cinfo.next_scanline * linesize];
705 qjpeg_write_scanlines (&cinfo, &scanline, 1);
710 qjpeg_finish_compress (&cinfo);
711 qjpeg_destroy_compress (&cinfo);