test_png_regress.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define STBI_WINDOWS_UTF8
  4. #ifdef _WIN32
  5. #define WIN32 // what stb.h checks
  6. #pragma comment(lib, "advapi32.lib")
  7. #endif
  8. #define STB_IMAGE_IMPLEMENTATION
  9. #include "stb_image.h"
  10. #define STB_DEFINE
  11. #include "deprecated/stb.h"
  12. static unsigned int fnv1a_hash32(const stbi_uc *bytes, size_t len)
  13. {
  14. unsigned int hash = 0x811c9dc5;
  15. unsigned int mul = 0x01000193;
  16. size_t i;
  17. for (i = 0; i < len; ++i)
  18. hash = (hash ^ bytes[i]) * mul;
  19. return hash;
  20. }
  21. // The idea for this test is to leave pngsuite/ref_results.csv checked in,
  22. // and then you can run this test after making PNG loader changes. If the
  23. // ref results change (as per git diff), confirm that the change was
  24. // intentional. If so, commit them as well; if not, undo.
  25. int main()
  26. {
  27. char **files;
  28. FILE *csv_file;
  29. int i;
  30. files = stb_readdir_recursive("pngsuite", "*.png");
  31. if (!files) {
  32. fprintf(stderr, "pngsuite files not found!\n");
  33. return 1;
  34. }
  35. // sort files by name
  36. qsort(files, stb_arr_len(files), sizeof(char*), stb_qsort_strcmp(0));
  37. csv_file = fopen("pngsuite/ref_results.csv", "w");
  38. if (!csv_file) {
  39. fprintf(stderr, "error opening ref results for writing!\n");
  40. stb_readdir_free(files);
  41. return 1;
  42. }
  43. fprintf(csv_file, "filename,width,height,ncomp,error,hash\n");
  44. for (i = 0; i < stb_arr_len(files); ++i) {
  45. char *filename = files[i];
  46. int width, height, ncomp;
  47. stbi_uc *pixels = stbi_load(filename, &width, &height, &ncomp, 0);
  48. const char *error = "";
  49. unsigned int hash = 0;
  50. if (!pixels)
  51. error = stbi_failure_reason();
  52. else {
  53. hash = fnv1a_hash32(pixels, width * height * ncomp);
  54. stbi_image_free(pixels);
  55. }
  56. fprintf(csv_file, "%s,%d,%d,%d,%s,0x%08x\n", filename, width, height, ncomp, error, hash);
  57. }
  58. fclose(csv_file);
  59. stb_readdir_free(files);
  60. }