graphics tidying

This commit is contained in:
Denis Molony 2020-03-30 14:57:07 +10:00
parent 1119ab40e5
commit 20055db2d5
3 changed files with 46 additions and 39 deletions

View File

@ -4,6 +4,7 @@ import java.awt.Color;
import java.awt.image.DataBuffer; import java.awt.image.DataBuffer;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.List; import java.util.List;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
@ -20,8 +21,10 @@ public abstract class HiResImage extends AbstractFile
"Super Hi-Res Image (Apple Preferred Format)", "Packed QuickDraw II PICT File", "Super Hi-Res Image (Apple Preferred Format)", "Packed QuickDraw II PICT File",
"Packed Super Hi-Res 3200 color image" }; "Packed Super Hi-Res 3200 color image" };
static final int COLOR_TABLE_SIZE = 32; static final int COLOR_TABLE_SIZE = 32;
static final int COLOR_TABLE_OFFSET = 32000; static final int COLOR_TABLE_OFFSET_AUX_0 = 32_256;
static final int COLOR_TABLE_OFFSET_AUX_2 = 32_000;
public static final int FADDEN_AUX = 0x8066; public static final int FADDEN_AUX = 0x8066;
private byte[] fourBuf = new byte[4];
// ---- ---- ------ -------------------------------------- ------------------------ // ---- ---- ------ -------------------------------------- ------------------------
// File Type Aux Name Description // File Type Aux Name Description
@ -320,11 +323,11 @@ public abstract class HiResImage extends AbstractFile
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
int mode320Line (int ptr, int element, int max, ColorTable colorTable, int mode320Line (int ptr, int element, int maxBytes, ColorTable colorTable,
DataBuffer dataBuffer, int imageWidth) DataBuffer dataBuffer, int imageWidth)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
for (int i = 0; i < max; i++) for (int i = 0; i < maxBytes; i++)
{ {
if (ptr >= buffer.length) if (ptr >= buffer.length)
{ {
@ -332,7 +335,7 @@ public abstract class HiResImage extends AbstractFile
return ptr; return ptr;
} }
// get two pixels from this byte // get two pixels from this byte
int left = (buffer[ptr] & 0xF0) >> 4; int left = (buffer[ptr] & 0xF0) >>> 4;
int right = buffer[ptr++] & 0x0F; int right = buffer[ptr++] & 0x0F;
// get pixel colors // get pixel colors
@ -343,18 +346,19 @@ public abstract class HiResImage extends AbstractFile
draw (dataBuffer, element + imageWidth, rgbLeft, rgbLeft, rgbRight, rgbRight); draw (dataBuffer, element + imageWidth, rgbLeft, rgbLeft, rgbRight, rgbRight);
element = draw (dataBuffer, element, rgbLeft, rgbLeft, rgbRight, rgbRight); element = draw (dataBuffer, element, rgbLeft, rgbLeft, rgbRight, rgbRight);
} }
return ptr; return ptr;
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
int mode640Line (int ptr, int element, int max, ColorTable colorTable, int mode640Line (int ptr, int element, int maxBytes, ColorTable colorTable,
DataBuffer dataBuffer, int imageWidth) DataBuffer dataBuffer, int imageWidth)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
for (int i = 0; i < max; i++) for (int i = 0; i < maxBytes; i++)
{ {
// get four pixels from this byte // get four pixels from this byte
int p1 = (buffer[ptr] & 0xC0) >> 6; int p1 = (buffer[ptr] & 0xC0) >>> 6;
int p2 = (buffer[ptr] & 0x30) >> 4; int p2 = (buffer[ptr] & 0x30) >> 4;
int p3 = (buffer[ptr] & 0x0C) >> 2; int p3 = (buffer[ptr] & 0x0C) >> 2;
int p4 = (buffer[ptr++] & 0x03); int p4 = (buffer[ptr++] & 0x03);
@ -369,21 +373,23 @@ public abstract class HiResImage extends AbstractFile
draw (dataBuffer, element + imageWidth, rgb1, rgb2, rgb3, rgb4); // 2nd line draw (dataBuffer, element + imageWidth, rgb1, rgb2, rgb3, rgb4); // 2nd line
element = draw (dataBuffer, element, rgb1, rgb2, rgb3, rgb4); // 1st line element = draw (dataBuffer, element, rgb1, rgb2, rgb3, rgb4); // 1st line
} }
return ptr; return ptr;
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
int draw (DataBuffer dataBuffer, int element, int... rgb1) int draw (DataBuffer dataBuffer, int element, int... rgbList)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
if (dataBuffer.getSize () < rgb1.length + element) if (dataBuffer.getSize () < rgbList.length + element)
{ {
System.out.printf ("Bollocks: %d %d %d%n", dataBuffer.getSize (), rgb1.length, System.out.printf ("Bollocks: %d %d %d%n", dataBuffer.getSize (), rgbList.length,
element); element);
return element; return element;
} }
for (int i = 0; i < rgb1.length; i++)
dataBuffer.setElem (element++, rgb1[i]); for (int rgb : rgbList)
dataBuffer.setElem (element++, rgb);
return element; return element;
} }
@ -410,41 +416,45 @@ public abstract class HiResImage extends AbstractFile
// routine found here - http://kpreid.livejournal.com/4319.html // routine found here - http://kpreid.livejournal.com/4319.html
byte[] newBuf = new byte[calculateBufferSize (buffer)]; byte[] newBuf = new byte[calculateBufferSize (buffer)];
byte[] fourBuf = new byte[4];
int ptr = 0, newPtr = 0; int ptr = 0, newPtr = 0;
while (ptr < buffer.length) while (ptr < buffer.length)
{ {
int type = (buffer[ptr] & 0xC0) >> 6; // 0-3 int type = (buffer[ptr] & 0xC0) >>> 6; // 0-3
int count = (buffer[ptr++] & 0x3F) + 1; // 1-64 int count = (buffer[ptr++] & 0x3F) + 1; // 1-64
switch (type) switch (type)
{ {
case 0: // copy next 1-64 bytes as is case 0: // copy next 1-64 bytes as is
count = Math.min (count, buffer.length - ptr); count = Math.min (count, buffer.length - ptr);
while (count-- != 0) System.arraycopy (buffer, ptr, newBuf, newPtr, count);
newBuf[newPtr++] = buffer[ptr++]; newPtr += count;
ptr += count;
break; break;
case 1: // repeat next byte 3/5/6/7 times case 1: // repeat next byte 3/5/6/7 times
byte b = buffer[ptr++]; Arrays.fill (newBuf, newPtr, newPtr + count, buffer[ptr++]);
while (count-- != 0) newPtr += count;
newBuf[newPtr++] = b;
break; break;
case 2: // repeat next 4 bytes (count) times case 2: // repeat next 4 bytes (count) times
for (int i = 0; i < 4; i++) fourBuf[0] = buffer[ptr++];
fourBuf[i] = buffer[ptr++]; fourBuf[1] = buffer[ptr++];
fourBuf[2] = buffer[ptr++];
fourBuf[3] = buffer[ptr++];
while (count-- != 0) while (count-- != 0)
for (int i = 0; i < 4; i++) {
newBuf[newPtr++] = fourBuf[i]; newBuf[newPtr++] = fourBuf[0];
newBuf[newPtr++] = fourBuf[1];
newBuf[newPtr++] = fourBuf[2];
newBuf[newPtr++] = fourBuf[3];
}
break; break;
case 3: // repeat next byte (4*count) times case 3: // repeat next byte (4*count) times
b = buffer[ptr++];
count *= 4; count *= 4;
while (count-- != 0) Arrays.fill (newBuf, newPtr, newPtr + count, buffer[ptr++]);
newBuf[newPtr++] = b; newPtr += count;
break; break;
} }
} }
@ -456,8 +466,6 @@ public abstract class HiResImage extends AbstractFile
int unpackLine (byte[] buffer, byte[] newBuf, int newPtr) int unpackLine (byte[] buffer, byte[] newBuf, int newPtr)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
byte[] fourBuf = new byte[4];
int ptr = 0; int ptr = 0;
while (ptr < buffer.length) while (ptr < buffer.length)
{ {

View File

@ -123,7 +123,9 @@ public class SHRPictureFile1 extends HiResImage
boolean mode320 = (mainBlock.masterMode & 0x80) == 0; boolean mode320 = (mainBlock.masterMode & 0x80) == 0;
int imageWidth = mainBlock.pixelsPerScanLine * (mode320 ? 2 : 4); int imageWidth = mainBlock.pixelsPerScanLine;
if (mode320)
imageWidth *= 2;
image = new BufferedImage (imageWidth, mainBlock.numScanLines * 2, image = new BufferedImage (imageWidth, mainBlock.numScanLines * 2,
BufferedImage.TYPE_INT_RGB); BufferedImage.TYPE_INT_RGB);
@ -148,12 +150,12 @@ public class SHRPictureFile1 extends HiResImage
multipalBlock != null ? multipalBlock.colorTables[line] multipalBlock != null ? multipalBlock.colorTables[line]
: mainBlock.colorTables[lo & 0x0F]; : mainBlock.colorTables[lo & 0x0F];
int max = mainBlock.pixelsPerScanLine / (mode320 ? 2 : 4); int maxBytes = mainBlock.pixelsPerScanLine / (mode320 ? 2 : 4);
if (mode320) // two pixels per byte if (mode320) // two pixels per byte
ptr = mode320Line (ptr, element, max, colorTable, dataBuffer, imageWidth); ptr = mode320Line (ptr, element, maxBytes, colorTable, dataBuffer, imageWidth);
else // four pixels per byte else // four pixels per byte
ptr = mode640Line (ptr, element, max, colorTable, dataBuffer, imageWidth); ptr = mode640Line (ptr, element, maxBytes, colorTable, dataBuffer, imageWidth);
element += imageWidth * 2; // drawing two lines at a time element += imageWidth * 2; // drawing two lines at a time
} }
@ -342,10 +344,6 @@ public class SHRPictureFile1 extends HiResImage
System.out.printf ("Unexpected line width %3d %5d %5d %3d%n", line, oldPtr, System.out.printf ("Unexpected line width %3d %5d %5d %3d%n", line, oldPtr,
ptr, ptr - oldPtr); ptr, ptr - oldPtr);
ptr = oldPtr + width; ptr = oldPtr + width;
// something strange happening here
if (line == 102 && name.equals ("DRAGON.SHR"))
ptr -= 132;
} }
SHRPictureFile1.this.buffer = unpackedBuffer; SHRPictureFile1.this.buffer = unpackedBuffer;

