Debian Codegraph @

converter/other/pamtosvg/image-proc.c

raw
1 /* image-proc.c: image processing routines */
2
3 #include <assert.h>
4 #include <math.h>
5 #include <string.h>
6
7 #include "mallocvar.h"
8
9 #include "message.h"
10
11 #include "image-proc.h"
12
13 #define BLACK 0
14 #define WHITE 0xff
15 #ifndef M_SQRT2
16 #define M_SQRT2 1.41421356237
17 #endif
18
19 #if 0
20 struct etyp { int t00, t11, t01, t01s; };
21
22
23 static bool get_edge(bitmap_type, int y, int x, struct etyp *t);
24 static void check(int v1, int v2, int v3, struct etyp *t);
25 #endif
26
27
28 /* Allocate storage for a new distance map with the same dimensions
29 as BITMAP and initialize it so that pixels in BITMAP with value
30 TARGET_VALUE are at distance zero and all other pixels are at
31 distance infinity. Then compute the gray-weighted distance from
32 every non-target point to the nearest target point. */
33
34 distance_map_type
35 new_distance_map(bitmap_type bitmap, unsigned char target_value, bool padded, at_exception_type * exp)
36 {
37 signed x, y;
38 float d, min;
39 distance_map_type dist;
40 unsigned char *b = bitmap.bitmap;
41 unsigned w = bitmap.width;
42 unsigned h = bitmap.height;
43 unsigned spp = bitmap.np;
44
45 dist.height = h; dist.width = w;
46 MALLOCARRAY(dist.d, h);
47 MALLOCARRAY(dist.weight, h);
48 if (dist.d == NULL || dist.weight == NULL)
49 pm_error("Unable to get memory for distance map");
50 for (y = 0; y < (signed) h; y++)
51 {
52 MALLOCARRAY(dist.d[y], w);
53 if (dist.d[y] == NULL)
54 pm_error("Unable to get memory for distance map");
55 memset(dist.d[y], 0, w * sizeof(float));
56
57 MALLOCARRAY(dist.weight[y], w);
58 if (dist.weight[y] == NULL)
59 pm_error("Unable to get memory for distance map");
60 }
61
62 if (spp == 3)
63 {
64 for (y = 0; y < (signed) h; y++)
65 {
66 for (x = 0; x < (signed) w; x++, b += spp)
67 {
68 int gray; float fgray;
69 gray = (int)LUMINANCE(b[0], b[1], b[2]);
70 dist.d[y][x] = (gray == target_value ? 0.0F : 1.0e10F);
71 fgray = gray * 0.0039215686F; /* = gray / 255.0F */
72 dist.weight[y][x] = 1.0F - fgray;
73 /* dist.weight[y][x] = 1.0F - (fgray * fgray);*/
74 /* dist.weight[y][x] = (fgray < 0.5F ? 1.0F - fgray : -2.0F * fgray * (fgray - 1.0F));*/
75 }
76 }
77 }
78 else
79 {
80 for (y = 0; y < (signed) h; y++)
81 {
82 for (x = 0; x < (signed) w; x++, b += spp)
83 {
84 int gray; float fgray;
85 gray = b[0];
86 dist.d[y][x] = (gray == target_value ? 0.0F : 1.0e10F);
87 fgray = gray * 0.0039215686F; /* = gray / 255.0F */
88 dist.weight[y][x] = 1.0F - fgray;
89 /* dist.weight[y][x] = 1.0F - (fgray * fgray);*/
90 /* dist.weight[y][x] = (fgray < 0.5F ? 1.0F - fgray : -2.0F * fgray * (fgray - 1.0F)); */
91 }
92 }
93 }
94
95 /* If the image is padded then border points are all at most
96 one unit away from the nearest target point. */
97 if (padded)
98 {
99 for (y = 0; y < (signed) h; y++)
100 {
101 if (dist.d[y][0] > dist.weight[y][0])
102 dist.d[y][0] = dist.weight[y][0];
103 if (dist.d[y][w - 1] > dist.weight[y][w - 1])
104 dist.d[y][w - 1] = dist.weight[y][w - 1];
105 }
106 for (x = 0; x < (signed) w; x++)
107 {
108 if (dist.d[0][x] > dist.weight[0][x])
109 dist.d[0][x] = dist.weight[0][x];
110 if (dist.d[h - 1][x] > dist.weight[h - 1][x])
111 dist.d[h - 1][x] = dist.weight[h - 1][x];
112 }
113 }
114
115 /* Scan the image from left to right, top to bottom.
116 Examine the already-visited neighbors of each point (those
117 situated above or to the left of it). Each neighbor knows
118 the distance to its nearest target point; add to this distance
119 the distance from the central point to the neighbor (either
120 sqrt(2) or one) multiplied by the central point's weight
121 (derived from its gray level). Replace the distance already
122 stored at the central point if the new distance is smaller. */
123 for (y = 1; y < (signed) h; y++)
124 {
125 for (x = 1; x < (signed) w; x++)
126 {
127 if (dist.d[y][x] == 0.0F) continue;
128
129 min = dist.d[y][x];
130
131 /* upper-left neighbor */
132 d = dist.d[y - 1][x - 1] + (float) M_SQRT2 * dist.weight[y][x];
133 if (d < min) min = dist.d[y][x] = d;
134
135 /* upper neighbor */
136 d = dist.d[y - 1][x] + dist.weight[y][x];
137 if (d < min) min = dist.d[y][x] = d;
138
139 /* left neighbor */
140 d = dist.d[y][x - 1] + dist.weight[y][x];
141 if (d < min) min = dist.d[y][x] = d;
142
143 /* upper-right neighbor (except at the last column) */
144 if (x + 1 < (signed) w)
145 {
146 d = dist.d[y - 1][x + 1] + (float) M_SQRT2 * dist.weight[y][x];
147 if (d < min) min = dist.d[y][x] = d;
148 }
149 }
150 }
151
152 /* Same as above, but now scanning right to left, bottom to top. */
153 for (y = h - 2; y >= 0; y--)
154 {
155 for (x = w - 2; x >= 0; x--)
156 {
157 min = dist.d[y][x];
158
159 /* lower-right neighbor */
160 d = dist.d[y + 1][x + 1] + (float) M_SQRT2 * dist.weight[y][x];
161 if (d < min) min = dist.d[y][x] = d;
162
163 /* lower neighbor */
164 d = dist.d[y + 1][x] + dist.weight[y][x];
165 if (d < min) min = dist.d[y][x] = d;
166
167 /* right neighbor */
168 d = dist.d[y][x + 1] + dist.weight[y][x];
169 if (d < min) min = dist.d[y][x] = d;
170
171 /* lower-left neighbor (except at the first column) */
172 if (x - 1 >= 0)
173 {
174 d = dist.d[y + 1][x - 1] + (float) M_SQRT2 * dist.weight[y][x];
175 if (d < min) min = dist.d[y][x] = d;
176 }
177 }
178 }
179 return dist;
180 }
181
182
183
184 /* Free the dynamically-allocated storage associated with a distance map. */
185
186 void
187 free_distance_map(distance_map_type *dist)
188 {
189 unsigned y, h;
190
191 if (!dist) return;
192
193 h = dist->height;
194
195 if (dist->d != NULL)
196 {
197 for (y = 0; y < h; y++)
198 free(dist->d[y]);
199 free(dist->d);
200 }
201 if (dist->weight != NULL)
202 {
203 for (y = 0; y < h; y++)
204 free(dist->weight[y]);
205 free(dist->weight);
206 }
207 }
208
209
210
211 #if 0
212 void
213 medial_axis(bitmap_type *bitmap, distance_map_type *dist,
214 bool bgSpec, pixel bg_color)
215 {
216 unsigned x, y, test;
217 unsigned w, h;
218 unsigned char *b;
219 float **d, f;
220 pixel bg;
221
222 assert(bitmap != NULL);
223
224 assert(BITMAP_PLANES(*bitmap) == 1);
225
226 b = BITMAP_BITS(*bitmap);
227 assert(b != NULL);
228 assert(dist != NULL);
229 d = dist->d;
230 assert(d != NULL);
231
232 h = BITMAP_HEIGHT(*dist);
233 w = BITMAP_WIDTH(*dist);
234 assert(BITMAP_WIDTH(*bitmap) == w && BITMAP_HEIGHT(*bitmap) == h);
235
236 if (bgSpec)
237 bg = bg_color;
238 else
239 PPM_ASSIGN(bg, 255, 255, 255);
240
241 f = d[0][0] + 0.5;
242 test = (f < d[1][0]) + (f < d[1][1]) + (f < d[0][1]);
243 if (test > 1) b[0] = PPM_GETR(bg);
244
245 f = d[0][w-1] + 0.5;
246 test = (f < d[1][w-1]) + (f < d[1][w-2]) + (f < d[0][w-2]);
247 if (test > 1) b[w-1] = PPM_GETR(bg);
248
249 for (x = 1; x < w - 1; x++)
250 {
251 f = d[0][x] + 0.5;
252 test = (f < d[0][x-1]) + (f < d[0][x+1]) + (f < d[1][x-1])
253 + (f < d[1][x]) + (f < d[1][x+1]);
254 if (test > 1) b[x] = PPM_GETR(bg);
255 }
256 b += w;
257
258 for (y = 1; y < h - 1; y++)
259 {
260 f = d[y][0] + 0.5;
261 test = (f < d[y-1][0]) + (f < d[y-1][1]) + (f < d[y][1])
262 + (f < d[y+1][0]) + (f < d[y+1][1]);
263 if (test > 1) b[0] = PPM_GETR(bg);
264
265 for (x = 1; x < w - 1; x++)
266 {
267 f = d[y][x] + 0.5;
268 test = (f < d[y-1][x-1]) + (f < d[y-1][x]) + (f < d[y-1][x+1])
269 + (f < d[y][x-1]) + (f < d[y][x+1])
270 + (f < d[y+1][x-1]) + (f < d[y+1][x]) + (f < d[y+1][x+1]);
271 if (test > 1) b[x] = PPM_GETR(bg)
272 }
273
274 f = d[y][w-1] + 0.5;
275 test = (f < d[y-1][w-1]) + (f < d[y-1][w-2]) + (f < d[y][w-2])
276 + (f < d[y+1][w-1]) + (f < d[y+1][w-2]);
277 if (test > 1)
278 b[w-1] = PPM_GETR(bg);
279
280 b += w;
281 }
282
283 for (x = 1; x < w - 1; x++)
284 {
285 f = d[h-1][x] + 0.5;
286 test = (f < d[h-1][x-1]) + (f < d[h-1][x+1])
287 + (f < d[h-2][x-1]) + (f < d[h-2][x]) + (f < d[h-2][x+1]);
288 if (test > 1) b[x] = PPM_GETR(bg);
289 }
290
291 f = d[h-1][0] + 0.5;
292 test = (f < d[h-2][0]) + (f < d[h-2][1]) + (f < d[h-1][1]);
293 if (test > 1) b[0] = PPM_GETR(bg);
294
295 f = d[h-1][w-1] + 0.5;
296 test = (f < d[h-2][w-1]) + (f < d[h-2][w-2]) + (f < d[h-1][w-2]);
297 if (test > 1) b[w-1] = PPM_GETR(bg);
298 }
299 #endif
300
301
302 /* Binarize a grayscale or color image. */
303
304 void
305 binarize(bitmap_type *bitmap)
306 {
307 unsigned i, npixels, spp;
308 unsigned char *b;
309
310 assert(bitmap != NULL);
311 assert(bitmap->bitmap != NULL);
312
313 b = bitmap->bitmap;
314 spp = bitmap->np;
315 npixels = bitmap->width * bitmap->height;
316
317 if (spp == 1)
318 {
319 for (i = 0; i < npixels; i++)
320 b[i] = (b[i] > GRAY_THRESHOLD ? WHITE : BLACK);
321 }
322 else if (spp == 3)
323 {
324 unsigned char *rgb = b;
325 for (i = 0; i < npixels; i++, rgb += 3)
326 {
327 b[i] = (LUMINANCE(rgb[0], rgb[1], rgb[2]) > GRAY_THRESHOLD
328 ? WHITE : BLACK);
329 }
330 REALLOCARRAY_NOFAIL(bitmap->bitmap, npixels);
331 bitmap->np = 1;
332 }
333 else
334 {
335 WARNING1("binarize: don't know how to interpret %u-plane images", spp);
336 }
337 }
338
339
340
341 #if 0
342 /* Thin a binary image, replacing the original image with the thinned one. */
343
344 bitmap_type
345 ip_thin(bitmap_type input_b)
346 {
347 unsigned y, x, i;
348 bool k, again;
349 struct etyp t;
350 unsigned w = BITMAP_WIDTH(input_b);
351 unsigned h = BITMAP_HEIGHT(input_b);
352 size_t num_bytes = w * h;
353 bitmap_type b = input_b;
354
355 if (BITMAP_PLANES(input_b) != 1)
356 {
357 FATAL1("thin: single-plane image required; "
358 "%u-plane images cannot be thinned", BITMAP_PLANES(input_b));
359 return b;
360 }
361
362 /* Process and return a copy of the input image. */
363 MALLOCARRAY(b.bitmap, num_bytes);
364 if (b.bitmap == NULL)
365 pm_error("Unable to get memory for bitmap");
366 memcpy(b.bitmap, input_b.bitmap, num_bytes);
367
368 /* Set background pixels to zero, foreground pixels to one. */
369 for (i = 0; i < num_bytes; i++)
370 b.bitmap[i] = (b.bitmap[i] == BLACK ? 1 : 0);
371
372 again = true;
373 while (again)
374 {
375 again = false;
376
377 for (y = 1; y < h - 1; y++)
378 {
379 for (x = 1; x < w - 1; x++)
380 {
381 /* During processing, pixels are used to store edge
382 type codes, so we can't just test for WHITE or BLACK. */
383 if (*BITMAP_PIXEL(b, y, x) == 0) continue;
384
385 k = (!get_edge(b, y, x, &t)
386 || (get_edge(b, y, x+1, &t) && *BITMAP_PIXEL(b, y-1, x)
387 && *BITMAP_PIXEL(b, y+1, x))
388 || (get_edge(b, y+1, x, &t) && *BITMAP_PIXEL(b, y, x-1)
389 && *BITMAP_PIXEL(b, y, x+1))
390 || (get_edge(b, y, x+1, &t) && get_edge(b, y+1, x+1, &t)
391 && get_edge(b, y+1, x, &t)));
392 if (k) continue;
393
394 get_edge(b, y, x, &t);
395 if (t.t01) *BITMAP_PIXEL(b, y, x) |= 4;
396 *BITMAP_PIXEL(b, y, x) |= 2;
397 again = true;
398 }
399 }
400
401 for (y = 0; y < h; y++)
402 for (x = 0; x < w; x++)
403 if (*BITMAP_PIXEL(b, y, x) & 02) *BITMAP_PIXEL(b, y, x) = 0;
404
405 for (y = 1; y < h - 1; y++)
406 {
407 for (x = 1; x < w - 1; x++)
408 {
409 if (*BITMAP_PIXEL(b, y, x) == 0) continue;
410
411 k = (!get_edge(b, y, x, &t)
412 || ((*BITMAP_PIXEL(b, y, x) & 04) == 0)
413 || (get_edge(b, y+1, x, &t) && (*BITMAP_PIXEL(b, y, x-1))
414 && *BITMAP_PIXEL(b, y, x+1))
415 || (get_edge(b, y, x+1, &t) && *BITMAP_PIXEL(b, y-1, x)
416 && *BITMAP_PIXEL(b, y+1, x))
417 || (get_edge(b, y+1, x, &t) && get_edge(b, y, x+1, &t)
418 && get_edge(b, y+1, x+1, &t)));
419 if (k) continue;
420
421 *BITMAP_PIXEL(b, y, x) |= 02;
422 again = true;
423 }
424 }
425
426 for (y = 0; y < h; y++)
427 {
428 for (x = 0; x < w; x++)
429 {
430 if (*BITMAP_PIXEL(b, y, x) & 02) *BITMAP_PIXEL(b, y, x) = 0;
431 else if (*BITMAP_PIXEL(b, y, x) > 0) *BITMAP_PIXEL(b, y, x) = 1;
432 }
433 }
434 }
435
436 /* Staircase removal; northward bias. */
437 for (y = 1; y < h - 1; y++)
438 {
439 for (x = 1; x < w - 1; x++)
440 {
441 if (*BITMAP_PIXEL(b, y, x) == 0) continue;
442
443 k = !(*BITMAP_PIXEL(b, y-1, x)
444 && ((*BITMAP_PIXEL(b, y, x+1) && !*BITMAP_PIXEL(b, y-1, x+1)
445 && !*BITMAP_PIXEL(b, y+1, x-1)
446 && (!*BITMAP_PIXEL(b, y, x-1) || !*BITMAP_PIXEL(b, y+1, x)))
447 || (*BITMAP_PIXEL(b, y, x-1) && !*BITMAP_PIXEL(b, y-1, x-1)
448 && !*BITMAP_PIXEL(b, y+1, x+1) &&
449 (!*BITMAP_PIXEL(b, y, x+1) || !*BITMAP_PIXEL(b, y+1, x)))));
450 if (k) continue;
451
452 *BITMAP_PIXEL(b, y, x) |= 02;
453 }
454 }
455 for (y = 0; y < h; y++)
456 {
457 for (x = 0; x < w; x++)
458 {
459 if (*BITMAP_PIXEL(b, y, x) & 02) *BITMAP_PIXEL(b, y, x) = 0;
460 else if (*BITMAP_PIXEL(b, y, x) > 0) *BITMAP_PIXEL(b, y, x) = 1;
461 }
462 }
463
464 /* Southward bias */
465 for (y = 1; y < h - 1; y++)
466 {
467 for (x = 1; x < w - 1; x++)
468 {
469 if (*BITMAP_PIXEL(b, y, x) == 0) continue;
470
471 k = !(*BITMAP_PIXEL(b, y+1, x)
472 && ((*BITMAP_PIXEL(b, y, x+1) && !*BITMAP_PIXEL(b, y+1, x+1)
473 && !*BITMAP_PIXEL(b, y-1, x-1) && (!*BITMAP_PIXEL(b, y, x-1)
474 || !*BITMAP_PIXEL(b, y-1, x))) || (*BITMAP_PIXEL(b, y, x-1)
475 && !*BITMAP_PIXEL(b, y+1, x-1) && !*BITMAP_PIXEL(b, y-1, x+1)
476 && (!*BITMAP_PIXEL(b, y, x+1) || !*BITMAP_PIXEL(b, y-1, x)) )));
477 if (k) continue;
478
479 *BITMAP_PIXEL(b, y, x) |= 02;
480 }
481 }
482 for (y = 0; y < h; y++)
483 {
484 for (x = 0; x < w; x++)
485 {
486 if (*BITMAP_PIXEL(b, y, x) & 02) *BITMAP_PIXEL(b, y, x) = 0;
487 else if (*BITMAP_PIXEL(b, y, x) > 0) *BITMAP_PIXEL(b, y, x) = 1;
488 }
489 }
490
491 /* Set background pixels to WHITE, foreground pixels to BLACK. */
492 for (i = 0; i < num_bytes; i++)
493 b.bitmap[i] = (b.bitmap[i] == 0 ? WHITE : BLACK);
494 return b;
495 }
496
497
498
499 bool get_edge(bitmap_type b, int y, int x, struct etyp *t)
500 {
501 t->t00 = 0; t->t01 = 0; t->t01s = 0; t->t11 = 0;
502 check(*BITMAP_PIXEL(b, y - 1, x - 1), *BITMAP_PIXEL(b, y - 1, x),
503 *BITMAP_PIXEL(b, y - 1, x + 1), t);
504 check(*BITMAP_PIXEL(b, y - 1, x + 1), *BITMAP_PIXEL(b, y, x + 1),
505 *BITMAP_PIXEL(b, y + 1, x + 1), t);
506 check(*BITMAP_PIXEL(b, y + 1, x + 1), *BITMAP_PIXEL(b, y + 1, x),
507 *BITMAP_PIXEL(b, y + 1, x - 1), t);
508 check(*BITMAP_PIXEL(b, y + 1, x - 1), *BITMAP_PIXEL(b, y, x - 1),
509 *BITMAP_PIXEL(b, y - 1, x - 1), t);
510 return *BITMAP_PIXEL(b, y, x) && t->t00 && t->t11 && !t->t01s;
511 }
512
513
514
515 void check(int v1, int v2, int v3, struct etyp *t)
516 {
517 if (!v2 && (!v1 || !v3)) t->t00 = 1;
518 if (v2 && (v1 || v3)) t->t11 = 1;
519 if ((!v1 && v2) || (!v2 && v3)) { t->t01s = t->t01; t->t01 = 1; }
520 }
521 #endif