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 ("Sprite is too large for the Lynx");
139 OutBuffer[OutIndex++] = byte;
141 Error ("Sprite 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 */
332 StrBuf* GenLynxSprite (const Bitmap* B, const Collection* A)
333 /* Generate binary output in Lynx sprite format for the bitmap B. The output
334 * is stored in a string buffer (which is actually a dynamic char array) and
337 * The Lynx will draw 4 quadrants:
343 * The sprite will end with a byte 0.
353 /* Action point of the sprite */
354 OX = GetActionPointX (A);
355 OY = GetActionPointY (A);
356 if (OX >= GetBitmapWidth (B)) {
357 Error ("Action point X cannot be larger than bitmap width");
359 if (OY >= GetBitmapHeight (B)) {
360 Error ("Action point Y cannot be larger than bitmap height");
363 /* Output the image properties */
364 Print (stdout, 1, "Image is %ux%u with %u colors%s\n",
365 GetBitmapWidth (B), GetBitmapHeight (B), GetBitmapColors (B),
366 BitmapIsIndexed (B)? " (indexed)" : "");
368 /* Get the sprite mode */
371 /* Now check if bitmap indexes are ok */
372 if (GetBitmapColors (B) > 16) {
373 Error ("Too many colors for a Lynx sprite");
377 if (GetBitmapColors (B) < 9) {
381 if (GetBitmapColors (B) < 5) {
385 if (GetBitmapColors (B) < 3) {
390 /* Create the output buffer and resize it to the required size. */
394 /* Convert the image for quadrant bottom right */
395 for (Y = OY; Y < (signed)GetBitmapHeight (B); ++Y) {
397 signed LastOpaquePixel = -1;
398 char LineBuffer[512]; /* The maximum size is 508 pixels */
400 /* Fill the LineBuffer for easier optimisation */
401 for (X = OX; X < (signed)GetBitmapWidth (B); ++X) {
403 /* Fetch next bit into byte buffer */
404 LineBuffer[i] = GetPixel (B, X, Y).Index & ColorMask;
412 encodeSprite(D, M, ColorBits, ColorMask, LineBuffer, i, LastOpaquePixel);
415 if ((OY == 0) && (OX == 0)) {
416 /* Trivial case only one quadrant */
418 /* Mark end of sprite */
419 SB_AppendChar (D, 0);
421 /* Return the converted bitmap */
426 SB_AppendChar (D, 1);
428 /* Convert the image for quadrant top right */
429 for (Y = OY - 1; Y >= 0; --Y) {
431 signed LastOpaquePixel = -1;
432 char LineBuffer[512]; /* The maximum size is 508 pixels */
434 /* Fill the LineBuffer for easier optimisation */
435 for (X = OX; X < (signed)GetBitmapWidth (B); ++X) {
437 /* Fetch next bit into byte buffer */
438 LineBuffer[i] = GetPixel (B, X, Y).Index & ColorMask;
446 encodeSprite(D, M, ColorBits, ColorMask, LineBuffer, i, LastOpaquePixel);
450 /* Special case only two quadrants */
452 /* Mark end of sprite */
453 SB_AppendChar (D, 0);
455 /* Return the converted bitmap */
460 SB_AppendChar (D, 1);
462 /* Convert the image for quadrant top left */
463 for (Y = OY - 1; Y >= 0; --Y) {
465 signed LastOpaquePixel = -1;
466 char LineBuffer[512]; /* The maximum size is 508 pixels */
468 /* Fill the LineBuffer for easier optimisation */
469 for (X = OX - 1; X >= 0; --X) {
471 /* Fetch next bit into byte buffer */
472 LineBuffer[i] = GetPixel (B, X, Y).Index & ColorMask;
480 encodeSprite(D, M, ColorBits, ColorMask, LineBuffer, i, LastOpaquePixel);
484 SB_AppendChar (D, 1);
486 /* Convert the image for quadrant bottom left */
487 for (Y = OY; Y < (signed)GetBitmapHeight (B); ++Y) {
489 signed LastOpaquePixel = -1;
490 char LineBuffer[512]; /* The maximum size is 508 pixels */
492 /* Fill the LineBuffer for easier optimisation */
493 for (X = OX - 1; X >= 0; --X) {
495 /* Fetch next bit into byte buffer */
496 LineBuffer[i] = GetPixel (B, X, Y).Index & ColorMask;
504 encodeSprite(D, M, ColorBits, ColorMask, LineBuffer, i, LastOpaquePixel);
508 SB_AppendChar (D, 0);
510 /* Return the converted bitmap */