Skip to content

Commit

Permalink
- Changed import mode to import all files, insead of only group files
Browse files Browse the repository at this point in the history
- Fixed (again) a graphical glitch for imported images in-game
- Added support for importing images with no encoding
  • Loading branch information
WizzardMaker committed Aug 10, 2020
1 parent ce1077e commit e3362bf
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 15 deletions.
30 changes: 22 additions & 8 deletions S4GFX/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace S4GFX
{
Expand Down Expand Up @@ -350,7 +353,7 @@ private static void SaveAllBitmaps(string path, ICollectionFileReader file = nul
private static ImageData[] LoadFromBitmap(string path, int i, ICollectionFileReader file) {
IGfxImage image = file.GetImage(i);

bool saveByIndex = file.HasDIL;
bool saveByIndex = false;//file.HasDIL;
int jobIndex = saveByIndex ? (image as GfxImage).jobIndex : 0;

string basePath = $"export/{path}/{(saveByIndex ? $"{jobIndex}/" : "")}";
Expand All @@ -359,7 +362,8 @@ private static ImageData[] LoadFromBitmap(string path, int i, ICollectionFileRea
var files = Directory.GetFiles(basePath, "*.*");
int maxlen = files.Max(x => x.Length);
var result = files.OrderBy(x => x.PadLeft(maxlen, '0')).ToList();


int ind = 0;
foreach (string filePath in result) {
Bitmap map = new Bitmap(filePath);

Expand All @@ -368,22 +372,32 @@ private static ImageData[] LoadFromBitmap(string path, int i, ICollectionFileRea
image.Width = map.Width;
image.Height = map.Height;

data.data = new Byte[map.Height * map.Width * 4];
BitmapData d = map.LockBits(new Rectangle(0, 0, map.Width, map.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

int size = map.Height * map.Width * 4;

data.data = new Byte[size];

byte[] argb = new byte[size];
Marshal.Copy(d.Scan0, argb, 0, size);

map.UnlockBits(d);

int index = 0;
for (int y = 0; y < map.Height; y++) {
for (int x = 0; x < map.Width; x++) {
Color c = map.GetPixel(x, y);

data.data[index + 0] = c.R;
data.data[index + 1] = c.G;
data.data[index + 2] = c.B;
data.data[index + 3] = 255;
data.data[index + 0] = argb[index + 1];//r
data.data[index + 1] = argb[index + 2];//g
data.data[index + 2] = argb[index + 3];//b
data.data[index + 3] = argb[index + 0];//a

index += 4;
}
}

Console.WriteLine($"{ind++}/{result.Count}");

imgs.Add(data);
}
return imgs.ToArray();
Expand Down
23 changes: 22 additions & 1 deletion S4GFXLibrary/FileReader/GfxFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,26 @@ public void ChangeImageData(string groupID, int index, ImageData[] newDatas) {

//Prepare the new palette
Palette p = paletteCollection.GetPalette();
int start = paletteCollection.GetOffset((GetImage(index) as GfxImage).jobIndex);

int palettePosition = 0;
int oldIndex = (GetImage(index) as GfxImage).jobIndex;
for (int j = 0; j < newDatas.Length; j++) {
if(index + j > images.Length) {
Array.Resize(ref images, index + j + 1);
images[index + j] = new GfxImage(
null,
p,
paletteCollection.GetOffset((GetImage(index + j - 1) as GfxImage).jobIndex));
}

GfxImage i = images[index + j];

if(i.jobIndex != oldIndex) {
palettePosition = 0;
}

int start = paletteCollection.GetOffset(i.jobIndex);

i.Width = newDatas[j].width;
i.Height = newDatas[j].height;
//i.DataOffset = offsetTable.GetImageOffset(index + j) + 12;
Expand All @@ -66,6 +80,10 @@ public void ChangeImageData(string groupID, int index, ImageData[] newDatas) {
}

i.DataOffset = 0; //Hack, the data is still at the same offset, but the way the we handle the reading forces us to set it to 0, as we write the new image data to buffer[0...] instead of buffer[DataOffset...]

oldIndex = i.jobIndex;

Console.WriteLine($"{j}/{newDatas.Length} importing...");
}
//return GetDataBufferCollection(groupID);
}
Expand Down Expand Up @@ -321,6 +339,9 @@ override public byte[] GetData() {

gfxDataBuffer[offsetTable.GetImageOffset(j + 1) - 1] = 0;
gfxDataBuffer[offsetTable.GetImageOffset(j + 1) - 2] = 1;
gfxDataBuffer[offsetTable.GetImageOffset(j + 1) - 3] = 2;
gfxDataBuffer[offsetTable.GetImageOffset(j + 1) - 4] = 2;
gfxDataBuffer[offsetTable.GetImageOffset(j + 1) - 5] = 2;
//gfxDataBuffer[offsetTable.GetImageOffset(j + 1) - 3] = 1;
}
}
Expand Down
37 changes: 31 additions & 6 deletions S4GFXLibrary/GFX/GfxImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,7 @@ public byte[] CreateImageData(ImageData newImage)
return data;
}

private byte[] WriteImageDataWithNoEncoding(byte[] data, int pos, int length)
{
throw new NotImplementedException();
}


/// <summary>
/// Counts the times a value was repeated. Used in the RunLengthEncoding as it replaces duplicates by [value,repeats]
Expand Down Expand Up @@ -199,7 +196,35 @@ private int GetSameValueCount(ref byte[] data, int start, int valueToFind, ref i
return count;
}

private byte[] CreateImageDataWithRunLengthEncoding(byte[] data, int length)
private byte[] WriteImageDataWithNoEncoding(byte[] data, int pos, int length) {
List<byte> newData = new List<byte>();

for (int i = 0; i < length; i += 4) {
int value;

byte red = data[i + 0];
byte green = data[i + 1];
byte blue = data[i + 2];
byte alpha = data[i + 3];

value = palette.GetIndex(paletteOffset, Palette.RGBToPalette(red, green, blue));
if (value == -1) {
Console.WriteLine($"Invalid color - not found in the palette! Color: R:{red}, G:{green}, B:{blue}");
}

value = value - paletteOffset;

if (value >= 252) {
Console.WriteLine("Invalid color - not found in the palette!");
}

newData.Add((byte)value);
}

return newData.ToArray();
}

private byte[] CreateImageDataWithRunLengthEncoding(byte[] data, int length)
{
List<byte> newData = new List<byte>();

Expand Down Expand Up @@ -229,7 +254,7 @@ private byte[] CreateImageDataWithRunLengthEncoding(byte[] data, int length)
{
value = palette.GetIndex(paletteOffset, Palette.RGBToPalette(red, green, blue));
if(value == -1) {
Console.WriteLine("Invalid color - not found in the palette!");
Console.WriteLine($"Invalid color - not found in the palette! Color: R:{red}, G:{green}, B:{blue}");
}

value = value - paletteOffset;
Expand Down

0 comments on commit e3362bf

Please sign in to comment.