Beispiel #1
0
        public unsafe static Bitmap QuantizeImage(Bitmap Image, int Colors)
        {
            byte[] BitmapArray = MakeBitmapArray(Image);

            var attr = Liq.liq_attr_create();

            Liq.liq_set_max_colors(attr, Colors);
            Liq.liq_set_min_posterization(attr, 2);

            var LiqImage     = Liq.liq_image_create_rgba(attr, BitmapArray, Image.Width, Image.Height, 0);
            var LiqQuantized = Liq.liq_quantize_image(attr, LiqImage);

            byte[] Texture = new byte[Image.Width * Image.Height];
            Liq.liq_write_remapped_image(LiqQuantized, LiqImage, Texture, (UIntPtr)(Image.Width * Image.Height));
            liq_palette LiqPalette = (liq_palette)Marshal.PtrToStructure(Liq.liq_get_palette(LiqQuantized), typeof(liq_palette));

            Bitmap BMP;

            fixed(byte *p = Texture)
            {
                IntPtr ptr = (IntPtr)p;

                BMP = new Bitmap(Image.Width, Image.Height, Image.Width, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, ptr);
            }

            System.Drawing.Imaging.ColorPalette pal = BMP.Palette;

            for (int i = 0; i < BMP.Palette.Entries.Count(); i++)
            {
                pal.Entries[i] = Color.FromArgb(LiqPalette.Entries[i].A, LiqPalette.Entries[i].R, LiqPalette.Entries[i].G, LiqPalette.Entries[i].B);
            }

            BMP.Palette = pal;
            return(BMP);
        }
Beispiel #2
0
                public override Image ReadImageData(IPnmDataReader dr, int width, int height)
                {
                    Image image;

                    try
                    {
                        Bitmap bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                        System.Drawing.Imaging.BitmapData   bitmapdata = bitmap.LockBits(new Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                        System.Drawing.Imaging.ColorPalette palette    = bitmap.Palette;
                        for (int i = 0; i < palette.Entries.Length; i++)
                        {
                            palette.Entries[i] = Color.FromArgb(i, i, i);
                        }
                        bitmap.Palette = palette;
                        byte[] data = new byte[height * bitmapdata.Stride];
                        for (int j = 0; j < height; j++)
                        {
                            dr.Read(data, j * bitmapdata.Stride, width);
                        }
                        System.Runtime.InteropServices.Marshal.Copy(data, 0, bitmapdata.Scan0, data.Length);
                        bitmap.UnlockBits(bitmapdata);
                        image = bitmap;
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        dr.Close();
                    }
                    return(image);
                }
Beispiel #3
0
                public override Image ReadImageData(IPnmDataReader dr, int width, int height)
                {
                    Image image;

                    try
                    {
                        int    count  = (dr is ASCIIDataReader) ? width : ((int)Math.Ceiling((double)(((double)width) / 8.0)));
                        Bitmap bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                        System.Drawing.Imaging.BitmapData   bitmapdata = bitmap.LockBits(new Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                        System.Drawing.Imaging.ColorPalette palette    = bitmap.Palette;
                        palette.Entries[0] = Color.Black;
                        palette.Entries[1] = Color.White;
                        bitmap.Palette     = palette;
                        byte[] data = new byte[height * bitmapdata.Stride];
                        for (int i = 0; i < height; i++)
                        {
                            dr.Read(data, i * bitmapdata.Stride, count);
                        }
                        System.Runtime.InteropServices.Marshal.Copy(data, 0, bitmapdata.Scan0, data.Length);
                        bitmap.UnlockBits(bitmapdata);
                        image = bitmap;
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        dr.Close();
                    }
                    return(image);
                }
Beispiel #4
0
        static void Main(string[] args)
        {
            if (!System.IO.File.Exists("Palette.dat"))
            {
                Console.WriteLine("Palette.dat could not be found. Place Palette.dat in the same folder as OldTibiaExtractor and try again.");
                Console.ReadKey();
                return;
            }

            Bitmap bmp = new Bitmap(32, 32, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);

            colorPalette = bmp.Palette;
            SetPalette("Palette.dat");

            foreach (string filename in System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)))
            {
                if (filename.Contains(".PIC"))
                {
                    ExtractPIC(filename);
                }

                if (filename.Contains(".SPR"))
                {
                    ExtractSPR(filename);
                }
            }

            bmp.Dispose();
        }
        /// <summary>
        /// Convert BitmapImage to Bitmap
        /// </summary>
        /// <param name="input">BitmapImage which be converted</param>
        /// <returns>Bitmap</returns>
        public static System.Drawing.Bitmap Bitmapimage2Bitmap(System.Windows.Media.Imaging.BitmapImage input)
        {
            if (input == null)
            {
                return(null);
            }

            using (System.IO.MemoryStream outmstream = new System.IO.MemoryStream())
            {
                System.Windows.Media.Imaging.BitmapEncoder enc = new System.Windows.Media.Imaging.BmpBitmapEncoder();
                enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(input));
                enc.Save(outmstream);
                System.Drawing.Bitmap retBitmap = new System.Drawing.Bitmap(outmstream);

                if (retBitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
                {
                    System.Drawing.Imaging.ColorPalette new_pal = retBitmap.Palette;
                    for (int i = 0; i < 256; i++)
                    {
                        new_pal.Entries[i] = System.Drawing.Color.FromArgb(255, i, i, i);
                    }
                    retBitmap.Palette = new_pal;
                }

                return(retBitmap);
            }
        }
