File: write.c

package info (click to toggle)
glide 2002.04.10ds1-21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 59,668 kB
  • sloc: ansic: 290,122; asm: 23,305; sh: 8,090; pascal: 3,854; makefile: 1,258; perl: 73
file content (395 lines) | stat: -rw-r--r-- 11,744 bytes parent folder | download | duplicates (10)
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
/*
** THIS SOFTWARE IS SUBJECT TO COPYRIGHT PROTECTION AND IS OFFERED ONLY
** PURSUANT TO THE 3DFX GLIDE GENERAL PUBLIC LICENSE. THERE IS NO RIGHT
** TO USE THE GLIDE TRADEMARK WITHOUT PRIOR WRITTEN PERMISSION OF 3DFX
** INTERACTIVE, INC. A COPY OF THIS LICENSE MAY BE OBTAINED FROM THE 
** DISTRIBUTOR OR BY CONTACTING 3DFX INTERACTIVE INC(info@3dfx.com). 
** THIS PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER 
** EXPRESSED OR IMPLIED. SEE THE 3DFX GLIDE GENERAL PUBLIC LICENSE FOR A
** FULL TEXT OF THE NON-WARRANTY PROVISIONS.  
** 
** USE, DUPLICATION OR DISCLOSURE BY THE GOVERNMENT IS SUBJECT TO
** RESTRICTIONS AS SET FORTH IN SUBDIVISION (C)(1)(II) OF THE RIGHTS IN
** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013,
** AND/OR IN SIMILAR OR SUCCESSOR CLAUSES IN THE FAR, DOD OR NASA FAR
** SUPPLEMENT. UNPUBLISHED RIGHTS RESERVED UNDER THE COPYRIGHT LAWS OF
** THE UNITED STATES.  
** 
** COPYRIGHT 3DFX INTERACTIVE, INC. 1999, ALL RIGHTS RESERVED
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "texusint.h"

/*
 * The only two file formats we can write are: 3df and tga.
 */
static  char *Version = "1.1";
static char* aspect_names[] = { "8 1", "4 1", "2 1", "1 1", 
                                                        "1 2", "1 4", "1 8" };

/*************************************** tga files ****************************/
/* 
 * Write a tga file from an ARGB8888 mipmap.
 */
static FxBool 
txWriteTGA( FILE *stream, TxMip *txMip)
{

    struct {
        FxU8 IDLength;
        FxU8 ColorMapType;
        FxU8 ImgType;
        FxU8 CMapStartLo;
        FxU8 CMapStartHi;
        FxU8 CMapLengthLo;
        FxU8 CMapLengthHi;
        FxU8 CMapDepth;
        FxU8 XOffSetLo;
        FxU8 XOffSetHi;
        FxU8 YOffSetLo;
        FxU8 YOffSetHi;
        FxU8 WidthLo;
        FxU8 WidthHi;
        FxU8 HeightLo;
        FxU8 HeightHi;
        FxU8 PixelDepth;
        FxU8 ImageDescriptor;
    } tgaHeader;

    FxU8        *data, *p;
    FxU32       *data32;
    int         outW, outH, w, h, i;

    if (txMip->format != GR_TEXFMT_ARGB_8888) {
        txPanic("TGA Image: Write: input format must be ARGB8888.");
    }
    if ( stream == NULL ) {
        txPanic("Bad file handle");
        return FXFALSE;
    }
    
    outW = txMip->width;
    outH = txMip->height;
    if (txMip->depth > 1) outW += outW/2;
    
    tgaHeader.IDLength         = 0;
    tgaHeader.ColorMapType     = 0;
    tgaHeader.ImgType          = 0x2;
    tgaHeader.CMapStartLo      = 0;
    tgaHeader.CMapStartHi      = 0;
    tgaHeader.CMapLengthLo     = 0;
    tgaHeader.CMapLengthHi     = 0;
    tgaHeader.CMapDepth        = 0;
    tgaHeader.XOffSetLo        = 0;
    tgaHeader.XOffSetHi        = 0;
    tgaHeader.YOffSetLo        = 0;
    tgaHeader.YOffSetHi        = 0;
    tgaHeader.WidthHi          = (FxU8)((outW >> 8) & 0xFF);
    tgaHeader.WidthLo          = (FxU8) (outW & 0xFF);
    tgaHeader.HeightHi         = (FxU8)((outH >> 8) & 0xFF);
    tgaHeader.HeightLo         = (FxU8) (outH & 0xFF);
    tgaHeader.PixelDepth       = 32;
    tgaHeader.ImageDescriptor  = 0x20;          // image always right side up.
        

    if ( fwrite( &tgaHeader, 1, 18, stream ) != 18 ) {
        txPanic("TGA Header stream write error");
        return FXFALSE;
    }

    /*
     * Allocate memory to hold all mipmaps, and copy the mipmaps.
     */
    p = data = txMalloc(outW * outH * 4);
    memset(data, 0, outW * outH * 4);

    /* Copy level 0 into malloc'd area */
    txRectCopy( data, outW * 4, txMip->data[0], txMip->width * 4, 
        txMip->width * 4, txMip->height);

    p += (txMip->width * 4);

    /* Copy the rest of the levels to the right of level 0 */
    w = txMip->width; 
    h = txMip->height; 
    for (i=1; i< txMip->depth; i++) {
        // printf("Copying: level = %d\n", i);
        if (w > 1) w >>= 1;
        if (h > 1) h >>= 1;
        txRectCopy(p, outW * 4, txMip->data[i], w * 4, w * 4, h);
        p += ( h * outW * 4);
    }

    /* Write out the data */
    data32 = (FxU32 *) data;
    for (i=outW*outH; i; i--) {
        putc(((*data32      ) & 0xff)    , stream);     
        putc(((*data32 >>  8) & 0xff)    , stream);     
        putc(((*data32 >> 16) & 0xff)    , stream);     
        putc(((*data32 >> 24) & 0xff)    , stream);     
        data32++;
    }
    return FXTRUE;
}


