-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathencoder.cpp
283 lines (240 loc) · 6.58 KB
/
encoder.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
// Aseprite TGA Library
// Copyright (C) 2020 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "tga.h"
#include <algorithm>
#include <cassert>
namespace tga {
template<typename T>
inline color_t get_pixel(Image& image, int x, int y) {
return *(T*)(image.pixels + y*image.rowstride + x*image.bytesPerPixel);
}
Encoder::Encoder(FileInterface* file)
: m_file(file)
{
}
void Encoder::writeHeader(const Header& header)
{
write8(header.idLength);
write8(header.colormapType);
write8(header.imageType);
write16(header.colormapOrigin);
write16(header.colormapLength);
write8(header.colormapDepth);
write16(header.xOrigin);
write16(header.yOrigin);
write16(header.width);
write16(header.height);
write8(header.bitsPerPixel);
write8(header.imageDescriptor);
m_hasAlpha = (header.bitsPerPixel == 16 ||
header.bitsPerPixel == 32);
assert(header.colormapLength == header.colormap.size());
// Write colormap
if (header.colormapType == 1) {
for (int i=0; i<header.colormap.size(); ++i) {
color_t c = header.colormap[i];
switch (header.colormapDepth) {
case 15:
case 16: write16Rgb(c); break;
case 24: write24Rgb(c); break;
case 32: write32Rgb(c); break;
}
}
}
else {
assert(header.colormapLength == 0);
}
}
void Encoder::writeFooter()
{
const char* tga2_footer = "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.\0";
for (int i=0; i<26; ++i)
write8(tga2_footer[i]);
}
void Encoder::writeImage(const Header& header,
const Image& image,
Delegate* delegate)
{
const int w = header.width;
const int h = header.height;
m_iterator = details::ImageIterator(header, const_cast<Image&>(image));
switch (header.imageType) {
case tga::UncompressedIndexed:
case tga::UncompressedGray:
for (int y=0; y<h; ++y) {
for (int x=0; x<w; ++x)
write8(m_iterator.getPixel<uint8_t>());
if (delegate && !delegate->notifyProgress(float(y) / float(h)))
return;
}
break;
case tga::RleIndexed:
case tga::RleGray:
for (int y=0; y<h; ++y) {
writeRleScanline<uint8_t>(w, image, &Encoder::write8Color);
if (delegate && !delegate->notifyProgress(float(y) / float(h)))
return;
}
break;
case tga::UncompressedRgb: {
switch (header.bitsPerPixel) {
case 15:
case 16:
for (int y=0; y<h; ++y) {
for (int x=0; x<w; ++x)
write16Rgb(m_iterator.getPixel<uint32_t>());
if (delegate && !delegate->notifyProgress(float(y) / float(h)))
return;
}
break;
case 24:
for (int y=0; y<h; ++y) {
for (int x=0; x<w; ++x)
write24Rgb(m_iterator.getPixel<uint32_t>());
if (delegate && !delegate->notifyProgress(float(y) / float(h)))
return;
}
break;
case 32:
for (int y=0; y<h; ++y) {
for (int x=0; x<w; ++x)
write32Rgb(m_iterator.getPixel<uint32_t>());
if (delegate && !delegate->notifyProgress(float(y) / float(h)))
return;
}
break;
}
}
break;
case tga::RleRgb:
for (int y=0; y<h; ++y) {
switch (header.bitsPerPixel) {
case 15:
case 16: writeRleScanline<uint32_t>(w, image, &Encoder::write16Rgb); break;
case 24: writeRleScanline<uint32_t>(w, image, &Encoder::write24Rgb); break;
case 32: writeRleScanline<uint32_t>(w, image, &Encoder::write32Rgb); break;
default:
assert(false);
return;
}
if (delegate && !delegate->notifyProgress(float(y) / float(h)))
return;
}
break;
}
}
template<typename T>
void Encoder::writeRleScanline(const int w,
const Image& image,
void (Encoder::*writePixel)(color_t))
{
int x = 0;
while (x < w) {
int count, offset;
countRepeatedPixels<T>(w, image, x, offset, count);
// Save a sequence of pixels with different colors
while (offset > 0) {
const int n = std::min(offset, 128);
assert(n >= 1 && n <= 128);
write8(static_cast<uint8_t>(n - 1));
for (int i=0; i<n; ++i) {
const color_t c = m_iterator.getPixel<T>();
(this->*writePixel)(c);
}
offset -= n;
x += n;
}
// Save a sequence of pixels with the same color
while (count*image.bytesPerPixel > 1+image.bytesPerPixel) {
const int n = std::min(count, 128);
const color_t c = m_iterator.getPixel<T>();
for (int i=1; i<n; ++i) {
#ifndef NDEBUG
const color_t c2 =
#endif
m_iterator.getPixel<T>();
assert(c == c2);
}
assert(n >= 1 && n <= 128);
write8(0x80 | static_cast<uint8_t>(n - 1));
(this->*writePixel)(c);
count -= n;
x += n;
}
}
assert(x == w);
}
template<typename T>
void Encoder::countRepeatedPixels(const int w,
const Image& image, int x0,
int& offset, int& count)
{
auto it = m_iterator;
for (int x=x0; x<w; ) {
const color_t p = it.getPixel<T>();
int u = x+1;
auto next_it = it;
for (; u<w; ++u) {
const color_t q = it.getPixel<T>();
if (p != q)
break;
}
if ((u - x)*image.bytesPerPixel > 1+image.bytesPerPixel) {
offset = x - x0;
count = u - x;
return;
}
++x;
it = next_it;
}
offset = w - x0;
count = 0;
}
void Encoder::write8(uint8_t value)
{
m_file->write8(value);
}
void Encoder::write16(uint16_t value)
{
// Little endian
m_file->write8(value & 0x00FF);
m_file->write8((value & 0xFF00) >> 8);
}
void Encoder::write32(uint32_t value)
{
// Little endian
m_file->write8(value & 0xFF);
m_file->write8((value >> 8) & 0xFF);
m_file->write8((value >> 16) & 0xFF);
m_file->write8((value >> 24) & 0xFF);
}
void Encoder::write16Rgb(color_t c)
{
const uint8_t r = getr(c);
const uint8_t g = getg(c);
const uint8_t b = getb(c);
const uint8_t a = geta(c);
const uint16_t v =
((r>>3) << 10) |
((g>>3) << 5) |
((b>>3)) |
(m_hasAlpha && a >= 128 ? 0x8000: 0); // TODO configurable threshold
write16(v);
}
void Encoder::write24Rgb(color_t c)
{
write8(getb(c));
write8(getg(c));
write8(getr(c));
}
void Encoder::write32Rgb(color_t c)
{
write8(getb(c));
write8(getg(c));
write8(getr(c));
write8(geta(c));
}
} // namespace tga