-
Notifications
You must be signed in to change notification settings - Fork 0
/
PNGImageReader.cpp
115 lines (95 loc) · 3.76 KB
/
PNGImageReader.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
// Copyright 2012-2014 Nicolas Normand <[email protected]>
//
// This file is part of LUTBasedNSDistanceTransform.
//
// LUTBasedNSDistanceTransform is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// LUTBasedNSDistanceTransform is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// LUTBasedNSDistanceTransform. If not, see <http://www.gnu.org/licenses/>.
//
// $Id: PNGImageReader.cpp 150 2014-02-26 08:59:35Z Nicolas.Normand $
/*
* PNGImageReader.cpp
* LUTBasedNSDistanceTransform
*/
#include "PNGImageReader.h"
PNGImageReader::PNGImageReader(ImageConsumer<BinaryPixelType>* consumer, FILE *input) :
RowImageProducer<BinaryPixelType>(consumer),
_input(input) {
}
void
PNGImageReader::startImage(size_t readBytes)
{
_png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!_png_ptr) {
return;
}
_info_ptr = png_create_info_struct(_png_ptr);
if (!_info_ptr) {
png_destroy_read_struct(&_png_ptr, (png_infopp)NULL, (png_infopp)NULL);
return;
}
if (setjmp(png_jmpbuf(_png_ptr))) {
png_destroy_read_struct(&_png_ptr, &_info_ptr, (png_infopp)NULL);
fprintf(stderr, "Error during PNGImageReader::PNGImageReader\n");
exit(1);
}
png_init_io(_png_ptr, _input);
// Tell libpng we've already read 'readBytes' bytes
png_set_sig_bytes (_png_ptr, readBytes);
png_read_info(_png_ptr, _info_ptr);
int bit_depth, color_type, interlace_type;
png_get_IHDR(_png_ptr, _info_ptr, &_cols, &_rows, &bit_depth, &color_type,
&interlace_type, NULL, NULL);
/* Convert to grayscale */
if (color_type == PNG_COLOR_TYPE_RGB ||
color_type == PNG_COLOR_TYPE_RGB_ALPHA)
png_set_rgb_to_gray_fixed(_png_ptr, 1 /* error_action */,
-1 /* int red_weight */, -1 /* int green_weight */);
/* Drop alpha channel */
if (color_type & PNG_COLOR_MASK_ALPHA)
png_set_strip_alpha(_png_ptr);
/* Downscale 16 bits/pixel grayscale images to 8 bits/pixel */
if (bit_depth == 16)
png_set_strip_16(_png_ptr);
/* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand_gray_1_2_4_to_8(_png_ptr);
//png_set_invert_mono(_png_ptr);
png_read_update_info(_png_ptr, _info_ptr);
png_get_IHDR(_png_ptr, _info_ptr, &_cols, &_rows, &bit_depth, &color_type,
&interlace_type, NULL, NULL);
// Now, everything should be grayscale, 8 bit/pixel
assert(bit_depth == 8);
assert(color_type == PNG_COLOR_TYPE_GRAY);
//int number_of_passes = png_set_interlace_handling(_png_ptr);
_inputRow = (BinaryPixelType *)malloc(png_get_rowbytes(_png_ptr, _info_ptr));
_consumer->beginOfImage(_cols, _rows);
}
void PNGImageReader::produceAllRows(size_t readBytes) {
startImage(readBytes);
if (setjmp(png_jmpbuf(_png_ptr))) {
png_destroy_read_struct(&_png_ptr, &_info_ptr, (png_infopp)NULL);
fprintf(stderr, "Error during PNGImageReader::produceAllRows\n");
exit(1);
}
while (_rows-- > 0) {
png_read_row(_png_ptr, (png_bytep)_inputRow, NULL);
for (unsigned int col = 0; col < _cols; col++) {
_inputRow[col] = _inputRow[col] > 127 ? BINARY_WHITE_PIXEL : BINARY_BLACK_PIXEL;
}
_consumer->processRow(_inputRow);
}
_consumer->endOfImage();
free(_inputRow);
_inputRow = NULL;
png_read_end(_png_ptr, NULL);
}