/*************************************** 3df files ****************************/
/* Write word, msb first */

static FxBool 
_txWrite16 (FILE *stream, FxU16 data)
{
    FxU8 byte[2];

    byte[0] = (FxU8) ((data >> 8) & 0xFF);
    byte[1] = (FxU8) ((data     ) & 0xFF);

    return (fwrite (byte, 2, 1, stream) != 1) ? FXFALSE : FXTRUE;
}

/* Write long word, msb first */

static FxBool 
_txWrite32 (FILE *stream, FxU32 data)
{
    FxU8 byte[4];

    byte[0] = (FxU8) ((data >> 24) & 0xFF);
    byte[1] = (FxU8) ((data >> 16) & 0xFF);
    byte[2] = (FxU8) ((data >>  8) & 0xFF);
    byte[3] = (FxU8) ((data      ) & 0xFF);

    return (fwrite (byte, 4, 1, stream) != 1) ? FXFALSE : FXTRUE;
}

/* Write NCC table */
static FxBool 
_txWrite3dfNCCTable (FILE *stream, FxU32 *yab)
{
    int         i;

    for (i = 0; i < 16; i++)
        if (!_txWrite16 (stream, (FxU16) (yab[i] & 0x00ff))) return FXFALSE;

    for (i = 0; i < 12; i++) 
        if (!_txWrite16 (stream, (FxU16) (yab[16+i] & 0xffff))) return FXFALSE;

    for (i = 0; i < 12; i++) 
        if (!_txWrite16 (stream, (FxU16) (yab[28+i] & 0xffff))) return FXFALSE;

    return FXTRUE;
}

static FxBool
_txWrite3dfPalTable (FILE *stream, FxU32 *pal)
{
        int     i;

        for (i=0; i<256; i++) {
                if (!_txWrite32 (stream, pal[i])) return FXFALSE;
        }
        return FXTRUE;
}

static FxBool 
txWrite3df (FILE *stream, TxMip *txMip)
{
    FxU32       i;
    FxU32       n_pixels;
    int         small_lod, large_lod, aspect;

    /* Write out the header */
    large_lod = (txMip->width > txMip->height) ? txMip->width : txMip->height;
    small_lod = large_lod >> (txMip->depth - 1);
    aspect    = txAspectRatio(txMip->width, txMip->height);

    // printf("Format = %d\n", txMip->format);
    // printf("Format = %s\n", Format_Name[txMip->format]);

    // printf("Writing header...\n");
    if (EOF == fprintf (stream, 
        "3df v%s\n%s\nlod range: %d %d\naspect ratio: %s\n",
        Version, 
        Format_Name[txMip->format],
        small_lod,
        large_lod,
        aspect_names[aspect])) return FXFALSE;

    /* write out ncc table if necessary */
    // printf("Writing NCC...\n");
    if ((txMip->format == GR_TEXFMT_YIQ_422) ||
        (txMip->format == GR_TEXFMT_AYIQ_8422)) {
                if (!_txWrite3dfNCCTable (stream, txMip->pal)) return FXFALSE;
    } 

    else if ((txMip->format == GR_TEXFMT_P_8) ||
        (txMip->format == GR_TEXFMT_AP_88)) {
                if (!_txWrite3dfPalTable (stream, txMip->pal)) return FXFALSE;
    } 


    /* write out mipmap image data */
    // printf("Writing mipmaps (%d bytes)...\n", txMip->size);
    if (txMip->format < GR_TEXFMT_16BIT) {
        n_pixels = txMip->size;
        if (n_pixels != fwrite (txMip->data[0], 1, n_pixels, stream)) {
                printf("Bad Bad Bad!\n");
                return FXFALSE;
        }
    }
    else if (txMip->format < GR_TEXFMT_32BIT) {
        FxU16* data = (FxU16 *) txMip->data[0];
        n_pixels = txMip->size >> 1;

        for (i = 0; i < n_pixels; i ++)
                if (FXFALSE == _txWrite16 (stream, *data++)) return FXFALSE;
    }
    else {
        FxU32* data = (FxU32*) txMip->data[0];
        n_pixels = txMip->size >> 2;

        for (i = 0; i < n_pixels; i ++)
                if (FXFALSE == _txWrite32 (stream, *data++)) return FXFALSE;
    }
    return FXTRUE;
}

