1 | /* |
---|
2 | * libpipi Pathetic image processing interface library |
---|
3 | * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id$ |
---|
7 | * |
---|
8 | * This library is free software. It comes without any warranty, to |
---|
9 | * the extent permitted by applicable law. You can redistribute it |
---|
10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | /* |
---|
16 | * dilate.c: dilate and erode functions |
---|
17 | */ |
---|
18 | |
---|
19 | #include "config.h" |
---|
20 | #include "common.h" |
---|
21 | |
---|
22 | #include <stdlib.h> |
---|
23 | #include <stdio.h> |
---|
24 | #include <string.h> |
---|
25 | #include <math.h> |
---|
26 | |
---|
27 | #include "pipi.h" |
---|
28 | #include "pipi_internals.h" |
---|
29 | |
---|
30 | /* FIXME: these functions are almost the same, try to merge them |
---|
31 | * somewhat efficiently. */ |
---|
32 | /* TODO: - dilate by k (Manhattan distance) |
---|
33 | * - dilate by r (euclidian distance, with non-integer r) */ |
---|
34 | pipi_image_t *pipi_dilate(pipi_image_t *src) |
---|
35 | { |
---|
36 | pipi_image_t *dst; |
---|
37 | pipi_pixels_t *srcp, *dstp; |
---|
38 | float *srcdata, *dstdata; |
---|
39 | int x, y, w, h, i, gray; |
---|
40 | |
---|
41 | w = src->w; |
---|
42 | h = src->h; |
---|
43 | |
---|
44 | gray = (src->last_modified == PIPI_PIXELS_Y_F); |
---|
45 | |
---|
46 | srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F) |
---|
47 | : pipi_getpixels(src, PIPI_PIXELS_RGBA_F); |
---|
48 | srcdata = (float *)srcp->pixels; |
---|
49 | |
---|
50 | dst = pipi_new(w, h); |
---|
51 | dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F) |
---|
52 | : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); |
---|
53 | dstdata = (float *)dstp->pixels; |
---|
54 | |
---|
55 | for(y = 0; y < h; y++) |
---|
56 | { |
---|
57 | for(x = 0; x < w; x++) |
---|
58 | { |
---|
59 | double t; |
---|
60 | int x2, y2, x3, y3; |
---|
61 | |
---|
62 | y2 = y - 1; |
---|
63 | if(y2 < 0) y2 = h - 1; |
---|
64 | y3 = y + 1; |
---|
65 | if(y3 >= h) y3 = 0; |
---|
66 | |
---|
67 | x2 = x - 1; |
---|
68 | if(x2 < 0) x2 = w - 1; |
---|
69 | x3 = x + 1; |
---|
70 | if(x3 >= w) x3 = 0; |
---|
71 | |
---|
72 | if(gray) |
---|
73 | { |
---|
74 | t = srcdata[y * w + x]; |
---|
75 | if(srcdata[y2 * w + x] > t) t = srcdata[y2 * w + x]; |
---|
76 | if(srcdata[y3 * w + x] > t) t = srcdata[y3 * w + x]; |
---|
77 | if(srcdata[y * w + x2] > t) t = srcdata[y * w + x2]; |
---|
78 | if(srcdata[y * w + x3] > t) t = srcdata[y * w + x3]; |
---|
79 | dstdata[y * w + x] = t; |
---|
80 | } |
---|
81 | else |
---|
82 | { |
---|
83 | for(i = 0; i < 4; i++) |
---|
84 | { |
---|
85 | t = srcdata[4 * (y * w + x) + i]; |
---|
86 | if(srcdata[4 * (y2 * w + x) + i] > t) |
---|
87 | t = srcdata[4 * (y2 * w + x) + i]; |
---|
88 | if(srcdata[4 * (y3 * w + x) + i] > t) |
---|
89 | t = srcdata[4 * (y3 * w + x) + i]; |
---|
90 | if(srcdata[4 * (y * w + x2) + i] > t) |
---|
91 | t = srcdata[4 * (y * w + x2) + i]; |
---|
92 | if(srcdata[4 * (y * w + x3) + i] > t) |
---|
93 | t = srcdata[4 * (y * w + x3) + i]; |
---|
94 | dstdata[4 * (y * w + x) + i] = t; |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | return dst; |
---|
101 | } |
---|
102 | |
---|
103 | pipi_image_t *pipi_erode(pipi_image_t *src) |
---|
104 | { |
---|
105 | pipi_image_t *dst; |
---|
106 | pipi_pixels_t *srcp, *dstp; |
---|
107 | float *srcdata, *dstdata; |
---|
108 | int x, y, w, h, i, gray; |
---|
109 | |
---|
110 | w = src->w; |
---|
111 | h = src->h; |
---|
112 | |
---|
113 | gray = (src->last_modified == PIPI_PIXELS_Y_F); |
---|
114 | |
---|
115 | srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F) |
---|
116 | : pipi_getpixels(src, PIPI_PIXELS_RGBA_F); |
---|
117 | srcdata = (float *)srcp->pixels; |
---|
118 | |
---|
119 | dst = pipi_new(w, h); |
---|
120 | dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F) |
---|
121 | : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); |
---|
122 | dstdata = (float *)dstp->pixels; |
---|
123 | |
---|
124 | for(y = 0; y < h; y++) |
---|
125 | { |
---|
126 | for(x = 0; x < w; x++) |
---|
127 | { |
---|
128 | double t; |
---|
129 | int x2, y2, x3, y3; |
---|
130 | |
---|
131 | y2 = y - 1; |
---|
132 | if(y2 < 0) y2 = h - 1; |
---|
133 | y3 = y + 1; |
---|
134 | if(y3 >= h) y3 = 0; |
---|
135 | |
---|
136 | x2 = x - 1; |
---|
137 | if(x2 < 0) x2 = w - 1; |
---|
138 | x3 = x + 1; |
---|
139 | if(x3 >= w) x3 = 0; |
---|
140 | |
---|
141 | if(gray) |
---|
142 | { |
---|
143 | t = srcdata[y * w + x]; |
---|
144 | if(srcdata[y2 * w + x] < t) t = srcdata[y2 * w + x]; |
---|
145 | if(srcdata[y3 * w + x] < t) t = srcdata[y3 * w + x]; |
---|
146 | if(srcdata[y * w + x2] < t) t = srcdata[y * w + x2]; |
---|
147 | if(srcdata[y * w + x3] < t) t = srcdata[y * w + x3]; |
---|
148 | dstdata[y * w + x] = t; |
---|
149 | } |
---|
150 | else |
---|
151 | { |
---|
152 | for(i = 0; i < 4; i++) |
---|
153 | { |
---|
154 | t = srcdata[4 * (y * w + x) + i]; |
---|
155 | if(srcdata[4 * (y2 * w + x) + i] < t) |
---|
156 | t = srcdata[4 * (y2 * w + x) + i]; |
---|
157 | if(srcdata[4 * (y3 * w + x) + i] < t) |
---|
158 | t = srcdata[4 * (y3 * w + x) + i]; |
---|
159 | if(srcdata[4 * (y * w + x2) + i] < t) |
---|
160 | t = srcdata[4 * (y * w + x2) + i]; |
---|
161 | if(srcdata[4 * (y * w + x3) + i] < t) |
---|
162 | t = srcdata[4 * (y * w + x3) + i]; |
---|
163 | dstdata[4 * (y * w + x) + i] = t; |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | return dst; |
---|
170 | } |
---|
171 | |
---|