blob: 68dd35d64f0d4b0c75923f93d139093b550c8286 [file] [log] [blame]
DRCe34390b2009-04-03 12:04:24 +00001/* Copyright (C)2004 Landmark Graphics Corporation
2 * Copyright (C)2005 Sun Microsystems, Inc.
DRC00556702009-04-05 21:53:20 +00003 * Copyright (C)2009 D. R. Commander
DRCe34390b2009-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);
DRCda73d262010-02-26 23:27:25 +0000191 *size=TJBUFSIZE(j->cinfo.image_width, j->cinfo.image_height)
192 -(unsigned long)(j->jdms.free_in_buffer);
DRCe34390b2009-04-03 12:04:24 +0000193
194 if(row_pointer) free(row_pointer);
195 return 0;
196}
197
198
199// DEC
200
201static boolean fill_input_buffer (struct jpeg_decompress_struct *dinfo)
202{
203 ERREXIT(dinfo, JERR_BUFFER_SIZE);
204 return TRUE;
205}
206
207static void skip_input_data (struct jpeg_decompress_struct *dinfo, long num_bytes)
208{
209 dinfo->src->next_input_byte += (size_t) num_bytes;
210 dinfo->src->bytes_in_buffer -= (size_t) num_bytes;
211}
212
213static void source_noop (struct jpeg_decompress_struct *dinfo)
214{
215}
216
217DLLEXPORT tjhandle DLLCALL tjInitDecompress(void)
218{
219 jpgstruct *j;
220 if((j=(jpgstruct *)malloc(sizeof(jpgstruct)))==NULL)
221 {sprintf(lasterror, "Memory allocation failure"); return NULL;}
222 memset(j, 0, sizeof(jpgstruct));
223 j->dinfo.err=jpeg_std_error(&j->jerr.pub);
224 j->jerr.pub.error_exit=my_error_exit;
225 j->jerr.pub.output_message=my_output_message;
226
227 if(setjmp(j->jerr.jb))
228 { // this will execute if LIBJPEG has an error
229 free(j); return NULL;
230 }
231
232 jpeg_create_decompress(&j->dinfo);
233 j->dinfo.src=&j->jsms;
234 j->jsms.init_source=source_noop;
235 j->jsms.fill_input_buffer = fill_input_buffer;
236 j->jsms.skip_input_data = skip_input_data;
237 j->jsms.resync_to_restart = jpeg_resync_to_restart;
238 j->jsms.term_source = source_noop;
239
240 j->initd=1;
241 return (tjhandle)j;
242}
243
244
245DLLEXPORT int DLLCALL tjDecompressHeader(tjhandle h,
246 unsigned char *srcbuf, unsigned long size,
247 int *width, int *height)
248{
249 checkhandle(h);
250
251 if(srcbuf==NULL || size<=0 || width==NULL || height==NULL)
252 _throw("Invalid argument in tjDecompressHeader()");
253 if(!j->initd) _throw("Instance has not been initialized for decompression");
254
255 if(setjmp(j->jerr.jb))
256 { // this will execute if LIBJPEG has an error
257 return -1;
258 }
259
260 j->jsms.bytes_in_buffer = size;
261 j->jsms.next_input_byte = srcbuf;
262
263 jpeg_read_header(&j->dinfo, TRUE);
264
265 *width=j->dinfo.image_width; *height=j->dinfo.image_height;
266
267 jpeg_abort_decompress(&j->dinfo);
268
269 if(*width<1 || *height<1) _throw("Invalid data returned in header");
270 return 0;
271}
272
273
274DLLEXPORT int DLLCALL tjDecompress(tjhandle h,
275 unsigned char *srcbuf, unsigned long size,
276 unsigned char *dstbuf, int width, int pitch, int height, int ps,
277 int flags)
278{
279 int i; JSAMPROW *row_pointer=NULL;
280
281 checkhandle(h);
282
283 if(srcbuf==NULL || size<=0
284 || dstbuf==NULL || width<=0 || pitch<0 || height<=0)
285 _throw("Invalid argument in tjDecompress()");
286 if(ps!=3 && ps!=4) _throw("This compressor can only take 24-bit or 32-bit RGB input");
287 if(!j->initd) _throw("Instance has not been initialized for decompression");
288
289 if(pitch==0) pitch=width*ps;
290
291 if(setjmp(j->jerr.jb))
292 { // this will execute if LIBJPEG has an error
293 if(row_pointer) free(row_pointer);
294 return -1;
295 }
296
297 j->jsms.bytes_in_buffer = size;
298 j->jsms.next_input_byte = srcbuf;
299
300 jpeg_read_header(&j->dinfo, TRUE);
301
302 if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL)
303 _throw("Memory allocation failed in tjInitDecompress()");
304 for(i=0; i<height; i++)
305 {
306 if(flags&TJ_BOTTOMUP) row_pointer[i]= &dstbuf[(height-i-1)*pitch];
307 else row_pointer[i]= &dstbuf[i*pitch];
308 }
309
310 #if JCS_EXTENSIONS==1
311 j->dinfo.out_color_space = JCS_EXT_RGB;
312 if(ps==3 && (flags&TJ_BGR))
313 j->dinfo.out_color_space = JCS_EXT_BGR;
314 else if(ps==4 && !(flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
315 j->dinfo.out_color_space = JCS_EXT_RGBX;
316 else if(ps==4 && (flags&TJ_BGR) && !(flags&TJ_ALPHAFIRST))
317 j->dinfo.out_color_space = JCS_EXT_BGRX;
318 else if(ps==4 && (flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
319 j->dinfo.out_color_space = JCS_EXT_XBGR;
320 else if(ps==4 && !(flags&TJ_BGR) && (flags&TJ_ALPHAFIRST))
321 j->dinfo.out_color_space = JCS_EXT_XRGB;
322 #else
323 #error "TurboJPEG requires JPEG colorspace extensions"
324 #endif
DRC00556702009-04-05 21:53:20 +0000325 if(flags&TJ_FASTUPSAMPLE) j->dinfo.do_fancy_upsampling=FALSE;
DRCe34390b2009-04-03 12:04:24 +0000326
327 jpeg_start_decompress(&j->dinfo);
328 while(j->dinfo.output_scanline<j->dinfo.output_height)
329 {
330 jpeg_read_scanlines(&j->dinfo, &row_pointer[j->dinfo.output_scanline],
331 j->dinfo.output_height-j->dinfo.output_scanline);
332 }
333 jpeg_finish_decompress(&j->dinfo);
334
335 if(row_pointer) free(row_pointer);
336 return 0;
337}
338
339
340// General
341
342DLLEXPORT char* DLLCALL tjGetErrorStr(void)
343{
344 return lasterror;
345}
346
347DLLEXPORT int DLLCALL tjDestroy(tjhandle h)
348{
349 checkhandle(h);
350 if(setjmp(j->jerr.jb)) return -1;
351 if(j->initc) jpeg_destroy_compress(&j->cinfo);
352 if(j->initd) jpeg_destroy_decompress(&j->dinfo);
353 free(j);
354 return 0;
355}