-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_image.cpp
297 lines (263 loc) · 8.99 KB
/
save_image.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/***************************************************************************
* Copyright (C) 2020 by Nuno Cardoso *
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/*
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <limits>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <map>
#include <vector>
#include <cmath>
#include <float.h>*/
#include <cstdio>
#include <string>
#include <sstream>
#include <png.h>
#include <zlib.h>
#include <cstdlib>
#include <stdint.h>
#include <limits>
#include <FL/Fl.H>
#include <FL/Fl_Widget.H>
#include <FL/Fl_RGB_Image.H>
#include <FL/fl_draw.H>
#include "log.h"
#include "save_image.h"
typedef uint8_t size8_t;
typedef uint16_t size16_t;
typedef uint32_t size32_t;
typedef uint64_t size64_t;
const size32_t NULL_INDEX = std::numeric_limits<size32_t>::max();
using namespace std;
const char *Image::FILE_CHOOSER_FILTER = "Portable Network Graphics File\t*.png\nBitmap File\t*.bmp\n"
"Truevision TGA File\t*.tga\nPortable Pixmap File\t*.ppm\n";
Image::Image(const char *f, size_t w, size_t h, Fl_Widget *wgt) : _file(NULL), _buffer(NULL), _width(w), _height(h), _error(0) {
// Images require a writable file
_file = fl_fopen(f, "wb");
if (_file == NULL) { return; }
_buffer = new(std::nothrow) uchar[3 * w * h];
if (_buffer == NULL) { return; }
if (wgt) {
fl_read_image(_buffer, wgt->x(), wgt->y(), wgt->w(), wgt->h());
// Flip buffer upside-down
uchar *temp_row = new(std::nothrow) uchar[3 * w];
if (temp_row == NULL) { return; }
for (size_t i = 0; i < h / 2; i++) {
uchar *top_row = &_buffer[3 * w * i];
uchar *bottom_row = &_buffer[3 * w * (h - i - 1)];
memcpy(temp_row, top_row, 3 * w);
memcpy(top_row, bottom_row, 3 * w);
memcpy(bottom_row, temp_row, 3 * w);
}
delete [] temp_row;
}
else {
//Uncomment if using OpenGL
// Fill the pixel buffer from the OpenGL context, in row-major order from bottom to top
//glPixelStorei(GL_PACK_ALIGNMENT, 1);
//glReadPixels(0, 0, (GLsizei)w, (GLsizei)h, GL_RGB, GL_UNSIGNED_BYTE, _buffer);
}
}
Image::~Image() {
if (_file) { fclose(_file); }
delete _buffer;
}
size_t Image::write(Image::Format f) {
if (error()) { return 0; }
switch (f) {
case PNG:
default:
return write_png();
case BMP:
return write_bmp();
case TGA:
return write_tga();
case PPM:
return write_ppm();
}
}
size_t Image::write_png() {
size_t n = 0;
// Create the necessary PNG structures
png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png == NULL) {
_error = 1;
return n;
}
png_infop info = png_create_info_struct(png);
if (info == NULL) {
_error = 1;
png_destroy_write_struct(&png, (png_infopp)NULL);
return n;
}
png_init_io(png, _file);
// Set compression options
png_set_compression_level(png, Z_BEST_COMPRESSION);
png_set_compression_mem_level(png, Z_BEST_COMPRESSION);
png_set_compression_strategy(png, Z_DEFAULT_STRATEGY);
png_set_compression_window_bits(png, 15);
png_set_compression_method(png, Z_DEFLATED);
png_set_compression_buffer_size(png, 8192);
// Write the PNG IHDR chunk
png_set_IHDR(png, info, (png_uint_32)_width, (png_uint_32)_height, 8, PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
// Write the other PNG header chunks
png_write_info(png, info);
// Write the RGB pixels in row-major order from top to bottom
size_t row_size = 3 * _width;
png_bytep png_row = new(std::nothrow) png_byte[row_size];
if (png_row) {
for (size_t i = _height; i-- > 0;) {
size_t row = row_size * i;
for (size_t j = 0; j < _width; j++) {
size_t col = 3 * j;
size_t px = row + col;
// False positive "C6386: Write overrun" error with Visual Studio 2013 code analysis
png_row[col] = _buffer[px];
png_row[col+1] = _buffer[px+1];
png_row[col+2] = _buffer[px+2];
n += 3;
}
png_write_row(png, png_row);
}
png_write_end(png, NULL);
}
delete [] png_row;
png_destroy_write_struct(&png, &info);
png_free_data(png, info, PNG_FREE_ALL, -1);
return n;
}
size_t Image::write_bmp() {
size_t n = 0;
size_t file_header_size = 14;
size_t info_header_size = 40;
size_t header_size = file_header_size + info_header_size;
size_t row_size = 3 * _width;
size_t row_pad = 4 - row_size % 4; // align rows to 32-bit boundaries
if (row_pad == 4) { row_pad = 0; }
size_t data_size = (row_size + row_pad) * _height;
size_t data_pad = 4 - data_size % 4;
if (data_pad == 4) { data_pad = 0; }
size_t image_size = data_size + data_pad;
size_t file_size = header_size + image_size;
float x_dpi, y_dpi;
Fl::screen_dpi(x_dpi, y_dpi);
size32_t x_ppm = (size32_t)(x_dpi * INCHES_PER_METER);
size32_t y_ppm = (size32_t)(y_dpi * INCHES_PER_METER);
uchar file_header[14] = {
'B', 'M', // magic number
LE32(file_size), // file size
LE16(0), // reserved 1 (unused)
LE16(0), // reserved 2 (unused)
LE32(header_size) // header size
};
uchar info_header[40] = {
LE32(info_header_size), // info header size
LE32(_width), // image width
LE32(_height), // image height
LE16(1), // num color planes
LE16(24), // bits per pixel
LE32(0), // compression method (RGB)
LE32(image_size), // image size in bytes
LE32(x_ppm), // horizontal pixels per meter
LE32(y_ppm), // vertical pixels per meter
LE32(0), // num colors (ignored)
LE32(0) // num important colors (ignored)
};
// Write the BMP file header
n += fwrite(&file_header, sizeof(file_header), 1, _file);
// Write the BMP info header
n += fwrite(&info_header, sizeof(info_header), 1, _file);
// Write the BGR pixels in row-major order from bottom to top
for (size_t i = 0; i < _height; i++) {
size_t row = row_size * i;
for (size_t j = 0; j < _width; j++) {
size_t px = row + 3 * j;
fputc(_buffer[px+2], _file); // blue
fputc(_buffer[px+1], _file); // green
fputc(_buffer[px], _file); // red
}
// Pad the rows to the nearest 4 bytes
for (size_t j = 0; j < row_pad; j++) {
fputc(0, _file);
}
}
// Pad the pixel data to the nearest 4 bytes
for (size_t i = 0; i < data_pad; i++) {
fputc(0, _file);
}
n += image_size;
return n;
}
size_t Image::write_tga() {
size_t n = 0;
uchar file_header[18] = {
0, // ID field size
0, // color map type (none)
2, // image type (RGB)
LE16(0), // color map offset
LE16(0), // color map size
24, // color map bits per pixel
LE16(0), // image x origin
LE16(0), // image y origin
LE16(_width), // image width
LE16(_height), // image height
24, // bits per pixel
0 // image descriptor
};
// Write the TGA header
n += fwrite(&file_header, sizeof(file_header), 1, _file);
// Write the BGR pixels in row-major order from bottom to top
size_t row_size = 3 * _width;
for (size_t i = 0; i < _height; i++) {
size_t row = row_size * i;
for (size_t j = 0; j < _width; j++) {
size_t px = row + 3 * j;
fputc(_buffer[px+2], _file); // blue
fputc(_buffer[px+1], _file); // green
fputc(_buffer[px], _file); // red
}
}
size_t data_size = row_size * _height;
n += data_size;
return n;
}
size_t Image::write_ppm() {
size_t n = 0;
// Write the PPM header
std::ostringstream ss;
ss << "P6 " << _width << " " << _height << " 255 ";
std::string s = ss.str();
size_t file_header_size = s.size();
const char *file_header = s.c_str();
n += fwrite(file_header, file_header_size, 1, _file);
// Write the RGB pixels in row-major order from top to bottom
size_t row_size = 3 * _width;
for (size_t i = _height; i-- > 0;) {
size_t row = row_size * i;
n += fwrite(_buffer + row, row_size, 1, _file);
}
return n;
}