-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiler.cpp
100 lines (85 loc) · 2.76 KB
/
tiler.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <sys/stat.h>
#include <Magick++.h>
using namespace std;
using namespace Magick;
/// <summary>
/// Renders the given image to multiple tiles whose details are provided in a tile specs file. The format of the file is:
///
/// # comment
/// outputFile tileSizeX tileSizeY sourceOffsetX sourceOffsetY scaleFactor opacity
/// ...
///
/// </summary>
int main(int argc, char *argv[])
{
if (argc != 3)
{
cout << "Usage: %prog <image> <tile specs>";
}
auto_ptr<Image> img(new Image::Image(argv[1]));
// Set the background color and virtual pixel to transparent.
img->virtualPixelMethod(TransparentVirtualPixelMethod);
img->backgroundColor(Color(0, 0, 0, TransparentOpacity));
ifstream tileSpecs(argv[2]);
while (!tileSpecs.eof())
{
string line;
getline(tileSpecs, line);
if (line.length() > 0)
{
stringstream lineStream(line);
if (line[0] == '#')
{
// Comments are echoed to stdout.
char c;
string s;
lineStream >> c >> s;
cout << s << ' ';
flush(cout);
}
else
{
string outputFile;
int tileSizeX;
int tileSizeY;
double sourceOffsetX;
double sourceOffsetY;
double scaleFactor;
int opacity;
// Read the tile spec.
lineStream >> outputFile >> tileSizeX >> tileSizeY >> sourceOffsetX >> sourceOffsetY >> scaleFactor >> opacity;
// Create an image for the tile that's a copy/reference to the source image.
auto_ptr<Image> tileImg(new Image::Image());
*tileImg = *img;
// Set/attenuate the opacity of the tile
tileImg->opacity(MaxRGB - opacity * (MaxRGB / 255));
// Set the viewport for the tile image to the tile size.
stringstream viewport;
viewport << tileSizeX << 'x' << tileSizeY << "+0+0";
SetImageArtifact(tileImg->image(), "distort:viewport", viewport.str().c_str());
// Distort the tile image using the the following distortion args, in the format: X,Y, Scale, Angle, NewX,NewY
// (See http://www.imagemagick.org/Usage/distorts/#srt for details.)
double distortArgs[] = { sourceOffsetX, sourceOffsetY, scaleFactor, 0, 0, 0 };
tileImg->distort(ScaleRotateTranslateDistortion, sizeof(distortArgs) / sizeof(double), distortArgs);
// If there's already an image for this tile then that means another image has rendered output
// to it. In that case we composite this image's contribution over the contribution(s) of the previous
// image(s).
struct stat stFileInfo;
if (stat(outputFile.c_str(), &stFileInfo) == 0)
{
auto_ptr<Image> existingTileImg(new Image::Image(outputFile));
existingTileImg->composite(*tileImg, 0, 0, OverCompositeOp);
existingTileImg->write(outputFile);
}
else
{
tileImg->write(outputFile);
}
}
}
}
return 0;
}