-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
149 lines (116 loc) · 4.86 KB
/
main.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
MIT License
Copyright Iñaki Griego (c) 2017
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <iostream>
#include <string>
#include <algorithm>
#include <unordered_set>
#include "lodepng.h"
using Pixel = std::uint32_t; // RGBA
constexpr unsigned PixelSize = sizeof(Pixel);
using ImageData = std::vector<unsigned char>; // We assume it is in RGBA format
using ImageIt = ImageData::const_iterator;
std::unordered_set<Pixel> extractedPlainTilesColors; // A plain tile is a monochrome one. We only are going to extract one of each color
Pixel getPixel(ImageIt it)
{
const std::uint32_t r = *(it + 0);
const std::uint32_t g = *(it + 1);
const std::uint32_t b = *(it + 2);
const std::uint32_t a = *(it + 3);
return r << 24 | g << 16 | b << 8 | a;
}
bool isPlainTile(ImageIt tileBeginIt, unsigned tileWidth, unsigned tileHeight, unsigned imageWidth)
{
const auto firstPixel = getPixel(tileBeginIt);
for(unsigned y = 0; y < tileHeight; ++ y)
{
for(unsigned x = 0; x < tileWidth; ++ x)
{
const auto it = tileBeginIt + (imageWidth * y + x) * PixelSize;
if(firstPixel != getPixel(it))
{
return false;
}
}
}
return true;
}
void saveTile(const std::string& fileName, ImageIt tileBeginIt, unsigned tileWidth, unsigned tileHeight, unsigned imageWidth)
{
ImageData image;
image.resize(tileWidth * tileHeight * PixelSize);
auto destinyIt = image.begin();
for(unsigned y = 0; y < tileHeight; ++ y)
{
const auto lineBeginIt = tileBeginIt + (imageWidth * y) * PixelSize;
const auto lineEndIt = lineBeginIt + tileWidth * PixelSize;
std::copy(lineBeginIt, lineEndIt, destinyIt);
destinyIt += tileWidth * PixelSize;
}
const auto error = lodepng::encode(fileName, image, tileWidth, tileHeight);
if(error)
{
std::cout << "Cannot save " << fileName << " file (" << error << "): " << lodepng_error_text(error) << std::endl;
}
}
int main(int argc, char *argv[])
{
if(argc != 4)
{
std::cout << "tile_extractor 1.0\n";
std::cout << "Usage: tile_extractor file_with_tiles.png tile_width tile_height\n";
std::cout << "Example: tile_extractor tileset.png 32 32\n";
return 0;
}
const std::string fileName{argv[1]};
const unsigned tileWidth = std::atoi(argv[2]);
const unsigned tileHeight = std::atoi(argv[3]);
ImageData image;
unsigned imageWidth, imageHeight;
const auto error = lodepng::decode(image, imageWidth, imageHeight, fileName);
if(error)
{
std::cout << "Cannot open " << fileName << " file (" << error << "): " << lodepng_error_text(error) << std::endl;
}
const auto horizontalTiles = imageWidth / tileWidth;
const auto verticalTiles = imageHeight / tileHeight;
for(unsigned tileY = 0; tileY < verticalTiles; ++ tileY)
{
for(unsigned tileX = 0; tileX < horizontalTiles; ++ tileX)
{
const int tileId = horizontalTiles * tileY + tileX;
const auto tileBeginIt = image.cbegin() + ((tileY * tileHeight) * imageWidth + (tileX * tileWidth)) * PixelSize;
std::cout << "Processing tile " << tileId << "... ";
if(isPlainTile(tileBeginIt, tileWidth, tileHeight, imageWidth))
{
const auto firstPixel = getPixel(tileBeginIt);
if(extractedPlainTilesColors.find(firstPixel) != extractedPlainTilesColors.end())
{
std::cout << "Already extracted plain tile, skipped\n";
continue;
}
extractedPlainTilesColors.insert(firstPixel);
}
const auto tileFileName = "tile" + std::to_string(tileId) + ".png";
saveTile(tileFileName, tileBeginIt, tileWidth, tileHeight, imageWidth);
std::cout << tileFileName << " saved\n";
}
}
return 0;
}