/// <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; } }
/// <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; } }