public void CalculatePitch() { Assert.AreEqual(4, ImageHeader.CalculatePitch(1, 1)); Assert.AreEqual(4, ImageHeader.CalculatePitch(1, 32)); Assert.AreEqual(8, ImageHeader.CalculatePitch(1, 33)); Assert.AreEqual(4, ImageHeader.CalculatePitch(4, 1)); Assert.AreEqual(4, ImageHeader.CalculatePitch(4, 8)); Assert.AreEqual(8, ImageHeader.CalculatePitch(4, 9)); Assert.AreEqual(4, ImageHeader.CalculatePitch(8, 1)); Assert.AreEqual(4, ImageHeader.CalculatePitch(8, 4)); Assert.AreEqual(8, ImageHeader.CalculatePitch(8, 5)); // Test non-static version of function ImageHeader imageHeader = new ImageHeader(); imageHeader.bitCount = 1; imageHeader.width = 1; Assert.AreEqual(4, imageHeader.CalculatePitch()); }
public OP2BitmapFile GetImage(int index) { artFile.VerifyImageIndexInBounds(index); ImageMeta imageMeta = artFile.imageMetas[index]; 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); int height = (int)System.Math.Abs(imageMeta.height); int pitch = ImageHeader.CalculatePitch(imageMeta.GetBitCount(), (int)imageMeta.width); SliceStream pixels = GetPixels(pixelOffset, (uint)(height * pitch)); 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. if (imageMeta.height > uint.MaxValue) { throw new System.Exception("Image height is too large to fit in standard bitmap file format."); } return(new OP2BitmapFile(imageMeta, palette, pixelContainer)); }