Beispiel #6
0
 private void Grayscale(System.Drawing.Bitmap bmp)
 {
     System.Drawing.Imaging.ColorPalette pal = bmp.Palette;
     for (int i = 0; i < 256; i++)
         pal.Entries[i] = System.Drawing.Color.FromArgb(255, i, i, i);
     bmp.Palette = pal;
 }
        public unsafe static Bitmap GetChannel(Bitmap SrcImg, Channel channel)
        {
            int    X, Y, Width, Height, StrideS, StrideD;
            byte * PointerS, PointerD;
            Bitmap DestImg = new Bitmap(SrcImg.Width, SrcImg.Height, PixelFormat.Format8bppIndexed);

            Width = SrcImg.Width; Height = SrcImg.Height;
            ColorPalette Pal = DestImg.Palette;

            for (Y = 0; Y < Pal.Entries.Length; Y++)
            {
                Pal.Entries[Y] = Color.FromArgb(255, Y, Y, Y);
            }
            DestImg.Palette = Pal;
            BitmapData SrcData  = SrcImg.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            BitmapData DestData = DestImg.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            StrideS = SrcData.Stride; StrideD = DestData.Stride;
            for (Y = 0; Y < Height; Y++)
            {
                PointerS = (byte *)SrcData.Scan0 + Y * StrideS;
                PointerD = (byte *)DestData.Scan0 + Y * StrideD;
                for (X = 0; X < Width; X++)
                {
                    *PointerD = *(PointerS + (int)channel);
                    PointerS += 3;
                    PointerD++;
                }
            }
            SrcImg.UnlockBits(SrcData);
            DestImg.UnlockBits(DestData);
            return(DestImg);
        }
Beispiel #8
0
        public BmpImage(string filename)
        {
            BinaryReader br = new BinaryReader(new FileStream(filename, FileMode.Open));

            // Read header

            br.ReadUInt16();
            br.ReadUInt32();
            br.ReadUInt16();
            br.ReadUInt16();
            br.ReadUInt32();

            // Read infoheader

            br.ReadUInt32();
            this.Create(br.ReadInt32(), br.ReadInt32());
            br.ReadUInt16();
            br.ReadUInt16();
            br.ReadUInt32();
            br.ReadUInt32();
            br.ReadInt32();
            br.ReadInt32();
            br.ReadUInt32();
            br.ReadUInt32();

            // Read palette

            System.Drawing.Imaging.ColorPalette palette = this.Bitmap.Palette;

            for (int i = 0; i < palette.Entries.Length; i++)
            {
                byte b = br.ReadByte();
                byte g = br.ReadByte();
                byte r = br.ReadByte();
                br.ReadByte();

                palette.Entries[i] = System.Drawing.Color.FromArgb(r, g, b);
            }

            this.Bitmap.Palette = palette;

            // Read data

            this.OpenData(System.Drawing.Imaging.ImageLockMode.WriteOnly, true);

            for (int y = 0; y < this.Height; y++)
            {
                this.DecLine();

                for (int x = 0; x < this.Width; x++)
                {
                    this.SetPixel(x, br.ReadByte());
                }
            }

            this.CloseData();

            br.Close();
        }
