about summary refs log tree commit diff
path: root/converter/ppm/tgatoppm.c
blob: 82225516f0fd444f37ae4a8e31a8fe5b7ae0230d (plain) (blame)
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
/* tgatoppm.c - read a TrueVision Targa file and write a portable pixmap
**
** Partially based on tga2rast, version 1.0, by Ian MacPhedran.
**
** Copyright (C) 1989 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#define _DEFAULT_SOURCE 1  /* New name for SVID & BSD source defines */
#define _BSD_SOURCE 1      /* Make sure strdup() is in string.h */
#define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */

#include <string.h>

#include "pm_c_util.h"
#include "ppm.h"
#include "tga.h"
#include "shhopt.h"
#include "nstring.h"

#define MAXCOLORS 16384

static int mapped, rlencoded;

static pixel ColorMap[MAXCOLORS];
static gray AlphaMap[MAXCOLORS];

struct cmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    const char *input_filename;
    unsigned int headerdump;
    const char *alpha_filename;
    unsigned int alpha_stdout;
};



static void
parseCommandLine(int argc, char ** argv,
                   struct cmdlineInfo *cmdlineP) {
/*----------------------------------------------------------------------------
   Note that many of the strings that this function returns in the
   *cmdlineP structure are actually in the supplied argv array.  And
   sometimes, one of these strings is actually just a suffix of an entry
   in argv!
-----------------------------------------------------------------------------*/
    optEntry *option_def = malloc(100*sizeof(optEntry));
        /* Instructions to OptParseOptions2 on how to parse our options.
         */
    optStruct3 opt;

    unsigned int option_def_index;

    unsigned int alpha_spec;

    option_def_index = 0;   /* incremented by OPTENTRY */
    OPTENT3(0, "headerdump", OPT_FLAG,   NULL, &cmdlineP->headerdump,   0);
    OPTENT3(0, "debug",      OPT_FLAG,   NULL, &cmdlineP->headerdump,   0);
    OPTENT3(0, "alphaout",   OPT_STRING, &cmdlineP->alpha_filename,
            &alpha_spec, 0);

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = FALSE;  /* We have no parms that are negative numbers */

    pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0);
        /* Uses and sets argc, argv, and some of *cmdlineP and others. */

    if (argc - 1 == 0)
        cmdlineP->input_filename = "-";  /* he wants stdin */
    else if (argc - 1 == 1)
        cmdlineP->input_filename = strdup(argv[1]);
    else
        pm_error("Too many arguments.  The only argument accepted "
                 "is the input file specification");

    if (alpha_spec &&
        streq(cmdlineP->alpha_filename, "-"))
        cmdlineP->alpha_stdout = 1;
    else
        cmdlineP->alpha_stdout = 0;

    if (!alpha_spec)
        cmdlineP->alpha_filename = NULL;
}



static unsigned char
getbyte(FILE * const ifP) {

    unsigned char c;

    if ( fread( (char*) &c, 1, 1, ifP ) != 1 )
        pm_error( "EOF / read error" );

    return c;
}



static int RLE_count = 0, RLE_flag = 0;



static void
handleRun(FILE * const ifP,
          bool * const repeatP) {

    if (rlencoded) {
        if (RLE_count == 0) {
            /* Have to restart run. */
            unsigned char i;
            i = getbyte(ifP);
            RLE_flag = (i & 0x80);
            if (RLE_flag == 0) {
                /* Stream of unencoded pixels. */
                RLE_count = i + 1;
            } else {
                /* Single pixel replicated. */
                RLE_count = i - 127;
            }
            /* Decrement count & get pixel. */
            --RLE_count;
            *repeatP = false;
        } else {
            /* Have already read count & (at least) first pixel. */
            --RLE_count;
            if (RLE_flag != 0) {
                /* Replicated pixels. */
                *repeatP = true;
            } else
                *repeatP = false;
        }
    } else
        *repeatP = false;
}



