//This works fine as shared /// <summary> /// Injects a custom bitmap into a .bin file /// </summary> /// <param name="OriginalData">The original file we want to import our image into</param> /// <param name="Image">Our image to import</param> /// <param name="Codec">The N64Codec to use</param> /// <param name="ImageOffset">The offset of the image within the file</param> /// <param name="PaletteOffset">The offset of the palette within the file</param> public byte[] InjectImageIntoByteArray(int ImageOffset, int PaletteOffset, byte[] OriginalData, Bitmap Image, N64Codec Codec) { byte[] imageData = null, paletteData = null; N64GraphicsCoding.Convert(ref imageData, ref paletteData, Codec, Image); ByteTools.TrimEnd(paletteData); Array.Copy(imageData, 0, OriginalData, ImageOffset, imageData.Length); Array.Copy(paletteData, 0, OriginalData, PaletteOffset, paletteData.Length); return(OriginalData); }
public void createImg(Graphics g, byte[] Data, int colval, int DataOffset, int PalOff, int SizeX, int SizeY) { byte[] Palette; switch (colval) { //ci4 case (0): { Palette = new byte[32]; //int PalOff = (int)DataOffset + 0x200; Array.Copy(Data, PalOff, Palette, 0, 32); if (DataOffset != PalOff) { N64GraphicsCoding.RenderTexture(g, Data, Palette, (int)DataOffset, SizeX, SizeY, 1, N64Codec.CI4, N64IMode.AlphaBinary); } else { N64GraphicsCoding.RenderTexture(g, Data, Palette, (int)DataOffset, SizeX, SizeY, 1, N64Codec.I4, N64IMode.AlphaBinary); } break; } //rgb16 case (1): { Palette = new byte[128]; //int arrayoffset = (int)DataOffset + 0x400; if (Data.Length > PalOff + 512) { Array.Copy(Data, PalOff, Palette, 0, 512); } else { Array.Copy(Data, PalOff, Palette, 0, Data.Length - PalOff); } if (DataOffset != PalOff) { N64GraphicsCoding.RenderTexture(g, Data, Palette, (int)DataOffset, SizeX, SizeY, 1, N64Codec.CI8, N64IMode.AlphaCopyIntensity); } else { N64GraphicsCoding.RenderTexture(g, Data, Palette, (int)DataOffset, SizeX, SizeY, 1, N64Codec.I8, N64IMode.AlphaCopyIntensity); } break; } //rgb16 case (2): { Palette = new byte[32]; //int PalOff = (int)DataOffset + 0x200; Array.Copy(Data, PalOff, Palette, 0, 32); N64GraphicsCoding.RenderTexture(g, Data, Palette, (int)DataOffset, SizeX, SizeY, 1, N64Codec.RGBA16, N64IMode.AlphaBinary); break; } //rgba32 case (3): { Palette = new byte[32]; //int PalOff = (int)DataOffset + 0x200; Array.Copy(Data, PalOff, Palette, 0, 32); N64GraphicsCoding.RenderTexture(g, Data, Palette, (int)DataOffset, SizeX, SizeY, 1, N64Codec.RGBA32, N64IMode.AlphaBinary); break; } } }
private void button1_Click(object sender, EventArgs e) { int p = 1; int f5; int SizeX = 32; int SizeY = 32; UInt32 DataOffset = 0; List <int> imageOffsets = new List <int>(); List <string> imageNames = new List <string>(); OpenFileDialog open = new OpenFileDialog(); open.Filter = "bin file|*.bin"; DialogResult res = open.ShowDialog(); CommonOpenFileDialog FolSelect = new CommonOpenFileDialog(); FolSelect.IsFolderPicker = true; if (res == DialogResult.OK && FolSelect.ShowDialog() == CommonFileDialogResult.Ok) { try { //OpenFileDialog open = new OpenFileDialog(); //open.Filter = "BIN file|*.bin"; //DialogResult res = open.ShowDialog(); //if (res == DialogResult.OK) { foreach (PictureBox b in LoadedImgs) { b.Dispose(); } foreach (Label l in Labels) { l.Dispose(); } //vScrollBar1.Maximum = 0; //LoadedImgs.Clear(); byte[] Data = File.ReadAllBytes(open.FileName); //detect if header contains offsets or DL's contain offsets /*List<string> HeaderData = new List<string>(); * HeaderData.AddRange(new HeaderReader().ReadHeader(Data).Split('\n')); * HeaderData.RemoveAll(x => !x.Contains("Offset:")); */ int[] Offsets = new HeaderReader().ReadOffsets(Data); bool HeaderIsDL = true; for (int i = 0; i < Offsets.Length / 2; i++) { if (Data[Offsets[i]] != 4) { HeaderIsDL = false; } } if (HeaderIsDL) { f5 = 0; int CurOffset = Offsets[0]; while (CurOffset < Data.Length) { if (Data[CurOffset] == 0x04 || Data[CurOffset] == 0xB8) { f5 = 0; } //count f5 third instance = pallette if (Data[CurOffset] == 0xF5) { f5++; int OffsetPrevFD = CurOffset; while (Data[OffsetPrevFD] != 0xFD) { OffsetPrevFD -= 1; } if (OffsetPrevFD >= CurOffset - 15) { //texture command //get sizes switch (Data[CurOffset + 6]) { case (0x40): { SizeY = 32; break; } case (0x80): { SizeY = 64; break; } } switch (Data[CurOffset + 7]) { case (0x50): { SizeX = 32; break; } case (0x60): { SizeX = 64; break; } } byte[] FixedData = new byte[4]; Array.Copy(Data, OffsetPrevFD + 4, FixedData, 0, 4); FixedData[0] = 0; DataOffset = ByteTools.Read4Bytes(FixedData, 0); //UInt32 //store offset at this point imageOffsets.Add((int)DataOffset); } } if (f5 == 3) { Bitmap b = new Bitmap(SizeX, SizeY, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(b); byte[] Palette; int colval; //string ColourType = Convert.ToString(Data[CurOffset + 1], 2); //ColourType += Convert.ToString(Data[CurOffset + 2], 2); //ColourType = ColourType.Substring(4, 2); colval = (Data[CurOffset + 1]); colval = (colval & 24); colval = colval >> 3; //Convert.ToInt32(ColourType, 2); switch (colval) { //ci4 case (0): { Palette = new byte[32]; int PalOff = (int)DataOffset + 0x200; Array.Copy(Data, PalOff, Palette, 0, 32); N64GraphicsCoding.RenderTexture(g, Data, Palette, (int)DataOffset, SizeX, SizeY, 1, N64Codec.CI4, N64IMode.AlphaBinary); break; } //rgb16 case (1): { Palette = new byte[512]; int arrayoffset = (int)DataOffset + 0x400; Array.Copy(Data, arrayoffset, Palette, 0, 512); N64GraphicsCoding.RenderTexture(g, Data, Palette, (int)DataOffset, SizeX, SizeY, 1, N64Codec.CI8, N64IMode.AlphaCopyIntensity); break; } //rgb16 case (2): { Palette = new byte[32]; int PalOff = (int)DataOffset + 0x200; Array.Copy(Data, PalOff, Palette, 0, 32); N64GraphicsCoding.RenderTexture(g, Data, Palette, (int)DataOffset, SizeX, SizeY, 1, N64Codec.RGBA16, N64IMode.AlphaBinary); break; } } PictureBox box = new PictureBox(); box.Image = b; box.Width = SizeX; box.Height = SizeY; LoadedImgs.Add(box); Label l = new Label(); l.Text = DataOffset.ToString("X"); Labels.Add(l); b.RotateFlip(RotateFlipType.RotateNoneFlipY); string filepath = @"\textures\" + Path.GetFileName(open.FileName.Substring(0, open.FileName.Length - 4) + "-" + p + ".bmp"); b.Save(FolSelect.FileName + filepath); imageNames.Add(FolSelect.FileName + filepath); p++; f5 = 0; } CurOffset += 8; } } else { /* * foreach(int off in Offsets) * { * byte[] pal = new byte[32]; * * Array.Copy(Data, off, pal, 0, 32); * Bitmap b = new Bitmap(32, 32, System.Drawing.Imaging.PixelFormat.Format32bppArgb); * Graphics g = Graphics.FromImage(b); * N64GraphicsCoding.RenderTexture(g, Data, pal, off, 32,32,1,N64Codec.CI4, N64IMode.AlphaBinary); * * PictureBox box = new PictureBox(); * box.Image = b; * box.Width = 32; * box.Height = 32; * LoadedImgs.Add(box); * * pal = new byte[256]; * * Array.Copy(Data, off, pal, 0, 256); * b = new Bitmap(256, 256, System.Drawing.Imaging.PixelFormat.Format32bppArgb); * g = Graphics.FromImage(b); * N64GraphicsCoding.RenderTexture(g, Data, pal, off, 64, 64, 1, N64Codec.CI8, N64IMode.AlphaBinary); * * box = new PictureBox(); * box.Image = b; * box.Width = 32; * box.Height = 32; * LoadedImgs.Add(box); * } * byte[] Pal = new byte[32]; * */ } int CurPosY = BaseBox.Location.Y; int CurPosX = BaseBox.Location.X; /*foreach (PictureBox b in LoadedImgs) * { * b.Location = new System.Drawing.Point(CurPosX, CurPosY); * CurPosY += b.Image.Height + 10; * vScrollBar1.Maximum += b.Image.Height + 10; * this.Controls.Add(b); * OrigY.Add(CurPosY); * }*/ for (int i = 0; i < LoadedImgs.Count; i++) { LoadedImgs[i].Location = new System.Drawing.Point(CurPosX, CurPosY); CurPosY += LoadedImgs[i].Image.Height + 10; //vScrollBar1.Maximum += LoadedImgs[i].Image.Height + 10; this.Controls.Add(LoadedImgs[i]); OrigY.Add(CurPosY); Labels[i].Location = new System.Drawing.Point(CurPosX + LoadedImgs[i].Width + 32, CurPosY); this.Controls.Add(Labels[i]); } BaseBox.Visible = false; } } catch (Exception ex) { MessageBox.Show(ex.Message); } try { SaveFileDialog mtl = new SaveFileDialog(); mtl.Filter = "material file|*.mtl"; res = mtl.ShowDialog(); if (res == DialogResult.OK) { //string[] OutObj = generateMtl(mtl.FileName, imageNames, RList); //File.WriteAllLines(mtl.FileName, OutObj); } SaveFileDialog obj = new SaveFileDialog(); obj.Filter = "object file|*.obj"; res = obj.ShowDialog(); if (res == DialogResult.OK) { string[] OutObj = new DLParser().GetParsedObject2(File.ReadAllBytes(open.FileName), imageOffsets, imageNames, mtl.FileName.Substring(0, mtl.FileName.Length - 4)); File.WriteAllLines(obj.FileName, OutObj); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
private void button1_Click(object sender, EventArgs e) { try { OpenFileDialog open = new OpenFileDialog(); open.Filter = "BIN file|*.bin"; DialogResult res = open.ShowDialog(); if (res == DialogResult.OK) { foreach (PictureBox b in LoadedImgs) { b.Dispose(); } foreach (Label l in Labels) { l.Dispose(); } vScrollBar1.Maximum = 0; LoadedImgs.Clear(); byte[] Data = File.ReadAllBytes(open.FileName); //detect if header contains offsets or DL's contain offsets /*List<string> HeaderData = new List<string>(); * HeaderData.AddRange(new HeaderReader().ReadHeader(Data).Split('\n')); * HeaderData.RemoveAll(x => !x.Contains("Offset:")); */ int[] Offsets = new HeaderReader().ReadOffsets(Data); bool HeaderIsDL = true; for (int i = 0; i < Offsets.Length / 2; i++) { if (Data[Offsets[i]] != 4) { HeaderIsDL = false; } } if (HeaderIsDL) { int CurOffset = Offsets[0]; while (CurOffset < Data.Length) { if (Data[CurOffset] == 0xF5) { int OffsetPrevFD = CurOffset; while (Data[OffsetPrevFD] != 0xFD) { OffsetPrevFD -= 1; } if (OffsetPrevFD > CurOffset - 15) { //texture command //get sizes int SizeX = 32; int SizeY = 32; switch (Data[CurOffset + 6]) { case (0x40): { SizeY = 32; break; } case (0x80): { SizeY = 64; break; } } switch (Data[CurOffset + 7]) { case (0x50): { SizeX = 32; break; } case (0x60): { SizeX = 64; break; } } byte[] FixedData = new byte[4]; Array.Copy(Data, OffsetPrevFD + 4, FixedData, 0, 4); FixedData[0] = 0; UInt32 DataOffset = ByteTools.Read4Bytes(FixedData, 0); Bitmap b = new Bitmap(SizeX, SizeY, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(b); byte[] Palette; string ColourType = Convert.ToString(Data[CurOffset + 1], 2); ColourType += Convert.ToString(Data[CurOffset + 2], 2); ColourType = ColourType.Substring(4, 2); switch (Convert.ToInt32(ColourType, 2)) { case (0): { Palette = new byte[32]; int PalOff = (int)DataOffset + 0x200; Array.Copy(Data, PalOff, Palette, 0, 32); N64GraphicsCoding.RenderTexture(g, Data, Palette, (int)DataOffset, SizeX, SizeY, 1, N64Codec.CI4, N64IMode.AlphaBinary); break; } case (1): { Palette = new byte[512]; Array.Copy(Data, (int)DataOffset + 0x1000, Palette, 0, 512); N64GraphicsCoding.RenderTexture(g, Data, Palette, (int)DataOffset, SizeX, SizeY, 1, N64Codec.CI8, N64IMode.AlphaBinary); break; } } PictureBox box = new PictureBox(); box.Image = b; box.Width = SizeX; box.Height = SizeY; LoadedImgs.Add(box); Label l = new Label(); l.Text = DataOffset.ToString("X"); Labels.Add(l); } } CurOffset += 8; } } else { /* * foreach(int off in Offsets) * { * byte[] pal = new byte[32]; * * Array.Copy(Data, off, pal, 0, 32); * Bitmap b = new Bitmap(32, 32, System.Drawing.Imaging.PixelFormat.Format32bppArgb); * Graphics g = Graphics.FromImage(b); * N64GraphicsCoding.RenderTexture(g, Data, pal, off, 32,32,1,N64Codec.CI4, N64IMode.AlphaBinary); * * PictureBox box = new PictureBox(); * box.Image = b; * box.Width = 32; * box.Height = 32; * LoadedImgs.Add(box); * * pal = new byte[256]; * * Array.Copy(Data, off, pal, 0, 256); * b = new Bitmap(256, 256, System.Drawing.Imaging.PixelFormat.Format32bppArgb); * g = Graphics.FromImage(b); * N64GraphicsCoding.RenderTexture(g, Data, pal, off, 64, 64, 1, N64Codec.CI8, N64IMode.AlphaBinary); * * box = new PictureBox(); * box.Image = b; * box.Width = 32; * box.Height = 32; * LoadedImgs.Add(box); * } * byte[] Pal = new byte[32]; * */ } int CurPosY = BaseBox.Location.Y; int CurPosX = BaseBox.Location.X; /*foreach (PictureBox b in LoadedImgs) * { * b.Location = new System.Drawing.Point(CurPosX, CurPosY); * CurPosY += b.Image.Height + 10; * vScrollBar1.Maximum += b.Image.Height + 10; * this.Controls.Add(b); * OrigY.Add(CurPosY); * }*/ for (int i = 0; i < LoadedImgs.Count; i++) { LoadedImgs[i].Location = new System.Drawing.Point(CurPosX, CurPosY); CurPosY += LoadedImgs[i].Image.Height + 10; vScrollBar1.Maximum += LoadedImgs[i].Image.Height + 10; this.Controls.Add(LoadedImgs[i]); OrigY.Add(CurPosY); Labels[i].Location = new System.Drawing.Point(CurPosX + LoadedImgs[i].Width + 32, CurPosY); this.Controls.Add(Labels[i]); } BaseBox.Visible = false; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }