Debian Codegraph @

terps/level9/bitmap.c

raw symbols SCIP
1 /***********************************************************************\
2 *
3 * Level 9 interpreter
4 * Version 5.2
5 * Copyright (c) 1996-2025 Glen Summers and contributors.
6 * Contributions from David Kinder, Alan Staniforth, Simon Baldwin,
7 * Dieter Baron and Andreas Scherrer.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
22 *
23 \***********************************************************************/
24
25 #include <math.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "level9.h"
30
31 extern Bitmap* bitmap;
32
33 L9UINT32 filelength(FILE *f);
34 void L9Allocate(L9BYTE **ptr,L9UINT32 Size);
35
36 L9BYTE* bitmap_load(char* file, L9UINT32* size)
37 {
38 L9BYTE* data = NULL;
39 FILE* f = fopen(file,"rb");
40 if (f)
41 {
42 *size = filelength(f);
43 L9Allocate(&data,*size);
44 if (fread(data,1,*size,f) != *size)
45 {
46 free(data);
47 data = NULL;
48 }
49 fclose(f);
50 }
51 return data;
52 }
53
54 Bitmap* bitmap_alloc(int x, int y)
55 {
56 Bitmap* bitmap = NULL;
57 L9Allocate((L9BYTE**)&bitmap,sizeof(Bitmap)+(x*y));
58
59 bitmap->width = x;
60 bitmap->height = y;
61 bitmap->bitmap = ((L9BYTE*)bitmap)+sizeof(Bitmap);
62 bitmap->npalette = 0;
63 return bitmap;
64 }
65
66 /*
67 A PC or ST palette colour is a sixteen bit value in which the low three nybbles
68 hold the rgb colour values. The lowest nybble holds the blue value, the
69 second nybble the blue value and the third nybble the red value. (The high
70 nybble is ignored). Within each nybble, only the low three bits are used
71 IE the value can only be 0-7 not the full possible 0-15 and so the MSbit in
72 each nybble is always 0.
73 */
74 Colour bitmap_pcst_colour(int big, int small)
75 {
76 Colour col;
77 L9UINT32 r = big & 0xF;
78 L9UINT32 g = (small >> 4) & 0xF;
79 L9UINT32 b = small & 0xF;
80
81 r *= 0x49; r >>= 1;
82 g *= 0x49; g >>= 1;
83 b *= 0x49; b >>= 1;
84
85 col.red = (L9BYTE)(r&0xFF);
86 col.green = (L9BYTE)(g&0xFF);
87 col.blue = (L9BYTE)(b&0xFF);
88 return col;
89 }
90
91 /*
92 ST Bitmaps
93
94 On the ST different graphics file formats were used for the early V4
95 games (Knight Orc, Gnome Ranger) and the later V4 games (Lancelot,
96 Ingrid's Back, Time & Magik and Scapeghost).
97 */
98
99 /*
100 Extracts the number of pixels requested from an eight-byte data block (4 bit-
101 planes) passed to it.
102
103 Note: On entry each one of four pointers is set to point to the start of each
104 bit-plane in the block. The function then indexes through each byte in
105 each bit plane. and uses shift and mask operations to extract each four
106 bit pixel into an L9PIXEL.
107
108 The bit belonging to the pixel in the current byte of the current bit-
109 plane is moved to its position in an eight-bit pixel. The byte is then
110 masked by a value to select only that bit and added to the final pixel
111 value.
112 */
113 L9UINT32 bitmap_st1_decode_pixels(L9BYTE* bitmap, L9BYTE* data, L9UINT32 count, L9UINT32 pixels)
114 {
115 L9UINT32 bitplane_length = count / 4; /* length of each bitplane */
116 L9BYTE *bitplane0 = data; /* address of bit0 bitplane */
117 L9BYTE *bitplane1 = data + (bitplane_length); /* address of bit1 bitplane */
118 L9BYTE *bitplane2 = data + (bitplane_length * 2); /* address of bit2 bitplane */
119 L9BYTE *bitplane3 = data + (bitplane_length * 3); /* address of bit3 bitplane */
120 L9UINT32 bitplane_index, pixel_index = 0; /* index variables */
121
122 for (bitplane_index = 0; bitplane_index < bitplane_length; bitplane_index++)
123 {
124 /* build the eight pixels from the current bitplane bytes, high bit to low */
125
126 /* bit7 byte */
127 bitmap[pixel_index] = ((bitplane3[bitplane_index] >> 4) & 0x08)
128 + ((bitplane2[bitplane_index] >> 5) & 0x04)
129 + ((bitplane1[bitplane_index] >> 6) & 0x02)
130 + ((bitplane0[bitplane_index] >> 7) & 0x01);
131 if (pixels == ++pixel_index)
132 break;
133
134 /* bit6 byte */
135 bitmap[pixel_index] = ((bitplane3[bitplane_index] >> 3) & 0x08)
136 + ((bitplane2[bitplane_index] >> 4) & 0x04)
137 + ((bitplane1[bitplane_index] >> 5) & 0x02)
138 + ((bitplane0[bitplane_index] >> 6) & 0x01);
139 if (pixels == ++pixel_index)
140 break;
141
142 /* bit5 byte */
143 bitmap[pixel_index] = ((bitplane3[bitplane_index] >> 2) & 0x08)
144 + ((bitplane2[bitplane_index] >> 3) & 0x04)
145 + ((bitplane1[bitplane_index] >> 4) & 0x02)
146 + ((bitplane0[bitplane_index] >> 5) & 0x01);
147 if (pixels == ++pixel_index)
148 break;
149
150 /* bit4 byte */
151 bitmap[pixel_index] = ((bitplane3[bitplane_index] >> 1) & 0x08)
152 + ((bitplane2[bitplane_index] >> 2) & 0x04)
153 + ((bitplane1[bitplane_index] >> 3) & 0x02)
154 + ((bitplane0[bitplane_index] >> 4) & 0x01);
155 if (pixels == ++pixel_index)
156 break;
157
158 /* bit3 byte */
159 bitmap[pixel_index] = ((bitplane3[bitplane_index]) & 0x08)
160 + ((bitplane2[bitplane_index] >> 1) & 0x04)
161 + ((bitplane1[bitplane_index] >> 2) & 0x02)
162 + ((bitplane0[bitplane_index] >> 3) & 0x01);
163 if (pixels == ++pixel_index)
164 break;
165
166 /* bit2 byte */
167 bitmap[pixel_index] = ((bitplane3[bitplane_index] << 1) & 0x08)
168 + ((bitplane2[bitplane_index]) & 0x04)
169 + ((bitplane1[bitplane_index] >> 1) & 0x02)
170 + ((bitplane0[bitplane_index] >> 2) & 0x01);
171 if (pixels == ++pixel_index)
172 break;
173
174 /* bit1 byte */
175 bitmap[pixel_index] = ((bitplane3[bitplane_index] << 2) & 0x08)
176 + ((bitplane2[bitplane_index] << 1) & 0x04)
177 + ((bitplane1[bitplane_index]) & 0x02)
178 + ((bitplane0[bitplane_index] >> 1) & 0x01);
179 if (pixels == ++pixel_index)
180 break;
181
182 /* bit0 byte */
183 bitmap[pixel_index] = ((bitplane3[bitplane_index] << 3) & 0x08)
184 + ((bitplane2[bitplane_index] << 2) & 0x04)
185 + ((bitplane1[bitplane_index] << 1) & 0x02)
186 + ((bitplane0[bitplane_index]) & 0x01);
187 if (pixels == ++pixel_index)
188 break;
189 }
190
191 return pixel_index;
192 }
193
194 /*
195 The ST image file has the following format. It consists of a 44 byte header
196 followed by the image data.
197
198 The header has the following format:
199 Bytes 0-31: sixteen entry ST palette
200 Bytes 32-33: padding
201 Bytes 34-35: big-endian word holding number of bitplanes needed to make
202 up a row of pixels*
203 Bytes 36-37: padding
204 Bytes 38-39: big-endian word holding number of rows in the image*
205 Bytes 40-41: padding**
206 Bytes 42-43: mask for pixels to show in last 16 pixel block. Again, this
207 is big endian
208
209 [*] these are probably big-endian unsigned longs but I have designated
210 the upper two bytes as padding because (a) Level 9 does not need
211 them as longs and (b) using unsigned shorts reduces byte sex induced
212 byte order juggling.
213 [**] not certain what this is for but I suspect that, like bytes 42-43
214 it is a mask to indicate which pixels to show, in this case in the
215 first 16 pixel block
216
217 The image data is essentially a memory dump of the video RAM representing
218 the image in lo-res mode. In lo-res mode each row is 320 pixels wide
219 and each pixel can be any one of sixteen colours - needs 4 bits to store.
220
221 In the ST video memory (in lo-res mode which we are dealing with here)
222 is organised as follows. The lowest point in memory in the frame buffer
223 represents the top-left of the screen, the highest the bottom-right.
224 Each row of pixels is stored in sequence.
225
226 Within each pixel row the pixels are stored as follows. Each row is
227 divided into groups of 16 pixels. Each sixteen pixel group is stored
228 in 8 bytes, logically four groups of two. Each two byte pair
229 is a bit-plane for that sixteen pixel group - that is it stores the
230 same bit of each pixel in that group. The first two bytes store the
231 lowest bit, the second pair the second bit &c.
232
233 The word at bytes 34-35 of the header stores the number of bitplanes
234 that make up each pixel row in the image. Multplying this number by
235 four gives the number of pixels in the row***. For title and frame
236 images that will be 320, for sub-images it will be less.
237
238 [***] Not always exactly. For GnomeRanger sub-images this value is 60
239 - implying there are 240 pixels per row. In fact there are only
240 225 pixels in each row. To identify this situation look at the
241 big-endian word in bytes 42-43 of the header. This is a mask
242 telling you the pixels to use. Each bit represents one pixel in
243 the block, with the MSBit representing the first pixel and the
244 LSbit the last.
245
246 In this situation, the file does contain the entire sixteen
247 pixel block (it has to with the bitplane arrangement) but
248 the pixels which are not part of the image are just noise. When
249 decoding the image, the L9BITMAP produced has the actual pixel
250 dimensions - the surplus pixels are discarded.
251
252 I suspect, though I have not found an instance, that in theory
253 the same situation could apply at the start of a pixel row and that
254 in this case the big-endian word at bytes 40-41 is the mask.
255
256 Having obtained the pixel dimensions of the image the function uses
257 them to allocate memory for the bitmap and then extracts the pixel
258 information from the bitmap row by row. For each row eight byte blocks
259 are read from the image data and passed to UnpackSTv1Pixels along with
260 the number of pixels to extract (usually 16, possibly less for the last
261 block in a row.)
262 */
263 L9BOOL bitmap_st1_decode(char* file, int x, int y)
264 {
265 L9BYTE* data = NULL;
266 int i, xi, yi, max_x, max_y, last_block;
267 int bitplanes_row, bitmaps_row, pixel_count, get_pixels;
268
269 L9UINT32 size;
270 data = bitmap_load(file,&size);
271 if (data == NULL)
272 return FALSE;
273
274 bitplanes_row = data[35]+data[34]*256;
275 bitmaps_row = bitplanes_row/4;
276 max_x = bitplanes_row*4;
277 max_y = data[39]+data[38]*256;
278 last_block = data[43]+data[42]*256;
279
280 /* Check if sub-image with rows shorter than max_x */
281 if (last_block != 0xFFFF)
282 {
283 /* use last_block to adjust max_x */
284 int i = 0;
285 while ((0x0001 & last_block) == 0) /* test for ls bit set */
286 {
287 last_block >>= 1; /* if not, shift right one bit */
288 i++;
289 }
290 max_x = max_x - i;
291 }
292
293 if (max_x > MAX_BITMAP_WIDTH || max_y > MAX_BITMAP_HEIGHT)
294 {
295 free(data);
296 return FALSE;
297 }
298
299 if ((x == 0) && (y == 0))
300 {
301 if (bitmap)
302 free(bitmap);
303 bitmap = bitmap_alloc(max_x,max_y);
304 }
305 if (bitmap == NULL)
306 {
307 free(data);
308 return FALSE;
309 }
310
311 if (x+max_x > bitmap->width)
312 max_x = bitmap->width-x;
313 if (y+max_y > bitmap->height)
314 max_y = bitmap->height-y;
315
316 for (yi = 0; yi < max_y; yi++)
317 {
318 pixel_count = 0;
319 for (xi = 0; xi < bitmaps_row; xi++)
320 {
321 if ((max_x - pixel_count) < 16)
322 get_pixels = max_x - pixel_count;
323 else
324 get_pixels = 16;
325
326 pixel_count += bitmap_st1_decode_pixels(
327 bitmap->bitmap+((y+yi)*bitmap->width)+x+(xi*16),
328 data+44+(yi*bitplanes_row*2)+(xi*8),8,get_pixels);
329 }
330 }
331
332 bitmap->npalette = 16;
333 for (i = 0; i < 16; i++)
334 bitmap->palette[i] = bitmap_pcst_colour(data[(i*2)],data[1+(i*2)]);
335
336 free(data);
337 return TRUE;
338 }
339
340 void bitmap_st2_name(int num, char* dir, char* out)
341 {
342 /* title picture is #30 */
343 if (num == 0)
344 num = 30;
345 sprintf(out,"%s%d.squ",dir,num);
346 }
347
348 /*
349 PC Bitmaps
350
351 On the PC different graphics file formats were used for the early V4
352 games (Knight Orc, Gnome Ranger) and the later V4 games (Lancelot,
353 Ingrid's Back, Time & Magik and Scapeghost).
354
355 The ST and the PC both use the same image file format for the later
356 V4 games (Lancelot, Ingrid's Back, Time & Magik and Scapeghost.)
357 */
358
359 void bitmap_pc_name(int num, char* dir, char* out)
360 {
361 /* title picture is #30 */
362 if (num == 0)
363 num = 30;
364 sprintf(out,"%s%d.pic",dir,num);
365 }
366
367 /*
368 The EGA standard for the IBM PCs and compatibles defines 64 colors, any
369 16 of which can be mapped to the usable palette at any given time. If
370 you display these 64 colors in numerical order, 16 at a time, you get a
371 hodgepodge of colors in no logical order. The 64 EGA color numbers are
372 assigned in a way that the numbers can easily be converted to a relative
373 intensity of each of the three phosphor colors R,G,B. If the number is
374 converted to six bit binary, the most significant three bits represent
375 the 25% level of R,G,B in that order and the least significant three
376 bits represent the 75% level of R,G,B in that order. Take EGA color 53
377 for example. In binary, 53 is 110101. Since both R bits are on, R = 1.0.
378 Of the G bits only the 25% bit is on so G = 0.25. Of the B bits only the
379 75% bit is on so B = 0.75.
380 */
381 Colour bitmap_pc1_colour(int i)
382 {
383 Colour col;
384 col.red = (((i&4)>>1) | ((i&0x20)>>5)) * 0x55;
385 col.green = ((i&2) | ((i&0x10)>>4)) * 0x55;
386 col.blue = (((i&1)<<1) | ((i&8)>>3)) * 0x55;
387 return col;
388 }
389
390 /*
391 The PC (v1) image file has the following format. It consists of a 22
392 byte header organised like this:
393
394 Byte 0: probably a file type flag
395 Byte 1: the MSB of the file's length as a word
396 Bytes 2-3: little-endian word with picture width in pixels
397 Bytes 4-5: little-endian word with picture height in pixel rows
398 Bytes 6-21: the image colour table. One EGA colour in each byte
399
400 The image data is extremely simple. The entire block is packed array
401 of 4-bit pixels - IE each byte holds two pixels - the first in the high
402 nybble, the second in the low. The pixel value is an index into the
403 image colour table. The pixels are organised with the top left first and
404 bottom left last, each row in turn.
405 */
406 L9BOOL bitmap_pc1_decode(char* file, int x, int y)
407 {
408 L9BYTE* data = NULL;
409 int i, xi, yi, max_x, max_y;
410
411 L9UINT32 size;
412 data = bitmap_load(file,&size);
413 if (data == NULL)
414 return FALSE;
415
416 max_x = data[2]+data[3]*256;
417 max_y = data[4]+data[5]*256;
418 if (max_x > MAX_BITMAP_WIDTH || max_y > MAX_BITMAP_HEIGHT)
419 {
420 free(data);
421 return FALSE;
422 }
423
424 if ((x == 0) && (y == 0))
425 {
426 if (bitmap)
427 free(bitmap);
428 bitmap = bitmap_alloc(max_x,max_y);
429 }
430 if (bitmap == NULL)
431 {
432 free(data);
433 return FALSE;
434 }
435
436 if (x+max_x > bitmap->width)
437 max_x = bitmap->width-x;
438 if (y+max_y > bitmap->height)
439 max_y = bitmap->height-y;
440
441 for (yi = 0; yi < max_y; yi++)
442 {
443 for (xi = 0; xi < max_x; xi++)
444 {
445 bitmap->bitmap[(bitmap->width*(y+yi))+(x+xi)] =
446 (data[23+((yi*max_x)/2)+(xi/2)]>>((1-(xi&1))*4)) & 0x0f;
447 }
448 }
449
450 bitmap->npalette = 16;
451 for (i = 0; i < 16; i++)
452 bitmap->palette[i] = bitmap_pc1_colour(data[6+i]);
453
454 free(data);
455 return TRUE;
456 }
457
458 /*
459 The PC (v2) image file has the following format. It consists of a 44
460 byte header followed by the image data.
461
462 The header has the following format:
463 Bytes 0-1: "datalen": length of file -1 as a big-endian word*
464 Bytes 2-3: "flagbyte1 & flagbyte2": unknown, possibly type identifiers.
465 Usually 0xFF or 0xFE followed by 0x84, 0x72, 0xFF, 0xFE or
466 some other (of a fairly small range of possibles) byte.
467 Bytes 4-35: "colour_index[]": sixteen entry palette. Basically an ST
468 palette (even if in a PC image file. Each entry is a sixteen
469 bit value in which the low three nybbles hold the rgb colour
470 values. The lowest nybble holds the blue value, the second
471 nybble the blue value and the third nybble the red value. (The
472 high nybble is ignored). Within each nybble, only the low
473 three bits are used IE the value can only be 0-7 not the full
474 possible 0-15 and so the MSbit in each nybble is always 0.**,
475 Bytes 36-37: "width": image width in pixels as a big-endian word
476 Bytes 38-39: "numrows": image height in pixel rows as a big-endian word
477 Byte 40: "seedByte": seed byte to start picture decoding.
478 Byte 41: "padByte": unknown. Possibly padding to word align the next
479 element?
480 Bytes 42-297: "pixelTable": an array of 0x100 bytes used as a lookup table
481 for pixel values
482 Bytes 298-313: "bitStripTable": an array of 0x10 bytes used as a lookup table
483 for the number of bytes to strip from the bit stream for the pixel being
484 decoded
485 Bytes 314-569: "indexByteTable": an array of 0x100 bytes used as a lookup
486 table to index into bitStripTable and pixelTable****
487
488 The encoded image data then follows ending in a 0x00 at the file length stored
489 in the first two bytes of the file. there is then one extra byte holding a
490 checksum produced by the addition of all the bytes in the file (except the first
491 two and itself)*
492
493 [*] in some PC games the file is padded out beyond this length to the
494 nearest 0x80/0x00 boundary with the byte 0x1A. The valid data in the
495 file still finishes where this word says with the checkbyte following it.
496 [**] I imagine when a game was running on a PC this standard palette
497 was algorithimcally changed to suit the graphics mode being used
498 (Hercules, MDA, CGA, EGA, MCGA, VGA &c.)
499 [***] Note also, in image 1 of PC Time & Magik I think one palette entry
500 is bad as what should be white in the image is actually set to
501 a very pale yellow. This is corrected with the display of the next
502 sub-picture and I am pretty sure it is note a decoding problem
503 here as when run on the PC the same image has the same pale yellow
504 cast.
505 [****] for detail of how all this works see below
506
507 As this file format is intended for two very different platforms the decoded
508 imaged data is in a neutral, intermediate form. Each pixel is extracted as a
509 byte with only the low four bits significant. The pixel value is an index into
510 the sixteen entry palette.
511
512 The pixel data is compressed, presumably to allow a greater number of images
513 to be distributed on the (rather small) default ST & PC floppy disks (in both
514 cases about 370 Kbytes.)*****
515
516 Here's how to decode the data. The image data is actually a contiguous bit
517 stream with the byte structure on disk having almost no relevance to the
518 encoding. We access the bit stream via a two-byte buffer arranged as a word.
519
520 Preparation:
521
522 Initially, move the first byte from the image data into the low byte of
523 theBitStreamBuffer and move the second byte of the image data into the
524 high byte of theBitStreamBuffer.
525
526 Set a counter (theBufferBitCounter) to 8 which you will use to keep track
527 of when it is necesary to refill the buffer.
528
529 Set a L9BYTE variable (theNewPixel) to byte 40 (seedByte) of the header.
530 We need to do this because as part of identifying the pixel being
531 extracted we need to know the value of the previous pixel extracted. Since
532 none exists at this point we must prime this variable with the correct
533 value.
534
535 Extraction:
536
537 Set up a loop which you will execute once for each pixel to be extracted
538 and within that loop do as follows.
539
540 Copy the low byte of theBitStreamBuffer to an L9BYTE
541 (theNewPixelIndexSelector). Examine theNewPixelIndexSelector. If this
542 is 0xFF this flags that the index to the new pixel is present as a
543 literal in the bit stream; if it is NOT 0xFF then the new pixel index
544 value has to be decoded.
545
546 If theNewPixelIndexSelector is NOT 0xFF do as follows:
547
548 Set the variable theNewPixelIndex to the byte in the
549 indexByteTable array of the header indexed by
550 theNewPixelIndexSelector.
551
552 Set the variable theBufferBitStripCount to the value in the
553 bitStripTable array of the header indexed by theNewPixelIndex.
554
555 One-by-one use right bit shift (>>) to remove
556 theBufferBitStripCount bits from theBitStreamBuffer. After each
557 shift decrement theBufferBitCounter and check whether it has
558 reached 0. If it has, get the next byte from the image data and
559 insert it in the high byte of theBitStreamBuffer and reset
560 theBufferBitCounter to 8. What is happening here is as we remove
561 each bit from the bottom of the bit stream buffer we check to see
562 if there are any bits left in the high byte of the buffer. As soon
563 as we know there are none, we refill it with the next eight bits
564 from the image data.
565
566 When this 'bit-stripping' is finished, other than actually identifying
567 the new pixel we are nearly done. I will leave that for the moment and
568 look at what happens if the low byte of theBitStreamBuffer we put in
569 theNewPixelIndexSelector was actually 0xFF:
570
571 In this case, instead of the above routine we begin by removing
572 the low eight bits from the theBitStreamBuffer. We use the same
573 ono-by-one bit shift right process described above to do this,
574 again checking after each shift if it is necesary to refill the
575 buffer's high byte.
576
577 When the eight bits have been removed we set theNewPixelIndex to
578 the value of the low four bits of theBitStreamBuffer. Having done
579 that we again one-by-one strip off those low four bits from the
580 theBitStreamBuffer, again checking if we need to refill the buffer
581 high byte.
582
583 Irrespective of whether we initially had 0xFF in
584 theNewPixelIndexSelector we now have a new value in theNewPixelIndex.
585 This value is used as follows to obtain the new pixel value.
586
587 The variable theNewPixel contains either the seedByte or the value of
588 the previously extracted pixel. In either case this is a 4-bit value
589 in the lower 4 bits. Use the left bit shift operator (or multiply by
590 16) to shift those four bits into the high four bits of theNewPixel.
591
592 Add the value in theNewPixelIndex (it is a 4-bit value) to
593 theNewPixel. The resulting value is used as an index into the
594 pixelTable array of the header to get the actual new pixel value so
595 theNewPixel = header.pixelTable[theNewPixel] gets us our new pixel and
596 primes theNewPixel for the same process next time around the loop.
597
598 Having got our new pixel it is stored in the next empty space in the
599 bitmap and we loop back and start again.
600
601 [*****] I am not sure how the compression was done - someone with a better
602 understanding of this area may be able to work out the method from the above.
603 I worked out how to decode it by spending many, many hours tracing through the
604 code in a debugger - thanks to the now defunct HiSoft for their DevPac ST and
605 Gerin Philippe for NoSTalgia <http://users.skynet.be/sky39147/>.
606 */
607 L9BOOL bitmap_pc2_decode(char* file, int x, int y)
608 {
609 L9BYTE* data = NULL;
610 int i, xi, yi, max_x, max_y;
611
612 L9BYTE theNewPixel, theNewPixelIndex;
613 L9BYTE theBufferBitCounter, theNewPixelIndexSelector, theBufferBitStripCount;
614 L9UINT16 theBitStreamBuffer, theImageDataIndex;
615 L9BYTE* theImageFileData;
616
617 L9UINT32 size;
618 data = bitmap_load(file,&size);
619 if (data == NULL)
620 return FALSE;
621
622 max_x = data[37]+data[36]*256;
623 max_y = data[39]+data[38]*256;
624 if (max_x > MAX_BITMAP_WIDTH || max_y > MAX_BITMAP_HEIGHT)
625 {
626 free(data);
627 return FALSE;
628 }
629
630 if ((x == 0) && (y == 0))
631 {
632 if (bitmap)
633 free(bitmap);
634 bitmap = bitmap_alloc(max_x,max_y);
635 }
636 if (bitmap == NULL)
637 {
638 free(data);
639 return FALSE;
640 }
641
642 if (x+max_x > bitmap->width)
643 max_x = bitmap->width-x;
644 if (y+max_y > bitmap->height)
645 max_y = bitmap->height-y;
646
647 /* prime the new pixel variable with the seed byte */
648 theNewPixel = data[40];
649 /* initialise the index to the image data */
650 theImageDataIndex = 0;
651 /* prime the bit stream buffer */
652 theImageFileData = data+570;
653 theBitStreamBuffer = theImageFileData[theImageDataIndex++];
654 theBitStreamBuffer = theBitStreamBuffer +
655 (0x100 * theImageFileData[theImageDataIndex++]);
656 /* initialise the bit stream buffer bit counter */
657 theBufferBitCounter = 8;
658
659 for (yi = 0; yi < max_y; yi++)
660 {
661 for (xi = 0; xi < max_x; xi++)
662 {
663 theNewPixelIndexSelector = (theBitStreamBuffer & 0x00FF);
664 if (theNewPixelIndexSelector != 0xFF)
665 {
666 /* get index for new pixel and bit strip count */
667 theNewPixelIndex = (data+314)[theNewPixelIndexSelector];
668 /* get the bit strip count */
669 theBufferBitStripCount = (data+298)[theNewPixelIndex];
670 /* strip theBufferBitStripCount bits from theBitStreamBuffer */
671 while (theBufferBitStripCount > 0)
672 {
673 theBitStreamBuffer = theBitStreamBuffer >> 1;
674 theBufferBitStripCount--;
675 theBufferBitCounter--;
676 if (theBufferBitCounter == 0)
677 {
678 /* need to refill the theBitStreamBuffer high byte */
679 theBitStreamBuffer = theBitStreamBuffer +
680 (0x100 * theImageFileData[theImageDataIndex++]);
681 /* re-initialise the bit stream buffer bit counter */
682 theBufferBitCounter = 8;
683 }
684 }
685 }
686 else
687 {
688 /* strip the 8 bits holding 0xFF from theBitStreamBuffer */
689 theBufferBitStripCount = 8;
690 while (theBufferBitStripCount > 0)
691 {
692 theBitStreamBuffer = theBitStreamBuffer >> 1;
693 theBufferBitStripCount--;
694 theBufferBitCounter--;
695 if (theBufferBitCounter == 0)
696 {
697 /* need to refill the theBitStreamBuffer high byte */
698 theBitStreamBuffer = theBitStreamBuffer +
699 (0x100 * theImageFileData[theImageDataIndex++]);
700 /* re-initialise the bit stream buffer bit counter */
701 theBufferBitCounter = 8;
702 }
703 }
704 /* get the literal pixel index value from the bit stream */
705 theNewPixelIndex = (0x000F & theBitStreamBuffer);
706 theBufferBitStripCount = 4;
707 /* strip 4 bits from theBitStreamBuffer */
708 while (theBufferBitStripCount > 0)
709 {
710 theBitStreamBuffer = theBitStreamBuffer >> 1;
711 theBufferBitStripCount--;
712 theBufferBitCounter--;
713 if (theBufferBitCounter == 0)
714 {
715 /* need to refill the theBitStreamBuffer high byte */
716 theBitStreamBuffer = theBitStreamBuffer +
717 (0x100 * theImageFileData[theImageDataIndex++]);
718 /* re-initialise the bit stream buffer bit counter */
719 theBufferBitCounter = 8;
720 }
721 }
722 }
723
724 /* shift the previous pixel into the high four bits of theNewPixel */
725 theNewPixel = (0xF0 & (theNewPixel << 4));
726 /* add the index to the new pixel to theNewPixel */
727 theNewPixel = theNewPixel + theNewPixelIndex;
728 /* extract the nex pixel from the table */
729 theNewPixel = (data+42)[theNewPixel];
730 /* store new pixel in the bitmap */
731 bitmap->bitmap[(bitmap->width*(y+yi))+(x+xi)] = theNewPixel;
732 }
733 }
734
735 bitmap->npalette = 16;
736 for (i = 0; i < 16; i++)
737 bitmap->palette[i] = bitmap_pcst_colour(data[4+(i*2)],data[5+(i*2)]);
738
739 free(data);
740 return TRUE;
741 }
742
743 BitmapType bitmap_pc_type(char* file)
744 {
745 BitmapType type = PC2_BITMAPS;
746
747 FILE* f = fopen(file,"rb");
748 if (f != NULL)
749 {
750 L9BYTE data[6];
751 int x, y;
752
753 if(fread(data,1,sizeof data,f) != sizeof data && !feof(f)) return NO_BITMAPS;
754 fclose(f);
755
756 x = data[2]+data[3]*256;
757 y = data[4]+data[5]*256;
758
759 if ((x == 0x0140) && (y == 0x0087))
760 type = PC1_BITMAPS;
761 if ((x == 0x00E0) && (y == 0x0074))
762 type = PC1_BITMAPS;
763 if ((x == 0x0140) && (y == 0x0087))
764 type = PC1_BITMAPS;
765 if ((x == 0x00E1) && (y == 0x0076))
766 type = PC1_BITMAPS;
767 }
768
769 return type;
770 }
771
772 /*
773 Amiga Bitmaps
774 */
775
776 void bitmap_noext_name(int num, char* dir, char* out)
777 {
778 if (num == 0)
779 {
780 FILE* f;
781
782 sprintf(out,"%stitle",dir);
783 f = fopen(out,"rb");
784 if (f != NULL)
785 {
786 fclose(f);
787 return;
788 }
789 else
790 num = 30;
791 }
792
793 sprintf(out,"%s%d",dir,num);
794 }
795
796 int bitmap_amiga_intensity(int col)
797 {
798 return (int)(pow((double)col/15,1.0/0.8) * 0xff);
799 }
800
801 /*
802 Amiga palette colours are word length structures with the red, green and blue
803 values stored in the second, third and lowest nybles respectively. The high
804 nybble is always zero.
805 */
806 Colour bitmap_amiga_colour(int i1, int i2)
807 {
808 Colour col;
809 col.red = bitmap_amiga_intensity(i1&0xf);
810 col.green = bitmap_amiga_intensity(i2>>4);
811 col.blue = bitmap_amiga_intensity(i2&0xf);
812 return col;
813 }
814
815 /*
816 The Amiga image file has the following format. It consists of a 44 byte
817 header followed by the image data.
818
819 The header has the following format:
820 Bytes 0-63: thirty-two entry Amiga palette
821 Bytes 64-65: padding
822 Bytes 66-67: big-endian word holding picture width in pixels*
823 Bytes 68-69: padding
824 Bytes 70-71: big-endian word holding number of pixel rows in the image*
825
826 [*] these are probably big-endian unsigned longs but I have designated
827 the upper two bytes as padding because (a) Level 9 does not need
828 them as longs and (b) using unsigned shorts reduces byte sex induced
829 byte order juggling.
830
831 The images are designed for an Amiga low-res mode screen - that is they
832 assume a 320*256 (or 320 * 200 if NSTC display) screen with a palette of
833 32 colours from the possible 4096.
834
835 The image data is organised the same way that Amiga video memory is. The
836 entire data block is divided into five equal length bit planes with the
837 first bit plane holding the low bit of each 5-bit pixel, the second bitplane
838 the second bit of the pixel and so on up to the fifth bit plane holding the
839 high bit of the f5-bit pixel.
840 */
841 L9BOOL bitmap_amiga_decode(char* file, int x, int y)
842 {
843 L9BYTE* data = NULL;
844 int i, xi, yi, max_x, max_y, p, b;
845
846 L9UINT32 size;
847 data = bitmap_load(file,&size);
848 if (data == NULL)
849 return FALSE;
850
851 max_x = (((((data[64]<<8)|data[65])<<8)|data[66])<<8)|data[67];
852 max_y = (((((data[68]<<8)|data[69])<<8)|data[70])<<8)|data[71];
853 if (max_x > MAX_BITMAP_WIDTH || max_y > MAX_BITMAP_HEIGHT)
854 {
855 free(data);
856 return FALSE;
857 }
858
859 if ((x == 0) && (y == 0))
860 {
861 if (bitmap)
862 free(bitmap);
863 bitmap = bitmap_alloc(max_x,max_y);
864 }
865 if (bitmap == NULL)
866 {
867 free(data);
868 return FALSE;
869 }
870
871 if (x+max_x > bitmap->width)
872 max_x = bitmap->width-x;
873 if (y+max_y > bitmap->height)
874 max_y = bitmap->height-y;
875
876 for (yi = 0; yi < max_y; yi++)
877 {
878 for (xi = 0; xi < max_x; xi++)
879 {
880 p = 0;
881 for (b = 0; b < 5; b++)
882 p |= ((data[72+(max_x/8)*(max_y*b+yi)+xi/8]>>(7-(xi%8)))&1)<<b;
883 bitmap->bitmap[(bitmap->width*(y+yi))+(x+xi)] = p;
884 }
885 }
886
887 bitmap->npalette = 32;
888 for (i = 0; i < 32; i++)
889 bitmap->palette[i] = bitmap_amiga_colour(data[i*2],data[i*2+1]);
890
891 free(data);
892 return TRUE;
893 }
894
895 BitmapType bitmap_noext_type(char* file)
896 {
897 FILE* f = fopen(file,"rb");
898 if (f != NULL)
899 {
900 L9BYTE data[72];
901 int x, y;
902
903 if(fread(data,1,sizeof data,f) != sizeof data && !feof(f)) return NO_BITMAPS;
904 fclose(f);
905
906 x = data[67]+data[66]*256;
907 y = data[71]+data[70]*256;
908
909 if ((x == 0x0140) && (y == 0x0088))
910 return AMIGA_BITMAPS;
911 if ((x == 0x0140) && (y == 0x0087))
912 return AMIGA_BITMAPS;
913 if ((x == 0x00E0) && (y == 0x0075))
914 return AMIGA_BITMAPS;
915 if ((x == 0x00E4) && (y == 0x0075))
916 return AMIGA_BITMAPS;
917 if ((x == 0x00E0) && (y == 0x0076))
918 return AMIGA_BITMAPS;
919 if ((x == 0x00DB) && (y == 0x0076))
920 return AMIGA_BITMAPS;
921
922 x = data[3]+data[2]*256;
923 y = data[7]+data[6]*256;
924
925 if ((x == 0x0200) && (y == 0x00D8))
926 return MAC_BITMAPS;
927 if ((x == 0x0168) && (y == 0x00BA))
928 return MAC_BITMAPS;
929 if ((x == 0x0168) && (y == 0x00BC))
930 return MAC_BITMAPS;
931 if ((x == 0x0200) && (y == 0x00DA))
932 return MAC_BITMAPS;
933 if ((x == 0x0168) && (y == 0x00DA))
934 return MAC_BITMAPS;
935
936 x = data[35]+data[34]*256;
937 y = data[39]+data[38]*256;
938
939 if ((x == 0x0050) && (y == 0x0087))
940 return ST1_BITMAPS;
941 if ((x == 0x0038) && (y == 0x0074))
942 return ST1_BITMAPS;
943 }
944
945 return NO_BITMAPS;
946 }
947
948 /*
949 Macintosh Bitmaps
950 */
951
952 /*
953 The Mac image file format is very simple. The header is ten bytes
954 with the width of the image in pixels in the first long and the
955 height (in pixel rows) in the second long - both are big-endian.
956 (In both cases I treat these as unsigned shorts to minimise byte
957 twiddling when working around byte sex issues). There follow two
958 unidentified bytes - possibly image type identifiers or maybe
959 valid pixel masks for the beginning and end of pixel rows in
960 sub-images.
961
962 The image data is extremely simple. The entire block is a packed array
963 of 1-bit pixels - I.E. each byte holds eight pixels - with 1 representing
964 white and 0 representing black. The pixels are organised with the top
965 left first and bottom left last, each row in turn.
966
967 The image sizes are 512 * 216 pixels for main images and 360 * 186 pixels
968 for sub-images.
969 */
970 L9BOOL bitmap_mac_decode(char* file, int x, int y)
971 {
972 L9BYTE* data = NULL;
973 int xi, yi, max_x, max_y;
974
975 L9UINT32 size;
976 data = bitmap_load(file,&size);
977 if (data == NULL)
978 return FALSE;
979
980 max_x = data[3]+data[2]*256;
981 max_y = data[7]+data[6]*256;
982 if (max_x > MAX_BITMAP_WIDTH || max_y > MAX_BITMAP_HEIGHT)
983 {
984 free(data);
985 return FALSE;
986 }
987
988 if (x > 0) /* Mac bug, apparently */
989 x = 78;
990
991 if ((x == 0) && (y == 0))
992 {
993 if (bitmap)
994 free(bitmap);
995 bitmap = bitmap_alloc(max_x,max_y);
996 }
997 if (bitmap == NULL)
998 {
999 free(data);
1000 return FALSE;
1001 }
1002
1003 if (x+max_x > bitmap->width)
1004 max_x = bitmap->width-x;
1005 if (y+max_y > bitmap->height)
1006 max_y = bitmap->height-y;
1007
1008 for (yi = 0; yi < max_y; yi++)
1009 {
1010 for (xi = 0; xi < max_x; xi++)
1011 {
1012 bitmap->bitmap[(bitmap->width*(y+yi))+(x+xi)] =
1013 (data[10+(max_x/8)*yi+xi/8]>>(7-(xi%8)))&1;
1014 }
1015 }
1016
1017 bitmap->npalette = 2;
1018 bitmap->palette[0].red = 0;
1019 bitmap->palette[0].green = 0;
1020 bitmap->palette[0].blue = 0;
1021 bitmap->palette[1].red = 0xff;
1022 bitmap->palette[1].green = 0xff;
1023 bitmap->palette[1].blue = 0xff;
1024
1025 free(data);
1026 return TRUE;
1027 }
1028
1029 /*
1030 C64 Bitmaps, also related formats (BBC B, Amstrad CPC and Spectrum +3)
1031 */
1032
1033 /* Commodore 64 palette from Vice */
1034 const Colour bitmap_c64_colours[] = {
1035 {0x00, 0x00, 0x00 },
1036 {0xff, 0xff, 0xff },
1037 {0x89, 0x40, 0x36 },
1038 {0x7a, 0xbf, 0xc7 },
1039 {0x8a, 0x46, 0xae },
1040 {0x68, 0xa9, 0x41 },
1041 {0x3e, 0x31, 0xa2 },
1042 {0xd0, 0xdc, 0x71 },
1043 {0x90, 0x5f, 0x25 },
1044 {0x5c, 0x47, 0x00 },
1045 {0xbb, 0x77, 0x6d },
1046 {0x55, 0x55, 0x55 },
1047 {0x80, 0x80, 0x80 },
1048 {0xac, 0xea, 0x88 },
1049 {0x7c, 0x70, 0xda },
1050 {0xab, 0xab, 0xab }};
1051
1052 const Colour bitmap_bbc_colours[] = {
1053 {0x00, 0x00, 0x00 },
1054 {0xff, 0x00, 0x00 },
1055 {0x00, 0xff, 0x00 },
1056 {0xff, 0xff, 0x00 },
1057 {0x00, 0x00, 0xff },
1058 {0xff, 0x00, 0xff },
1059 {0x00, 0xff, 0xff },
1060 {0xff, 0xff, 0xff }};
1061
1062 void bitmap_c64_name(int num, char* dir, char* out)
1063 {
1064 if (num == 0)
1065 sprintf(out,"%stitle mpic",dir);
1066 else
1067 sprintf(out,"%spic%d",dir,num);
1068 }
1069
1070 void bitmap_bbc_name(int num, char* dir, char* out)
1071 {
1072 FILE* f;
1073
1074 if (num == 0)
1075 {
1076 sprintf(out,"%sP.Title",dir);
1077 f = fopen(out,"rb");
1078 if (f != NULL)
1079 {
1080 fclose(f);
1081 return;
1082 }
1083
1084 sprintf(out,"%stitle",dir);
1085 }
1086 else
1087 {
1088 sprintf(out,"%sP.Pic%d",dir,num);
1089 f = fopen(out,"rb");
1090 if (f != NULL)
1091 {
1092 fclose(f);
1093 return;
1094 }
1095
1096 sprintf(out,"%spic%d",dir,num);
1097 }
1098 }
1099
1100 void bitmap_cpc_name(int num, char* dir, char* out)
1101 {
1102 if (num == 0)
1103 sprintf(out,"%stitle.pic",dir);
1104 else if (num == 1)
1105 sprintf(out,"%s1.pic",dir);
1106 else
1107 sprintf(out,"%sallpics.pic",dir);
1108 }
1109
1110 BitmapType bitmap_c64_type(char* file)
1111 {
1112 BitmapType type = C64_BITMAPS;
1113
1114 FILE* f = fopen(file,"rb");
1115 if (f != NULL)
1116 {
1117 L9UINT32 size = filelength(f);
1118 fclose(f);
1119
1120 if (size == 10048)
1121 type = BBC_BITMAPS;
1122 if (size == 6494)
1123 type = BBC_BITMAPS;
1124 }
1125
1126 return type;
1127 }
1128
1129 /*
1130 The C64 graphics file format is (loosely) based on the layout of
1131 C64 graphics memory. There are in fact two formats (i) the
1132 standard game images and (ii) title pictures. For both formats
1133 the file begins within the 2-byte pair 0x00 and 0x20.
1134
1135 The images are "multi-color bitmap mode" images which means they
1136 have rows of 160 double width pixels and can be up to 200 rows
1137 long. (The title images are 200 lines long, the game images are
1138 136 lines long.) Unlike Amiga, Mac, ST and PC graphics there are
1139 no "main" and "sub" images. All game graphics have the same
1140 dimensions and each completely replaces its predecessor.
1141
1142 The graphics files used on the Amstrad CPC and Spectrum +3 are also
1143 virtually identical to C64 graphics files. This choice was presumably
1144 made because although the CPC screen was more capable than the c64 it
1145 was (in low resolution) the same size (160*200) and presumably
1146 algorothmic conversion conversion of the colours was trivial for
1147 the interpreter. In addition (a) the artwork already existed so no
1148 extra expense would be incurred and (b) by accepting the C64's
1149 limitation of only four colours in each 4*8 pixel block (but still
1150 with sixteen colours on screen) they got a compressed file format
1151 allowing more pictures on each disk.
1152
1153 The file organisation is rather different though. Only picture
1154 one and the title picture are separate files. All the other
1155 pictures (2-29) are stored in one large file "allpics.pic".
1156
1157 On these platforms the picture 1 file and title picture file have
1158 an AMSDOS header (a 128 byte block of metadata) which contains a
1159 checksum of the first 66 bytes of the header in a little-endian
1160 word at bytes 67 & 68. On the original C64 platform there was a
1161 simple two byte header. Following the header the data is organised
1162 exactly as in the C64 game and title image files. The
1163 'allpics.pic" file has no header and consists of 0x139E blocks
1164 each forming a picture, in the C64 game file format (minus the two
1165 byte header).
1166 */
1167 L9BOOL bitmap_c64_decode(char* file, BitmapType type, int num)
1168 {
1169 L9BYTE* data = NULL;
1170 int i, xi, yi, max_x, max_y, cx, cy, px, py, p;
1171 int off, off_scr, off_col, off_bg, col_comp;
1172
1173 L9UINT32 size;
1174 data = bitmap_load(file,&size);
1175 if (data == NULL)
1176 return FALSE;
1177
1178 if (type == C64_BITMAPS)
1179 {
1180 if (size == 10018) /* C64 title picture */
1181 {
1182 max_x = 320;
1183 max_y = 200;
1184 off = 2;
1185 off_scr = 8002;
1186 off_bg = 9003;
1187 off_col = 9018;
1188 col_comp = 0;
1189 }
1190 else if (size == 6464) /* C64 picture */
1191 {
1192 max_x = 320;
1193 max_y = 136;
1194 off = 2;
1195 off_scr = 5442;
1196 off_col = 6122;
1197 off_bg = 6463;
1198 col_comp = 1;
1199 }
1200 else
1201 return FALSE;
1202 }
1203 else if (type == BBC_BITMAPS)
1204 {
1205 if (size == 10058) /* BBC title picture */
1206 {
1207 max_x = 320;
1208 max_y = 200;
1209 off = 10;
1210 off_scr = 8010;
1211 off_bg = 9011;
1212 off_col = 9026;
1213 col_comp = 0;
1214 }
1215 else if (size == 10048) /* BBC title picture */
1216 {
1217 max_x = 320;
1218 max_y = 200;
1219 off = 0;
1220 off_scr = 8000;
1221 off_bg = 9001;
1222 off_col = 9016;
1223 col_comp = 0;
1224 }
1225 else if (size == 6504) /* BBC picture */
1226 {
1227 max_x = 320;
1228 max_y = 136;
1229 off = 10;
1230 off_scr = 5450;
1231 off_col = 6130;
1232 off_bg = 6471;
1233 col_comp = 1;
1234 }
1235 else if (size == 6494) /* BBC picture */
1236 {
1237 max_x = 320;
1238 max_y = 136;
1239 off = 0;
1240 off_scr = 5440;
1241 off_col = 6120;
1242 off_bg = 6461;
1243 col_comp = 1;
1244 }
1245 else
1246 return FALSE;
1247 }
1248 else if (type == CPC_BITMAPS)
1249 {
1250 if (num == 0) /* CPC/+3 title picture */
1251 {
1252 max_x = 320;
1253 max_y = 200;
1254 off = 128;
1255 off_scr = 8128;
1256 off_bg = 9128;
1257 off_col = 9144;
1258 col_comp = 0;
1259 }
1260 else if (num == 1) /* First CPC/+3 picture */
1261 {
1262 max_x = 320;
1263 max_y = 136;
1264 off = 128;
1265 off_scr = 5568;
1266 off_col = 6248;
1267 off_bg = 6588;
1268 col_comp = 1;
1269 }
1270 else if (num >= 2 && num <= 29) /* Subsequent CPC/+3 pictures */
1271 {
1272 max_x = 320;
1273 max_y = 136;
1274 off = ((num-2)*6462);
1275 off_scr = 5440+((num-2)*6462);
1276 off_col = 6120+((num-2)*6462);
1277 off_bg = 6460+((num-2)*6462);
1278 col_comp = 1;
1279 }
1280 else
1281 return FALSE;
1282 }
1283
1284 if (bitmap)
1285 free(bitmap);
1286 bitmap = bitmap_alloc(max_x,max_y);
1287 if (bitmap == NULL)
1288 {
1289 free(data);
1290 return FALSE;
1291 }
1292
1293 for (yi = 0; yi < max_y; yi++)
1294 {
1295 for (xi = 0; xi < max_x/2; xi++)
1296 {
1297 cx = xi/4;
1298 px = xi%4;
1299 cy = yi/8;
1300 py = yi%8;
1301
1302 p = data[off+(cy*40+cx)*8+py];
1303 p = (p>>((3-px)*2))&3;
1304
1305 switch (p)
1306 {
1307 case 0:
1308 i = data[off_bg] & 0x0f;
1309 break;
1310 case 1:
1311 i = data[off_scr+cy*40+cx] >> 4;
1312 break;
1313 case 2:
1314 i = data[off_scr+cy*40+cx] & 0x0f;
1315 break;
1316 case 3:
1317 if (col_comp)
1318 i = (data[off_col+(cy*40+cx)/2]>>((1-(cx%2))*4)) & 0x0f;
1319 else
1320 i = data[off_col+(cy*40+cx)] & 0x0f;
1321 break;
1322 }
1323
1324 bitmap->bitmap[(bitmap->width*yi)+(xi*2)] = i;
1325 bitmap->bitmap[(bitmap->width*yi)+(xi*2)+1] = i;
1326 }
1327 }
1328
1329 bitmap->npalette = 16;
1330 for (i = 0; i < 16; i++)
1331 bitmap->palette[i] = bitmap_c64_colours[i];
1332
1333 free(data);
1334 return TRUE;
1335 }
1336
1337 /*
1338 The graphics files used by the BBC B are virtually identical
1339 to C64 graphics files. I assume that (as with the CPC and
1340 Spectrum+3) this choice was made because the BBC mode 2 screen,
1341 was nearly the same size (160*256) and had roughly the same capability
1342 as the C64 screen (displays 16 colours, although eight of those ar
1343 just the first eight flashing).
1344
1345 In addition (a) the artwork already existed so no extra expense would
1346 be incurred and (b) by accepting the C64's limitation of only four
1347 colours in each 4*8 pixel block (but still with sixteen colours on
1348 screen) they got a compressed file format allowing more pictures
1349 on each disk.
1350
1351 The file organisation is very close to the C64. The naming system
1352 can be the same eg "PIC12", but another form is also used :
1353 "P.Pic12". Unlike the C64 the BBC has well defined title images,
1354 called "TITLE" or P.Title. All pictures are in separate files.
1355
1356 The only difference seems to be:
1357
1358 * There is either *no* header before the image data or a simple
1359 10 byte header which I think *may* be a file system header
1360 left in place by the extractor system.
1361
1362 * There is an extra 32 bytes following the data at the end of
1363 each file. These bytes encode a table to convert between the 16
1364 C64 colours and 16, four-pixel pix-patterns used to let the BBC
1365 (with only 8 colours) represent the sixteen possible C64 colours.
1366
1367 A pix-pattern looks like this:
1368
1369 | Even | Odd |
1370 | Column | Column |
1371 -----------------------------
1372 Even Row |Pixel 1 | Pixel 2 |
1373 ---------|--------|---------|
1374 Odd Row |Pixel 3 | Pixel 4 |
1375 -----------------------------
1376
1377 Each of the four pixel *can* be any of the eight BBC Mode 2
1378 steady colours. In practice they seem either to be all the
1379 same or a simple check of two colours - the pixels in the
1380 odd row being in the reverse order to those in the even row.
1381
1382 When converting a C64 pixel to a BBC pixel the game uses the
1383 value of the C64 pixel as an index into the array of sixteen
1384 BBC pix-patterns. The game looks at the selected pattern and
1385 chooses the BBC pixel colour thus: if the pixel is in an even
1386 numbered row and an even numbered column, it uses Pixel 1 from
1387 the pattern, if in an even row but an odd column, it uses Pixel 3
1388 and so on.
1389
1390 The pix-pattern data is encoded thus: the first sixteen bytes
1391 encode the even row pixels for the patterns, one byte per
1392 pattern, and in the same way the second sixteen bytes encode
1393 the odd row pixels for each pattern. For example for the
1394 pattern representing C64 colour 0 the even row pixels are encoded
1395 in the first byte and the odd row pixels in the sixteenth byte.
1396
1397 Within each byte the pixels are encoded in this way:
1398
1399 Bit 7 6 5 4 3 2 1 0
1400 -------------------------------------
1401 0 0 1 0 0 1 1 1
1402 | | | | | | | |
1403 +---|---+---|---+---|---+---|----- Even Pixel 0101 (5)
1404 | | | |
1405 +-------+-------+-------+----- Odd Pixel 0011 (3)
1406
1407 This function calls the C64 decoding routines to do the actual
1408 loading. See the comments to that function for details of how the
1409 image is encoded and stored.
1410 */
1411 L9BOOL bitmap_bbc_decode(char* file, BitmapType type, int num)
1412 {
1413 unsigned char patRowData[32];
1414 unsigned char patArray[16][2][2];
1415 FILE* f;
1416 int i,j,k,isOddColumn,isOddRow;
1417 L9BYTE pixel;
1418
1419 if (bitmap_c64_decode(file,type,num) == FALSE)
1420 return FALSE;
1421
1422 f = fopen(file,"rb");
1423 if (f == NULL)
1424 return FALSE;
1425
1426 /* Seek to the offset of the pixPat data and read in the data */
1427 fseek(f,filelength(f)-32,SEEK_SET);
1428 if(fread(patRowData,sizeof (L9BYTE),32,f) != 32 && !feof(f)) return FALSE;
1429 fclose(f);
1430
1431 /* Extract the patterns */
1432 i = 0;
1433 for (k = 0; k < 2; k++)
1434 {
1435 for (j = 0; j < 16; j++)
1436 {
1437 /* Extract the even col pixel for this pattern row */
1438 patArray[j][k][0] =
1439 ((patRowData[i] >> 4) & 0x8) + ((patRowData[i] >> 3) & 0x4) +
1440 ((patRowData[i] >> 2) & 0x2) + ((patRowData[i] >> 1) & 0x1);
1441 /* Extract the odd col pixel for this pattern row */
1442 patArray[j][k][1] =
1443 ((patRowData[i] >> 3) & 0x8) + ((patRowData[i] >> 2) & 0x4) +
1444 ((patRowData[i] >> 1) & 0x2) + (patRowData[i] & 0x1);
1445 i++;
1446 }
1447 }
1448
1449 /* Convert the image. Each BBC pixel is represented by two pixels here */
1450 i = 0;
1451 isOddRow = 0;
1452 for (j = 0; j < bitmap->height; j++)
1453 {
1454 isOddColumn = 0;
1455 for (k = 0; k < bitmap->width/2; k++)
1456 {
1457 pixel = bitmap->bitmap[i];
1458 bitmap->bitmap[i] = patArray[pixel][isOddColumn][isOddRow];
1459 bitmap->bitmap[i+1] = patArray[pixel][isOddColumn][isOddRow];
1460 isOddColumn ^= 1;
1461 i += 2;
1462 }
1463 isOddRow ^= 1;
1464 }
1465
1466 bitmap->npalette = 8;
1467 for (i = 0; i < 8; i++)
1468 bitmap->palette[i] = bitmap_bbc_colours[i];
1469
1470 return TRUE;
1471 }
1472
1473 BitmapType DetectBitmaps(char* dir)
1474 {
1475 char file[MAX_PATH];
1476
1477 bitmap_noext_name(2,dir,file);
1478 if (os_find_file(file))
1479 return bitmap_noext_type(file);
1480
1481 bitmap_pc_name(2,dir,file);
1482 if (os_find_file(file))
1483 return bitmap_pc_type(file);
1484
1485 bitmap_c64_name(2,dir,file);
1486 if (os_find_file(file))
1487 return bitmap_c64_type(file);
1488
1489 bitmap_bbc_name(2,dir,file);
1490 if (os_find_file(file))
1491 return BBC_BITMAPS;
1492
1493 bitmap_cpc_name(2,dir,file);
1494 if (os_find_file(file))
1495 return CPC_BITMAPS;
1496
1497 bitmap_st2_name(2,dir,file);
1498 if (os_find_file(file))
1499 return ST2_BITMAPS;
1500
1501 return NO_BITMAPS;
1502 }
1503
1504 Bitmap* DecodeBitmap(char* dir, BitmapType type, int num, int x, int y)
1505 {
1506 char file[MAX_PATH];
1507
1508 switch (type)
1509 {
1510 case PC1_BITMAPS:
1511 bitmap_pc_name(num,dir,file);
1512 if (os_find_file(file))
1513 {
1514 if (bitmap_pc1_decode(file,x,y))
1515 return bitmap;
1516 }
1517 break;
1518
1519 case PC2_BITMAPS:
1520 bitmap_pc_name(num,dir,file);
1521 if (os_find_file(file))
1522 {
1523 if (bitmap_pc2_decode(file,x,y))
1524 return bitmap;
1525 }
1526 break;
1527
1528 case AMIGA_BITMAPS:
1529 bitmap_noext_name(num,dir,file);
1530 if (os_find_file(file))
1531 {
1532 if (bitmap_amiga_decode(file,x,y))
1533 return bitmap;
1534 }
1535 break;
1536
1537 case C64_BITMAPS:
1538 bitmap_c64_name(num,dir,file);
1539 if (os_find_file(file))
1540 {
1541 if (bitmap_c64_decode(file,type,num))
1542 return bitmap;
1543 }
1544 break;
1545
1546 case BBC_BITMAPS:
1547 bitmap_bbc_name(num,dir,file);
1548 if (os_find_file(file))
1549 {
1550 if (bitmap_bbc_decode(file,type,num))
1551 return bitmap;
1552 }
1553 break;
1554
1555 case CPC_BITMAPS:
1556 bitmap_cpc_name(num,dir,file);
1557 if (os_find_file(file))
1558 {
1559 if (bitmap_c64_decode(file,type,num)) /* Nearly identical to C64 */
1560 return bitmap;
1561 }
1562 break;
1563
1564 case MAC_BITMAPS:
1565 bitmap_noext_name(num,dir,file);
1566 if (os_find_file(file))
1567 {
1568 if (bitmap_mac_decode(file,x,y))
1569 return bitmap;
1570 }
1571 break;
1572
1573 case ST1_BITMAPS:
1574 bitmap_noext_name(num,dir,file);
1575 if (os_find_file(file))
1576 {
1577 if (bitmap_st1_decode(file,x,y))
1578 return bitmap;
1579 }
1580 break;
1581
1582 case ST2_BITMAPS:
1583 bitmap_st2_name(num,dir,file);
1584 if (os_find_file(file))
1585 {
1586 if (bitmap_pc2_decode(file,x,y))
1587 return bitmap;
1588 }
1589 break;
1590 }
1591
1592 return NULL;
1593 }
1594