1 /*****************************************************************************/
5 /* Lynx sprite format backend for the sp65 sprite and bitmap utility */
9 /* (C) 2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
45 #include "lynxsprite.h"
49 /*****************************************************************************/
51 /*****************************************************************************/
64 /*****************************************************************************/
66 /*****************************************************************************/
70 static enum Mode GetMode (const Collection* A)
71 /* Return the sprite mode from the attribute collection A */
73 /* Check for a mode attribute */
74 const char* Mode = GetAttrVal (A, "mode");
76 if (strcmp (Mode, "literal") == 0) {
78 } else if (strcmp (Mode, "packed") == 0) {
80 } else if (strcmp (Mode, "shaped") == 0) {
83 Error ("Invalid value for attribute `mode'");
91 static unsigned GetActionPointX (const Collection* A)
92 /* Return the sprite mode from the attribute collection A */
94 /* Check for a action point x attribute */
95 const char* ActionPointX = GetAttrVal (A, "ax");
97 return atoi(ActionPointX);
104 static unsigned GetActionPointY (const Collection* A)
105 /* Return the sprite mode from the attribute collection A */
107 /* Check for a action point y attribute */
108 const char* ActionPointY = GetAttrVal (A, "ay");
110 return atoi(ActionPointY);
116 static char OutBuffer[512]; /* The maximum size is 508 pixels */
117 static unsigned char OutIndex;
119 static void AssembleByte(unsigned bits, char val)
121 static char bit_counter = 8, byte = 0;
130 /* handle end of line */
132 if (bit_counter != 8) {
133 byte <<= bit_counter;
134 OutBuffer[OutIndex++] = byte;
136 Error ("ASprite is too large for the Lynx");
139 OutBuffer[OutIndex++] = byte;
141 Error ("BSprite is too large for the Lynx");
147 /* handle end of line for literal */
149 if (bit_counter != 8) {
150 byte <<= bit_counter;
151 OutBuffer[OutIndex++] = byte;
153 Error ("ASprite is too large for the Lynx");
166 if (!(--bit_counter)) {
167 OutBuffer[OutIndex++] = byte;
169 Error ("Sprite is too large for the Lynx");
180 static unsigned char ChoosePackagingMode(signed len, signed index, char LineBuffer[512])
186 if (LineBuffer[index] != LineBuffer[index + 1]) {
192 static void WriteOutBuffer(StrBuf *D)
196 /* Fix bug in Lynx where the count cannot be 1 */
198 OutBuffer[OutIndex++] = 0;
200 /* Write the byte count to the end of the scanline */
201 if (OutIndex == 255) {
202 Error ("Sprite is too large for the Lynx");
204 SB_AppendChar (D, OutIndex+1);
205 /* Write scanline data */
206 for (i = 0; i < OutIndex; i++) {
207 SB_AppendChar (D, OutBuffer[i]);
211 static void encodeSprite(StrBuf *D, enum Mode M, char ColorBits, char ColorMask, char LineBuffer[512],
212 int len, int LastOpaquePixel) {
214 * The data starts with a byte count. It tells the number of bytes on this
216 * Special case is a count of 1. It will change to next quadrant.
217 * Other special case is 0. It will end the sprite.
219 * Ordinary data packet. These are bits in a stream.
222 * for literal you put "count" values
223 * for packed you repeat the value "count" times
224 * Never use packed mode for one pixel
225 * If the last bit on a line is 1 you need to add a byte of zeroes
226 * A sequence 00000 ends a scan line
228 * All data is high nybble first
233 unsigned char differ[16];
234 unsigned char *d_ptr;
240 for (i = 0; i < len; i++) {
241 /* Fetch next pixel index into pixel buffer */
242 AssembleByte(ColorBits, LineBuffer[i] & ColorMask);
245 /* Write the buffer to file */
251 if (ChoosePackagingMode(len, i, LineBuffer)) {
252 /* Make runlength packet */
261 } while (V == LineBuffer[i] && len && count != 15);
263 AssembleByte(5, count);
264 AssembleByte(ColorBits, V);
267 /* Make packed literal packet */
273 while (ChoosePackagingMode(len, i, LineBuffer) == 0 && len && count != 15) {
280 AssembleByte(5, count | 0x10);
283 AssembleByte(ColorBits, *d_ptr++);
284 } while (--count >= 0);
289 /* Write the buffer to file */
294 if (LastOpaquePixel > -1) {
295 if (LastOpaquePixel < len - 1) {
296 len = LastOpaquePixel + 1;
300 if (ChoosePackagingMode(len, i, LineBuffer)) {
301 /* Make runlength packet */
310 } while (V == LineBuffer[i] && len && count != 15);
312 AssembleByte(5, count);
313 AssembleByte(ColorBits, V);
316 /* Make packed literal packet */
322 while (ChoosePackagingMode(len, i, LineBuffer) == 0 && len && count != 15) {
329 AssembleByte(5, count | 0x10);
332 AssembleByte(ColorBits, *d_ptr++);
333 } while (--count >= 0);
339 /* Write the buffer to file */
346 StrBuf* GenLynxSprite (const Bitmap* B, const Collection* A)
347 /* Generate binary output in Lynx sprite format for the bitmap B. The output
348 * is stored in a string buffer (which is actually a dynamic char array) and
351 * The Lynx will draw 4 quadrants:
357 * The sprite will end with a byte 0.
367 /* Action point of the sprite */
368 OX = GetActionPointX (A);
369 OY = GetActionPointY (A);
370 if (OX >= GetBitmapWidth (B)) {
371 Error ("Action point X cannot be larger than bitmap width");
373 if (OY >= GetBitmapHeight (B)) {
374 Error ("Action point Y cannot be larger than bitmap height");
377 /* Output the image properties */
378 Print (stdout, 1, "Image is %ux%u with %u colors%s\n",
379 GetBitmapWidth (B), GetBitmapHeight (B), GetBitmapColors (B),
380 BitmapIsIndexed (B)? " (indexed)" : "");
382 /* Get the sprite mode */
385 /* Now check if bitmap indexes are ok */
386 if (GetBitmapColors (B) > 16) {
387 Error ("Too many colors for a Lynx sprite");
391 if (GetBitmapColors (B) < 9) {
395 if (GetBitmapColors (B) < 5) {
399 if (GetBitmapColors (B) < 3) {
404 /* Create the output buffer and resize it to the required size. */
408 /* Convert the image for quadrant bottom right */
409 for (Y = OY; Y < (signed)GetBitmapHeight (B); ++Y) {
411 signed LastOpaquePixel = -1;
412 char LineBuffer[512]; /* The maximum size is 508 pixels */
414 /* Fill the LineBuffer for easier optimisation */
415 for (X = OX; X < (signed)GetBitmapWidth (B); ++X) {
417 /* Fetch next bit into byte buffer */
418 LineBuffer[i] = GetPixel (B, X, Y).Index & ColorMask;
426 encodeSprite(D, M, ColorBits, ColorMask, LineBuffer, i, LastOpaquePixel);
429 if ((OY == 0) && (OX == 0)) {
430 /* Trivial case only one quadrant */
432 /* Mark end of sprite */
433 SB_AppendChar (D, 0);
435 /* Return the converted bitmap */
440 SB_AppendChar (D, 1);
442 /* Convert the image for quadrant top right */
443 for (Y = OY - 1; Y >= 0; --Y) {
445 signed LastOpaquePixel = -1;
446 char LineBuffer[512]; /* The maximum size is 508 pixels */
448 /* Fill the LineBuffer for easier optimisation */
449 for (X = OX; X < (signed)GetBitmapWidth (B); ++X) {
451 /* Fetch next bit into byte buffer */
452 LineBuffer[i] = GetPixel (B, X, Y).Index & ColorMask;
460 encodeSprite(D, M, ColorBits, ColorMask, LineBuffer, i, LastOpaquePixel);
464 /* Special case only two quadrants */
466 /* Mark end of sprite */
467 SB_AppendChar (D, 0);
469 /* Return the converted bitmap */
474 SB_AppendChar (D, 1);
476 /* Convert the image for quadrant top left */
477 for (Y = OY - 1; Y >= 0; --Y) {
479 signed LastOpaquePixel = -1;
480 char LineBuffer[512]; /* The maximum size is 508 pixels */
482 /* Fill the LineBuffer for easier optimisation */
483 for (X = OX - 1; X >= 0; --X) {
485 /* Fetch next bit into byte buffer */
486 LineBuffer[i] = GetPixel (B, X, Y).Index & ColorMask;
494 encodeSprite(D, M, ColorBits, ColorMask, LineBuffer, i, LastOpaquePixel);
498 SB_AppendChar (D, 1);
500 /* Convert the image for quadrant bottom left */
501 for (Y = OY; Y < (signed)GetBitmapHeight (B); ++Y) {
503 signed LastOpaquePixel = -1;
504 char LineBuffer[512]; /* The maximum size is 508 pixels */
506 /* Fill the LineBuffer for easier optimisation */
507 for (X = OX - 1; X >= 0; --X) {
509 /* Fetch next bit into byte buffer */
510 LineBuffer[i] = GetPixel (B, X, Y).Index & ColorMask;
518 encodeSprite(D, M, ColorBits, ColorMask, LineBuffer, i, LastOpaquePixel);
522 SB_AppendChar (D, 0);
524 /* Return the converted bitmap */