img_test.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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;
  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;
  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 void test_truncated(const char *name)
  210. {
  211. char path[256];
  212. unsigned int n, step, len;
  213. struct img_mem mem;
  214. struct img_options opt;
  215. struct img_image im;
  216. unsigned int i;
  217. snprintf(path, sizeof(path), "testdata/images/%s", name);
  218. if (read_file(path, filebuf, sizeof(filebuf), &n))
  219. return;
  220. step = n / 64;
  221. if (!step)
  222. step = 1;
  223. for (len = 0; len < n; len += step) {
  224. img_mem_init(&mem, arena, sizeof(arena));
  225. opt.max_pixels = 0;
  226. memset(&im, 0, sizeof(im));
  227. (void)img_decode(filebuf, len, &opt, &mem, &im);
  228. }
  229. /* Deterministic mutations. */
  230. for (i = 0; i < 400; i++) {
  231. unsigned int seed = i * 1103515245u + 12345u;
  232. unsigned int pos = seed % n;
  233. unsigned char old = filebuf[pos];
  234. filebuf[pos] ^= (unsigned char)(1u << (seed >> 16 & 7));
  235. img_mem_init(&mem, arena, sizeof(arena));
  236. memset(&im, 0, sizeof(im));
  237. (void)img_decode(filebuf, n, &opt, &mem, &im);
  238. filebuf[pos] = old;
  239. }
  240. }
  241. static void test_inflate(void)
  242. {
  243. static unsigned char raw[65536];
  244. static unsigned char out[65536];
  245. unsigned int n, rn, olen;
  246. unsigned int ws[sizeof(struct inflate) / 4 + 8] __attribute__((aligned(16)));
  247. if (read_file("testdata/images/inflate.raw", raw, sizeof(raw), &rn)) {
  248. printf("FAIL inflate: raw read\n");
  249. failures++;
  250. return;
  251. }
  252. if (read_file("testdata/images/inflate.z", filebuf, sizeof(filebuf), &n) ||
  253. inflate_zlib_mem(filebuf, n, out, sizeof(out), &olen, ws) ||
  254. olen != rn || memcmp(out, raw, rn) != 0) {
  255. printf("FAIL inflate: zlib\n");
  256. failures++;
  257. }
  258. if (read_file("testdata/images/inflate.gz", filebuf, sizeof(filebuf), &n) ||
  259. inflate_gzip_mem(filebuf, n, out, sizeof(out), &olen, ws) ||
  260. olen != rn || memcmp(out, raw, rn) != 0) {
  261. printf("FAIL inflate: gzip\n");
  262. failures++;
  263. }
  264. }
  265. static void test_blit(void)
  266. {
  267. struct img_image im;
  268. unsigned short px[4] = { 0xF800, 0x07E0, 0x001F, 0xFFFF };
  269. unsigned char alpha[4] = { 255, 128, 255, 0 };
  270. unsigned short dst[16 * 16];
  271. struct img_blit b;
  272. int i;
  273. memset(&im, 0, sizeof(im));
  274. im.w = 2;
  275. im.h = 2;
  276. im.px = px;
  277. im.alpha = alpha;
  278. im.format = IMG_FMT_PNG;
  279. for (i = 0; i < 256; i++)
  280. dst[i] = 0x1234;
  281. memset(&b, 0, sizeof(b));
  282. b.dst = dst;
  283. b.dst_w = 16;
  284. b.dst_h = 16;
  285. b.clip_x = 0;
  286. b.clip_y = 0;
  287. b.clip_w = 16;
  288. b.clip_h = 16;
  289. b.x = 4;
  290. b.y = 4;
  291. b.scale = 2u << 16; /* 2x2 -> 4x4 */
  292. b.flags = IMG_BLIT_CHECKER;
  293. img_draw(&im, &b);
  294. img_draw(&im, &(struct img_blit){
  295. .dst = dst, .dst_w = 16, .dst_h = 16,
  296. .clip_x = 0, .clip_y = 0, .clip_w = 16, .clip_h = 16,
  297. .x = -1, .y = -1, .scale = 1 << 16,
  298. .flags = IMG_BLIT_FAST | IMG_BLIT_OVER_DST
  299. });
  300. }
  301. static int test_gif_anim(void)
  302. {
  303. unsigned int n, frames = 0, delay = 0;
  304. int ha = 0;
  305. struct img_mem mem;
  306. struct img_image im;
  307. int r;
  308. if (read_file("testdata/images/anim.gif", filebuf, sizeof(filebuf), &n))
  309. return 1;
  310. if (img_gif_frames(filebuf, n, &frames, &delay, &ha) != 0 || frames < 2) {
  311. printf("FAIL anim.gif: frame count %u\n", frames);
  312. return 1;
  313. }
  314. img_mem_init(&mem, arena, sizeof(arena));
  315. memset(&im, 0, sizeof(im));
  316. r = img_decode_gif_frame(filebuf, n, 0, &mem, &im, &delay);
  317. if (r == 0)
  318. r = img_decode_gif_frame(filebuf, n, 1, &mem, &im, &delay);
  319. if (r != 0) {
  320. printf("FAIL anim.gif: frame decode %d\n", r);
  321. return 1;
  322. }
  323. return 0;
  324. }
  325. static int test_unsupported(void)
  326. {
  327. unsigned int n;
  328. struct img_mem mem;
  329. struct img_options opt;
  330. struct img_image im;
  331. if (read_file("testdata/images/cmyk.jpg", filebuf, sizeof(filebuf), &n))
  332. return 1;
  333. img_mem_init(&mem, arena, sizeof(arena));
  334. opt.max_pixels = 0;
  335. memset(&im, 0, sizeof(im));
  336. if (img_decode(filebuf, n, &opt, &mem, &im) != IMG_ERR_UNSUPPORTED) {
  337. printf("FAIL cmyk.jpg: not rejected\n");
  338. return 1;
  339. }
  340. return 0;
  341. }
  342. int main(void)
  343. {
  344. unsigned int i;
  345. test_blit();
  346. test_inflate();
  347. if (test_unsupported())
  348. failures++;
  349. if (test_gif_anim())
  350. failures++;
  351. for (i = 0; i < IMG_EXP_COUNT; i++) {
  352. if (test_lossless(&img_exp[i]))
  353. failures++;
  354. test_truncated(img_exp[i].path);
  355. }
  356. for (i = 0; i < IMG_JPEG_COUNT; i++) {
  357. if (test_jpeg(img_jpeg[i]))
  358. failures++;
  359. test_truncated(img_jpeg[i]);
  360. }
  361. test_truncated("anim.gif");
  362. if (failures) {
  363. printf("%d image test(s) failed\n", failures);
  364. return 1;
  365. }
  366. printf("all image tests passed\n");
  367. return 0;
  368. }