forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_io.cpp
217 lines (177 loc) · 6.16 KB
/
image_io.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
// Aseprite Document Library
// Copyright (c) 2019-2024 Igara Studio S.A.
// Copyright (c) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "doc/image_io.h"
#include "base/exception.h"
#include "base/serialization.h"
#include "doc/cancel_io.h"
#include "doc/image.h"
#include "zlib.h"
#include <algorithm>
#include <iostream>
#include <memory>
namespace doc {
using namespace base::serialization;
using namespace base::serialization::little_endian;
// TODO Create a zlib wrapper for iostreams
bool write_image(std::ostream& os, const Image* image, CancelIO* cancel)
{
write32(os, image->id());
write8(os, image->pixelFormat()); // Pixel format
write16(os, image->width()); // Width
write16(os, image->height()); // Height
write32(os, image->maskColor()); // Mask color
// Number of bytes for visible pixels on each row
const int widthBytes = image->widthBytes();
#if 0
{
for (int c=0; c<image->height(); c++)
os.write((char*)image->getPixelAddress(0, c), widthBytes);
}
#else
{
std::ostream::pos_type total_output_pos = os.tellp();
write32(os, 0); // Compressed size (we update this value later)
z_stream zstream;
zstream.zalloc = (alloc_func)0;
zstream.zfree = (free_func)0;
zstream.opaque = (voidpf)0;
int err = deflateInit(&zstream, Z_DEFAULT_COMPRESSION);
if (err != Z_OK)
throw base::Exception("ZLib error %d in deflateInit().", err);
std::vector<uint8_t> compressed(4096);
int total_output_bytes = 0;
for (int y=0; y<image->height(); y++) {
if (cancel && cancel->isCanceled()) {
deflateEnd(&zstream);
return false;
}
zstream.next_in = (Bytef*)image->getPixelAddress(0, y);
zstream.avail_in = widthBytes;
int flush = (y == image->height()-1 ? Z_FINISH: Z_NO_FLUSH);
do {
zstream.next_out = (Bytef*)&compressed[0];
zstream.avail_out = compressed.size();
// Compress
err = deflate(&zstream, flush);
if (err != Z_OK && err != Z_STREAM_END && err != Z_BUF_ERROR)
throw base::Exception("ZLib error %d in deflate().", err);
int output_bytes = compressed.size() - zstream.avail_out;
if (output_bytes > 0) {
if (os.write((char*)&compressed[0], output_bytes).fail())
throw base::Exception("Error writing compressed image pixels.\n");
total_output_bytes += output_bytes;
}
} while (zstream.avail_out == 0);
}
err = deflateEnd(&zstream);
if (err != Z_OK)
throw base::Exception("ZLib error %d in deflateEnd().", err);
std::ostream::pos_type bak = os.tellp();
os.seekp(total_output_pos);
write32(os, total_output_bytes);
os.seekp(bak);
}
#endif
return true;
}
Image* read_image(std::istream& is, const bool setId)
{
ObjectId id = read32(is);
int pixelFormat = read8(is); // Pixel format
int width = read16(is); // Width
int height = read16(is); // Height
uint32_t maskColor = read32(is); // Mask color
if ((pixelFormat != IMAGE_RGB &&
pixelFormat != IMAGE_GRAYSCALE &&
pixelFormat != IMAGE_INDEXED &&
pixelFormat != IMAGE_BITMAP &&
pixelFormat != IMAGE_TILEMAP) ||
(width < 1 || height < 1) ||
(width > 0xfffff || height > 0xfffff))
return nullptr;
std::unique_ptr<Image> image(
Image::create(static_cast<PixelFormat>(pixelFormat), width, height));
const int widthBytes = image->widthBytes();
#if 0
{
for (int c=0; c<image->height(); c++)
is.read((char*)image->getPixelAddress(0, c), widthBytes);
}
#else
{
int avail_bytes = read32(is);
z_stream zstream;
zstream.zalloc = (alloc_func)0;
zstream.zfree = (free_func)0;
zstream.opaque = (voidpf)0;
int err = inflateInit(&zstream);
if (err != Z_OK)
throw base::Exception("ZLib error %d in inflateInit().", err);
int remain = avail_bytes;
std::vector<uint8_t> compressed(4096);
int y = 0;
uint8_t* address = nullptr;
uint8_t* address_end = nullptr;
while (remain > 0) {
int len = std::min(remain, int(compressed.size()));
if (is.read((char*)&compressed[0], len).fail()) {
ASSERT(false);
throw base::Exception("Error reading stream to restore image");
}
int bytes_read = (int)is.gcount();
if (bytes_read == 0) {
ASSERT(remain == 0);
break;
}
remain -= bytes_read;
zstream.next_in = (Bytef*)&compressed[0];
zstream.avail_in = (uInt)bytes_read;
do {
if (address == address_end) {
if (y < image->height()) {
address = image->getPixelAddress(0, y++);
address_end = address + widthBytes;
}
else {
// Special reported case where we just fill the whole
// output image buffer (avail_out == 0), and more input
// was previously reported as available (avail_in != 0).
//
// Not sure why zlib reports this in certain cases, where
// avail_in != 0 and err == Z_OK instead of err ==
// Z_STREAM_END and we have to do a final inflate() call
// (even w/avail_out=0) to get the final Z_STREAM_END
// result.
ASSERT(y == image->height());
ASSERT(err == Z_OK);
}
}
zstream.next_out = (Bytef*)address;
zstream.avail_out = address_end - address;
err = inflate(&zstream, Z_NO_FLUSH);
if (err != Z_OK && err != Z_STREAM_END && err != Z_BUF_ERROR)
throw base::Exception("ZLib error %d in inflate().", err);
int uncompressed_bytes = (int)((address_end - address) - zstream.avail_out);
if (uncompressed_bytes > 0) {
address += uncompressed_bytes;
}
} while (zstream.avail_in != 0 && zstream.avail_out == 0);
}
err = inflateEnd(&zstream);
if (err != Z_OK)
throw base::Exception("ZLib error %d in inflateEnd().", err);
}
#endif
image->setMaskColor(maskColor);
if (setId)
image->setId(id);
return image.release();
}
}