static void
getPixel(FILE *  const ifP,
         pixel * const dest,
         int     const size,
         gray *  const alphaP) {

    static pixval red, grn, blu;
    static pixval alpha;
    static unsigned int l;
    unsigned char j, k;
    bool repeat;
        /* Next pixel is just a repeat (from an encoded run) */

    handleRun(ifP, &repeat);

    if (repeat) {
        /* Use red, grn, blu, alpha, and l from prior call to getPixel */
    } else {
        /* Read appropriate number of bytes, break into RGB. */
        switch (size) {
        case 8:             /* Grayscale, read and triplicate. */
            red = grn = blu = l = getbyte(ifP);
            alpha = 0;
            break;

        case 16:            /* 5 bits each of red green and blue. */
        case 15:            /* Watch byte order. */
            j = getbyte(ifP);
            k = getbyte(ifP);
            l = ((unsigned int)k << 8) + j;
            red = (k & 0x7C) >> 2;
            grn = ((k & 0x03) << 3) + ((j & 0xE0) >> 5);
            blu = j & 0x1F;
            alpha = 0;
            break;

        case 32:            /* 8 bits each of blue, green, red, and alpha */
        case 24:            /* 8 bits each of blue, green, and red. */
            blu = getbyte(ifP);
            grn = getbyte(ifP);
            red = getbyte(ifP);
            if (size == 32)
                alpha = getbyte(ifP);
            else
                alpha = 0;
            l = 0;
            break;

        default:
            pm_error("unknown pixel size (#2) - %d", size);
        }
    }
    if (mapped) {
        *dest = ColorMap[l];
        *alphaP = AlphaMap[l];
    } else {
        PPM_ASSIGN(*dest, red, grn, blu);
        *alphaP = alpha;
    }
}



static void
readtga(FILE * const ifP, struct ImageHeader * tgaP) {

    unsigned char flags;
    ImageIDField junk;

    tgaP->IdLength = getbyte( ifP );
    tgaP->CoMapType = getbyte( ifP );
    tgaP->ImgType = getbyte( ifP );
    tgaP->Index_lo = getbyte( ifP );
    tgaP->Index_hi = getbyte( ifP );
    tgaP->Length_lo = getbyte( ifP );
    tgaP->Length_hi = getbyte( ifP );
    tgaP->CoSize = getbyte( ifP );
    tgaP->X_org_lo = getbyte( ifP );
    tgaP->X_org_hi = getbyte( ifP );
    tgaP->Y_org_lo = getbyte( ifP );
    tgaP->Y_org_hi = getbyte( ifP );
    tgaP->Width_lo = getbyte( ifP );
    tgaP->Width_hi = getbyte( ifP );
    tgaP->Height_lo = getbyte( ifP );
    tgaP->Height_hi = getbyte( ifP );
    tgaP->PixelSize = getbyte( ifP );
    flags = getbyte( ifP );
    tgaP->AttBits = flags & 0xf;
    tgaP->Rsrvd = ( flags & 0x10 ) >> 4;
    tgaP->OrgBit = ( flags & 0x20 ) >> 5;
    tgaP->IntrLve = ( flags & 0xc0 ) >> 6;

    if ( tgaP->IdLength != 0 )
        fread( junk, 1, (int) tgaP->IdLength, ifP );
}



static void
get_map_entry(FILE * const ifP, pixel * Value, int Size, gray * Alpha) {

    unsigned char j, k, r, g, b, a;

    /* Read appropriate number of bytes, break into rgb & put in map. */
    switch ( Size )
    {
    case 8:             /* Grayscale, read and triplicate. */
        r = g = b = getbyte( ifP );
        a = 0;
        break;

    case 16:            /* 5 bits each of red green and blue. */
    case 15:            /* Watch for byte order. */
        j = getbyte( ifP );
        k = getbyte( ifP );
        r = ( k & 0x7C ) >> 2;
        g = ( ( k & 0x03 ) << 3 ) + ( ( j & 0xE0 ) >> 5 );
        b = j & 0x1F;
        a = 0;
        break;

    case 32:            /* 8 bits each of blue, green, red, and alpha */
    case 24:            /* 8 bits each of blue green and red. */
        b = getbyte( ifP );
        g = getbyte( ifP );
        r = getbyte( ifP );
        if ( Size == 32 )
            a = getbyte( ifP );
        else
            a = 0;
        break;

    default:
        pm_error( "unknown colormap pixel size (#2) - %d", Size );
    }
    PPM_ASSIGN( *Value, r, g, b );
    *Alpha = a;
}



