Remove unused RendererTiles::tile_cols
[geeqie.git] / src / image-load-dds.cc
1 /*
2  * Copyright (C) 2018 The Geeqie Team
3  *
4  * Author: Wolfgang Lieff
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20  * Derived from dds_reader written by:
21  * Copyright (c) 2015 Kenji Sasaki
22  * Released under the MIT license.
23  * https://github.com/npedotnet/DDSReader/blob/master/LICENSE
24
25  */
26
27 #include "image-load-dds.h"
28
29 #include <sys/types.h>
30
31 #include <gdk-pixbuf/gdk-pixbuf.h>
32 #include <glib-object.h>
33 #include <glib.h>
34
35 #include "image-load.h"
36
37 namespace
38 {
39
40 struct ImageLoaderDDS : public ImageLoaderBackend
41 {
42 public:
43         ~ImageLoaderDDS() override;
44
45         void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
46         gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
47         GdkPixbuf *get_pixbuf() override;
48         gchar *get_format_name() override;
49         gchar **get_format_mime_types() override;
50
51 private:
52         AreaUpdatedCb area_updated_cb;
53         gpointer data;
54
55         GdkPixbuf *pixbuf;
56 };
57
58 void free_buffer(guchar *pixels, gpointer)
59 {
60         g_free(pixels);
61 }
62
63 uint ddsGetHeight(unsigned const char * buffer) {
64         return (buffer[12] & 0xFF) | (buffer[13] & 0xFF) << 8 | (buffer[14] & 0xFF) << 16 | (buffer[15] & 0xFF) << 24;
65 }
66
67 uint ddsGetWidth(unsigned const char * buffer) {
68         return (buffer[16] & 0xFF) | (buffer[17] & 0xFF) << 8 | (buffer[18] & 0xFF) << 16 | (buffer[19] & 0xFF) << 24;
69 }
70
71 #pragma GCC diagnostic push
72 #pragma GCC diagnostic ignored "-Wunused-function"
73 uint ddsGetMipmap_unused(unsigned const char * buffer) {
74         return (buffer[28] & 0xFF) | (buffer[29] & 0xFF) << 8 | (buffer[30] & 0xFF) << 16 | (buffer[31] & 0xFF) << 24;
75 }
76 #pragma GCC diagnostic pop
77
78 uint ddsGetPixelFormatFlags(unsigned const char * buffer) {
79         return (buffer[80] & 0xFF) | (buffer[81] & 0xFF) << 8 | (buffer[82] & 0xFF) << 16 | (buffer[83] & 0xFF) << 24;
80 }
81
82 uint ddsGetFourCC(unsigned const char * buffer) {
83         return (buffer[84] & 0xFF) << 24 | (buffer[85] & 0xFF) << 16 | (buffer[86] & 0xFF) << 8 | (buffer[87] & 0xFF);
84 }
85
86 uint ddsGetBitCount(unsigned const char * buffer) {
87         return (buffer[88] & 0xFF) | (buffer[89] & 0xFF) << 8 | (buffer[90] & 0xFF) << 16 | (buffer[91] & 0xFF) << 24;
88 }
89
90 uint ddsGetRedMask(unsigned const char * buffer) {
91         return (buffer[92] & 0xFF) | (buffer[93] & 0xFF) << 8 | (buffer[94] & 0xFF) << 16 | (buffer[95] & 0xFF) << 24;
92 }
93
94 uint ddsGetGreenMask(unsigned const char * buffer) {
95         return (buffer[96] & 0xFF) | (buffer[97] & 0xFF) << 8 | (buffer[98] & 0xFF) << 16 | (buffer[99] & 0xFF) << 24;
96 }
97
98 uint ddsGetBlueMask(unsigned const char * buffer) {
99         return (buffer[100] & 0xFF) | (buffer[101] & 0xFF) << 8 | (buffer[102] & 0xFF) << 16 | (buffer[103] & 0xFF) << 24;
100 }
101
102 uint ddsGetAlphaMask(unsigned const char * buffer) {
103         return (buffer[104] & 0xFF) | (buffer[105] & 0xFF) << 8 | (buffer[106] & 0xFF) << 16 | (buffer[107] & 0xFF) << 24;
104 }
105
106 // Image Type
107 enum {
108         DXT1 = (0x44585431),
109         DXT2 = (0x44585432),
110         DXT3 = (0x44585433),
111         DXT4 = (0x44585434),
112         DXT5 = (0x44585435),
113         A1R5G5B5 = ((1 << 16) | 2),
114         X1R5G5B5 = ((2 << 16) | 2),
115         A4R4G4B4 = ((3 << 16) | 2),
116         X4R4G4B4 = ((4 << 16) | 2),
117         R5G6B5 = ((5 << 16) | 2),
118         R8G8B8 = ((1 << 16) | 3),
119         A8B8G8R8 = ((1 << 16) | 4),
120         X8B8G8R8 = ((2 << 16) | 4),
121         A8R8G8B8 = ((3 << 16) | 4),
122         X8R8G8B8 = ((4 << 16) | 4)
123 };
124
125 // RGBA Masks
126 constexpr uint A1R5G5B5_MASKS[] = { 0x7C00, 0x03E0, 0x001F, 0x8000 };
127 constexpr uint X1R5G5B5_MASKS[] = { 0x7C00, 0x03E0, 0x001F, 0x0000 };
128 constexpr uint A4R4G4B4_MASKS[] = { 0x0F00, 0x00F0, 0x000F, 0xF000 };
129 constexpr uint X4R4G4B4_MASKS[] = { 0x0F00, 0x00F0, 0x000F, 0x0000 };
130 constexpr uint R5G6B5_MASKS[] = { 0xF800, 0x07E0, 0x001F, 0x0000 };
131 constexpr uint R8G8B8_MASKS[] = { 0xFF0000, 0x00FF00, 0x0000FF, 0x000000 };
132 constexpr uint A8B8G8R8_MASKS[] = { 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 };
133 constexpr uint X8B8G8R8_MASKS[] = { 0x000000FF, 0x0000FF00, 0x00FF0000, 0x00000000 };
134 constexpr uint A8R8G8B8_MASKS[] = { 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 };
135 constexpr uint X8R8G8B8_MASKS[] = { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 };
136
137 // BIT4 = 17 * index;
138 constexpr uint BIT5[] = { 0, 8, 16, 25, 33, 41, 49, 58, 66, 74, 82, 90, 99, 107, 115, 123, 132, 140, 148, 156, 165, 173, 181, 189, 197, 206, 214, 222, 230, 239, 247, 255 };
139 constexpr uint BIT6[] = { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 45, 49, 53, 57, 61, 65, 69, 73, 77, 81, 85, 89, 93, 97, 101, 105, 109, 113, 117, 121, 125, 130, 134, 138, 142, 146, 150, 154, 158, 162, 166, 170, 174, 178, 182, 186, 190, 194, 198, 202, 206, 210, 215, 219, 223, 227, 231, 235, 239, 243, 247, 251, 255 };
140
141 uint ddsGetType(const unsigned char *buffer) {
142         uint type = 0;
143         uint flags = ddsGetPixelFormatFlags(buffer);
144         if ((flags & 0x04) != 0) {
145                 // DXT
146                 type = ddsGetFourCC(buffer);
147         }
148         else if ((flags & 0x40) != 0) {
149                 // RGB
150                 uint bitCount = ddsGetBitCount(buffer);
151                 uint redMask = ddsGetRedMask(buffer);
152                 uint greenMask = ddsGetGreenMask(buffer);
153                 uint blueMask = ddsGetBlueMask(buffer);
154                 uint alphaMask = ((flags & 0x01) != 0) ? ddsGetAlphaMask(buffer) : 0; // 0x01 alpha
155                 if (bitCount == 16) {
156                         if (redMask == A1R5G5B5_MASKS[0] && greenMask == A1R5G5B5_MASKS[1] && blueMask == A1R5G5B5_MASKS[2] && alphaMask == A1R5G5B5_MASKS[3]) {
157                                 // A1R5G5B5
158                                 type = A1R5G5B5;
159                         }
160                         else if (redMask == X1R5G5B5_MASKS[0] && greenMask == X1R5G5B5_MASKS[1] && blueMask == X1R5G5B5_MASKS[2] && alphaMask == X1R5G5B5_MASKS[3]) {
161                                 // X1R5G5B5
162                                 type = X1R5G5B5;
163                         }
164                         else if (redMask == A4R4G4B4_MASKS[0] && greenMask == A4R4G4B4_MASKS[1] && blueMask == A4R4G4B4_MASKS[2] && alphaMask == A4R4G4B4_MASKS[3]) {
165                                 // A4R4G4B4
166                                 type = A4R4G4B4;
167                         }
168                         else if (redMask == X4R4G4B4_MASKS[0] && greenMask == X4R4G4B4_MASKS[1] && blueMask == X4R4G4B4_MASKS[2] && alphaMask == X4R4G4B4_MASKS[3]) {
169                                 // X4R4G4B4
170                                 type = X4R4G4B4;
171                         }
172                         else if (redMask == R5G6B5_MASKS[0] && greenMask == R5G6B5_MASKS[1] && blueMask == R5G6B5_MASKS[2] && alphaMask == R5G6B5_MASKS[3]) {
173                                 // R5G6B5
174                                 type = R5G6B5;
175                         }
176                         else {
177                                 // Unsupported 16bit RGB image
178                         }
179                 }
180                 else if (bitCount == 24) {
181                         if (redMask == R8G8B8_MASKS[0] && greenMask == R8G8B8_MASKS[1] && blueMask == R8G8B8_MASKS[2] && alphaMask == R8G8B8_MASKS[3]) {
182                                 // R8G8B8
183                                 type = R8G8B8;
184                         }
185                         else {
186                                 // Unsupported 24bit RGB image
187                         }
188                 }
189                 else if (bitCount == 32) {
190                         if (redMask == A8B8G8R8_MASKS[0] && greenMask == A8B8G8R8_MASKS[1] && blueMask == A8B8G8R8_MASKS[2] && alphaMask == A8B8G8R8_MASKS[3]) {
191                                 // A8B8G8R8
192                                 type = A8B8G8R8;
193                         }
194                         else if (redMask == X8B8G8R8_MASKS[0] && greenMask == X8B8G8R8_MASKS[1] && blueMask == X8B8G8R8_MASKS[2] && alphaMask == X8B8G8R8_MASKS[3]) {
195                                 // X8B8G8R8
196                                 type = X8B8G8R8;
197                         }
198                         else if (redMask == A8R8G8B8_MASKS[0] && greenMask == A8R8G8B8_MASKS[1] && blueMask == A8R8G8B8_MASKS[2] && alphaMask == A8R8G8B8_MASKS[3]) {
199                                 // A8R8G8B8
200                                 type = A8R8G8B8;
201                         }
202                         else if (redMask == X8R8G8B8_MASKS[0] && greenMask == X8R8G8B8_MASKS[1] && blueMask == X8R8G8B8_MASKS[2] && alphaMask == X8R8G8B8_MASKS[3]) {
203                                 // X8R8G8B8
204                                 type = X8R8G8B8;
205                         }
206                         else {
207                                 // Unsupported 32bit RGB image
208                         }
209                 }
210         }
211         else {
212                 // YUV or LUMINANCE image
213         }
214         return type;
215 }
216
217 uint ddsGetDXTColor2_1(uint c0, uint c1, uint a) {
218         // 2*c0/3 + c1/3
219         uint r = (2 * BIT5[(c0 & 0xFC00) >> 11] + BIT5[(c1 & 0xFC00) >> 11]) / 3;
220         uint g = (2 * BIT6[(c0 & 0x07E0) >> 5] + BIT6[(c1 & 0x07E0) >> 5]) / 3;
221         uint b = (2 * BIT5[c0 & 0x001F] + BIT5[c1 & 0x001F]) / 3;
222         return (a << 24) | (r << 0) | (g << 8) | (b << 16);
223 }
224
225 uint ddsGetDXTColor1_1(uint c0, uint c1, uint a) {
226         // (c0+c1) / 2
227         uint r = (BIT5[(c0 & 0xFC00) >> 11] + BIT5[(c1 & 0xFC00) >> 11]) / 2;
228         uint g = (BIT6[(c0 & 0x07E0) >> 5] + BIT6[(c1 & 0x07E0) >> 5]) / 2;
229         uint b = (BIT5[c0 & 0x001F] + BIT5[c1 & 0x001F]) / 2;
230         return (a << 24) | (r << 0) | (g << 8) | (b << 16);
231 }
232
233 uint ddsGetDXTColor1(uint c, uint a) {
234         uint r = BIT5[(c & 0xFC00) >> 11];
235         uint g = BIT6[(c & 0x07E0) >> 5];
236         uint b = BIT5[(c & 0x001F)];
237         return (a << 24) | (r << 0) | (g << 8) | (b << 16);
238 }
239
240 uint ddsGetDXTColor(uint c0, uint c1, uint a, uint t) {
241         switch (t) {
242         case 0: return ddsGetDXTColor1(c0, a);
243         case 1: return ddsGetDXTColor1(c1, a);
244         case 2: return (c0 > c1) ? ddsGetDXTColor2_1(c0, c1, a) : ddsGetDXTColor1_1(c0, c1, a);
245         case 3: return (c0 > c1) ? ddsGetDXTColor2_1(c1, c0, a) : 0;
246         }
247         return 0;
248 }
249
250 guchar *ddsDecodeDXT1(uint width, uint height, const unsigned char *buffer) {
251         auto pixels = static_cast<uint *>(g_try_malloc(4 * width*height));
252         if (pixels == nullptr) return nullptr;
253         uint index = 128;
254         uint w = (width + 3) / 4;
255         uint h = (height + 3) / 4;
256         for (uint i = 0; i<h; i++) {
257                 for (uint j = 0; j<w; j++) {
258                         uint c0 = (buffer[index] & 0xFF) | (buffer[index + 1] & 0xFF) << 8; index += 2;
259                         uint c1 = (buffer[index] & 0xFF) | (buffer[index + 1] & 0xFF) << 8; index += 2;
260                         for (uint k = 0; k<4; k++) {
261                                 if (4 * i + k >= height) break;
262                                 uint t0 = (buffer[index] & 0x03);
263                                 uint t1 = (buffer[index] & 0x0C) >> 2;
264                                 uint t2 = (buffer[index] & 0x30) >> 4;
265                                 uint t3 = (buffer[index++] & 0xC0) >> 6;
266                                 pixels[4 * width*i + 4 * j + width*k + 0] = ddsGetDXTColor(c0, c1, 0xFF, t0);
267                                 if (4 * j + 1 >= width) continue;
268                                 pixels[4 * width*i + 4 * j + width*k + 1] = ddsGetDXTColor(c0, c1, 0xFF, t1);
269                                 if (4 * j + 2 >= width) continue;
270                                 pixels[4 * width*i + 4 * j + width*k + 2] = ddsGetDXTColor(c0, c1, 0xFF, t2);
271                                 if (4 * j + 3 >= width) continue;
272                                 pixels[4 * width*i + 4 * j + width*k + 3] = ddsGetDXTColor(c0, c1, 0xFF, t3);
273                         }
274                 }
275         }
276         return reinterpret_cast<guchar *>(pixels);
277 }
278
279 guchar *ddsDecodeDXT3(uint width, uint height, const unsigned char *buffer) {
280         auto pixels = static_cast<uint *>(g_try_malloc(4 * width*height));
281         if (pixels == nullptr) return nullptr;
282         uint index = 128;
283         uint w = (width + 3) / 4;
284         uint h = (height + 3) / 4;
285         uint alphaTable[16];
286         for (uint i = 0; i<h; i++) {
287                 for (uint j = 0; j<w; j++) {
288                         // create alpha table(4bit to 8bit)
289                         for (uint k = 0; k<4; k++) {
290                                 uint a0 = (buffer[index++] & 0xFF);
291                                 uint a1 = (buffer[index++] & 0xFF);
292                                 // 4bit alpha to 8bit alpha
293                                 alphaTable[4 * k + 0] = 17 * ((a0 & 0xF0) >> 4);
294                                 alphaTable[4 * k + 1] = 17 * (a0 & 0x0F);
295                                 alphaTable[4 * k + 2] = 17 * ((a1 & 0xF0) >> 4);
296                                 alphaTable[4 * k + 3] = 17 * (a1 & 0x0F);
297                         }
298                         uint c0 = (buffer[index] & 0xFF) | (buffer[index + 1] & 0xFF) << 8; index += 2;
299                         uint c1 = (buffer[index] & 0xFF) | (buffer[index + 1] & 0xFF) << 8; index += 2;
300                         for (uint k = 0; k<4; k++) {
301                                 if (4 * i + k >= height) break;
302                                 uint t0 = (buffer[index] & 0x03);
303                                 uint t1 = (buffer[index] & 0x0C) >> 2;
304                                 uint t2 = (buffer[index] & 0x30) >> 4;
305                                 uint t3 = (buffer[index++] & 0xC0) >> 6;
306                                 pixels[4 * width*i + 4 * j + width*k + 0] = ddsGetDXTColor(c0, c1, alphaTable[4 * k + 0], t0);
307                                 if (4 * j + 1 >= width) continue;
308                                 pixels[4 * width*i + 4 * j + width*k + 1] = ddsGetDXTColor(c0, c1, alphaTable[4 * k + 1], t1);
309                                 if (4 * j + 2 >= width) continue;
310                                 pixels[4 * width*i + 4 * j + width*k + 2] = ddsGetDXTColor(c0, c1, alphaTable[4 * k + 2], t2);
311                                 if (4 * j + 3 >= width) continue;
312                                 pixels[4 * width*i + 4 * j + width*k + 3] = ddsGetDXTColor(c0, c1, alphaTable[4 * k + 3], t3);
313                         }
314                 }
315         }
316         return reinterpret_cast<guchar *>(pixels);
317 }
318
319 guchar *ddsDecodeDXT2(uint width, uint height, const unsigned char *buffer) {
320         return ddsDecodeDXT3(width, height, buffer);
321 }
322
323 int ddsGetDXT5Alpha(uint a0, uint a1, uint t) {
324         if (a0 > a1) switch (t) {
325         case 0: return a0;
326         case 1: return a1;
327         case 2: return (6 * a0 + a1) / 7;
328         case 3: return (5 * a0 + 2 * a1) / 7;
329         case 4: return (4 * a0 + 3 * a1) / 7;
330         case 5: return (3 * a0 + 4 * a1) / 7;
331         case 6: return (2 * a0 + 5 * a1) / 7;
332         case 7: return (a0 + 6 * a1) / 7;
333         }
334         else switch (t) {
335         case 0: return a0;
336         case 1: return a1;
337         case 2: return (4 * a0 + a1) / 5;
338         case 3: return (3 * a0 + 2 * a1) / 5;
339         case 4: return (2 * a0 + 3 * a1) / 5;
340         case 5: return (a0 + 4 * a1) / 5;
341         case 6: return 0;
342         case 7: return 255;
343         }
344         return 0;
345 }
346
347 guchar *ddsDecodeDXT5(uint width, uint height, const unsigned char *buffer) {
348         auto pixels = static_cast<uint *>(g_try_malloc(4 * width*height));
349         if (pixels == nullptr) return nullptr;
350         uint index = 128;
351         uint w = (width + 3) / 4;
352         uint h = (height + 3) / 4;
353         uint alphaTable[16];
354         for (uint i = 0; i<h; i++) {
355                 for (uint j = 0; j<w; j++) {
356                         // create alpha table
357                         uint a0 = (buffer[index++] & 0xFF);
358                         uint a1 = (buffer[index++] & 0xFF);
359                         uint b0 = (buffer[index] & 0xFF) | (buffer[index + 1] & 0xFF) << 8 | (buffer[index + 2] & 0xFF) << 16; index += 3;
360                         uint b1 = (buffer[index] & 0xFF) | (buffer[index + 1] & 0xFF) << 8 | (buffer[index + 2] & 0xFF) << 16; index += 3;
361                         alphaTable[0] = b0 & 0x07;
362                         alphaTable[1] = (b0 >> 3) & 0x07;
363                         alphaTable[2] = (b0 >> 6) & 0x07;
364                         alphaTable[3] = (b0 >> 9) & 0x07;
365                         alphaTable[4] = (b0 >> 12) & 0x07;
366                         alphaTable[5] = (b0 >> 15) & 0x07;
367                         alphaTable[6] = (b0 >> 18) & 0x07;
368                         alphaTable[7] = (b0 >> 21) & 0x07;
369                         alphaTable[8] = b1 & 0x07;
370                         alphaTable[9] = (b1 >> 3) & 0x07;
371                         alphaTable[10] = (b1 >> 6) & 0x07;
372                         alphaTable[11] = (b1 >> 9) & 0x07;
373                         alphaTable[12] = (b1 >> 12) & 0x07;
374                         alphaTable[13] = (b1 >> 15) & 0x07;
375                         alphaTable[14] = (b1 >> 18) & 0x07;
376                         alphaTable[15] = (b1 >> 21) & 0x07;
377                         uint c0 = (buffer[index] & 0xFF) | (buffer[index + 1] & 0xFF) << 8; index += 2;
378                         uint c1 = (buffer[index] & 0xFF) | (buffer[index + 1] & 0xFF) << 8; index += 2;
379                         for (uint k = 0; k<4; k++) {
380                                 if (4 * i + k >= height) break;
381                                 uint t0 = (buffer[index] & 0x03);
382                                 uint t1 = (buffer[index] & 0x0C) >> 2;
383                                 uint t2 = (buffer[index] & 0x30) >> 4;
384                                 uint t3 = (buffer[index++] & 0xC0) >> 6;
385                                 pixels[4 * width*i + 4 * j + width*k + 0] = ddsGetDXTColor(c0, c1, ddsGetDXT5Alpha(a0, a1, alphaTable[4 * k + 0]), t0);
386                                 if (4 * j + 1 >= width) continue;
387                                 pixels[4 * width*i + 4 * j + width*k + 1] = ddsGetDXTColor(c0, c1, ddsGetDXT5Alpha(a0, a1, alphaTable[4 * k + 1]), t1);
388                                 if (4 * j + 2 >= width) continue;
389                                 pixels[4 * width*i + 4 * j + width*k + 2] = ddsGetDXTColor(c0, c1, ddsGetDXT5Alpha(a0, a1, alphaTable[4 * k + 2]), t2);
390                                 if (4 * j + 3 >= width) continue;
391                                 pixels[4 * width*i + 4 * j + width*k + 3] = ddsGetDXTColor(c0, c1, ddsGetDXT5Alpha(a0, a1, alphaTable[4 * k + 3]), t3);
392                         }
393                 }
394         }
395         return reinterpret_cast<guchar *>(pixels);
396 }
397
398 guchar *ddsDecodeDXT4(uint width, uint height, const unsigned char *buffer) {
399         return ddsDecodeDXT5(width, height, buffer);
400 }
401
402 guchar *ddsReadA1R5G5B5(uint width, uint height, const unsigned char *buffer) {
403         auto pixels = static_cast<uint *>(g_try_malloc(4 * width*height));
404         if (pixels == nullptr) return nullptr;
405         uint index = 128;
406         for (uint i = 0; i<height*width; i++) {
407                 uint rgba = (buffer[index] & 0xFF) | (buffer[index + 1] & 0xFF) << 8; index += 2;
408                 uint r = BIT5[(rgba & A1R5G5B5_MASKS[0]) >> 10];
409                 uint g = BIT5[(rgba & A1R5G5B5_MASKS[1]) >> 5];
410                 uint b = BIT5[(rgba & A1R5G5B5_MASKS[2])];
411                 uint a = 255 * ((rgba & A1R5G5B5_MASKS[3]) >> 15);
412                 pixels[i] = (a << 24) | (r << 0) | (g << 8) | (b << 16);
413         }
414         return reinterpret_cast<guchar *>(pixels);
415 }
416
417 guchar *ddsReadX1R5G5B5(uint width, uint height, const unsigned char *buffer) {
418         auto pixels = static_cast<uint *>(g_try_malloc(4 * width*height));
419         if (pixels == nullptr) return nullptr;
420         uint index = 128;
421         for (uint i = 0; i<height*width; i++) {
422                 uint rgba = (buffer[index] & 0xFF) | (buffer[index + 1] & 0xFF) << 8; index += 2;
423                 uint r = BIT5[(rgba & X1R5G5B5_MASKS[0]) >> 10];
424                 uint g = BIT5[(rgba & X1R5G5B5_MASKS[1]) >> 5];
425                 uint b = BIT5[(rgba & X1R5G5B5_MASKS[2])];
426                 uint a = 255;
427                 pixels[i] = (a << 24) | (r << 0) | (g << 8) | (b << 16);
428         }
429         return reinterpret_cast<guchar *>(pixels);
430 }
431
432 guchar *ddsReadA4R4G4B4(uint width, uint height, const unsigned char *buffer) {
433         auto pixels = static_cast<uint *>(g_try_malloc(4 * width*height));
434         if (pixels == nullptr) return nullptr;
435         uint index = 128;
436         for (uint i = 0; i<height*width; i++) {
437                 uint rgba = (buffer[index] & 0xFF) | (buffer[index + 1] & 0xFF) << 8; index += 2;
438                 uint r = 17 * ((rgba & A4R4G4B4_MASKS[0]) >> 8);
439                 uint g = 17 * ((rgba & A4R4G4B4_MASKS[1]) >> 4);
440                 uint b = 17 * ((rgba & A4R4G4B4_MASKS[2]));
441                 uint a = 17 * ((rgba & A4R4G4B4_MASKS[3]) >> 12);
442                 pixels[i] = (a << 24) | (r << 0) | (g << 8) | (b << 16);
443         }
444         return reinterpret_cast<guchar *>(pixels);
445 }
446
447 guchar *ddsReadX4R4G4B4(uint width, uint height, const unsigned char *buffer) {
448         auto pixels = static_cast<uint *>(g_try_malloc(4 * width*height));
449         if (pixels == nullptr) return nullptr;
450         uint index = 128;
451         for (uint i = 0; i<height*width; i++) {
452                 uint rgba = (buffer[index] & 0xFF) | (buffer[index + 1] & 0xFF) << 8; index += 2;
453                 uint r = 17 * ((rgba & A4R4G4B4_MASKS[0]) >> 8);
454                 uint g = 17 * ((rgba & A4R4G4B4_MASKS[1]) >> 4);
455                 uint b = 17 * ((rgba & A4R4G4B4_MASKS[2]));
456                 uint a = 255;
457                 pixels[i] = (a << 24) | (r << 0) | (g << 8) | (b << 16);
458         }
459         return reinterpret_cast<guchar *>(pixels);
460 }
461
462 guchar *ddsReadR5G6B5(uint width, uint height, const unsigned char *buffer) {
463         auto pixels = static_cast<uint *>(g_try_malloc(4 * width*height));
464         if (pixels == nullptr) return nullptr;
465         uint index = 128;
466         for (uint i = 0; i<height*width; i++) {
467                 uint rgba = (buffer[index] & 0xFF) | (buffer[index + 1] & 0xFF) << 8; index += 2;
468                 uint r = BIT5[((rgba & R5G6B5_MASKS[0]) >> 11)];
469                 uint g = BIT6[((rgba & R5G6B5_MASKS[1]) >> 5)];
470                 uint b = BIT5[((rgba & R5G6B5_MASKS[2]))];
471                 uint a = 255;
472                 pixels[i] = (a << 24) | (r << 0) | (g << 8) | (b << 16);
473         }
474         return reinterpret_cast<guchar *>(pixels);
475 }
476
477 guchar *ddsReadR8G8B8(uint width, uint height, const unsigned char *buffer) {
478         auto pixels = static_cast<uint *>(g_try_malloc(4 * width*height));
479         if (pixels == nullptr) return nullptr;
480         uint index = 128;
481         for (uint i = 0; i<height*width; i++) {
482                 uint b = buffer[index++] & 0xFF;
483                 uint g = buffer[index++] & 0xFF;
484                 uint r = buffer[index++] & 0xFF;
485                 uint a = 255;
486                 pixels[i] = (a << 24) | (r << 0) | (g << 8) | (b << 16);
487         }
488         return reinterpret_cast<guchar *>(pixels);
489 }
490
491 guchar *ddsReadA8B8G8R8(uint width, uint height, const unsigned char *buffer) {
492         auto pixels = static_cast<uint *>(g_try_malloc(4 * width*height));
493         if (pixels == nullptr) return nullptr;
494         uint index = 128;
495         for (uint i = 0; i<height*width; i++) {
496                 uint r = buffer[index++] & 0xFF;
497                 uint g = buffer[index++] & 0xFF;
498                 uint b = buffer[index++] & 0xFF;
499                 uint a = buffer[index++] & 0xFF;
500                 pixels[i] = (a << 24) | (r << 0) | (g << 8) | (b << 16);
501         }
502         return reinterpret_cast<guchar *>(pixels);
503 }
504
505 guchar *ddsReadX8B8G8R8(uint width, uint height, const unsigned char *buffer) {
506         auto pixels = static_cast<uint *>(g_try_malloc(4 * width*height));
507         if (pixels == nullptr) return nullptr;
508         uint index = 128;
509         for (uint i = 0; i<height*width; i++) {
510                 uint r = buffer[index++] & 0xFF;
511                 uint g = buffer[index++] & 0xFF;
512                 uint b = buffer[index++] & 0xFF;
513                 uint a = 255; index++;
514                 pixels[i] = (a << 24) | (r << 0) | (g << 8) | (b << 16);
515         }
516         return reinterpret_cast<guchar *>(pixels);
517 }
518
519 guchar *ddsReadA8R8G8B8(uint width, uint height, const unsigned char *buffer) {
520         auto pixels = static_cast<uint *>(g_try_malloc(4 * width*height));
521         if (pixels == nullptr) return nullptr;
522         uint index = 128;
523         for (uint i = 0; i<height*width; i++) {
524                 uint b = buffer[index++] & 0xFF;
525                 uint g = buffer[index++] & 0xFF;
526                 uint r = buffer[index++] & 0xFF;
527                 uint a = buffer[index++] & 0xFF;
528                 pixels[i] = (a << 24) | (r << 0) | (g << 8) | (b << 16);
529         }
530         return reinterpret_cast<guchar *>(pixels);
531 }
532
533 guchar *ddsReadX8R8G8B8(uint width, uint height, const unsigned char *buffer) {
534         auto pixels = static_cast<uint *>(g_try_malloc(4 * width*height));
535         if (pixels == nullptr) return nullptr;
536         uint index = 128;
537         for (uint i = 0; i<height*width; i++) {
538                 uint b = buffer[index++] & 0xFF;
539                 uint g = buffer[index++] & 0xFF;
540                 uint r = buffer[index++] & 0xFF;
541                 uint a = 255; index++;
542                 pixels[i] = (a << 24) | (r << 0) | (g << 8) | (b << 16);
543         }
544         return reinterpret_cast<guchar *>(pixels);
545 }
546
547 gboolean ImageLoaderDDS::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
548 {
549         uint width = ddsGetWidth(buf);
550         uint height = ddsGetHeight(buf);
551         uint type = ddsGetType(buf);
552         if (type == 0) return FALSE;
553         {
554                 guchar *pixels = nullptr;
555                 guint rowstride = width * 4;
556                 switch (type) {
557                 case DXT1: pixels = ddsDecodeDXT1(width, height, buf); break;
558                 case DXT2: pixels = ddsDecodeDXT2(width, height, buf); break;
559                 case DXT3: pixels = ddsDecodeDXT3(width, height, buf); break;
560                 case DXT4: pixels = ddsDecodeDXT4(width, height, buf); break;
561                 case DXT5: pixels = ddsDecodeDXT5(width, height, buf); break;
562                 case A1R5G5B5: pixels = ddsReadA1R5G5B5(width, height, buf); break;
563                 case X1R5G5B5: pixels = ddsReadX1R5G5B5(width, height, buf); break;
564                 case A4R4G4B4: pixels = ddsReadA4R4G4B4(width, height, buf); break;
565                 case X4R4G4B4: pixels = ddsReadX4R4G4B4(width, height, buf); break;
566                 case R5G6B5: pixels = ddsReadR5G6B5(width, height, buf); break;
567                 case R8G8B8: pixels = ddsReadR8G8B8(width, height, buf); break;
568                 case A8B8G8R8: pixels = ddsReadA8B8G8R8(width, height, buf); break;
569                 case X8B8G8R8: pixels = ddsReadX8B8G8R8(width, height, buf); break;
570                 case A8R8G8B8: pixels = ddsReadA8R8G8B8(width, height, buf); break;
571                 case X8R8G8B8: pixels = ddsReadX8R8G8B8(width, height, buf); break;
572                 }
573                 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8, width, height, rowstride, free_buffer, nullptr);
574                 area_updated_cb(nullptr, 0, 0, width, height, data);
575                 chunk_size = count;
576                 return TRUE;
577         }
578 }
579
580 void ImageLoaderDDS::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
581 {
582         this->area_updated_cb = area_updated_cb;
583         this->data = data;
584 }
585
586 GdkPixbuf *ImageLoaderDDS::get_pixbuf()
587 {
588         return pixbuf;
589 }
590
591 gchar *ImageLoaderDDS::get_format_name()
592 {
593         return g_strdup("dds");
594 }
595
596 gchar **ImageLoaderDDS::get_format_mime_types()
597 {
598         static const gchar *mime[] = {"image/vnd-ms.dds", nullptr};
599         return g_strdupv(const_cast<gchar **>(mime));
600 }
601
602 ImageLoaderDDS::~ImageLoaderDDS()
603 {
604         if (pixbuf) g_object_unref(pixbuf);
605 }
606
607 } // namespace
608
609 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_dds()
610 {
611         return std::make_unique<ImageLoaderDDS>();
612 }
613
614 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */