-
Notifications
You must be signed in to change notification settings - Fork 9
/
io-vtf.c
788 lines (694 loc) · 29.5 KB
/
io-vtf.c
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/*
* GdkPixbuf library - VTF image loader
*
* Copyright (C) 2010 Forrest Voight
*
* Authors: Forrest Voight <voights@gmail.com> & Jan Dudek <jd@jandudek.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk-pixbuf/gdk-pixbuf-io.h>
#include <gdk-pixbuf/gdk-pixbuf-animation.h>
#include <glib/gstdio.h>
#define VTF_70_HEADER_SIZE 63
#define VTF_71_HEADER_SIZE 65
#define VTF_73_HEADER_SIZE 72
#define VTF_RESOURCE_HEADER_SIZE 8
typedef struct
{
char signature[4]; // File signature ("VTF\0").
uint32_t version[2]; // version[0].version[1] (currently 7.2).
uint32_t headerSize; // Size of the header struct (16 byte aligned; currently 80 bytes).
uint16_t width; // Width of the largest mipmap in pixels. Must be a power of 2.
uint16_t height; // Height of the largest mipmap in pixels. Must be a power of 2.
uint32_t flags; // VTF flags.
uint16_t frames; // Number of frames, if animated (1 for no animation).
uint16_t firstFrame; // First frame in animation (0 based).
uint8_t padding0[4]; // reflectivity padding (16 byte alignment).
float reflectivity[3]; // reflectivity vector.
uint8_t padding1[4]; // reflectivity padding (8 byte packing).
float bumpmapScale; // Bumpmap scale.
uint32_t highResImageFormat; // High resolution image format.
uint8_t mipmapCount; // Number of mipmaps.
uint32_t lowResImageFormat; // Low resolution image format (always DXT1).
uint8_t lowResImageWidth; // Low resolution image width.
uint8_t lowResImageHeight; // Low resolution image height.
uint16_t depth; // Depth of the largest mipmap in pixels.
// Must be a power of 2. Can be 0 or 1 for a 2D texture (v7.2 only).
uint8_t padding2[3]; // padding
uint32_t resources; // VTF 7.3 resource count
} VtfHeader;
enum
{
IMAGE_FORMAT_NONE = -1,
IMAGE_FORMAT_RGBA8888 = 0,
IMAGE_FORMAT_ABGR8888,
IMAGE_FORMAT_RGB888,
IMAGE_FORMAT_BGR888,
IMAGE_FORMAT_RGB565,
IMAGE_FORMAT_I8,
IMAGE_FORMAT_IA88,
IMAGE_FORMAT_P8,
IMAGE_FORMAT_A8,
IMAGE_FORMAT_RGB888_BLUESCREEN,
IMAGE_FORMAT_BGR888_BLUESCREEN,
IMAGE_FORMAT_ARGB8888,
IMAGE_FORMAT_BGRA8888,
IMAGE_FORMAT_DXT1,
IMAGE_FORMAT_DXT3,
IMAGE_FORMAT_DXT5,
IMAGE_FORMAT_BGRX8888,
IMAGE_FORMAT_BGR565,
IMAGE_FORMAT_BGRX5551,
IMAGE_FORMAT_BGRA4444,
IMAGE_FORMAT_DXT1_ONEBITALPHA,
IMAGE_FORMAT_BGRA5551,
IMAGE_FORMAT_UV88,
IMAGE_FORMAT_UVWQ8888,
IMAGE_FORMAT_RGBA16161616F,
IMAGE_FORMAT_RGBA16161616,
IMAGE_FORMAT_UVLX8888
};
enum
{
// Flags from the *.txt config file
TEXTUREFLAGS_POINTSAMPLE = 0x00000001,
TEXTUREFLAGS_TRILINEAR = 0x00000002,
TEXTUREFLAGS_CLAMPS = 0x00000004,
TEXTUREFLAGS_CLAMPT = 0x00000008,
TEXTUREFLAGS_ANISOTROPIC = 0x00000010,
TEXTUREFLAGS_HINT_DXT5 = 0x00000020,
TEXTUREFLAGS_PWL_CORRECTED = 0x00000040,
TEXTUREFLAGS_NORMAL = 0x00000080,
TEXTUREFLAGS_NOMIP = 0x00000100,
TEXTUREFLAGS_NOLOD = 0x00000200,
TEXTUREFLAGS_ALL_MIPS = 0x00000400,
TEXTUREFLAGS_PROCEDURAL = 0x00000800,
// These are automatically generated by vtex from the texture data.
TEXTUREFLAGS_ONEBITALPHA = 0x00001000,
TEXTUREFLAGS_EIGHTBITALPHA = 0x00002000,
// Newer flags from the *.txt config file
TEXTUREFLAGS_ENVMAP = 0x00004000,
TEXTUREFLAGS_RENDERTARGET = 0x00008000,
TEXTUREFLAGS_DEPTHRENDERTARGET = 0x00010000,
TEXTUREFLAGS_NODEBUGOVERRIDE = 0x00020000,
TEXTUREFLAGS_SINGLECOPY = 0x00040000,
TEXTUREFLAGS_PRE_SRGB = 0x00080000,
TEXTUREFLAGS_UNUSED_00100000 = 0x00100000,
TEXTUREFLAGS_UNUSED_00200000 = 0x00200000,
TEXTUREFLAGS_UNUSED_00400000 = 0x00400000,
TEXTUREFLAGS_NODEPTHBUFFER = 0x00800000,
TEXTUREFLAGS_UNUSED_01000000 = 0x01000000,
TEXTUREFLAGS_CLAMPU = 0x02000000,
TEXTUREFLAGS_VERTEXTEXTURE = 0x04000000,
TEXTUREFLAGS_SSBUMP = 0x08000000,
TEXTUREFLAGS_UNUSED_10000000 = 0x10000000,
TEXTUREFLAGS_BORDER = 0x20000000,
TEXTUREFLAGS_UNUSED_40000000 = 0x40000000,
TEXTUREFLAGS_UNUSED_80000000 = 0x80000000
};
typedef struct
{
GdkPixbufModuleSizeFunc size_func;
GdkPixbufModulePreparedFunc prepared_func;
GdkPixbufModuleUpdatedFunc updated_func;
gpointer user_data;
guchar *buffer;
guint buffer_size;
guint buffer_data_size;
} VtfContext;
static size_t frame_size(uint32_t image_format, uint16_t width, uint16_t height) {
switch (image_format) {
case IMAGE_FORMAT_NONE: return 0;
case IMAGE_FORMAT_RGBA8888: return width * height * 4;
case IMAGE_FORMAT_ABGR8888: return width * height * 4;
case IMAGE_FORMAT_RGB888: return width * height * 3;
case IMAGE_FORMAT_BGR888: return width * height * 3;
case IMAGE_FORMAT_RGB565: return width * height * 2;
case IMAGE_FORMAT_I8: return width * height * 1;
case IMAGE_FORMAT_IA88: return width * height * 2;
// case IMAGE_FORMAT_P8: return TODO;
case IMAGE_FORMAT_A8: return width * height * 1;
// case IMAGE_FORMAT_RGB888_BLUESCREEN: return TODO;
// case IMAGE_FORMAT_BGR888_BLUESCREEN: return TODO;
case IMAGE_FORMAT_ARGB8888: return width * height * 4;
case IMAGE_FORMAT_BGRA8888: return width * height * 4;
case IMAGE_FORMAT_DXT1: return ((width+3)/4) * ((height+3)/4) * 8;
// case IMAGE_FORMAT_DXT3: return TODO;
case IMAGE_FORMAT_DXT5: return ((width+3)/4) * ((height+3)/4) * 16;
// case IMAGE_FORMAT_BGRX8888: return TODO;
// case IMAGE_FORMAT_BGR565: return TODO;
// case IMAGE_FORMAT_BGRX5551: return TODO;
// case IMAGE_FORMAT_BGRA4444: return TODO;
// case IMAGE_FORMAT_DXT1_ONEBITALPHA: return TODO;
// case IMAGE_FORMAT_BGRA5551: return TODO;
// case IMAGE_FORMAT_UV88: return TODO;
// case IMAGE_FORMAT_UVWQ8888: return TODO;
case IMAGE_FORMAT_RGBA16161616F: return width * height * 8;
case IMAGE_FORMAT_RGBA16161616: return width * height * 8;
// case IMAGE_FORMAT_UVLX8888: return TODO;
// not yet supported or illegal value
default: return SIZE_MAX;
}
}
// cube maps have 6 or 7 faces, other maps have only 1
/*
static size_t face_count(const VtfHeader *header) {
return header->flags & TEXTUREFLAGS_ENVMAP ?
(header->firstFrame != 0xffff && (header->version[0] < 7 || (header->version[0] == 7 && header->version[1] < 5)) ? 7 : 6) :
1;
}
*/
static float read_float16(const uint8_t **data) {
union {
uint16_t bits[2];
float fval;
} val;
val.bits[0] = 0;
memcpy(&val.bits[1], *data, 2);
*data += 2;
return val.fval;
}
static uint16_t read_le16(const uint8_t **data) {
uint16_t val = 0;
memcpy(&val, *data, sizeof(val));
*data += sizeof(val);
return val;
}
static gpointer
gdk_pixbuf__vtf_image_begin_load (GdkPixbufModuleSizeFunc size_func,
GdkPixbufModulePreparedFunc prepared_func,
GdkPixbufModuleUpdatedFunc updated_func,
gpointer user_data,
GError **error)
{
VtfContext* context = g_malloc(sizeof(VtfContext));
if (context == NULL) {
g_set_error (
error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
("Not enough memory"));
return NULL;
}
context->size_func = size_func;
context->prepared_func = prepared_func;
context->updated_func = updated_func;
context->user_data = user_data;
context->buffer_size = 1000000;
context->buffer = g_malloc(context->buffer_size);
context->buffer_data_size = 0;
return (gpointer) context;
}
static uint
vtf_mip_size(VtfHeader *header, uint mipLevel, uint depth) {
uint mipWidth = header->width >> mipLevel;
uint mipHeight = header->height >> mipLevel;
uint mipDepth = depth >> mipLevel;
if (mipWidth < 1) mipWidth = 1;
if (mipHeight < 1) mipHeight = 1;
if (mipDepth < 1) mipDepth = 1;
return frame_size(header->highResImageFormat, mipWidth, mipHeight) * mipDepth;
}
static uint
vtf_offset(VtfHeader *header, uint frame, uint face, uint slice, int mipLevel)
{
uint offset = 0;
uint facecount = 1;
for (int i = header->mipmapCount - 1; i > mipLevel; i--)
offset += vtf_mip_size (header, i, header->depth);
offset *= header->frames * facecount;
uint volume_bytes = vtf_mip_size (header, mipLevel, header->depth);
uint slice_bytes = vtf_mip_size (header, mipLevel, 1);
offset += volume_bytes * (frame * facecount + face);
offset += slice_bytes * slice;
return offset;
}
static GdkPixbuf*
gdk_pixbuf__vtf_load_frame (VtfHeader *header, VtfContext *context, GError **error, int pos) {
int i, j;
GdkPixbuf* pixbuf;
if(header->highResImageFormat == IMAGE_FORMAT_RGBA8888) {
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, header->width, header->height);
uint32_t stride = gdk_pixbuf_get_rowstride(pixbuf);
guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
for (i = 0; i < header->height; i++)
for (j = 0; j < header->width; j++) {
pixels[stride*i + 4*j + 0] = context->buffer[pos++];
pixels[stride*i + 4*j + 1] = context->buffer[pos++];
pixels[stride*i + 4*j + 2] = context->buffer[pos++];
pixels[stride*i + 4*j + 3] = context->buffer[pos++];
}
} else if(header->highResImageFormat == IMAGE_FORMAT_ABGR8888) {
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, header->width, header->height);
if (pixbuf == NULL) {
goto pixbufallocerror;
}
uint32_t stride = gdk_pixbuf_get_rowstride(pixbuf);
guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
for (i = 0; i < header->height; i++)
for (j = 0; j < header->width; j++) {
pixels[stride*i + 4*j + 3] = context->buffer[pos++];
pixels[stride*i + 4*j + 2] = context->buffer[pos++];
pixels[stride*i + 4*j + 1] = context->buffer[pos++];
pixels[stride*i + 4*j + 0] = context->buffer[pos++];
}
} else if(header->highResImageFormat == IMAGE_FORMAT_RGB888) {
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, header->width, header->height);
if (pixbuf == NULL) {
goto pixbufallocerror;
}
uint32_t stride = gdk_pixbuf_get_rowstride(pixbuf);
guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
for (i = 0; i < header->height; i++)
for (j = 0; j < header->width; j++) {
pixels[stride*i + 3*j + 0] = context->buffer[pos++];
pixels[stride*i + 3*j + 1] = context->buffer[pos++];
pixels[stride*i + 3*j + 2] = context->buffer[pos++];
}
} else if(header->highResImageFormat == IMAGE_FORMAT_BGR888) {
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, header->width, header->height);
if (pixbuf == NULL) {
goto pixbufallocerror;
}
uint32_t stride = gdk_pixbuf_get_rowstride(pixbuf);
guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
for (i = 0; i < header->height; i++)
for (j = 0; j < header->width; j++) {
pixels[stride*i + 3*j + 2] = context->buffer[pos++];
pixels[stride*i + 3*j + 1] = context->buffer[pos++];
pixels[stride*i + 3*j + 0] = context->buffer[pos++];
}
} else if(header->highResImageFormat == IMAGE_FORMAT_RGB565) {
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, header->width, header->height);
if (pixbuf == NULL) {
goto pixbufallocerror;
}
uint32_t stride = gdk_pixbuf_get_rowstride(pixbuf);
guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
for (i = 0; i < header->height; i++)
for (j = 0; j < header->width; j++) {
uint16_t c0 = context->buffer[pos++];
c0 |= context->buffer[pos++] << 8;
uint16_t r, g, b;
r = (c0 >> 11) & 31;
r = (r << 3) | (r >> 5);
g = (c0 >> 5) & 63;
g = (g << 2) | (g >> 6);
b = (c0 >> 0) & 31;
b = (b << 3) | (b >> 5);
pixels[stride*i + 3*j + 0] = r;
pixels[stride*i + 3*j + 1] = g;
pixels[stride*i + 3*j + 2] = b;
}
} else if(header->highResImageFormat == IMAGE_FORMAT_I8) {
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, header->width, header->height);
if (pixbuf == NULL) {
goto pixbufallocerror;
}
uint32_t stride = gdk_pixbuf_get_rowstride(pixbuf);
guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
for (i = 0; i < header->height; i++)
for (j = 0; j < header->width; j++) {
uint8_t v = context->buffer[pos++];
pixels[stride*i + 3*j + 0] = v;
pixels[stride*i + 3*j + 1] = v;
pixels[stride*i + 3*j + 2] = v;
}
} else if(header->highResImageFormat == IMAGE_FORMAT_IA88) {
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, header->width, header->height);
if (pixbuf == NULL) {
goto pixbufallocerror;
}
uint32_t stride = gdk_pixbuf_get_rowstride(pixbuf);
guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
for (i = 0; i < header->height; i++)
for (j = 0; j < header->width; j++) {
uint8_t v = context->buffer[pos++];
pixels[stride*i + 4*j + 0] = v;
pixels[stride*i + 4*j + 1] = v;
pixels[stride*i + 4*j + 2] = v;
pixels[stride*i + 4*j + 3] = context->buffer[pos++];
}
} else if(header->highResImageFormat == IMAGE_FORMAT_A8) {
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, header->width, header->height);
if (pixbuf == NULL) {
goto pixbufallocerror;
}
uint32_t stride = gdk_pixbuf_get_rowstride(pixbuf);
guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
for (i = 0; i < header->height; i++)
for (j = 0; j < header->width; j++) {
pixels[stride*i + 4*j + 0] = 255;
pixels[stride*i + 4*j + 1] = 255;
pixels[stride*i + 4*j + 2] = 255;
pixels[stride*i + 4*j + 3] = context->buffer[pos++];
}
} else if(header->highResImageFormat == IMAGE_FORMAT_DXT1) {
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, header->width, header->height);
if (pixbuf == NULL) {
goto pixbufallocerror;
}
uint32_t stride = gdk_pixbuf_get_rowstride(pixbuf);
guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
for (i = 0; i < header->height; i+=4) {
for (j = 0; j < header->width; j+=4) {
uint16_t c0 = context->buffer[pos++];
c0 |= context->buffer[pos++] << 8;
uint16_t c1 = context->buffer[pos++];
c1 |= context->buffer[pos++] << 8;
uint16_t r[4], g[4], b[4];
r[0] = (c0 >> 11) & 31;
r[0] = (r[0] << 3) | (r[0] >> 5);
g[0] = (c0 >> 5) & 63;
g[0] = (g[0] << 2) | (g[0] >> 6);
b[0] = (c0 >> 0) & 31;
b[0] = (b[0] << 3) | (b[0] >> 5);
r[1] = (c1 >> 11) & 31;
r[1] = (r[1] << 3) | (r[1] >> 5);
g[1] = (c1 >> 5) & 63;
g[1] = (g[1] << 2) | (g[1] >> 6);
b[1] = (c1 >> 0) & 31;
b[1] = (b[1] << 3) | (b[1] >> 5);
uint16_t a[4];
a[0] = 255;
a[1] = 255;
if(c0 > c1) {
r[2] = (4*r[0] + 2*r[1] + 3)/6;
g[2] = (4*g[0] + 2*g[1] + 3)/6;
b[2] = (4*b[0] + 2*b[1] + 3)/6;
a[2] = 255;
r[3] = (2*r[0] + 4*r[1] + 3)/6;
g[3] = (2*g[0] + 4*g[1] + 3)/6;
b[3] = (2*b[0] + 4*b[1] + 3)/6;
a[3] = 255;
} else {
r[2] = (r[0] + r[1] + 1)/2;
g[2] = (g[0] + g[1] + 1)/2;
b[2] = (b[0] + b[1] + 1)/2;
a[2] = 255;
r[3] = 0;
g[3] = 0;
b[3] = 0;
a[3] = 0;
}
uint32_t sel = context->buffer[pos++];
sel |= context->buffer[pos++] << 8;
sel |= context->buffer[pos++] << 16;
sel |= context->buffer[pos++] << 24;
int ii, jj;
for(ii = 0; ii < 4; ii++)
for(jj = 0; jj < 4; jj++) {
pixels[stride*(i+ii) + 4*(j+jj) + 0] = r[sel & 3];
pixels[stride*(i+ii) + 4*(j+jj) + 1] = g[sel & 3];
pixels[stride*(i+ii) + 4*(j+jj) + 2] = b[sel & 3];
pixels[stride*(i+ii) + 4*(j+jj) + 3] = a[sel & 3];
sel >>= 2;
}
}
}
} else if(header->highResImageFormat == IMAGE_FORMAT_DXT5) {
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, header->width, header->height);
if (pixbuf == NULL) {
goto pixbufallocerror;
}
uint32_t stride = gdk_pixbuf_get_rowstride(pixbuf);
guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
for (i = 0; i < header->height; i+=4) {
for (j = 0; j < header->width; j+=4) {
{
uint16_t a[8];
a[0] = context->buffer[pos++];
a[1] = context->buffer[pos++];
if(a[0] > a[1]) {
a[2] = (12*a[0] + 2*a[1] + 7)/14;
a[3] = (10*a[0] + 4*a[1] + 7)/14;
a[4] = (8*a[0] + 6*a[1] + 7)/14;
a[5] = (6*a[0] + 8*a[1] + 7)/14;
a[6] = (4*a[0] + 10*a[1] + 7)/14;
a[7] = (2*a[0] + 12*a[1] + 7)/14;
} else {
a[2] = (8*a[0] + 2*a[1] + 5)/10;
a[3] = (6*a[0] + 4*a[1] + 5)/10;
a[4] = (4*a[0] + 6*a[1] + 5)/10;
a[5] = (2*a[0] + 8*a[1] + 5)/10;
a[6] = 0;
a[7] = 255;
}
uint64_t sel = context->buffer[pos++];
sel |= (uint64_t)context->buffer[pos++] << 8;
sel |= (uint64_t)context->buffer[pos++] << 16;
sel |= (uint64_t)context->buffer[pos++] << 24;
sel |= (uint64_t)context->buffer[pos++] << 32;
sel |= (uint64_t)context->buffer[pos++] << 40;
int ii, jj;
for(ii = 0; ii < 4; ii++)
for(jj = 0; jj < 4; jj++) {
pixels[stride*(i+ii) + 4*(j+jj)+3] = a[sel & 7];
sel >>= 3;
}
}
{
uint16_t c0 = context->buffer[pos++];
c0 |= context->buffer[pos++] << 8;
uint16_t c1 = context->buffer[pos++];
c1 |= context->buffer[pos++] << 8;
uint16_t r[4], g[4], b[4];
r[0] = (c0 >> 11) & 31;
r[0] = (r[0] << 3) | (r[0] >> 5);
g[0] = (c0 >> 5) & 63;
g[0] = (g[0] << 2) | (g[0] >> 6);
b[0] = (c0 >> 0) & 31;
b[0] = (b[0] << 3) | (b[0] >> 5);
r[1] = (c1 >> 11) & 31;
r[1] = (r[1] << 3) | (r[1] >> 5);
g[1] = (c1 >> 5) & 63;
g[1] = (g[1] << 2) | (g[1] >> 6);
b[1] = (c1 >> 0) & 31;
b[1] = (b[1] << 3) | (b[1] >> 5);
r[2] = (4*r[0] + 2*r[1] + 3)/6;
g[2] = (4*g[0] + 2*g[1] + 3)/6;
b[2] = (4*b[0] + 2*b[1] + 3)/6;
r[3] = (2*r[0] + 4*r[1] + 3)/6;
g[3] = (2*g[0] + 4*g[1] + 3)/6;
b[3] = (2*b[0] + 4*b[1] + 3)/6;
uint32_t sel = context->buffer[pos++];
sel |= context->buffer[pos++] << 8;
sel |= context->buffer[pos++] << 16;
sel |= context->buffer[pos++] << 24;
int ii, jj;
for(ii = 0; ii < 4; ii++)
for(jj = 0; jj < 4; jj++) {
pixels[stride*(i+ii) + 4*(j+jj) + 0] = r[sel & 3];
pixels[stride*(i+ii) + 4*(j+jj) + 1] = g[sel & 3];
pixels[stride*(i+ii) + 4*(j+jj) + 2] = b[sel & 3];
sel >>= 2;
}
}
}
}
} else if(header->highResImageFormat == IMAGE_FORMAT_ARGB8888) {
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, header->width, header->height);
if (pixbuf == NULL) {
goto pixbufallocerror;
}
uint32_t stride = gdk_pixbuf_get_rowstride(pixbuf);
guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
for (i = 0; i < header->height; i++)
for (j = 0; j < header->width; j++) {
pixels[stride*i + 4*j + 1] = context->buffer[pos++];
pixels[stride*i + 4*j + 2] = context->buffer[pos++];
pixels[stride*i + 4*j + 3] = context->buffer[pos++];
pixels[stride*i + 4*j + 0] = context->buffer[pos++];
}
} else if(header->highResImageFormat == IMAGE_FORMAT_BGRA8888) {
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, header->width, header->height);
if (pixbuf == NULL) {
goto pixbufallocerror;
}
uint32_t stride = gdk_pixbuf_get_rowstride(pixbuf);
guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
for (i = 0; i < header->height; i++)
for (j = 0; j < header->width; j++) {
pixels[stride*i + 4*j + 2] = context->buffer[pos++];
pixels[stride*i + 4*j + 1] = context->buffer[pos++];
pixels[stride*i + 4*j + 0] = context->buffer[pos++];
pixels[stride*i + 4*j + 3] = context->buffer[pos++];
}
} else if(header->highResImageFormat == IMAGE_FORMAT_RGBA16161616F) {
// won't accept 16 bit color depth so I have to convert it to 8 bit
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, header->width, header->height);
if (pixbuf == NULL) {
goto pixbufallocerror;
}
uint32_t stride = gdk_pixbuf_get_rowstride(pixbuf);
guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
const uint8_t *hdrdata = context->buffer + pos;
for (i = 0; i < header->height; i++) {
for (j = 0; j < header->width; j++) {
pixels[stride*i + 4*j + 0] = lrintf(read_float16(&hdrdata) * 255);
pixels[stride*i + 4*j + 1] = lrintf(read_float16(&hdrdata) * 255);
pixels[stride*i + 4*j + 2] = lrintf(read_float16(&hdrdata) * 255);
pixels[stride*i + 4*j + 3] = lrintf(read_float16(&hdrdata) * 255);
}
}
} else if(header->highResImageFormat == IMAGE_FORMAT_RGBA16161616) {
// won't accept 16 bit color depth so I have to convert it to 8 bit
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, header->width, header->height);
if (pixbuf == NULL) {
goto pixbufallocerror;
}
uint32_t stride = gdk_pixbuf_get_rowstride(pixbuf);
guchar* pixels = gdk_pixbuf_get_pixels(pixbuf);
const uint8_t *hdrdata = context->buffer + pos;
for (i = 0; i < header->height; i++) {
for (j = 0; j < header->width; j++) {
pixels[stride*i + 4*j + 0] = read_le16(&hdrdata) / 257;
pixels[stride*i + 4*j + 1] = read_le16(&hdrdata) / 257;
pixels[stride*i + 4*j + 2] = read_le16(&hdrdata) / 257;
pixels[stride*i + 4*j + 3] = read_le16(&hdrdata) / 257;
pos += 8;
}
}
} else {
goto unsupported;
}
return pixbuf;
pixbufallocerror:
g_set_error (
error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
"Could not allocate pixbuf object");
return NULL;
unsupported:
/*fprintf(stderr,"Unsupported VTF format - version %u.%u, lowres format %u, highres format %u.\n",
header->version[0], header->version[1], header->lowResImageFormat, header->highResImageFormat);*/
g_set_error (
error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
"Can't read this yet.");
return NULL;
}
static gboolean
gdk_pixbuf__vtf_image_stop_load (gpointer context_ptr, GError **error)
{
VtfContext *context = (VtfContext *) context_ptr;
gboolean retval = TRUE;
if (context->buffer_data_size < sizeof(VtfHeader))
goto corrupt;
VtfHeader header;
memcpy(&header, context->buffer, sizeof(header));
if(header.signature[0] != 'V' || header.signature[1] != 'T' || header.signature[2] != 'F' || header.signature[3] != 0 || header.frames == 0)
goto corrupt;
if (header.version[0] < 7 || (header.version[0] == 7 && header.version[1] < 2))
header.depth = 1;
if (header.version[0] < 7 || (header.version[0] == 7 && header.version[1] < 3))
header.resources = 0;
uint fulldata = vtf_offset(&header, 0, 0, 0, -1);
if (context->buffer_data_size < fulldata)
goto corrupt;
uint base = context->buffer_data_size - fulldata;
GdkPixbufSimpleAnim *anim = gdk_pixbuf_simple_anim_new(header.width, header.height, 8);
gdk_pixbuf_simple_anim_set_loop(anim, TRUE);
for (int i=0; i < header.frames; i++) {
GdkPixbuf *pixbuf = gdk_pixbuf__vtf_load_frame(&header, context, error,
base + vtf_offset(&header, i, 0, 0, 0));
if (!pixbuf) {
g_object_unref(anim);
goto end;
}
gdk_pixbuf_simple_anim_add_frame(anim, pixbuf);
if (i == 0)
context->prepared_func(pixbuf, GDK_PIXBUF_ANIMATION(anim), context->user_data);
}
end:
g_free(context->buffer);
g_free(context);
return retval;
corrupt:
g_set_error (
error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
"File corrupt or incomplete");
retval = FALSE;
goto end;
}
static gboolean
gdk_pixbuf__vtf_image_load_increment (gpointer context_ptr,
const guchar *data,
guint size,
GError **error)
{
VtfContext* context = (VtfContext*) context_ptr;
context->buffer_data_size += size;
if(context->buffer_data_size > context->buffer_size) {
while(context->buffer_data_size > context->buffer_size)
context->buffer_size *= 2;
context->buffer = g_realloc(context->buffer, context->buffer_size);
if(context->buffer == NULL) {
g_set_error (
error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
("Not enough memory"));
return FALSE;
}
}
memcpy(context->buffer + context->buffer_data_size - size, data, size);
return TRUE;
}
#ifndef INCLUDE_vtf
#define MODULE_ENTRY(function) G_MODULE_EXPORT void function
#else
#define MODULE_ENTRY(function) void _gdk_pixbuf__vtf_ ## function
#endif
MODULE_ENTRY (fill_vtable) (GdkPixbufModule* module)
{
module->begin_load = gdk_pixbuf__vtf_image_begin_load;
module->stop_load = gdk_pixbuf__vtf_image_stop_load;
module->load_increment = gdk_pixbuf__vtf_image_load_increment;
}
MODULE_ENTRY (fill_info) (GdkPixbufFormat *info)
{
static GdkPixbufModulePattern signature[] = {
{ "VTF\0", NULL, 100 },
{ NULL, NULL, 0 }
};
static gchar * mime_types[] = {
"image/x-vtf",
NULL
};
static gchar * extensions[] = {
"vtf",
NULL
};
info->name = "vtf";
info->signature = signature;
info->description = "Valve Texture format";
info->mime_types = mime_types;
info->extensions = extensions;
info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
info->flags = 0;
info->license = "LGPL";
}