void
txMipWrite(TxMip *txMip, char *file, char *ext, int split)
{
    int         tgaformat;
    FILE        *stream;
    char        filename[128];
    int         i, w, h;
    TxMip       splitImg;

    if ((txMip->width  & (txMip->width  - 1)) ||
        (txMip->height & (txMip->height - 1))) {
                txPanic("txMipWrite: size not power of 2!");
    }

    if (strcmp(ext, ".tga") && strcmp(ext, ".3df")) {
                txPanic("txMipWrite: Bad output format");
    }

    tgaformat = (strcmp(ext, ".tga") == 0);
    if (tgaformat && (txMip->format != GR_TEXFMT_ARGB_8888)) {
        txPanic("txMipWrite: TGA format must be ARGB_8888");
    }

    /* If not split, write out a single file */
    if (!split) {
        strcpy(filename, file);
        strcat(filename, ext);
        if( txVerbose )
          {
            printf("Writing file \"%s\" (format: %s)\n", 
                   filename, Format_Name[txMip->format]);
          }
        stream = fopen(filename, "wb");
        if (stream == NULL) {
                txPanic("Unable to open output file.");
        }
        if (tgaformat) {
                if (!txWriteTGA( stream, txMip)) {
                        txPanic("txMipWrite: Write failed.");
                }
        } else {
                if (!txWrite3df( stream, txMip)) {
                        txPanic("txMipWrite: Write failed.");
                }
        }
        fclose(stream);
        return;
    }

    /* Otherwise, we need to write out separate output files */
    w = txMip->width;
    h = txMip->height;

    for (i=0; i<txMip->depth; i++) {
        char    suffix[2];

        splitImg = *txMip;              // Copy everything first, including palette.

        // Then change stuff.
        splitImg.format = txMip->format;
        splitImg.width  = w;
        splitImg.height = h;
        splitImg.size   = w * h * GR_TEXFMT_SIZE( txMip->format );
        splitImg.depth  = 1;
        splitImg.data[0] = txMip->data[i];

        // manufacture a new name.
        suffix[0] = '0' + i;
        suffix[1] = 0;
        strcpy(filename, file);
        strcat(filename, suffix);
        strcat(filename, ext);
        stream = fopen(filename, "wb");
        if (stream == NULL) {
                txPanic("Unable to open output file.");
        }
        if (tgaformat) {
                if (!txWriteTGA( stream, &splitImg)) {
                        txPanic("txMipWrite: Write failed.");
                }
        } else {
                if (!txWrite3df( stream, &splitImg)) {
                        txPanic("txMipWrite: Write failed.");
                }
        }
        fclose(stream);
        if (w > 1) w >>= 1;
        if (h > 1) h >>= 1;
    }
}

FxBool txWrite( Gu3dfInfo *info, FILE *fp, FxU32 flags )
{
  TxMip mip;

  mip.format    = info->header.format;
  mip.width     = info->header.width;
  mip.height    = info->header.height;
#ifdef GLIDE3
  mip.depth     = info->header.large_lod - info->header.small_lod + 1;
#else
  mip.depth     = info->header.small_lod - info->header.large_lod + 1;
#endif
  mip.size      = info->mem_required;
  mip.data[0]   = info->data;
  if( mip.format == GR_TEXFMT_P_8 || mip.format == GR_TEXFMT_AP_88 )
    {
      memcpy( mip.pal, info->table.palette.data, sizeof( FxU32 ) * 256 );
    }
  if( mip.format == GR_TEXFMT_YIQ_422 || mip.format == GR_TEXFMT_AYIQ_8422 )
    {
      txNccToPal( mip.pal, &info->table.nccTable);
    }
  switch( flags & TX_WRITE_MASK )
    {
    case TX_WRITE_3DF:
      if( !txWrite3df( fp, &mip ) )
        return FXFALSE;
      break;
    case TX_WRITE_TGA:
      if( mip.format == GR_TEXFMT_YIQ_422 || 
          mip.format == GR_TEXFMT_AYIQ_8422 )
        {
          txPanic( "Don't know how to write NCC textures\n" );
        }
      if( !txWriteTGA( fp, &mip ) )
        return FXFALSE;
      break;
    default:
      txPanic( "Unknown texture write format" );
      break;
    }
  return FXTRUE;
}