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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
#import <stdlib.h>
#import <stdint.h>
#import <assert.h>
#import <math.h>
#import <unistd.h>
#import "GrayMap.h"
enum
{
kDownscale = 16,
kHalfDownscale = kDownscale / 2,
kThreshold = 128
};
static void PerformDistanceMapping(GrayMap *source, GrayMap *dmap, GrayMap *amap);
static void DistanceMapOnePixel(GrayMap *source, GrayMap *dmap, GrayMap *amap, uint32_t x, uint32_t y);
static bool wrap = false;
static inline uint32_t RoundSize(uint32_t size)
{
return (size + kDownscale - 1) / kDownscale;
}
int main (int argc, char * argv[])
{
GrayMap *source = NULL;
GrayMap *dmap = NULL;
GrayMap *amap = NULL;
bool printUsage = false;
bool angleMap = false;
// Get options
for (;;)
{
int option = getopt(argc, argv, "wa");
if (option == -1) break;
switch (option)
{
case 'w':
wrap = true;
break;
case 'a':
angleMap = true;
break;
default:
printUsage = true;
}
}
if (argc <= optind) printUsage = true;
if (printUsage)
{
fprintf(stderr, "Usage: %s [-w] <filename.png>\n", argv[0]);
return EXIT_FAILURE;
}
source = ReadGrayMap(argv[optind]);
if (source == NULL) return EXIT_FAILURE;
dmap = NewGrayMap(RoundSize(source->width), RoundSize(source->height), 0);
if (angleMap) amap = NewGrayMap(RoundSize(source->width), RoundSize(source->height), 0);
if (dmap == NULL || (angleMap && amap == NULL))
{
fprintf(stderr, "Could not allocate memory for output image.\n");
return EXIT_FAILURE;
}
PerformDistanceMapping(source, dmap, amap);
WriteGrayMap("distance_map.png", dmap);
if (angleMap) WriteGrayMap("angle_map.png", amap);
return 0;
}
static void PerformDistanceMapping(GrayMap *source, GrayMap *dmap, GrayMap *amap)
{
uint32_t width, height, x, y;
assert(source && dmap);
width = dmap->width;
height = dmap->height;
for (y = 0; y != height; ++y)
{
for (x = 0; x != width; ++x)
{
DistanceMapOnePixel(source, dmap, amap, x, y);
}
putchar('.');
fflush(stdout);
}
}
static bool ReadPx(GrayMap *source, uint32_t x, uint32_t y, int16_t dx, int16_t dy);
static inline int32_t Max(int32_t a, int32_t b)
{
return (a > b) ? a : b;
}
static inline int32_t Min(int32_t a, int32_t b)
{
return (a < b) ? a : b;
}
static inline int32_t Abs(int32_t a)
{
return (a >= 0) ? a : -a;
}
static void DistanceMapOnePixel(GrayMap *source, GrayMap *dmap, GrayMap *amap, uint32_t x, uint32_t y)
{
int32_t dx, dy;
bool target;
uint32_t distanceSq, bestDistanceSq = UINT32_MAX;
uint32_t bestDistance;
float bestAngle;
uint32_t currDistance, maxDistance = Max(source->width, source->height);
int8_t ddx = 1, ddy = 0, ddt;
uint8_t countdown = 3;
uint32_t i, length = 2;
int32_t bestDx = 1, bestDy = 1;
dx = 0;
dy = -1;
if (amap == NULL) maxDistance = Min(maxDistance, 128);
target = !ReadPx(source, x, y, 0, 0);
for (;;)
{
// Spiral outwards.
do
{
for (i = 0; i < length; i++)
{
if (ReadPx(source, x, y, dx, dy) == target)
{
distanceSq = dx * dx + dy * dy;
if (distanceSq < bestDistanceSq)
{
bestDistanceSq = distanceSq;
bestDx = dx;
bestDy = dy;
}
}
dx += ddx;
dy += ddy;
}
// Turn a corner.
ddt = ddx;
ddx = -ddy;
ddy = ddt;
}
while (--countdown);
currDistance = Max(Abs(dx), Abs(dy));
if ((currDistance * currDistance) > bestDistanceSq || currDistance > maxDistance) break;
countdown = 2;
length++;
}
bestDistance = sqrt(bestDistanceSq);
if (target)
{
if (bestDistance > 128) bestDistance = 0;
else bestDistance = 128 - bestDistance;
bestDx = -bestDx;
bestDy = -bestDy;
}
else
{
bestDistance = 127 + bestDistance;
if (bestDistance > 255) bestDistance = 255;
}
GrayMapSet(dmap, x, y, bestDistance);
if (amap != NULL)
{
bestAngle = atan2(bestDx, bestDy);
bestAngle = (bestAngle + M_PI) * 127.5 / M_PI; // Convert from +/-pi to 0..255
GrayMapSet(amap, x, y, bestAngle);
}
}
static bool ReadPx(GrayMap *source, uint32_t x, uint32_t y, int16_t dx, int16_t dy)
{
int32_t ax = x * kDownscale + dx - kHalfDownscale;
int32_t ay = y * kDownscale + dy - kHalfDownscale;
if (wrap)
{
ax = ax % source->width;
if (ax < 0) ax += source->width;
ay = ay % source->height;
if (ay < 0) ay += source->height;
}
return GrayMapGet(source, ax, ay) >= kThreshold;
}
|