static void
dumpHeader(struct ImageHeader const tga_head) {
    const char * imgTypeName;
    switch(tga_head.ImgType) {
    case TGA_Map:      imgTypeName = "TGA_Map";      break;
    case TGA_RGB:      imgTypeName = "TGA_RGB";      break;
    case TGA_Mono:     imgTypeName = "TGA_Mono";     break;
    case TGA_RLEMap:   imgTypeName = "TGA_RLEMap";   break;
    case TGA_RLERGB:   imgTypeName = "TGA_RLERGB";   break;
    case TGA_RLEMono:  imgTypeName = "TGA_RLEMono";  break;
    case TGA_CompMap:  imgTypeName = "TGA_CompMap";  break;
    case TGA_CompMap4: imgTypeName = "TGA_CompMap4"; break;
    default:           imgTypeName = "unknown";
    }
    pm_message( "IdLength = %d", (int) tga_head.IdLength );
    pm_message( "CoMapType = %d", (int) tga_head.CoMapType );
    pm_message( "ImgType = %d (%s)", (int) tga_head.ImgType, imgTypeName );
    pm_message( "Index_lo = %d", (int) tga_head.Index_lo );
    pm_message( "Index_hi = %d", (int) tga_head.Index_hi );
    pm_message( "Length_lo = %d", (int) tga_head.Length_lo );
    pm_message( "Length_hi = %d", (int) tga_head.Length_hi );
    pm_message( "CoSize = %d", (int) tga_head.CoSize );
    pm_message( "X_org_lo = %d", (int) tga_head.X_org_lo );
    pm_message( "X_org_hi = %d", (int) tga_head.X_org_hi );
    pm_message( "Y_org_lo = %d", (int) tga_head.Y_org_lo );
    pm_message( "Y_org_hi = %d", (int) tga_head.Y_org_hi );
    pm_message( "Width_lo = %d", (int) tga_head.Width_lo );
    pm_message( "Width_hi = %d", (int) tga_head.Width_hi );
    pm_message( "Height_lo = %d", (int) tga_head.Height_lo );
    pm_message( "Height_hi = %d", (int) tga_head.Height_hi );
    pm_message( "PixelSize = %d", (int) tga_head.PixelSize );
    pm_message( "AttBits = %d", (int) tga_head.AttBits );
    pm_message( "Rsrvd = %d", (int) tga_head.Rsrvd );
    pm_message( "OrgBit = %d", (int) tga_head.OrgBit );
    pm_message( "IntrLve = %d", (int) tga_head.IntrLve );
}



