File: test_png_regress.c

package info (click to toggle)
libstb 0.0~git20250907.fede005%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,520 kB
  • sloc: ansic: 80,787; cpp: 1,506; makefile: 114; sh: 22
file content (75 lines) | stat: -rw-r--r-- 1,923 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>

#define STBI_WINDOWS_UTF8

#ifdef _WIN32
#define WIN32 // what stb.h checks
#pragma comment(lib, "advapi32.lib")
#endif

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

#define STB_DEFINE
#include "deprecated/stb.h"

static unsigned int fnv1a_hash32(const stbi_uc *bytes, size_t len)
{
   unsigned int hash = 0x811c9dc5;
   unsigned int mul = 0x01000193;
   size_t i;

   for (i = 0; i < len; ++i)
      hash = (hash ^ bytes[i]) * mul;

   return hash;
}

// The idea for this test is to leave pngsuite/ref_results.csv checked in,
// and then you can run this test after making PNG loader changes. If the
// ref results change (as per git diff), confirm that the change was
// intentional. If so, commit them as well; if not, undo.
int main()
{
   char **files;
   FILE *csv_file;
   int i;

   files = stb_readdir_recursive("pngsuite", "*.png");
   if (!files) {
      fprintf(stderr, "pngsuite files not found!\n");
      return 1;
   }

   // sort files by name
   qsort(files, stb_arr_len(files), sizeof(char*), stb_qsort_strcmp(0));

   csv_file = fopen("pngsuite/ref_results.csv", "w");
   if (!csv_file) {
      fprintf(stderr, "error opening ref results for writing!\n");
      stb_readdir_free(files);
      return 1;
   }

   fprintf(csv_file, "filename,width,height,ncomp,error,hash\n");
   for (i = 0; i < stb_arr_len(files); ++i) {
      char *filename = files[i];
      int width, height, ncomp;
      stbi_uc *pixels = stbi_load(filename, &width, &height, &ncomp, 0);
      const char *error = "";
      unsigned int hash = 0;

      if (!pixels)
         error = stbi_failure_reason();
      else {
         hash = fnv1a_hash32(pixels, width * height * ncomp);
         stbi_image_free(pixels);
      }

      fprintf(csv_file, "%s,%d,%d,%d,%s,0x%08x\n", filename, width, height, ncomp, error, hash);
   }

   fclose(csv_file);
   stb_readdir_free(files);
}