-
Notifications
You must be signed in to change notification settings - Fork 68
/
VertexScannerMain.cpp
129 lines (115 loc) · 2.54 KB
/
VertexScannerMain.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* VertexScannerMain.cpp
* VertexHelper
*
* Created by Peter Siroki on 2010.06.21.
*
*/
#include "PNGLoader.h"
#include "VertexScanner.h"
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int cellWidth = -1;
int cellHeight = -1;
int rows = -1;
int cols = -1;
opterr = 0;
int c;
bool help = false;
while((c = getopt(argc, argv, "x:y:r:c:h")) != -1)
{
fprintf(stderr, "%c\n", c);
switch(c)
{
case 'h':
help = true;
break;
case 'x':
cellWidth = atoi(optarg);
break;
case 'y':
cellHeight = atoi(optarg);
break;
case 'r':
rows = atoi(optarg);
break;
case 'c':
cols = atoi(optarg);
break;
case '?':
if(strchr("xyrc", optopt))
{
fprintf(stderr, "Option -%c requires an argument. See help for details.\n", optopt);
return -1;
} else
{
fprintf(stderr, "Unknown option: %c\n", optopt);
return -1;
}
break;
}
}
if(help || optind != argc-1 || argc <= 1 || (cellWidth <= 0 && cols <= 0) || (cellHeight <= 0 && rows <= 0))
{
fprintf(stderr, "Usage:\n\tVertexScanner -r <rows> -c <cols> <png-file>\n");
return -1;
}
ImageDesc img;
loadPNG(argv[optind], &img);
if(cols > 0 && cellWidth <= 0)
cellWidth = img.width/cols;
if(cellWidth > 0 && cols <= 0)
cols = img.width/cellWidth;
if(rows > 0 && cellHeight <= 0)
cellHeight = img.height/rows;
if(cellHeight > 0 && rows <= 0)
rows = img.height/cellHeight;
if(cellWidth*cols > img.width)
{
fprintf(stderr, "Invalid width: %d (image width is %d)\n",
int(cellWidth*cols),
int(img.width));
return -1;
}
if(cellHeight*rows > img.height)
{
fprintf(stderr, "Invalid height: %d (image height is %d)\n",
int(cellHeight*rows),
int(img.height));
return -1;
}
for(int cy=0; cy<rows; cy++)
{
for(int cx=0; cx<cols; cx++)
{
ImageDesc cell;
cell.width = cellWidth;
cell.height = cellHeight;
// the image is a top-down image
cell.pitch = img.pitch;
// so data will point to the last row
cell.data = img.data;
// also offset it by the coordinates of the cell
cell.data += (cx*cellWidth*4)+(cy*cellHeight*img.pitch);
Vec2Array points;
points.points = NULL;
findPoints(&cell, &points);
printf("\n// Cell %d %d\n", cx, cy);
if(points.count > 0)
{
for(int i=0; i<points.count; i++)
{
Vec2 p = points.points[i];
p.x -= cellWidth*0.5f;
p.y -= cellHeight*0.5f;
printf("%g %g\n", p.x, p.y);
}
}
if(points.points)
free(points.points);
}
}
}