int
main(int argc, char * argv[]) {

    struct cmdlineInfo cmdline;
    struct ImageHeader tga_head;
    FILE* ifP;
    FILE *imageout_file, *alpha_file;
    int rows, cols, row, realrow, truerow, baserow;
    int maxval;
    pixel** pixels;   /* The image array in ppm format */
    gray** alpha;     /* The alpha channel array in pgm format */

    ppm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.input_filename);

    if (cmdline.alpha_stdout)
        alpha_file = stdout;
    else if (cmdline.alpha_filename == NULL)
        alpha_file = NULL;
    else
        alpha_file = pm_openw(cmdline.alpha_filename);

    if (cmdline.alpha_stdout)
        imageout_file = NULL;
    else
        imageout_file = stdout;

    /* Read the Targa file header. */
    readtga(ifP, &tga_head);

    if (cmdline.headerdump)
        dumpHeader(tga_head);

    rows = ((int) tga_head.Height_lo) + ((int) tga_head.Height_hi) * 256;
    cols = ((int) tga_head.Width_lo)  + ((int) tga_head.Width_hi)  * 256;

    switch (tga_head.ImgType) {
    case TGA_Map:
    case TGA_RGB:
    case TGA_Mono:
    case TGA_RLEMap:
    case TGA_RLERGB:
    case TGA_RLEMono:
        break;
    case TGA_CompMap:
    case TGA_CompMap4:
        pm_error("Targa image type %d (compressed color-mapped data). "
                 "Cannot handle this format.", tga_head.ImgType);
        break;
    default:
        pm_error("unknown Targa image type %d", tga_head.ImgType);
    }

    if (tga_head.ImgType == TGA_Map ||
        tga_head.ImgType == TGA_RLEMap ||
        tga_head.ImgType == TGA_CompMap ||
        tga_head.ImgType == TGA_CompMap4)
    { /* Color-mapped image */
        if (tga_head.CoMapType != 1)
            pm_error(
                "mapped image (type %d) with color map type != 1",
                tga_head.ImgType );
        mapped = true;
        /* Figure maxval from CoSize. */
        switch (tga_head.CoSize) {
        case 8:
        case 24:
        case 32:
            maxval = 255;
            break;

        case 15:
        case 16:
            maxval = 31;
            break;

        default:
        pm_error(
        "unknown colormap pixel size - %d", tga_head.CoSize );
        }
    } else {
        /* Not colormap, so figure maxval from PixelSize. */
        mapped = false;
        switch ( tga_head.PixelSize ) {
        case 8:
        case 24:
        case 32:
            maxval = 255;
            break;

        case 15:
        case 16:
            maxval = 31;
            break;

        default:
            pm_error("unknown pixel size - %d", tga_head.PixelSize);
        }
    }

    /* If required, read the color map information. */
    if ( tga_head.CoMapType != 0 ) {
        unsigned int i;
        unsigned int temp1, temp2;

        temp1 = tga_head.Index_lo + tga_head.Index_hi * 256;
        temp2 = tga_head.Length_lo + tga_head.Length_hi * 256;
        if ((temp1 + temp2 + 1) >= MAXCOLORS)
            pm_error("too many colors - %d", (temp1 + temp2 + 1));
        for (i = temp1; i < (temp1 + temp2); ++i)
            get_map_entry(ifP, &ColorMap[i], (int) tga_head.CoSize,
                          &AlphaMap[i]);
    }

    /* Check run-length encoding. */
    if (tga_head.ImgType == TGA_RLEMap ||
        tga_head.ImgType == TGA_RLERGB ||
        tga_head.ImgType == TGA_RLEMono)
        rlencoded = 1;
    else
        rlencoded = 0;

    /* Read the Targa file body and convert to portable format. */
    pixels = ppm_allocarray( cols, rows );
    alpha = pgm_allocarray( cols, rows );
    truerow = 0;
    baserow = 0;
    for (row = 0; row < rows; ++row) {
        unsigned int col;

        realrow = truerow;
        if (tga_head.OrgBit == 0)
            realrow = rows - realrow - 1;

        for (col = 0; col < cols; ++col)
            getPixel(ifP, &(pixels[realrow][col]), (int) tga_head.PixelSize,
                     &(alpha[realrow][col]));
        if (tga_head.IntrLve == TGA_IL_Four)
            truerow += 4;
        else if (tga_head.IntrLve == TGA_IL_Two)
            truerow += 2;
        else
            ++truerow;
        if (truerow >= rows)
            truerow = ++baserow;
    }
    pm_close(ifP);

    if (imageout_file)
        ppm_writeppm(imageout_file, pixels, cols, rows, (pixval) maxval, 0);
    if (alpha_file)
        pgm_writepgm(alpha_file, alpha, cols, rows, (pixval) maxval, 0);
    if (imageout_file)
        pm_close(imageout_file);
    if (alpha_file)
        pm_close(alpha_file);

    return 0;
}