/// <summary> /// Construct a new DefineBitsLossLessTag object /// from an image object. /// </summary> /// <param name="characterId">Character id.</param> /// <param name="image">Image.</param> /// <returns></returns> public static DefineBitsLossLessTag FromImage(ushort characterId, Image image) { if (image.RawFormat.Equals(ImageFormat.Bmp) == false && image.RawFormat.Equals(ImageFormat.MemoryBmp) == false) { throw new InvalidImageFormatException(); } Bitmap bitmap = (Bitmap)image; byte format = 0; PixelFormat pxFormat = bitmap.PixelFormat; if (pxFormat.Equals(PixelFormat.Format8bppIndexed)) { format = 3; } else if (pxFormat.Equals(PixelFormat.Format16bppRgb555) || pxFormat.Equals(PixelFormat.Format16bppRgb565)) { format = 4; } else if (pxFormat.Equals(PixelFormat.Format24bppRgb)) { format = 5; } else { throw new InvalidPixelFormatException(); } DefineBitsLossLessTag bmp = new DefineBitsLossLessTag(); bmp.CharacterId = characterId; bmp.BitmapFormat = format; bmp.BitmapWidth = (ushort)bitmap.Width; bmp.BitmapHeight = (ushort)bitmap.Height; int imageSize = bitmap.Width * bitmap.Height; if (bmp.BitmapFormat == 3) { //TODO } else if (bmp.BitmapFormat == 4) { Pix15[] bitmapPixelData = new Pix15[imageSize]; int k = 0; for (int i = 0; i < bitmap.Height; i++) { for (int j = 0; j < bitmap.Width; j++) { Color color = bitmap.GetPixel(j, i); bitmapPixelData[k] = new Pix15((byte)color.R, (byte)color.G, (byte)color.B); k++; } } bmp.BitmapData = new BitmapColorData(bitmapPixelData); } else if (bmp.BitmapFormat == 5) { Pix24[] bitmapPixelData = new Pix24[imageSize]; int k = 0; for (int i = 0; i < bitmap.Height; i++) { for (int j = 0; j < bitmap.Width; j++) { Color color = bitmap.GetPixel(j, i); bitmapPixelData[k] = new Pix24((byte)color.R, (byte)color.G, (byte)color.B); k++; } } bmp.BitmapData = new BitmapColorData(bitmapPixelData); } return(bmp); }
/// <summary> /// see <see cref="SwfDotNet.IO.Tags.BaseTag">base class</see> /// </summary> public override void ReadData(byte version, BufferedBinaryReader binaryReader) { RecordHeader rh = new RecordHeader(); rh.ReadData(binaryReader); int beforePos = (int)binaryReader.BaseStream.Position; int toReaded = (int)rh.TagLength - 7; _characterId = binaryReader.ReadUInt16(); _bitmapFormat = binaryReader.ReadByte(); _bitmapWidth = binaryReader.ReadUInt16(); _bitmapHeight = binaryReader.ReadUInt16(); _bitmapColorTableSize = 0; if (_bitmapFormat == 3) { _bitmapColorTableSize = binaryReader.ReadByte(); toReaded--; } if (_bitmapFormat == 3) { _colorMapData = new ColorMapData(); _colorMapData.ReadData(binaryReader, _bitmapColorTableSize, _bitmapWidth, _bitmapHeight, toReaded); } else if (_bitmapFormat == 4 || _bitmapFormat == 5) { int imageSize = _bitmapWidth * _bitmapHeight; int uncompressedSize = imageSize; if (_bitmapFormat == 4) { uncompressedSize *= 2; } else { uncompressedSize *= 4; } byte[] uncompressed = new byte[uncompressedSize]; byte[] compressed = binaryReader.ReadBytes(toReaded); uncompressed = DeflatorWraper.Decompress(compressed); _bitmapColorData = null; if (_bitmapFormat == 4) { Pix15[] bitmapPixelData = new Pix15[imageSize]; for (int i = 0, j = 0; i < imageSize; i++, j += 2) { byte[] data = new byte[2] { uncompressed[j], uncompressed[j + 1] }; bitmapPixelData[i] = new Pix15(data); } _bitmapColorData = new BitmapColorData(bitmapPixelData); } else { Pix24[] bitmapPixelData = new Pix24[imageSize]; for (int i = 0, j = 0; i < imageSize; i++, j += 4) { byte reserved = uncompressed[j]; byte red = uncompressed[j + 1]; byte green = uncompressed[j + 2]; byte blue = uncompressed[j + 3]; bitmapPixelData[i] = new Pix24(red, green, blue); } _bitmapColorData = new BitmapColorData(bitmapPixelData); } } }