Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #21 from MetLob/master
Browse files Browse the repository at this point in the history
Pull Request for issues #18, #19, #7, #8
  • Loading branch information
pleonex committed Feb 28, 2016
2 parents 581845a + 11df05e commit 97e7186
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 58 deletions.
43 changes: 33 additions & 10 deletions Ekona/Images/Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,26 @@ public static uint Add_Image(ref byte[] data, byte[] newData, uint blockSize)
return offset;
}

public static uint Add_Image(ref byte[] data, byte[] newData, uint partOffset, uint partSize, uint blockSize, out uint addedLength)
{
// Add the image to the end of the partition data
// Return the offset where the data has been inserted
List<byte> result = new List<byte>(data);
uint offset = partOffset + partSize;

addedLength = (partSize % blockSize != 0) ? blockSize - partSize % blockSize : 0;
if (offset == result.Count) result.AddRange(new byte[addedLength]);
else result.InsertRange((int)offset, new byte[addedLength]);
offset += addedLength;

if (offset == result.Count) result.AddRange(newData);
else result.InsertRange((int)offset, newData);
addedLength += (uint)newData.Length;

data = result.ToArray();
return offset;
}

public static int FindNextColor(Color c, Color[] palette, decimal threshold = 0)
{
int id = -1;
Expand Down Expand Up @@ -877,14 +897,17 @@ public static NTFS[] Create_BasicMap(int num_tiles, int startTile = 0, byte pale

return map;
}
public static NTFS[] Create_Map(ref byte[] data, int tile_width, int tile_size, byte palette = 0)
public static NTFS[] Create_Map(ref byte[] data, int bpp, int tile_size, byte palette = 0)
{
int ppt = tile_size * tile_size; // pixels per tile
int tile_length = ppt * bpp / 8;

// Divide the data in tiles
byte[][] tiles = new byte[data.Length / (tile_width * 8)][];
byte[][] tiles = new byte[data.Length / tile_length][];
for (int i = 0; i < tiles.Length; i++)
{
tiles[i] = new byte[tile_width * 8];
Array.Copy(data, i * (tile_width * 8), tiles[i], 0, tile_width * 8);
tiles[i] = new byte[tile_length];
Array.Copy(data, i * tiles[i].Length, tiles[i], 0, tiles[i].Length);
}

NTFS[] map = new NTFS[tiles.Length];
Expand All @@ -906,19 +929,19 @@ public static NTFS[] Create_Map(ref byte[] data, int tile_width, int tile_size,
index = t;
break;
}
if (Compare_Array(newtiles[t], XFlip(tiles[i], tile_size, tile_width)))
if (Compare_Array(newtiles[t], XFlip(tiles[i], tile_size, bpp)))
{
index = t;
flipX = 1;
break;
}
if (Compare_Array(newtiles[t], YFlip(tiles[i], tile_size, tile_width)))
if (Compare_Array(newtiles[t], YFlip(tiles[i], tile_size, bpp)))
{
index = t;
flipY = 1;
break;
}
if (Compare_Array(newtiles[t], YFlip(XFlip(tiles[i], tile_size, tile_width), tile_size, tile_width)))
if (Compare_Array(newtiles[t], YFlip(XFlip(tiles[i], tile_size, bpp), tile_size, bpp)))
{
index = t;
flipX = 1;
Expand All @@ -939,10 +962,10 @@ public static NTFS[] Create_Map(ref byte[] data, int tile_width, int tile_size,
}

// Save the new tiles
data = new byte[newtiles.Count * tile_width * 8];
data = new byte[newtiles.Count * tile_length];
for (int i = 0; i < newtiles.Count; i++)
for (int j = 0; j < newtiles[i].Length; j++)
data[j + i * (tile_width * 8)] = newtiles[i][j];
data[j + i * tile_length] = newtiles[i][j];
return map;
}
public static bool Compare_Array(byte[] d1, byte[] d2)
Expand Down Expand Up @@ -1092,7 +1115,7 @@ public static Bitmap Get_Image(Bank bank, uint blockSize, ImageBase img, Palette
ImageBase cell_img = new TestImage();
cell_img.Set_Tiles((byte[])img.Tiles.Clone(), bank.oams[i].width, bank.oams[i].height, img.FormatColor,
img.FormTile, false);
cell_img.StartByte = (int)tileOffset * 0x20;
cell_img.StartByte = (int)(tileOffset * 0x20 + bank.data_offset);

byte num_pal = bank.oams[i].obj2.index_palette;
if (num_pal >= pal.NumberOfPalettes)
Expand Down
6 changes: 6 additions & 0 deletions Ekona/Images/Dialogs/OAMEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public OAMEditor(string langxml, Bank bank, SpriteBase sprite, ImageBase image,
InitializeComponent();
this.bank = bank;
numOAM.Maximum = bank.oams.Length - 1;
numOffset.Maximum = (bank.data_size == 0)
? image.Tiles.Length / (0x20 << (int)sprite.BlockSize) - 1
: bank.data_size / (0x20 << (int)sprite.BlockSize) - 1;

preview = true;
this.sprite = sprite;
Expand All @@ -73,6 +76,9 @@ public OAMEditor(XElement langxml, Bank bank, SpriteBase sprite, ImageBase image
InitializeComponent();
this.bank = bank;
numOAM.Maximum = bank.oams.Length - 1;
numOffset.Maximum = (bank.data_size == 0)
? image.Tiles.Length / (0x20 << (int)sprite.BlockSize) - 1
: bank.data_size / (0x20 << (int)sprite.BlockSize) - 1;

preview = true;
this.sprite = sprite;
Expand Down
6 changes: 3 additions & 3 deletions Ekona/Images/ImageControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,13 @@ private void btnImport_Click(object sender, EventArgs e)
image.FormTile = TileForm.Horizontal;
}

// Create a map file
// Create a map file // MetLob edition 19/05/2015
if (isMap && checkMapCmp.Checked)
map.Set_Map(Actions.Create_Map(ref tiles, image.BPP, image.TileSize), map.CanEdit, bitmap.Width, bitmap.Height);
map.Set_Map(Actions.Create_Map(ref tiles, image.BPP, image.TileSize, (byte)numPal.Value), 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);
map.Set_Map(Actions.Create_BasicMap(num_tiles, 0, (byte)numPal.Value), map.CanEdit);
}

// Set the data
Expand Down
11 changes: 8 additions & 3 deletions Ekona/Images/RawData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,19 @@ public void Read(string fileIn, TileForm form, ColorFormat format, bool editable

public override void Write(string fileOut, PaletteBase palette)
{
int image_size = Width * Height * BPP / 8;
// MetLob edition 25/12/2015
int dataSize = (post_data.Length == 0) ? Tiles.Length : Math.Min(Tiles.Length, ori_data.Length - StartByte);
if (dataSize < Tiles.Length)
MessageBox.Show(
"Tiles data size exceeds the allowable length and will be trimmed.",
"Image import processing");

BinaryWriter bw = new BinaryWriter(File.OpenWrite(fileOut));
bw.Write(prev_data);
for (int i = 0; i < StartByte; i++)
bw.Write(ori_data[i]);
bw.Write(Tiles);
for (int i = image_size + StartByte; i < ori_data.Length; i++)
bw.Write(Tiles, 0, dataSize);
for (int i = Tiles.Length + StartByte; i < ori_data.Length; i++)
bw.Write(ori_data[i]);
bw.Write(post_data);
bw.Flush();
Expand Down
3 changes: 3 additions & 0 deletions Ekona/Images/SpriteBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ public struct Bank

public ushort height;
public ushort width;

public uint data_offset;
public uint data_size;
}
public struct OAM
{
Expand Down
26 changes: 22 additions & 4 deletions Ekona/Images/SpriteControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ private void Import_File(string path, int banki)
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++)
{
Expand Down Expand Up @@ -498,21 +500,37 @@ private void Import_File(string path, int banki)
// If Add image is checked add the new image to the end of the original file and change the tileOffset
if (radioImgAdd.Checked)
{
uint offset = Actions.Add_Image(ref imgData, cellImg, (uint)(1 << (int)sprite.BlockSize) * 0x20);
offset /= 0x20;
offset >>= (int)sprite.BlockSize;
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;
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);
Expand Down
Loading

0 comments on commit 97e7186

Please sign in to comment.