-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrwpng.cpp
272 lines (229 loc) · 8.58 KB
/
rwpng.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
Copyright (c) 2011, Eric Haines
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "stdafx.h"
#include "rwpng.h"
#include <assert.h>
#include <iostream>
// from http://lodev.org/lodepng/example_decode.cpp
//Decode from disk to raw pixels with a single function call
// return 0 on success
int readpng(progimage_info *im, wchar_t *filename, LodePNGColorType colortype)
{
//char filename[MAX_PATH];
//dumb_wcharToChar(wfilename,filename);
//decode
unsigned int width, height;
unsigned int error = lodepng::decode(im->image_data, width, height, filename, colortype);
//if there's an error, display it
if (error)
{
im->width = 0;
im->height = 0;
//std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
return (int)error;
}
im->width = (int)width;
im->height = (int)height;
//the pixels are now in the vector "image", for color+alpha these are 4 bytes per pixel, ordered RGBARGBA..., use it as texture, draw it, ...
return 0;
}
void readpng_cleanup(int mode, progimage_info *im)
{
// mode was important back when libpng was in use
if ( mode == 1 )
{
im->image_data.clear();
}
}
// return 0 if no error
int readpngheader(progimage_info* im, wchar_t* filename, LodePNGColorType& colortype)
{
unsigned int width, height;
std::vector<unsigned char> buffer;
lodepng::load_file(buffer, filename);
colortype = LCT_RGBA;
unsigned bitdepth = 8;
LodePNGState state;
lodepng_state_init(&state);
state.info_raw.colortype = colortype;
state.info_raw.bitdepth = bitdepth;
// reads header and resets other parameters in state->info_png
state.error = lodepng_inspect(&width, &height, &state, buffer.empty() ? 0 : &buffer[0], (unsigned)buffer.size());
unsigned int error = state.error;
colortype = state.info_png.color.colortype;
lodepng_state_cleanup(&state);
//if there's an error, display it
if (error)
{
im->width = 0;
im->height = 0;
//std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
return (int)error;
}
im->width = (int)width;
im->height = (int)height;
return 0;
}
// from http://lodev.org/lodepng/example_encode.cpp
//Encode from raw pixels to disk with a single function call
//The image argument has width * height RGBA pixels or width * height * channels
// return 0 on success
int writepng(progimage_info *im, int channels, wchar_t *filename)
{
//char filename[MAX_PATH];
//dumb_wcharToChar(wfilename,filename);
//Encode the image, depending on type
unsigned int error = 1; // 1 means didn't reach lodepng
if ( channels == 4 )
{
// 32 bit RGBA, the default
error = lodepng::encode(filename, im->image_data, (unsigned int)im->width, (unsigned int)im->height, LCT_RGBA );
}
else if ( channels == 3 )
{
// 24 bit RGB
error = lodepng::encode(filename, im->image_data, (unsigned int)im->width, (unsigned int)im->height, LCT_RGB );
}
else if (channels == 1)
{
// 8 bit grayscale
error = lodepng::encode(filename, im->image_data, (unsigned int)im->width, (unsigned int)im->height, LCT_GREY);
}
else
{
assert(0);
}
//if there's an error, display it
if (error)
{
//std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
return (int)error;
}
return 0;
}
void writepng_cleanup(progimage_info *im)
{
im->image_data.clear();
}
progimage_info* allocateGrayscaleImage(progimage_info* source_ptr)
{
// allocate output image and fill it up
progimage_info* destination_ptr = new progimage_info();
destination_ptr->width = source_ptr->width;
destination_ptr->height = source_ptr->height;
destination_ptr->image_data.resize(destination_ptr->width * destination_ptr->height * 1 * sizeof(unsigned char), 0x0);
return destination_ptr;
}
progimage_info* allocateRGBImage(progimage_info* source_ptr)
{
// allocate output image and fill it up
progimage_info* destination_ptr = new progimage_info();
destination_ptr->width = source_ptr->width;
destination_ptr->height = source_ptr->height;
destination_ptr->image_data.resize(destination_ptr->width * destination_ptr->height * 3 * sizeof(unsigned char), 0x0);
return destination_ptr;
}
void copyOneChannel(progimage_info* dst, int channel, progimage_info* src, LodePNGColorType colortype)
{
int row, col;
dst->width = src->width;
dst->height = src->height;
unsigned char* dst_data = &dst->image_data[0];
unsigned char* src_data = &src->image_data[0] + channel;
int channelIncrement = (colortype == LCT_RGB) ? 3 : 4;
for (row = 0; row < src->height; row++)
{
for (col = 0; col < src->width; col++)
{
*dst_data++ = *src_data;
src_data += channelIncrement;
}
}
}
void convertToGrayscale(progimage_info* dst, progimage_info* src, LodePNGColorType colortype)
{
int row, col;
dst->width = src->width;
dst->height = src->height;
unsigned char* dst_data = &dst->image_data[0];
unsigned char* src_data = &src->image_data[0];
int channelIncrement = (colortype == LCT_RGB) ? 3 : 4;
for (row = 0; row < src->height; row++)
{
for (col = 0; col < src->width; col++)
{
// https://en.wikipedia.org/wiki/Grayscale#Luma_coding_in_video_systems
*dst_data++ = (unsigned char)((0.2126f * ((float)src_data[0] / 255.0f) + 0.7152f * ((float)src_data[0] / 255.0f) + 0.0722f * ((float)src_data[0] / 255.0f)) * 255.0f + 0.5f);
src_data += channelIncrement;
}
}
}
// to avoid defining boolean, etc., make this one return 1 if true, 0 if false
int channelEqualsValue(progimage_info* src, int channel, int numChannels, unsigned char value, int ignoreGrayscale)
{
// look at all data in given channel - all equal to the given value?
assert(numChannels > 0);
assert(channel < numChannels);
assert(ignoreGrayscale ? numChannels > 1 : 1);
int row, col;
unsigned char* src_data = &src->image_data[0] + channel;
for (row = 0; row < src->height; row++)
{
for (col = 0; col < src->width; col++)
{
if (*src_data != value)
{
// do grayscale test?
if (ignoreGrayscale) {
if ((src_data[-channel] == src_data[1 - channel]) && (src_data[1 - channel] == src_data[2 - channel])) {
// it's gray, so ignore it (could be a cutout background pixel)
src_data += numChannels;
continue;
}
}
return 0;
}
src_data += numChannels;
}
}
return 1;
}
void changeValueToValue(progimage_info* src, int channel, int numChannels, unsigned char value, unsigned char newValue)
{
// if value in channel is equal to input value, change it to the new value
assert(numChannels > 0);
assert(channel < numChannels);
int row, col;
unsigned char* src_data = &src->image_data[0] + channel;
for (row = 0; row < src->height; row++)
{
for (col = 0; col < src->width; col++)
{
if (*src_data == value)
{
*src_data = newValue;
}
src_data += numChannels;
}
}
}