-
Notifications
You must be signed in to change notification settings - Fork 2
/
circularize.cpp
112 lines (100 loc) · 2.73 KB
/
circularize.cpp
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
// $Revision: 1.2 $
// circularize.cpp
// (c) 2004-2022 Matthew Arcus
// MIT License
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "utils.h"
#include "constants.h"
#include "image.h"
const char* usage =
"Usage: %s [-bg r:g:b][-fuzz x%%][-f filename][-out filename]\n";
static inline double square(double x) {
return x * x;
}
int main(int argc, char* argv[])
{
Image inImage;
const char* infilename = "images/earth.ppm";
const char* outfilename = NULL;
Rgb background = black;
Rgb ibackground = black;
int width;
int height;
double fuzz = 2.0;
// array of pointers. Dynamic allocation is less typing than
// stack allocating everything.
OptSpec *optspecs [] = {
// General options
new RgbSpec ("-bg", background),
new RgbSpec ("-ibg", ibackground),
new DoubleSpec ("-fuzz", fuzz),
new StringSpec ("-f", infilename),
new StringSpec ("-out", outfilename),
NULL // Finish up
};
char *progname = argv[0];
argc--;argv++;
while (argc > 0) {
int result = OptSpec::ReadOpts(argc, argv, optspecs);
if (result != 1) {
error(usage, progname);
}
argc--; argv++;
}
inImage.Read(infilename);
height = inImage.Height();
// Make sure image is big enough to fit a full circular image
width = inImage.Height();
double radius = height/2.0;
Image outImage(width, height);
//fprintf(stderr, "Size %d\n", height);
for (int row = 0; row < inImage.Height(); row++) {
int first = -1;
int last = -1;
int column;
// First scan the row in the input image to find the bounds.
for (column = 0; column < inImage.Width(); column++) {
Rgb rgb;
inImage.GetRgb(column, row, rgb);
if (!rgb.Match(ibackground, fuzz)) {
first = column;
break;
}
}
if (first < 0) {
// No non-background pixel found, what to do?
first = last = inImage.Width()/2;
} else {
// Now look from the right
for (column = inImage.Width()-1; column >= 0; column--) {
Rgb rgb;
inImage.GetRgb(column, row, rgb);
if (!rgb.Match(ibackground, fuzz)) {
last = column+1;
break;
}
}
}
//fprintf(stderr, "%d %d\n", first, last);
// Work out the half width of the image on this scan line
double hw = sqrt(square(radius) - square(radius - (row + 0.5)));
double a = (last-first)/(2.0*hw);
double b = first - a * (radius - hw);
for (column = 0; column < width; column++) {
// Find the corresponding pixel in the input image
int p = int(floor(a * column + b));
if (p >= first && p < last) {
Rgb rgb;
inImage.GetRgb(p,row,rgb);
outImage.SetRgb(column,row,rgb);
} else {
outImage.SetRgb(column,row,background);
}
}
}
outImage.Write(outfilename);
return 0;
}