blob: 82acf23dc1a490e5e5323b49d5c57ba0a508e2ef [file] [log] [blame]
DRC2e7b76b2009-04-03 12:04:24 +00001/* Copyright (C)2004 Landmark Graphics Corporation
2 * Copyright (C)2005 Sun Microsystems, Inc.
DRC61e51f92009-04-05 21:53:20 +00003 * Copyright (C)2009 D. R. Commander
DRC2e7b76b2009-04-03 12:04:24 +00004 *
5 * This library is free software and may be redistributed and/or modified under
6 * the terms of the wxWindows Library License, Version 3.1 or (at your option)
7 * any later version. The full license is in the LICENSE.txt file included
8 * with this distribution.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * wxWindows Library License for more details.
14 */
15
16// This implements a JPEG compressor/decompressor using the libjpeg API
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <jpeglib.h>
22#include <jerror.h>
23#include <setjmp.h>
24#include "./turbojpeg.h"
25
26
27// Error handling
28
29static char lasterror[JMSG_LENGTH_MAX]="No error";
30
31typedef struct _error_mgr
32{
33 struct jpeg_error_mgr pub;
34 jmp_buf jb;
35} error_mgr;
36
37static void my_error_exit(j_common_ptr cinfo)
38{
39 error_mgr *myerr = (error_mgr *)cinfo->err;
40 (*cinfo->err->output_message)(cinfo);
41 longjmp(myerr->jb, 1);
42}
43
44static void my_output_message(j_common_ptr cinfo)
45{
46 (*cinfo->err->format_message)(cinfo, lasterror);
47}
48
49
50// Global structures, macros, etc.
51
52typedef struct _jpgstruct
53{
54 struct jpeg_compress_struct cinfo;
55 struct jpeg_decompress_struct dinfo;
56 struct jpeg_destination_mgr jdms;
57 struct jpeg_source_mgr jsms;
58 error_mgr jerr;
59 int initc, initd;
60} jpgstruct;
61
62static const int hsampfactor[NUMSUBOPT]={1, 2, 2, 1};
63static const int vsampfactor[NUMSUBOPT]={1, 1, 2, 1};
64
65#define _throw(c) {sprintf(lasterror, "%s", c); return -1;}
66#define _catch(f) {if((f)==-1) return -1;}
67#define checkhandle(h) jpgstruct *j=(jpgstruct *)h; \
68 if(!j) _throw("Invalid handle");
69
70
71// CO
72
73static boolean empty_output_buffer(struct jpeg_compress_struct *cinfo)
74{
75 ERREXIT(cinfo, JERR_BUFFER_SIZE);
76 return TRUE;
77}
78
79static void destination_noop(struct jpeg_compress_struct *cinfo)
80{
81}
82
83DLLEXPORT tjhandle DLLCALL tjInitCompress(void)
84{
85 jpgstruct *j=NULL;
86 if((j=(jpgstruct *)malloc(sizeof(jpgstruct)))==NULL)
87 {sprintf(lasterror, "Memory allocation failure"); return NULL;}
88 memset(j, 0, sizeof(jpgstruct));
89 j->cinfo.err=jpeg_std_error(&j->jerr.pub);
90 j->jerr.pub.error_exit=my_error_exit;
91 j->jerr.pub.output_message=my_output_message;
92
93 if(setjmp(j->jerr.jb))
94 { // this will execute if LIBJPEG has an error
95 if(j) free(j); return NULL;
96 }
97
98 jpeg_create_compress(&j->cinfo);
99 j->cinfo.dest=&j->jdms;
100 j->jdms.init_destination=destination_noop;
101 j->jdms.empty_output_buffer=empty_output_buffer;
102 j->jdms.term_destination=destination_noop;
103
104 j->initc=1;
105 return (tjhandle)j;
106}
107
108DLLEXPORT unsigned long DLLCALL TJBUFSIZE(int width, int height)
109{
110 // This allows enough room in case the image doesn't compress
111 return ((width+15)&(~15)) * ((height+15)&(~15)) * 6 + 2048;
112}
113
114DLLEXPORT int DLLCALL tjCompress(tjhandle h,
115 unsigned char *srcbuf, int width, int pitch, int height, int ps,
116 unsigned char *dstbuf, unsigned long *size,
117 int jpegsub, int qual, int flags)
118{
119 int i; JSAMPROW *row_pointer=NULL;
120
121 checkhandle(h);
122
123 if(srcbuf==NULL || width<=0 || pitch<0 || height<=0
124 || dstbuf==NULL || size==NULL
125 || jpegsub<0 || jpegsub>=NUMSUBOPT || qual<0 || qual>100)
126 _throw("Invalid argument in tjCompress()");
127 if(ps!=3 && ps!=4) _throw("This compressor can only take 24-bit or 32-bit RGB input");
128 if(!j->initc) _throw("Instance has not been initialized for compression");
129
130 if(pitch==0) pitch=width*ps;
131
132 j->cinfo.image_width = width;
133 j->cinfo.image_height = height;
134 j->cinfo.input_components = ps;
135
136 #if JCS_EXTENSIONS==1
137 j->cinfo.in_color_space = JCS_EXT_RGB;
138 if(ps==3 && (flags&TJ_BGR))
139 j->cinfo.in_color_space = JCS_EXT_BGR;
140 else if(ps==4 && !(flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
141 j->cinfo.in_color_space = JCS_EXT_RGBX;
142 else if(ps==4 && (flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
143 j->cinfo.in_color_space = JCS_EXT_BGRX;
144 else if(ps==4 && (flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
145 j->cinfo.in_color_space = JCS_EXT_XBGR;
146 else if(ps==4 && !(flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
147 j->cinfo.in_color_space = JCS_EXT_XRGB;
148 #else
149 #error "TurboJPEG requires JPEG colorspace extensions"
150 #endif
151
152 if(setjmp(j->jerr.jb))
153 { // this will execute if LIBJPEG has an error
154 if(row_pointer) free(row_pointer);
155 return -1;
156 }
157
158 jpeg_set_defaults(&j->cinfo);
159
160 jpeg_set_quality(&j->cinfo, qual, TRUE);
161 if(jpegsub==TJ_GRAYSCALE)
162 jpeg_set_colorspace(&j->cinfo, JCS_GRAYSCALE);
163 else
164 jpeg_set_colorspace(&j->cinfo, JCS_YCbCr);
165 j->cinfo.dct_method = JDCT_FASTEST;
166
167 j->cinfo.comp_info[0].h_samp_factor=hsampfactor[jpegsub];
168 j->cinfo.comp_info[1].h_samp_factor=1;
169 j->cinfo.comp_info[2].h_samp_factor=1;
170 j->cinfo.comp_info[0].v_samp_factor=vsampfactor[jpegsub];
171 j->cinfo.comp_info[1].v_samp_factor=1;
172 j->cinfo.comp_info[2].v_samp_factor=1;
173
174 j->jdms.next_output_byte = dstbuf;
175 j->jdms.free_in_buffer = TJBUFSIZE(j->cinfo.image_width, j->cinfo.image_height);
176
177 if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL)
178 _throw("Memory allocation failed in tjInitCompress()");
179 for(i=0; i<height; i++)
180 {
181 if(flags&TJ_BOTTOMUP) row_pointer[i]= &srcbuf[(height-i-1)*pitch];
182 else row_pointer[i]= &srcbuf[i*pitch];
183 }
184 jpeg_start_compress(&j->cinfo, TRUE);
185 while(j->cinfo.next_scanline<j->cinfo.image_height)
186 {
187 jpeg_write_scanlines(&j->cinfo, &row_pointer[j->cinfo.next_scanline],
188 j->cinfo.image_height-j->cinfo.next_scanline);
189 }
190 jpeg_finish_compress(&j->cinfo);
191 *size=TJBUFSIZE(j->cinfo.image_width, j->cinfo.image_height)-(j->jdms.free_in_buffer);
192
193 if(row_pointer) free(row_pointer);
194 return 0;
195}
196
197
198// DEC
199
200static boolean fill_input_buffer (struct jpeg_decompress_struct *dinfo)
201{
202 ERREXIT(dinfo, JERR_BUFFER_SIZE);
203 return TRUE;
204}
205
206static void skip_input_data (struct jpeg_decompress_struct *dinfo, long num_bytes)
207{
208 dinfo->src->next_input_byte += (size_t) num_bytes;
209 dinfo->src->bytes_in_buffer -= (size_t) num_bytes;
210}
211
212static void source_noop (struct jpeg_decompress_struct *dinfo)
213{
214}
215
216DLLEXPORT tjhandle DLLCALL tjInitDecompress(void)
217{
218 jpgstruct *j;
219 if((j=(jpgstruct *)malloc(sizeof(jpgstruct)))==NULL)
220 {sprintf(lasterror, "Memory allocation failure"); return NULL;}
221 memset(j, 0, sizeof(jpgstruct));
222 j->dinfo.err=jpeg_std_error(&j->jerr.pub);
223 j->jerr.pub.error_exit=my_error_exit;
224 j->jerr.pub.output_message=my_output_message;
225
226 if(setjmp(j->jerr.jb))
227 { // this will execute if LIBJPEG has an error
228 free(j); return NULL;
229 }
230
231 jpeg_create_decompress(&j->dinfo);
232 j->dinfo.src=&j->jsms;
233 j->jsms.init_source=source_noop;
234 j->jsms.fill_input_buffer = fill_input_buffer;
235 j->jsms.skip_input_data = skip_input_data;
236 j->jsms.resync_to_restart = jpeg_resync_to_restart;
237 j->jsms.term_source = source_noop;
238
239 j->initd=1;
240 return (tjhandle)j;
241}
242
243
244DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle h,
245 unsigned char *srcbuf, unsigned long size,
246 int *width, int *height)
247{
248 checkhandle(h);
249
250 if(srcbuf==NULL || size<=0 || width==NULL || height==NULL)
251 _throw("Invalid argument in tjDecompressHeader()");
252 if(!j->initd) _throw("Instance has not been initialized for decompression");
253
254 if(setjmp(j->jerr.jb))
255 { // this will execute if LIBJPEG has an error
256 return -1;
257 }
258
259 j->jsms.bytes_in_buffer = size;
260 j->jsms.next_input_byte = srcbuf;
261
262 jpeg_read_header(&j->dinfo, TRUE);
263
264 *width=j->dinfo.image_width; *height=j->dinfo.image_height;
265
266 jpeg_abort_decompress(&j->dinfo);
267
268 if(*width<1 || *height<1) _throw("Invalid data returned in header");
269 return 0;
270}
271
272
273DLLEXPORT int DLLCALL tjDecompress(tjhandle h,
274 unsigned char *srcbuf, unsigned long size,
275 unsigned char *dstbuf, int width, int pitch, int height, int ps,
276 int flags)
277{
278 int i; JSAMPROW *row_pointer=NULL;
279
280 checkhandle(h);
281
282 if(srcbuf==NULL || size<=0
283 || dstbuf==NULL || width<=0 || pitch<0 || height<=0)
284 _throw("Invalid argument in tjDecompress()");
285 if(ps!=3 && ps!=4) _throw("This compressor can only take 24-bit or 32-bit RGB input");
286 if(!j->initd) _throw("Instance has not been initialized for decompression");
287
288 if(pitch==0) pitch=width*ps;
289
290 if(setjmp(j->jerr.jb))
291 { // this will execute if LIBJPEG has an error
292 if(row_pointer) free(row_pointer);
293 return -1;
294 }
295
296 j->jsms.bytes_in_buffer = size;
297 j->jsms.next_input_byte = srcbuf;
298
299 jpeg_read_header(&j->dinfo, TRUE);
300
301 if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL)
302 _throw("Memory allocation failed in tjInitDecompress()");
303 for(i=0; i<height; i++)
304 {
305 if(flags&TJ_BOTTOMUP) row_pointer[i]= &dstbuf[(height-i-1)*pitch];
306 else row_pointer[i]= &dstbuf[i*pitch];
307 }
308
309 #if JCS_EXTENSIONS==1
310 j->dinfo.out_color_space = JCS_EXT_RGB;
311 if(ps==3 && (flags&TJ_BGR))
312 j->dinfo.out_color_space = JCS_EXT_BGR;
313 else if(ps==4 && !(flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
314 j->dinfo.out_color_space = JCS_EXT_RGBX;
315 else if(ps==4 && (flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
316 j->dinfo.out_color_space = JCS_EXT_BGRX;
317 else if(ps==4 && (flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
318 j->dinfo.out_color_space = JCS_EXT_XBGR;
319 else if(ps==4 && !(flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
320 j->dinfo.out_color_space = JCS_EXT_XRGB;
321 #else
322 #error "TurboJPEG requires JPEG colorspace extensions"
323 #endif
DRC61e51f92009-04-05 21:53:20 +0000324 if(flags&TJ_FASTUPSAMPLE) j->dinfo.do_fancy_upsampling=FALSE;
DRC2e7b76b2009-04-03 12:04:24 +0000325
326 jpeg_start_decompress(&j->dinfo);
327 while(j->dinfo.output_scanline<j->dinfo.output_height)
328 {
329 jpeg_read_scanlines(&j->dinfo, &row_pointer[j->dinfo.output_scanline],
330 j->dinfo.output_height-j->dinfo.output_scanline);
331 }
332 jpeg_finish_decompress(&j->dinfo);
333
334 if(row_pointer) free(row_pointer);
335 return 0;
336}
337
338
339// General
340
341DLLEXPORT char* DLLCALL tjGetErrorStr(void)
342{
343 return lasterror;
344}
345
346DLLEXPORT int DLLCALL tjDestroy(tjhandle h)
347{
348 checkhandle(h);
349 if(setjmp(j->jerr.jb)) return -1;
350 if(j->initc) jpeg_destroy_compress(&j->cinfo);
351 if(j->initd) jpeg_destroy_decompress(&j->dinfo);
352 free(j);
353 return 0;
354}