forked from tvspelsfreak/texconv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvpal.cpp
More file actions
457 lines (384 loc) · 15 KB
/
Copy pathconvpal.cpp
File metadata and controls
457 lines (384 loc) · 15 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include "imagecontainer.h"
#include "twiddler.h"
#include "palette.h"
#include "vqtools.h"
#include "common.h"
#include <iostream>
#include <vector>
#include <cstring>
static void vectorizeARGB(const ImageContainer& images, std::vector<Vec<4>>& vectors) {
for ( int i = 0; i < images.imageCount(); i++ ) {
const Image& img = images.getByIndex(i);
for ( int y = 0; y < img.height(); y++ ) {
for ( int x=0; x < img.width(); x++ ) {
RGBA px = img.pixel(x,y);
Vec<4> vec;
vec[0] = px.a/255.f;
vec[1] = px.r/255.f;
vec[2] = px.g/255.f;
vec[3] = px.b/255.f;
vectors.push_back(vec);
}
}
}
}
static void devectorizeARGB(const ImageContainer& srcImages, const std::vector<Vec<4>>& vectors, const VectorQuantizer<4>& vq, std::vector<Image>& indexedImages, Palette& palette) {
int vindex = 0;
for ( int i = 0; i < srcImages.imageCount(); i++ ) {
const Image& src = srcImages.getByIndex(i);
Image dst( src.width(), src.height() );
dst.allocateIndexed(256);
for ( int y = 0; y < src.height(); y++ ) {
for ( int x = 0; x < src.width(); x++ ) {
const Vec<4>& vec = vectors[vindex++];
int codeIndex = vq.findClosest(vec);
dst.setIndexedPixel( x, y, (uint8_t) codeIndex );
}
}
indexedImages.push_back(dst);
}
for ( int i = 0; i < vq.codeCount(); i++ ) {
const Vec<4>& v = vq.codeVector(i);
uint32_t color = (uint8_t)( v[0]*255)<<24 | (uint8_t)(v[1]*255)<<16 |
(uint8_t)( v[2]*255)<<8 | (uint8_t)(v[3]*255);
palette.insert(color);
}
}
void convertToIndexedImages(const ImageContainer& src, const Palette& pal, std::vector<Image>& dst);
void writeUncompressed4BPPData(std::ostream& stream, const std::vector<Image>& indexedImages);
void writeUncompressed8BPPData(std::ostream& stream, const std::vector<Image>& indexedImages);
void writeUncompressedPreview(const std::string& filename, const std::vector<Image>& indexedImages, const Palette& palette);
void writeCompressed4BPPData(std::ostream& stream, const std::vector<Image>& indexedImages, const Palette& palette);
void writeCompressed8BPPData(std::ostream& stream, const std::vector<Image>& indexedImages, const Palette& palette);
/*
* This conversion basically has three modes:
*
* 1. The source images contain <= unique colors than the requested mode
* needs, so conversion will be quick and lossless.
*
* 2. The source images contain > unique colors than the requested mode
* needs. In this case we utilize vector quantization to reduce the
* color count.
*
* 3. The user has requested for the image to be compressed. This is a two
* stage process. First, reduce the input images to the color count needed.
* Then, using the reduced images as input, perform vector quantization
* with a vector dimension of 32 or 64 (2x4 or 4x4 pixel blocks).
*/
void convertPaletted(std::ostream& stream, const ImageContainer& images, int textureType, const std::string& paletteFilename) {
const int maxColors = isFormat(textureType, PIXELFORMAT_PAL4BPP) ? 16 : 256;
Palette palette(images);
std::vector<Image> indexedImages;
//qDebug("Palette contains %d colors", palette.colorCount());
if (palette.colorCount() > maxColors) {
// The palette has too many colors, so perform a vector quantization to reduce
// the color count down to what we need.
//qDebug("Reducing palette to %d colors", maxColors);
palette.clear();
VectorQuantizer<4> vq;
std::vector<Vec<4>> vectors;
vectorizeARGB(images, vectors);
vq.compress(vectors, maxColors);
devectorizeARGB(images, vectors, vq, indexedImages, palette);
} else {
// Convert the input images to indexed images so we can use the same output code
// as the reduced color images.
convertToIndexedImages(images, palette, indexedImages);
}
// The palette is finished now, so save it.
palette.save(paletteFilename);
// Write data
if (textureType & FLAG_COMPRESSED) {
if (isFormat(textureType, PIXELFORMAT_PAL4BPP))
writeCompressed4BPPData(stream, indexedImages, palette);
if (isFormat(textureType, PIXELFORMAT_PAL8BPP))
writeCompressed8BPPData(stream, indexedImages, palette);
} else {
if (isFormat(textureType, PIXELFORMAT_PAL4BPP))
writeUncompressed4BPPData(stream, indexedImages);
if (isFormat(textureType, PIXELFORMAT_PAL8BPP))
writeUncompressed8BPPData(stream, indexedImages);
}
}
// Converts the src images to indexed images.
// The indexed images are sorted from smallest to largest.
void convertToIndexedImages(const ImageContainer& src, const Palette& pal, std::vector<Image>& dst) {
for (int i=0; i<src.imageCount(); i++) {
const Image& img = src.getByIndex(i);
Image dstImg(img.width(), img.height()/*, Image::Format_ARGB32*/);
for (int y=0; y<img.height(); y++)
for (int x=0; x<img.width(); x++) {
RGBA px = img.pixel(x,y);
uint32_t argb = packColor(px);
uint8_t index = (uint8_t) pal.indexOf(argb);
dstImg.setIndexedPixel( x, y, index );
}
dst.push_back(dstImg);
}
}
void writeUncompressed4BPPData(std::ostream& stream, const std::vector<Image>& indexedImages) {
// Write mipmap offset if necessary
if (indexedImages.size() > 1)
writeZeroes(stream, MIPMAP_OFFSET_4BPP);
// Write all mipmaps from smallest to largest
for (int i=0; i<indexedImages.size(); i++) {
const Image& img = indexedImages[i];
// Special case. There's only one pixel in the 1x1 mipmap level,
// but it's stored by itself in one byte.
if (img.width() == 1) {
uint8_t val = img.indexedPixelAt(0,0);
stream.write( (char*) &val, 1 );
continue;
}
Twiddler twiddler(img.width(), img.height());
const int pixels = img.width() * img.height();
// Write all pixels in pairs
// First pixel in the least significant nibble.
// Second pixel in the most significant nibble.
for (int j=0; j<pixels; j+=2) {
uint8_t palindex[2];
for (int k=0; k<2; k++) {
const int index = twiddler.index(j + k);
const int x = index % img.width();
const int y = index / img.width();
palindex[k] = (uint8_t) img.indexedPixelAt(x, y);
}
uint8_t packed = (((palindex[1] & 0xF) << 4) | (palindex[0] & 0xF));
stream.write( (char*) &packed, 1 );
}
}
}
void writeUncompressed8BPPData(std::ostream& stream, const std::vector<Image>& indexedImages) {
// Write mipmap offset if necessary
if (indexedImages.size() > 1)
writeZeroes(stream, MIPMAP_OFFSET_8BPP);
// Write all mipmaps from smallest to largest
for (int i=0; i<indexedImages.size(); i++) {
const Image& img = indexedImages[i];
Twiddler twiddler(img.width(), img.height());
const int pixels = img.width() * img.height();
for (int j=0; j<pixels; j++) {
const int index = twiddler.index(j);
const int x = index % img.width();
const int y = index / img.width();
uint8_t value = img.indexedPixelAt(x, y);
stream.write( (char*) &value, 1 );
}
}
}
#define STORE_FULL 0 // Store the block in a full 32D vector
#define STORE_LEFT 1 // Store the block in the left half of a 64D vector
#define STORE_RIGHT 2 // Store the block in the right half of a 64D vector
template<uint N>
static void grab2x4Block(const Image& img, const Palette& pal, const int x, const int y, Vec<N>& vec, const uint storeMethod) {
static const int indexLUT[3][8] = {
{ 0, 4, 8, 12, 16, 20, 24, 28 }, // Full 32D vector
{ 0, 4, 16, 20, 32, 36, 48, 52 }, // Left half of 64D vector
{ 8, 12, 24, 28, 40, 44, 56, 60 } // Right half of 64D vector
};
int index = 0;
uint hash = vec.hash();
for (int yy=y; yy<(y+4); yy++) {
for (int xx=x; xx<(x+2); xx++) {
uint32_t pixel = pal.colorAt(img.indexedPixelAt(xx, yy));
argb2vec(pixel, vec, indexLUT[storeMethod][index]);
RGBA color = unpackColor( pixel );
hash = combineHash(color, hash);
index++;
}
}
vec.setHash(hash);
}
static void vectorizePalette(const Palette& pal, std::vector<Vec<4>>& vectors) {
for (int i=0; i<pal.colorCount(); i++) {
Vec<4> vec;
argb2vec(pal.colorAt(i), vec);
vectors.push_back(vec);
}
}
static uint8_t findClosest(const std::vector<Vec<4>>& vectors, const Vec<4>& vec) {
uint8_t closestIndex = 0;
float closestDistance = Vec<4>::distanceSquared(vectors[0], vec);
for (int i=1; i<vectors.size(); i++) {
float distance = Vec<4>::distanceSquared(vectors[i], vec);
if (distance < closestDistance) {
closestIndex = (uint8_t)i;
closestDistance = distance;
}
}
return closestIndex;
}
void writeCompressed4BPPData(std::ostream& stream, const std::vector<Image>& indexedImages, const Palette& palette) {
VectorQuantizer<64> vq;
std::vector<Vec<64>> vectors;
// Vectorize the input images.
// Each vector represents a pair of 2x4 pixel blocks. For single images, it's
// easy since we can just grab a number of 4x4 blocks straight from the source
// image. It's a bit more complicated for mipmapped images though. They're
// essentially aligned on a nibble boundary so a single vector represents the
// second half of the 4x4 pixel block at twiddledIndex[n] as well as the first
// half of the 4x4 pixel block at twiddledIndex[n+1]. This makes the mipmapped
// vectorization code a lot more complex.
if (indexedImages.size() > 1) {
Vec<64> vec(0);
for (int i=0; i<indexedImages.size(); i++) {
const Image& img = indexedImages[i];
// Ignore images smaller than this
if (img.width() < MIN_MIPMAP_PALVQ || img.height() < MIN_MIPMAP_PALVQ)
continue;
const int imgw = img.width();
const int imgh = img.height();
const int blocks = (imgw * imgh) / 16;
const Twiddler twiddler(imgw / 4, imgh / 4);
for (int j=0; j<blocks; j++) {
const int twidx = twiddler.index(j);
const int x = (twidx % (imgw / 4)) * 4;
const int y = (twidx / (imgw / 4)) * 4;
// If this is the first vector we're processing, the first
// half of it will be empty. So instead of leaving it empty
// and potentially mess up the encoding by introducing colors that
// don't exist in the image, we copy the second half of the vector
// to the first half.
if (vectors.empty()) {
grab2x4Block(img, palette, x, y, vec, STORE_LEFT);
}
// First half of this block is the second half of the
// vector we're currently creating.
grab2x4Block(img, palette, x, y, vec, STORE_RIGHT);
// This vector is done now, so flush it and remember to
// clear the hash for the next vector.
vectors.push_back(vec);
vec.setHash(0);
// Second half of this block is the first half of the next
// vector we're creating.
grab2x4Block(img, palette, x + 2, y, vec, STORE_LEFT);
// If this is the last block of the last image, remember to
// fill the current vector with something good and flush it.
if ((i == (indexedImages.size() - 1)) && (j == (blocks - 1))) {
grab2x4Block(img, palette, x + 2, y, vec, STORE_RIGHT);
vectors.push_back(vec);
}
}
}
} else {
// There's only one image, and it's on a byte boundary, so this
// is simple. Twiddle the data here though, since the mipmapped
// vectors need to be twiddled, so the same code can be used to
// devectorize this as well as mipmapped stuff.
const Image& img = indexedImages[0];
const int imgw = img.width();
const int imgh = img.height();
const int blocks = (imgw * imgh) / 16;
const Twiddler twiddler(imgw / 4, imgh / 4);
for (int j=0; j<blocks; j++) {
const int twidx = twiddler.index(j);
const int x = (twidx % (imgw / 4)) * 4;
const int y = (twidx / (imgw / 4)) * 4;
Vec<64> vec(0);
grab2x4Block(img, palette, x + 0, y, vec, STORE_LEFT);
grab2x4Block(img, palette, x + 2, y, vec, STORE_RIGHT);
vectors.push_back(vec);
}
}
vq.compress(vectors, 256);
// The palette needs to be in a vector format for the next part,
// since we need to be able to perform searches in it.
std::vector<Vec<4>> vectorizedPalette;
vectorizePalette(palette, vectorizedPalette);
// Build the codebook
uint8_t codebook[2048];
memset(codebook, 0, 2048);
const Twiddler nibbleLUT(4, 4);
for (int i=0; i<vq.codeCount(); i++) {
const Vec<64>& vec = vq.codeVector(i);
for (int j=0; j<16; j++) {
Vec<4> color;
color.set(0, vec[nibbleLUT.index(j) * 4 + 0]);
color.set(1, vec[nibbleLUT.index(j) * 4 + 1]);
color.set(2, vec[nibbleLUT.index(j) * 4 + 2]);
color.set(3, vec[nibbleLUT.index(j) * 4 + 3]);
// Search the vectorized palette for the closest index
uint8_t closestIndex = findClosest(vectorizedPalette, color);
const int byte = j / 2;
const int nibble = j % 2;
if (nibble == 1)
codebook[i*8+byte] |= ((closestIndex & 0xF) << 4);
else
codebook[i*8+byte] |= (closestIndex & 0xF);
}
}
// Write the codebook
stream.write( (char*) codebook, 2048 );
// Don't write out a zero for the 1x1 mipmap like we would usually
// do for mipmapped VQ textures. The reason for this is that it's
// represented by a single nibble in PAL4BPPVQMM textures. And that
// nibble is part of the first index byte, which will be written next.
//if (indexedImages.size() > 1)
// writeZeroes(stream, 1);
// Write the index data
for (int i=0; i<vectors.size(); i++) {
const Vec<64>& srcvec = vectors.at(i);
const int c = vq.findClosest(srcvec);
stream << (uint8_t)c;
}
}
void writeCompressed8BPPData(std::ostream& stream, const std::vector<Image>& indexedImages, const Palette& palette) {
VectorQuantizer<32> vq;
std::vector<Vec<32>> vectors;
// Vectorize the input images.
// Each vector represents a 2x4 pixel block.
// Grab the data as twiddled, it's simpler than twiddling it
// when we write it to file.
for (int i=0; i<indexedImages.size(); i++) {
const Image& img = indexedImages[i];
// Ignore images smaller than this
if (img.width() < MIN_MIPMAP_PALVQ || img.height() < MIN_MIPMAP_PALVQ)
continue;
const int imgw = img.width();
const int imgh = img.height();
const int blocks = (imgw * imgh) / 16;
const Twiddler twiddler(imgw / 4, imgh / 4);
for (int j=0; j<blocks; j++) {
const int twidx = twiddler.index(j);
const int x = (twidx % (imgw / 4)) * 4;
const int y = (twidx / (imgw / 4)) * 4;
Vec<32> vec;
grab2x4Block(img, palette, x + 0, y, vec, STORE_FULL);
vectors.push_back(vec);
grab2x4Block(img, palette, x + 2, y, vec, STORE_FULL);
vectors.push_back(vec);
}
}
vq.compress(vectors, 256);
// The palette needs to be in a vector format for the next part,
// since we need to be able to perform searches in it.
std::vector<Vec<4>> vectorizedPalette;
vectorizePalette(palette, vectorizedPalette);
// Build the codebook
uint8_t codebook[2048];
memset(codebook, 0, 2048);
const Twiddler nibbleLUT(2, 4);
for (int i=0; i<vq.codeCount(); i++) {
const Vec<32>& vec = vq.codeVector(i);
for (int j=0; j<8; j++) {
Vec<4> color;
color.set(0, vec[nibbleLUT.index(j) * 4 + 0]);
color.set(1, vec[nibbleLUT.index(j) * 4 + 1]);
color.set(2, vec[nibbleLUT.index(j) * 4 + 2]);
color.set(3, vec[nibbleLUT.index(j) * 4 + 3]);
// Search the palette for the closest index
codebook[i * 8 + j] = findClosest(vectorizedPalette, color);
}
}
// Write the codebook
stream.write( (char*) codebook, 2048 );
// Write the 1x1 mipmap level
if (indexedImages.size() > 1)
writeZeroes(stream, 1);
// Write the index data
for (int i=0; i<vectors.size(); i++) {
const Vec<32>& srcvec = vectors.at(i);
const int c = vq.findClosest(srcvec);
stream << (uint8_t)c;
}
}