private static Bitmap GenerateTexture(ModelTexHeader header, byte[] scanLines, byte[] palette) { var bmpTex = new Bitmap(header._bmWidth, header._bmHeight, PixelFormat.Format32bppArgb); var numPixels = header._bmWidth * header._bmHeight; var stride = header._bmSize / numPixels; var bmpData = bmpTex.LockBits(new Rectangle(0, 0, bmpTex.Width, bmpTex.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); var bytes = bmpData.Stride * bmpData.Height; var texBuffer = new byte[bytes]; //WARNING: This only works with powers of 2 sized images for (var i = 0; i < numPixels; i++) { var index = scanLines[i * stride + 0] * 4; texBuffer[i * 4 + 3] = stride == 2 ? scanLines[i * stride + 1] : (byte)255; // A texBuffer[i * 4 + 2] = palette[index + 2]; // R texBuffer[i * 4 + 1] = palette[index + 1]; // G texBuffer[i * 4 + 0] = palette[index + 0]; // B } Marshal.Copy(texBuffer, 0, bmpData.Scan0, bytes); bmpTex.UnlockBits(bmpData); return(bmpTex); }
private void Deserialize(BinaryReader reader) { header = reader.ReadBytes(ModelTexHeader.ARRAY_SIZE).ToStruct <ModelTexHeader>(); var bmLines = reader.ReadBytes(header._bmSize); var palBuf = reader.ReadBytes(256 * 4); Tex = GenerateTexture(header, bmLines, palBuf); }
private static Bitmap GenerateTexture(ModelTexHeader header, byte[] scanLines, byte[] palette) { var bmpTex = new Bitmap(header._bmWidth, header._bmHeight, PixelFormat.Format32bppArgb); var numPixels = header._bmWidth*header._bmHeight; var stride = header._bmSize/numPixels; var bmpData = bmpTex.LockBits(new Rectangle(0, 0, bmpTex.Width, bmpTex.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); var bytes = bmpData.Stride*bmpData.Height; var texBuffer = new byte[bytes]; //WARNING: This only works with powers of 2 sized images for (var i = 0; i < numPixels; i++) { var index = scanLines[i*stride + 0]*4; texBuffer[i*4 + 3] = stride == 2 ? scanLines[i*stride + 1] : (byte) 255; // A texBuffer[i*4 + 2] = palette[index + 2]; // R texBuffer[i*4 + 1] = palette[index + 1]; // G texBuffer[i*4 + 0] = palette[index + 0]; // B } Marshal.Copy(texBuffer, 0, bmpData.Scan0, bytes); bmpTex.UnlockBits(bmpData); return bmpTex; }
private void Deserialize(BinaryReader reader) { header = reader.ReadBytes(ModelTexHeader.ARRAY_SIZE).ToStruct<ModelTexHeader>(); var bmLines = reader.ReadBytes(header._bmSize); var palBuf = reader.ReadBytes(256*4); Tex = GenerateTexture(header, bmLines, palBuf); }