public void DiscardUnusedColorsPaletted(int imageNumber, PaletteSection paletteSection, int paletteNumber) { List <uint> pal = paletteSection.Palettes[paletteNumber]; List <uint> img = Images[imageNumber]; bool[] usedPaletteEntries = new bool[pal.Count]; for (int i = 0; i < usedPaletteEntries.Length; ++i) { usedPaletteEntries[i] = false; // initialize array to false } for (int i = 0; i < img.Count; ++i) { usedPaletteEntries[img[i]] = true; // all used palette entries get set to true } // remap old palette entries to new ones by essentially skipping over all unused colors uint[] remapTable = new uint[pal.Count]; uint counter = 0; for (int i = 0; i < usedPaletteEntries.Length; ++i) { if (usedPaletteEntries[i]) { remapTable[i] = counter; counter++; } else { remapTable[i] = 0xFFFFFFFFu; // just making sure these aren't used } } // remap the image for (int i = 0; i < img.Count; ++i) { img[i] = remapTable[img[i]]; } // generate the new palette List <uint> newPal = new List <uint>((int)counter); for (int i = 0; i < usedPaletteEntries.Length; ++i) { if (usedPaletteEntries[i]) { newPal.Add(pal[i]); } } paletteSection.Palettes[paletteNumber] = newPal; }
public void HomogenizePalette() { ImageSection isec = null; PaletteSection psec = null; foreach (var section in Sections) { if (section.GetType() == typeof(ImageSection)) { isec = (ImageSection)section; } if (section.GetType() == typeof(PaletteSection)) { psec = (PaletteSection)section; } } for (int i = 0; i < isec.ImageCount; ++i) { isec.DiscardUnusedColorsPaletted(i, psec, i); } List <uint> PaletteList = new List <uint>(); foreach (List <uint> pal in psec.Palettes) { PaletteList.AddRange(pal); } List <uint> NewPalette = PaletteList.Distinct().ToList(); int maxColors = 1 << isec.ColorDepth; if (NewPalette.Count > maxColors) { string err = "ERROR: Combined Palette over the amount of allowed colors. (" + NewPalette.Count + " > " + maxColors + ")"; Console.WriteLine(err); throw new Exception(err); } while (NewPalette.Count < maxColors) { NewPalette.Add(0); } for (int i = 0; i < isec.ImageCount; ++i) { isec.ConvertToTruecolor(i, psec.Palettes[i]); isec.CovertToPaletted(i, NewPalette.ToArray()); psec.Palettes[i] = NewPalette.ToList(); } }
public void ReduceToOneImage(int imageNumber) { foreach (var section in Sections) { if (section.GetType() == typeof(ImageSection)) { ImageSection isec = (ImageSection)section; byte[] img = isec.ImagesRawBytes[imageNumber]; isec.ImagesRawBytes = new byte[1][]; isec.ImagesRawBytes[0] = img; isec.Width = (ushort)(isec.Width >> imageNumber); isec.Height = (ushort)(isec.Height >> imageNumber); } if (section.GetType() == typeof(PaletteSection)) { PaletteSection psec = (PaletteSection)section; byte[] pal = psec.PalettesRawBytes[imageNumber]; psec.PalettesRawBytes = new byte[1][]; psec.PalettesRawBytes[0] = pal; } } uint fileinfosection = 0; foreach (var section in Sections) { section.Recalculate(0); if (section.GetType() == typeof(FileInfoSection)) { fileinfosection = section.GetPartSize(); } } uint Filesize = GetTotalFilesize(); foreach (var section in Sections) { if (section.GetType() == typeof(EndOfFileSection)) { section.Recalculate((int)Filesize - 0x10); } if (section.GetType() == typeof(EndOfImageSection)) { section.Recalculate((int)Filesize - 0x20 - (int)fileinfosection); } } }
public List <System.Drawing.Bitmap> ConvertToBitmaps() { ImageSection isec = null; PaletteSection psec = null; foreach (var section in Sections) { if (section.GetType() == typeof(ImageSection)) { isec = (ImageSection)section; } if (section.GetType() == typeof(PaletteSection)) { psec = (PaletteSection)section; } } return(isec.ConvertToBitmaps(psec)); }
public void Initialize(byte[] File) { this.File = File; uint location = 0x10; Sections = new List <ISection>(); Sections.Add(new HeaderSection(File, 0)); while (location < File.Length) { ushort CurrentType = BitConverter.ToUInt16(File, (int)location); ISection section; switch (CurrentType) { case 0x02: section = new EndOfFileSection(File, (int)location); break; case 0x03: section = new EndOfImageSection(File, (int)location); break; case 0x04: section = new ImageSection(File, (int)location); break; case 0x05: section = new PaletteSection(File, (int)location); break; case 0xFF: section = new FileInfoSection(File, (int)location); break; default: throw new Exception("Invalid Section Type"); } Sections.Add(section); location += section.GetPartSize(); } }
public List <Bitmap> ConvertToBitmaps(PaletteSection psec) { List <Bitmap> bitmaps = new List <Bitmap>(); for (int i = 0; i < Images.Count; ++i) { int w = (ushort)(Width >> i); int h = (ushort)(Height >> i); Bitmap bmp = new Bitmap(w, h); IPixelOrderIterator pixelPosition; switch (PxOrder) { case PixelOrder.Normal: pixelPosition = new LinearPixelOrderIterator(w, h); break; case PixelOrder.Faster: pixelPosition = new GimPixelOrderFasterIterator(w, h, GetBitPerPixel()); break; default: throw new Exception("Unexpected pixel order: " + PxOrder); } for (int idx = 0; idx < Images[i].Count; ++idx) { var pp = pixelPosition.GetEnumerator(); uint rawcolor = Images[i][idx]; Color color; switch (Format) { case ImageFormat.RGBA5650: color = ColorFromRGBA5650(rawcolor); break; case ImageFormat.RGBA5551: color = ColorFromRGBA5551(rawcolor); break; case ImageFormat.RGBA4444: color = ColorFromRGBA4444(rawcolor); break; case ImageFormat.RGBA8888: color = ColorFromRGBA8888(rawcolor); break; case ImageFormat.Index4: case ImageFormat.Index8: case ImageFormat.Index16: case ImageFormat.Index32: switch (psec.Format) { case ImageFormat.RGBA5650: color = ColorFromRGBA5650(psec.Palettes[i][(int)rawcolor]); break; case ImageFormat.RGBA5551: color = ColorFromRGBA5551(psec.Palettes[i][(int)rawcolor]); break; case ImageFormat.RGBA4444: color = ColorFromRGBA4444(psec.Palettes[i][(int)rawcolor]); break; case ImageFormat.RGBA8888: color = ColorFromRGBA8888(psec.Palettes[i][(int)rawcolor]); break; default: throw new Exception("Unexpected palette color type: " + psec.Format); } break; default: throw new Exception("Unexpected image color type: " + psec.Format); } pp.MoveNext(); var xy = pp.Current; if (xy.X < w && xy.Y < h) { bmp.SetPixel(xy.X, xy.Y, color); } } bitmaps.Add(bmp); } return(bitmaps); }
public void Initialize( byte[] File ) { this.File = File; uint location = 0x10; Sections = new List<ISection>(); Sections.Add( new HeaderSection( File, 0 ) ); while ( location < File.Length ) { ushort CurrentType = BitConverter.ToUInt16( File, (int)location ); ISection section; switch ( CurrentType ) { case 0x02: section = new EndOfFileSection( File, (int)location ); break; case 0x03: section = new EndOfImageSection( File, (int)location ); break; case 0x04: section = new ImageSection( File, (int)location ); break; case 0x05: section = new PaletteSection( File, (int)location ); break; case 0xFF: section = new FileInfoSection( File, (int)location ); break; default: throw new Exception( "Invalid Section Type" ); } Sections.Add( section ); location += section.GetPartSize(); } }
public void DiscardUnusedColorsPaletted( int imageNumber, PaletteSection paletteSection, int paletteNumber ) { List<uint> pal = paletteSection.Palettes[paletteNumber]; List<uint> img = Images[imageNumber]; bool[] usedPaletteEntries = new bool[pal.Count]; for ( int i = 0; i < usedPaletteEntries.Length; ++i ) { usedPaletteEntries[i] = false; // initialize array to false } for ( int i = 0; i < img.Count; ++i ) { usedPaletteEntries[img[i]] = true; // all used palette entries get set to true } // remap old palette entries to new ones by essentially skipping over all unused colors uint[] remapTable = new uint[pal.Count]; uint counter = 0; for ( int i = 0; i < usedPaletteEntries.Length; ++i ) { if ( usedPaletteEntries[i] ) { remapTable[i] = counter; counter++; } else { remapTable[i] = 0xFFFFFFFFu; // just making sure these aren't used } } // remap the image for ( int i = 0; i < img.Count; ++i ) { img[i] = remapTable[img[i]]; } // generate the new palette List<uint> newPal = new List<uint>( (int)counter ); for ( int i = 0; i < usedPaletteEntries.Length; ++i ) { if ( usedPaletteEntries[i] ) { newPal.Add( pal[i] ); } } paletteSection.Palettes[paletteNumber] = newPal; }