img_test.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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_image im;
  151. int r, x, y;
  152. unsigned long long sum = 0, count = 0;
  153. unsigned int mx = 0;
  154. snprintf(path, sizeof(path), "testdata/images/%s", name);
  155. snprintf(refpath, sizeof(refpath), "testdata/images/ref/%s.ppm", name);
  156. if (read_file(path, filebuf, sizeof(filebuf), &n) ||
  157. read_ppm(refpath, &rw, &rh, &ref)) {
  158. printf("FAIL %s: read\n", name);
  159. return 1;
  160. }
  161. img_mem_init(&mem, arena, sizeof(arena));
  162. opt.max_pixels = 0;
  163. memset(&im, 0, sizeof(im));
  164. r = img_decode(filebuf, n, &opt, &mem, &im);
  165. if (r) {
  166. printf("FAIL %s: %s\n", name, img_error_string(r));
  167. free(ref);
  168. return 1;
  169. }
  170. if (im.w != rw || im.h != rh) {
  171. printf("FAIL %s: size mismatch\n", name);
  172. free(ref);
  173. return 1;
  174. }
  175. for (y = 0; y < (int)im.h; y++) {
  176. for (x = 0; x < (int)im.w; x++) {
  177. unsigned int px = im.px[y * im.w + x];
  178. int rr = ((px >> 11) & 31) * 255 / 31;
  179. int gg = ((px >> 5) & 63) * 255 / 63;
  180. int bb = (px & 31) * 255 / 31;
  181. unsigned char *p = ref + ((unsigned int)y * im.w + x) * 3;
  182. int dr = abs(rr - p[0]);
  183. int dg = abs(gg - p[1]);
  184. int db = abs(bb - p[2]);
  185. sum += (unsigned long long)dr + dg + db;
  186. count += 3;
  187. if ((unsigned int)dr > mx) mx = dr;
  188. if ((unsigned int)dg > mx) mx = dg;
  189. if ((unsigned int)db > mx) mx = db;
  190. }
  191. }
  192. free(ref);
  193. if (sum / count > 8 || mx > 130) {
  194. printf("FAIL %s: mean %.2f max %u\n", name,
  195. (double)sum / (double)count, mx);
  196. return 1;
  197. }
  198. return 0;
  199. }
  200. static void test_truncated(const char *name)
  201. {
  202. char path[256];
  203. unsigned int n, step, len;
  204. struct img_mem mem;
  205. struct img_options opt;
  206. struct img_image im;
  207. unsigned int i;
  208. snprintf(path, sizeof(path), "testdata/images/%s", name);
  209. if (read_file(path, filebuf, sizeof(filebuf), &n))
  210. return;
  211. step = n / 64;
  212. if (!step)
  213. step = 1;
  214. for (len = 0; len < n; len += step) {
  215. img_mem_init(&mem, arena, sizeof(arena));
  216. opt.max_pixels = 0;
  217. memset(&im, 0, sizeof(im));
  218. (void)img_decode(filebuf, len, &opt, &mem, &im);
  219. }
  220. /* Deterministic mutations. */
  221. for (i = 0; i < 400; i++) {
  222. unsigned int seed = i * 1103515245u + 12345u;
  223. unsigned int pos = seed % n;
  224. unsigned char old = filebuf[pos];
  225. filebuf[pos] ^= (unsigned char)(1u << (seed >> 16 & 7));
  226. img_mem_init(&mem, arena, sizeof(arena));
  227. memset(&im, 0, sizeof(im));
  228. (void)img_decode(filebuf, n, &opt, &mem, &im);
  229. filebuf[pos] = old;
  230. }
  231. }
  232. static void test_inflate(void)
  233. {
  234. static unsigned char raw[65536];
  235. static unsigned char out[65536];
  236. unsigned int n, rn, olen;
  237. unsigned int ws[sizeof(struct inflate) / 4 + 8] __attribute__((aligned(16)));
  238. if (read_file("testdata/images/inflate.raw", raw, sizeof(raw), &rn)) {
  239. printf("FAIL inflate: raw read\n");
  240. failures++;
  241. return;
  242. }
  243. if (read_file("testdata/images/inflate.z", filebuf, sizeof(filebuf), &n) ||
  244. inflate_zlib_mem(filebuf, n, out, sizeof(out), &olen, ws) ||
  245. olen != rn || memcmp(out, raw, rn) != 0) {
  246. printf("FAIL inflate: zlib\n");
  247. failures++;
  248. }
  249. if (read_file("testdata/images/inflate.gz", filebuf, sizeof(filebuf), &n) ||
  250. inflate_gzip_mem(filebuf, n, out, sizeof(out), &olen, ws) ||
  251. olen != rn || memcmp(out, raw, rn) != 0) {
  252. printf("FAIL inflate: gzip\n");
  253. failures++;
  254. }
  255. }
  256. static void test_blit(void)
  257. {
  258. struct img_image im;
  259. unsigned short px[4] = { 0xF800, 0x07E0, 0x001F, 0xFFFF };
  260. unsigned char alpha[4] = { 255, 128, 255, 0 };
  261. unsigned short dst[16 * 16];
  262. struct img_blit b;
  263. int i;
  264. memset(&im, 0, sizeof(im));
  265. im.w = 2;
  266. im.h = 2;
  267. im.px = px;
  268. im.alpha = alpha;
  269. im.format = IMG_FMT_PNG;
  270. for (i = 0; i < 256; i++)
  271. dst[i] = 0x1234;
  272. memset(&b, 0, sizeof(b));
  273. b.dst = dst;
  274. b.dst_w = 16;
  275. b.dst_h = 16;
  276. b.clip_x = 0;
  277. b.clip_y = 0;
  278. b.clip_w = 16;
  279. b.clip_h = 16;
  280. b.x = 4;
  281. b.y = 4;
  282. b.scale = 2u << 16; /* 2x2 -> 4x4 */
  283. b.flags = IMG_BLIT_CHECKER;
  284. img_draw(&im, &b);
  285. img_draw(&im, &(struct img_blit){
  286. .dst = dst, .dst_w = 16, .dst_h = 16,
  287. .clip_x = 0, .clip_y = 0, .clip_w = 16, .clip_h = 16,
  288. .x = -1, .y = -1, .scale = 1 << 16,
  289. .flags = IMG_BLIT_FAST | IMG_BLIT_OVER_DST
  290. });
  291. }
  292. static int test_unsupported(void)
  293. {
  294. unsigned int n;
  295. struct img_mem mem;
  296. struct img_options opt;
  297. struct img_image im;
  298. if (read_file("testdata/images/prog.jpg", filebuf, sizeof(filebuf), &n))
  299. return 1;
  300. img_mem_init(&mem, arena, sizeof(arena));
  301. opt.max_pixels = 0;
  302. memset(&im, 0, sizeof(im));
  303. if (img_decode(filebuf, n, &opt, &mem, &im) != IMG_ERR_UNSUPPORTED) {
  304. printf("FAIL prog.jpg: not rejected\n");
  305. return 1;
  306. }
  307. return 0;
  308. }
  309. int main(void)
  310. {
  311. unsigned int i;
  312. test_blit();
  313. test_inflate();
  314. if (test_unsupported())
  315. failures++;
  316. for (i = 0; i < IMG_EXP_COUNT; i++) {
  317. if (test_lossless(&img_exp[i]))
  318. failures++;
  319. test_truncated(img_exp[i].path);
  320. }
  321. for (i = 0; i < IMG_JPEG_COUNT; i++) {
  322. if (test_jpeg(img_jpeg[i]))
  323. failures++;
  324. test_truncated(img_jpeg[i]);
  325. }
  326. test_truncated("anim.gif");
  327. if (failures) {
  328. printf("%d image test(s) failed\n", failures);
  329. return 1;
  330. }
  331. printf("all image tests passed\n");
  332. return 0;
  333. }