-
Notifications
You must be signed in to change notification settings - Fork 0
/
charaMaker.cpp
168 lines (146 loc) · 6.23 KB
/
charaMaker.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
LodePNG Examples
Copyright (c) 2005-2012 Lode Vandevenne
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <string>
#include <cctype>
#include <algorithm>
#include "lodepng.h"
#include "lodepng.cpp"
//Input image must be RGB buffer (3 bytes per pixel), but you can easily make it
//support RGBA input and output by changing the inputChannels and/or outputChannels
//in the function to 4.
void encodeBMP(std::vector<unsigned char>& bmp, const unsigned char* image, int w, int h)
{
//3 bytes per pixel used for both input and output.
int inputChannels = 4;
int outputChannels = 3;
//bytes 0-13
bmp.push_back('B'); bmp.push_back('M'); //0: bfType
bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); //2: bfSize; size not yet known for now, filled in later.
bmp.push_back(0); bmp.push_back(0); //6: bfReserved1
bmp.push_back(0); bmp.push_back(0); //8: bfReserved2
bmp.push_back(54 % 256); bmp.push_back(54 / 256); bmp.push_back(0); bmp.push_back(0); //10: bfOffBits (54 header bytes)
//bytes 14-53
bmp.push_back(40); bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); //14: biSize
bmp.push_back(w % 256); bmp.push_back(w / 256); bmp.push_back(0); bmp.push_back(0); //18: biWidth
bmp.push_back(h % 256); bmp.push_back(h / 256); bmp.push_back(0); bmp.push_back(0); //22: biHeight
bmp.push_back(1); bmp.push_back(0); //26: biPlanes
bmp.push_back(outputChannels * 8); bmp.push_back(0); //28: biBitCount
bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); //30: biCompression
bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); //34: biSizeImage
bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); //38: biXPelsPerMeter
bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); //42: biYPelsPerMeter
bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); //46: biClrUsed
bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); bmp.push_back(0); //50: biClrImportant
/*
Convert the input RGBRGBRGB pixel buffer to the BMP pixel buffer format. There are 3 differences with the input buffer:
-BMP stores the rows inversed, from bottom to top
-BMP stores the color channels in BGR instead of RGB order
-BMP requires each row to have a multiple of 4 bytes, so sometimes padding bytes are added between rows
*/
int imagerowbytes = outputChannels * w;
imagerowbytes = imagerowbytes % 4 == 0 ? imagerowbytes : imagerowbytes + (4 - imagerowbytes % 4); //must be multiple of 4
for(int y = h - 1; y >= 0; y--) //the rows are stored inversed in bmp
{
int c = 0;
for(int x = 0; x < imagerowbytes; x++)
{
if(x < w * outputChannels)
{
int inc = c;
//Convert RGB(A) into BGR(A)
if(c == 0) inc = 2;
else if(c == 2) inc = 0;
bmp.push_back(image[inputChannels * (w * y + x / outputChannels) + inc]);
}
else bmp.push_back(0);
c++;
if(c >= outputChannels) c = 0;
}
}
// Fill in the size
bmp[2] = bmp.size() % 256;
bmp[3] = (bmp.size() / 256) % 256;
bmp[4] = (bmp.size() / 65536) % 256;
bmp[5] = bmp.size() / 16777216;
}
std::string ToUpper(std::string str)
{
std::string stmp = str;
transform(stmp.begin(), stmp.end(), stmp.begin(), (int (*)(int))toupper);
return stmp;
}
std::string ToLower(std::string str)
{
std::string stmp = str;
transform(stmp.begin(), stmp.end(), stmp.begin(), (int (*)(int))tolower);
return stmp;
}
int main(int argc, char *argv[])
{
if (argc < 2) {
std::cout << "Bad Command!" << std::endl;
return 0;
}
std::string infile, outfile = "";
unsigned acolor = 0xFF00FF;
for (int i = 1; i < argc; ++i) {
std::string stmp = ToUpper(argv[i]);
if (i == argc - 1) {
std::cout << "Bad Command!" << std::endl;
return 1;
}
if (stmp == "-I") {
infile = argv[++i];
continue;
}
if (stmp == "-O") {
outfile = argv[++i];
continue;
}
if (stmp == "-C") {
std::istringstream is(argv[++i]);
is >> std::hex >> acolor >> std::dec;
continue;
}
}
if (outfile == "") outfile = infile + ".converted.bmp";
std::vector<unsigned char> image; //the raw pixels
unsigned width, height;
unsigned error = lodepng::decode(image, width, height, infile);
if (error) {
std::cout << "error " << error << ": " << lodepng_error_text(error) << std::endl;
return error;
}
int pnglen = image.size() / 4;
for (int i = 1; i <= pnglen; i++) {
if (image[i*4-1] == 0) {
image[i*4-2] = acolor & 0x0000FF;
image[i*4-3] = (acolor & 0x00FF00) >> 8;
image[i*4-4] = (acolor & 0xFF0000) >> 16;
}
//printf("%2x %2x %2x %d\n", image[i*4-4], image[i*4-3], image[i*4-2], image[i*4-1]);
}
std::vector<unsigned char> bmp;
encodeBMP(bmp, &image[0], width, height);
lodepng::save_file(bmp, outfile);
}