Beispiel #9
0
        public static byte[] Encode(System.Drawing.Bitmap bmp, ref byte[] palbin, byte csa = 0)
        {
            int pixelSize = bmp.Width * bmp.Height;

            System.Diagnostics.Debug.WriteLine("TexUt2.Decode: bw = " + (bmp.Width / 128) + "; BH = " + (bmp.Height / 64) + "; type = " + (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed ? "19" : "20") + "; size = " + pixelSize + "; width = " + bmp.Width + "; height = " + bmp.Height);
            switch (bmp.PixelFormat)
            {
            case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                csa = 0;
                break;

            case System.Drawing.Imaging.PixelFormat.Format4bppIndexed:
                pixelSize /= 2;
                csa       *= 64;
                break;

            default: throw new NotSupportedException("Unsupported type");
            }
            //Palette
            {
                byte[] destinationArray = new byte[1024];
                for (int i = 0; i < 256; i++)
                {
                    Buffer.BlockCopy(palbin, 4 * i, destinationArray, 4 * (i + tbl[i & 0x7F]), 4);
                }
                System.Drawing.Imaging.ColorPalette palette = bmp.Palette;
                for (int i = 0; i < palette.Entries.Length; i++)
                {
                    int  c    = csa + (i * 4);
                    uint argb = (uint)palette.Entries[i].ToArgb();
                    destinationArray[c + 3] = (byte)Math.Ceiling((double)(argb >> 24) / 2);
                    destinationArray[c]     = (byte)(argb >> 16);
                    destinationArray[c + 1] = (byte)(argb >> 8);
                    destinationArray[c + 2] = (byte)argb;
                }
                for (int i = 0; i < 256; i++)
                {
                    Buffer.BlockCopy(destinationArray, 4 * (i + tbl[i & 0x7F]), palbin, 4 * i, 4);
                }
            }
            //Pixels
            byte[] bin = new byte[pixelSize];
            {
                System.Drawing.Imaging.BitmapData bitmapdata = bmp.LockBits(System.Drawing.Rectangle.FromLTRB(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
                try { System.Runtime.InteropServices.Marshal.Copy(bitmapdata.Scan0, bin, 0, pixelSize); }
                finally { bmp.UnlockBits(bitmapdata); }
            }
            switch (bmp.PixelFormat)
            {
            case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                bin = Reform.Encode8(bin, bmp.Width / 128, bmp.Height / 64);
                break;

            case System.Drawing.Imaging.PixelFormat.Format4bppIndexed:
                bin = Reform.Encode4(bin, bmp.Width / 128, bmp.Height / 128);
                break;
            }
            return(bin);
        }
Beispiel #10
0
        public void Create(int width, int height, System.Drawing.Imaging.ColorPalette palette)
        {
            this.bitmap         = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
            this.bitmap.Palette = palette;

            this.width  = this.bitmap.Width;
            this.height = this.bitmap.Height;
        }
Beispiel #11
0
 public static void Apply(System.Drawing.Image image, Color[] lut)
 {
     System.Drawing.Imaging.ColorPalette palette = image.Palette;
     for (int i = 0; i < palette.Entries.Length; i++)
     {
         palette.Entries[i] = System.Drawing.Color.FromArgb(lut[i].A, lut[i].R, lut[i].G, lut[i].B);
     }
     image.Palette = palette;
 }
Beispiel #12
0
 public void Apply(Bitmap bmp)
 {
     System.Drawing.Imaging.ColorPalette pal = bmp.Palette;
     for (int i = 0; i < 256; i++)
     {
         pal.Entries[i] = GetColor((byte)i);
     }
     bmp.Palette = pal;
 }
Beispiel #13
0
        public static System.Drawing.Bitmap Decode(byte[] picbin, byte[] palbin, uint type, ushort width, ushort height, byte csa = 0)
        {
            System.Drawing.Imaging.PixelFormat pf;
            System.Diagnostics.Debug.WriteLine("TexUt2.Decode: bw = " + (width / 128) + "; BH = " + (height / 64) + "; type = " + type + "; size = " + picbin.Length + "; width = " + width + "; height = " + height);
            switch (type)
            {
            case 19:
                pf     = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
                picbin = Reform.Decode8(picbin, width / 128, height / 64);
                break;

            case 20:
                pf     = System.Drawing.Imaging.PixelFormat.Format4bppIndexed;
                picbin = Reform.Decode4(picbin, width / 128, height / 128);
                break;

            default: throw new NotSupportedException("Unsupported image type.");
            }
            System.Drawing.Bitmap pic = new System.Drawing.Bitmap(width, height, pf);
            {
                System.Drawing.Imaging.BitmapData bitmapdata = pic.LockBits(System.Drawing.Rectangle.FromLTRB(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.WriteOnly, pf);
                try { System.Runtime.InteropServices.Marshal.Copy(picbin, 0, bitmapdata.Scan0, Math.Min(picbin.Length, bitmapdata.Stride * height)); }
                finally { pic.UnlockBits(bitmapdata); }
            }
            /*The palette is swizzled, or something...*/
            {
                byte[] destinationArray = new byte[1024];
                for (int i = 0; i < 256; i++)
                {
                    Buffer.BlockCopy(palbin, 4 * i, destinationArray, 4 * (i + tbl[i & 0x7F]), 4);
                }
                Buffer.BlockCopy(destinationArray, 0, palbin, 0, 1024);
            }
            type = 0;
            System.Drawing.Imaging.ColorPalette palette = pic.Palette;
            switch (pf)
            {
            case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                type = 256;
                csa  = 0;
                break;

            case System.Drawing.Imaging.PixelFormat.Format4bppIndexed:
                type = 16;
                csa *= 64;
                break;
            }
            for (int i = 0; i < type; i++)
            {
                int c = csa + (i * 4);
                //Because of rounding, when reading values back use Math.Ceiling(input/2)
                palette.Entries[i] = System.Drawing.Color.FromArgb((int)Math.Min(palbin[c + 3] * 2, 255), palbin[c], palbin[c + 1], palbin[c + 2]);
            }
            pic.Palette = palette;
            return(pic);
        }
Beispiel #14
0
        private void btnProcessing_Click(object sender, EventArgs e)
        {
            try {
                unsafe {
                    // ** pinvoke
                    ImgInfomation input = new ImgInfomation();
                    input.width    = SrcImg.Width;
                    input.height   = SrcImg.Height;
                    input.channels = 3;
                    System.Drawing.Imaging.BitmapData imgData = SrcImg.LockBits(new Rectangle(0, 0, SrcImg.Width, SrcImg.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    input.src  = (byte *)imgData.Scan0;
                    input.step = imgData.Stride;

                    ImgInfomation output;
                    output = DllSetting.Convert2Gray(input);
                    System.Drawing.Bitmap ResultImg = new System.Drawing.Bitmap(output.width, output.height, output.step, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, (System.IntPtr)output.src);
                    System.Drawing.Imaging.ColorPalette GreyColorPalette = null;
                    GreyColorPalette = ResultImg.Palette;
                    for (int Index = 0; Index <= byte.MaxValue; Index++)
                    {
                        GreyColorPalette.Entries[Index] = Color.FromArgb(byte.MaxValue, Index, Index, Index);
                    }
                    ResultImg.Palette = GreyColorPalette;

                    // managed class by cli
                    TestCSUsingCppDll.MangementDll_proc test   = new TestCSUsingCppDll.MangementDll_proc();
                    TestCSUsingCppDll.ImgInfomation2    input2 = new TestCSUsingCppDll.ImgInfomation2();
                    input2.width    = SrcImg.Width;
                    input2.height   = SrcImg.Height;
                    input2.channels = 3;
                    input2.src      = (byte *)imgData.Scan0;
                    input2.step     = imgData.Stride;
                    TestCSUsingCppDll.ImgInfomation2 result;
                    result = test.ConvertRgb2gray(input2);

                    System.Drawing.Bitmap ResultImg2 = new System.Drawing.Bitmap(result.width, result.height, result.step, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, (System.IntPtr)result.src);
                    System.Drawing.Imaging.ColorPalette GreyColorPalette2 = null;
                    GreyColorPalette2 = ResultImg2.Palette;
                    for (int Index = 0; Index <= byte.MaxValue; Index++)
                    {
                        GreyColorPalette2.Entries[Index] = Color.FromArgb(byte.MaxValue, Index, Index, Index);
                    }
                    ResultImg2.Palette = GreyColorPalette2;

                    //Console.WriteLine(test.channels.ToString());

                    SrcImg.UnlockBits(imgData);
                    pctbxRecievePInvokeImg.Image     = ResultImg;
                    pctbxRecieveManageClassImg.Image = ResultImg2;
                }
            }
            catch (System.Exception err)
            {
                MessageBox.Show(err.ToString(), "ERROR!");
            }
        }
Beispiel #15
0
            internal static void Refresh(IconImgData imageData, IconImgType version, string directory)
            {
                Debug.Assert(version != IconImgType.Invalid);
                DebugLogger.Log("Hardcoded", "Refreshing IconImages of version {0}", version);
                int         stride       = imageData.ByteStride;
                IconImgFile iconFileInfo = imageData.Versions[(int)version];
                string      iconImgFile  = Path.Combine(directory, version.ToString() + "Icon.dat");

                byte[]         fileData     = File.ReadAllBytes(iconImgFile);
                IconImgEntry[] imageEntries = iconFileInfo.Entries;
                int            id           = 0;

                images = new List <IconImgEntry>();
                foreach (IconImgEntry entry in imageEntries)
                {
                    Size   imageSize = entry.ImageSize;
                    Bitmap bm        = new Bitmap(imageSize.Width, imageSize.Height, System.Drawing.Imaging.PixelFormat.Format4bppIndexed);
                    System.Drawing.Imaging.ColorPalette imgPalette = bm.Palette;
                    Color[] entries = imgPalette.Entries;
                    for (int i = 0, colourOffset = entry.PaletteLocation; i < 16; ++i, colourOffset += sizeof(ushort))
                    {
                        ushort colour = BitConverter.ToUInt16(fileData, colourOffset);
                        entries[i] = ((colour & 0x7fff) == 0) ? Color.Transparent : MakeColorFromBGR555(colour);
                    }
                    bm.Palette = imgPalette;
                    System.Drawing.Imaging.BitmapData bmData = bm.LockBits(new Rectangle(Point.Empty, bm.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format4bppIndexed);
                    // These are 4bpp, so the 1 X byte = 2 X pixels, so we half it
                    int    lineBytes       = imageSize.Width / 2;
                    int    height          = imageSize.Height;
                    int    lockDataStride  = bmData.Stride;
                    byte[] imageBuffer     = new byte[lockDataStride * height];
                    int    fileIter        = (entry.ImageLocation.Y * stride) + (entry.ImageLocation.X / 2);
                    int    imageBufferIter = 0;
                    // the image buffer in the file is Big endian, like this (pixel numbers within each byte)
                    // [2, 1], [4, 3], [6, 5]
                    // we need it in little endian so this copies the data while swizzling it into
                    // [1, 2], [3, 4], [5, 6]
                    //
                    for (int y = 0; y < height; ++y, fileIter += stride, imageBufferIter += lockDataStride)
                    {
                        Buffer.BlockCopy(fileData, fileIter, imageBuffer, imageBufferIter, lineBytes);
                        for (int x = 0; x < lineBytes; ++x)
                        {
                            byte pixels        = imageBuffer[imageBufferIter + x];
                            byte swizzedPixels = (byte)(((pixels << 4) & 0xF0) | (pixels >> 4));
                            imageBuffer[imageBufferIter + x] = swizzedPixels;
                        }
                    }
                    // blit it to the real bitmap data
                    System.Runtime.InteropServices.Marshal.Copy(imageBuffer, 0, bmData.Scan0, imageBuffer.Length);
                    bm.UnlockBits(bmData);
                    entry.Image = bm;
                    entry.Id    = id++;
                    images.Add(entry);
                }
            }
Beispiel #16
0
        /// <summary>
        /// Converts an old GDI Palette to the new WPF BitmapPalette
        /// </summary>
        /// <param name="GDIPalette">Old palette to convert.</param>
        /// <returns>New WPF Palette.</returns>
        public static BitmapPalette ConvertGDIPaletteToWPF(System.Drawing.Imaging.ColorPalette GDIPalette)
        {
            List <Color> Colours = new List <Color>();

            foreach (var colour in GDIPalette.Entries)
            {
                Colours.Add(Color.FromArgb(colour.A, colour.R, colour.G, colour.B));
            }

            return(Colours.Count > 0 ? new BitmapPalette(Colours) : BitmapPalettes.Halftone256Transparent);
        }
 public ImageUnpacker(Bitmap image, string fileName)
 {
     if (image.Palette.Entries.Length > 0)
     {
         this.pallette = image.Palette;
     }
     this.original = new Bitmap((Bitmap)image.Clone());
     this.originalSize = image.Size;
     this.boxes = new List<Rectangle>();
     this.FileName = fileName;
     this.IsLarge = (this.original.Width * this.original.Height) > (800 * 800);
 }
Beispiel #18
0
        private static System.Drawing.Imaging.ColorPalette Set_8bppIndexed_Palette(System.Drawing.Imaging.ColorPalette inputPalette)
        {
            System.Drawing.Imaging.ColorPalette pal = inputPalette;
            if (inputPalette.Entries.Length < 1)
            {
                for (int i = 0; i < 256; i++)
                {
                    pal.Entries[i] = System.Drawing.Color.FromArgb(255, i, i, i);
                }
            }

            return(pal);
        }
Beispiel #19
0
        //Function: display the output image in the interface
        public static Bitmap show_image()
        {
            {
                int i, j;

                for (i = 0; i < global.IMG_ROWS; i++)
                {
                    for (j = 0; j < global.IMG_COLS; j++)
                    {
                        global.pic[j, i] = Convert.ToByte(global.out_image[j, i]);
                    }
                }

                byte[] pixels = new byte[global.pic.GetLength(0) * global.pic.GetLength(1)];

                for (int y = 0; y < global.pic.GetLength(1); y++)
                {
                    for (int x = 0; x < global.pic.GetLength(0); x++)
                    {
                        pixels[y * global.pic.GetLength(0) + x] = global.pic[x, y];
                    }
                }

                //create a new Bitmap
                Bitmap bmp = new Bitmap(global.pic.GetLength(0), global.pic.GetLength(1), System.Drawing.Imaging.PixelFormat.Format8bppIndexed);

                System.Drawing.Imaging.ColorPalette pal = bmp.Palette;

                //create grayscale palette
                for (int g = 0; g < 256; g++)
                {
                    pal.Entries[g] = Color.FromArgb((int)255, g, g, g);
                }

                //assign to bmp
                bmp.Palette = pal;

                //lock it to get the BitmapData Object
                System.Drawing.Imaging.BitmapData bmData =
                    bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);

                //copy the bytes
                System.Runtime.InteropServices.Marshal.Copy(pixels, 0, bmData.Scan0, bmData.Stride * bmData.Height);

                //never forget to unlock the bitmap
                bmp.UnlockBits(bmData);

                //display
                return(bmp);
            }
        }
        internal static void SetPalette(this System.Drawing.Bitmap bitmap, Palette palette)
        {
            Debug.Assert(palette.Entries != null);
            Debug.Assert(palette.Entries.Length > 0);
            Debug.Assert(palette.Entries.Length == palette.Length);

            System.Drawing.Imaging.ColorPalette currentPalette = bitmap.Palette;
            for (int i = 0; i < currentPalette.Entries.Length; ++i)
            {
                Color color = palette[i];
                currentPalette.Entries[i] = System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);
            }
            bitmap.Palette = currentPalette;
        }
Beispiel #21
0
        public void ApplyLevelPalette(System.Drawing.Bitmap image)
        {
            System.Drawing.Imaging.ColorPalette bitmapPalette = image.Palette; // Get brush paletteIndex

            NesPalette bgPalette     = Level.BgPalette;                        // Get ROM palettes
            NesPalette spritePalette = Level.SpritePalette;

            bgPalette.ApplyTable(bitmapPalette.Entries); // Apply to brush paletteIndex
            bgPalette.ApplyTable(bitmapPalette.Entries, 16);
            spritePalette.ApplyTable(bitmapPalette.Entries, 32);
            spritePalette.ApplyTable(bitmapPalette.Entries, 48);

            image.Palette = bitmapPalette; // Apply back to brush paletteIndex
        }
Beispiel #22
0
        public IAPImage(string filename)
        {
            BinaryReader br = new BinaryReader(new FileStream(filename, FileMode.Open));

            // Read format

            this.Create(br.ReadInt32(), br.ReadInt32());

            // Read palette

            System.Drawing.Imaging.ColorPalette palette = this.Bitmap.Palette;

            for (int i = 0; i < palette.Entries.Length; i++)
            {
                palette.Entries[i] = System.Drawing.Color.FromArgb(br.ReadByte(), br.ReadByte(), br.ReadByte());
            }

            this.Bitmap.Palette = palette;

            // Read data

            byte[] bytes = new byte[br.ReadInt32()];

            for (int i = 0; i < bytes.Length; i++)
            {
                bytes[i] = br.ReadByte();
            }

            bytes = Decompress(bytes);

            this.OpenData(System.Drawing.Imaging.ImageLockMode.ReadOnly, false);

            int index = 0;

            for (int y = 0; y < this.Height; y++)
            {
                for (int x = 0; x < this.Width; x++)
                {
                    this.SetPixel(x, bytes[index]);

                    index++;
                }

                this.IncLine();
            }

            this.CloseData();

            br.Close();
        }
Beispiel #23
0
        public Bitmap GetBitmapIndexed()
        {
            // create and fill a pixel array for the 8-bit final image
            Console.WriteLine($"Creating BMP from data with extremes: {data.Min()} {data.Max()}");

            byte[] pixelsOutput = GetDisplayBytes();

            // trim-off extra bytes if width is not a multiple of 4 bytes
            int strideByteMultiple = 4;
            int strideOverhang     = width % strideByteMultiple;

            if (strideOverhang > 0)
            {
                int    strideBytesNeededPerRow = strideByteMultiple - (strideOverhang);
                byte[] pixelsOutputOriginal    = new byte[pixelCount];
                Array.Copy(pixelsOutput, pixelsOutputOriginal, pixelCount);
                pixelsOutput = new byte[pixelCount + strideBytesNeededPerRow * height];
                int newStrideWidth = width + strideBytesNeededPerRow;
                for (int row = 0; row < height; row++)
                {
                    for (int col = 0; col < width; col++)
                    {
                        pixelsOutput[row * newStrideWidth + col] = pixelsOutputOriginal[row * width + col];
                    }
                }
            }

            // create the output bitmap (8-bit indexed color)
            var    formatOutput = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
            Bitmap bmp          = new Bitmap(width, height, formatOutput);

            // Create a grayscale palette, although other colors and LUTs could go here
            System.Drawing.Imaging.ColorPalette pal = bmp.Palette;
            for (int i = 0; i < 256; i++)
            {
                pal.Entries[i] = System.Drawing.Color.FromArgb(255, i, i, i);
            }
            bmp.Palette = pal;

            // copy the new pixel data into the data of our output bitmap
            var rect = new Rectangle(0, 0, width, height);

            System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, formatOutput);
            System.Runtime.InteropServices.Marshal.Copy(pixelsOutput, 0, bmpData.Scan0, pixelsOutput.Length);
            bmp.UnlockBits(bmpData);

            return(bmp);
        }
 public ImageUnpacker(Bitmap image, string fileName, bool removeTransparency)
 {
     if (image.Palette.Entries.Length > 0)
     {
         this.pallette = image.Palette;
     }
     this.original     = new Bitmap((Bitmap)image.Clone());
     this.originalSize = image.Size;
     if (removeTransparency)
     {
         this.original = this.RemoveTransparencyFromImage(this.original);
     }
     this.boxes    = new List <Rectangle>();
     this.FileName = fileName;
     this.IsLarge  = (this.original.Width * this.original.Height) > (800 * 800);
 }
Beispiel #25
0
        private void ExportButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();

            dlg.DefaultExt = ".gif";
            dlg.Filter     = "Portable Network Graphics|*.png|Graphics Interchange Format Image|*.gif|Bitmap Image|*.bmp";

            if (dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                if (TransparentCB.Checked)
                {
                    System.Drawing.Imaging.ColorPalette cp = GFX.gfxImage.Palette;
                    GFX.gfxImage.Palette.Entries[0] = Color.FromArgb(255, 0, 255);
                }

                switch (dlg.FilterIndex - 1)
                {
                case 0:
                    string txt0 = this.Text;
                    this.Text = "Exporting - " + txt0;
                    GFX.export(dlg.FileName, System.Drawing.Imaging.ImageFormat.Png);     //Export to indexed PNG
                    this.Text = txt0;
                    break;

                case 1:
                    string txt1 = this.Text;
                    this.Text = "Exporting - " + txt1;
                    GFX.export(dlg.FileName, System.Drawing.Imaging.ImageFormat.Gif);     //Export to indexed GIF
                    this.Text = txt1;
                    break;

                case 2:
                    string txt2 = this.Text;
                    this.Text = "Exporting - " + txt2;
                    GFX.export(dlg.FileName, System.Drawing.Imaging.ImageFormat.Bmp);     //Export to indexed BMP
                    this.Text = txt2;
                    break;

                default:
                    string txt3 = this.Text;
                    this.Text = "Exporting - " + txt3;
                    GFX.export(dlg.FileName, System.Drawing.Imaging.ImageFormat.Gif);     //Export to indexed GIF
                    this.Text = txt3;
                    break;
                }
            }
        }
Beispiel #26
0
        /// <summary>
        /// Creates a bitmap based on data, width, height, stride and pixel format.
        /// </summary>
        /// <param name="sourceData">Byte array of raw source data</param>
        /// <param name="width">Width of the image</param>
        /// <param name="height">Height of the image</param>
        /// <param name="stride">Scanline length inside the data</param>
        /// <param name="pixelFormat">Pixel format</param>
        /// <param name="palette">Color palette</param>
        /// <param name="defaultColor">Default color to fill in on the palette if the given colors don't fully fill it.</param>
        /// <returns>The new image</returns>
        static Bitmap BuildImage(Byte[] sourceData, Int32 width, Int32 height, Int32 stride, System.Drawing.Imaging.PixelFormat pixelFormat, Color[] palette, Color?defaultColor)
        {
            Bitmap newImage = new Bitmap(width, height, pixelFormat);

            System.Drawing.Imaging.BitmapData targetData = newImage.LockBits(new Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.WriteOnly, newImage.PixelFormat);
            Int32 newDataWidth = ((Image.GetPixelFormatSize(pixelFormat) * width) + 7) / 8;
            // Compensate for possible negative stride on BMP format.
            Boolean isFlipped = stride < 0;

            stride = Math.Abs(stride);
            // Cache these to avoid unnecessary getter calls.
            Int32 targetStride = targetData.Stride;
            Int64 scan0        = targetData.Scan0.ToInt64();

            for (Int32 y = 0; y < height; y++)
            {
                System.Runtime.InteropServices.Marshal.Copy(sourceData, y * stride, new IntPtr(scan0 + y * targetStride), newDataWidth);
            }
            newImage.UnlockBits(targetData);
            // Fix negative stride on BMP format.
            if (isFlipped)
            {
                newImage.RotateFlip(RotateFlipType.Rotate180FlipX);
            }
            // For indexed images, set the palette.
            if ((pixelFormat & System.Drawing.Imaging.PixelFormat.Indexed) != 0 && palette != null)
            {
                System.Drawing.Imaging.ColorPalette pal = newImage.Palette;
                for (Int32 i = 0; i < pal.Entries.Length; i++)
                {
                    if (i < palette.Length)
                    {
                        pal.Entries[i] = palette[i];
                    }
                    else if (defaultColor.HasValue)
                    {
                        pal.Entries[i] = defaultColor.Value;
                    }
                    else
                    {
                        break;
                    }
                }
                newImage.Palette = pal;
            }
            return(newImage);
        }
Beispiel #27
0
        public static void RGBMask2BGMask(string rgbMaskPath, string bgMaskPath)
        {
            // Learned how to go from managed memory to pointers for image manipulation from this:
            // https://docs.microsoft.com/en-us/dotnet/api/system.drawing.imaging.bitmapdata?view=netframework-4.7.2

            // src
            var src  = (Bitmap)Image.FromFile(rgbMaskPath);
            var rect = new Rectangle(0, 0, src.Width, src.Height);

            System.Drawing.Imaging.BitmapData srcData = src.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, src.PixelFormat);
            IntPtr srcPtr   = srcData.Scan0;
            int    srcBytes = Math.Abs(srcData.Stride) * src.Height;

            byte[] srcRgbValues = new byte[srcBytes];
            System.Runtime.InteropServices.Marshal.Copy(srcPtr, srcRgbValues, 0, srcBytes);

            // dst
            var dst = new Bitmap(src.Width, src.Height, format: System.Drawing.Imaging.PixelFormat.Format8bppIndexed);

            System.Drawing.Imaging.BitmapData dstData = dst.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, dst.PixelFormat);
            IntPtr dstPtr   = dstData.Scan0;
            int    dstBytes = Math.Abs(dstData.Stride) * dst.Height;

            byte[] dstRgbValues = new byte[dstBytes];
            System.Runtime.InteropServices.Marshal.Copy(dstPtr, dstRgbValues, 0, dstBytes);

            // dst pallete - set to gray scale
            System.Drawing.Imaging.ColorPalette pal = dst.Palette;
            for (int i = 0; i < 256; i++)
            {
                pal.Entries[i] = Color.FromArgb(i, i, i);
            }
            dst.Palette = pal;

            // Update bg mask
            for (int d = 0; d < dstRgbValues.Length; d++)
            {
                int s = d * 4;                          // src is 4 bytes (rgba) per pixel; dst is 1 byte per pixel;
                dstRgbValues[d] = srcRgbValues[s + 3];  // set dst color to src alpha (4th byte)
            }

            System.Runtime.InteropServices.Marshal.Copy(dstRgbValues, 0, dstPtr, dstBytes);
            src.UnlockBits(srcData);
            dst.UnlockBits(dstData);

            dst.Save(bgMaskPath);
        }
