blob: 3b02fba39bba72f911d759d771d0531175405f77 [file] [log] [blame]
DRC2e7b76b2009-04-03 12:04:24 +00001/* Copyright (C)2004 Landmark Graphics Corporation
2 * Copyright (C)2005 Sun Microsystems, Inc.
3 * Copyright (C)2009 D. R. Commander
4 *
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#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include "./rrtimer.h"
20#include "./turbojpeg.h"
21
22#define _catch(f) {if((f)==-1) {printf("TJPEG: %s\n", tjGetErrorStr()); goto finally;}}
23
DRC61e51f92009-04-05 21:53:20 +000024const char *_subnamel[NUMSUBOPT]={"4:4:4", "4:2:2", "4:2:0", "GRAY"};
25const char *_subnames[NUMSUBOPT]={"444", "422", "420", "GRAY"};
DRC2e7b76b2009-04-03 12:04:24 +000026
27int pixels[9][3]=
28{
29 {0, 255, 0},
30 {255, 0, 255},
31 {255, 255, 0},
32 {0, 0, 255},
33 {0, 255, 255},
34 {255, 0, 0},
35 {255, 255, 255},
36 {0, 0, 0},
37 {255, 0, 0}
38};
39
40void initbuf(unsigned char *buf, int w, int h, int ps, int flags)
41{
42 int roffset=(flags&TJ_BGR)?2:0, goffset=1, boffset=(flags&TJ_BGR)?0:2, i,
43 _i, j;
44 if(flags&TJ_ALPHAFIRST) {roffset++; goffset++; boffset++;}
45 memset(buf, 0, w*h*ps);
46 for(_i=0; _i<16; _i++)
47 {
48 if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i;
49 for(j=0; j<w; j++)
50 {
51 buf[(w*i+j)*ps+roffset]=255;
52 if(((_i/8)+(j/8))%2==0)
53 {
54 buf[(w*i+j)*ps+goffset]=255;
55 buf[(w*i+j)*ps+boffset]=255;
56 }
57 }
58 }
59 for(_i=16; _i<h; _i++)
60 {
61 if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i;
62 for(j=0; j<w; j++)
63 {
64 if(((_i/8)+(j/8))%2!=0)
65 {
66 buf[(w*i+j)*ps+roffset]=255;
67 buf[(w*i+j)*ps+goffset]=255;
68 }
69 }
70 }
71}
72
73int dumpbuf(unsigned char *buf, int w, int h, int ps, int flags)
74{
75 int roffset=(flags&TJ_BGR)?2:0, goffset=1, boffset=(flags&TJ_BGR)?0:2, i,
76 j;
77 for(i=0; i<h; i++)
78 {
79 for(j=0; j<w; j++)
80 {
81 printf("%.3d/%.3d/%.3d ", buf[(w*i+j)*ps+roffset],
82 buf[(w*i+j)*ps+roffset], buf[(w*i+j)*ps+roffset]);
83 }
84 printf("\n");
85 }
86}
87
88int checkbuf(unsigned char *buf, int w, int h, int ps, int subsamp, int flags)
89{
90 int roffset=(flags&TJ_BGR)?2:0, goffset=1, boffset=(flags&TJ_BGR)?0:2, i,
91 _i, j;
92 if(flags&TJ_ALPHAFIRST) {roffset++; goffset++; boffset++;}
93 if(subsamp==TJ_GRAYSCALE)
94 {
95 for(_i=0; _i<16; _i++)
96 {
97 if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i;
98 for(j=0; j<w; j++)
99 {
100 unsigned char r=buf[(w*i+j)*ps+roffset],
101 g=buf[(w*i+j)*ps+goffset],
102 b=buf[(w*i+j)*ps+boffset];
103 if(((_i/8)+(j/8))%2==0)
104 {
105 if(r<253 || g<253 || b<253) return 0;
106 }
107 else
108 {
109 if(r<74 || r>78 || g<74 || g>78 || b<74 || b>78) return 0;
110 }
111 }
112 }
113 for(_i=16; _i<h; _i++)
114 {
115 if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i;
116 for(j=0; j<w; j++)
117 {
118 unsigned char r=buf[(w*i+j)*ps+roffset],
119 g=buf[(w*i+j)*ps+goffset],
120 b=buf[(w*i+j)*ps+boffset];
121 if(((_i/8)+(j/8))%2==0)
122 {
123 if(r>2 || g>2 || b>2) return 0;
124 }
125 else
126 {
127 if(r<224 || r>228 || g<224 || g>228 || b<224 || b>228) return 0;
128 }
129 }
130 }
131 }
132 else
133 {
134 for(_i=0; _i<16; _i++)
135 {
136 if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i;
137 for(j=0; j<w; j++)
138 {
139 if(buf[(w*i+j)*ps+roffset]<253) return 0;
140 if(((_i/8)+(j/8))%2==0)
141 {
142 if(buf[(w*i+j)*ps+goffset]<253) return 0;
143 if(buf[(w*i+j)*ps+boffset]<253) return 0;
144 }
145 else
146 {
147 if(buf[(w*i+j)*ps+goffset]>2) return 0;
148 if(buf[(w*i+j)*ps+boffset]>2) return 0;
149 }
150 }
151 }
152 for(_i=16; _i<h; _i++)
153 {
154 if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i;
155 for(j=0; j<w; j++)
156 {
157 if(buf[(w*i+j)*ps+boffset]>2) return 0;
158 if(((_i/8)+(j/8))%2==0)
159 {
160 if(buf[(w*i+j)*ps+roffset]>2) return 0;
161 if(buf[(w*i+j)*ps+goffset]>2) return 0;
162 }
163 else
164 {
165 if(buf[(w*i+j)*ps+roffset]<253) return 0;
166 if(buf[(w*i+j)*ps+goffset]<253) return 0;
167 }
168 }
169 }
170 }
171 return 1;
172}
173
174void writejpeg(unsigned char *jpegbuf, unsigned long jpgbufsize, char *filename)
175{
176 FILE *outfile=NULL;
177 if((outfile=fopen(filename, "wb"))==NULL)
178 {
179 printf("ERROR: Could not open %s for writing.\n", filename);
180 goto finally;
181 }
182 if(fwrite(jpegbuf, jpgbufsize, 1, outfile)!=1)
183 {
184 printf("ERROR: Could not write to %s.\n", filename);
185 goto finally;
186 }
187
188 finally:
189 if(outfile) fclose(outfile);
190}
191
192void gentestjpeg(tjhandle hnd, unsigned char *jpegbuf, unsigned long *size,
193 int w, int h, int ps, char *basefilename, int subsamp, int qual, int flags)
194{
195 char tempstr[1024]; unsigned char *bmpbuf=NULL;
196 const char *pixformat; double t;
197
198 if(flags&TJ_BGR)
199 {
200 if(ps==3) pixformat="BGR";
201 else {if(flags&TJ_ALPHAFIRST) pixformat="ABGR"; else pixformat="BGRA";}
202 }
203 else
204 {
205 if(ps==3) pixformat="RGB";
206 else {if(flags&TJ_ALPHAFIRST) pixformat="ARGB"; else pixformat="RGBA";}
207 }
208 printf("%s %s -> %s Q%d ... ", pixformat,
209 (flags&TJ_BOTTOMUP)?"Bottom-Up":"Top-Down ", _subnamel[subsamp], qual);
210
211 if((bmpbuf=(unsigned char *)malloc(w*h*ps+1))==NULL)
212 {
213 printf("ERROR: Could not allocate buffer\n"); goto finally;
214 }
215 initbuf(bmpbuf, w, h, ps, flags);
216 memset(jpegbuf, 0, TJBUFSIZE(w, h));
217
218 t=rrtime();
219 _catch(tjCompress(hnd, bmpbuf, w, 0, h, ps, jpegbuf, size, subsamp, qual, flags));
220 t=rrtime()-t;
221
222 sprintf(tempstr, "%s_enc_%s_%s_%sQ%d.jpg", basefilename, pixformat,
223 (flags&TJ_BOTTOMUP)? "BU":"TD", _subnames[subsamp], qual);
224 writejpeg(jpegbuf, *size, tempstr);
225 printf("Done. %f ms\n Result in %s\n", t*1000., tempstr);
226
227 finally:
228 if(bmpbuf) free(bmpbuf);
229}
230
231void gentestbmp(tjhandle hnd, unsigned char *jpegbuf, unsigned long jpegsize,
232 int w, int h, int ps, char *basefilename, int subsamp, int qual, int flags)
233{
234 unsigned char *bmpbuf=NULL;
235 const char *pixformat; int _w=0, _h=0; double t;
236
237 if(flags&TJ_BGR)
238 {
239 if(ps==3) pixformat="BGR";
240 else {if(flags&TJ_ALPHAFIRST) pixformat="ABGR"; else pixformat="BGRA";}
241 }
242 else
243 {
244 if(ps==3) pixformat="RGB";
245 else {if(flags&TJ_ALPHAFIRST) pixformat="ARGB"; else pixformat="RGBA";}
246 }
247 printf("JPEG -> %s %s ... ", pixformat, (flags&TJ_BOTTOMUP)?"Bottom-Up":"Top-Down ");
248
249 _catch(tjDecompressHeader(hnd, jpegbuf, jpegsize, &_w, &_h));
250 if(_w!=w || _h!=h)
251 {
252 printf("Incorrect JPEG header\n"); goto finally;
253 }
254
255 if((bmpbuf=(unsigned char *)malloc(w*h*ps+1))==NULL)
256 {
257 printf("ERROR: Could not allocate buffer\n"); goto finally;
258 }
259 memset(bmpbuf, 0, w*ps*h);
260
261 t=rrtime();
262 _catch(tjDecompress(hnd, jpegbuf, jpegsize, bmpbuf, w, w*ps, h, ps, flags));
263 t=rrtime()-t;
264
265 if(checkbuf(bmpbuf, w, h, ps, subsamp, flags)) printf("Passed.");
266 else {printf("FAILED!"); dumpbuf(bmpbuf, w, h, ps, flags);}
267
268 printf(" %f ms\n\n", t*1000.);
269
270 finally:
271 if(bmpbuf) free(bmpbuf);
272}
273
274void dotest(int w, int h, int ps, int subsamp, char *basefilename)
275{
276 tjhandle hnd=NULL, dhnd=NULL; unsigned char *jpegbuf=NULL;
277 unsigned long size;
278
279 if((jpegbuf=(unsigned char *)malloc(TJBUFSIZE(w, h))) == NULL)
280 {
281 puts("ERROR: Could not allocate buffer."); goto finally;
282 }
283
284 if((hnd=tjInitCompress())==NULL)
285 {printf("Error in tjInitCompress():\n%s\n", tjGetErrorStr()); goto finally;}
286 if((dhnd=tjInitDecompress())==NULL)
287 {printf("Error in tjInitDecompress():\n%s\n", tjGetErrorStr()); goto finally;}
288
289 gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, 0);
290 gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 100, 0);
291
292 gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_BGR);
293 gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 100, TJ_BGR);
294
295 gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_BOTTOMUP);
296 gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 100, TJ_BOTTOMUP);
297
298 gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_BGR|TJ_BOTTOMUP);
299 gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 100, TJ_BGR|TJ_BOTTOMUP);
300
301 if(ps==4)
302 {
303 gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST);
304 gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST);
305
306 gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST|TJ_BGR);
307 gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST|TJ_BGR);
308
309 gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST|TJ_BOTTOMUP);
310 gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST|TJ_BOTTOMUP);
311
312 gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST|TJ_BGR|TJ_BOTTOMUP);
313 gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST|TJ_BGR|TJ_BOTTOMUP);
314 }
315
316 finally:
317 if(hnd) tjDestroy(hnd);
318 if(dhnd) tjDestroy(dhnd);
319
320 if(jpegbuf) free(jpegbuf);
321}
322
323#define MAXLENGTH 2048
324
325void dotest1(void)
326{
327 int i, j, i2; unsigned char *bmpbuf=NULL, *jpgbuf=NULL;
328 tjhandle hnd=NULL; unsigned long size;
329 if((hnd=tjInitCompress())==NULL)
330 {printf("Error in tjInitCompress():\n%s\n", tjGetErrorStr()); goto finally;}
331 printf("Buffer size regression test\n");
332 for(j=1; j<48; j++)
333 {
334 for(i=1; i<(j==1?MAXLENGTH:48); i++)
335 {
336 if(i%100==0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", i, j);
337 if((bmpbuf=(unsigned char *)malloc(i*j*4))==NULL
338 || (jpgbuf=(unsigned char *)malloc(TJBUFSIZE(i, j)))==NULL)
339 {
340 printf("Memory allocation failure\n"); goto finally;
341 }
342 memset(bmpbuf, 0, i*j*4);
343 for(i2=0; i2<i*j; i2++)
344 {
345 bmpbuf[i2*4]=pixels[i2%9][2];
346 bmpbuf[i2*4+1]=pixels[i2%9][1];
347 bmpbuf[i2*2+2]=pixels[i2%9][0];
348 }
349 _catch(tjCompress(hnd, bmpbuf, i, i*4, j, 4,
350 jpgbuf, &size, TJ_444, 100, TJ_BGR));
351 free(bmpbuf); bmpbuf=NULL; free(jpgbuf); jpgbuf=NULL;
352
353 if((bmpbuf=(unsigned char *)malloc(j*i*4))==NULL
354 || (jpgbuf=(unsigned char *)malloc(TJBUFSIZE(j, i)))==NULL)
355 {
356 printf("Memory allocation failure\n"); goto finally;
357 }
358 for(i2=0; i2<j*i*4; i2++)
359 {
360 if(i2%2==0) bmpbuf[i2]=0xFF;
361 else bmpbuf[i2]=0;
362 }
363 _catch(tjCompress(hnd, bmpbuf, j, j*4, i, 4,
364 jpgbuf, &size, TJ_444, 100, TJ_BGR));
365 free(bmpbuf); bmpbuf=NULL; free(jpgbuf); jpgbuf=NULL;
366 }
367 }
368 printf("Done. \n");
369
370 finally:
371 if(bmpbuf) free(bmpbuf); if(jpgbuf) free(jpgbuf);
372 if(hnd) tjDestroy(hnd);
373}
374
375int main(int argc, char *argv[])
376{
377 dotest(35, 41, 3, TJ_444, "test");
378 dotest(35, 41, 4, TJ_444, "test");
379 dotest(35, 41, 3, TJ_GRAYSCALE, "test");
380 dotest(35, 41, 4, TJ_GRAYSCALE, "test");
381 dotest1();
382
383 return 0;
384}