-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.hpp
121 lines (99 loc) · 3.32 KB
/
image.hpp
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
// ---------------------------------------------------------------------------
// This software is in the public domain, furnished "as is", without technical
// support, and with no warranty, express or implied, as to its usefulness for
// any purpose.
//
// IMAGE.HPP
// Image class is able to stitch multiple images together to one giant image
//
// Author: Thomas Brüggemann
// ---------------------------------------------------------------------------
#ifndef IMAGE_HPP
#define IMAGE_HPP
#include <vector>
#include <string>
#include <Magick++.h>
#include <exception>
#include "patches.hpp"
#include "tiles.hpp"
#include "crayons.hpp"
Crayon IMAGE_RED(FG_RED);
Crayon IMAGE_DEFAULT(FG_DEFAULT);
class Image
{
private:
public:
Image();
inline bool stitchTogether(TilesResult tiles, int zoom, std::string fileName);
};
// CONSTRUCTOR
Image::Image()
{
Magick::InitializeMagick("");
}
// STITCH TOGETHER
inline bool Image::stitchTogether(TilesResult tiles, int zoom, std::string fileName)
{
try
{
// check if stitched image already exists
std::ifstream checkfile(fileName);
if(checkfile.good()) return true;
std::vector<Magick::Image> sourceImageList;
Magick::Image image;
int errorCount = 0;
// loop all coordinates
for(XY xy : tiles.coordinates)
{
std::string imageName = "tiles/" + patches::to_string(zoom) + "_" + patches::to_string(xy.x) +
"_" + patches::to_string(xy.y) + ".png";
// read content of image
std::ifstream ifs(imageName);
std::string content((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
// check if some html crap is in this image file
if(content.find("<html") != std::string::npos)
{
std::cout << IMAGE_RED << "ERROR: html content in file " << imageName << IMAGE_DEFAULT << std::endl;
errorCount++;
}
else
{
image.read(imageName);
sourceImageList.push_back(image);
}
}
// quit if more than one error occured
if(errorCount > 0)
{
exit(2);
}
Magick::Geometry tileGeometry(tiles.width, tiles.height);
// background color for the montage
Magick::Color color("rgba(0,0,0,0)");
// prepare settings for the montage
Magick::Montage montageSettings;
montageSettings.geometry("256x256-0-0");
montageSettings.shadow(false);
montageSettings.backgroundColor(color);
montageSettings.tile(tileGeometry);
std::vector<Magick::Image> montagelist;
Magick::montageImages( &montagelist, sourceImageList.begin(), sourceImageList.end(), montageSettings);
// reduce amount of colors for every image
for(Magick::Image &image : montagelist)
{
image.type(Magick::PaletteType);
image.quantizeColors(128);
image.quantize();
}
// This will give the expected result
Magick::writeImages(montagelist.begin(), montagelist.end(), fileName);
return true;
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
return false;
}
}
#endif