private void btnImport_Click(object sender, EventArgs e) { OpenFileDialog o = new OpenFileDialog(); o.CheckFileExists = true; o.Filter = "Supported images |*.png;*.bmp;*.jpg;*.jpeg;*.tif;*.tiff;*.gif;*.ico;*.icon|" + "BitMaP (*.bmp)|*.bmp|" + "Portable Network Graphic (*.png)|*.png|" + "JPEG (*.jpg)|*.jpg;*.jpeg|" + "Tagged Image File Format (*.tiff)|*.tiff;*.tif|" + "Graphic Interchange Format (*.gif)|*.gif|" + "Icon (*.ico)|*.ico;*.icon"; o.Multiselect = false; if (o.ShowDialog() != DialogResult.OK) return; Bitmap bitmap = (Bitmap)Image.FromFile(o.FileName); // Get tiles + palette from the current image byte[] tiles = new byte[0]; Color[] pal = new Color[0]; if (radioOriginalPal.Checked) { BMP bmp = new BMP(o.FileName); tiles = bmp.Tiles; pal = bmp.Palette.Palette[0]; } else { try { Actions.Indexed_Image(bitmap, image.FormatColor, out tiles, out pal); } catch (Exception ex) { MessageBox.Show(ex.Message); Console.WriteLine(ex.Message); return; } } // Swap palettes if "Swap palette" is checked. Try to change the colors to the old palette if (radioSwapPal.Checked) { try { Actions.Swap_Palette(ref tiles, palette.Palette[(int)numPal.Value], pal, image.FormatColor); } catch (Exception ex) { MessageBox.Show(ex.Message); Console.WriteLine(ex.Message); return; } } // If the tile form is horizontal convert to it if (image.FormTile == TileForm.Horizontal || isMap) { tiles = Actions.HorizontalToLineal(tiles, bitmap.Width, bitmap.Height, image.BPP, 8); image.FormTile = TileForm.Horizontal; } // Create a map file if (isMap && checkMapCmp.Checked) map.Set_Map(Actions.Create_Map(ref tiles, image.BPP, image.TileSize), map.CanEdit, bitmap.Width, bitmap.Height); else if (isMap) { int num_tiles = (tiles.Length * 8 / image.BPP) / (image.TileSize * image.TileSize); map.Set_Map(Actions.Create_BasicMap(num_tiles), map.CanEdit); } // Set the data image.Set_Tiles(tiles, bitmap.Width, bitmap.Height, image.FormatColor, image.FormTile, image.CanEdit, 8); if (radioReplacePal.Checked) palette.Set_Palette(pal, (int)numPal.Value); Save_Files(); Update_Image(); Update_Info(); }
private void Import_File(string path, int banki) { Bitmap bitmap = (Bitmap)Image.FromFile(path); Console.WriteLine("Importing image {0} to bank {1}", path, banki.ToString()); OAM[] oams = (OAM[])sprite.Banks[banki].oams.Clone(); Color[][] pals = (Color[][])palette.Palette.Clone(); byte[] imgData = (byte[])image.Tiles.Clone(); // Get data from image byte[] tiles = new byte[0]; Color[] pal = new Color[0]; if (radioOriginalPal.Checked) { BMP bmp = new BMP(path); tiles = bmp.Tiles; if (image.FormatColor != bmp.FormatColor) if (image.FormatColor == ColorFormat.colors16) tiles = Helper.BitsConverter.Bits4ToByte(tiles); else if (image.FormatColor == ColorFormat.colors256) tiles = Helper.BitsConverter.BytesToBit4(tiles); pal = bmp.Palette.Palette[0]; } uint addedSize = 0; // Get the data of a oam and add to the end of the image for (int i = 0; i < oams.Length; i++) { if (!checkListOAM.GetItemChecked(i)) continue; Console.WriteLine("Processing cell {0}", oams[i].num_cell.ToString()); byte[] cellImg; if (!radioOriginalPal.Checked) { Bitmap subImg = (Bitmap)bitmap.Clone(new Rectangle( oams[i].obj1.xOffset + 256, oams[i].obj0.yOffset + 128, oams[i].width, oams[i].height), System.Drawing.Imaging.PixelFormat.DontCare); Actions.Indexed_Image(subImg, image.FormatColor, out cellImg, out pal); } else cellImg = Actions.Get_OAMdata(oams[i], tiles, image.FormatColor); // Swap palettes if "Swap palette" is checked. Try to change the colors to the old palette if (radioSwapPal.Checked) { try { Actions.Swap_Palette(ref cellImg, palette.Palette[oams[i].obj2.index_palette], pal, image.FormatColor, numThreshold.Value); } catch (Exception ex) { MessageBox.Show(ex.Message); Console.WriteLine(ex.Message); return; } } else if (radioReplacePal.Checked) // Set the palette pals[oams[i].obj2.index_palette] = pal; if (image.FormTile == TileForm.Horizontal) cellImg = Actions.HorizontalToLineal(cellImg, oams[i].width, oams[i].height, image.BPP, 8); // If Add image is checked add the new image to the end of the original file and change the tileOffset if (radioImgAdd.Checked) { uint added = 0; uint size = (sprite.Banks[banki].data_size > 0) ? sprite.Banks[banki].data_size + addedSize : (uint)imgData.Length; uint offset = Actions.Add_Image(ref imgData, cellImg, sprite.Banks[banki].data_offset, size, (uint)(0x20 << (int)sprite.BlockSize), out added) - sprite.Banks[banki].data_offset; addedSize += added; offset = (offset / 0x20) >> (int)this.sprite.BlockSize; if (offset >= 0x400) { MessageBox.Show( "The characters data size has exceeded the boundaries of what is permitted!\r\nSome characters will not be displayed."); break; } oams[i].obj2.tileOffset = offset; oams[i].obj1.flipX = 0; oams[i].obj1.flipY = 0; } else // Replace the old image { uint tileOffset = oams[i].obj2.tileOffset; tileOffset = (uint)(tileOffset << (byte)sprite.BlockSize) * 0x20 + sprite.Banks[banki].data_offset; Array.Copy(cellImg, 0, imgData, tileOffset, cellImg.Length); } } if (sprite.Banks[banki].data_size > 0) { sprite.Banks[banki].data_size += addedSize; for (int i = banki + 1; i < sprite.Banks.Length; i++) sprite.Banks[i].data_offset += addedSize; } // If everthing goes right then set the new data int height = (imgData.Length * 8 / image.BPP) / image.Width; image.Set_Tiles(imgData, image.Width, height, image.FormatColor, image.FormTile, image.CanEdit); sprite.Banks[banki].oams = oams; palette.Set_Palette(pals); bitmap.Dispose(); bitmap = null; }