Skip to content

Commit

Permalink
Fix for shadow images
Browse files Browse the repository at this point in the history
Fixed shadow images by setting palette colors to two. Also changed pixel data read/write to use height and pitch.
  • Loading branch information
TechCor8 committed Jul 12, 2020
1 parent 5989c9f commit 0a62a54
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
13 changes: 8 additions & 5 deletions OP2UtilityDotNet/src/Bitmap/BitmapFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static void WriteIndexed(BinaryWriter seekableWriter, ushort bitCount, in
for (int i=0; i < palette.Length; ++i)
palette[i].Serialize(seekableWriter);

WritePixels(seekableWriter, indexedPixels, width, bitCount);
WritePixels(seekableWriter, indexedPixels, width, height, bitCount);
}
public static void WriteIndexed(string filename, BitmapFile bitmapFile)
{
Expand Down Expand Up @@ -203,16 +203,19 @@ private static void WriteHeaders(BinaryWriter seekableWriter, ushort bitCount, i
bmpHeader.Serialize(seekableWriter);
imageHeader.Serialize(seekableWriter);
}
private static void WritePixels(BinaryWriter seekableWriter, byte[] pixels, int width, ushort bitCount)
private static void WritePixels(BinaryWriter seekableWriter, byte[] pixels, int width, int height, ushort bitCount)
{
height = System.Math.Abs(height);

int pitch = ImageHeader.CalculatePitch(bitCount, width);
int bytesOfPixelsPerRow = ImageHeader.CalcPixelByteWidth(bitCount, width);
byte[] padding = new byte[pitch - bytesOfPixelsPerRow];

for (int i = 0; i < pixels.Length;) {
seekableWriter.Write(pixels, i, bytesOfPixelsPerRow);
//for (int i = 0; i+bytesOfPixelsPerRow <= pixels.Length; i += pitch) {
for (int y=0; y < height; ++y)
{
seekableWriter.Write(pixels, y*pitch, bytesOfPixelsPerRow);
seekableWriter.Write(padding);
i += pitch;
}
}

Expand Down
25 changes: 20 additions & 5 deletions OP2UtilityDotNet/src/Sprite/OP2BmpLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,29 @@ public MemoryStream GetImageStream(int index)

ImageMeta imageMeta = artFile.imageMetas[index];

Color[] palette = new Color[artFile.palettes[imageMeta.paletteIndex].colors.Length];
System.Array.Copy(artFile.palettes[imageMeta.paletteIndex].colors, palette, palette.Length);
Color[] palette;
if (imageMeta.type.bShadow != 0)
{
// Shadow graphic uses a 2 color palette
palette = new Color[2];
System.Array.Copy(artFile.palettes[imageMeta.paletteIndex].colors, palette, palette.Length);
}
else
{
// Normal graphic
palette = new Color[artFile.palettes[imageMeta.paletteIndex].colors.Length];
System.Array.Copy(artFile.palettes[imageMeta.paletteIndex].colors, palette, palette.Length);
}

// Palette length is always 256 for OP2's master BMP
uint pixelOffset = (uint)(imageMeta.pixelDataOffset + 14 + ImageHeader.SizeInBytes + 256 * Color.SizeInBytes);

uint pixelOffset = (uint)(imageMeta.pixelDataOffset + 14 + ImageHeader.SizeInBytes + palette.Length * Color.SizeInBytes);
int height = (int)System.Math.Abs(imageMeta.height);
int pitch = ImageHeader.CalculatePitch(imageMeta.GetBitCount(), (int)imageMeta.width);

SliceStream pixels = GetPixels(pixelOffset, imageMeta.scanLineByteWidth * imageMeta.height);
SliceStream pixels = GetPixels(pixelOffset, (uint)(height * pitch));

byte[] pixelContainer = new byte[imageMeta.scanLineByteWidth * imageMeta.height];
byte[] pixelContainer = new byte[height * pitch];
pixels.Read(pixelContainer, 0, pixelContainer.Length);

// Outpost 2 stores pixels in normal raster scan order (top-down). This requires a negative height for BMP file format.
Expand Down

0 comments on commit 0a62a54

Please sign in to comment.