Skip to content
Myr

test_png_regress.c

This file

Path

tests/test_png_regress.c

75 lines · 1.9 KiB · C

Last changed by

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

History of this file

Take it

1#include <stdio.h>2#include <stdlib.h>34#define STBI_WINDOWS_UTF856#ifdef _WIN327#define WIN32 // what stb.h checks8#pragma comment(lib, "advapi32.lib")9#endif1011#define STB_IMAGE_IMPLEMENTATION12#include "stb_image.h"1314#define STB_DEFINE15#include "deprecated/stb.h"1617static unsigned int fnv1a_hash32(const stbi_uc *bytes, size_t len)18{19   unsigned int hash = 0x811c9dc5;20   unsigned int mul = 0x01000193;21   size_t i;2223   for (i = 0; i < len; ++i)24      hash = (hash ^ bytes[i]) * mul;2526   return hash;27}2829// The idea for this test is to leave pngsuite/ref_results.csv checked in,30// and then you can run this test after making PNG loader changes. If the31// ref results change (as per git diff), confirm that the change was32// intentional. If so, commit them as well; if not, undo.33int main()34{35   char **files;36   FILE *csv_file;37   int i;3839   files = stb_readdir_recursive("pngsuite", "*.png");40   if (!files) {41      fprintf(stderr, "pngsuite files not found!\n");42      return 1;43   }4445   // sort files by name46   qsort(files, stb_arr_len(files), sizeof(char*), stb_qsort_strcmp(0));4748   csv_file = fopen("pngsuite/ref_results.csv", "w");49   if (!csv_file) {50      fprintf(stderr, "error opening ref results for writing!\n");51      stb_readdir_free(files);52      return 1;53   }5455   fprintf(csv_file, "filename,width,height,ncomp,error,hash\n");56   for (i = 0; i < stb_arr_len(files); ++i) {57      char *filename = files[i];58      int width, height, ncomp;59      stbi_uc *pixels = stbi_load(filename, &width, &height, &ncomp, 0);60      const char *error = "";61      unsigned int hash = 0;6263      if (!pixels)64         error = stbi_failure_reason();65      else {66         hash = fnv1a_hash32(pixels, width * height * ncomp);67         stbi_image_free(pixels);68      }6970      fprintf(csv_file, "%s,%d,%d,%d,%s,0x%08x\n", filename, width, height, ncomp, error, hash);71   }7273   fclose(csv_file);74   stb_readdir_free(files);75}