hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 1 | /* |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 2 | * Copyright (C)2009-2014, 2017-2018 D. R. Commander. All Rights Reserved. |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +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 | /* |
| 30 | * This program tests the various code paths in the TurboJPEG C Wrapper |
| 31 | */ |
| 32 | |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <errno.h> |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 37 | #include "tjutil.h" |
| 38 | #include "turbojpeg.h" |
| 39 | #include "md5/md5.h" |
| 40 | #include "cmyk.h" |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 41 | #ifdef _WIN32 |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 42 | #include <time.h> |
| 43 | #define random() rand() |
| 44 | #else |
| 45 | #include <unistd.h> |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
| 48 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 49 | #ifndef GTEST |
| 50 | static void usage(char *progName) |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 51 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 52 | printf("\nUSAGE: %s [options]\n\n", progName); |
| 53 | printf("Options:\n"); |
| 54 | printf("-yuv = test YUV encoding/decoding support\n"); |
| 55 | printf("-noyuvpad = do not pad each line of each Y, U, and V plane to the nearest\n"); |
| 56 | printf(" 4-byte boundary\n"); |
| 57 | printf("-alloc = test automatic buffer allocation\n"); |
| 58 | printf("-bmp = tjLoadImage()/tjSaveImage() unit test\n\n"); |
| 59 | exit(1); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 60 | } |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 61 | #endif |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 62 | |
| 63 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 64 | #define _throwtj() { \ |
| 65 | printf("TurboJPEG ERROR:\n%s\n", tjGetErrorStr()); \ |
| 66 | bailout() \ |
| 67 | } |
| 68 | #define _tj(f) { if ((f) == -1) _throwtj(); } |
| 69 | #define _throw(m) { printf("ERROR: %s\n", m); bailout() } |
| 70 | #define _throwmd5(filename, md5sum, ref) { \ |
| 71 | printf("\n%s has an MD5 sum of %s.\n Should be %s.\n", filename, md5sum, \ |
| 72 | ref); \ |
| 73 | bailout() \ |
| 74 | } |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 75 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 76 | static const char *subNameLong[TJ_NUMSAMP] = { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 77 | "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1" |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 78 | }; |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 79 | static const char *subName[TJ_NUMSAMP] = { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 80 | "444", "422", "420", "GRAY", "440", "411" |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 83 | const char *pixFormatStr[TJ_NUMPF] = { |
| 84 | "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale", |
| 85 | "RGBA", "BGRA", "ABGR", "ARGB", "CMYK" |
| 86 | }; |
hbono@chromium.org | c6beb74 | 2011-11-29 05:16:26 +0000 | [diff] [blame] | 87 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 88 | static const int _3byteFormats[] = { TJPF_RGB, TJPF_BGR }; |
| 89 | static const int _4byteFormats[] = { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 90 | TJPF_RGBX, TJPF_BGRX, TJPF_XBGR, TJPF_XRGB, TJPF_CMYK |
| 91 | }; |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 92 | static const int _onlyGray[] = { TJPF_GRAY }; |
| 93 | static const int _onlyRGB[] = { TJPF_RGB }; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 94 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 95 | static int doYUV = 0, alloc = 0, pad = 4; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 96 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 97 | static int exitStatus = 0; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 98 | #define bailout() { exitStatus = -1; goto bailout; } |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 99 | |
| 100 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 101 | static void initBuf(unsigned char *buf, int w, int h, int pf, int flags) |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 102 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 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; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 108 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 109 | if (pf == TJPF_GRAY) { |
| 110 | memset(buf, 0, w * h * ps); |
| 111 | for (row = 0; row < h; row++) { |
| 112 | for (col = 0; col < w; col++) { |
| 113 | if (flags & TJFLAG_BOTTOMUP) index = (h - row - 1) * w + col; |
| 114 | else index = row * w + col; |
| 115 | if (((row / 8) + (col / 8)) % 2 == 0) |
| 116 | buf[index] = (row < halfway) ? 255 : 0; |
| 117 | else buf[index] = (row < halfway) ? 76 : 226; |
| 118 | } |
| 119 | } |
| 120 | } else if (pf == TJPF_CMYK) { |
| 121 | memset(buf, 255, w * h * ps); |
| 122 | for (row = 0; row < h; row++) { |
| 123 | for (col = 0; col < w; col++) { |
| 124 | if (flags & TJFLAG_BOTTOMUP) index = (h - row - 1) * w + col; |
| 125 | else index = row * w + col; |
| 126 | if (((row / 8) + (col / 8)) % 2 == 0) { |
| 127 | if (row >= halfway) buf[index * ps + 3] = 0; |
| 128 | } else { |
| 129 | buf[index * ps + 2] = 0; |
| 130 | if (row < halfway) buf[index * ps + 1] = 0; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } else { |
| 135 | memset(buf, 0, w * h * ps); |
| 136 | for (row = 0; row < h; row++) { |
| 137 | for (col = 0; col < w; col++) { |
| 138 | if (flags & TJFLAG_BOTTOMUP) index = (h - row - 1) * w + col; |
| 139 | else index = row * w + col; |
| 140 | if (((row / 8) + (col / 8)) % 2 == 0) { |
| 141 | if (row < halfway) { |
| 142 | buf[index * ps + roffset] = 255; |
| 143 | buf[index * ps + goffset] = 255; |
| 144 | buf[index * ps + boffset] = 255; |
| 145 | } |
| 146 | } else { |
| 147 | buf[index * ps + roffset] = 255; |
| 148 | if (row >= halfway) buf[index * ps + goffset] = 255; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
| 156 | #define checkval(v, cv) { \ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 157 | if (v < cv - 1 || v > cv + 1) { \ |
| 158 | printf("\nComp. %s at %d,%d should be %d, not %d\n", #v, row, col, cv, \ |
| 159 | v); \ |
| 160 | retval = 0; exitStatus = -1; goto bailout; \ |
| 161 | } \ |
| 162 | } |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 163 | |
| 164 | #define checkval0(v) { \ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 165 | if (v > 1) { \ |
| 166 | printf("\nComp. %s at %d,%d should be 0, not %d\n", #v, row, col, v); \ |
| 167 | retval = 0; exitStatus = -1; goto bailout; \ |
| 168 | } \ |
| 169 | } |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 170 | |
| 171 | #define checkval255(v) { \ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 172 | if (v < 254) { \ |
| 173 | printf("\nComp. %s at %d,%d should be 255, not %d\n", #v, row, col, v); \ |
| 174 | retval = 0; exitStatus = -1; goto bailout; \ |
| 175 | } \ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 179 | static int checkBuf(unsigned char *buf, int w, int h, int pf, int subsamp, |
| 180 | tjscalingfactor sf, int flags) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 181 | { |
| 182 | int roffset = tjRedOffset[pf]; |
| 183 | int goffset = tjGreenOffset[pf]; |
| 184 | int boffset = tjBlueOffset[pf]; |
| 185 | int aoffset = tjAlphaOffset[pf]; |
| 186 | int ps = tjPixelSize[pf]; |
| 187 | int index, row, col, retval = 1; |
| 188 | int halfway = 16 * sf.num / sf.denom; |
| 189 | int blocksize = 8 * sf.num / sf.denom; |
| 190 | |
| 191 | if (pf == TJPF_GRAY) roffset = goffset = boffset = 0; |
| 192 | |
| 193 | if (pf == TJPF_CMYK) { |
| 194 | for (row = 0; row < h; row++) { |
| 195 | for (col = 0; col < w; col++) { |
| 196 | unsigned char c, m, y, k; |
| 197 | |
| 198 | if (flags & TJFLAG_BOTTOMUP) index = (h - row - 1) * w + col; |
| 199 | else index = row * w + col; |
| 200 | c = buf[index * ps]; |
| 201 | m = buf[index * ps + 1]; |
| 202 | y = buf[index * ps + 2]; |
| 203 | k = buf[index * ps + 3]; |
| 204 | if (((row / blocksize) + (col / blocksize)) % 2 == 0) { |
| 205 | checkval255(c); checkval255(m); checkval255(y); |
| 206 | if (row < halfway) checkval255(k) |
| 207 | else checkval0(k) |
| 208 | } else { |
| 209 | checkval255(c); checkval0(y); checkval255(k); |
| 210 | if (row < halfway) checkval0(m) |
| 211 | else checkval255(m) |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | return 1; |
| 216 | } |
| 217 | |
| 218 | for (row = 0; row < h; row++) { |
| 219 | for (col = 0; col < w; col++) { |
| 220 | unsigned char r, g, b, a; |
| 221 | |
| 222 | if (flags & TJFLAG_BOTTOMUP) index = (h - row - 1) * w + col; |
| 223 | else index = row * w + col; |
| 224 | r = buf[index * ps + roffset]; |
| 225 | g = buf[index * ps + goffset]; |
| 226 | b = buf[index * ps + boffset]; |
| 227 | a = aoffset >= 0 ? buf[index * ps + aoffset] : 0xFF; |
| 228 | if (((row / blocksize) + (col / blocksize)) % 2 == 0) { |
| 229 | if (row < halfway) { |
| 230 | checkval255(r); checkval255(g); checkval255(b); |
| 231 | } else { |
| 232 | checkval0(r); checkval0(g); checkval0(b); |
| 233 | } |
| 234 | } else { |
| 235 | if (subsamp == TJSAMP_GRAY) { |
| 236 | if (row < halfway) { |
| 237 | checkval(r, 76); checkval(g, 76); checkval(b, 76); |
| 238 | } else { |
| 239 | checkval(r, 226); checkval(g, 226); checkval(b, 226); |
| 240 | } |
| 241 | } else { |
| 242 | if (row < halfway) { |
| 243 | checkval255(r); checkval0(g); checkval0(b); |
| 244 | } else { |
| 245 | checkval255(r); checkval255(g); checkval0(b); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | checkval255(a); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | bailout: |
| 254 | if (retval == 0) { |
| 255 | for (row = 0; row < h; row++) { |
| 256 | for (col = 0; col < w; col++) { |
| 257 | if (pf == TJPF_CMYK) |
| 258 | printf("%.3d/%.3d/%.3d/%.3d ", buf[(row * w + col) * ps], |
| 259 | buf[(row * w + col) * ps + 1], buf[(row * w + col) * ps + 2], |
| 260 | buf[(row * w + col) * ps + 3]); |
| 261 | else |
| 262 | printf("%.3d/%.3d/%.3d ", buf[(row * w + col) * ps + roffset], |
| 263 | buf[(row * w + col) * ps + goffset], |
| 264 | buf[(row * w + col) * ps + boffset]); |
| 265 | } |
| 266 | printf("\n"); |
| 267 | } |
| 268 | } |
| 269 | return retval; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | #define PAD(v, p) ((v + (p) - 1) & (~((p) - 1))) |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 274 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 275 | static int checkBufYUV(unsigned char *buf, int w, int h, int subsamp, |
| 276 | tjscalingfactor sf) |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 277 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 278 | int row, col; |
| 279 | int hsf = tjMCUWidth[subsamp] / 8, vsf = tjMCUHeight[subsamp] / 8; |
| 280 | int pw = PAD(w, hsf), ph = PAD(h, vsf); |
| 281 | int cw = pw / hsf, ch = ph / vsf; |
| 282 | int ypitch = PAD(pw, pad), uvpitch = PAD(cw, pad); |
| 283 | int retval = 1; |
| 284 | int halfway = 16 * sf.num / sf.denom; |
| 285 | int blocksize = 8 * sf.num / sf.denom; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 286 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 287 | for (row = 0; row < ph; row++) { |
| 288 | for (col = 0; col < pw; col++) { |
| 289 | unsigned char y = buf[ypitch * row + col]; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 290 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 291 | if (((row / blocksize) + (col / blocksize)) % 2 == 0) { |
| 292 | if (row < halfway) checkval255(y) |
| 293 | else checkval0(y); |
| 294 | } else { |
| 295 | if (row < halfway) checkval(y, 76) |
| 296 | else checkval(y, 226); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | if (subsamp != TJSAMP_GRAY) { |
| 301 | int halfway = 16 / vsf * sf.num / sf.denom; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 302 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 303 | for (row = 0; row < ch; row++) { |
| 304 | for (col = 0; col < cw; col++) { |
| 305 | unsigned char u = buf[ypitch * ph + (uvpitch * row + col)], |
| 306 | v = buf[ypitch * ph + uvpitch * ch + (uvpitch * row + col)]; |
| 307 | |
| 308 | if (((row * vsf / blocksize) + (col * hsf / blocksize)) % 2 == 0) { |
| 309 | checkval(u, 128); checkval(v, 128); |
| 310 | } else { |
| 311 | if (row < halfway) { |
| 312 | checkval(u, 85); checkval255(v); |
| 313 | } else { |
| 314 | checkval0(u); checkval(v, 149); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | bailout: |
| 322 | if (retval == 0) { |
| 323 | for (row = 0; row < ph; row++) { |
| 324 | for (col = 0; col < pw; col++) |
| 325 | printf("%.3d ", buf[ypitch * row + col]); |
| 326 | printf("\n"); |
| 327 | } |
| 328 | printf("\n"); |
| 329 | for (row = 0; row < ch; row++) { |
| 330 | for (col = 0; col < cw; col++) |
| 331 | printf("%.3d ", buf[ypitch * ph + (uvpitch * row + col)]); |
| 332 | printf("\n"); |
| 333 | } |
| 334 | printf("\n"); |
| 335 | for (row = 0; row < ch; row++) { |
| 336 | for (col = 0; col < cw; col++) |
| 337 | printf("%.3d ", |
| 338 | buf[ypitch * ph + uvpitch * ch + (uvpitch * row + col)]); |
| 339 | printf("\n"); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return retval; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 347 | static void writeJPEG(unsigned char *jpegBuf, unsigned long jpegSize, |
| 348 | char *filename) |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 349 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 350 | FILE *file = fopen(filename, "wb"); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 351 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 352 | if (!file || fwrite(jpegBuf, jpegSize, 1, file) != 1) { |
| 353 | printf("ERROR: Could not write to %s.\n%s\n", filename, strerror(errno)); |
| 354 | bailout() |
| 355 | } |
| 356 | |
| 357 | bailout: |
| 358 | if (file) fclose(file); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 362 | static void compTest(tjhandle handle, unsigned char **dstBuf, |
| 363 | unsigned long *dstSize, int w, int h, int pf, |
| 364 | char *basename, int subsamp, int jpegQual, int flags) |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 365 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 366 | char tempStr[1024]; |
| 367 | unsigned char *srcBuf = NULL, *yuvBuf = NULL; |
| 368 | const char *pfStr = pixFormatStr[pf]; |
| 369 | const char *buStrLong = |
| 370 | (flags & TJFLAG_BOTTOMUP) ? "Bottom-Up" : "Top-Down "; |
| 371 | const char *buStr = (flags & TJFLAG_BOTTOMUP) ? "BU" : "TD"; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 372 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 373 | if ((srcBuf = (unsigned char *)malloc(w * h * tjPixelSize[pf])) == NULL) |
| 374 | _throw("Memory allocation failure"); |
| 375 | initBuf(srcBuf, w, h, pf, flags); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 376 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 377 | if (*dstBuf && *dstSize > 0) memset(*dstBuf, 0, *dstSize); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 378 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 379 | if (!alloc) flags |= TJFLAG_NOREALLOC; |
| 380 | if (doYUV) { |
| 381 | unsigned long yuvSize = tjBufSizeYUV2(w, pad, h, subsamp); |
| 382 | tjscalingfactor sf = { 1, 1 }; |
| 383 | tjhandle handle2 = tjInitCompress(); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 384 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 385 | if (!handle2) _throwtj(); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 386 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 387 | if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL) |
| 388 | _throw("Memory allocation failure"); |
| 389 | memset(yuvBuf, 0, yuvSize); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 390 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 391 | printf("%s %s -> YUV %s ... ", pfStr, buStrLong, subNameLong[subsamp]); |
| 392 | _tj(tjEncodeYUV3(handle2, srcBuf, w, 0, h, pf, yuvBuf, pad, subsamp, |
| 393 | flags)); |
| 394 | tjDestroy(handle2); |
| 395 | if (checkBufYUV(yuvBuf, w, h, subsamp, sf)) printf("Passed.\n"); |
| 396 | else printf("FAILED!\n"); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 397 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 398 | printf("YUV %s %s -> JPEG Q%d ... ", subNameLong[subsamp], buStrLong, |
| 399 | jpegQual); |
| 400 | _tj(tjCompressFromYUV(handle, yuvBuf, w, pad, h, subsamp, dstBuf, dstSize, |
| 401 | jpegQual, flags)); |
| 402 | } else { |
| 403 | printf("%s %s -> %s Q%d ... ", pfStr, buStrLong, subNameLong[subsamp], |
| 404 | jpegQual); |
| 405 | _tj(tjCompress2(handle, srcBuf, w, 0, h, pf, dstBuf, dstSize, subsamp, |
| 406 | jpegQual, flags)); |
| 407 | } |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 408 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 409 | snprintf(tempStr, 1024, "%s_enc_%s_%s_%s_Q%d.jpg", basename, pfStr, buStr, |
| 410 | subName[subsamp], jpegQual); |
| 411 | writeJPEG(*dstBuf, *dstSize, tempStr); |
| 412 | printf("Done.\n Result in %s\n", tempStr); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 413 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 414 | bailout: |
| 415 | if (yuvBuf) free(yuvBuf); |
| 416 | if (srcBuf) free(srcBuf); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 420 | static void _decompTest(tjhandle handle, unsigned char *jpegBuf, |
| 421 | unsigned long jpegSize, int w, int h, int pf, |
| 422 | char *basename, int subsamp, int flags, |
| 423 | tjscalingfactor sf) |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 424 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 425 | unsigned char *dstBuf = NULL, *yuvBuf = NULL; |
| 426 | int _hdrw = 0, _hdrh = 0, _hdrsubsamp = -1; |
| 427 | int scaledWidth = TJSCALED(w, sf); |
| 428 | int scaledHeight = TJSCALED(h, sf); |
| 429 | unsigned long dstSize = 0; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 430 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 431 | _tj(tjDecompressHeader2(handle, jpegBuf, jpegSize, &_hdrw, &_hdrh, |
| 432 | &_hdrsubsamp)); |
| 433 | if (_hdrw != w || _hdrh != h || _hdrsubsamp != subsamp) |
| 434 | _throw("Incorrect JPEG header"); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 435 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 436 | dstSize = scaledWidth * scaledHeight * tjPixelSize[pf]; |
| 437 | if ((dstBuf = (unsigned char *)malloc(dstSize)) == NULL) |
| 438 | _throw("Memory allocation failure"); |
| 439 | memset(dstBuf, 0, dstSize); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 440 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 441 | if (doYUV) { |
| 442 | unsigned long yuvSize = tjBufSizeYUV2(scaledWidth, pad, scaledHeight, |
| 443 | subsamp); |
| 444 | tjhandle handle2 = tjInitDecompress(); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 445 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 446 | if (!handle2) _throwtj(); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 447 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 448 | if ((yuvBuf = (unsigned char *)malloc(yuvSize)) == NULL) |
| 449 | _throw("Memory allocation failure"); |
| 450 | memset(yuvBuf, 0, yuvSize); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 451 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 452 | printf("JPEG -> YUV %s ", subNameLong[subsamp]); |
| 453 | if (sf.num != 1 || sf.denom != 1) |
| 454 | printf("%d/%d ... ", sf.num, sf.denom); |
| 455 | else printf("... "); |
| 456 | _tj(tjDecompressToYUV2(handle, jpegBuf, jpegSize, yuvBuf, scaledWidth, pad, |
| 457 | scaledHeight, flags)); |
| 458 | if (checkBufYUV(yuvBuf, scaledWidth, scaledHeight, subsamp, sf)) |
| 459 | printf("Passed.\n"); |
| 460 | else printf("FAILED!\n"); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 461 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 462 | printf("YUV %s -> %s %s ... ", subNameLong[subsamp], pixFormatStr[pf], |
| 463 | (flags & TJFLAG_BOTTOMUP) ? "Bottom-Up" : "Top-Down "); |
| 464 | _tj(tjDecodeYUV(handle2, yuvBuf, pad, subsamp, dstBuf, scaledWidth, 0, |
| 465 | scaledHeight, pf, flags)); |
| 466 | tjDestroy(handle2); |
| 467 | } else { |
| 468 | printf("JPEG -> %s %s ", pixFormatStr[pf], |
| 469 | (flags & TJFLAG_BOTTOMUP) ? "Bottom-Up" : "Top-Down "); |
| 470 | if (sf.num != 1 || sf.denom != 1) |
| 471 | printf("%d/%d ... ", sf.num, sf.denom); |
| 472 | else printf("... "); |
| 473 | _tj(tjDecompress2(handle, jpegBuf, jpegSize, dstBuf, scaledWidth, 0, |
| 474 | scaledHeight, pf, flags)); |
| 475 | } |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 476 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 477 | if (checkBuf(dstBuf, scaledWidth, scaledHeight, pf, subsamp, sf, flags)) |
| 478 | printf("Passed."); |
| 479 | else printf("FAILED!"); |
| 480 | printf("\n"); |
| 481 | |
| 482 | bailout: |
| 483 | if (yuvBuf) free(yuvBuf); |
| 484 | if (dstBuf) free(dstBuf); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 488 | static void decompTest(tjhandle handle, unsigned char *jpegBuf, |
| 489 | unsigned long jpegSize, int w, int h, int pf, |
| 490 | char *basename, int subsamp, int flags) |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 491 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 492 | int i, n = 0; |
| 493 | tjscalingfactor *sf = tjGetScalingFactors(&n); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 494 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 495 | if (!sf || !n) _throwtj(); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 496 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 497 | for (i = 0; i < n; i++) { |
| 498 | if (subsamp == TJSAMP_444 || subsamp == TJSAMP_GRAY || |
| 499 | (subsamp == TJSAMP_411 && sf[i].num == 1 && |
| 500 | (sf[i].denom == 2 || sf[i].denom == 1)) || |
| 501 | (subsamp != TJSAMP_411 && sf[i].num == 1 && |
| 502 | (sf[i].denom == 4 || sf[i].denom == 2 || sf[i].denom == 1))) |
| 503 | _decompTest(handle, jpegBuf, jpegSize, w, h, pf, basename, subsamp, |
| 504 | flags, sf[i]); |
| 505 | } |
| 506 | |
| 507 | bailout: |
| 508 | return; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 512 | static void doTest(int w, int h, const int *formats, int nformats, int subsamp, |
| 513 | char *basename) |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 514 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 515 | tjhandle chandle = NULL, dhandle = NULL; |
| 516 | unsigned char *dstBuf = NULL; |
| 517 | unsigned long size = 0; |
| 518 | int pfi, pf, i; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 519 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 520 | if (!alloc) |
| 521 | size = tjBufSize(w, h, subsamp); |
| 522 | if (size != 0) |
| 523 | if ((dstBuf = (unsigned char *)tjAlloc(size)) == NULL) |
| 524 | _throw("Memory allocation failure."); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 525 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 526 | if ((chandle = tjInitCompress()) == NULL || |
| 527 | (dhandle = tjInitDecompress()) == NULL) |
| 528 | _throwtj(); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 529 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 530 | for (pfi = 0; pfi < nformats; pfi++) { |
| 531 | for (i = 0; i < 2; i++) { |
| 532 | int flags = 0; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 533 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 534 | if (subsamp == TJSAMP_422 || subsamp == TJSAMP_420 || |
| 535 | subsamp == TJSAMP_440 || subsamp == TJSAMP_411) |
| 536 | flags |= TJFLAG_FASTUPSAMPLE; |
| 537 | if (i == 1) flags |= TJFLAG_BOTTOMUP; |
| 538 | pf = formats[pfi]; |
| 539 | compTest(chandle, &dstBuf, &size, w, h, pf, basename, subsamp, 100, |
| 540 | flags); |
| 541 | decompTest(dhandle, dstBuf, size, w, h, pf, basename, subsamp, flags); |
| 542 | if (pf >= TJPF_RGBX && pf <= TJPF_XRGB) { |
| 543 | printf("\n"); |
| 544 | decompTest(dhandle, dstBuf, size, w, h, pf + (TJPF_RGBA - TJPF_RGBX), |
| 545 | basename, subsamp, flags); |
| 546 | } |
| 547 | printf("\n"); |
| 548 | } |
| 549 | } |
| 550 | printf("--------------------\n\n"); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 551 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 552 | bailout: |
| 553 | if (chandle) tjDestroy(chandle); |
| 554 | if (dhandle) tjDestroy(dhandle); |
| 555 | if (dstBuf) tjFree(dstBuf); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 559 | static void bufSizeTest(void) |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 560 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 561 | int w, h, i, subsamp; |
| 562 | unsigned char *srcBuf = NULL, *dstBuf = NULL; |
| 563 | tjhandle handle = NULL; |
| 564 | unsigned long dstSize = 0; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 565 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 566 | if ((handle = tjInitCompress()) == NULL) _throwtj(); |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 567 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 568 | printf("Buffer size regression test\n"); |
| 569 | for (subsamp = 0; subsamp < TJ_NUMSAMP; subsamp++) { |
| 570 | for (w = 1; w < 48; w++) { |
| 571 | int maxh = (w == 1) ? 2048 : 48; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 572 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 573 | for (h = 1; h < maxh; h++) { |
| 574 | if (h % 100 == 0) printf("%.4d x %.4d\b\b\b\b\b\b\b\b\b\b\b", w, h); |
| 575 | if ((srcBuf = (unsigned char *)malloc(w * h * 4)) == NULL) |
| 576 | _throw("Memory allocation failure"); |
| 577 | if (!alloc || doYUV) { |
| 578 | if (doYUV) dstSize = tjBufSizeYUV2(w, pad, h, subsamp); |
| 579 | else dstSize = tjBufSize(w, h, subsamp); |
| 580 | if ((dstBuf = (unsigned char *)tjAlloc(dstSize)) == NULL) |
| 581 | _throw("Memory allocation failure"); |
| 582 | } |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 583 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 584 | for (i = 0; i < w * h * 4; i++) { |
| 585 | if (random() < RAND_MAX / 2) srcBuf[i] = 0; |
| 586 | else srcBuf[i] = 255; |
| 587 | } |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 588 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 589 | if (doYUV) { |
| 590 | _tj(tjEncodeYUV3(handle, srcBuf, w, 0, h, TJPF_BGRX, dstBuf, pad, |
| 591 | subsamp, 0)); |
| 592 | } else { |
| 593 | _tj(tjCompress2(handle, srcBuf, w, 0, h, TJPF_BGRX, &dstBuf, |
| 594 | &dstSize, subsamp, 100, |
| 595 | alloc ? 0 : TJFLAG_NOREALLOC)); |
| 596 | } |
| 597 | free(srcBuf); srcBuf = NULL; |
| 598 | if (!alloc || doYUV) { |
| 599 | tjFree(dstBuf); dstBuf = NULL; |
| 600 | } |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 601 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 602 | if ((srcBuf = (unsigned char *)malloc(h * w * 4)) == NULL) |
| 603 | _throw("Memory allocation failure"); |
| 604 | if (!alloc || doYUV) { |
| 605 | if (doYUV) dstSize = tjBufSizeYUV2(h, pad, w, subsamp); |
| 606 | else dstSize = tjBufSize(h, w, subsamp); |
| 607 | if ((dstBuf = (unsigned char *)tjAlloc(dstSize)) == NULL) |
| 608 | _throw("Memory allocation failure"); |
| 609 | } |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 610 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 611 | for (i = 0; i < h * w * 4; i++) { |
| 612 | if (random() < RAND_MAX / 2) srcBuf[i] = 0; |
| 613 | else srcBuf[i] = 255; |
| 614 | } |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 615 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 616 | if (doYUV) { |
| 617 | _tj(tjEncodeYUV3(handle, srcBuf, h, 0, w, TJPF_BGRX, dstBuf, pad, |
| 618 | subsamp, 0)); |
| 619 | } else { |
| 620 | _tj(tjCompress2(handle, srcBuf, h, 0, w, TJPF_BGRX, &dstBuf, |
| 621 | &dstSize, subsamp, 100, |
| 622 | alloc ? 0 : TJFLAG_NOREALLOC)); |
| 623 | } |
| 624 | free(srcBuf); srcBuf = NULL; |
| 625 | if (!alloc || doYUV) { |
| 626 | tjFree(dstBuf); dstBuf = NULL; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | printf("Done. \n"); |
| 632 | |
| 633 | bailout: |
| 634 | if (srcBuf) free(srcBuf); |
| 635 | if (dstBuf) tjFree(dstBuf); |
| 636 | if (handle) tjDestroy(handle); |
| 637 | } |
| 638 | |
| 639 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 640 | static void initBitmap(unsigned char *buf, int width, int pitch, int height, |
| 641 | int pf, int flags) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 642 | { |
| 643 | int roffset = tjRedOffset[pf]; |
| 644 | int goffset = tjGreenOffset[pf]; |
| 645 | int boffset = tjBlueOffset[pf]; |
| 646 | int ps = tjPixelSize[pf]; |
| 647 | int i, j; |
| 648 | |
| 649 | for (j = 0; j < height; j++) { |
| 650 | int row = (flags & TJFLAG_BOTTOMUP) ? height - j - 1 : j; |
| 651 | |
| 652 | for (i = 0; i < width; i++) { |
| 653 | unsigned char r = (i * 256 / width) % 256; |
| 654 | unsigned char g = (j * 256 / height) % 256; |
| 655 | unsigned char b = (j * 256 / height + i * 256 / width) % 256; |
| 656 | |
| 657 | memset(&buf[row * pitch + i * ps], 0, ps); |
| 658 | if (pf == TJPF_GRAY) buf[row * pitch + i * ps] = b; |
| 659 | else if (pf == TJPF_CMYK) |
| 660 | rgb_to_cmyk(r, g, b, &buf[row * pitch + i * ps + 0], |
| 661 | &buf[row * pitch + i * ps + 1], |
| 662 | &buf[row * pitch + i * ps + 2], |
| 663 | &buf[row * pitch + i * ps + 3]); |
| 664 | else { |
| 665 | buf[row * pitch + i * ps + roffset] = r; |
| 666 | buf[row * pitch + i * ps + goffset] = g; |
| 667 | buf[row * pitch + i * ps + boffset] = b; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 674 | static int cmpBitmap(unsigned char *buf, int width, int pitch, int height, |
| 675 | int pf, int flags, int gray2rgb) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 676 | { |
| 677 | int roffset = tjRedOffset[pf]; |
| 678 | int goffset = tjGreenOffset[pf]; |
| 679 | int boffset = tjBlueOffset[pf]; |
| 680 | int aoffset = tjAlphaOffset[pf]; |
| 681 | int ps = tjPixelSize[pf]; |
| 682 | int i, j; |
| 683 | |
| 684 | for (j = 0; j < height; j++) { |
| 685 | int row = (flags & TJFLAG_BOTTOMUP) ? height - j - 1 : j; |
| 686 | |
| 687 | for (i = 0; i < width; i++) { |
| 688 | unsigned char r = (i * 256 / width) % 256; |
| 689 | unsigned char g = (j * 256 / height) % 256; |
| 690 | unsigned char b = (j * 256 / height + i * 256 / width) % 256; |
| 691 | |
| 692 | if (pf == TJPF_GRAY) { |
| 693 | if (buf[row * pitch + i * ps] != b) |
| 694 | return 0; |
| 695 | } else if (pf == TJPF_CMYK) { |
| 696 | unsigned char rf, gf, bf; |
| 697 | |
| 698 | cmyk_to_rgb(buf[row * pitch + i * ps + 0], |
| 699 | buf[row * pitch + i * ps + 1], |
| 700 | buf[row * pitch + i * ps + 2], |
| 701 | buf[row * pitch + i * ps + 3], &rf, &gf, &bf); |
| 702 | if (gray2rgb) { |
| 703 | if (rf != b || gf != b || bf != b) |
| 704 | return 0; |
| 705 | } else if (rf != r || gf != g || bf != b) return 0; |
| 706 | } else { |
| 707 | if (gray2rgb) { |
| 708 | if (buf[row * pitch + i * ps + roffset] != b || |
| 709 | buf[row * pitch + i * ps + goffset] != b || |
| 710 | buf[row * pitch + i * ps + boffset] != b) |
| 711 | return 0; |
| 712 | } else if (buf[row * pitch + i * ps + roffset] != r || |
| 713 | buf[row * pitch + i * ps + goffset] != g || |
| 714 | buf[row * pitch + i * ps + boffset] != b) |
| 715 | return 0; |
| 716 | if (aoffset >= 0 && buf[row * pitch + i * ps + aoffset] != 0xFF) |
| 717 | return 0; |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | return 1; |
| 722 | } |
| 723 | |
| 724 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 725 | static int doBmpTest(const char *ext, int width, int align, int height, int pf, |
| 726 | int flags) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 727 | { |
| 728 | char filename[80], *md5sum, md5buf[65]; |
| 729 | int ps = tjPixelSize[pf], pitch = PAD(width * ps, align), loadWidth = 0, |
| 730 | loadHeight = 0, retval = 0, pixelFormat = pf; |
| 731 | unsigned char *buf = NULL; |
| 732 | char *md5ref; |
| 733 | |
| 734 | if (pf == TJPF_GRAY) { |
| 735 | md5ref = !strcasecmp(ext, "ppm") ? "112c682e82ce5de1cca089e20d60000b" : |
| 736 | "51976530acf75f02beddf5d21149101d"; |
| 737 | } else { |
| 738 | md5ref = !strcasecmp(ext, "ppm") ? "c0c9f772b464d1896326883a5c79c545" : |
| 739 | "6d659071b9bfcdee2def22cb58ddadca"; |
| 740 | } |
| 741 | |
| 742 | if ((buf = (unsigned char *)tjAlloc(pitch * height)) == NULL) |
| 743 | _throw("Could not allocate memory"); |
| 744 | initBitmap(buf, width, pitch, height, pf, flags); |
| 745 | |
| 746 | snprintf(filename, 80, "test_bmp_%s_%d_%s.%s", pixFormatStr[pf], align, |
| 747 | (flags & TJFLAG_BOTTOMUP) ? "bu" : "td", ext); |
| 748 | _tj(tjSaveImage(filename, buf, width, pitch, height, pf, flags)); |
| 749 | md5sum = MD5File(filename, md5buf); |
| 750 | if (strcasecmp(md5sum, md5ref)) |
| 751 | _throwmd5(filename, md5sum, md5ref); |
| 752 | |
| 753 | tjFree(buf); buf = NULL; |
| 754 | if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf, |
| 755 | flags)) == NULL) |
| 756 | _throwtj(); |
| 757 | if (width != loadWidth || height != loadHeight) { |
| 758 | printf("\n Image dimensions of %s are bogus\n", filename); |
| 759 | retval = -1; goto bailout; |
| 760 | } |
| 761 | if (!cmpBitmap(buf, width, pitch, height, pf, flags, 0)) { |
| 762 | printf("\n Pixel data in %s is bogus\n", filename); |
| 763 | retval = -1; goto bailout; |
| 764 | } |
| 765 | if (pf == TJPF_GRAY) { |
| 766 | tjFree(buf); buf = NULL; |
| 767 | pf = TJPF_XBGR; |
| 768 | if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf, |
| 769 | flags)) == NULL) |
| 770 | _throwtj(); |
| 771 | pitch = PAD(width * tjPixelSize[pf], align); |
| 772 | if (!cmpBitmap(buf, width, pitch, height, pf, flags, 1)) { |
| 773 | printf("\n Converting %s to RGB failed\n", filename); |
| 774 | retval = -1; goto bailout; |
| 775 | } |
| 776 | |
| 777 | tjFree(buf); buf = NULL; |
| 778 | pf = TJPF_CMYK; |
| 779 | if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight, &pf, |
| 780 | flags)) == NULL) |
| 781 | _throwtj(); |
| 782 | pitch = PAD(width * tjPixelSize[pf], align); |
| 783 | if (!cmpBitmap(buf, width, pitch, height, pf, flags, 1)) { |
| 784 | printf("\n Converting %s to CMYK failed\n", filename); |
| 785 | retval = -1; goto bailout; |
| 786 | } |
| 787 | } |
| 788 | /* Verify that tjLoadImage() returns the proper "preferred" pixel format for |
| 789 | the file type. */ |
| 790 | tjFree(buf); buf = NULL; |
| 791 | pf = pixelFormat; |
| 792 | pixelFormat = TJPF_UNKNOWN; |
| 793 | if ((buf = tjLoadImage(filename, &loadWidth, align, &loadHeight, |
| 794 | &pixelFormat, flags)) == NULL) |
| 795 | _throwtj(); |
| 796 | if ((pf == TJPF_GRAY && pixelFormat != TJPF_GRAY) || |
| 797 | (pf != TJPF_GRAY && !strcasecmp(ext, "bmp") && |
| 798 | pixelFormat != TJPF_BGR) || |
| 799 | (pf != TJPF_GRAY && !strcasecmp(ext, "ppm") && |
| 800 | pixelFormat != TJPF_RGB)) { |
| 801 | printf("\n tjLoadImage() returned unexpected pixel format: %s\n", |
| 802 | pixFormatStr[pixelFormat]); |
| 803 | retval = -1; |
| 804 | } |
| 805 | unlink(filename); |
| 806 | |
| 807 | bailout: |
| 808 | if (buf) tjFree(buf); |
| 809 | if (exitStatus < 0) return exitStatus; |
| 810 | return retval; |
| 811 | } |
| 812 | |
| 813 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 814 | static int bmpTest(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 815 | { |
| 816 | int align, width = 35, height = 39, format; |
| 817 | |
| 818 | for (align = 1; align <= 8; align *= 2) { |
| 819 | for (format = 0; format < TJ_NUMPF; format++) { |
| 820 | printf("%s Top-Down BMP (row alignment = %d bytes) ... ", |
| 821 | pixFormatStr[format], align); |
| 822 | if (doBmpTest("bmp", width, align, height, format, 0) == -1) |
| 823 | return -1; |
| 824 | printf("OK.\n"); |
| 825 | |
| 826 | printf("%s Top-Down PPM (row alignment = %d bytes) ... ", |
| 827 | pixFormatStr[format], align); |
| 828 | if (doBmpTest("ppm", width, align, height, format, |
| 829 | TJFLAG_BOTTOMUP) == -1) |
| 830 | return -1; |
| 831 | printf("OK.\n"); |
| 832 | |
| 833 | printf("%s Bottom-Up BMP (row alignment = %d bytes) ... ", |
| 834 | pixFormatStr[format], align); |
| 835 | if (doBmpTest("bmp", width, align, height, format, 0) == -1) |
| 836 | return -1; |
| 837 | printf("OK.\n"); |
| 838 | |
| 839 | printf("%s Bottom-Up PPM (row alignment = %d bytes) ... ", |
| 840 | pixFormatStr[format], align); |
| 841 | if (doBmpTest("ppm", width, align, height, format, |
| 842 | TJFLAG_BOTTOMUP) == -1) |
| 843 | return -1; |
| 844 | printf("OK.\n"); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | return 0; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 851 | #ifdef GTEST |
| 852 | static void initTJUnitTest(int yuv, int noyuvpad, int autoalloc) |
| 853 | { |
| 854 | doYUV = yuv ? 1 : 0; |
| 855 | pad = noyuvpad ? 1 : 4; |
| 856 | alloc = autoalloc ? 1 : 0; |
| 857 | |
| 858 | exitStatus = 0; |
| 859 | } |
| 860 | |
| 861 | |
| 862 | int testBmp(int yuv, int noyuvpad, int autoalloc) |
| 863 | { |
| 864 | initTJUnitTest(yuv, noyuvpad, autoalloc); |
| 865 | |
| 866 | return bmpTest(); |
| 867 | } |
| 868 | |
| 869 | |
| 870 | int testThreeByte444(int yuv, int noyuvpad, int autoalloc) |
| 871 | { |
| 872 | initTJUnitTest(yuv, noyuvpad, autoalloc); |
| 873 | |
| 874 | doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test"); |
| 875 | return exitStatus; |
| 876 | } |
| 877 | |
| 878 | |
| 879 | int testFourByte444(int yuv, int noyuvpad, int autoalloc) |
| 880 | { |
| 881 | initTJUnitTest(yuv, noyuvpad, autoalloc); |
| 882 | |
| 883 | int num4bf = doYUV ? 4 : 5; |
| 884 | doTest(39, 41, _4byteFormats, num4bf, TJSAMP_444, "test"); |
| 885 | return exitStatus; |
| 886 | } |
| 887 | |
| 888 | |
| 889 | int testThreeByte422(int yuv, int noyuvpad, int autoalloc) |
| 890 | { |
| 891 | initTJUnitTest(yuv, noyuvpad, autoalloc); |
| 892 | |
| 893 | doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test"); |
| 894 | return exitStatus; |
| 895 | } |
| 896 | |
| 897 | |
| 898 | int testFourByte422(int yuv, int noyuvpad, int autoalloc) |
| 899 | { |
| 900 | initTJUnitTest(yuv, noyuvpad, autoalloc); |
| 901 | |
| 902 | int num4bf = doYUV ? 4 : 5; |
| 903 | doTest(35, 39, _4byteFormats, num4bf, TJSAMP_422, "test"); |
| 904 | return exitStatus; |
| 905 | } |
| 906 | |
| 907 | |
| 908 | int testThreeByte420(int yuv, int noyuvpad, int autoalloc) |
| 909 | { |
| 910 | initTJUnitTest(yuv, noyuvpad, autoalloc); |
| 911 | |
| 912 | doTest(39, 41, _3byteFormats, 2, TJSAMP_420, "test"); |
| 913 | return exitStatus; |
| 914 | } |
| 915 | |
| 916 | |
| 917 | int testFourByte420(int yuv, int noyuvpad, int autoalloc) |
| 918 | { |
| 919 | initTJUnitTest(yuv, noyuvpad, autoalloc); |
| 920 | |
| 921 | int num4bf = doYUV ? 4 : 5; |
| 922 | doTest(41, 35, _4byteFormats, num4bf, TJSAMP_420, "test"); |
| 923 | return exitStatus; |
| 924 | } |
| 925 | |
| 926 | |
| 927 | int testThreeByte440(int yuv, int noyuvpad, int autoalloc) |
| 928 | { |
| 929 | initTJUnitTest(yuv, noyuvpad, autoalloc); |
| 930 | |
| 931 | doTest(35, 39, _3byteFormats, 2, TJSAMP_440, "test"); |
| 932 | return exitStatus; |
| 933 | } |
| 934 | |
| 935 | |
| 936 | int testFourByte440(int yuv, int noyuvpad, int autoalloc) |
| 937 | { |
| 938 | initTJUnitTest(yuv, noyuvpad, autoalloc); |
| 939 | |
| 940 | int num4bf = doYUV ? 4 : 5; |
| 941 | doTest(39, 41, _4byteFormats, num4bf, TJSAMP_440, "test"); |
| 942 | return exitStatus; |
| 943 | } |
| 944 | |
| 945 | |
| 946 | int testThreeByte411(int yuv, int noyuvpad, int autoalloc) |
| 947 | { |
| 948 | initTJUnitTest(yuv, noyuvpad, autoalloc); |
| 949 | |
| 950 | doTest(41, 35, _3byteFormats, 2, TJSAMP_411, "test"); |
| 951 | return exitStatus; |
| 952 | } |
| 953 | |
| 954 | |
| 955 | int testFourByte411(int yuv, int noyuvpad, int autoalloc) |
| 956 | { |
| 957 | initTJUnitTest(yuv, noyuvpad, autoalloc); |
| 958 | |
| 959 | int num4bf = doYUV ? 4 : 5; |
| 960 | doTest(35, 39, _4byteFormats, num4bf, TJSAMP_411, "test"); |
| 961 | return exitStatus; |
| 962 | } |
| 963 | |
| 964 | |
| 965 | int testOnlyGray(int yuv, int noyuvpad, int autoalloc) |
| 966 | { |
| 967 | initTJUnitTest(yuv, noyuvpad, autoalloc); |
| 968 | |
| 969 | doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test"); |
| 970 | return exitStatus; |
| 971 | } |
| 972 | |
| 973 | |
| 974 | int testThreeByteGray(int yuv, int noyuvpad, int autoalloc) |
| 975 | { |
| 976 | initTJUnitTest(yuv, noyuvpad, autoalloc); |
| 977 | |
| 978 | doTest(41, 35, _3byteFormats, 2, TJSAMP_GRAY, "test"); |
| 979 | return exitStatus; |
| 980 | } |
| 981 | |
| 982 | |
| 983 | int testFourByteGray(int yuv, int noyuvpad, int autoalloc) |
| 984 | { |
| 985 | initTJUnitTest(yuv, noyuvpad, autoalloc); |
| 986 | |
| 987 | doTest(35, 39, _4byteFormats, 4, TJSAMP_GRAY, "test"); |
| 988 | return exitStatus; |
| 989 | } |
| 990 | |
| 991 | |
| 992 | int testBufSize(int yuv, int noyuvpad, int autoalloc) |
| 993 | { |
| 994 | initTJUnitTest(yuv, noyuvpad, autoalloc); |
| 995 | |
| 996 | bufSizeTest(); |
| 997 | return exitStatus; |
| 998 | } |
| 999 | |
| 1000 | |
| 1001 | int testYUVOnlyRGB444(int noyuvpad, int autoalloc) |
| 1002 | { |
| 1003 | initTJUnitTest(1, noyuvpad, autoalloc); |
| 1004 | |
| 1005 | doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0"); |
| 1006 | return exitStatus; |
| 1007 | } |
| 1008 | |
| 1009 | |
| 1010 | int testYUVOnlyRGB422(int noyuvpad, int autoalloc) |
| 1011 | { |
| 1012 | initTJUnitTest(1, noyuvpad, autoalloc); |
| 1013 | |
| 1014 | doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0"); |
| 1015 | return exitStatus; |
| 1016 | } |
| 1017 | |
| 1018 | |
| 1019 | int testYUVOnlyRGB420(int noyuvpad, int autoalloc) |
| 1020 | { |
| 1021 | initTJUnitTest(1, noyuvpad, autoalloc); |
| 1022 | |
| 1023 | doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0"); |
| 1024 | return exitStatus; |
| 1025 | } |
| 1026 | |
| 1027 | |
| 1028 | int testYUVOnlyRGB440(int noyuvpad, int autoalloc) |
| 1029 | { |
| 1030 | initTJUnitTest(1, noyuvpad, autoalloc); |
| 1031 | |
| 1032 | doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0"); |
| 1033 | return exitStatus; |
| 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | int testYUVOnlyRGB411(int noyuvpad, int autoalloc) |
| 1038 | { |
| 1039 | initTJUnitTest(1, noyuvpad, autoalloc); |
| 1040 | |
| 1041 | doTest(48, 48, _onlyRGB, 1, TJSAMP_411, "test_yuv0"); |
| 1042 | return exitStatus; |
| 1043 | } |
| 1044 | |
| 1045 | |
| 1046 | int testYUVOnlyRGBGray(int noyuvpad, int autoalloc) |
| 1047 | { |
| 1048 | initTJUnitTest(1, noyuvpad, autoalloc); |
| 1049 | |
| 1050 | doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0"); |
| 1051 | return exitStatus; |
| 1052 | } |
| 1053 | |
| 1054 | |
| 1055 | int testYUVOnlyGrayGray(int noyuvpad, int autoalloc) |
| 1056 | { |
| 1057 | initTJUnitTest(1, noyuvpad, autoalloc); |
| 1058 | |
| 1059 | doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0"); |
| 1060 | return exitStatus; |
| 1061 | } |
| 1062 | |
| 1063 | #else |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 1064 | |
| 1065 | int main(int argc, char *argv[]) |
| 1066 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1067 | int i, num4bf = 5; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 1068 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 1069 | #ifdef _WIN32 |
| 1070 | srand((unsigned int)time(NULL)); |
| 1071 | #endif |
| 1072 | if (argc > 1) { |
| 1073 | for (i = 1; i < argc; i++) { |
| 1074 | if (!strcasecmp(argv[i], "-yuv")) doYUV = 1; |
| 1075 | else if (!strcasecmp(argv[i], "-noyuvpad")) pad = 1; |
| 1076 | else if (!strcasecmp(argv[i], "-alloc")) alloc = 1; |
| 1077 | else if (!strcasecmp(argv[i], "-bmp")) return bmpTest(); |
| 1078 | else usage(argv[0]); |
| 1079 | } |
| 1080 | } |
| 1081 | if (alloc) printf("Testing automatic buffer allocation\n"); |
| 1082 | if (doYUV) num4bf = 4; |
| 1083 | doTest(35, 39, _3byteFormats, 2, TJSAMP_444, "test"); |
| 1084 | doTest(39, 41, _4byteFormats, num4bf, TJSAMP_444, "test"); |
| 1085 | doTest(41, 35, _3byteFormats, 2, TJSAMP_422, "test"); |
| 1086 | doTest(35, 39, _4byteFormats, num4bf, TJSAMP_422, "test"); |
| 1087 | doTest(39, 41, _3byteFormats, 2, TJSAMP_420, "test"); |
| 1088 | doTest(41, 35, _4byteFormats, num4bf, TJSAMP_420, "test"); |
| 1089 | doTest(35, 39, _3byteFormats, 2, TJSAMP_440, "test"); |
| 1090 | doTest(39, 41, _4byteFormats, num4bf, TJSAMP_440, "test"); |
| 1091 | doTest(41, 35, _3byteFormats, 2, TJSAMP_411, "test"); |
| 1092 | doTest(35, 39, _4byteFormats, num4bf, TJSAMP_411, "test"); |
| 1093 | doTest(39, 41, _onlyGray, 1, TJSAMP_GRAY, "test"); |
| 1094 | doTest(41, 35, _3byteFormats, 2, TJSAMP_GRAY, "test"); |
| 1095 | doTest(35, 39, _4byteFormats, 4, TJSAMP_GRAY, "test"); |
| 1096 | bufSizeTest(); |
| 1097 | if (doYUV) { |
| 1098 | printf("\n--------------------\n\n"); |
| 1099 | doTest(48, 48, _onlyRGB, 1, TJSAMP_444, "test_yuv0"); |
| 1100 | doTest(48, 48, _onlyRGB, 1, TJSAMP_422, "test_yuv0"); |
| 1101 | doTest(48, 48, _onlyRGB, 1, TJSAMP_420, "test_yuv0"); |
| 1102 | doTest(48, 48, _onlyRGB, 1, TJSAMP_440, "test_yuv0"); |
| 1103 | doTest(48, 48, _onlyRGB, 1, TJSAMP_411, "test_yuv0"); |
| 1104 | doTest(48, 48, _onlyRGB, 1, TJSAMP_GRAY, "test_yuv0"); |
| 1105 | doTest(48, 48, _onlyGray, 1, TJSAMP_GRAY, "test_yuv0"); |
| 1106 | } |
| 1107 | |
| 1108 | return exitStatus; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 1109 | } |
Jonathan Wright | 6cb95b8 | 2020-06-11 16:10:15 +0100 | [diff] [blame] | 1110 | #endif |