-
Notifications
You must be signed in to change notification settings - Fork 6
/
s2tc_compress.c
739 lines (695 loc) · 18.1 KB
/
s2tc_compress.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
/*
* Copyright (C) 2011 Rudolf Polzer All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* RUDOLF POLZER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define S2TC_LICENSE_IDENTIFIER s2tc_compress_license
#include "s2tc_license.h"
#include <getopt.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#ifdef ENABLE_RUNTIME_LINKING
#include <dlfcn.h>
#include <GL/gl.h>
typedef void (tx_compress_dxtn_t)(GLint srccomps, GLint width, GLint height,
const GLubyte *srcPixData, GLenum destformat,
GLubyte *dest, GLint dstRowStride);
tx_compress_dxtn_t *tx_compress_dxtn = NULL;
bool load_libraries(const char *n)
{
void *l = dlopen(n, RTLD_NOW);
if(!l)
{
fprintf(stderr, "Cannot load library: %s\n", dlerror());
return false;
}
tx_compress_dxtn = (tx_compress_dxtn_t *) dlsym(l, "tx_compress_dxtn");
if(!tx_compress_dxtn)
{
fprintf(stderr, "The selected libtxc_dxtn.so does not contain all required symbols.");
dlclose(l);
return false;
}
return true;
}
#else
#include "txc_dxtn.h"
#endif
/* START stuff that originates from image.c in DarkPlaces */
/*
Thu Jul 14 21:58:02 CEST 2011
21:25:25 @divVerent | LordHavoc: http://paste.pocoo.org/show/438804/
21:25:31 @divVerent | can I have this code under a MIT-style license?
21:59:58 @LordHavoc | divVerent: yeah, have them under any license you want
22:00:11 @LordHavoc | divVerent: my attitude toward licenses is generally "WTFPL would be preferably if I could use it" :P
22:04:01 @divVerent | LordHavoc: okay, thanks
*/
int image_width, image_height;
typedef struct _TargaHeader
{
unsigned char id_length, colormap_type, image_type;
unsigned short colormap_index, colormap_length;
unsigned char colormap_size;
unsigned short x_origin, y_origin, width, height;
unsigned char pixel_size, attributes;
}
TargaHeader;
unsigned char *LoadTGA_BGRA (const unsigned char *f, int filesize)
{
int x, y, pix_inc, row_inci, runlen, alphabits;
unsigned char *image_buffer;
unsigned int *pixbufi;
const unsigned char *fin, *enddata;
TargaHeader targa_header;
unsigned int palettei[256];
union
{
unsigned int i;
unsigned char b[4];
}
bgra;
if (filesize < 19)
return NULL;
enddata = f + filesize;
targa_header.id_length = f[0];
targa_header.colormap_type = f[1];
targa_header.image_type = f[2];
targa_header.colormap_index = f[3] + f[4] * 256;
targa_header.colormap_length = f[5] + f[6] * 256;
targa_header.colormap_size = f[7];
targa_header.x_origin = f[8] + f[9] * 256;
targa_header.y_origin = f[10] + f[11] * 256;
targa_header.width = image_width = f[12] + f[13] * 256;
targa_header.height = image_height = f[14] + f[15] * 256;
targa_header.pixel_size = f[16];
targa_header.attributes = f[17];
if (image_width > 32768 || image_height > 32768 || image_width <= 0 || image_height <= 0)
{
printf("LoadTGA: invalid size\n");
return NULL;
}
/* advance to end of header */
fin = f + 18;
/* skip TARGA image comment (usually 0 bytes) */
fin += targa_header.id_length;
/* read/skip the colormap if present (note: according to the TARGA spec it */
/* can be present even on 1color or greyscale images, just not used by */
/* the image data) */
if (targa_header.colormap_type)
{
if (targa_header.colormap_length > 256)
{
printf("LoadTGA: only up to 256 colormap_length supported\n");
return NULL;
}
if (targa_header.colormap_index)
{
printf("LoadTGA: colormap_index not supported\n");
return NULL;
}
if (targa_header.colormap_size == 24)
{
for (x = 0;x < targa_header.colormap_length;x++)
{
bgra.b[0] = *fin++;
bgra.b[1] = *fin++;
bgra.b[2] = *fin++;
bgra.b[3] = 255;
palettei[x] = bgra.i;
}
}
else if (targa_header.colormap_size == 32)
{
memcpy(palettei, fin, targa_header.colormap_length*4);
fin += targa_header.colormap_length * 4;
}
else
{
printf("LoadTGA: Only 32 and 24 bit colormap_size supported\n");
return NULL;
}
}
/* check our pixel_size restrictions according to image_type */
switch (targa_header.image_type & ~8)
{
case 2:
if (targa_header.pixel_size != 24 && targa_header.pixel_size != 32)
{
printf("LoadTGA: only 24bit and 32bit pixel sizes supported for type 2 and type 10 images\n");
return NULL;
}
break;
case 3:
/* set up a palette to make the loader easier */
for (x = 0;x < 256;x++)
{
bgra.b[0] = bgra.b[1] = bgra.b[2] = x;
bgra.b[3] = 255;
palettei[x] = bgra.i;
}
/* fall through to colormap case */
case 1:
if (targa_header.pixel_size != 8)
{
printf("LoadTGA: only 8bit pixel size for type 1, 3, 9, and 11 images supported\n");
return NULL;
}
break;
default:
printf("LoadTGA: Only type 1, 2, 3, 9, 10, and 11 targa RGB images supported, image_type = %i\n", targa_header.image_type);
return NULL;
}
if (targa_header.attributes & 0x10)
{
printf("LoadTGA: origin must be in top left or bottom left, top right and bottom right are not supported\n");
return NULL;
}
/* number of attribute bits per pixel, we only support 0 or 8 */
alphabits = targa_header.attributes & 0x0F;
if (alphabits != 8 && alphabits != 0)
{
printf("LoadTGA: only 0 or 8 attribute (alpha) bits supported\n");
return NULL;
}
image_buffer = (unsigned char *)malloc(image_width * image_height * 4);
if (!image_buffer)
{
printf("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
return NULL;
}
/* If bit 5 of attributes isn't set, the image has been stored from bottom to top */
if ((targa_header.attributes & 0x20) == 0)
{
pixbufi = (unsigned int*)image_buffer + (image_height - 1)*image_width;
row_inci = -image_width*2;
}
else
{
pixbufi = (unsigned int*)image_buffer;
row_inci = 0;
}
pix_inc = 1;
if ((targa_header.image_type & ~8) == 2)
pix_inc = (targa_header.pixel_size + 7) / 8;
switch (targa_header.image_type)
{
case 1: /* colormapped, uncompressed */
case 3: /* greyscale, uncompressed */
if (fin + image_width * image_height * pix_inc > enddata)
break;
for (y = 0;y < image_height;y++, pixbufi += row_inci)
for (x = 0;x < image_width;x++)
*pixbufi++ = palettei[*fin++];
break;
case 2:
/* BGR or BGRA, uncompressed */
if (fin + image_width * image_height * pix_inc > enddata)
break;
if (targa_header.pixel_size == 32 && alphabits)
{
for (y = 0;y < image_height;y++)
memcpy(pixbufi + y * (image_width + row_inci), fin + y * image_width * pix_inc, image_width*4);
}
else
{
for (y = 0;y < image_height;y++, pixbufi += row_inci)
{
for (x = 0;x < image_width;x++, fin += pix_inc)
{
bgra.b[0] = fin[0];
bgra.b[1] = fin[1];
bgra.b[2] = fin[2];
bgra.b[3] = 255;
*pixbufi++ = bgra.i;
}
}
}
break;
case 9: /* colormapped, RLE */
case 11: /* greyscale, RLE */
for (y = 0;y < image_height;y++, pixbufi += row_inci)
{
for (x = 0;x < image_width;)
{
if (fin >= enddata)
break; /* error - truncated file */
runlen = *fin++;
if (runlen & 0x80)
{
/* RLE - all pixels the same color */
runlen += 1 - 0x80;
if (fin + pix_inc > enddata)
break; /* error - truncated file */
if (x + runlen > image_width)
break; /* error - line exceeds width */
bgra.i = palettei[*fin++];
for (;runlen--;x++)
*pixbufi++ = bgra.i;
}
else
{
/* uncompressed - all pixels different color */
runlen++;
if (fin + pix_inc * runlen > enddata)
break; /* error - truncated file */
if (x + runlen > image_width)
break; /* error - line exceeds width */
for (;runlen--;x++)
*pixbufi++ = palettei[*fin++];
}
}
if (x != image_width)
{
/* pixbufi is useless now */
printf("LoadTGA: corrupt file\n");
break;
}
}
break;
case 10:
/* BGR or BGRA, RLE */
if (targa_header.pixel_size == 32 && alphabits)
{
for (y = 0;y < image_height;y++, pixbufi += row_inci)
{
for (x = 0;x < image_width;)
{
if (fin >= enddata)
break; /* error - truncated file */
runlen = *fin++;
if (runlen & 0x80)
{
/* RLE - all pixels the same color */
runlen += 1 - 0x80;
if (fin + pix_inc > enddata)
break; /* error - truncated file */
if (x + runlen > image_width)
break; /* error - line exceeds width */
bgra.b[0] = fin[0];
bgra.b[1] = fin[1];
bgra.b[2] = fin[2];
bgra.b[3] = fin[3];
fin += pix_inc;
for (;runlen--;x++)
*pixbufi++ = bgra.i;
}
else
{
/* uncompressed - all pixels different color */
runlen++;
if (fin + pix_inc * runlen > enddata)
break; /* error - truncated file */
if (x + runlen > image_width)
break; /* error - line exceeds width */
for (;runlen--;x++)
{
bgra.b[0] = fin[0];
bgra.b[1] = fin[1];
bgra.b[2] = fin[2];
bgra.b[3] = fin[3];
fin += pix_inc;
*pixbufi++ = bgra.i;
}
}
}
if (x != image_width)
{
/* pixbufi is useless now */
printf("LoadTGA: corrupt file\n");
break;
}
}
}
else
{
for (y = 0;y < image_height;y++, pixbufi += row_inci)
{
for (x = 0;x < image_width;)
{
if (fin >= enddata)
break; /* error - truncated file */
runlen = *fin++;
if (runlen & 0x80)
{
/* RLE - all pixels the same color */
runlen += 1 - 0x80;
if (fin + pix_inc > enddata)
break; /* error - truncated file */
if (x + runlen > image_width)
break; /* error - line exceeds width */
bgra.b[0] = fin[0];
bgra.b[1] = fin[1];
bgra.b[2] = fin[2];
bgra.b[3] = 255;
fin += pix_inc;
for (;runlen--;x++)
*pixbufi++ = bgra.i;
}
else
{
/* uncompressed - all pixels different color */
runlen++;
if (fin + pix_inc * runlen > enddata)
break; /* error - truncated file */
if (x + runlen > image_width)
break; /* error - line exceeds width */
for (;runlen--;x++)
{
bgra.b[0] = fin[0];
bgra.b[1] = fin[1];
bgra.b[2] = fin[2];
bgra.b[3] = 255;
fin += pix_inc;
*pixbufi++ = bgra.i;
}
}
}
if (x != image_width)
{
/* pixbufi is useless now */
printf("LoadTGA: corrupt file\n");
break;
}
}
}
break;
default:
/* unknown image_type */
break;
}
return image_buffer;
}
/* in can be the same as out */
void Image_MipReduce32(const unsigned char *in, unsigned char *out, int *width, int *height, int destwidth, int destheight)
{
const unsigned char *inrow;
int x, y, nextrow;
/* note: if given odd width/height this discards the last row/column of
* pixels, rather than doing a proper box-filter scale down
*/
inrow = in;
nextrow = *width * 4;
if (*width > destwidth)
{
*width >>= 1;
if (*height > destheight)
{
/* reduce both */
*height >>= 1;
for (y = 0;y < *height;y++, inrow += nextrow * 2)
{
for (in = inrow, x = 0;x < *width;x++)
{
out[0] = (unsigned char) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
out[1] = (unsigned char) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
out[2] = (unsigned char) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
out[3] = (unsigned char) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
out += 4;
in += 8;
}
}
}
else
{
/* reduce width */
for (y = 0;y < *height;y++, inrow += nextrow)
{
for (in = inrow, x = 0;x < *width;x++)
{
out[0] = (unsigned char) ((in[0] + in[4]) >> 1);
out[1] = (unsigned char) ((in[1] + in[5]) >> 1);
out[2] = (unsigned char) ((in[2] + in[6]) >> 1);
out[3] = (unsigned char) ((in[3] + in[7]) >> 1);
out += 4;
in += 8;
}
}
}
}
else
{
if (*height > destheight)
{
/* reduce height */
*height >>= 1;
for (y = 0;y < *height;y++, inrow += nextrow * 2)
{
for (in = inrow, x = 0;x < *width;x++)
{
out[0] = (unsigned char) ((in[0] + in[nextrow ]) >> 1);
out[1] = (unsigned char) ((in[1] + in[nextrow+1]) >> 1);
out[2] = (unsigned char) ((in[2] + in[nextrow+2]) >> 1);
out[3] = (unsigned char) ((in[3] + in[nextrow+3]) >> 1);
out += 4;
in += 4;
}
}
}
}
}
unsigned char *FS_LoadFile(const char *fn, int *len)
{
unsigned char *buf = NULL;
int n;
FILE *f = fn ? fopen(fn, "rb") : stdin;
*len = 0;
if(!f)
return NULL;
for(;;)
{
unsigned char *newbuf = (unsigned char *) realloc(buf, *len + 65536);
if(newbuf)
{
buf = newbuf;
}
else
{
if(fn)
fclose(f);
free(buf);
*len = 0;
return NULL;
}
n = fread(buf + *len, 1, 65536, f);
if(n < 0)
{
if(fn)
fclose(f);
free(buf);
*len = 0;
return NULL;
}
*len += n;
if(n < 65536)
break;
}
if(fn)
fclose(f);
return buf;
}
/* END of darkplaces stuff */
uint32_t LittleLong(uint32_t w)
{
union
{
unsigned char c[4];
uint32_t u;
}
un;
un.c[0] = w;
un.c[1] = w >> 8;
un.c[2] = w >> 16;
un.c[3] = w >> 24;
return un.u;
}
int usage(const char *me)
{
fprintf(stderr, "usage:\n"
"%s \n"
" [-i infile.tga]\n"
" [-o outfile.dds]\n"
" [-t {DXT1|DXT3|DXT5}]\n"
#ifdef ENABLE_RUNTIME_LINKING
" [-l path_to_libtxc_dxtn.so]\n"
#endif
,
me);
return 1;
}
int main(int argc, char **argv)
{
const char *infile = NULL, *outfile = NULL;
FILE *outfh;
unsigned char *picdata, *pic;
int x, mipcount, piclen;
const char *fourcc;
int blocksize;
GLenum dxt = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
#ifdef ENABLE_RUNTIME_LINKING
const char *library = "libtxc_dxtn.so";
#endif
int opt;
while((opt = getopt(argc, argv, "i:o:t:"
#ifdef ENABLE_RUNTIME_LINKING
"l:"
#endif
)) != -1)
{
switch(opt)
{
case 'i':
infile = optarg;
break;
case 'o':
outfile = optarg;
break;
case 't':
if(!strcasecmp(optarg, "DXT1"))
dxt = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
else if(!strcasecmp(optarg, "DXT3"))
dxt = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
else if(!strcasecmp(optarg, "DXT5"))
dxt = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
else
return usage(argv[0]);
break;
#ifdef ENABLE_RUNTIME_LINKING
case 'l':
library = optarg;
break;
#endif
default:
return usage(argv[0]);
break;
}
}
#ifdef ENABLE_RUNTIME_LINKING
if(!load_libraries(library))
return 1;
#endif
outfh = outfile ? fopen(outfile, "wb") : stdout;
if(!outfh)
{
printf("opening output failed\n");
return 2;
}
picdata = FS_LoadFile(infile, &piclen);
if(!picdata)
{
printf("FS_LoadFile failed\n");
return 2;
}
pic = LoadTGA_BGRA(picdata, piclen);
for(x = 0; x < image_width*image_height; ++x) {
unsigned char h = pic[4*x];
pic[4*x] = pic[4*x+2];
pic[4*x+2] = h;
}
mipcount = 0;
while(image_width >= (1 << mipcount) || image_height >= (1 << mipcount))
++mipcount;
/* now, (1 << mipcount) >= width, height */
switch(dxt)
{
case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
blocksize = 8;
fourcc = "DXT1";
break;
case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
blocksize = 16;
fourcc = "DXT3";
break;
default:
case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
blocksize = 16;
fourcc = "DXT5";
break;
}
{
uint32_t zero = LittleLong(0);
bool alphapixels = false;
int x, y;
uint32_t dds_picsize, dds_mipcount, dds_width, dds_height;
for(y = 0; y < image_height; ++y)
for(x = 0; x < image_width; ++x)
if(pic[(y*image_width+x)*4+3] != 255)
{
alphapixels = true;
break;
}
dds_picsize = LittleLong(((image_width+3)/4) * ((image_height+3)/4) * blocksize);
dds_mipcount = LittleLong(mipcount);
dds_width = LittleLong(image_width);
dds_height = LittleLong(image_height);
/* 0 */
fwrite("DDS ", 4, 1, outfh);
fwrite("\x7c\x00\x00\x00", 4, 1, outfh);
fwrite("\x07\x10\x0a\x00", 4, 1, outfh);
fwrite(&dds_height, 4, 1, outfh);
fwrite(&dds_width, 4, 1, outfh);
fwrite(&dds_picsize, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
fwrite(&dds_mipcount, 4, 1, outfh);
/* 32 */
fwrite(&zero, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
/* 64 */
fwrite(&zero, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
fwrite("\x20\x00\x00\x00", 4, 1, outfh);
fwrite(alphapixels ? "\x05\x00\x00\x00" : "\x04\x00\x00\x00", 4, 1, outfh);
fwrite(fourcc, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
/* 96 */
fwrite(&zero, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
fwrite("\x08\x10\x40\x00", 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
fwrite(&zero, 4, 1, outfh);
}
for(;;)
{
int blocks_w = (image_width + 3) / 4;
int blocks_h = (image_height + 3) / 4;
GLubyte *obuf = (GLubyte *) malloc(blocksize * blocks_w * blocks_h);
tx_compress_dxtn(4, image_width, image_height, pic, dxt, obuf, blocks_w * blocksize);
fwrite(obuf, blocksize * blocks_w * blocks_h, 1, outfh);
free(obuf);
if(image_width == 1 && image_height == 1)
break;
Image_MipReduce32(pic, pic, &image_width, &image_height, 1, 1);
}
if(outfile)
fclose(outfh);
return 0;
}