コード例 #1
0
        public static ExtendedBitmap GetBitmap(StciData RGBData)
        {
            byte[] argbFile = convert16ARGBTo32ARGB(RGBData);

            List <byte> bmpFile = new List <byte>();

            bmpFile.AddRange(BMP.CreateBmpHeader
                             (
                                 (UInt32)argbFile.Length,
                                 (UInt32)RGBData.Width,
                                 (UInt32)RGBData.Height,
                                 (UInt16)32,
                                 (UInt32)0
                             ));
            bmpFile.AddRange(argbFile);

            using (MemoryStream argbStream = new MemoryStream(bmpFile.ToArray()))
            {
                Bitmap bm;
                try
                {
                    bm = new Bitmap(argbStream);
                }
                catch
                {
                    bm = new Bitmap(RGBData.Width, RGBData.Height);
                }
                bm.RotateFlip(RotateFlipType.Rotate180FlipX);
                return(new ExtendedBitmap(bm, RGBData._RGB));                //, (UInt16)RGBData.Height, (UInt16)RGBData.Width);
            }
        }
コード例 #2
0
ファイル: BMP.cs プロジェクト: egorovav/Ja2Project
        static ColorPalette getPalette(byte[] palette)
        {
            List <byte> paletteFile = new List <byte>();

            paletteFile.AddRange(BMP.CreateBmpHeader
                                 (
                                     (UInt32)4,
                                     (UInt32)4,
                                     (UInt32)1,
                                     (UInt16)8,
                                     (UInt32)palette.Length
                                 ));
            paletteFile.AddRange(palette);
            paletteFile.AddRange(new byte[4]);
            Bitmap bmPalette;

            using (MemoryStream argbStream = new MemoryStream(paletteFile.ToArray()))
                try
                {
                    bmPalette = new Bitmap(argbStream);
                }
                catch { return(null); }

            return(bmPalette.Palette);
        }
コード例 #3
0
ファイル: BMP.cs プロジェクト: egorovav/Ja2Project
        public static ColorPalette MakeTransparentPalette(ColorPalette palette, Color bC)
        {
            List <byte> paletteFile = new List <byte>();

            paletteFile.AddRange(BMP.CreateBmpHeader
                                 (
                                     (UInt32)4,
                                     (UInt32)4,
                                     (UInt32)1,
                                     (UInt16)8,
                                     (UInt32)(palette.Entries.Length * 4)
                                 ));
            paletteFile.AddRange(new byte[] { bC.B, bC.G, bC.R, bC.A });
            for (int i = 1; i < palette.Entries.Length; i++)
            {
                Color c = palette.Entries[i];
                paletteFile.AddRange(new byte[] { c.B, c.G, c.R, c.A });
            }
            paletteFile.AddRange(new byte[4]);
            Bitmap bmPalette;

            using (MemoryStream argbStream = new MemoryStream(paletteFile.ToArray()))
                try
                {
                    bmPalette = new Bitmap(argbStream);
                }
                catch { return(null); }

            return(bmPalette.Palette);
        }
コード例 #4
0
ファイル: BMP.cs プロジェクト: egorovav/Ja2Project
 public static byte[] CreateBmpHeader(
     Int32 imageLength, Int32 width, Int32 height, Int32 bitPerPixel, Int32 paletteLength)
 {
     return(BMP.CreateBmpHeader(
                (UInt32)imageLength, (UInt32)width, (UInt32)height, (UInt16)bitPerPixel, (UInt32)paletteLength));
 }
