img_test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /* Host test for the image library (make test-images). Uses only the
  2. * freestanding decoder code; no kernel headers are involved. */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "img.h"
  7. #include "inflate.h"
  8. #include "testdata/images/expected.h"
  9. static unsigned char arena[64u * 1024u * 1024u] __attribute__((aligned(16)));
  10. static unsigned char filebuf[8u * 1024u * 1024u];
  11. static int failures;
  12. static int read_file(const char *path, unsigned char *buf, unsigned int cap,
  13. unsigned int *len)
  14. {
  15. FILE *f = fopen(path, "rb");
  16. long n;
  17. if (!f) {
  18. fprintf(stderr, "cannot open %s\n", path);
  19. return -1;
  20. }
  21. fseek(f, 0, SEEK_END);
  22. n = ftell(f);
  23. fseek(f, 0, SEEK_SET);
  24. if (n <= 0 || (unsigned long)n > cap) {
  25. fclose(f);
  26. return -1;
  27. }
  28. if (fread(buf, 1, (size_t)n, f) != (size_t)n) {
  29. fclose(f);
  30. return -1;
  31. }
  32. fclose(f);
  33. *len = (unsigned int)n;
  34. return 0;
  35. }
  36. static unsigned int fnv565(const struct img_image *im, int alpha)
  37. {
  38. unsigned int h = 2166136261u, i;
  39. for (i = 0; i < im->w * im->h; i++) {
  40. unsigned int px = im->px[i];
  41. unsigned int a = alpha ? im->alpha[i] : 255;
  42. if (!alpha || a != 0) {
  43. h = (h ^ (px & 255)) * 16777619u;
  44. h = (h ^ ((px >> 8) & 255)) * 16777619u;
  45. }
  46. if (alpha)
  47. h = (h ^ a) * 16777619u;
  48. }
  49. return h;
  50. }
  51. static int test_lossless(const struct img_exp *e)
  52. {
  53. char path[256];
  54. unsigned char *data = filebuf;
  55. unsigned int n;
  56. struct img_mem mem;
  57. struct img_options opt = {0};
  58. struct img_info info;
  59. struct img_image im;
  60. int r;
  61. snprintf(path, sizeof(path), "testdata/images/%s", e->path);
  62. if (read_file(path, data, sizeof(filebuf), &n)) {
  63. printf("FAIL %s: read\n", e->path);
  64. return 1;
  65. }
  66. memset(&info, 0, sizeof(info));
  67. if (img_sniff(data, n, &info) != 0 || info.width != e->w ||
  68. info.height != e->h) {
  69. printf("FAIL %s: sniff %ux%u (expected %ux%u)\n", e->path,
  70. info.width, info.height, e->w, e->h);
  71. return 1;
  72. }
  73. img_mem_init(&mem, arena, sizeof(arena));
  74. opt.max_pixels = 0;
  75. memset(&im, 0, sizeof(im));
  76. r = img_decode(data, n, &opt, &mem, &im);
  77. if (r) {
  78. printf("FAIL %s: %s (%d)\n", e->path, img_error_string(r), r);
  79. return 1;
  80. }
  81. if (im.w != e->w || im.h != e->h) {
  82. printf("FAIL %s: size %ux%u, expected %ux%u\n", e->path,
  83. im.w, im.h, e->w, e->h);
  84. return 1;
  85. }
  86. if (!!im.alpha != !!e->alpha) {
  87. printf("FAIL %s: alpha %d, expected %d\n", e->path,
  88. im.alpha != 0, e->alpha);
  89. return 1;
  90. }
  91. /* Animated GIFs: only the first frame is defined; PIL's first-frame
  92. * composition differs for the disposal/transparency corner case, so
  93. * only the size/alpha are checked here. */
  94. if (strcmp(e->path, "anim.gif") == 0)
  95. return 0;
  96. {
  97. unsigned int h = fnv565(&im, e->alpha);
  98. if (h != e->hash) {
  99. printf("FAIL %s: hash %08x, expected %08x\n", e->path,
  100. h, e->hash);
  101. return 1;
  102. }
  103. }
  104. return 0;
  105. }
  106. static int read_ppm(const char *path, unsigned int *w, unsigned int *h,
  107. unsigned char **pixels)
  108. {
  109. FILE *f = fopen(path, "rb");
  110. char magic[3];
  111. unsigned int maxv, n;
  112. int c;
  113. if (!f)
  114. return -1;
  115. if (fscanf(f, "%2s", magic) != 1 || strcmp(magic, "P6") != 0)
  116. goto fail;
  117. /* skip whitespace/comments */
  118. for (;;) {
  119. c = fgetc(f);
  120. if (c == '#') {
  121. while (c != '\n' && c != EOF)
  122. c = fgetc(f);
  123. } else if (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
  124. continue;
  125. } else {
  126. ungetc(c, f);
  127. break;
  128. }
  129. }
  130. if (fscanf(f, "%u %u %u", w, h, &maxv) != 3 || maxv != 255)
  131. goto fail;
  132. fgetc(f); /* single whitespace after maxval */
  133. n = *w * *h * 3;
  134. *pixels = (unsigned char *)malloc(n);
  135. if (!*pixels || fread(*pixels, 1, n, f) != n)
  136. goto fail;
  137. fclose(f);
  138. return 0;
  139. fail:
  140. fclose(f);
  141. return -1;
  142. }
  143. static int test_jpeg(const char *name)
  144. {
  145. char path[256], refpath[256];
  146. unsigned int n, rw, rh;
  147. unsigned char *ref;
  148. struct img_mem mem;
  149. struct img_options opt = {0};
  150. struct img_info info;
  151. struct img_image im;
  152. int r, x, y;
  153. unsigned long long sum = 0, count = 0;
  154. unsigned int mx = 0;
  155. snprintf(path, sizeof(path), "testdata/images/%s", name);
  156. snprintf(refpath, sizeof(refpath), "testdata/images/ref/%s.ppm", name);
  157. if (read_file(path, filebuf, sizeof(filebuf), &n) ||
  158. read_ppm(refpath, &rw, &rh, &ref)) {
  159. printf("FAIL %s: read\n", name);
  160. return 1;
  161. }
  162. memset(&info, 0, sizeof(info));
  163. if (img_sniff(filebuf, n, &info) != 0 || info.width != rw ||
  164. info.height != rh) {
  165. printf("FAIL %s: sniff %ux%u, expected %ux%u\n", name,
  166. info.width, info.height, rw, rh);
  167. free(ref);
  168. return 1;
  169. }
  170. img_mem_init(&mem, arena, sizeof(arena));
  171. opt.max_pixels = 0;
  172. memset(&im, 0, sizeof(im));
  173. r = img_decode(filebuf, n, &opt, &mem, &im);
  174. if (r) {
  175. printf("FAIL %s: %s\n", name, img_error_string(r));
  176. free(ref);
  177. return 1;
  178. }
  179. if (im.w != rw || im.h != rh) {
  180. printf("FAIL %s: size mismatch\n", name);
  181. free(ref);
  182. return 1;
  183. }
  184. for (y = 0; y < (int)im.h; y++) {
  185. for (x = 0; x < (int)im.w; x++) {
  186. unsigned int px = im.px[y * im.w + x];
  187. int rr = ((px >> 11) & 31) * 255 / 31;
  188. int gg = ((px >> 5) & 63) * 255 / 63;
  189. int bb = (px & 31) * 255 / 31;
  190. unsigned char *p = ref + ((unsigned int)y * im.w + x) * 3;
  191. int dr = abs(rr - p[0]);
  192. int dg = abs(gg - p[1]);
  193. int db = abs(bb - p[2]);
  194. sum += (unsigned long long)dr + dg + db;
  195. count += 3;
  196. if ((unsigned int)dr > mx) mx = dr;
  197. if ((unsigned int)dg > mx) mx = dg;
  198. if ((unsigned int)db > mx) mx = db;
  199. }
  200. }
  201. free(ref);
  202. if (sum / count > 8 || mx > 130) {
  203. printf("FAIL %s: mean %.2f max %u\n", name,
  204. (double)sum / (double)count, mx);
  205. return 1;
  206. }
  207. return 0;
  208. }
  209. static int test_jpeg_scaled(const char *name)
  210. {
  211. char path[256];
  212. unsigned int n, w, h;
  213. unsigned short *full;
  214. struct img_mem mem;
  215. struct img_options opt = {0};
  216. struct img_image im;
  217. int r, div, x, y;
  218. snprintf(path, sizeof(path), "testdata/images/%s", name);
  219. if (read_file(path, filebuf, sizeof(filebuf), &n))
  220. return 1;
  221. img_mem_init(&mem, arena, sizeof(arena));
  222. memset(&im, 0, sizeof(im));
  223. if (img_decode(filebuf, n, &opt, &mem, &im) != 0)
  224. return 1;
  225. w = im.w;
  226. h = im.h;
  227. full = (unsigned short *)malloc((size_t)w * h * 2);
  228. if (!full)
  229. return 1;
  230. memcpy(full, im.px, (size_t)w * h * 2);
  231. for (div = 2; div <= 4; div *= 2) {
  232. unsigned int ow = (w + div - 1) / div;
  233. unsigned int oh = (h + div - 1) / div;
  234. unsigned int mr = 0, mg = 0, mb = 0, mx = 0;
  235. unsigned long long count = 0;
  236. img_mem_init(&mem, arena, sizeof(arena));
  237. opt.max_pixels = 0;
  238. opt.max_w = w / div;
  239. opt.max_h = h / div;
  240. memset(&im, 0, sizeof(im));
  241. r = img_decode(filebuf, n, &opt, &mem, &im);
  242. if (r != 0 || im.w != ow || im.h != oh) {
  243. printf("FAIL %s 1/%d: %s %ux%u (want %ux%u)\n",
  244. name, div, r ? img_error_string(r) : "size",
  245. im.w, im.h, ow, oh);
  246. free(full);
  247. return 1;
  248. }
  249. for (y = 0; y < (int)oh; y++) {
  250. for (x = 0; x < (int)ow; x++) {
  251. unsigned int sum[3] = {0, 0, 0};
  252. unsigned int cnt = 0;
  253. unsigned short p = im.px[y * im.w + x];
  254. unsigned int sr = (p >> 11) & 31;
  255. unsigned int sg = (p >> 5) & 63;
  256. unsigned int sb = p & 31;
  257. int dx, dy;
  258. for (dy = 0; dy < div; dy++)
  259. for (dx = 0; dx < div; dx++) {
  260. unsigned int xx = x * div + dx;
  261. unsigned int yy = y * div + dy;
  262. unsigned short q;
  263. if (xx >= w || yy >= h)
  264. continue;
  265. q = full[yy * w + xx];
  266. sum[0] += (q >> 11) & 31;
  267. sum[1] += (q >> 5) & 63;
  268. sum[2] += q & 31;
  269. cnt++;
  270. }
  271. {
  272. unsigned int dr = (unsigned int)abs((int)sr - (int)(sum[0] / cnt));
  273. unsigned int dg = (unsigned int)abs((int)sg - (int)(sum[1] / cnt));
  274. unsigned int db = (unsigned int)abs((int)sb - (int)(sum[2] / cnt));
  275. mr += dr; mg += dg; mb += db; count++;
  276. if (dr > mx) mx = dr;
  277. if (dg > mx) mx = dg;
  278. if (db > mx) mx = db;
  279. }
  280. }
  281. }
  282. if (count) {
  283. unsigned int mean = (mr + mg + mb) / (unsigned int)(count * 3);
  284. if (mean > 3 || mx > 20) {
  285. printf("FAIL %s 1/%d: mean %u max %u\n",
  286. name, div, mean, mx);
  287. free(full);
  288. return 1;
  289. }
  290. }
  291. }
  292. free(full);
  293. return 0;
  294. }
  295. static void test_truncated(const char *name)
  296. {
  297. char path[256];
  298. unsigned int n, step, len;
  299. struct img_mem mem;
  300. struct img_options opt = {0};
  301. struct img_image im;
  302. unsigned int i;
  303. snprintf(path, sizeof(path), "testdata/images/%s", name);
  304. if (read_file(path, filebuf, sizeof(filebuf), &n))
  305. return;
  306. step = n / 64;
  307. if (!step)
  308. step = 1;
  309. for (len = 0; len < n; len += step) {
  310. img_mem_init(&mem, arena, sizeof(arena));
  311. opt.max_pixels = 0;
  312. memset(&im, 0, sizeof(im));
  313. (void)img_decode(filebuf, len, &opt, &mem, &im);
  314. }
  315. /* Deterministic mutations. */
  316. for (i = 0; i < 400; i++) {
  317. unsigned int seed = i * 1103515245u + 12345u;
  318. unsigned int pos = seed % n;
  319. unsigned char old = filebuf[pos];
  320. filebuf[pos] ^= (unsigned char)(1u << (seed >> 16 & 7));
  321. img_mem_init(&mem, arena, sizeof(arena));
  322. memset(&im, 0, sizeof(im));
  323. (void)img_decode(filebuf, n, &opt, &mem, &im);
  324. filebuf[pos] = old;
  325. }
  326. }
  327. static void test_inflate(void)
  328. {
  329. static unsigned char raw[65536];
  330. static unsigned char out[65536];
  331. unsigned int n, rn, olen;
  332. unsigned int ws[sizeof(struct inflate) / 4 + 8] __attribute__((aligned(16)));
  333. if (read_file("testdata/images/inflate.raw", raw, sizeof(raw), &rn)) {
  334. printf("FAIL inflate: raw read\n");
  335. failures++;
  336. return;
  337. }
  338. if (read_file("testdata/images/inflate.z", filebuf, sizeof(filebuf), &n) ||
  339. inflate_zlib_mem(filebuf, n, out, sizeof(out), &olen, ws) ||
  340. olen != rn || memcmp(out, raw, rn) != 0) {
  341. printf("FAIL inflate: zlib\n");
  342. failures++;
  343. }
  344. if (read_file("testdata/images/inflate.gz", filebuf, sizeof(filebuf), &n) ||
  345. inflate_gzip_mem(filebuf, n, out, sizeof(out), &olen, ws) ||
  346. olen != rn || memcmp(out, raw, rn) != 0) {
  347. printf("FAIL inflate: gzip\n");
  348. failures++;
  349. }
  350. }
  351. static void test_blit(void)
  352. {
  353. struct img_image im;
  354. unsigned short px[4] = { 0xF800, 0x07E0, 0x001F, 0xFFFF };
  355. unsigned char alpha[4] = { 255, 128, 255, 0 };
  356. unsigned short dst[16 * 16];
  357. struct img_blit b;
  358. int i;
  359. memset(&im, 0, sizeof(im));
  360. im.w = 2;
  361. im.h = 2;
  362. im.px = px;
  363. im.alpha = alpha;
  364. im.format = IMG_FMT_PNG;
  365. for (i = 0; i < 256; i++)
  366. dst[i] = 0x1234;
  367. memset(&b, 0, sizeof(b));
  368. b.dst = dst;
  369. b.dst_w = 16;
  370. b.dst_h = 16;
  371. b.clip_x = 0;
  372. b.clip_y = 0;
  373. b.clip_w = 16;
  374. b.clip_h = 16;
  375. b.x = 4;
  376. b.y = 4;
  377. b.scale = 2u << 16; /* 2x2 -> 4x4 */
  378. b.flags = IMG_BLIT_CHECKER;
  379. img_draw(&im, &b);
  380. img_draw(&im, &(struct img_blit){
  381. .dst = dst, .dst_w = 16, .dst_h = 16,
  382. .clip_x = 0, .clip_y = 0, .clip_w = 16, .clip_h = 16,
  383. .x = -1, .y = -1, .scale = 1 << 16,
  384. .flags = IMG_BLIT_FAST | IMG_BLIT_OVER_DST
  385. });
  386. }
  387. static int test_gif_anim(void)
  388. {
  389. unsigned int n, frames = 0, delay = 0;
  390. int ha = 0;
  391. struct img_mem mem;
  392. struct img_image im;
  393. int r;
  394. if (read_file("testdata/images/anim.gif", filebuf, sizeof(filebuf), &n))
  395. return 1;
  396. if (img_gif_frames(filebuf, n, &frames, &delay, &ha) != 0 || frames < 2) {
  397. printf("FAIL anim.gif: frame count %u\n", frames);
  398. return 1;
  399. }
  400. img_mem_init(&mem, arena, sizeof(arena));
  401. memset(&im, 0, sizeof(im));
  402. r = img_decode_gif_frame(filebuf, n, 0, &mem, &im, &delay);
  403. if (r == 0)
  404. r = img_decode_gif_frame(filebuf, n, 1, &mem, &im, &delay);
  405. if (r != 0) {
  406. printf("FAIL anim.gif: frame decode %d\n", r);
  407. return 1;
  408. }
  409. return 0;
  410. }
  411. static int test_unsupported(void)
  412. {
  413. unsigned int n;
  414. struct img_mem mem;
  415. struct img_options opt = {0};
  416. struct img_image im;
  417. if (read_file("testdata/images/cmyk.jpg", filebuf, sizeof(filebuf), &n))
  418. return 1;
  419. img_mem_init(&mem, arena, sizeof(arena));
  420. opt.max_pixels = 0;
  421. memset(&im, 0, sizeof(im));
  422. if (img_decode(filebuf, n, &opt, &mem, &im) != IMG_ERR_UNSUPPORTED) {
  423. printf("FAIL cmyk.jpg: not rejected\n");
  424. return 1;
  425. }
  426. return 0;
  427. }
  428. int main(void)
  429. {
  430. unsigned int i;
  431. test_blit();
  432. test_inflate();
  433. if (test_unsupported())
  434. failures++;
  435. if (test_gif_anim())
  436. failures++;
  437. for (i = 0; i < IMG_EXP_COUNT; i++) {
  438. if (test_lossless(&img_exp[i]))
  439. failures++;
  440. test_truncated(img_exp[i].path);
  441. }
  442. for (i = 0; i < IMG_JPEG_COUNT; i++) {
  443. if (test_jpeg(img_jpeg[i]))
  444. failures++;
  445. test_truncated(img_jpeg[i]);
  446. }
  447. if (test_jpeg_scaled("j420.jpg"))
  448. failures++;
  449. if (test_jpeg_scaled("j422.jpg"))
  450. failures++;
  451. if (test_jpeg_scaled("j444.jpg"))
  452. failures++;
  453. if (test_jpeg_scaled("jgray.jpg"))
  454. failures++;
  455. if (test_jpeg_scaled("jrst.jpg"))
  456. failures++;
  457. if (test_jpeg_scaled("prog.jpg"))
  458. failures++;
  459. if (test_jpeg_scaled("progrst.jpg"))
  460. failures++;
  461. if (test_jpeg_scaled("prog444.jpg"))
  462. failures++;
  463. if (test_jpeg_scaled("prog422.jpg"))
  464. failures++;
  465. test_truncated("anim.gif");
  466. if (failures) {
  467. printf("%d image test(s) failed\n", failures);
  468. return 1;
  469. }
  470. printf("all image tests passed\n");
  471. return 0;
  472. }