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 qbyte instead of int on Win32
43 typedef qbyte 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 qbyte *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 qbyte arith_dc_L[NUM_ARITH_TBLS];
190 qbyte arith_dc_U[NUM_ARITH_TBLS];
191 qbyte arith_ac_K[NUM_ARITH_TBLS];
192 unsigned int restart_interval;
193 jboolean saw_JFIF_marker;
194 qbyte JFIF_major_version;
195 qbyte JFIF_minor_version;
197 unsigned short X_density;
198 unsigned short Y_density;
199 jboolean saw_Adobe_marker;
200 qbyte 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 qbyte arith_dc_L[NUM_ARITH_TBLS];
254 qbyte arith_dc_U[NUM_ARITH_TBLS];
255 qbyte 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 qbyte JFIF_major_version;
271 qbyte JFIF_minor_version;
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 qbyte* 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, qbyte** 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, qbyte** 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 qbyte 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;
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)
404 // TODO: make Sys_LoadLibrary support multiple names
406 dllname = "libjpeg.dll";
407 #elif defined(__FreeBSD__)
408 dllname = "libjpeg.so";
410 dllname = "libjpeg.so.62";
414 if (! Sys_LoadLibrary (dllname, &jpeg_dll, jpegfuncs))
416 Con_Printf ("JPEG support disabled\n");
420 Con_Printf ("JPEG support enabled\n");
432 void JPEG_CloseLibrary (void)
434 Sys_UnloadLibrary (&jpeg_dll);
439 =================================================================
443 =================================================================
446 static void JPEG_Noop (j_decompress_ptr cinfo) {}
448 static jboolean JPEG_FillInputBuffer (j_decompress_ptr cinfo)
450 // Insert a fake EOI marker
451 cinfo->src->next_input_byte = jpeg_eoi_marker;
452 cinfo->src->bytes_in_buffer = 2;
457 static void JPEG_SkipInputData (j_decompress_ptr cinfo, long num_bytes)
459 if (cinfo->src->bytes_in_buffer <= (unsigned long)num_bytes)
461 cinfo->src->bytes_in_buffer = 0;
465 cinfo->src->next_input_byte += num_bytes;
466 cinfo->src->bytes_in_buffer -= num_bytes;
469 static void JPEG_MemSrc (j_decompress_ptr cinfo, qbyte *buffer)
471 cinfo->src = cinfo->mem->alloc_small ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (struct jpeg_source_mgr));
473 cinfo->src->next_input_byte = buffer;
474 cinfo->src->bytes_in_buffer = fs_filesize;
476 cinfo->src->init_source = JPEG_Noop;
477 cinfo->src->fill_input_buffer = JPEG_FillInputBuffer;
478 cinfo->src->skip_input_data = JPEG_SkipInputData;
479 cinfo->src->resync_to_restart = qjpeg_resync_to_restart; // use the default method
480 cinfo->src->term_source = JPEG_Noop;
483 static void JPEG_ErrorExit (j_common_ptr cinfo)
485 ((struct jpeg_decompress_struct*)cinfo)->err->output_message (cinfo);
486 error_in_jpeg = true;
494 Load a JPEG image into a RGBA buffer
497 qbyte* JPEG_LoadImage (qbyte *f, int matchwidth, int matchheight)
499 struct jpeg_decompress_struct cinfo;
500 struct jpeg_error_mgr jerr;
501 qbyte *image_rgba, *scanline;
508 cinfo.err = qjpeg_std_error (&jerr);
509 qjpeg_create_decompress (&cinfo);
510 JPEG_MemSrc (&cinfo, f);
511 qjpeg_read_header (&cinfo, TRUE);
512 qjpeg_start_decompress (&cinfo);
514 image_width = cinfo.image_width;
515 image_height = cinfo.image_height;
517 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
519 qjpeg_finish_decompress (&cinfo);
520 qjpeg_destroy_decompress (&cinfo);
523 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
525 Con_Printf("JPEG_LoadImage: invalid image size %ix%i\n", image_width, image_height);
529 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
530 scanline = Mem_Alloc(tempmempool, image_width * cinfo.output_components);
531 if (!image_rgba || !scanline)
534 Mem_Free (image_rgba);
536 Con_Printf("JPEG_LoadImage: not enough memory for %i by %i image\n", image_width, image_height);
537 qjpeg_finish_decompress (&cinfo);
538 qjpeg_destroy_decompress (&cinfo);
542 // Decompress the image, line by line
544 while (cinfo.output_scanline < cinfo.output_height)
549 qjpeg_read_scanlines (&cinfo, &scanline, 1);
551 // Convert the image to RGBA
552 switch (cinfo.output_components)
556 buffer_ptr = &image_rgba[image_width * line * 4];
557 for (ind = 0; ind < image_width * 3; ind += 3, buffer_ptr += 4)
559 buffer_ptr[0] = scanline[ind];
560 buffer_ptr[1] = scanline[ind + 1];
561 buffer_ptr[2] = scanline[ind + 2];
566 // Greyscale images (default to it, just in case)
569 buffer_ptr = &image_rgba[image_width * line * 4];
570 for (ind = 0; ind < image_width; ind++, buffer_ptr += 4)
572 buffer_ptr[0] = scanline[ind];
573 buffer_ptr[1] = scanline[ind];
574 buffer_ptr[2] = scanline[ind];
583 qjpeg_finish_decompress (&cinfo);
584 qjpeg_destroy_decompress (&cinfo);
591 =================================================================
595 =================================================================
598 #define JPEG_OUTPUT_BUF_SIZE 4096
599 static void JPEG_InitDestination (j_compress_ptr cinfo)
601 my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
602 dest->buffer = (qbyte*)cinfo->mem->alloc_small ((j_common_ptr) cinfo, JPOOL_IMAGE, JPEG_OUTPUT_BUF_SIZE * sizeof(qbyte));
603 dest->pub.next_output_byte = dest->buffer;
604 dest->pub.free_in_buffer = JPEG_OUTPUT_BUF_SIZE;
607 static jboolean JPEG_EmptyOutputBuffer (j_compress_ptr cinfo)
609 my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
611 if (FS_Write (dest->outfile, dest->buffer, JPEG_OUTPUT_BUF_SIZE) != (size_t) JPEG_OUTPUT_BUF_SIZE)
613 error_in_jpeg = true;
617 dest->pub.next_output_byte = dest->buffer;
618 dest->pub.free_in_buffer = JPEG_OUTPUT_BUF_SIZE;
622 static void JPEG_TermDestination (j_compress_ptr cinfo)
624 my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
625 size_t datacount = JPEG_OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
627 // Write any data remaining in the buffer
629 if (FS_Write (dest->outfile, dest->buffer, datacount) != datacount)
630 error_in_jpeg = true;
633 static void JPEG_MemDest (j_compress_ptr cinfo, qfile_t* outfile)
637 // First time for this JPEG object?
638 if (cinfo->dest == NULL)
639 cinfo->dest = (struct jpeg_destination_mgr *)(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(my_destination_mgr));
641 dest = (my_dest_ptr)cinfo->dest;
642 dest->pub.init_destination = JPEG_InitDestination;
643 dest->pub.empty_output_buffer = JPEG_EmptyOutputBuffer;
644 dest->pub.term_destination = JPEG_TermDestination;
645 dest->outfile = outfile;
651 JPEG_SaveImage_preflipped
653 Save a preflipped JPEG image to a file
656 qboolean JPEG_SaveImage_preflipped (const char *filename, int width, int height, qbyte *data)
658 struct jpeg_compress_struct cinfo;
659 struct jpeg_error_mgr jerr;
661 unsigned int offset, linesize;
667 Con_Print("You need the libjpeg library to save JPEG images\n");
672 file = FS_Open (filename, "wb", true);
676 cinfo.err = qjpeg_std_error (&jerr);
677 cinfo.err->error_exit = JPEG_ErrorExit;
678 error_in_jpeg = false;
680 qjpeg_create_compress (&cinfo);
681 JPEG_MemDest (&cinfo, file);
683 // Set the parameters for compression
684 cinfo.image_width = width;
685 cinfo.image_height = height;
686 cinfo.in_color_space = JCS_RGB;
687 cinfo.input_components = 3;
688 qjpeg_set_defaults (&cinfo);
689 qjpeg_set_quality (&cinfo, scr_screenshot_jpeg_quality.value * 100, TRUE);
690 qjpeg_start_compress (&cinfo, true);
692 // Compress each scanline
693 linesize = cinfo.image_width * 3;
694 offset = linesize * (cinfo.image_height - 1);
695 while (cinfo.next_scanline < cinfo.image_height)
697 scanline = &data[offset - cinfo.next_scanline * linesize];
699 qjpeg_write_scanlines (&cinfo, &scanline, 1);
704 qjpeg_finish_compress (&cinfo);
705 qjpeg_destroy_compress (&cinfo);