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
30 =================================================================
32 Minimal set of definitions from the JPEG lib
34 WARNING: for a matter of simplicity, several pointer types are
35 casted to "void*", and most enumerated values are not included
37 =================================================================
40 // jboolean is qbyte instead of int on Win32
42 typedef qbyte jboolean;
47 #define JPEG_LIB_VERSION 62 // Version 6b
49 typedef void *j_common_ptr;
50 typedef struct jpeg_compress_struct *j_compress_ptr;
51 typedef struct jpeg_decompress_struct *j_decompress_ptr;
61 typedef enum {JPEG_DUMMY1} J_DCT_METHOD;
62 typedef enum {JPEG_DUMMY2} J_DITHER_MODE;
63 typedef unsigned int JDIMENSION;
65 #define JPOOL_PERMANENT 0 // lasts until master record is destroyed
66 #define JPOOL_IMAGE 1 // lasts until done with image/datastream
68 #define JPEG_EOI 0xD9 // EOI marker code
70 #define JMSG_STR_PARM_MAX 80
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
80 struct jpeg_memory_mgr
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) ();
92 void (*self_destruct) ();
94 long max_memory_to_use;
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);
108 char s[JMSG_STR_PARM_MAX];
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;
119 struct jpeg_source_mgr
121 const qbyte *next_input_byte;
122 size_t bytes_in_buffer;
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);
131 struct jpeg_decompress_struct
133 struct jpeg_error_mgr *err; // USED
134 struct jpeg_memory_mgr *mem; // USED
138 jboolean is_decompressor;
141 struct jpeg_source_mgr *src; // USED
142 JDIMENSION image_width; // USED
143 JDIMENSION image_height; // USED
146 J_COLOR_SPACE jpeg_color_space;
147 J_COLOR_SPACE out_color_space;
148 unsigned int scale_num, scale_denom;
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;
164 JDIMENSION output_height; // USED
166 int out_color_components;
168 int output_components; // USED
170 int rec_outbuf_height;
171 int actual_number_of_colors;
174 JDIMENSION output_scanline; // USED
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];
186 jboolean progressive_mode;
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;
196 unsigned short X_density;
197 unsigned short Y_density;
198 jboolean saw_Adobe_marker;
199 qbyte Adobe_transform;
200 jboolean CCIR601_sampling;
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;
208 void *cur_comp_info[MAX_COMPS_IN_SCAN];
209 JDIMENSION MCUs_per_row;
210 JDIMENSION MCU_rows_in_scan;
212 int MCU_membership[D_MAX_BLOCKS_IN_MCU];
229 struct jpeg_compress_struct
231 struct jpeg_error_mgr *err;
232 struct jpeg_memory_mgr *mem;
235 jboolean is_decompressor;
239 JDIMENSION image_width;
240 JDIMENSION image_height;
241 int input_components;
242 J_COLOR_SPACE in_color_space;
247 J_COLOR_SPACE jpeg_color_space;
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];
257 const void *scan_info;
258 jboolean raw_data_in;
260 jboolean optimize_coding;
261 jboolean CCIR601_sampling;
262 int smoothing_factor;
263 J_DCT_METHOD dct_method;
265 unsigned int restart_interval;
268 jboolean write_JFIF_header;
269 qbyte JFIF_major_version;
270 qbyte JFIF_minor_version;
272 unsigned short X_density;
273 unsigned short Y_density;
274 jboolean write_Adobe_marker;
275 JDIMENSION next_scanline;
277 jboolean progressive_mode;
278 int max_h_samp_factor;
279 int max_v_samp_factor;
280 JDIMENSION total_iMCU_rows;
282 void *cur_comp_info[MAX_COMPS_IN_SCAN];
283 JDIMENSION MCUs_per_row;
284 JDIMENSION MCU_rows_in_scan;
286 int MCU_membership[C_MAX_BLOCKS_IN_MCU];
299 int script_space_size;
302 struct jpeg_destination_mgr
304 qbyte* next_output_byte;
305 size_t free_in_buffer;
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);
314 =================================================================
316 DarkPlaces definitions
318 =================================================================
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))
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);
343 static dllfunction_t jpegfuncs[] =
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},
363 // Handle for JPEG DLL
364 dllhandle_t jpeg_dll = NULL;
366 static qbyte jpeg_eoi_marker [2] = {0xFF, JPEG_EOI};
367 static qboolean error_in_jpeg;
369 // Our own output manager for JPEG compression
372 struct jpeg_destination_mgr pub;
376 } my_destination_mgr;
377 typedef my_destination_mgr* my_dest_ptr;
381 =================================================================
385 =================================================================
392 Try to load the JPEG DLL
395 qboolean JPEG_OpenLibrary (void)
398 const dllfunction_t *func;
405 dllname = "libjpeg.dll";
407 dllname = "libjpeg.so.62";
411 for (func = jpegfuncs; func && func->name != NULL; func++)
412 *func->funcvariable = NULL;
415 if (! (jpeg_dll = Sys_LoadLibrary (dllname)))
417 Con_Printf ("Can't find %s. JPEG support disabled\n", dllname);
421 // Get the function adresses
422 for (func = jpegfuncs; func && func->name != NULL; func++)
423 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (jpeg_dll, func->name)))
425 Con_Printf ("missing function \"%s\" - broken JPEG library!\n", func->name);
426 JPEG_CloseLibrary ();
430 Con_Printf ("%s loaded. JPEG support enabled\n", dllname);
442 void JPEG_CloseLibrary (void)
447 Sys_UnloadLibrary (jpeg_dll);
453 =================================================================
457 =================================================================
460 static void JPEG_Noop (j_decompress_ptr cinfo) {}
462 static jboolean JPEG_FillInputBuffer (j_decompress_ptr cinfo)
464 // Insert a fake EOI marker
465 cinfo->src->next_input_byte = jpeg_eoi_marker;
466 cinfo->src->bytes_in_buffer = 2;
471 static void JPEG_SkipInputData (j_decompress_ptr cinfo, long num_bytes)
473 if (cinfo->src->bytes_in_buffer <= (unsigned long)num_bytes)
475 cinfo->src->bytes_in_buffer = 0;
479 cinfo->src->next_input_byte += num_bytes;
480 cinfo->src->bytes_in_buffer -= num_bytes;
483 static void JPEG_MemSrc (j_decompress_ptr cinfo, qbyte *buffer)
485 cinfo->src = cinfo->mem->alloc_small ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (struct jpeg_source_mgr));
487 cinfo->src->next_input_byte = buffer;
488 cinfo->src->bytes_in_buffer = fs_filesize;
490 cinfo->src->init_source = JPEG_Noop;
491 cinfo->src->fill_input_buffer = JPEG_FillInputBuffer;
492 cinfo->src->skip_input_data = JPEG_SkipInputData;
493 cinfo->src->resync_to_restart = qjpeg_resync_to_restart; // use the default method
494 cinfo->src->term_source = JPEG_Noop;
497 static void JPEG_ErrorExit (j_common_ptr cinfo)
499 ((struct jpeg_decompress_struct*)cinfo)->err->output_message (cinfo);
500 error_in_jpeg = true;
508 Load a JPEG image into a RGBA buffer
511 qbyte* JPEG_LoadImage (qbyte *f, int matchwidth, int matchheight)
513 struct jpeg_decompress_struct cinfo;
514 struct jpeg_error_mgr jerr;
515 qbyte *image_rgba, *scanline;
522 cinfo.err = qjpeg_std_error (&jerr);
523 qjpeg_create_decompress (&cinfo);
524 JPEG_MemSrc (&cinfo, f);
525 qjpeg_read_header (&cinfo, TRUE);
526 qjpeg_start_decompress (&cinfo);
528 image_width = cinfo.image_width;
529 image_height = cinfo.image_height;
531 if ((matchwidth && image_width != matchwidth) || (matchheight && image_height != matchheight))
533 qjpeg_finish_decompress (&cinfo);
534 qjpeg_destroy_decompress (&cinfo);
537 if (image_width > 4096 || image_height > 4096 || image_width <= 0 || image_height <= 0)
539 Con_Printf("JPEG_LoadImage: invalid image size %ix%i\n", image_width, image_height);
543 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
544 scanline = Mem_Alloc(tempmempool, image_width * cinfo.output_components);
545 if (!image_rgba || !scanline)
548 Mem_Free (image_rgba);
550 Con_Printf("JPEG_LoadImage: not enough memory for %i by %i image\n", image_width, image_height);
551 qjpeg_finish_decompress (&cinfo);
552 qjpeg_destroy_decompress (&cinfo);
556 // Decompress the image, line by line
558 while (cinfo.output_scanline < cinfo.output_height)
563 qjpeg_read_scanlines (&cinfo, &scanline, 1);
565 // Convert the image to RGBA
566 switch (cinfo.output_components)
570 buffer_ptr = &image_rgba[image_width * line * 4];
571 for (ind = 0; ind < image_width * 3; ind += 3, buffer_ptr += 4)
573 buffer_ptr[0] = scanline[ind];
574 buffer_ptr[1] = scanline[ind + 1];
575 buffer_ptr[2] = scanline[ind + 2];
580 // Greyscale images (default to it, just in case)
583 buffer_ptr = &image_rgba[image_width * line * 4];
584 for (ind = 0; ind < image_width; ind++, buffer_ptr += 4)
586 buffer_ptr[0] = scanline[ind];
587 buffer_ptr[1] = scanline[ind];
588 buffer_ptr[2] = scanline[ind];
597 qjpeg_finish_decompress (&cinfo);
598 qjpeg_destroy_decompress (&cinfo);
605 =================================================================
609 =================================================================
612 #define JPEG_OUTPUT_BUF_SIZE 4096
613 static void JPEG_InitDestination (j_compress_ptr cinfo)
615 my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
616 dest->buffer = (qbyte*)cinfo->mem->alloc_small ((j_common_ptr) cinfo, JPOOL_IMAGE, JPEG_OUTPUT_BUF_SIZE * sizeof(qbyte));
617 dest->pub.next_output_byte = dest->buffer;
618 dest->pub.free_in_buffer = JPEG_OUTPUT_BUF_SIZE;
621 static jboolean JPEG_EmptyOutputBuffer (j_compress_ptr cinfo)
623 my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
625 if (FS_Write (dest->outfile, dest->buffer, JPEG_OUTPUT_BUF_SIZE) != (size_t) JPEG_OUTPUT_BUF_SIZE)
627 error_in_jpeg = true;
631 dest->pub.next_output_byte = dest->buffer;
632 dest->pub.free_in_buffer = JPEG_OUTPUT_BUF_SIZE;
636 static void JPEG_TermDestination (j_compress_ptr cinfo)
638 my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
639 size_t datacount = JPEG_OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
641 // Write any data remaining in the buffer
643 if (FS_Write (dest->outfile, dest->buffer, datacount) != datacount)
644 error_in_jpeg = true;
647 static void JPEG_MemDest (j_compress_ptr cinfo, qfile_t* outfile)
651 // First time for this JPEG object?
652 if (cinfo->dest == NULL)
653 cinfo->dest = (struct jpeg_destination_mgr *)(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(my_destination_mgr));
655 dest = (my_dest_ptr)cinfo->dest;
656 dest->pub.init_destination = JPEG_InitDestination;
657 dest->pub.empty_output_buffer = JPEG_EmptyOutputBuffer;
658 dest->pub.term_destination = JPEG_TermDestination;
659 dest->outfile = outfile;
665 JPEG_SaveImage_preflipped
667 Save a preflipped JPEG image to a file
670 qboolean JPEG_SaveImage_preflipped (const char *filename, int width, int height, qbyte *data)
672 struct jpeg_compress_struct cinfo;
673 struct jpeg_error_mgr jerr;
675 unsigned int offset, linesize;
681 Con_Printf ("You need the libjpeg library to save JPEG images\n");
686 file = FS_Open (filename, "wb", true);
690 cinfo.err = qjpeg_std_error (&jerr);
691 cinfo.err->error_exit = JPEG_ErrorExit;
692 error_in_jpeg = false;
694 qjpeg_create_compress (&cinfo);
695 JPEG_MemDest (&cinfo, file);
697 // Set the parameters for compression
698 cinfo.image_width = width;
699 cinfo.image_height = height;
700 cinfo.in_color_space = JCS_RGB;
701 cinfo.input_components = 3;
702 qjpeg_set_defaults (&cinfo);
703 qjpeg_set_quality (&cinfo, 90, TRUE); // 90% quality; FIXME: use a cvar
704 qjpeg_start_compress (&cinfo, true);
706 // Compress each scanline
707 linesize = cinfo.image_width * 3;
708 offset = linesize * (cinfo.image_height - 1);
709 while (cinfo.next_scanline < cinfo.image_height)
711 scanline = &data[offset - cinfo.next_scanline * linesize];
713 qjpeg_write_scanlines (&cinfo, &scanline, 1);
718 qjpeg_finish_compress (&cinfo);
719 qjpeg_destroy_compress (&cinfo);