View File

@ -153,7 +153,8 @@ public class SHRPictureFile2 extends HiResImage
colorTables = new ColorTable[16]; colorTables = new ColorTable[16];
for (int i = 0; i < colorTables.length; i++) for (int i = 0; i < colorTables.length; i++)
colorTables[i] = new ColorTable (i, buffer, 32256 + i * COLOR_TABLE_SIZE); colorTables[i] =
new ColorTable (i, buffer, COLOR_TABLE_OFFSET_AUX_0 + i * COLOR_TABLE_SIZE);
break; break;
@ -171,12 +172,12 @@ public class SHRPictureFile2 extends HiResImage
failureReason = "Buffer should be 38,400 bytes"; failureReason = "Buffer should be 38,400 bytes";
return; return;
} }
int maxTables = (buffer.length - COLOR_TABLE_OFFSET) / COLOR_TABLE_SIZE; int maxTables = (buffer.length - COLOR_TABLE_OFFSET_AUX_2) / COLOR_TABLE_SIZE;
colorTables = new ColorTable[maxTables]; colorTables = new ColorTable[maxTables];
for (int i = 0; i < colorTables.length; i++) for (int i = 0; i < colorTables.length; i++)
{ {
colorTables[i] = colorTables[i] =
new ColorTable (i, buffer, COLOR_TABLE_OFFSET + i * COLOR_TABLE_SIZE); new ColorTable (i, buffer, COLOR_TABLE_OFFSET_AUX_2 + i * COLOR_TABLE_SIZE);
colorTables[i].reverse (); colorTables[i].reverse ();
} }
break; break;