private void AddChunkToImage(TIMChunk c, Graphics g) { int lPixelsPerTwoBytes; switch (c.BPP) { case TIMChunk.TimBPP._4BPP: lPixelsPerTwoBytes = 4; break; case TIMChunk.TimBPP._8BPP: lPixelsPerTwoBytes = 2; break; case TIMChunk.TimBPP._16BPP: lPixelsPerTwoBytes = 1; break; default: throw new Exception("Can't deal with this BPP"); } RectangleF lDestRect = new RectangleF( WIDTH_SCALE * c.ImageOrgX, c.ImageOrgY, c.ImageWidth * WIDTH_SCALE / lPixelsPerTwoBytes, c.ImageHeight); RectangleF lClipRect = g.ClipBounds; if (!(lClipRect.IntersectsWith(lDestRect))) { return; } g.DrawImage(c.ToBitmap(), lDestRect); if (c.Palette != null) { if (c.ClutCount != 1) { throw new Exception("Don't know what to do with multi-CLUT TIMs"); } for (int palIdx = 0; palIdx < c.Palette.Length; palIdx++) { Color col = Color.FromArgb(Utils.PS16bitColorToARGB(c.Palette[palIdx])); Brush br = new SolidBrush(col); Rectangle rect = new Rectangle( WIDTH_SCALE * (c.PaletteOrgX + palIdx), c.PaletteOrgY, WIDTH_SCALE, 1); g.FillRectangle(br, rect); } } }
private static void AddChunkToImage(Chunk xiChunk, Bitmap xiBmp, int[,] xiPixelUsageMap) { if (xiChunk is TIMChunk) { try { TIMChunk c = (TIMChunk)xiChunk; Bitmap lTIMImage = c.ToBitmap(); int lPixelsPerTwoBytes; switch (c.BPP) { case TIMChunk.TimBPP._4BPP: lPixelsPerTwoBytes = 4; break; case TIMChunk.TimBPP._8BPP: lPixelsPerTwoBytes = 2; break; case TIMChunk.TimBPP._16BPP: lPixelsPerTwoBytes = 1; break; default: throw new Exception("Can't deal with this BPP"); } Rectangle lDestRect = new Rectangle( WIDTH_SCALE * c.ImageOrgX, c.ImageOrgY, c.ImageWidth * WIDTH_SCALE / lPixelsPerTwoBytes, c.ImageHeight); int lWidthScale = WIDTH_SCALE / lPixelsPerTwoBytes; for (int y = lDestRect.Top; y < lDestRect.Bottom; y++) { for (int x = lDestRect.Left; x < lDestRect.Right; x++) { Color lFromTIM = lTIMImage.GetPixel((x - lDestRect.Left) * lWidthScale, y - lDestRect.Top); SetPixel(xiBmp, x, y, lFromTIM, xiPixelUsageMap); } } if (c.Palette != null) { if (c.ClutCount != 1) { throw new Exception("Don't know what to do with multi-CLUT TIMs"); } for (int palIdx = 0; palIdx < c.Palette.Length; palIdx++) { Color col = Color.FromArgb(Utils.PS16bitColorToARGB(c.Palette[palIdx])); for (int x = 0; x < WIDTH_SCALE; x++) { SetPixel(xiBmp, WIDTH_SCALE * (c.PaletteOrgX + palIdx) + x, c.PaletteOrgY, col, xiPixelUsageMap); } } } } catch (Exception e) { Console.Error.WriteLine("Error: {0}\nSkipping this TIM", e); } } else { foreach (Chunk c in xiChunk.GetChildren()) { AddChunkToImage(c, xiBmp, xiPixelUsageMap); } } }