void ActionsTabExportFlatImagesButton_Click(object sender, EventArgs e) { if (mSubject as Level != null) { Level lLevel = (Level)mSubject; FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.SelectedPath = mMainForm.LocalSettings.LastFlatImagesDir; if (fbd.ShowDialog(mMainForm) == DialogResult.OK) { string lExceptionWhen = "opening the directory"; try { foreach (FlatChunk lFlat in lLevel.SHET.Flats) { lExceptionWhen = string.Format("creating image for flat {0}", lFlat.DeclaredName); TIMChunk lTopLeftTile = lLevel.GetTileById(lFlat.TextureIds[0][0]); if (lTopLeftTile == null) { throw new Exception("top left tile was null!"); } int lTileWidth = lTopLeftTile.ImageWidth; int lTileHeight = lTopLeftTile.ImageHeight; if (lTileHeight != 64 || lTileWidth != 64) { MessageBox.Show(string.Format("Warning: Skipping tile {0} because only 64x64 tiles are supported for this action.", lFlat.DeclaredName)); } Bitmap lBmp = new Bitmap(lFlat.Width * lTileWidth, lFlat.Height * lTileHeight); Graphics g = Graphics.FromImage(lBmp); g.Clear(Color.Black); for (int x = 0; x < lFlat.Width; x++) { for (int y = 0; y < lFlat.Height; y++) { g.DrawImageUnscaled( lLevel.GetTileById(lFlat.TextureIds[x][y]).ToBitmap(), x * lTileWidth, y * lTileHeight); } } lExceptionWhen = string.Format("saving image for flat {0}", lFlat.DeclaredName); using (FileStream fs = File.Create(GetImageNameForFlat(fbd.SelectedPath, lFlat))) { lBmp.Save(fs, System.Drawing.Imaging.ImageFormat.Png); } } } catch (Exception err) { MessageBox.Show(string.Format("Exception occurred while {0}: {1}", lExceptionWhen, err.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } mMainForm.LocalSettings.LastFlatImagesDir = fbd.SelectedPath; } else { MessageBox.Show("Error: mSubject is null!"); } }
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); } } }