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");
155 if (!(--bit_counter)) {
156 OutBuffer[OutIndex++] = byte;
158 Error ("Sprite is too large for the Lynx");
169 static void AssembleByteLiteral(unsigned bits, char val)
171 static char bit_counter = 8, byte = 0;
180 /* handle end of line */
182 if (bit_counter != 8) {
183 byte <<= bit_counter;
184 OutBuffer[OutIndex++] = byte;
186 Error ("Sprite is too large for the Lynx");
199 if (!(--bit_counter)) {
200 OutBuffer[OutIndex++] = byte;
202 Error ("Sprite is too large for the Lynx");
213 static unsigned char ChoosePackagingMode(signed len, signed index, char LineBuffer[512])
219 if (LineBuffer[index] != LineBuffer[index + 1]) {
225 static void WriteOutBuffer(StrBuf *D)
229 /* Fix bug in Lynx where the count cannot be 1 */
231 OutBuffer[OutIndex++] = 0;
233 /* Write the byte count to the end of the scanline */
234 if (OutIndex == 255) {
235 Error ("Sprite is too large for the Lynx");
237 SB_AppendChar (D, OutIndex+1);
238 /* Write scanline data */
239 for (i = 0; i < OutIndex; i++) {
240 SB_AppendChar (D, OutBuffer[i]);
244 static void encodeSprite(StrBuf *D, enum Mode M, char ColorBits, char ColorMask, char LineBuffer[512],
245 int len, int LastOpaquePixel) {
247 * The data starts with a byte count. It tells the number of bytes on this
249 * Special case is a count of 1. It will change to next quadrant.
250 * Other special case is 0. It will end the sprite.
252 * Ordinary data packet. These are bits in a stream.
255 * for literal you put "count" values
256 * for packed you repeat the value "count" times
257 * Never use packed mode for one pixel
258 * If the last bit on a line is 1 you need to add a byte of zeroes
259 * A sequence 00000 ends a scan line
261 * All data is high nybble first
266 unsigned char differ[16];
267 unsigned char *d_ptr;
272 AssembleByteLiteral(0, 0);
273 for (i = 0; i < len; i++) {
274 /* Fetch next pixel index into pixel buffer */
275 AssembleByteLiteral(ColorBits, LineBuffer[i] & ColorMask);
277 AssembleByteLiteral(8, 0);
278 /* Write the buffer to file */
285 if (ChoosePackagingMode(len, i, LineBuffer)) {
286 /* Make runlength packet */
295 } while (V == LineBuffer[i] && len && count != 15);
297 AssembleByte(5, count);
298 AssembleByte(ColorBits, V);
301 /* Make packed literal packet */
307 while (ChoosePackagingMode(len, i, LineBuffer) == 0 && len && count != 15) {
314 AssembleByte(5, count | 0x10);
317 AssembleByte(ColorBits, *d_ptr++);
318 } while (--count >= 0);
323 /* Write the buffer to file */
328 if (LastOpaquePixel >= 0 && LastOpaquePixel < len) {
329 len = LastOpaquePixel;
334 if (ChoosePackagingMode(len, i, LineBuffer)) {
335 /* Make runlength packet */
344 } while (V == LineBuffer[i] && len && count != 15);
346 AssembleByte(5, count);
347 AssembleByte(ColorBits, V);
350 /* Make packed literal packet */
356 while (ChoosePackagingMode(len, i, LineBuffer) == 0 && len && count != 15) {
363 AssembleByte(5, count | 0x10);
366 AssembleByte(ColorBits, *d_ptr++);
367 } while (--count >= 0);
373 /* Write the buffer to file */
379 StrBuf* GenLynxSprite (const Bitmap* B, const Collection* A)
380 /* Generate binary output in Lynx sprite format for the bitmap B. The output
381 * is stored in a string buffer (which is actually a dynamic char array) and
384 * The Lynx will draw 4 quadrants:
390 * The sprite will end with a byte 0.
400 /* Action point of the sprite */
401 OX = GetActionPointX (A);
402 OY = GetActionPointY (A);
403 if (OX >= GetBitmapWidth (B)) {
404 Error ("Action point X cannot be larger than bitmap width");
406 if (OY >= GetBitmapHeight (B)) {
407 Error ("Action point Y cannot be larger than bitmap height");
410 /* Output the image properties */
411 Print (stdout, 1, "Image is %ux%u with %u colors%s\n",
412 GetBitmapWidth (B), GetBitmapHeight (B), GetBitmapColors (B),
413 BitmapIsIndexed (B)? " (indexed)" : "");
415 /* Get the sprite mode */
418 /* Now check if bitmap indexes are ok */
419 if (GetBitmapColors (B) > 16) {
420 Error ("Too many colors for a Lynx sprite");
424 if (GetBitmapColors (B) < 9) {
428 if (GetBitmapColors (B) < 5) {
432 if (GetBitmapColors (B) < 3) {
437 /* Create the output buffer and resize it to the required size. */
441 /* Convert the image for quadrant bottom right */
442 for (Y = OY; Y < (signed)GetBitmapHeight (B); ++Y) {
444 signed LastOpaquePixel = -1;
445 char LineBuffer[512]; /* The maximum size is 508 pixels */
447 /* Fill the LineBuffer for easier optimisation */
448 for (X = OX; X < (signed)GetBitmapWidth (B); ++X) {
450 /* Fetch next bit into byte buffer */
451 LineBuffer[i] = GetPixel (B, X, Y).Index & ColorMask;
459 encodeSprite(D, M, ColorBits, ColorMask, LineBuffer, i, LastOpaquePixel);
462 if ((OY == 0) && (OX == 0)) {
463 /* Trivial case only one quadrant */
465 /* Mark end of sprite */
466 SB_AppendChar (D, 0);
468 /* Return the converted bitmap */
473 SB_AppendChar (D, 1);
475 /* Convert the image for quadrant top right */
476 for (Y = OY - 1; Y >= 0; --Y) {
478 signed LastOpaquePixel = -1;
479 char LineBuffer[512]; /* The maximum size is 508 pixels */
481 /* Fill the LineBuffer for easier optimisation */
482 for (X = OX; X < (signed)GetBitmapWidth (B); ++X) {
484 /* Fetch next bit into byte buffer */
485 LineBuffer[i] = GetPixel (B, X, Y).Index & ColorMask;
493 encodeSprite(D, M, ColorBits, ColorMask, LineBuffer, i, LastOpaquePixel);
497 /* Special case only two quadrants */
499 /* Mark end of sprite */
500 SB_AppendChar (D, 0);
502 /* Return the converted bitmap */
507 SB_AppendChar (D, 1);
509 /* Convert the image for quadrant top left */
510 for (Y = OY - 1; Y >= 0; --Y) {
512 signed LastOpaquePixel = -1;
513 char LineBuffer[512]; /* The maximum size is 508 pixels */
515 /* Fill the LineBuffer for easier optimisation */
516 for (X = OX - 1; X >= 0; --X) {
518 /* Fetch next bit into byte buffer */
519 LineBuffer[i] = GetPixel (B, X, Y).Index & ColorMask;
527 encodeSprite(D, M, ColorBits, ColorMask, LineBuffer, i, LastOpaquePixel);
531 SB_AppendChar (D, 1);
533 /* Convert the image for quadrant bottom left */
534 for (Y = OY; Y < (signed)GetBitmapHeight (B); ++Y) {
536 signed LastOpaquePixel = -1;
537 char LineBuffer[512]; /* The maximum size is 508 pixels */
539 /* Fill the LineBuffer for easier optimisation */
540 for (X = OX - 1; X >= 0; --X) {
542 /* Fetch next bit into byte buffer */
543 LineBuffer[i] = GetPixel (B, X, Y).Index & ColorMask;
551 encodeSprite(D, M, ColorBits, ColorMask, LineBuffer, i, LastOpaquePixel);
555 SB_AppendChar (D, 0);
557 /* Return the converted bitmap */