tjunittest.c 30 KB

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