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
|
/*
* ppmtpspu.c - Read a raw PPM file on stdin and write an uncompressed
* Spectrum file on stdout.
*
* Copyright (C) 1990, Steve Belczyk
*/
#include <assert.h>
#include <stdio.h>
#include "pm_c_util.h"
#include "mallocvar.h"
#include "shhopt.h"
#include "pam.h"
#define SPU_WIDTH 320
#define SPU_HEIGHT 200
struct CmdlineInfo {
/* All the information the user supplied in the command line,
in a form easy for the program to use.
*/
const char * inputFileName; /* Name of input file */
unsigned int dithflag;
/* dithering flag */
};
static void
parseCommandLine(int argc, const char ** argv,
struct CmdlineInfo * const cmdlineP) {
/*----------------------------------------------------------------------------
Parse the program arguments (given by argc and argv) into a form
the program can deal with more easily -- a cmdline_info structure.
If the syntax is invalid, issue a message and exit the program via
pm_error().
Note that the file spec array we return is stored in the storage that
was passed to us as the argv array.
-----------------------------------------------------------------------------*/
optEntry * option_def; /* malloc'ed */
optStruct3 opt; /* set by OPTENT3 */
unsigned int option_def_index;
unsigned int d0Spec, d2Spec, d4Spec;
MALLOCARRAY_NOFAIL(option_def, 100);
option_def_index = 0; /* incremented by OPTENT3 */
OPTENT3(0, "d0", OPT_FLAG,
NULL, &d0Spec, 0);
OPTENT3(0, "d2", OPT_FLAG,
NULL, &d2Spec, 0);
OPTENT3(0, "d4", OPT_FLAG,
NULL, &d4Spec, 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, (char **)argv, opt, sizeof(opt), 0);
/* Uses and sets argc, argv, and some of *cmdlineP and others. */
if (d4Spec)
cmdlineP->dithflag = 4;
else if (d2Spec)
cmdlineP->dithflag = 2;
else if (d0Spec)
cmdlineP->dithflag = 0;
else
cmdlineP->dithflag = 2;
if (argc-1 < 1)
cmdlineP->inputFileName = "-";
else {
cmdlineP->inputFileName = argv[1];
if (argc-1 > 1)
pm_error("Program takes zero or one argument (filename). You "
"specified %u", argc-1);
}
}
/* This is the stuff to remember about each pixel */
struct PixelType {
unsigned int index4; /* 4-bit color, used in bitmap */
unsigned int x; /* Pixel's original x-position */
unsigned int popularity; /* Popularity of this pixel's color */
unsigned int color9; /* 9-bit color this pixel actually got */
};
typedef struct {
int index[SPU_WIDTH][16]; /* Indices into the 48 color entries */
} Index48;
typedef struct {
/* These are the palettes, 3 16-color palettes per each of 200 scan lines */
int pal[SPU_HEIGHT][48]; /* -1 means free */
} Pal;
static void
initializePalette(Pal * const palP) {
/*----------------------------------------------------------------------------
Set palettes to zero
-----------------------------------------------------------------------------*/
unsigned int row;
for (row = 0; row < SPU_HEIGHT; ++row) {
unsigned int j;
for (j = 0; j < 48; ++j)
palP->pal[row][j] = 0;
}
}
static int
findIndex(unsigned int const col,
unsigned int const index) {
/*----------------------------------------------------------------------------
Given an x-coordinate and a color index, return the corresponding
Spectrum palette index.
-----------------------------------------------------------------------------*/
int r, x1;
x1 = 10 * index; /* initial value */
if (index & 0x1)
x1 -= 5;
else
++x1;
r = index; /* initial value */
if ((col >= x1) && (col < (x1+160)))
r += 16;
if (col >= (x1+160))
r += 32;
return r;
}
static void
setup48(Index48 * const index48P) {
/*----------------------------------------------------------------------------
For each pixel position, set up the indices into the 48-color
palette
-----------------------------------------------------------------------------*/
unsigned int col;
for (col = 0; col < SPU_WIDTH; ++col) {
unsigned int i;
for (i = 0; i < 16; ++i)
index48P->index[col][i] = findIndex(col, i);
}
}
static void
dither(unsigned int const row,
tuple * const tuplerow,
unsigned int const dithflag,
struct PixelType * const pixelType) {
static int const dith4[4][4] = {
{ 0, 8, 2, 10 },
{ 12, 4, 14, 6 },
{ 3, 11, 1, 9 },
{ 15, 7, 13, 5 }
};
static int const dith2[2][2] = {
{ 0, 2 },
{ 3, 1 }
};
unsigned int c[3]; /* An element for each plane */
unsigned int col;
for (col = 0; col < SPU_WIDTH; ++col) {
unsigned int plane;
for (plane = 0; plane < 3; ++plane) {
unsigned int t;
c[plane] = ((tuplerow[col][plane] & 0xe0) >> 5) & 0x7;
/* initial value */
switch (dithflag) {
case 0:
break;
case 2:
t = (tuplerow[col][plane] & 0x18 ) >> 3;
if (t > dith2[col%2][row%2])
++c[plane];
break;
case 4:
t = (tuplerow[col][plane] & 0x1e) >> 1;
if (t > dith4[col%4][row%4])
++c[plane];
break;
}
c[plane] = MIN(7, c[plane]);
}
pixelType[col].color9 = (c[0] << 6) | (c[1] << 3) | c[2];
pixelType[col].x = col;
}
}
static void
swapPixelType(struct PixelType * const pixelType,
unsigned int const i,
unsigned int const j) {
struct PixelType const w = pixelType[i];
pixelType[i] = pixelType[j];
pixelType[j] = w;
}
static void
sort(struct PixelType * const pixelType,
unsigned int const left,
unsigned int const right) {
/*----------------------------------------------------------------------------
Sort pixelType[] from element 'left' to (not including) element 'right' in
increasing popularity.
Good ol' Quicksort.
-----------------------------------------------------------------------------*/
unsigned int const pivot = pixelType[(left+right-1)/2].popularity;
unsigned int i, j;
/* Rearrange so that everything less than 'pivot' is on the left side of
the subject array slice and everything greater than is on the right
side and elements equal could be on either side (we won't know until
we're done where the dividing line between the sides is), then sort
those two sides.
*/
assert(left < right);
for (i = left, j = right; i < j; ) {
while (pixelType[i].popularity < pivot)
++i;
while (pixelType[j-1].popularity > pivot)
--j;
if (i < j) {
/* An element not less popular than pivot is to the left of a
pixel not more popular than pivot, so swap them. Note that we
could be swapping equal (pivot-valued) elements. Though the
swap isn't necessary, moving 'i' and 'j' is.
*/
swapPixelType(pixelType, i, j-1);
++i;
--j;
}
}
if (j - left > 1)
sort(pixelType, left, j);
if (right - i > 1)
sort(pixelType, i, right);
}
static void
computePalette(struct PixelType * const pixelType) {
unsigned int hist[512]; /* Count for each color */
unsigned int col;
unsigned int i;
/* Uses popularity algorithm */
/* Count the occurrences of each color */
for (i = 0; i < 512; ++i)
hist[i] = 0;
for (col = 0; col < SPU_WIDTH; ++col)
++hist[pixelType[col].color9];
/* Set the popularity of each pixel's color */
for (col = 0; col < SPU_WIDTH; ++col)
pixelType[col].popularity = hist[pixelType[col].color9];
/* Sort to find the most popular colors */
sort(pixelType, 0, SPU_WIDTH);
}
static int
dist9(unsigned int const x,
unsigned int const y) {
/*----------------------------------------------------------------------------
Return the distance between two 9-bit colors.
-----------------------------------------------------------------------------*/
unsigned int i;
unsigned int d;
int x0[3], y0[3];
x0[0] = (x & 0x007);
x0[1] = (x & 0x038) >> 3;
x0[2] = (x & 0x1c0) >> 6;
y0[0] = (y & 0x007);
y0[1] = (y & 0x038) >> 3;
y0[2] = (y & 0x1c0) >> 6;
for (i = 0, d = 0; i < 3; ++i) {
unsigned int const t = x0[i] - y0[i];
d += t * t;
}
return d;
}
static void
convertPixel(unsigned int const col,
unsigned int const row,
struct PixelType * const pixelType,
Pal * const palP,
const Index48 * const index48P) {
int ifree;
unsigned int const x = pixelType[col].x;
unsigned int const c = pixelType[col].color9;
ifree = -1; /* Set if free slot found */
/* Handle each possible case, from easiest to hardest, in the hopes the
easy ones are more frequent.
*/
/* If it wants black, it gets it */
if (c == 0)
pixelType[col].index4 = 0;
else {
/* If another pixel is using this color, it gets it */
unsigned int i;
for (i = 1; i < 15; ++i) {
/* Check for free slots while we're here */
if ((ifree < 0) &&
(palP->pal[row][index48P->index[x][i]] == -1))
ifree = i;
else if (c == palP->pal[row][index48P->index[x][i]]) {
pixelType[col].index4 = i;
return;
}
}
/* If there are no free slots, we must use the closest entry in use so
far
*/
if (ifree < 0) {
unsigned int i;
unsigned int d;
unsigned int b;
for (i = 1, d = 1000; i < 15; ++i) {
unsigned int const t =
dist9(c, palP->pal[row][index48P->index[x][i]]);
if (t < d) {
d = t;
b = i;
}
}
/* See if it would be better off with black */
if (d > dist9(c, 0))
b = 0;
pixelType[col].index4 = b;
} else {
/* Use up a slot and give it what it wants */
palP->pal[row][index48P->index[x][ifree]] = c;
pixelType[col].index4 = ifree;
}
}
}
static void
setPixel(unsigned int const col,
unsigned int const row,
unsigned int const c,
short * const screen) {
unsigned int index, bit, plane;
/* In the next few statements, the bit operations are a little
quicker, but the arithmetic versions are easier to read and
maybe more portable. Please try swapping them if you have
trouble on your machine.
*/
/* index = (80 * row) + 4 * (col / 16); */
index = (row << 6) + (row << 4) + ((col >> 4) << 2);
/* bit = 0x8000 >> (col % 16); */
bit = 0x8000 >> (col & 0x0f);
for (plane=0; plane<4; ++plane) {
if (c & (1 << plane))
screen[index + plane] |= bit;
}
}
static void
convertRow(unsigned int const row,
struct PixelType * const pixelType,
Pal * const palP,
const Index48 * const index48P,
short * const screen) {
unsigned int i;
/* Mark palette entries as all free */
for (i = 0; i < 48; ++i)
palP->pal[row][i] = -1;
/* Mark reserved palette entries */
palP->pal[row][0] = palP->pal[row][15] = palP->pal[row][16] = 0;
palP->pal[row][31] = palP->pal[row][32] = palP->pal[row][47] = 0;
/* Convert each pixel */
{
/* Process the pixels in order of the popularity of the desired
color
*/
int col;
for (col = SPU_WIDTH-1; col >= 0; --col) {
convertPixel(col, row, pixelType, palP, index48P);
setPixel(pixelType[col].x, row, pixelType[col].index4, screen);
}
}
}
static void
doRow(unsigned int const row,
tuple * const tuplerow,
unsigned int const dithflag,
const Index48 * const index48P,
Pal * const palP,
short * const screen) {
struct PixelType pixelType[SPU_WIDTH];
/* Dither and reduce to 9 bits */
dither(row, tuplerow, dithflag, pixelType);
/* Compute the best colors for this row */
computePalette(pixelType);
/* Convert this row */
convertRow(row, pixelType, palP, index48P, screen);
}
static void
writeScreen(const short * const screen) {
/* Write the bitmap */
unsigned int i;
for (i = 0; i < 16000; ++i) {
char const c0 = 0xff & (screen[i] >> 8);
char const c1 = 0xff & screen[i];
putchar(c0);
putchar(c1);
}
}
static void
writePalettes(const Pal * const palP) {
unsigned int row;
for (row = 1; row < SPU_HEIGHT; ++row) {
unsigned int i;
for (i = 0; i < 48; ++i) {
int const p = palP->pal[row][i];
unsigned int const q =
((p & 0x1c0) << 2) +
((p & 0x038) << 1) +
((p & 0x007) << 0);
putchar((q >> 8) & 0xff);
putchar((q >> 0) & 0xff);
}
}
}
static void
writeSpu(const short * const screen,
const Pal * const palP ) {
writeScreen(screen);
writePalettes(palP);
}
int
main (int argc, const char ** argv) {
struct pam pam;
struct CmdlineInfo cmdline;
FILE * ifP;
tuple ** tuples;
Pal pal;
Index48 index48;
short screen[16000]; /* This is the ST's video RAM */
pm_proginit(&argc, argv);
parseCommandLine(argc, argv, &cmdline);
ifP = pm_openr(cmdline.inputFileName);
tuples = pnm_readpam(ifP, &pam, PAM_STRUCT_SIZE(tuple_type));
if (pam.depth < 3)
pm_error("Image must be RGB, so at least 3 deep. This image is "
"only %u deep", pam.depth);
if ((pam.width != SPU_WIDTH) || (pam.height != SPU_HEIGHT))
pm_error("Image size must be %ux%u. This one is %u x %u",
SPU_WIDTH, SPU_HEIGHT, pam.width, pam.height);
{
unsigned int i;
for (i = 0; i < 16000; screen[i++] = 0);
}
setup48(&index48);
initializePalette(&pal);
{
/* Set first row of screen data to black */
unsigned int i;
for (i = 0; i < 80; ++i)
screen[i] = 0;
}
{
unsigned int row;
for (row = 0; row < SPU_HEIGHT; ++row)
doRow(row, tuples[row], cmdline.dithflag, &index48, &pal, screen);
}
writeSpu(screen, &pal);
return 0;
}
|