blob: db46b6da4cbb9c8472ef800ce0e2b01b496a5230 [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
DRC65d05c12010-02-18 00:40:17 +000022#define _catch(f) {if((f)==-1) {printf("TJPEG: %s\n", tjGetErrorStr()); bailout();}}
DRC2e7b76b2009-04-03 12:04:24 +000023
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
DRC65d05c12010-02-18 00:40:17 +000027int exitstatus=0;
28#define bailout() {exitstatus=-1; goto finally;}
29
DRC2e7b76b2009-04-03 12:04:24 +000030int pixels[9][3]=
31{
32 {0, 255, 0},
33 {255, 0, 255},
34 {255, 255, 0},
35 {0, 0, 255},
36 {0, 255, 255},
37 {255, 0, 0},
38 {255, 255, 255},
39 {0, 0, 0},
40 {255, 0, 0}
41};
42
43void initbuf(unsigned char *buf, int w, int h, int ps, int flags)
44{
45 int roffset=(flags&TJ_BGR)?2:0, goffset=1, boffset=(flags&TJ_BGR)?0:2, i,
46 _i, j;
47 if(flags&TJ_ALPHAFIRST) {roffset++; goffset++; boffset++;}
48 memset(buf, 0, w*h*ps);
DRC09854f52010-11-04 22:39:59 +000049 if(ps==1)
50 {
51 for(_i=0; _i<16; _i++)
52 {
53 if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i;
54 for(j=0; j<w; j++)
55 {
56 if(((_i/8)+(j/8))%2==0) buf[w*i+j]=255;
57 else buf[w*i+j]=76;
58 }
59 }
60 for(_i=16; _i<h; _i++)
61 {
62 if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i;
63 for(j=0; j<w; j++)
64 {
65 if(((_i/8)+(j/8))%2==0) buf[w*i+j]=0;
66 else buf[w*i+j]=226;
67 }
68 }
69 return;
70 }
DRC2e7b76b2009-04-03 12:04:24 +000071 for(_i=0; _i<16; _i++)
72 {
73 if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i;
74 for(j=0; j<w; j++)
75 {
76 buf[(w*i+j)*ps+roffset]=255;
77 if(((_i/8)+(j/8))%2==0)
78 {
79 buf[(w*i+j)*ps+goffset]=255;
80 buf[(w*i+j)*ps+boffset]=255;
81 }
82 }
83 }
84 for(_i=16; _i<h; _i++)
85 {
86 if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i;
87 for(j=0; j<w; j++)
88 {
89 if(((_i/8)+(j/8))%2!=0)
90 {
91 buf[(w*i+j)*ps+roffset]=255;
92 buf[(w*i+j)*ps+goffset]=255;
93 }
94 }
95 }
96}
97
DRCea197882010-02-18 05:12:58 +000098void dumpbuf(unsigned char *buf, int w, int h, int ps, int flags)
DRC2e7b76b2009-04-03 12:04:24 +000099{
100 int roffset=(flags&TJ_BGR)?2:0, goffset=1, boffset=(flags&TJ_BGR)?0:2, i,
101 j;
102 for(i=0; i<h; i++)
103 {
104 for(j=0; j<w; j++)
105 {
106 printf("%.3d/%.3d/%.3d ", buf[(w*i+j)*ps+roffset],
107 buf[(w*i+j)*ps+roffset], buf[(w*i+j)*ps+roffset]);
108 }
109 printf("\n");
110 }
111}
112
113int checkbuf(unsigned char *buf, int w, int h, int ps, int subsamp, int flags)
114{
115 int roffset=(flags&TJ_BGR)?2:0, goffset=1, boffset=(flags&TJ_BGR)?0:2, i,
116 _i, j;
117 if(flags&TJ_ALPHAFIRST) {roffset++; goffset++; boffset++;}
DRC09854f52010-11-04 22:39:59 +0000118 if(ps==1) roffset=goffset=boffset=0;
DRC2e7b76b2009-04-03 12:04:24 +0000119 if(subsamp==TJ_GRAYSCALE)
120 {
121 for(_i=0; _i<16; _i++)
122 {
123 if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i;
124 for(j=0; j<w; j++)
125 {
126 unsigned char r=buf[(w*i+j)*ps+roffset],
127 g=buf[(w*i+j)*ps+goffset],
128 b=buf[(w*i+j)*ps+boffset];
129 if(((_i/8)+(j/8))%2==0)
130 {
131 if(r<253 || g<253 || b<253) return 0;
132 }
133 else
134 {
135 if(r<74 || r>78 || g<74 || g>78 || b<74 || b>78) return 0;
136 }
137 }
138 }
139 for(_i=16; _i<h; _i++)
140 {
141 if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i;
142 for(j=0; j<w; j++)
143 {
144 unsigned char r=buf[(w*i+j)*ps+roffset],
145 g=buf[(w*i+j)*ps+goffset],
146 b=buf[(w*i+j)*ps+boffset];
147 if(((_i/8)+(j/8))%2==0)
148 {
149 if(r>2 || g>2 || b>2) return 0;
150 }
151 else
152 {
153 if(r<224 || r>228 || g<224 || g>228 || b<224 || b>228) return 0;
154 }
155 }
156 }
157 }
158 else
159 {
160 for(_i=0; _i<16; _i++)
161 {
162 if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i;
163 for(j=0; j<w; j++)
164 {
165 if(buf[(w*i+j)*ps+roffset]<253) return 0;
166 if(((_i/8)+(j/8))%2==0)
167 {
168 if(buf[(w*i+j)*ps+goffset]<253) return 0;
169 if(buf[(w*i+j)*ps+boffset]<253) return 0;
170 }
171 else
172 {
173 if(buf[(w*i+j)*ps+goffset]>2) return 0;
174 if(buf[(w*i+j)*ps+boffset]>2) return 0;
175 }
176 }
177 }
178 for(_i=16; _i<h; _i++)
179 {
180 if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i;
181 for(j=0; j<w; j++)
182 {
183 if(buf[(w*i+j)*ps+boffset]>2) return 0;
184 if(((_i/8)+(j/8))%2==0)
185 {
186 if(buf[(w*i+j)*ps+roffset]>2) return 0;
187 if(buf[(w*i+j)*ps+goffset]>2) return 0;
188 }
189 else
190 {
191 if(buf[(w*i+j)*ps+roffset]<253) return 0;
192 if(buf[(w*i+j)*ps+goffset]<253) return 0;
193 }
194 }
195 }
196 }
197 return 1;
198}
199
200void writejpeg(unsigned char *jpegbuf, unsigned long jpgbufsize, char *filename)
201{
202 FILE *outfile=NULL;
203 if((outfile=fopen(filename, "wb"))==NULL)
204 {
205 printf("ERROR: Could not open %s for writing.\n", filename);
DRC65d05c12010-02-18 00:40:17 +0000206 bailout();
DRC2e7b76b2009-04-03 12:04:24 +0000207 }
208 if(fwrite(jpegbuf, jpgbufsize, 1, outfile)!=1)
209 {
210 printf("ERROR: Could not write to %s.\n", filename);
DRC65d05c12010-02-18 00:40:17 +0000211 bailout();
DRC2e7b76b2009-04-03 12:04:24 +0000212 }
213
214 finally:
215 if(outfile) fclose(outfile);
216}
217
218void gentestjpeg(tjhandle hnd, unsigned char *jpegbuf, unsigned long *size,
219 int w, int h, int ps, char *basefilename, int subsamp, int qual, int flags)
220{
221 char tempstr[1024]; unsigned char *bmpbuf=NULL;
222 const char *pixformat; double t;
223
224 if(flags&TJ_BGR)
225 {
226 if(ps==3) pixformat="BGR";
227 else {if(flags&TJ_ALPHAFIRST) pixformat="ABGR"; else pixformat="BGRA";}
228 }
229 else
230 {
231 if(ps==3) pixformat="RGB";
232 else {if(flags&TJ_ALPHAFIRST) pixformat="ARGB"; else pixformat="RGBA";}
233 }
DRC09854f52010-11-04 22:39:59 +0000234 if(ps==1) pixformat="Grayscale";
DRC2e7b76b2009-04-03 12:04:24 +0000235 printf("%s %s -> %s Q%d ... ", pixformat,
236 (flags&TJ_BOTTOMUP)?"Bottom-Up":"Top-Down ", _subnamel[subsamp], qual);
237
238 if((bmpbuf=(unsigned char *)malloc(w*h*ps+1))==NULL)
239 {
DRC65d05c12010-02-18 00:40:17 +0000240 printf("ERROR: Could not allocate buffer\n"); bailout();
DRC2e7b76b2009-04-03 12:04:24 +0000241 }
242 initbuf(bmpbuf, w, h, ps, flags);
243 memset(jpegbuf, 0, TJBUFSIZE(w, h));
244
245 t=rrtime();
246 _catch(tjCompress(hnd, bmpbuf, w, 0, h, ps, jpegbuf, size, subsamp, qual, flags));
247 t=rrtime()-t;
248
249 sprintf(tempstr, "%s_enc_%s_%s_%sQ%d.jpg", basefilename, pixformat,
250 (flags&TJ_BOTTOMUP)? "BU":"TD", _subnames[subsamp], qual);
251 writejpeg(jpegbuf, *size, tempstr);
252 printf("Done. %f ms\n Result in %s\n", t*1000., tempstr);
253
254 finally:
255 if(bmpbuf) free(bmpbuf);
256}
257
258void gentestbmp(tjhandle hnd, unsigned char *jpegbuf, unsigned long jpegsize,
259 int w, int h, int ps, char *basefilename, int subsamp, int qual, int flags)
260{
261 unsigned char *bmpbuf=NULL;
262 const char *pixformat; int _w=0, _h=0; double t;
263
264 if(flags&TJ_BGR)
265 {
266 if(ps==3) pixformat="BGR";
267 else {if(flags&TJ_ALPHAFIRST) pixformat="ABGR"; else pixformat="BGRA";}
268 }
269 else
270 {
271 if(ps==3) pixformat="RGB";
272 else {if(flags&TJ_ALPHAFIRST) pixformat="ARGB"; else pixformat="RGBA";}
273 }
DRC09854f52010-11-04 22:39:59 +0000274 if(ps==1) pixformat="Grayscale";
DRC2e7b76b2009-04-03 12:04:24 +0000275 printf("JPEG -> %s %s ... ", pixformat, (flags&TJ_BOTTOMUP)?"Bottom-Up":"Top-Down ");
276
277 _catch(tjDecompressHeader(hnd, jpegbuf, jpegsize, &_w, &_h));
278 if(_w!=w || _h!=h)
279 {
DRC65d05c12010-02-18 00:40:17 +0000280 printf("Incorrect JPEG header\n"); bailout();
DRC2e7b76b2009-04-03 12:04:24 +0000281 }
282
283 if((bmpbuf=(unsigned char *)malloc(w*h*ps+1))==NULL)
284 {
DRC65d05c12010-02-18 00:40:17 +0000285 printf("ERROR: Could not allocate buffer\n"); bailout();
DRC2e7b76b2009-04-03 12:04:24 +0000286 }
287 memset(bmpbuf, 0, w*ps*h);
288
289 t=rrtime();
290 _catch(tjDecompress(hnd, jpegbuf, jpegsize, bmpbuf, w, w*ps, h, ps, flags));
291 t=rrtime()-t;
292
293 if(checkbuf(bmpbuf, w, h, ps, subsamp, flags)) printf("Passed.");
294 else {printf("FAILED!"); dumpbuf(bmpbuf, w, h, ps, flags);}
295
296 printf(" %f ms\n\n", t*1000.);
297
298 finally:
299 if(bmpbuf) free(bmpbuf);
300}
301
302void dotest(int w, int h, int ps, int subsamp, char *basefilename)
303{
304 tjhandle hnd=NULL, dhnd=NULL; unsigned char *jpegbuf=NULL;
305 unsigned long size;
306
307 if((jpegbuf=(unsigned char *)malloc(TJBUFSIZE(w, h))) == NULL)
308 {
DRC65d05c12010-02-18 00:40:17 +0000309 puts("ERROR: Could not allocate buffer."); bailout();
DRC2e7b76b2009-04-03 12:04:24 +0000310 }
311
312 if((hnd=tjInitCompress())==NULL)
DRC65d05c12010-02-18 00:40:17 +0000313 {printf("Error in tjInitCompress():\n%s\n", tjGetErrorStr()); bailout();}
DRC2e7b76b2009-04-03 12:04:24 +0000314 if((dhnd=tjInitDecompress())==NULL)
DRC65d05c12010-02-18 00:40:17 +0000315 {printf("Error in tjInitDecompress():\n%s\n", tjGetErrorStr()); bailout();}
DRC2e7b76b2009-04-03 12:04:24 +0000316
317 gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, 0);
318 gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 100, 0);
319
DRC09854f52010-11-04 22:39:59 +0000320 if(ps==1) goto finally;
321
DRC2e7b76b2009-04-03 12:04:24 +0000322 gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_BGR);
323 gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 100, TJ_BGR);
324
325 gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_BOTTOMUP);
326 gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 100, TJ_BOTTOMUP);
327
328 gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_BGR|TJ_BOTTOMUP);
329 gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 100, TJ_BGR|TJ_BOTTOMUP);
330
331 if(ps==4)
332 {
333 gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST);
334 gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST);
335
336 gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST|TJ_BGR);
337 gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST|TJ_BGR);
338
339 gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST|TJ_BOTTOMUP);
340 gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST|TJ_BOTTOMUP);
341
342 gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST|TJ_BGR|TJ_BOTTOMUP);
343 gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST|TJ_BGR|TJ_BOTTOMUP);
344 }
345
346 finally:
347 if(hnd) tjDestroy(hnd);
348 if(dhnd) tjDestroy(dhnd);
349
350 if(jpegbuf) free(jpegbuf);
351}
352
353#define MAXLENGTH 2048
354
355void dotest1(void)
356{
357 int i, j, i2; unsigned char *bmpbuf=NULL, *jpgbuf=NULL;
358 tjhandle hnd=NULL; unsigned long size;
359 if((hnd=tjInitCompress())==NULL)
DRC65d05c12010-02-18 00:40:17 +0000360 {printf("Error in tjInitCompress():\n%s\n", tjGetErrorStr()); bailout();}
DRC2e7b76b2009-04-03 12:04:24 +0000361 printf("Buffer size regression test\n");
362 for(j=1; j<48; j++)
363 {
364 for(i=1; i<(j==1?MAXLENGTH:48); i++)
365 {
366 if(i%100==0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", i, j);
367 if((bmpbuf=(unsigned char *)malloc(i*j*4))==NULL
368 || (jpgbuf=(unsigned char *)malloc(TJBUFSIZE(i, j)))==NULL)
369 {
DRC65d05c12010-02-18 00:40:17 +0000370 printf("Memory allocation failure\n"); bailout();
DRC2e7b76b2009-04-03 12:04:24 +0000371 }
372 memset(bmpbuf, 0, i*j*4);
373 for(i2=0; i2<i*j; i2++)
374 {
375 bmpbuf[i2*4]=pixels[i2%9][2];
376 bmpbuf[i2*4+1]=pixels[i2%9][1];
377 bmpbuf[i2*2+2]=pixels[i2%9][0];
378 }
379 _catch(tjCompress(hnd, bmpbuf, i, i*4, j, 4,
380 jpgbuf, &size, TJ_444, 100, TJ_BGR));
381 free(bmpbuf); bmpbuf=NULL; free(jpgbuf); jpgbuf=NULL;
382
383 if((bmpbuf=(unsigned char *)malloc(j*i*4))==NULL
384 || (jpgbuf=(unsigned char *)malloc(TJBUFSIZE(j, i)))==NULL)
385 {
DRC65d05c12010-02-18 00:40:17 +0000386 printf("Memory allocation failure\n"); bailout();
DRC2e7b76b2009-04-03 12:04:24 +0000387 }
388 for(i2=0; i2<j*i*4; i2++)
389 {
390 if(i2%2==0) bmpbuf[i2]=0xFF;
391 else bmpbuf[i2]=0;
392 }
393 _catch(tjCompress(hnd, bmpbuf, j, j*4, i, 4,
394 jpgbuf, &size, TJ_444, 100, TJ_BGR));
395 free(bmpbuf); bmpbuf=NULL; free(jpgbuf); jpgbuf=NULL;
396 }
397 }
398 printf("Done. \n");
399
400 finally:
401 if(bmpbuf) free(bmpbuf); if(jpgbuf) free(jpgbuf);
402 if(hnd) tjDestroy(hnd);
403}
404
405int main(int argc, char *argv[])
406{
407 dotest(35, 41, 3, TJ_444, "test");
408 dotest(35, 41, 4, TJ_444, "test");
DRC09854f52010-11-04 22:39:59 +0000409 dotest(35, 41, 1, TJ_GRAYSCALE, "test");
DRC2e7b76b2009-04-03 12:04:24 +0000410 dotest(35, 41, 3, TJ_GRAYSCALE, "test");
411 dotest(35, 41, 4, TJ_GRAYSCALE, "test");
412 dotest1();
413
DRC65d05c12010-02-18 00:40:17 +0000414 return exitstatus;
DRC2e7b76b2009-04-03 12:04:24 +0000415}