Example #1
0
            private static 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;

                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)
                        {
                            Debug.LogError("Indexed bitmap has indices greater than it's color palette");
                            return;
                        }

                        data[x + y * w] = bmp.palette[v];
                    }

                    bitReader.Flush();
                    for (int i = 0; i < pad; i++)
                    {
                        aReader.ReadByte();
                    }
                }
            }