Esempio n. 1
0
        /// <summary>
        /// Creates a new GBA.Image from a sub-region of an existing image.
        /// </summary>
        public Image(Bitmap image, Rectangle region, Palette palette = null)
        {
            Load(region.Width, region.Height, new Palette(16));

            int   index = 0;
            Color color;
            int   HI_nibble;
            int   LO_nibble;

            if (palette == null)
            {
                for (int y = 0; y < region.Height; y++)
                {
                    for (int x = 0; x < region.Width; x += 2)
                    {
                        color     = image.GetColor(region.X + x, region.Y + y);
                        LO_nibble = Colors.Find(color);
                        if (LO_nibble == -1)
                        {
                            LO_nibble = Colors.Count;
                            Colors.Add(color);
                        }
                        color     = image.GetColor(region.X + x + 1, region.Y + y);
                        HI_nibble = Colors.Find(color);
                        if (HI_nibble == -1)
                        {
                            HI_nibble = Colors.Count;
                            Colors.Add(color);
                        }

                        Bytes[index++] = (byte)((HI_nibble << 4) | LO_nibble);
                    }
                }
            }
            else
            {
                for (int y = 0; y < region.Height; y++)
                {
                    for (int x = 0; x < region.Width; x += 2)
                    {
                        color     = image.GetColor(region.X + x + 1, region.Y + y);
                        HI_nibble = palette.Find(color);
                        color     = image.GetColor(region.X + x, region.Y + y);
                        LO_nibble = palette.Find(color);

                        if (HI_nibble == -1 || LO_nibble == -1)
                        {
                            throw new Exception("A color in the image was not found in the given palette.\n" +
                                                "At coordinates: x = " + (region.X + x) + ", y = " + (region.Y + y));
                        }
                        else
                        {
                            Bytes[index] = (byte)((HI_nibble << 4) | LO_nibble);
                        }
                        index++;
                    }
                }
                Colors = palette;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a GBA.Image from a GBA.Bitmap and the given GBA.Palette (set palette as null and it will generate a Palette)
        /// If colors are found in the Bitmap from outside the given Palette, an error will be reported.
        /// </summary>
        public Image(Bitmap image, Palette palette = null)
        {
            Load(image.Width, image.Height, new Palette(16));

            int   index = 0;
            Color color;
            int   HI_nibble;
            int   LO_nibble;

            if (palette == null)
            {
                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x += 2)
                    {
                        color     = image.GetColor(x, y);
                        LO_nibble = Colors.Find(color);
                        if (LO_nibble == -1)
                        {
                            LO_nibble = Colors.Count;
                            Colors.Add(color);
                        }
                        color     = image.GetColor(x + 1, y);
                        HI_nibble = Colors.Find(color);
                        if (HI_nibble == -1)
                        {
                            HI_nibble = Colors.Count;
                            Colors.Add(color);
                        }

                        Bytes[index++] = (byte)((HI_nibble << 4) | LO_nibble);
                    }
                }
            }
            else
            {
                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x += 2)
                    {
                        color     = image.GetColor(x + 1, y);
                        HI_nibble = palette.Find(color);
                        color     = image.GetColor(x, y);
                        LO_nibble = palette.Find(color);

                        if (HI_nibble == -1 || LO_nibble == -1)
                        {
                            throw new Exception("A color in the image was not found in the palette.");
                        }
                        else
                        {
                            Bytes[index] = (byte)((HI_nibble << 4) | LO_nibble);
                        }
                        index++;
                    }
                }
                Colors = palette;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Makes a GBA.Image from the image at the given filepath (and using the given Palette if not null)
        /// </summary>
        public Image(string filepath, Palette palette = null)
        {
            try
            {
                using (System.Drawing.Bitmap file = new System.Drawing.Bitmap(filepath))
                {
                    Load(file.Width, file.Height, palette ?? new Palette(16));

                    Color color;
                    int   HI_nibble;
                    int   LO_nibble;
                    int   index = 0;
                    if (palette == null)
                    {
                        for (int y = 0; y < Height; y++)
                        {
                            for (int x = 0; x < Width; x += 2)
                            {
                                color     = (GBA.Color)file.GetPixel(x, y);
                                LO_nibble = Colors.Find(color);
                                if (LO_nibble == -1)
                                {
                                    LO_nibble = Colors.Count;
                                    Colors.Add(color);
                                }
                                color     = (GBA.Color)file.GetPixel(x + 1, y);
                                HI_nibble = Colors.Find(color);
                                if (HI_nibble == -1)
                                {
                                    HI_nibble = Colors.Count;
                                    Colors.Add(color);
                                }

                                Bytes[index++] = (byte)((HI_nibble << 4) | LO_nibble);
                            }
                        }
                    }
                    else
                    {
                        for (int y = 0; y < Height; y++)
                        {
                            for (int x = 0; x < Width; x += 2)
                            {
                                color     = (GBA.Color)file.GetPixel(x, y);
                                LO_nibble = palette.Find(color);
                                if (LO_nibble == -1)
                                {
                                    LO_nibble = Color.GetNearest(palette, color);
                                }
                                color     = (GBA.Color)file.GetPixel(x + 1, y);
                                HI_nibble = palette.Find(color);
                                if (HI_nibble == -1)
                                {
                                    HI_nibble = Color.GetNearest(palette, color);
                                }

                                if (HI_nibble == -1 || LO_nibble == -1)
                                {
                                    throw new Exception("A color in the image was not found in the palette given.");
                                }

                                Bytes[index++] = (byte)((HI_nibble << 4) | LO_nibble);
                            }
                        }
                        Colors = palette;
                    }
                }
            }
            catch (Exception ex)
            {
                Program.ShowError("The image file could not be read:\n" + filepath, ex);
            }
        }