refactoring graphics code

This commit is contained in:
Denis Molony 2020-03-31 21:00:40 +10:00
parent 538ef50d45
commit 0b233842a8
3 changed files with 95 additions and 40 deletions

View File

@ -25,6 +25,8 @@ public abstract class HiResImage extends AbstractFile
static final int COLOR_TABLE_OFFSET_AUX_2 = 32_000;
public static final int FADDEN_AUX = 0x8066;
private byte[] fourBuf = new byte[4];
private ColorTable defaultColorTable320 = new ColorTable (0, 0x00);
private ColorTable defaultColorTable640 = new ColorTable (0, 0x80);
// ---- ---- ------ -------------------------------------- ------------------------
// File Type Aux Name Description
@ -323,11 +325,14 @@ public abstract class HiResImage extends AbstractFile
}
// ---------------------------------------------------------------------------------//
int mode320Line (int ptr, int element, int maxBytes, ColorTable colorTable,
int mode320Line (int ptr, int element, int dataWidth, ColorTable colorTable,
DataBuffer dataBuffer, int imageWidth)
// ---------------------------------------------------------------------------------//
{
for (int i = 0; i < maxBytes; i++)
if (colorTable == null)
colorTable = defaultColorTable320;
for (int i = 0; i < dataWidth; i++)
{
if (ptr >= buffer.length)
{
@ -351,11 +356,14 @@ public abstract class HiResImage extends AbstractFile
}
// ---------------------------------------------------------------------------------//
int mode640Line (int ptr, int element, int maxBytes, ColorTable colorTable,
int mode640Line (int ptr, int element, int dataWidth, ColorTable colorTable,
DataBuffer dataBuffer, int imageWidth)
// ---------------------------------------------------------------------------------//
{
for (int i = 0; i < maxBytes; i++)
if (colorTable == null)
colorTable = defaultColorTable640;
for (int i = 0; i < dataWidth; i++)
{
// get four pixels from this byte
int p1 = (buffer[ptr] & 0xC0) >>> 6;
@ -408,7 +416,7 @@ public abstract class HiResImage extends AbstractFile
* (as in 10xxxxxx case)
*/
// this should call unpackLine()
// only called by SHRPictureFile2
// ---------------------------------------------------------------------------------//
byte[] unpack (byte[] buffer) throws ArrayIndexOutOfBoundsException
// ---------------------------------------------------------------------------------//
@ -462,52 +470,94 @@ public abstract class HiResImage extends AbstractFile
}
// Super Hi-res IIGS (MAIN in $C0/02)
// only called by SHRPictureFile1
// ---------------------------------------------------------------------------------//
int unpackLine (byte[] buffer, byte[] newBuf, int newPtr)
int unpackLine (byte[] buffer, int ptr, byte[] newBuf, int newPtr)
// ---------------------------------------------------------------------------------//
{
int ptr = 0;
while (ptr < buffer.length)
int savePtr = newPtr;
while (ptr < buffer.length - 1) // minimum 2 bytes needed
{
int type = (buffer[ptr] & 0xC0) >>> 6; // 0-3
int count = (buffer[ptr++] & 0x3F) + 1; // 1-64
switch (type)
{
case 0:
while (count-- != 0)
if (newPtr < newBuf.length && ptr < buffer.length)
newBuf[newPtr++] = buffer[ptr++];
case 0: // 2-65 bytes
while (count-- != 0 && newPtr < newBuf.length && ptr < buffer.length)
newBuf[newPtr++] = buffer[ptr++];
break;
case 1:
case 1: // 2 bytes
byte b = buffer[ptr++];
while (count-- != 0)
if (newPtr < newBuf.length)
newBuf[newPtr++] = b;
while (count-- != 0 && newPtr < newBuf.length)
newBuf[newPtr++] = b;
break;
case 2:
case 2: // 5 bytes
for (int i = 0; i < 4; i++)
if (ptr < buffer.length)
fourBuf[i] = buffer[ptr++];
fourBuf[i] = ptr < buffer.length ? buffer[ptr++] : 0;
while (count-- != 0)
for (int i = 0; i < 4; i++)
if (newPtr < newBuf.length)
newBuf[newPtr++] = fourBuf[i];
break;
case 3:
case 3: // 2 bytes
b = buffer[ptr++];
count *= 4;
while (count-- != 0)
if (newPtr < newBuf.length)
newBuf[newPtr++] = b;
while (count-- != 0 && newPtr < newBuf.length)
newBuf[newPtr++] = b;
break;
}
}
return newPtr;
return newPtr - savePtr; // bytes unpacked
}
// ---------------------------------------------------------------------------------//
void debug (byte[] buffer, int ptr, int length)
// ---------------------------------------------------------------------------------//
{
// int ptr = 0;
int size = 0;
int max = ptr + length;
while (ptr < max)
{
int type = (buffer[ptr] & 0xC0) >>> 6; // 0-3
int count = (buffer[ptr++] & 0x3F) + 1; // 1-64
System.out.printf ("%04X/%04d: %02X (%d,%2d) ", ptr - 1, size, buffer[ptr - 1],
type, count);
if (type == 0)
{
System.out.println (HexFormatter.getHexString (buffer, ptr, count));
ptr += count;
size += count;
}
else if (type == 1)
{
System.out.println (HexFormatter.getHexString (buffer, ptr, 1));
ptr++;
size += count;
}
else if (type == 2)
{
System.out.println (HexFormatter.getHexString (buffer, ptr, 4));
ptr += 4;
size += count * 4;
}
else
{
System.out.println (HexFormatter.getHexString (buffer, ptr, 1));
ptr++;
size += count * 4;
}
}
}
// ---------------------------------------------------------------------------------//

View File

@ -125,7 +125,7 @@ public class SHRPictureFile1 extends HiResImage
int imageWidth = mainBlock.pixelsPerScanLine;
if (mode320)
imageWidth *= 2;
imageWidth *= 2; // every horizontal pixel is drawn twice
image = new BufferedImage (imageWidth, mainBlock.numScanLines * 2,
BufferedImage.TYPE_INT_RGB);
@ -150,12 +150,12 @@ public class SHRPictureFile1 extends HiResImage
multipalBlock != null ? multipalBlock.colorTables[line]
: mainBlock.colorTables[lo & 0x0F];
int maxBytes = mainBlock.pixelsPerScanLine / (mode320 ? 2 : 4);
int dataWidth = mainBlock.pixelsPerScanLine / (mode320 ? 2 : 4);
if (mode320) // two pixels per byte
ptr = mode320Line (ptr, element, maxBytes, colorTable, dataBuffer, imageWidth);
if (mode320) // two pixels per byte, each shown twice
ptr = mode320Line (ptr, element, dataWidth, colorTable, dataBuffer, imageWidth);
else // four pixels per byte
ptr = mode640Line (ptr, element, maxBytes, colorTable, dataBuffer, imageWidth);
ptr = mode640Line (ptr, element, dataWidth, colorTable, dataBuffer, imageWidth);
element += imageWidth * 2; // drawing two lines at a time
}
@ -242,7 +242,7 @@ public class SHRPictureFile1 extends HiResImage
if (ptr < data.length - 32)
colorTables[i] = new ColorTable (i, data, ptr);
else
colorTables[i] = new ColorTable (i, 0x00); // default empty table
colorTables[i] = new ColorTable (i, 0x00); // default empty table !! not finished
ptr += 32;
}
}
@ -279,6 +279,7 @@ public class SHRPictureFile1 extends HiResImage
DirEntry[] scanLineDirectory; // [numScanLines]
byte[][] packedScanLines;
boolean mode640;
int dataWidth;
public Main (String kind, byte[] data)
{
@ -326,9 +327,9 @@ public class SHRPictureFile1 extends HiResImage
ptr += numBytes;
}
int width = pixelsPerScanLine / (mode640 ? 4 : 2);
dataWidth = pixelsPerScanLine / (mode640 ? 4 : 2);
byte[] unpackedBuffer = new byte[numScanLines * width];
byte[] unpackedBuffer = new byte[numScanLines * dataWidth];
ptr = 0;
for (int line = 0; line < numScanLines; line++)
{
@ -338,12 +339,13 @@ public class SHRPictureFile1 extends HiResImage
break;
}
int oldPtr = ptr;
ptr = unpackLine (packedScanLines[line], unpackedBuffer, ptr);
if (oldPtr + width != ptr)
System.out.printf ("Unexpected line width %3d %5d %5d %3d%n", line, oldPtr,
ptr, ptr - oldPtr);
ptr = oldPtr + width;
int bytesUnpacked = unpackLine (packedScanLines[line], 0, unpackedBuffer, ptr);
if (bytesUnpacked != dataWidth)
System.out.printf ("Unexpected line width %3d %5d %3d %3d%n", line, ptr,
bytesUnpacked, dataWidth);
ptr += dataWidth;
}
SHRPictureFile1.this.buffer = unpackedBuffer;
@ -370,7 +372,8 @@ public class SHRPictureFile1 extends HiResImage
text.append (String.format ("Kind ................. %s%n", kind));
text.append (String.format ("MasterMode ........... %04X%n", masterMode));
text.append (String.format ("PixelsPerScanLine .... %d%n", pixelsPerScanLine));
text.append (String.format ("PixelsPerScanLine .... %d / %d = %d bytes%n",
pixelsPerScanLine, (mode640 ? 4 : 2), dataWidth));
text.append (String.format ("NumColorTables ....... %d%n", numColorTables));
text.append (String.format ("NumScanLines ......... %d%n%n", numScanLines));

View File

@ -223,8 +223,10 @@ public class SHRPictureFile2 extends HiResImage
mode320 = (controlByte & 0x80) == 0;
fillMode = (controlByte & 0x20) != 0;
}
else
else if (line < colorTables.length)
colorTable = colorTables[line];
else
colorTable = null;
if (mode320) // two pixels per col
ptr = mode320Line (ptr, element, max, colorTable, dataBuffer, imageWidth);