Beispiel #28
0
        public void SetColor(System.Drawing.Color from, System.Drawing.Color to)
        {
            System.Drawing.Imaging.ColorPalette palette = this.bitmap.Palette;

            for (int i = 0; i < palette.Entries.Length; i++)
            {
                double f = ((double)i / (double)palette.Entries.Length);

                int r = (int)((double)from.R + ((double)to.R - (double)from.R) * f);
                int g = (int)((double)from.G + ((double)to.G - (double)from.G) * f);
                int b = (int)((double)from.B + ((double)to.B - (double)from.B) * f);

                palette.Entries[i] = System.Drawing.Color.FromArgb(r, g, b);
            }

            this.bitmap.Palette = palette;
        }
Beispiel #29
0
        private static Color[] GetIndexed(SD.Bitmap bitmap, byte[] data)
        {
            int width     = bitmap.Width;
            int height    = bitmap.Height;
            int totalSize = width * height;

            SD.Imaging.ColorPalette palette = bitmap.Palette;
            Color[] colors = new Color[totalSize];

            for (int i = 0; i < totalSize; i++)
            {
                int      index = data[i];
                SD.Color c     = palette.Entries[index];
                colors[i] = new Color(c.R, c.G, c.B, c.A);
            }
            return(colors);
        }
        internal static Palette?ToAGSPalette(this System.Drawing.Imaging.ColorPalette palette)
        {
            if (palette.Entries.Length == 0)
            {
                return(null);
            }

            Color[] entries = new Color[palette.Entries.Length];

            for (int i = 0; i < entries.Length; ++i)
            {
                System.Drawing.Color color = palette.Entries[i];
                entries[i] = new Color(color.R, color.G, color.B, color.A);
            }

            return(new Palette(entries));
        }
