about summary refs log tree commit diff
path: root/converter/ppm/sputoppm.c
blob: acf61c1b751d1b0c35682c3803ad8a04428e12ae (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
/* sputoppm.c - read an uncompressed Spectrum file and produce a PPM
**
** Copyright (C) 1991 by Steve Belczyk and 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.
*/

#include "ppm.h"

#define ROWS 200
#define COLS 320
#define MAXVAL 7


typedef struct {
    pixel pal[ROWS][48];  /* Spectrum palettes, three per row */
} Pal;



static void
readPalettes(FILE * const ifP,
             Pal *  const palP) {

    unsigned int row;

    /* Clear the first palette line. */
    {
        unsigned int j;
        for (j = 0; j < 48; ++j)
            PPM_ASSIGN(palP->pal[0][j], 0, 0, 0);
    }
    /* Read the palettes. */
    for (row = 1; row < ROWS; ++row) {
        unsigned int j;
        for (j = 0; j < 48; ++j) {
            short k;
            pm_readbigshort(ifP, &k);
            PPM_ASSIGN(palP->pal[row][j],
                       (k & 0x700) >> 8,
                       (k & 0x070) >> 4,
                       (k & 0x007) >> 0);
        }
    }
}



int
main(int argc, const char ** argv) {

    FILE * ifP;
    unsigned int i;
    pixel * pixelrow;
    unsigned int row;
    Pal pal;
    short screen[ROWS*COLS/4];      /* simulates the Atari's video RAM */

    pm_proginit(&argc, argv);

    /* Check args. */
    if ( argc > 2 )
        pm_usage( "[spufile]" );

    if ( argc == 2 )
        ifP = pm_openr( argv[1] );
    else
        ifP = stdin;

    /* Read the SPU file */

    /* Read the screen data. */
    for (i = 0; i < ROWS*COLS/4; ++i)
        pm_readbigshort(ifP, &screen[i]);

    readPalettes(ifP, &pal);

    pm_close(ifP);

    /* Ok, get set for writing PPM. */
    ppm_writeppminit(stdout, COLS, ROWS, MAXVAL, 0);
    pixelrow = ppm_allocrow(COLS);

    /* Now do the conversion. */
    for (row = 0; row < ROWS; ++row) {
        unsigned int col;
        for (col = 0; col < COLS; ++col) {
            /* Compute pixel value. */
            unsigned int const ind = 80 * row + ((col >> 4) << 2);
            unsigned int const b = 0x8000 >> (col & 0xf);
            unsigned int c;
            unsigned int plane;
            unsigned int x1;

            c = 0;  /* initial value */
            for (plane = 0; plane < 4; ++plane) {
                if (b & screen[ind + plane])
                    c |= (1 << plane);
            }
            /* Compute palette index. */
            x1 = 10 * c;
            if ((c & 1) != 0)
                x1 -= 5;
            else
                ++x1;
            if ((col >= x1 ) && (col < (x1 + 160)))
                c += 16;
            if (col >= (x1 + 160))
                c += 32;

            /* Set the proper color. */
            pixelrow[col] = pal.pal[row][c];
        }
        ppm_writeppmrow(stdout, pixelrow, COLS, MAXVAL, 0);
    }

    ppm_freerow(pixelrow);
    pm_close(stdout);

    return 0;
}