DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 1 | /* |
DRC | f3ad13e | 2017-11-13 16:00:35 -0600 | [diff] [blame] | 2 | * Copyright (C)2009-2014, 2017 D. R. Commander. All Rights Reserved. |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * - Redistributions of source code must retain the above copyright notice, |
| 8 | * this list of conditions and the following disclaimer. |
| 9 | * - Redistributions in binary form must reproduce the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer in the documentation |
| 11 | * and/or other materials provided with the distribution. |
| 12 | * - Neither the name of the libjpeg-turbo Project nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived from this |
| 14 | * software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS", |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | * POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | /* |
DRC | c52c556 | 2011-06-15 02:43:42 +0000 | [diff] [blame] | 30 | * This program tests the various code paths in the TurboJPEG C Wrapper |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <errno.h> |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame^] | 37 | #include "tjutil.h" |
| 38 | #include "turbojpeg.h" |
| 39 | #include "md5/md5.h" |
| 40 | #include "cmyk.h" |
DRC | e835ee3 | 2011-07-15 10:06:56 +0000 | [diff] [blame] | 41 | #ifdef _WIN32 |
| 42 | #include <time.h> |
| 43 | #define random() rand() |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame^] | 44 | #else |
| 45 | #include <unistd.h> |
DRC | e835ee3 | 2011-07-15 10:06:56 +0000 | [diff] [blame] | 46 | #endif |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 47 | |
| 48 | |
| 49 | void usage(char *progName) |
| 50 | { |
DRC | 5426a4c | 2017-09-02 04:08:06 +0000 | [diff] [blame] | 51 | printf("\nUSAGE: %s [options]\n\n", progName); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 52 | printf("Options:\n"); |
| 53 | printf("-yuv = test YUV encoding/decoding support\n"); |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 54 | printf("-noyuvpad = do not pad each line of each Y, U, and V plane to the nearest\n"); |
DRC | fef9852 | 2013-04-28 01:32:52 +0000 | [diff] [blame] | 55 | printf(" 4-byte boundary\n"); |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame^] | 56 | printf("-alloc = test automatic buffer allocation\n"); |
| 57 | printf("-bmp = tjLoadImage()/tjSaveImage() unit test\n\n"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 58 | exit(1); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | #define _throwtj() {printf("TurboJPEG ERROR:\n%s\n", tjGetErrorStr()); \ |
| 63 | bailout();} |
| 64 | #define _tj(f) {if((f)==-1) _throwtj();} |
| 65 | #define _throw(m) {printf("ERROR: %s\n", m); bailout();} |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame^] | 66 | #define _throwmd5(filename, md5sum, ref) { \ |
| 67 | printf("\n%s has an MD5 sum of %s.\n Should be %s.\n", filename, \ |
| 68 | md5sum, ref); \ |
| 69 | bailout(); \ |
| 70 | } |
| 71 | |
| 72 | #define RGB2GRAY(r, g, b) \ |
| 73 | (unsigned char)((double)(r)*0.299+(double)(g)*0.587+(double)(b)*0.114+0.5) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 74 | |
| 75 | const char *subNameLong[TJ_NUMSAMP]= |
| 76 | { |
DRC | 1f3635c | 2013-08-18 10:19:00 +0000 | [diff] [blame] | 77 | "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1" |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 78 | }; |
DRC | 1f3635c | 2013-08-18 10:19:00 +0000 | [diff] [blame] | 79 | const char *subName[TJ_NUMSAMP]={"444", "422", "420", "GRAY", "440", "411"}; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 80 | |
| 81 | const char *pixFormatStr[TJ_NUMPF]= |
| 82 | { |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 83 | "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale", |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 84 | "RGBA", "BGRA", "ABGR", "ARGB", "CMYK" |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 87 | const int alphaOffset[TJ_NUMPF] = {-1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1}; |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame] | 88 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 89 | const int _3byteFormats[]={TJPF_RGB, TJPF_BGR}; |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 90 | const int _4byteFormats[]={TJPF_RGBX, TJPF_BGRX, TJPF_XBGR, TJPF_XRGB, |
| 91 | TJPF_CMYK}; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 92 | const int _onlyGray[]={TJPF_GRAY}; |
| 93 | const int _onlyRGB[]={TJPF_RGB}; |
| 94 | |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 95 | int doyuv=0, alloc=0, pad=4; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 96 | |
| 97 | int exitStatus=0; |
| 98 | #define bailout() {exitStatus=-1; goto bailout;} |
| 99 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 100 | |
| 101 | void initBuf(unsigned char *buf, int w, int h, int pf, int flags) |
| 102 | { |
| 103 | int roffset=tjRedOffset[pf]; |
| 104 | int goffset=tjGreenOffset[pf]; |
| 105 | int boffset=tjBlueOffset[pf]; |
| 106 | int ps=tjPixelSize[pf]; |
| 107 | int index, row, col, halfway=16; |
| 108 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 109 | if(pf==TJPF_GRAY) |
| 110 | { |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 111 | memset(buf, 0, w*h*ps); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 112 | for(row=0; row<h; row++) |
| 113 | { |
| 114 | for(col=0; col<w; col++) |
| 115 | { |
| 116 | if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col; |
| 117 | else index=row*w+col; |
| 118 | if(((row/8)+(col/8))%2==0) buf[index]=(row<halfway)? 255:0; |
| 119 | else buf[index]=(row<halfway)? 76:226; |
| 120 | } |
| 121 | } |
| 122 | } |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 123 | else if(pf==TJPF_CMYK) |
| 124 | { |
| 125 | memset(buf, 255, w*h*ps); |
| 126 | for(row=0; row<h; row++) |
| 127 | { |
| 128 | for(col=0; col<w; col++) |
| 129 | { |
| 130 | if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col; |
| 131 | else index=row*w+col; |
| 132 | if(((row/8)+(col/8))%2==0) |
| 133 | { |
| 134 | if(row>=halfway) buf[index*ps+3]=0; |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | buf[index*ps+2]=0; |
| 139 | if(row<halfway) buf[index*ps+1]=0; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 144 | else |
| 145 | { |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 146 | memset(buf, 0, w*h*ps); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 147 | for(row=0; row<h; row++) |
| 148 | { |
| 149 | for(col=0; col<w; col++) |
| 150 | { |
| 151 | if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col; |
| 152 | else index=row*w+col; |
| 153 | if(((row/8)+(col/8))%2==0) |
| 154 | { |
| 155 | if(row<halfway) |
| 156 | { |
| 157 | buf[index*ps+roffset]=255; |
| 158 | buf[index*ps+goffset]=255; |
| 159 | buf[index*ps+boffset]=255; |
| 160 | } |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | buf[index*ps+roffset]=255; |
| 165 | if(row>=halfway) buf[index*ps+goffset]=255; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | |
| 173 | #define checkval(v, cv) { \ |
| 174 | if(v<cv-1 || v>cv+1) { \ |
| 175 | printf("\nComp. %s at %d,%d should be %d, not %d\n", \ |
| 176 | #v, row, col, cv, v); \ |
| 177 | retval=0; exitStatus=-1; goto bailout; \ |
| 178 | }} |
| 179 | |
| 180 | #define checkval0(v) { \ |
| 181 | if(v>1) { \ |
| 182 | printf("\nComp. %s at %d,%d should be 0, not %d\n", #v, row, col, v); \ |
| 183 | retval=0; exitStatus=-1; goto bailout; \ |
| 184 | }} |
| 185 | |
| 186 | #define checkval255(v) { \ |
| 187 | if(v<254) { \ |
| 188 | printf("\nComp. %s at %d,%d should be 255, not %d\n", #v, row, col, v); \ |
| 189 | retval=0; exitStatus=-1; goto bailout; \ |
| 190 | }} |
| 191 | |
| 192 | |
| 193 | int checkBuf(unsigned char *buf, int w, int h, int pf, int subsamp, |
| 194 | tjscalingfactor sf, int flags) |
| 195 | { |
| 196 | int roffset=tjRedOffset[pf]; |
| 197 | int goffset=tjGreenOffset[pf]; |
| 198 | int boffset=tjBlueOffset[pf]; |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame] | 199 | int aoffset=alphaOffset[pf]; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 200 | int ps=tjPixelSize[pf]; |
| 201 | int index, row, col, retval=1; |
| 202 | int halfway=16*sf.num/sf.denom; |
| 203 | int blocksize=8*sf.num/sf.denom; |
| 204 | |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 205 | if(pf==TJPF_CMYK) |
| 206 | { |
| 207 | for(row=0; row<h; row++) |
| 208 | { |
| 209 | for(col=0; col<w; col++) |
| 210 | { |
| 211 | unsigned char c, m, y, k; |
| 212 | if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col; |
| 213 | else index=row*w+col; |
| 214 | c=buf[index*ps]; |
| 215 | m=buf[index*ps+1]; |
| 216 | y=buf[index*ps+2]; |
| 217 | k=buf[index*ps+3]; |
| 218 | if(((row/blocksize)+(col/blocksize))%2==0) |
| 219 | { |
| 220 | checkval255(c); checkval255(m); checkval255(y); |
| 221 | if(row<halfway) checkval255(k) |
| 222 | else checkval0(k) |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | checkval255(c); checkval0(y); checkval255(k); |
| 227 | if(row<halfway) checkval0(m) |
| 228 | else checkval255(m) |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | return 1; |
| 233 | } |
| 234 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 235 | for(row=0; row<h; row++) |
| 236 | { |
| 237 | for(col=0; col<w; col++) |
| 238 | { |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame] | 239 | unsigned char r, g, b, a; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 240 | if(flags&TJFLAG_BOTTOMUP) index=(h-row-1)*w+col; |
| 241 | else index=row*w+col; |
| 242 | r=buf[index*ps+roffset]; |
| 243 | g=buf[index*ps+goffset]; |
| 244 | b=buf[index*ps+boffset]; |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame] | 245 | a=aoffset>=0? buf[index*ps+aoffset]:0xFF; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 246 | if(((row/blocksize)+(col/blocksize))%2==0) |
| 247 | { |
| 248 | if(row<halfway) |
| 249 | { |
| 250 | checkval255(r); checkval255(g); checkval255(b); |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | checkval0(r); checkval0(g); checkval0(b); |
| 255 | } |
| 256 | } |
| 257 | else |
| 258 | { |
| 259 | if(subsamp==TJSAMP_GRAY) |
| 260 | { |
| 261 | if(row<halfway) |
| 262 | { |
| 263 | checkval(r, 76); checkval(g, 76); checkval(b, 76); |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | checkval(r, 226); checkval(g, 226); checkval(b, 226); |
| 268 | } |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | if(row<halfway) |
| 273 | { |
| 274 | checkval255(r); checkval0(g); checkval0(b); |
| 275 | } |
| 276 | else |
| 277 | { |
| 278 | checkval255(r); checkval255(g); checkval0(b); |
| 279 | } |
| 280 | } |
| 281 | } |
DRC | c08e8c1 | 2011-09-08 23:54:40 +0000 | [diff] [blame] | 282 | checkval255(a); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | bailout: |
| 287 | if(retval==0) |
| 288 | { |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 289 | for(row=0; row<h; row++) |
| 290 | { |
| 291 | for(col=0; col<w; col++) |
| 292 | { |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 293 | if(pf==TJPF_CMYK) |
| 294 | printf("%.3d/%.3d/%.3d/%.3d ", buf[(row*w+col)*ps], |
| 295 | buf[(row*w+col)*ps+1], buf[(row*w+col)*ps+2], |
| 296 | buf[(row*w+col)*ps+3]); |
| 297 | else |
| 298 | printf("%.3d/%.3d/%.3d ", buf[(row*w+col)*ps+roffset], |
| 299 | buf[(row*w+col)*ps+goffset], buf[(row*w+col)*ps+boffset]); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 300 | } |
| 301 | printf("\n"); |
| 302 | } |
| 303 | } |
| 304 | return retval; |
| 305 | } |
| 306 | |
| 307 | |
| 308 | #define PAD(v, p) ((v+(p)-1)&(~((p)-1))) |
| 309 | |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 310 | int checkBufYUV(unsigned char *buf, int w, int h, int subsamp, |
| 311 | tjscalingfactor sf) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 312 | { |
| 313 | int row, col; |
| 314 | int hsf=tjMCUWidth[subsamp]/8, vsf=tjMCUHeight[subsamp]/8; |
| 315 | int pw=PAD(w, hsf), ph=PAD(h, vsf); |
| 316 | int cw=pw/hsf, ch=ph/vsf; |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 317 | int ypitch=PAD(pw, pad), uvpitch=PAD(cw, pad); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 318 | int retval=1; |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 319 | int halfway=16*sf.num/sf.denom; |
| 320 | int blocksize=8*sf.num/sf.denom; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 321 | |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 322 | for(row=0; row<ph; row++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 323 | { |
| 324 | for(col=0; col<pw; col++) |
| 325 | { |
| 326 | unsigned char y=buf[ypitch*row+col]; |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 327 | if(((row/blocksize)+(col/blocksize))%2==0) |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 328 | { |
| 329 | if(row<halfway) checkval255(y) else checkval0(y); |
| 330 | } |
| 331 | else |
| 332 | { |
| 333 | if(row<halfway) checkval(y, 76) else checkval(y, 226); |
| 334 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | if(subsamp!=TJSAMP_GRAY) |
| 338 | { |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 339 | int halfway=16/vsf*sf.num/sf.denom; |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 340 | for(row=0; row<ch; row++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 341 | { |
| 342 | for(col=0; col<cw; col++) |
| 343 | { |
| 344 | unsigned char u=buf[ypitch*ph + (uvpitch*row+col)], |
| 345 | v=buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)]; |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 346 | if(((row*vsf/blocksize)+(col*hsf/blocksize))%2==0) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 347 | { |
| 348 | checkval(u, 128); checkval(v, 128); |
| 349 | } |
| 350 | else |
| 351 | { |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 352 | if(row<halfway) |
| 353 | { |
| 354 | checkval(u, 85); checkval255(v); |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | checkval0(u); checkval(v, 149); |
| 359 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | bailout: |
| 366 | if(retval==0) |
| 367 | { |
| 368 | for(row=0; row<ph; row++) |
| 369 | { |
| 370 | for(col=0; col<pw; col++) |
| 371 | printf("%.3d ", buf[ypitch*row+col]); |
| 372 | printf("\n"); |
| 373 | } |
| 374 | printf("\n"); |
| 375 | for(row=0; row<ch; row++) |
| 376 | { |
| 377 | for(col=0; col<cw; col++) |
| 378 | printf("%.3d ", buf[ypitch*ph + (uvpitch*row+col)]); |
| 379 | printf("\n"); |
| 380 | } |
| 381 | printf("\n"); |
| 382 | for(row=0; row<ch; row++) |
| 383 | { |
| 384 | for(col=0; col<cw; col++) |
| 385 | printf("%.3d ", buf[ypitch*ph + uvpitch*ch + (uvpitch*row+col)]); |
| 386 | printf("\n"); |
| 387 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | return retval; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | void writeJPEG(unsigned char *jpegBuf, unsigned long jpegSize, char *filename) |
| 395 | { |
| 396 | FILE *file=fopen(filename, "wb"); |
| 397 | if(!file || fwrite(jpegBuf, jpegSize, 1, file)!=1) |
| 398 | { |
| 399 | printf("ERROR: Could not write to %s.\n%s\n", filename, strerror(errno)); |
| 400 | bailout(); |
| 401 | } |
| 402 | |
| 403 | bailout: |
| 404 | if(file) fclose(file); |
| 405 | } |
| 406 | |
| 407 | |
| 408 | void compTest(tjhandle handle, unsigned char **dstBuf, |
| 409 | unsigned long *dstSize, int w, int h, int pf, char *basename, |
| 410 | int subsamp, int jpegQual, int flags) |
| 411 | { |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 412 | char tempStr[1024]; unsigned char *srcBuf=NULL, *yuvBuf=NULL; |
| 413 | const char *pfStr=pixFormatStr[pf]; |
DRC | fe73965 | 2013-10-31 04:53:27 +0000 | [diff] [blame] | 414 | const char *buStrLong=(flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down "; |
| 415 | const char *buStr=(flags&TJFLAG_BOTTOMUP)? "BU":"TD"; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 416 | |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 417 | if((srcBuf=(unsigned char *)malloc(w*h*tjPixelSize[pf]))==NULL) |
| 418 | _throw("Memory allocation failure"); |
| 419 | initBuf(srcBuf, w, h, pf, flags); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 420 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 421 | if(*dstBuf && *dstSize>0) memset(*dstBuf, 0, *dstSize); |
| 422 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 423 | |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 424 | if(!alloc) flags|=TJFLAG_NOREALLOC; |
| 425 | if(doyuv) |
| 426 | { |
| 427 | unsigned long yuvSize=tjBufSizeYUV2(w, pad, h, subsamp); |
| 428 | tjscalingfactor sf={1, 1}; |
DRC | c901449 | 2014-03-10 09:34:04 +0000 | [diff] [blame] | 429 | tjhandle handle2=tjInitCompress(); |
| 430 | if(!handle2) _throwtj(); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 431 | |
| 432 | if((yuvBuf=(unsigned char *)malloc(yuvSize))==NULL) |
| 433 | _throw("Memory allocation failure"); |
DRC | 20e158d | 2014-03-08 20:50:35 +0000 | [diff] [blame] | 434 | memset(yuvBuf, 0, yuvSize); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 435 | |
| 436 | printf("%s %s -> YUV %s ... ", pfStr, buStrLong, subNameLong[subsamp]); |
DRC | c901449 | 2014-03-10 09:34:04 +0000 | [diff] [blame] | 437 | _tj(tjEncodeYUV3(handle2, srcBuf, w, 0, h, pf, yuvBuf, pad, subsamp, |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 438 | flags)); |
DRC | c901449 | 2014-03-10 09:34:04 +0000 | [diff] [blame] | 439 | tjDestroy(handle2); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 440 | if(checkBufYUV(yuvBuf, w, h, subsamp, sf)) printf("Passed.\n"); |
| 441 | else printf("FAILED!\n"); |
| 442 | |
| 443 | printf("YUV %s %s -> JPEG Q%d ... ", subNameLong[subsamp], buStrLong, |
| 444 | jpegQual); |
| 445 | _tj(tjCompressFromYUV(handle, yuvBuf, w, pad, h, subsamp, dstBuf, |
| 446 | dstSize, jpegQual, flags)); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 447 | } |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 448 | else |
| 449 | { |
| 450 | printf("%s %s -> %s Q%d ... ", pfStr, buStrLong, subNameLong[subsamp], |
| 451 | jpegQual); |
| 452 | _tj(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp, |
| 453 | jpegQual, flags)); |
| 454 | } |
| 455 | |
| 456 | snprintf(tempStr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basename, pfStr, buStr, |
| 457 | subName[subsamp], jpegQual); |
| 458 | writeJPEG(*dstBuf, *dstSize, tempStr); |
| 459 | printf("Done.\n Result in %s\n", tempStr); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 460 | |
| 461 | bailout: |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 462 | if(yuvBuf) free(yuvBuf); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 463 | if(srcBuf) free(srcBuf); |
| 464 | } |
| 465 | |
| 466 | |
| 467 | void _decompTest(tjhandle handle, unsigned char *jpegBuf, |
| 468 | unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp, |
| 469 | int flags, tjscalingfactor sf) |
| 470 | { |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 471 | unsigned char *dstBuf=NULL, *yuvBuf=NULL; |
DRC | 2bdadb4 | 2014-02-28 09:06:42 +0000 | [diff] [blame] | 472 | int _hdrw=0, _hdrh=0, _hdrsubsamp=-1; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 473 | int scaledWidth=TJSCALED(w, sf); |
| 474 | int scaledHeight=TJSCALED(h, sf); |
| 475 | unsigned long dstSize=0; |
| 476 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 477 | _tj(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh, |
| 478 | &_hdrsubsamp)); |
| 479 | if(_hdrw!=w || _hdrh!=h || _hdrsubsamp!=subsamp) |
| 480 | _throw("Incorrect JPEG header"); |
| 481 | |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 482 | dstSize=scaledWidth*scaledHeight*tjPixelSize[pf]; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 483 | if((dstBuf=(unsigned char *)malloc(dstSize))==NULL) |
| 484 | _throw("Memory allocation failure"); |
| 485 | memset(dstBuf, 0, dstSize); |
| 486 | |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 487 | if(doyuv) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 488 | { |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 489 | unsigned long yuvSize=tjBufSizeYUV2(scaledWidth, pad, scaledHeight, |
| 490 | subsamp); |
DRC | c901449 | 2014-03-10 09:34:04 +0000 | [diff] [blame] | 491 | tjhandle handle2=tjInitDecompress(); |
| 492 | if(!handle2) _throwtj(); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 493 | |
| 494 | if((yuvBuf=(unsigned char *)malloc(yuvSize))==NULL) |
| 495 | _throw("Memory allocation failure"); |
DRC | 20e158d | 2014-03-08 20:50:35 +0000 | [diff] [blame] | 496 | memset(yuvBuf, 0, yuvSize); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 497 | |
| 498 | printf("JPEG -> YUV %s ", subNameLong[subsamp]); |
| 499 | if(sf.num!=1 || sf.denom!=1) |
| 500 | printf("%d/%d ... ", sf.num, sf.denom); |
| 501 | else printf("... "); |
| 502 | _tj(tjDecompressToYUV2(handle, jpegBuf, jpegSize, yuvBuf, scaledWidth, |
DRC | f610d61 | 2013-04-26 10:33:29 +0000 | [diff] [blame] | 503 | pad, scaledHeight, flags)); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 504 | if(checkBufYUV(yuvBuf, scaledWidth, scaledHeight, subsamp, sf)) |
| 505 | printf("Passed.\n"); |
| 506 | else printf("FAILED!\n"); |
| 507 | |
| 508 | printf("YUV %s -> %s %s ... ", subNameLong[subsamp], pixFormatStr[pf], |
| 509 | (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down "); |
DRC | c901449 | 2014-03-10 09:34:04 +0000 | [diff] [blame] | 510 | _tj(tjDecodeYUV(handle2, yuvBuf, pad, subsamp, dstBuf, scaledWidth, 0, |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 511 | scaledHeight, pf, flags)); |
DRC | c901449 | 2014-03-10 09:34:04 +0000 | [diff] [blame] | 512 | tjDestroy(handle2); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 513 | } |
| 514 | else |
| 515 | { |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 516 | printf("JPEG -> %s %s ", pixFormatStr[pf], |
| 517 | (flags&TJFLAG_BOTTOMUP)? "Bottom-Up":"Top-Down "); |
| 518 | if(sf.num!=1 || sf.denom!=1) |
| 519 | printf("%d/%d ... ", sf.num, sf.denom); |
| 520 | else printf("... "); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 521 | _tj(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0, |
| 522 | scaledHeight, pf, flags)); |
| 523 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 524 | |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 525 | if(checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags)) |
| 526 | printf("Passed."); |
| 527 | else printf("FAILED!"); |
DRC | 2bdadb4 | 2014-02-28 09:06:42 +0000 | [diff] [blame] | 528 | printf("\n"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 529 | |
| 530 | bailout: |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 531 | if(yuvBuf) free(yuvBuf); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 532 | if(dstBuf) free(dstBuf); |
| 533 | } |
| 534 | |
| 535 | |
| 536 | void decompTest(tjhandle handle, unsigned char *jpegBuf, |
| 537 | unsigned long jpegSize, int w, int h, int pf, char *basename, int subsamp, |
| 538 | int flags) |
| 539 | { |
| 540 | int i, n=0; |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 541 | tjscalingfactor *sf=tjGetScalingFactors(&n); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 542 | if(!sf || !n) _throwtj(); |
| 543 | |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 544 | for(i=0; i<n; i++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 545 | { |
DRC | 418fe28 | 2013-05-07 21:17:35 +0000 | [diff] [blame] | 546 | if(subsamp==TJSAMP_444 || subsamp==TJSAMP_GRAY || |
DRC | 1f3635c | 2013-08-18 10:19:00 +0000 | [diff] [blame] | 547 | (subsamp==TJSAMP_411 && sf[i].num==1 && |
| 548 | (sf[i].denom==2 || sf[i].denom==1)) || |
| 549 | (subsamp!=TJSAMP_411 && sf[i].num==1 && |
| 550 | (sf[i].denom==4 || sf[i].denom==2 || sf[i].denom==1))) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 551 | _decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp, |
| 552 | flags, sf[i]); |
| 553 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 554 | |
| 555 | bailout: |
DRC | b7c4193 | 2013-05-04 23:41:33 +0000 | [diff] [blame] | 556 | return; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | |
| 560 | void doTest(int w, int h, const int *formats, int nformats, int subsamp, |
| 561 | char *basename) |
| 562 | { |
| 563 | tjhandle chandle=NULL, dhandle=NULL; |
| 564 | unsigned char *dstBuf=NULL; |
| 565 | unsigned long size=0; int pfi, pf, i; |
| 566 | |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 567 | if(!alloc) |
DRC | 910a357 | 2013-10-30 23:02:57 +0000 | [diff] [blame] | 568 | size=tjBufSize(w, h, subsamp); |
| 569 | if(size!=0) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 570 | if((dstBuf=(unsigned char *)tjAlloc(size))==NULL) |
| 571 | _throw("Memory allocation failure."); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 572 | |
| 573 | if((chandle=tjInitCompress())==NULL || (dhandle=tjInitDecompress())==NULL) |
| 574 | _throwtj(); |
| 575 | |
| 576 | for(pfi=0; pfi<nformats; pfi++) |
| 577 | { |
| 578 | for(i=0; i<2; i++) |
| 579 | { |
| 580 | int flags=0; |
DRC | 1f3635c | 2013-08-18 10:19:00 +0000 | [diff] [blame] | 581 | if(subsamp==TJSAMP_422 || subsamp==TJSAMP_420 || subsamp==TJSAMP_440 || |
| 582 | subsamp==TJSAMP_411) |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 583 | flags|=TJFLAG_FASTUPSAMPLE; |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 584 | if(i==1) flags|=TJFLAG_BOTTOMUP; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 585 | pf=formats[pfi]; |
| 586 | compTest(chandle, &dstBuf, &size, w, h, pf, basename, subsamp, 100, |
| 587 | flags); |
| 588 | decompTest(dhandle, dstBuf, size, w, h, pf, basename, subsamp, |
| 589 | flags); |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 590 | if(pf>=TJPF_RGBX && pf<=TJPF_XRGB) |
DRC | 4798b7e | 2014-03-08 20:31:31 +0000 | [diff] [blame] | 591 | { |
| 592 | printf("\n"); |
DRC | 67ce3b2 | 2011-12-19 02:21:03 +0000 | [diff] [blame] | 593 | decompTest(dhandle, dstBuf, size, w, h, pf+(TJPF_RGBA-TJPF_RGBX), |
| 594 | basename, subsamp, flags); |
DRC | 4798b7e | 2014-03-08 20:31:31 +0000 | [diff] [blame] | 595 | } |
DRC | b7c4193 | 2013-05-04 23:41:33 +0000 | [diff] [blame] | 596 | printf("\n"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
DRC | b7c4193 | 2013-05-04 23:41:33 +0000 | [diff] [blame] | 599 | printf("--------------------\n\n"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 600 | |
| 601 | bailout: |
| 602 | if(chandle) tjDestroy(chandle); |
| 603 | if(dhandle) tjDestroy(dhandle); |
| 604 | |
| 605 | if(dstBuf) tjFree(dstBuf); |
| 606 | } |
| 607 | |
| 608 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 609 | void bufSizeTest(void) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 610 | { |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 611 | int w, h, i, subsamp; |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame] | 612 | unsigned char *srcBuf=NULL, *dstBuf=NULL; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 613 | tjhandle handle=NULL; |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame] | 614 | unsigned long dstSize=0; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 615 | |
| 616 | if((handle=tjInitCompress())==NULL) _throwtj(); |
| 617 | |
| 618 | printf("Buffer size regression test\n"); |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 619 | for(subsamp=0; subsamp<TJ_NUMSAMP; subsamp++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 620 | { |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 621 | for(w=1; w<48; w++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 622 | { |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 623 | int maxh=(w==1)? 2048:48; |
| 624 | for(h=1; h<maxh; h++) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 625 | { |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 626 | if(h%100==0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h); |
| 627 | if((srcBuf=(unsigned char *)malloc(w*h*4))==NULL) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 628 | _throw("Memory allocation failure"); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 629 | if(!alloc || doyuv) |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 630 | { |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 631 | if(doyuv) dstSize=tjBufSizeYUV2(w, pad, h, subsamp); |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame] | 632 | else dstSize=tjBufSize(w, h, subsamp); |
| 633 | if((dstBuf=(unsigned char *)tjAlloc(dstSize))==NULL) |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 634 | _throw("Memory allocation failure"); |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 635 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 636 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 637 | for(i=0; i<w*h*4; i++) |
| 638 | { |
| 639 | if(random()<RAND_MAX/2) srcBuf[i]=0; |
| 640 | else srcBuf[i]=255; |
| 641 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 642 | |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 643 | if(doyuv) |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame] | 644 | { |
| 645 | _tj(tjEncodeYUV3(handle, srcBuf, w, 0, h, TJPF_BGRX, dstBuf, pad, |
| 646 | subsamp, 0)); |
| 647 | } |
| 648 | else |
| 649 | { |
| 650 | _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &dstBuf, |
| 651 | &dstSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC)); |
| 652 | } |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 653 | free(srcBuf); srcBuf=NULL; |
mayeut | f57bae0 | 2016-02-25 23:14:45 +0100 | [diff] [blame] | 654 | if(!alloc || doyuv) |
DRC | fe80ec2 | 2014-08-21 15:51:47 +0000 | [diff] [blame] | 655 | { |
| 656 | tjFree(dstBuf); dstBuf=NULL; |
| 657 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 658 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 659 | if((srcBuf=(unsigned char *)malloc(h*w*4))==NULL) |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 660 | _throw("Memory allocation failure"); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 661 | if(!alloc || doyuv) |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 662 | { |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 663 | if(doyuv) dstSize=tjBufSizeYUV2(h, pad, w, subsamp); |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame] | 664 | else dstSize=tjBufSize(h, w, subsamp); |
| 665 | if((dstBuf=(unsigned char *)tjAlloc(dstSize))==NULL) |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 666 | _throw("Memory allocation failure"); |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 667 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 668 | |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 669 | for(i=0; i<h*w*4; i++) |
| 670 | { |
| 671 | if(random()<RAND_MAX/2) srcBuf[i]=0; |
| 672 | else srcBuf[i]=255; |
| 673 | } |
| 674 | |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 675 | if(doyuv) |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame] | 676 | { |
| 677 | _tj(tjEncodeYUV3(handle, srcBuf, h, 0, w, TJPF_BGRX, dstBuf, pad, |
| 678 | subsamp, 0)); |
| 679 | } |
| 680 | else |
| 681 | { |
| 682 | _tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &dstBuf, |
| 683 | &dstSize, subsamp, 100, alloc? 0:TJFLAG_NOREALLOC)); |
| 684 | } |
DRC | 724c56b | 2011-07-12 06:22:06 +0000 | [diff] [blame] | 685 | free(srcBuf); srcBuf=NULL; |
mayeut | f57bae0 | 2016-02-25 23:14:45 +0100 | [diff] [blame] | 686 | if(!alloc || doyuv) |
DRC | fe80ec2 | 2014-08-21 15:51:47 +0000 | [diff] [blame] | 687 | { |
| 688 | tjFree(dstBuf); dstBuf=NULL; |
| 689 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 690 | } |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | printf("Done. \n"); |
| 694 | |
| 695 | bailout: |
| 696 | if(srcBuf) free(srcBuf); |
DRC | eb66974 | 2014-08-22 19:59:51 +0000 | [diff] [blame] | 697 | if(dstBuf) tjFree(dstBuf); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 698 | if(handle) tjDestroy(handle); |
| 699 | } |
| 700 | |
| 701 | |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame^] | 702 | void initBitmap(unsigned char *buf, int width, int pitch, int height, int pf, |
| 703 | int flags) |
| 704 | { |
| 705 | int roffset=tjRedOffset[pf]; |
| 706 | int goffset=tjGreenOffset[pf]; |
| 707 | int boffset=tjBlueOffset[pf]; |
| 708 | int ps=tjPixelSize[pf]; |
| 709 | int i, j; |
| 710 | |
| 711 | for(j=0; j<height; j++) |
| 712 | { |
| 713 | int row=(flags&TJFLAG_BOTTOMUP)? height-j-1:j; |
| 714 | for(i=0; i<width; i++) |
| 715 | { |
| 716 | unsigned char r=(i*256/width)%256; |
| 717 | unsigned char g=(j*256/height)%256; |
| 718 | unsigned char b=(j*256/height+i*256/width)%256; |
| 719 | memset(&buf[row*pitch+i*ps], 0, ps); |
| 720 | if(pf==TJPF_GRAY) buf[row*pitch+i*ps]=RGB2GRAY(r, g, b); |
| 721 | else if(pf==TJPF_CMYK) |
| 722 | rgb_to_cmyk(r, g, b, &buf[row*pitch+i*ps+0], &buf[row*pitch+i*ps+1], |
| 723 | &buf[row*pitch+i*ps+2], &buf[row*pitch+i*ps+3]); |
| 724 | else |
| 725 | { |
| 726 | buf[row*pitch+i*ps+roffset]=r; |
| 727 | buf[row*pitch+i*ps+goffset]=g; |
| 728 | buf[row*pitch+i*ps+boffset]=b; |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | |
| 735 | int cmpBitmap(unsigned char *buf, int width, int pitch, int height, int pf, |
| 736 | int flags, int gray2rgb) |
| 737 | { |
| 738 | int roffset=tjRedOffset[pf]; |
| 739 | int goffset=tjGreenOffset[pf]; |
| 740 | int boffset=tjBlueOffset[pf]; |
| 741 | int aoffset=alphaOffset[pf]; |
| 742 | int ps=tjPixelSize[pf]; |
| 743 | int i, j; |
| 744 | |
| 745 | for(j=0; j<height; j++) |
| 746 | { |
| 747 | int row=(flags&TJFLAG_BOTTOMUP)? height-j-1:j; |
| 748 | for(i=0; i<width; i++) |
| 749 | { |
| 750 | unsigned char r=(i*256/width)%256; |
| 751 | unsigned char g=(j*256/height)%256; |
| 752 | unsigned char b=(j*256/height+i*256/width)%256; |
| 753 | if(pf==TJPF_GRAY) |
| 754 | { |
| 755 | if(buf[row*pitch+i*ps]!=RGB2GRAY(r, g, b)) |
| 756 | return 0; |
| 757 | } |
| 758 | else if(pf==TJPF_CMYK) |
| 759 | { |
| 760 | unsigned char rf, gf, bf; |
| 761 | cmyk_to_rgb(buf[row*pitch+i*ps+0], buf[row*pitch+i*ps+1], |
| 762 | buf[row*pitch+i*ps+2], buf[row*pitch+i*ps+3], &rf, &gf, |
| 763 | &bf); |
| 764 | if(gray2rgb) |
| 765 | { |
| 766 | unsigned char gray=RGB2GRAY(r, g, b); |
| 767 | if(rf!=gray || gf!=gray || bf!=gray) |
| 768 | return 0; |
| 769 | } |
| 770 | else if(rf!=r || gf!=g || bf!=b) return 0; |
| 771 | } |
| 772 | else |
| 773 | { |
| 774 | if(gray2rgb) |
| 775 | { |
| 776 | unsigned char gray=RGB2GRAY(r, g, b); |
| 777 | if(buf[row*pitch+i*ps+roffset]!=gray || |
| 778 | buf[row*pitch+i*ps+goffset]!=gray || |
| 779 | buf[row*pitch+i*ps+boffset]!=gray) |
| 780 | return 0; |
| 781 | } |
| 782 | else if(buf[row*pitch+i*ps+roffset]!=r || |
| 783 | buf[row*pitch+i*ps+goffset]!=g || |
| 784 | buf[row*pitch+i*ps+boffset]!=b) |
| 785 | return 0; |
| 786 | if(aoffset>=0 && buf[row*pitch+i*ps+aoffset]!=0xFF) |
| 787 | return 0; |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | return 1; |
| 792 | } |
| 793 | |
| 794 | |
| 795 | int doBmpTest(const char *ext, int width, int align, int height, int pf, |
| 796 | int flags) |
| 797 | { |
| 798 | char filename[80], *md5sum, md5buf[65]; |
| 799 | int ps=tjPixelSize[pf], pitch=PAD(width*ps, align), |
| 800 | loadWidth=0, loadHeight=0, retval=0; |
| 801 | unsigned char *buf=NULL; |
| 802 | char *md5ref; |
| 803 | |
| 804 | if(pf==TJPF_GRAY) |
| 805 | { |
| 806 | md5ref=!strcasecmp(ext, "ppm")? "bc77dea8eaf006aa187582b301f67e02": |
| 807 | "2670a3f8cf19d855183c02ccf18d2a35"; |
| 808 | } |
| 809 | else |
| 810 | { |
| 811 | md5ref=!strcasecmp(ext, "ppm")? "c0c9f772b464d1896326883a5c79c545": |
| 812 | "6d659071b9bfcdee2def22cb58ddadca"; |
| 813 | } |
| 814 | |
| 815 | if((buf=(unsigned char *)tjAlloc(pitch*height))==NULL) |
| 816 | _throw("Could not allocate memory"); |
| 817 | initBitmap(buf, width, pitch, height, pf, flags); |
| 818 | |
| 819 | snprintf(filename, 80, "test_bmp_%s_%d_%s.%s", pixFormatStr[pf], align, |
| 820 | (flags&TJFLAG_BOTTOMUP)? "bu":"td", ext); |
| 821 | _tj(tjSaveImage(filename, buf, width, pitch, height, pf, flags)); |
| 822 | md5sum=MD5File(filename, md5buf); |
| 823 | if(strcasecmp(md5sum, md5ref)) |
| 824 | _throwmd5(filename, md5sum, md5ref); |
| 825 | |
| 826 | tjFree(buf); buf=NULL; |
| 827 | if((buf=tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf, |
| 828 | flags))==NULL) |
| 829 | _throwtj(); |
| 830 | if(width!=loadWidth || height!=loadHeight) |
| 831 | { |
| 832 | printf("\n Image dimensions of %s are bogus\n", filename); |
| 833 | retval=-1; goto bailout; |
| 834 | } |
| 835 | if(!cmpBitmap(buf, width, pitch, height, pf, flags, 0)) |
| 836 | { |
| 837 | printf("\n Pixel data in %s is bogus\n", filename); |
| 838 | retval=-1; goto bailout; |
| 839 | } |
| 840 | if(pf==TJPF_GRAY) |
| 841 | { |
| 842 | tjFree(buf); buf=NULL; |
| 843 | pf=TJPF_XBGR; |
| 844 | if((buf=tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf, |
| 845 | flags))==NULL) |
| 846 | _throwtj(); |
| 847 | pitch=PAD(width*tjPixelSize[pf], align); |
| 848 | if(!cmpBitmap(buf, width, pitch, height, pf, flags, 1)) |
| 849 | { |
| 850 | printf("\n Converting %s to RGB failed\n", filename); |
| 851 | retval=-1; goto bailout; |
| 852 | } |
| 853 | |
| 854 | tjFree(buf); buf=NULL; |
| 855 | pf=TJPF_CMYK; |
| 856 | if((buf=tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf, |
| 857 | flags))==NULL) |
| 858 | _throwtj(); |
| 859 | pitch=PAD(width*tjPixelSize[pf], align); |
| 860 | if(!cmpBitmap(buf, width, pitch, height, pf, flags, 1)) |
| 861 | { |
| 862 | printf("\n Converting %s to CMYK failed\n", filename); |
| 863 | retval=-1; goto bailout; |
| 864 | } |
| 865 | } |
| 866 | else if(pf!=TJPF_CMYK) |
| 867 | { |
| 868 | tjFree(buf); buf=NULL; |
| 869 | pf=TJPF_GRAY; |
| 870 | if((buf=tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf, |
| 871 | flags))==NULL) |
| 872 | _throwtj(); |
| 873 | pitch=PAD(width, align); |
| 874 | if(!cmpBitmap(buf, width, pitch, height, pf, flags, 0)) |
| 875 | { |
| 876 | printf("\n Converting %s to grayscale failed\n", filename); |
| 877 | retval=-1; goto bailout; |
| 878 | } |
| 879 | } |
| 880 | unlink(filename); |
| 881 | |
| 882 | bailout: |
| 883 | if(buf) tjFree(buf); |
| 884 | if(exitStatus<0) return exitStatus; |
| 885 | return retval; |
| 886 | } |
| 887 | |
| 888 | |
| 889 | int bmpTest(void) |
| 890 | { |
| 891 | int align, width=35, height=39, format; |
| 892 | |
| 893 | for(align=1; align<=8; align*=2) |
| 894 | { |
| 895 | for(format=0; format<TJ_NUMPF; format++) |
| 896 | { |
| 897 | printf("%s Top-Down BMP (row alignment = %d bytes) ... ", |
| 898 | pixFormatStr[format], align); |
| 899 | if(doBmpTest("bmp", width, align, height, format, 0)==-1) |
| 900 | return -1; |
| 901 | printf("OK.\n"); |
| 902 | |
| 903 | printf("%s Top-Down PPM (row alignment = %d bytes) ... ", |
| 904 | pixFormatStr[format], align); |
| 905 | if(doBmpTest("ppm", width, align, height, format, TJFLAG_BOTTOMUP)==-1) |
| 906 | return -1; |
| 907 | printf("OK.\n"); |
| 908 | |
| 909 | printf("%s Bottom-Up BMP (row alignment = %d bytes) ... ", |
| 910 | pixFormatStr[format], align); |
| 911 | if(doBmpTest("bmp", width, align, height, format, 0)==-1) |
| 912 | return -1; |
| 913 | printf("OK.\n"); |
| 914 | |
| 915 | printf("%s Bottom-Up PPM (row alignment = %d bytes) ... ", |
| 916 | pixFormatStr[format], align); |
| 917 | if(doBmpTest("ppm", width, align, height, format, TJFLAG_BOTTOMUP)==-1) |
| 918 | return -1; |
| 919 | printf("OK.\n"); |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 927 | int main(int argc, char *argv[]) |
| 928 | { |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 929 | int i, num4bf=5; |
DRC | e835ee3 | 2011-07-15 10:06:56 +0000 | [diff] [blame] | 930 | #ifdef _WIN32 |
| 931 | srand((unsigned int)time(NULL)); |
| 932 | #endif |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 933 | if(argc>1) |
| 934 | { |
| 935 | for(i=1; i<argc; i++) |
| 936 | { |
| 937 | if(!strcasecmp(argv[i], "-yuv")) doyuv=1; |
DRC | f3ad13e | 2017-11-13 16:00:35 -0600 | [diff] [blame] | 938 | else if(!strcasecmp(argv[i], "-noyuvpad")) pad=1; |
| 939 | else if(!strcasecmp(argv[i], "-alloc")) alloc=1; |
DRC | aa74590 | 2017-11-16 18:09:07 -0600 | [diff] [blame^] | 940 | else if(!strcasecmp(argv[i], "-bmp")) return bmpTest(); |
DRC | f3ad13e | 2017-11-13 16:00:35 -0600 | [diff] [blame] | 941 | else usage(argv[0]); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | if(alloc) printf("Testing automatic buffer allocation\n"); |
DRC | 34dca05 | 2014-02-28 09:17:14 +0000 | [diff] [blame] | 945 | if(doyuv) num4bf=4; |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 946 | doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test"); |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 947 | doTest(39, 41, _4byteFormats, num4bf, TJSAMP_444, "test"); |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 948 | doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test"); |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 949 | doTest(35, 39, _4byteFormats, num4bf, TJSAMP_422, "test"); |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 950 | doTest(39, 41, _3byteFormats, 2, TJSAMP_420, "test"); |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 951 | doTest(41, 35, _4byteFormats, num4bf, TJSAMP_420, "test"); |
DRC | cac1051 | 2012-03-16 14:37:36 +0000 | [diff] [blame] | 952 | doTest(35, 39, _3byteFormats, 2, TJSAMP_440, "test"); |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 953 | doTest(39, 41, _4byteFormats, num4bf, TJSAMP_440, "test"); |
DRC | 1f3635c | 2013-08-18 10:19:00 +0000 | [diff] [blame] | 954 | doTest(41, 35, _3byteFormats, 2, TJSAMP_411, "test"); |
DRC | cd7c3e6 | 2013-08-23 02:49:25 +0000 | [diff] [blame] | 955 | doTest(35, 39, _4byteFormats, num4bf, TJSAMP_411, "test"); |
DRC | 1f3635c | 2013-08-18 10:19:00 +0000 | [diff] [blame] | 956 | doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test"); |
| 957 | doTest(41, 35, _3byteFormats, 2, TJSAMP_GRAY, "test"); |
| 958 | doTest(35, 39, _4byteFormats, 4, TJSAMP_GRAY, "test"); |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame] | 959 | bufSizeTest(); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 960 | if(doyuv) |
| 961 | { |
DRC | 38c9970 | 2014-02-11 09:45:18 +0000 | [diff] [blame] | 962 | printf("\n--------------------\n\n"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 963 | doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 964 | doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 965 | doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 966 | doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0"); |
DRC | 1f3635c | 2013-08-18 10:19:00 +0000 | [diff] [blame] | 967 | doTest(48, 48, _onlyRGB, 1, TJSAMP_411, "test_yuv0"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 968 | doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 969 | doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0"); |
DRC | b8b359a | 2011-05-25 03:54:56 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | return exitStatus; |
| 973 | } |