コード例 #5
0
ファイル: BMP.cs プロジェクト: egorovav/Ja2Project
        public static Bitmap Convert32argbToIndexed(Bitmap argbFile, ColorPalette palette)
        {
            // if palette = null, create palette.
            // if file contains more than 256 different colors throw excepiton
            int _headerSize    = 54;
            int _colorNum      = 256;
            int _paletteSize   = 256 * 4;
            int _width         = argbFile.Width;
            int _height        = argbFile.Height;
            int _imageDataSize = _width * _height;

            byte[] _indexedBmData = new byte[_headerSize + _paletteSize + _imageDataSize];
            bool   _isNewPalette  = false;

            if (palette == null)
            {
                byte[] _paletteData = new byte[_paletteSize];
                palette       = BMP.getPalette(_paletteData);
                _isNewPalette = true;
            }

            int _paletteIndex = 0;

            byte[] _argbBmData = new byte[_headerSize + _imageDataSize * 4];

            Bitmap _indexedBm = null;

            using (MemoryStream _argbStream = new MemoryStream(_argbBmData))
            {
                argbFile.Save(_argbStream, ImageFormat.Bmp);
                using (MemoryStream _indexedStream = new MemoryStream(_indexedBmData))
                {
                    byte[] _header = BMP.CreateBmpHeader(
                        _imageDataSize, argbFile.Width, argbFile.Height, 8, _colorNum);
                    _indexedStream.Write(_header, 0, _headerSize);

                    _indexedStream.Seek(_paletteSize, SeekOrigin.Current);
                    _argbStream.Seek(_headerSize, SeekOrigin.Begin);

                    for (int i = 0; i < _height; i++)
                    {
                        for (int j = 0; j < _width; j++)
                        {
                            // argb data is reversed
                            // TODO GetPixel works very slowly. Use stream instead.
                            //Color _c1 = argbFile.GetPixel(_width - j - 1, _height - i - 1);
                            int   _b = _argbStream.ReadByte();
                            int   _g = _argbStream.ReadByte();
                            int   _r = _argbStream.ReadByte();
                            int   _a = _argbStream.ReadByte();
                            Color _c = Color.FromArgb(_a, _r, _g, _b);

                            int _ci = Array.IndexOf(palette.Entries, _c);
                            if (_ci >= 0)
                            {
                                _indexedStream.WriteByte((byte)_ci);
                            }
                            else
                            {
                                if (_isNewPalette)
                                {
                                    if (_paletteIndex < _colorNum)
                                    {
                                        palette.Entries[_paletteIndex] = _c;
                                        _paletteIndex++;
                                    }
                                    else
                                    {
                                        string _msg = String.Format(
                                            "There are more than {0} colors in input file.", _colorNum);
                                        throw new ArgumentException(_msg, "argbFile");
                                    }
                                }
                                else
                                {
                                    string _msg = String.Format(
                                        "Color R={0} G={1} B={2} is not found in palette.", _c.R, _c.G, _c.B);
                                    throw new ArgumentException(_msg, "palette");
                                }
                            }
                        }
                    }

                    _indexedBm         = new Bitmap(_indexedStream);
                    _indexedBm.Palette = palette;
                }
            }

            return(_indexedBm);
        }
コード例 #6
0
        static List <ExtendedBitmap> ConvertEtrleDataToBitmaps
            (UInt16 startIndex, UInt16 length, ETRLEData data)
        {
            byte[] palette = data.palette;
            List <ExtendedBitmap> bitmaps = new List <ExtendedBitmap>();

            for (int objIndex = startIndex; objIndex < startIndex + length; objIndex++)
            {
                if (objIndex >= data.ETRLEObjects.Length)
                {
                    return(bitmaps);
                }

                ETRLEObject obj = data.ETRLEObjects[objIndex];

                byte[] file = new byte[obj.uiDataLength];
                Array.Copy(data.imageData, obj.uiDataOffset, file, 0, obj.uiDataLength);
                Int32 remainder = (4 - obj.usWidth % 4) % 4;

                List <byte> argbFile = new List <byte>();

                using (BinaryReader br = new BinaryReader(new MemoryStream(file)))
                {
                    while (br.BaseStream.Position < br.BaseStream.Length)
                    {
                        Int32 firstSectionByte = br.ReadByte();
                        if (firstSectionByte == 0)
                        {
                            for (int i = 0; i < remainder; i++)
                            {
                                argbFile.Add(0);
                            }
                        }
                        else if (firstSectionByte > 127)
                        {
                            for (int j = 0; j < firstSectionByte - 128; j++)
                            {
                                argbFile.Add(0);
                            }
                        }
                        else
                        {
                            for (int j = 0; j < firstSectionByte; j++)
                            {
                                byte index = br.ReadByte();
                                argbFile.Add(index);
                            }
                        }
                    }
                }
                List <byte> bmpFile = new List <byte>();
                bmpFile.AddRange(BMP.CreateBmpHeader
                                 (
                                     (UInt32)argbFile.Count,
                                     (UInt32)(obj.usWidth),
                                     (UInt32)obj.usHeight,
                                     (UInt16)8,
                                     (UInt32)palette.Length
                                 ));
                bmpFile.AddRange(palette);
                bmpFile.AddRange(argbFile);

                byte[] bFile = bmpFile.ToArray();
                using (MemoryStream argbStream = new MemoryStream(bFile))
                {
                    Bitmap bm;
                    try
                    {
                        bm = new Bitmap(argbStream);
                    }
                    catch
                    {
                        bm = new Bitmap(obj.usWidth, obj.usHeight);
                    }
                    bm.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    ExtendedBitmap exBm = new ExtendedBitmap(bm, obj.sOffsetX, obj.sOffsetY);
                    if (data.pAppData != null)
                    {
                        exBm.ApplicationData = new byte[16];
                        Array.Copy(data.pAppData, objIndex * 16, exBm.ApplicationData, 0, 16);
                    }
                    bitmaps.Add(exBm);
                }
            }
            return(bitmaps);
        }