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
|
/* pc1toppm.c - read a Degas Elite PC1 file and produce a PPM
Copyright (C) 1991 by Steve Belczyk (seb3@gte.com) and Jef Poskanzer.
Copyright (C) 2004 by Roine Gustafsson (roine@users.sourceforge.net)
Converted to modern Netpbm code style by Bryan Henderson April 2004.
That work is contributed to the public domain by Bryan.
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.
Algorithm for PC1 compression from "gimp-degas" GIMP plugin
by Markus F.X.J. Oberhumer
*/
#include "ppm.h"
/* The code has not been tested for other resolutions than 320x200 */
static unsigned int const rows = 200;
static unsigned int const cols = 320;
static unsigned int const planes = 4; /* 4 bits for 16 colors */
static pixval const maxval = 7;
unsigned int const colsPerBlock = 16;
#define BLOCKS (cols/colsPerBlock)
#define ROWBYTES (cols * planes / 8)
#define BLOCKBYTES (colsPerBlock / 8)
#define PLANEBYTES (ROWBYTES / planes)
#define ROWSHORTS (cols * planes / 16)
#define BLOCKSHORTS (colsPerBlock * planes / 16)
#define PLANESHORTS (ROWSHORTS / planes)
static void
readPalette(FILE * const ifP,
pixel (* const palP)[]) {
/* Read the palette. */
unsigned int i;
for (i = 0; i < 16; ++i) {
unsigned short j;
pm_readbigshortu(ifP, &j);
PPM_ASSIGN((*palP)[i],
(j & 0x700) >> 8,
(j & 0x070) >> 4,
(j & 0x007) >> 0);
}
}
static void
processStretch(unsigned int const countbyte,
FILE * const ifP,
unsigned int * const colP,
unsigned char ** const lineposP) {
if (countbyte <= 127) {
/* countbyte+1 literal bytes follows */
unsigned int const count = countbyte + 1;
unsigned int i;
if (*colP + count > ROWBYTES)
pm_error("Error in PC1 file. "
"A literal stretch extends beyond the end of a row");
for (i = 0; i < count; ++i) {
*(*lineposP)++ = getc(ifP);
++(*colP);
}
} else {
/* next byte repeated 257-countbyte times */
unsigned char const duplicated_color = getc(ifP);
unsigned int const count = 257 - countbyte;
unsigned int i;
if (*colP + count > ROWBYTES)
pm_error("Error in PC1 file. "
"A run extends beyond the end of a row.");
for (i = 0; i < count; ++i) {
*(*lineposP)++ = duplicated_color;
++(*colP);
}
}
}
static void
readPc1(FILE * const ifP,
pixel (*palP)[],
unsigned char (*bufferP)[]) {
unsigned short j;
unsigned int row;
unsigned char* bufferCursor;
/* Check resolution word */
pm_readbigshortu(ifP, &j);
if (j != 0x8000)
pm_error("This is not a PC1 file. "
"The first two bytes are 0x%04X instead of 0x8000", j);
readPalette(ifP, palP);
/* Read the screen data */
bufferCursor = &(*bufferP)[0];
for (row = 0; row < rows; ++row) {
unsigned int col;
for (col = 0; col < ROWBYTES;) {
unsigned int const countbyte = getc(ifP);
processStretch(countbyte, ifP, &col, &bufferCursor);
}
}
}
static void
reInterleave(unsigned char const buffer[],
unsigned short (* const screenP)[]) {
/* The buffer is in one plane for each line, to optimize packing */
/* Re-interleave to match the Atari screen layout */
unsigned short * const screen = *screenP;
unsigned int row;
for (row = 0; row < rows; ++row) {
unsigned int block;
for (block = 0; block < BLOCKS; ++block) {
unsigned int plane;
for (plane = 0; plane < planes; ++plane) {
unsigned int const blockIndex =
row*ROWBYTES + plane*PLANEBYTES + block*BLOCKBYTES;
screen[row*ROWSHORTS + block*BLOCKSHORTS + plane] =
(buffer[blockIndex+0] << 8) + (buffer[blockIndex+1]);
}
}
}
}
static void
writePpm(FILE * const ofP,
unsigned short const screen[],
pixel const palette[]) {
pixel* pixelrow;
unsigned int row;
ppm_writeppminit(ofP, cols, rows, maxval, 0);
pixelrow = ppm_allocrow(cols);
for (row = 0; row < rows; ++row) {
/* Each row is arranged into blocks of 16 columns. Each block
is represented by 4 shorts (16 bit integers). The
first is for Plane 0, the second for Plane 1, etc. Each short
contains the bits for that plane for each of the 16 columns,
arranged from most significant bit to least in increasing column
number.
*/
unsigned int col0ScreenIndex = cols/16*planes * row;
unsigned int col;
for (col = 0; col < cols; ++col) {
unsigned int const ind = col0ScreenIndex + col/16 * planes;
unsigned int const b = 0x8000 >> (col % 16);
unsigned int plane;
unsigned int colorIndex;
colorIndex = 0;
for (plane = 0; plane < planes; ++plane)
if (b & screen[ind+plane])
colorIndex |= (1 << plane);
pixelrow[col] = palette[colorIndex];
}
ppm_writeppmrow(ofP, pixelrow, cols, maxval, 0);
}
ppm_freerow(pixelrow);
}
int
main(int argc, char ** argv) {
const char * inputFilename;
FILE* ifP;
pixel palette[16]; /* Degas palette */
static unsigned short screen[32000/2];
/* simulates the Atari's video RAM */
static unsigned char buffer[32000];
/* simulates the Atari's video RAM */
ppm_init(&argc, argv);
if (argc-1 == 0)
inputFilename = "-";
else if (argc-1 == 1)
inputFilename = argv[1];
else
pm_error("There is at most one argument to this program: "
"the input file name. You specified %d", argc-1);
ifP = pm_openr(inputFilename);
readPc1(ifP, &palette, &buffer);
pm_close(ifP);
reInterleave(buffer, &screen);
writePpm(stdout, screen, palette);
pm_close(stdout);
return 0;
}
|