1 /*
2 A test suite for dsmooth, compares dsmooth with dsmooth2
3
4 Jon Barron, 2007
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <dirent.h>
11 #include <regex.h>
12 #include <cairo.h>
13 #include <math.h>
14
15 #include "cairoutils.h"
16 #include "dimage.h"
17
18 #define PEAKDIST 4
19
is_png(const struct dirent * de)20 int is_png(const struct dirent *de) {
21 regex_t preq;
22 regcomp(&preq, "[^ ]*[.](png|PNG)$", REG_EXTENDED);
23 return !regexec(&preq, de->d_name, (size_t) 0, NULL, 0);
24 }
25
is_jpeg(const struct dirent * de)26 int is_jpeg(const struct dirent *de) {
27 regex_t preq;
28 regcomp(&preq, "[^ ]*[.](jpg|JPG|jpeg|JPEG)$", REG_EXTENDED);
29 return !regexec(&preq, de->d_name, (size_t) 0, NULL, 0);
30 }
31
is_image(const struct dirent * de)32 int is_image(const struct dirent *de) {
33 return is_jpeg(de) || is_png(de);
34 }
35
is_output(const struct dirent * de)36 int is_output(const struct dirent *de) {
37 regex_t preq;
38 regcomp(&preq, "out_[^ ]*", REG_EXTENDED);
39 return !regexec(&preq, de->d_name, (size_t) 0, NULL, 0);
40 }
41
is_input_image(const struct dirent * de)42 int is_input_image(const struct dirent *de) {
43 return is_image(de) && !is_output(de);
44 }
45
to_bw_f(unsigned char * image,int imW,int imH)46 float* to_bw_f(unsigned char *image, int imW, int imH) {
47 int w, h, c;
48 float *image_bw;
49 float v;
50
51 image_bw = malloc(sizeof(float) * imW * imH);
52 for (w = 0; w < imW; w++) {
53 for (h = 0; h < imH; h++) {
54 v = 0.0;
55 for (c = 0; c <= 2; c++) {
56 v = v + ((float)(image[4*(w*imH+h) + c])) / 3.0;
57 }
58 image_bw[w*imH + h] = v;
59 }
60 }
61
62 return image_bw;
63 }
64
to_bw_u8(unsigned char * image,int imW,int imH)65 unsigned char* to_bw_u8(unsigned char *image, int imW, int imH) {
66 int i;
67 unsigned char *image_bw, *p;
68 image_bw = p = malloc(sizeof(unsigned char) * imW * imH);
69 for (i = 0; i < imW*imH; i++, image += 4, p++) {
70 int total = image[0] + image[1] + image[2];
71 *p = total / 3;
72 }
73 return image_bw;
74 }
75
to_cairo_bw(float * image_bw,int imW,int imH)76 unsigned char* to_cairo_bw(float *image_bw, int imW, int imH) {
77 int w, h, c;
78 unsigned char* image_cairo;
79
80 image_cairo = malloc(sizeof(unsigned char) * imW * imH * 4);
81 for (w = 0; w < imW; w++) {
82 for (h = 0; h < imH; h++) {
83 for (c = 0; c <= 2; c++) {
84 image_cairo[4*(w*imH + h) + c] = (unsigned char)(image_bw[w*imH + h]);
85 }
86 image_cairo[4*(w*imH + h) + 3] = 255;
87 }
88 }
89
90 return image_cairo;
91 }
92
main(void)93 int main(void) {
94 struct dirent **namelist;
95 int i, n, N;
96 unsigned char *image = NULL;
97 float *image_bw_f;
98 unsigned char *image_bw_u8;
99 float *image_out_old;
100 float *image_out_new;
101 char fullpath[255];
102 char outpath_old[255];
103 char outpath_new[255];
104 int imW, imH;
105
106 float sigma;
107 float err;
108
109 sigma = 1.0;
110
111 N = scandir("demo_simplexy_images", &namelist, is_input_image, alphasort);
112 if (N < 0) {
113 perror("scandir");
114 return 1;
115 }
116
117 for (n = 0; n < N; n++) {
118
119 strcpy(fullpath, "demo_simplexy_images/");
120 strcat(fullpath, namelist[n]->d_name);
121
122 strcpy(outpath_old, "demo_simplexy_images/out_");
123 strcat(outpath_old, namelist[n]->d_name);
124 outpath_old[strlen(outpath_old)-4] = '\0';
125 strcat(outpath_old, "_old");
126 strcat(outpath_old, ".png");
127
128 strcpy(outpath_new, "demo_simplexy_images/out_");
129 strcat(outpath_new, namelist[n]->d_name);
130 outpath_new[strlen(outpath_new)-4] = '\0';
131 strcat(outpath_new, "_new");
132 strcat(outpath_new, ".png");
133
134 fprintf(stderr,"demo_dsmooth: loading %s ", fullpath);
135
136 if (is_png(namelist[n])) {
137 fprintf(stderr, "as a PNG\n");
138 image = cairoutils_read_png(fullpath, &imW, &imH);
139 }
140
141 if (is_jpeg(namelist[n])) {
142 fprintf(stderr, "as a JPEG\n");
143 image = cairoutils_read_jpeg(fullpath, &imW, &imH);
144 }
145
146 image_bw_u8 = to_bw_u8(image, imW, imH);
147 image_bw_f = malloc(sizeof(float) * imW * imH);
148 for (i = 0; i < imW*imH; i++) {
149 image_bw_f[i] = (float)image_bw_u8[i];
150 }
151
152 image_out_old = malloc(sizeof(float)*imW*imH);
153 image_out_new = malloc(sizeof(float)*imW*imH);
154
155 fprintf(stderr,"demo_dsmooth: running %s through dsmooth\n", fullpath);
156 dsmooth(image_bw_f, imW, imH, sigma, image_out_old);
157
158 fprintf(stderr,"demo_dsmooth: running %s through dsmooth2\n", fullpath);
159 dsmooth2(image_bw_f, imW, imH, sigma, image_out_new);
160
161 err = 0.0;
162 for (i = 0; i < imW*imH; i++) {
163 err += fabs(image_out_old[i]-image_out_new[i]);
164 }
165 err = err / (imW*imH);
166
167 fprintf(stderr, "demo_dsmooth: error between smooths: %f per pixel\n", err);
168
169 // fprintf(stderr, "demo_dsmooth: writing old dsmoothed image to %s\n", outpath_old);
170 // cairoutils_write_png(outpath_old, to_cairo_bw(image_out_old, imW, imH), imW, imH);
171
172 // fprintf(stderr, "demo_dsmooth: writing new dsmoothed image to %s\n", outpath_new);
173 // cairoutils_write_png(outpath_new, to_cairo_bw(image_out_new, imW, imH), imW, imH);
174
175 free(namelist[n]);
176 free(image);
177 free(image_bw_f);
178 free(image_bw_u8);
179 free(image_out_old);
180 free(image_out_new);
181
182 }
183 free(namelist);
184
185 return 0;
186 }
187
188
189