DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 1 | /* Copyright (C)2004 Landmark Graphics Corporation |
| 2 | * Copyright (C)2005 Sun Microsystems, Inc. |
DRC | cfef7f1 | 2011-02-15 05:17:20 +0000 | [diff] [blame] | 3 | * Copyright (C)2009-2011 D. R. Commander |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 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" |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 21 | #ifndef _WIN32 |
| 22 | #define stricmp strcasecmp |
| 23 | #endif |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 24 | |
DRC | 65d05c1 | 2010-02-18 00:40:17 +0000 | [diff] [blame] | 25 | #define _catch(f) {if((f)==-1) {printf("TJPEG: %s\n", tjGetErrorStr()); bailout();}} |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 26 | |
DRC | 61e51f9 | 2009-04-05 21:53:20 +0000 | [diff] [blame] | 27 | const char *_subnamel[NUMSUBOPT]={"4:4:4", "4:2:2", "4:2:0", "GRAY"}; |
| 28 | const char *_subnames[NUMSUBOPT]={"444", "422", "420", "GRAY"}; |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 29 | const int _hsf[NUMSUBOPT]={1, 2, 2, 1}; |
| 30 | const int _vsf[NUMSUBOPT]={1, 1, 2, 1}; |
| 31 | |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 32 | enum {YUVENCODE=1, YUVDECODE}; |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 33 | int yuv=0; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 34 | |
DRC | 65d05c1 | 2010-02-18 00:40:17 +0000 | [diff] [blame] | 35 | int exitstatus=0; |
| 36 | #define bailout() {exitstatus=-1; goto finally;} |
| 37 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 38 | int pixels[9][3]= |
| 39 | { |
| 40 | {0, 255, 0}, |
| 41 | {255, 0, 255}, |
| 42 | {255, 255, 0}, |
| 43 | {0, 0, 255}, |
| 44 | {0, 255, 255}, |
| 45 | {255, 0, 0}, |
| 46 | {255, 255, 255}, |
| 47 | {0, 0, 0}, |
| 48 | {255, 0, 0} |
| 49 | }; |
| 50 | |
| 51 | void initbuf(unsigned char *buf, int w, int h, int ps, int flags) |
| 52 | { |
| 53 | int roffset=(flags&TJ_BGR)?2:0, goffset=1, boffset=(flags&TJ_BGR)?0:2, i, |
| 54 | _i, j; |
| 55 | if(flags&TJ_ALPHAFIRST) {roffset++; goffset++; boffset++;} |
| 56 | memset(buf, 0, w*h*ps); |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 57 | if(ps==1) |
| 58 | { |
| 59 | for(_i=0; _i<16; _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) buf[w*i+j]=255; |
| 65 | else buf[w*i+j]=76; |
| 66 | } |
| 67 | } |
| 68 | for(_i=16; _i<h; _i++) |
| 69 | { |
| 70 | if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i; |
| 71 | for(j=0; j<w; j++) |
| 72 | { |
| 73 | if(((_i/8)+(j/8))%2==0) buf[w*i+j]=0; |
| 74 | else buf[w*i+j]=226; |
| 75 | } |
| 76 | } |
| 77 | return; |
| 78 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 79 | for(_i=0; _i<16; _i++) |
| 80 | { |
| 81 | if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i; |
| 82 | for(j=0; j<w; j++) |
| 83 | { |
| 84 | buf[(w*i+j)*ps+roffset]=255; |
| 85 | if(((_i/8)+(j/8))%2==0) |
| 86 | { |
| 87 | buf[(w*i+j)*ps+goffset]=255; |
| 88 | buf[(w*i+j)*ps+boffset]=255; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | for(_i=16; _i<h; _i++) |
| 93 | { |
| 94 | if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i; |
| 95 | for(j=0; j<w; j++) |
| 96 | { |
| 97 | if(((_i/8)+(j/8))%2!=0) |
| 98 | { |
| 99 | buf[(w*i+j)*ps+roffset]=255; |
| 100 | buf[(w*i+j)*ps+goffset]=255; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
DRC | ea19788 | 2010-02-18 05:12:58 +0000 | [diff] [blame] | 106 | void dumpbuf(unsigned char *buf, int w, int h, int ps, int flags) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 107 | { |
| 108 | int roffset=(flags&TJ_BGR)?2:0, goffset=1, boffset=(flags&TJ_BGR)?0:2, i, |
| 109 | j; |
DRC | da58a91 | 2011-02-27 00:07:51 +0000 | [diff] [blame] | 110 | printf("\n"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 111 | for(i=0; i<h; i++) |
| 112 | { |
| 113 | for(j=0; j<w; j++) |
| 114 | { |
| 115 | printf("%.3d/%.3d/%.3d ", buf[(w*i+j)*ps+roffset], |
DRC | cce6b43 | 2011-02-23 01:41:26 +0000 | [diff] [blame] | 116 | buf[(w*i+j)*ps+goffset], buf[(w*i+j)*ps+boffset]); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 117 | } |
| 118 | printf("\n"); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | int checkbuf(unsigned char *buf, int w, int h, int ps, int subsamp, int flags) |
| 123 | { |
| 124 | int roffset=(flags&TJ_BGR)?2:0, goffset=1, boffset=(flags&TJ_BGR)?0:2, i, |
| 125 | _i, j; |
| 126 | if(flags&TJ_ALPHAFIRST) {roffset++; goffset++; boffset++;} |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 127 | if(ps==1) roffset=goffset=boffset=0; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 128 | if(subsamp==TJ_GRAYSCALE) |
| 129 | { |
| 130 | for(_i=0; _i<16; _i++) |
| 131 | { |
| 132 | if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i; |
| 133 | for(j=0; j<w; j++) |
| 134 | { |
| 135 | unsigned char r=buf[(w*i+j)*ps+roffset], |
| 136 | g=buf[(w*i+j)*ps+goffset], |
| 137 | b=buf[(w*i+j)*ps+boffset]; |
| 138 | if(((_i/8)+(j/8))%2==0) |
| 139 | { |
| 140 | if(r<253 || g<253 || b<253) return 0; |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | if(r<74 || r>78 || g<74 || g>78 || b<74 || b>78) return 0; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | for(_i=16; _i<h; _i++) |
| 149 | { |
| 150 | if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i; |
| 151 | for(j=0; j<w; j++) |
| 152 | { |
| 153 | unsigned char r=buf[(w*i+j)*ps+roffset], |
| 154 | g=buf[(w*i+j)*ps+goffset], |
| 155 | b=buf[(w*i+j)*ps+boffset]; |
| 156 | if(((_i/8)+(j/8))%2==0) |
| 157 | { |
| 158 | if(r>2 || g>2 || b>2) return 0; |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | if(r<224 || r>228 || g<224 || g>228 || b<224 || b>228) return 0; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | for(_i=0; _i<16; _i++) |
| 170 | { |
| 171 | if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i; |
| 172 | for(j=0; j<w; j++) |
| 173 | { |
| 174 | if(buf[(w*i+j)*ps+roffset]<253) return 0; |
| 175 | if(((_i/8)+(j/8))%2==0) |
| 176 | { |
| 177 | if(buf[(w*i+j)*ps+goffset]<253) return 0; |
| 178 | if(buf[(w*i+j)*ps+boffset]<253) return 0; |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | if(buf[(w*i+j)*ps+goffset]>2) return 0; |
| 183 | if(buf[(w*i+j)*ps+boffset]>2) return 0; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | for(_i=16; _i<h; _i++) |
| 188 | { |
| 189 | if(flags&TJ_BOTTOMUP) i=h-_i-1; else i=_i; |
| 190 | for(j=0; j<w; j++) |
| 191 | { |
| 192 | if(buf[(w*i+j)*ps+boffset]>2) return 0; |
| 193 | if(((_i/8)+(j/8))%2==0) |
| 194 | { |
| 195 | if(buf[(w*i+j)*ps+roffset]>2) return 0; |
| 196 | if(buf[(w*i+j)*ps+goffset]>2) return 0; |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | if(buf[(w*i+j)*ps+roffset]<253) return 0; |
| 201 | if(buf[(w*i+j)*ps+goffset]<253) return 0; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | return 1; |
| 207 | } |
| 208 | |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 209 | #define checkval(v, cv) { \ |
| 210 | if(v<cv-1 || v>cv+1) { \ |
| 211 | printf("\nComp. %s at %d,%d should be %d, not %d\n", #v, i, j, cv, v); \ |
| 212 | retval=0; goto bailout; \ |
| 213 | }} |
| 214 | |
| 215 | #define checkval0(v) { \ |
| 216 | if(v>1) { \ |
| 217 | printf("\nComp. %s at %d,%d should be 0, not %d\n", #v, i, j, v); \ |
| 218 | retval=0; goto bailout; \ |
| 219 | }} |
| 220 | |
| 221 | #define checkval255(v) { \ |
DRC | 6ee5459 | 2011-03-01 08:18:30 +0000 | [diff] [blame] | 222 | if(v<254) { \ |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 223 | printf("\nComp. %s at %d,%d should be 255, not %d\n", #v, i, j, v); \ |
| 224 | retval=0; goto bailout; \ |
| 225 | }} |
| 226 | |
| 227 | #define PAD(v, p) ((v+(p)-1)&(~((p)-1))) |
| 228 | |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 229 | int checkbufyuv(unsigned char *buf, int w, int h, int subsamp) |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 230 | { |
| 231 | int i, j; |
| 232 | int hsf=_hsf[subsamp], vsf=_vsf[subsamp]; |
| 233 | int pw=PAD(w, hsf), ph=PAD(h, vsf); |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 234 | int cw=pw/hsf, ch=ph/vsf; |
| 235 | int ypitch=PAD(pw, 4), uvpitch=PAD(cw, 4); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 236 | int retval=1; |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 237 | |
| 238 | for(i=0; i<16; i++) |
| 239 | { |
| 240 | for(j=0; j<pw; j++) |
| 241 | { |
| 242 | unsigned char y=buf[ypitch*i+j]; |
| 243 | if(((i/8)+(j/8))%2==0) checkval255(y) |
| 244 | else checkval(y, 76) |
| 245 | } |
| 246 | } |
| 247 | for(i=16; i<ph; i++) |
| 248 | { |
| 249 | for(j=0; j<pw; j++) |
| 250 | { |
| 251 | unsigned char y=buf[ypitch*i+j]; |
| 252 | if(((i/8)+(j/8))%2==0) checkval0(y) |
| 253 | else checkval(y, 226) |
| 254 | } |
| 255 | } |
| 256 | if(subsamp!=TJ_GRAYSCALE) |
| 257 | { |
| 258 | for(i=0; i<16/vsf; i++) |
| 259 | { |
| 260 | for(j=0; j<cw; j++) |
| 261 | { |
| 262 | unsigned char u=buf[ypitch*ph + (uvpitch*i+j)], |
| 263 | v=buf[ypitch*ph + uvpitch*ch + (uvpitch*i+j)]; |
| 264 | if(((i*vsf/8)+(j*hsf/8))%2==0) |
| 265 | { |
| 266 | checkval(u, 128); checkval(v, 128); |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | checkval(u, 85); checkval255(v); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | for(i=16/vsf; i<ch; i++) |
| 275 | { |
| 276 | for(j=0; j<cw; j++) |
| 277 | { |
| 278 | unsigned char u=buf[ypitch*ph + (uvpitch*i+j)], |
| 279 | v=buf[ypitch*ph + uvpitch*ch + (uvpitch*i+j)]; |
| 280 | if(((i*vsf/8)+(j*hsf/8))%2==0) |
| 281 | { |
| 282 | checkval(u, 128); checkval(v, 128); |
| 283 | } |
| 284 | else |
| 285 | { |
| 286 | checkval0(u); checkval(v, 149); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | bailout: |
| 293 | if(retval==0) |
| 294 | { |
| 295 | for(i=0; i<ph; i++) |
| 296 | { |
| 297 | for(j=0; j<pw; j++) |
| 298 | printf("%.3d ", buf[ypitch*i+j]); |
| 299 | printf("\n"); |
| 300 | } |
| 301 | printf("\n"); |
| 302 | for(i=0; i<ch; i++) |
| 303 | { |
| 304 | for(j=0; j<cw; j++) |
| 305 | printf("%.3d ", buf[ypitch*ph + (uvpitch*i+j)]); |
| 306 | printf("\n"); |
| 307 | } |
| 308 | printf("\n"); |
| 309 | for(i=0; i<ch; i++) |
| 310 | { |
| 311 | for(j=0; j<cw; j++) |
| 312 | printf("%.3d ", buf[ypitch*ph + uvpitch*ch + (uvpitch*i+j)]); |
| 313 | printf("\n"); |
| 314 | } |
| 315 | printf("\n"); |
| 316 | } |
| 317 | |
| 318 | return retval; |
| 319 | } |
| 320 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 321 | void writejpeg(unsigned char *jpegbuf, unsigned long jpgbufsize, char *filename) |
| 322 | { |
| 323 | FILE *outfile=NULL; |
| 324 | if((outfile=fopen(filename, "wb"))==NULL) |
| 325 | { |
| 326 | printf("ERROR: Could not open %s for writing.\n", filename); |
DRC | 65d05c1 | 2010-02-18 00:40:17 +0000 | [diff] [blame] | 327 | bailout(); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 328 | } |
| 329 | if(fwrite(jpegbuf, jpgbufsize, 1, outfile)!=1) |
| 330 | { |
| 331 | printf("ERROR: Could not write to %s.\n", filename); |
DRC | 65d05c1 | 2010-02-18 00:40:17 +0000 | [diff] [blame] | 332 | bailout(); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | finally: |
| 336 | if(outfile) fclose(outfile); |
| 337 | } |
| 338 | |
| 339 | void gentestjpeg(tjhandle hnd, unsigned char *jpegbuf, unsigned long *size, |
| 340 | int w, int h, int ps, char *basefilename, int subsamp, int qual, int flags) |
| 341 | { |
| 342 | char tempstr[1024]; unsigned char *bmpbuf=NULL; |
| 343 | const char *pixformat; double t; |
| 344 | |
| 345 | if(flags&TJ_BGR) |
| 346 | { |
| 347 | if(ps==3) pixformat="BGR"; |
DRC | b4b3140 | 2011-02-19 17:31:24 +0000 | [diff] [blame] | 348 | else {if(flags&TJ_ALPHAFIRST) pixformat="XBGR"; else pixformat="BGRX";} |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 349 | } |
| 350 | else |
| 351 | { |
| 352 | if(ps==3) pixformat="RGB"; |
DRC | b4b3140 | 2011-02-19 17:31:24 +0000 | [diff] [blame] | 353 | else {if(flags&TJ_ALPHAFIRST) pixformat="XRGB"; else pixformat="RGBX";} |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 354 | } |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 355 | if(ps==1) pixformat="Grayscale"; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 356 | if(yuv==YUVENCODE) |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 357 | printf("%s %s -> %s YUV ... ", pixformat, |
| 358 | (flags&TJ_BOTTOMUP)?"Bottom-Up":"Top-Down ", _subnamel[subsamp]); |
| 359 | else |
| 360 | printf("%s %s -> %s Q%d ... ", pixformat, |
| 361 | (flags&TJ_BOTTOMUP)?"Bottom-Up":"Top-Down ", _subnamel[subsamp], qual); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 362 | |
| 363 | if((bmpbuf=(unsigned char *)malloc(w*h*ps+1))==NULL) |
| 364 | { |
DRC | 65d05c1 | 2010-02-18 00:40:17 +0000 | [diff] [blame] | 365 | printf("ERROR: Could not allocate buffer\n"); bailout(); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 366 | } |
| 367 | initbuf(bmpbuf, w, h, ps, flags); |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 368 | memset(jpegbuf, 0, |
| 369 | yuv==YUVENCODE? TJBUFSIZEYUV(w, h, subsamp):TJBUFSIZE(w, h)); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 370 | |
| 371 | t=rrtime(); |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 372 | if(yuv==YUVENCODE) |
| 373 | { |
| 374 | _catch(tjEncodeYUV(hnd, bmpbuf, w, 0, h, ps, jpegbuf, subsamp, flags)); |
| 375 | *size=TJBUFSIZEYUV(w, h, subsamp); |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | _catch(tjCompress(hnd, bmpbuf, w, 0, h, ps, jpegbuf, size, subsamp, qual, |
| 380 | flags)); |
| 381 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 382 | t=rrtime()-t; |
| 383 | |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 384 | if(yuv==YUVENCODE) |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 385 | sprintf(tempstr, "%s_enc_%s_%s_%s.yuv", basefilename, pixformat, |
| 386 | (flags&TJ_BOTTOMUP)? "BU":"TD", _subnames[subsamp]); |
| 387 | else |
| 388 | sprintf(tempstr, "%s_enc_%s_%s_%sQ%d.jpg", basefilename, pixformat, |
| 389 | (flags&TJ_BOTTOMUP)? "BU":"TD", _subnames[subsamp], qual); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 390 | writejpeg(jpegbuf, *size, tempstr); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 391 | if(yuv==YUVENCODE) |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 392 | { |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 393 | if(checkbufyuv(jpegbuf, w, h, subsamp)) printf("Passed."); |
DRC | cfef7f1 | 2011-02-15 05:17:20 +0000 | [diff] [blame] | 394 | else {printf("FAILED!"); exitstatus=-1;} |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 395 | } |
| 396 | else printf("Done."); |
| 397 | printf(" %f ms\n Result in %s\n", t*1000., tempstr); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 398 | |
| 399 | finally: |
| 400 | if(bmpbuf) free(bmpbuf); |
| 401 | } |
| 402 | |
| 403 | void gentestbmp(tjhandle hnd, unsigned char *jpegbuf, unsigned long jpegsize, |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 404 | int w, int h, int ps, char *basefilename, int subsamp, int flags) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 405 | { |
| 406 | unsigned char *bmpbuf=NULL; |
| 407 | const char *pixformat; int _w=0, _h=0; double t; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 408 | unsigned long size=0; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 409 | |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 410 | if(yuv==YUVENCODE) return; |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 411 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 412 | if(flags&TJ_BGR) |
| 413 | { |
| 414 | if(ps==3) pixformat="BGR"; |
DRC | b4b3140 | 2011-02-19 17:31:24 +0000 | [diff] [blame] | 415 | else {if(flags&TJ_ALPHAFIRST) pixformat="XBGR"; else pixformat="BGRX";} |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 416 | } |
| 417 | else |
| 418 | { |
| 419 | if(ps==3) pixformat="RGB"; |
DRC | b4b3140 | 2011-02-19 17:31:24 +0000 | [diff] [blame] | 420 | else {if(flags&TJ_ALPHAFIRST) pixformat="XRGB"; else pixformat="RGBX";} |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 421 | } |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 422 | if(ps==1) pixformat="Grayscale"; |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 423 | if(yuv==YUVDECODE) |
| 424 | printf("JPEG -> YUV %s ... ", _subnames[subsamp]); |
| 425 | else |
| 426 | printf("JPEG -> %s %s ... ", pixformat, |
| 427 | (flags&TJ_BOTTOMUP)?"Bottom-Up":"Top-Down "); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 428 | |
| 429 | _catch(tjDecompressHeader(hnd, jpegbuf, jpegsize, &_w, &_h)); |
| 430 | if(_w!=w || _h!=h) |
| 431 | { |
DRC | 65d05c1 | 2010-02-18 00:40:17 +0000 | [diff] [blame] | 432 | printf("Incorrect JPEG header\n"); bailout(); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 433 | } |
| 434 | |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 435 | if(yuv==YUVDECODE) size=TJBUFSIZEYUV(w, h, subsamp); |
| 436 | else size=w*h*ps+1; |
| 437 | if((bmpbuf=(unsigned char *)malloc(size))==NULL) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 438 | { |
DRC | 65d05c1 | 2010-02-18 00:40:17 +0000 | [diff] [blame] | 439 | printf("ERROR: Could not allocate buffer\n"); bailout(); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 440 | } |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 441 | memset(bmpbuf, 0, size); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 442 | |
| 443 | t=rrtime(); |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 444 | if(yuv==YUVDECODE) |
| 445 | { |
| 446 | _catch(tjDecompressToYUV(hnd, jpegbuf, jpegsize, bmpbuf, flags)); |
| 447 | } |
| 448 | else |
| 449 | { |
| 450 | _catch(tjDecompress(hnd, jpegbuf, jpegsize, bmpbuf, w, w*ps, h, ps, |
| 451 | flags)); |
| 452 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 453 | t=rrtime()-t; |
| 454 | |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 455 | if(yuv==YUVDECODE) |
| 456 | { |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 457 | if(checkbufyuv(bmpbuf, w, h, subsamp)) printf("Passed."); |
DRC | cfef7f1 | 2011-02-15 05:17:20 +0000 | [diff] [blame] | 458 | else {printf("FAILED!"); exitstatus=-1;} |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 459 | } |
| 460 | else |
| 461 | { |
| 462 | if(checkbuf(bmpbuf, w, h, ps, subsamp, flags)) printf("Passed."); |
DRC | cfef7f1 | 2011-02-15 05:17:20 +0000 | [diff] [blame] | 463 | else |
| 464 | { |
| 465 | printf("FAILED!"); exitstatus=-1; |
| 466 | dumpbuf(bmpbuf, w, h, ps, flags); |
| 467 | } |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 468 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 469 | printf(" %f ms\n\n", t*1000.); |
| 470 | |
| 471 | finally: |
| 472 | if(bmpbuf) free(bmpbuf); |
| 473 | } |
| 474 | |
| 475 | void dotest(int w, int h, int ps, int subsamp, char *basefilename) |
| 476 | { |
| 477 | tjhandle hnd=NULL, dhnd=NULL; unsigned char *jpegbuf=NULL; |
| 478 | unsigned long size; |
| 479 | |
DRC | eeab695 | 2011-02-25 00:02:04 +0000 | [diff] [blame] | 480 | size=(yuv==YUVENCODE? TJBUFSIZEYUV(w, h, subsamp):TJBUFSIZE(w, h)); |
| 481 | if((jpegbuf=(unsigned char *)malloc(size)) == NULL) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 482 | { |
DRC | 65d05c1 | 2010-02-18 00:40:17 +0000 | [diff] [blame] | 483 | puts("ERROR: Could not allocate buffer."); bailout(); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | if((hnd=tjInitCompress())==NULL) |
DRC | 65d05c1 | 2010-02-18 00:40:17 +0000 | [diff] [blame] | 487 | {printf("Error in tjInitCompress():\n%s\n", tjGetErrorStr()); bailout();} |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 488 | if((dhnd=tjInitDecompress())==NULL) |
DRC | 65d05c1 | 2010-02-18 00:40:17 +0000 | [diff] [blame] | 489 | {printf("Error in tjInitDecompress():\n%s\n", tjGetErrorStr()); bailout();} |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 490 | |
| 491 | gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, 0); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 492 | gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, 0); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 493 | |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 494 | if(ps==1 || yuv==YUVDECODE) goto finally; |
DRC | 09854f5 | 2010-11-04 22:39:59 +0000 | [diff] [blame] | 495 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 496 | gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_BGR); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 497 | gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, TJ_BGR); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 498 | |
| 499 | gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_BOTTOMUP); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 500 | gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, TJ_BOTTOMUP); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 501 | |
| 502 | gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_BGR|TJ_BOTTOMUP); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 503 | gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, TJ_BGR|TJ_BOTTOMUP); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 504 | |
| 505 | if(ps==4) |
| 506 | { |
| 507 | gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 508 | gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, TJ_ALPHAFIRST); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 509 | |
| 510 | gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST|TJ_BGR); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 511 | gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, TJ_ALPHAFIRST|TJ_BGR); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 512 | |
| 513 | gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST|TJ_BOTTOMUP); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 514 | gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, TJ_ALPHAFIRST|TJ_BOTTOMUP); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 515 | |
| 516 | gentestjpeg(hnd, jpegbuf, &size, w, h, ps, basefilename, subsamp, 100, TJ_ALPHAFIRST|TJ_BGR|TJ_BOTTOMUP); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 517 | gentestbmp(dhnd, jpegbuf, size, w, h, ps, basefilename, subsamp, TJ_ALPHAFIRST|TJ_BGR|TJ_BOTTOMUP); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | finally: |
| 521 | if(hnd) tjDestroy(hnd); |
| 522 | if(dhnd) tjDestroy(dhnd); |
| 523 | |
| 524 | if(jpegbuf) free(jpegbuf); |
| 525 | } |
| 526 | |
| 527 | #define MAXLENGTH 2048 |
| 528 | |
| 529 | void dotest1(void) |
| 530 | { |
| 531 | int i, j, i2; unsigned char *bmpbuf=NULL, *jpgbuf=NULL; |
| 532 | tjhandle hnd=NULL; unsigned long size; |
| 533 | if((hnd=tjInitCompress())==NULL) |
DRC | 65d05c1 | 2010-02-18 00:40:17 +0000 | [diff] [blame] | 534 | {printf("Error in tjInitCompress():\n%s\n", tjGetErrorStr()); bailout();} |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 535 | printf("Buffer size regression test\n"); |
| 536 | for(j=1; j<48; j++) |
| 537 | { |
| 538 | for(i=1; i<(j==1?MAXLENGTH:48); i++) |
| 539 | { |
| 540 | if(i%100==0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", i, j); |
| 541 | if((bmpbuf=(unsigned char *)malloc(i*j*4))==NULL |
| 542 | || (jpgbuf=(unsigned char *)malloc(TJBUFSIZE(i, j)))==NULL) |
| 543 | { |
DRC | 65d05c1 | 2010-02-18 00:40:17 +0000 | [diff] [blame] | 544 | printf("Memory allocation failure\n"); bailout(); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 545 | } |
| 546 | memset(bmpbuf, 0, i*j*4); |
| 547 | for(i2=0; i2<i*j; i2++) |
| 548 | { |
| 549 | bmpbuf[i2*4]=pixels[i2%9][2]; |
| 550 | bmpbuf[i2*4+1]=pixels[i2%9][1]; |
DRC | d5a69af | 2011-02-25 02:13:07 +0000 | [diff] [blame] | 551 | bmpbuf[i2*4+2]=pixels[i2%9][0]; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 552 | } |
DRC | d5a69af | 2011-02-25 02:13:07 +0000 | [diff] [blame] | 553 | _catch(tjCompress(hnd, bmpbuf, i, 0, j, 4, |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 554 | jpgbuf, &size, TJ_444, 100, TJ_BGR)); |
| 555 | free(bmpbuf); bmpbuf=NULL; free(jpgbuf); jpgbuf=NULL; |
| 556 | |
| 557 | if((bmpbuf=(unsigned char *)malloc(j*i*4))==NULL |
| 558 | || (jpgbuf=(unsigned char *)malloc(TJBUFSIZE(j, i)))==NULL) |
| 559 | { |
DRC | 65d05c1 | 2010-02-18 00:40:17 +0000 | [diff] [blame] | 560 | printf("Memory allocation failure\n"); bailout(); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 561 | } |
DRC | d5a69af | 2011-02-25 02:13:07 +0000 | [diff] [blame] | 562 | for(i2=0; i2<j*i; i2++) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 563 | { |
DRC | d5a69af | 2011-02-25 02:13:07 +0000 | [diff] [blame] | 564 | if(i2%2==0) bmpbuf[i2*4]=bmpbuf[i2*4+1]=bmpbuf[i2*4+2]=0xFF; |
| 565 | else bmpbuf[i2*4]=bmpbuf[i2*4+1]=bmpbuf[i2*4+2]=0; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 566 | } |
DRC | d5a69af | 2011-02-25 02:13:07 +0000 | [diff] [blame] | 567 | _catch(tjCompress(hnd, bmpbuf, j, 0, i, 4, |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 568 | jpgbuf, &size, TJ_444, 100, TJ_BGR)); |
| 569 | free(bmpbuf); bmpbuf=NULL; free(jpgbuf); jpgbuf=NULL; |
| 570 | } |
| 571 | } |
| 572 | printf("Done. \n"); |
| 573 | |
| 574 | finally: |
| 575 | if(bmpbuf) free(bmpbuf); if(jpgbuf) free(jpgbuf); |
| 576 | if(hnd) tjDestroy(hnd); |
| 577 | } |
| 578 | |
| 579 | int main(int argc, char *argv[]) |
| 580 | { |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 581 | int doyuv=0; |
| 582 | if(argc>1 && !stricmp(argv[1], "-yuv")) doyuv=1; |
| 583 | if(doyuv) yuv=YUVENCODE; |
| 584 | dotest(35, 39, 3, TJ_444, "test"); |
| 585 | dotest(39, 41, 4, TJ_444, "test"); |
| 586 | if(doyuv) |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 587 | { |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 588 | dotest(41, 35, 3, TJ_422, "test"); |
| 589 | dotest(35, 39, 4, TJ_422, "test"); |
| 590 | dotest(39, 41, 3, TJ_420, "test"); |
| 591 | dotest(41, 35, 4, TJ_420, "test"); |
DRC | fbb6747 | 2010-11-24 04:02:37 +0000 | [diff] [blame] | 592 | } |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 593 | dotest(35, 39, 1, TJ_GRAYSCALE, "test"); |
| 594 | dotest(39, 41, 3, TJ_GRAYSCALE, "test"); |
| 595 | dotest(41, 35, 4, TJ_GRAYSCALE, "test"); |
| 596 | if(!doyuv) dotest1(); |
| 597 | if(doyuv) |
| 598 | { |
| 599 | yuv=YUVDECODE; |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 600 | dotest(48, 48, 3, TJ_444, "test"); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 601 | dotest(35, 39, 3, TJ_444, "test"); |
DRC | 404e82e | 2011-02-22 19:59:54 +0000 | [diff] [blame] | 602 | dotest(48, 48, 3, TJ_422, "test"); |
| 603 | dotest(39, 41, 3, TJ_422, "test"); |
| 604 | dotest(48, 48, 3, TJ_420, "test"); |
| 605 | dotest(41, 35, 3, TJ_420, "test"); |
DRC | 3484e0b | 2011-02-25 06:21:15 +0000 | [diff] [blame] | 606 | dotest(48, 48, 3, TJ_GRAYSCALE, "test"); |
| 607 | dotest(35, 39, 3, TJ_GRAYSCALE, "test"); |
DRC | f9cf5c7 | 2010-12-10 10:58:49 +0000 | [diff] [blame] | 608 | dotest(48, 48, 1, TJ_GRAYSCALE, "test"); |
DRC | 3484e0b | 2011-02-25 06:21:15 +0000 | [diff] [blame] | 609 | dotest(39, 41, 1, TJ_GRAYSCALE, "test"); |
DRC | 9e17f7d | 2010-12-10 04:59:13 +0000 | [diff] [blame] | 610 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 611 | |
DRC | 65d05c1 | 2010-02-18 00:40:17 +0000 | [diff] [blame] | 612 | return exitstatus; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 613 | } |