private void ReadIndexedImage(BinaryReader aReader, BMPImage bmp) { int w = Mathf.Abs(bmp.info.width); int h = Mathf.Abs(bmp.info.height); int bitCount = bmp.info.nBitsPerPixel; int rowLength = ((bitCount * w + 31) / 32) * 4; int count = rowLength * h; int pad = rowLength - (w * bitCount + 7) / 8; if (useTransparencyKey) { //bmp.palette[0] = new Color32(0, 0, 0, 0); for (int i = 0; i < bmp.palette.Count; i++) { var col = bmp.palette[i]; if (col.r == bmp.palette[0].r && col.g == bmp.palette[0].g && col.b == bmp.palette[0].b ) { bmp.palette[i] = new Color32(col.r, col.g, col.b, 0); } } } Color32[] data = bmp.imageData = new Color32[w * h]; if (aReader.BaseStream.Position + count > aReader.BaseStream.Length) { Debug.LogError("Unexpected end of file. (Have " + (aReader.BaseStream.Position + count) + " bytes, expected " + aReader.BaseStream.Length + " bytes)"); return; } BitStreamReader bitReader = new BitStreamReader(aReader); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int v = (int)bitReader.ReadBits(bitCount); if (v >= bmp.palette.Count) { v = 0; } if (v >= bmp.palette.Count) { //Debug.LogError("Indexed bitmap has indices greater than it's color palette"); continue; } data[x + y * w] = bmp.palette[v]; } bitReader.Flush(); for (int i = 0; i < pad; i++) { aReader.ReadByte(); } } }
private static void ReadIndexedImage(System.IO.BinaryReader aReader, BMPImage bmp) { int w = Mathf.Abs(bmp.info.width); int h = Mathf.Abs(bmp.info.height); int bitCount = bmp.info.nBitsPerPixel; int rowLength = ((bitCount * w + 31) / 32) * 4; int count = rowLength * h; int pad = rowLength - (w * bitCount + 7) / 8; Color32[] data = bmp.imageData = new Color32[w * h]; if (aReader.BaseStream.Position + count > aReader.BaseStream.Length) { Debug.LogError("Unexpected end of file. (Have " + (aReader.BaseStream.Position + count) + " bytes, expected " + aReader.BaseStream.Length + " bytes)"); return; } BitStreamReader bitReader = new BitStreamReader(aReader); bool outOfPalette = false; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int v = (int)bitReader.ReadBits(bitCount); if (v >= bmp.palette.Count) { outOfPalette = true; v = 0; } data[x + y * w] = bmp.palette[v]; } bitReader.Flush(); for (int i = 0; i < pad; i++) { aReader.ReadByte(); } } if (outOfPalette) { Debug.LogWarning("Indexed bitmap has indices greater than it's color palette"); } }