Esempio n. 1
0
        public void ToSwf(SwfWriter w)
        {
            uint start = (uint)w.Position;

            w.AppendTagIDAndLength(this.TagType, 0, true);

            w.AppendUI16(CharacterId);
            w.AppendByte((byte)BitmapFormat);
            w.AppendUI16(Width);
            w.AppendUI16(Height);

            if (BitmapFormat == BitmapFormat.Colormapped8Bit) // 8-bit colormapped image
            {
                w.AppendByte((byte)(ColorCount - 1));

                uint colorBytes   = HasAlpha ? (uint)4 : (uint)3;
                uint padWidth     = this.Width + (4 - (this.Width % 4));
                uint unzippedSize = (this.ColorCount * colorBytes) + (padWidth * this.Height);

                byte[] mapData = new byte[unzippedSize];

                for (int i = 0; i < this.ColorCount; i++)
                {
                    mapData[i * colorBytes + 0] = ColorTable[i].R;
                    mapData[i * colorBytes + 1] = ColorTable[i].G;
                    mapData[i * colorBytes + 2] = ColorTable[i].B;
                    if (HasAlpha)
                    {
                        mapData[i * colorBytes + 3] = ColorTable[i].A;
                    }
                }

                int index = 0;
                int st    = (int)(this.ColorCount * colorBytes);
                for (int i = st; i < unzippedSize; i++)
                {
                    if (((i - st) % padWidth) < this.Width)// exclude padding
                    {
                        mapData[i] = (byte)this.ColorData[index++];
                    }
                    else
                    {
                        mapData[i] = 0;
                    }
                }
                byte[] zipped = SwfWriter.ZipBytes(mapData);
                if (OrgBitmapData != null)
                {
                    w.AppendBytes(OrgBitmapData);
                }
                else
                {
                    w.AppendBytes(zipped);
                }
            }

            else if (BitmapFormat == BitmapFormat.RGB15Bit) // rbg 15
            {
                // todo: find a test file for rgb555
                uint colorBytes = 2;
                uint padWidth   = this.Width * colorBytes;
                padWidth += (4 - padWidth) % 4;
                uint unzippedSize = (padWidth * this.Height) * colorBytes;

                byte[] mapData = new byte[unzippedSize];

                int  index     = 0;
                uint byteCount = padWidth * this.Height;
                for (uint i = 0; i < unzippedSize; i += colorBytes)
                {
                    byte rd = this.BitmapData[index].R;
                    byte gr = this.BitmapData[index].G;
                    byte bl = this.BitmapData[index].B;

                    byte b0 = 0;
                    byte b1 = 0;
                    b0 |= (byte)((rd & 0x7C) << 1);
                    b0 |= (byte)((gr & 0x03) << 6);
                    b1 |= (byte)((gr & 0xE0) >> 2);
                    b1 |= (byte)((bl & 0x1F) << 3);

                    mapData[i + 0] = b0;
                    mapData[i + 1] = b1;

                    index++;
                }
                byte[] zipped = SwfWriter.ZipBytes(mapData);
                if (OrgBitmapData != null)
                {
                    w.AppendBytes(OrgBitmapData);
                }
                else
                {
                    w.AppendBytes(zipped);
                }
            }

            else if (BitmapFormat == BitmapFormat.RGB24Bit) // RGB 24
            {
                uint colorBytes   = HasAlpha ? (uint)4 : (uint)3;
                uint unzippedSize = (this.Width * this.Height) * colorBytes;

                byte[] mapData = new byte[unzippedSize];

                int index = 0;
                for (uint i = 0; i < unzippedSize; i += colorBytes)
                {
                    mapData[i + 0] = this.BitmapData[index].A;
                    mapData[i + 1] = this.BitmapData[index].R;
                    mapData[i + 2] = this.BitmapData[index].G;
                    if (HasAlpha)
                    {
                        mapData[i + 3] = this.BitmapData[index].B;
                    }
                    index++;
                }
                byte[] zipped = SwfWriter.ZipBytes(mapData);
                if (OrgBitmapData != null)
                {
                    w.AppendBytes(OrgBitmapData);
                }
                else
                {
                    w.AppendBytes(zipped);
                }
            }


            w.ResetLongTagLength(this.TagType, start, true);
        }