Beispiel #31
0
 public Ansifntdef(int w, int h, System.Drawing.Color tc, Bitmap fimg, Bitmap fbgimg)
 {
     m_Fontwidth       = w;
     m_FontHeight      = h;
     m_FontTranspColor = tc;
     m_FontBGImage     = fbgimg;
     m_Palette         = m_FontBGImage.Palette;
     //m_FontImage = BitmapToIndexed(fimg, m_Palette)
     //m_FontImage.Palette = m_Palette
     m_FontImage = fimg;
     m_FontImage.MakeTransparent(m_FontTranspColor);
     if (m_Fontwidth == 8)
     {
         BuildFntBits();
     }
     FontSet = true;
 }
 private void Convert8bppBMPToGrayscale(Bitmap bmp)
 {
     if (pal == null) // pal is defined at module level as --- ColorPalette pal;
     {
         pal = bmp.Palette;
         for (int i = 0; i < 256; i++)
         {
             pal.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);
         }
     }
     bmp.Palette = pal;
 }
Beispiel #33
0
        static void Main(string[] args)
        {
            if (!System.IO.File.Exists("Palette.dat")) {
                Console.WriteLine("Palette.dat could not be found. Place Palette.dat in the same folder as OldTibiaExtractor and try again.");
                Console.ReadKey();
                return;
            }

            Bitmap bmp = new Bitmap(32, 32, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
            colorPalette = bmp.Palette;
            SetPalette("Palette.dat");

            foreach (string filename in System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location))) {
                if (filename.Contains(".PIC")) {
                    ExtractPIC(filename);
                }

                if (filename.Contains(".SPR")) {
                    ExtractSPR(filename);
                }
            }

            bmp.Dispose();
        }
Beispiel #34
0
        public void SetPalette(System.Drawing.Imaging.ColorPalette pal)
        {
            this._activePalette = pal;

            if (pal != null)
            {
                IEnumerable<Color> distinct = pal.Entries.Distinct();
                this._colorGrid.Colors = new Cyotek.Windows.Forms.ColorCollection(distinct);
                this._colorGrid.Colors.Sort(Cyotek.Windows.Forms.ColorCollectionSortOrder.Value);
            }
            else
            {
                this._colorGrid.Colors.Clear();
                this._colorGrid.Palette = Cyotek.Windows.Forms.ColorPalette.None;
            }

            ToggleEnable();
        }