+ /* Convert the image for quadrant bottom right */
+ for (Y = OY; Y < GetBitmapHeight (B); ++Y) {
+ unsigned char V = 0;
+ unsigned char OutIndex;
+ unsigned W = 0;
+ signed i = 0;
+ signed j;
+ signed k;
+ signed LastOpaquePixel = -1;
+
+ /* Fill the LineBuffer for easier optimisation */
+ for (X = OX; X < GetBitmapWidth (B); ++X) {
+
+ /* Fetch next bit into byte buffer */
+ LineBuffer[i] = GetPixel (B, X, Y).Index & ColorMask;
+
+ if (LineBuffer[i]) {
+ LastOpaquePixel = i;
+ }
+ ++i;
+ }
+
+ switch (M) {
+ case smLiteral:
+ OutIndex = 0;
+ for (j = 0; j < i; j++) {
+ /* Fetch next pixel index into pixel buffer */
+ W = (W << ColorBits) | (LineBuffer[j] & ColorMask);
+ k += ColorBits;
+ if (k > 7) {
+ /* The byte is ready */
+ k -= 8;
+ V = (W >> k) & 0xFF;
+ OutBuffer[OutIndex++] = V;
+ if (!OutIndex) {
+ Error ("Sprite is too large for the Lynx");
+ }
+ }
+ }
+ /* Output last bits */
+ if (k != 0) {
+ W = (W << (8-k));
+ k = 0;
+ V = W & 0xFF;
+ OutBuffer[OutIndex++] = V;
+ if (!OutIndex) {
+ Error ("Sprite is too large for the Lynx");
+ }
+ }
+ /* Fix bug in Lynx where the last bit on a line is 1 */
+ if (V & 1) {
+ OutBuffer[OutIndex++] = 0;
+ if (!OutIndex) {
+ Error ("Sprite is too large for the Lynx");
+ }
+ }
+ /* Fix bug in Lynx where the count cannot be 1 */
+ if (OutIndex == 1) {
+ OutBuffer[OutIndex++] = 0;
+ }
+ /* Write the byte count to the end of the scanline */
+ if (OutIndex == 255) {
+ Error ("Sprite is too large for the Lynx");
+ }
+ SB_AppendChar (D, OutIndex+1);
+ /* Write scanline data */
+ for (j = 0; j < OutIndex; j++) {
+ SB_AppendChar (D, OutBuffer[j]);
+ }
+ break;
+ case smPacked:
+ /* Bug workaround: If last bit is 1 and it is in bit0 add a zero byte */
+ /* Note: These extra pixels will be painted also. There is no workaround for this */
+ if (LineBuffer[i - 1] & 0x01) {
+ LineBuffer[i++] = 0;
+ }
+ /* Logical problem workaround: The count can not be 1 so add an extra byte */
+ if (i == 1) {
+ LineBuffer[i++] = 0;
+ }
+ /* Write the byte count for this partial scanline */
+ SB_AppendChar (D, i);
+ for (i = 0; i < LineBuffer[0]; i++) {
+ SB_AppendChar (D, LineBuffer[i]);
+ }
+ break;
+ case smPackedTransparent:
+ break;
+ }
+ }
+
+ if ((OY == 0) && (OX == 0)) {
+ /* Trivial case only one quadrant */
+
+ /* Return the converted bitmap */
+ return D;
+ }
+
+ /* Next quadrant */
+ SB_AppendChar (D, 1);
+
+ /* Convert the image for quadrant top right */
+ for (Y = OY - 1; Y >= 0; --Y) {