tjunittest.c 30 KB

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