Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions OpenKh.Bbs/FontsArc.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OpenKh.Common;
using OpenKh.Common;
using OpenKh.Imaging;
using System;
using System.Collections.Generic;
Expand All @@ -10,7 +10,7 @@ namespace OpenKh.Bbs
{
public class FontsArc
{
private class Image : IImageRead
private class Image : IImage
{
private readonly byte[] _imageData;
private readonly byte[] _clutData;
Expand All @@ -20,6 +20,7 @@ internal Image(string name, Arc.Entry mtx, byte[] clut, int width, int maxHeight
Name = name;
Size = new Size(width, maxHeight);
PixelFormat = pixelFormat;
ClutFormat = PixelFormat.Rgba8888;

var bpp = 0;
switch (pixelFormat)
Expand Down Expand Up @@ -48,6 +49,8 @@ internal Image(string name, Arc.Entry mtx, byte[] clut, int width, int maxHeight

public PixelFormat PixelFormat { get; }

public PixelFormat ClutFormat { get; }

public byte[] GetClut() => _clutData;

public byte[] GetData() => _imageData;
Expand All @@ -56,8 +59,8 @@ internal Image(string name, Arc.Entry mtx, byte[] clut, int width, int maxHeight
public class Font
{
public string Name { get; }
public IImageRead Image1 { get; }
public IImageRead Image2 { get; }
public IImage Image1 { get; }
public IImage Image2 { get; }
public FontInfo Info { get; }
public FontCharacterInfo[] CharactersInfo { get; }

Expand Down Expand Up @@ -96,7 +99,7 @@ private FontsArc(Stream stream)
FontNumeral = new Font(_entries, "numeral");
}

public IImageRead FontIcon { get; }
public IImage FontIcon { get; }
public Font FontCmd { get; }
public Font FontHelp { get; }
public Font FontMenu { get; }
Expand Down
6 changes: 3 additions & 3 deletions OpenKh.Command.ImgTool/Utils/ImgdBitmapUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ImgdBitmapUtil
{
class ReadAs32bppPixels
{
public ReadAs32bppPixels(IImageRead bitmap)
public ReadAs32bppPixels(IImage bitmap)
{
Width = bitmap.Size.Width;
Height = bitmap.Size.Height;
Expand Down Expand Up @@ -88,7 +88,7 @@ public int FindNearest(uint pixel)
}
}

public static Imgd ToImgd(IImageRead bitmap, int bpp, Func<IImageRead, IImageRead> quantizer, bool swizzle = false)
public static Imgd ToImgd(IImage bitmap, int bpp, Func<IImage, IImage> quantizer, bool swizzle = false)
{
if (quantizer != null)
{
Expand Down Expand Up @@ -224,7 +224,7 @@ public static Imgd ToImgd(IImageRead bitmap, int bpp, Func<IImageRead, IImageRea
throw new NotSupportedException($"BitsPerPixel {bpp} not recognized!");
}

public static IEnumerable<Imgd> FromFileToImgdList(string anyFile, int bitsPerPixel, Func<IImageRead, IImageRead> quantizer, bool swizzle)
public static IEnumerable<Imgd> FromFileToImgdList(string anyFile, int bitsPerPixel, Func<IImage, IImage> quantizer, bool swizzle)
{
switch (Path.GetExtension(anyFile).ToLowerInvariant())
{
Expand Down
11 changes: 7 additions & 4 deletions OpenKh.Command.ImgTool/Utils/QuantizerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private class CommonQuantizerParam : ICommonQuantizerParam
public bool PngQuant { get; set; }
}

public static Func<IImageRead, IImageRead> MakeFrom(int BitsPerPixel, bool InvokePngquant) =>
public static Func<IImage, IImage> MakeFrom(int BitsPerPixel, bool InvokePngquant) =>
MakeFrom(
new CommonQuantizerParam
{
Expand All @@ -29,7 +29,7 @@ public static Func<IImageRead, IImageRead> MakeFrom(int BitsPerPixel, bool Invok
}
);

public static Func<IImageRead, IImageRead> MakeFrom(ICommonQuantizerParam param)
public static Func<IImage, IImage> MakeFrom(ICommonQuantizerParam param)
{
return bitmap =>
{
Expand Down Expand Up @@ -97,10 +97,11 @@ public static Func<IImageRead, IImageRead> MakeFrom(ICommonQuantizerParam param)
};
}

private class LocalImager : IImageRead
private class LocalImager : IImage
{
public Size Size { get; set; }
public PixelFormat PixelFormat { get; set; }
public PixelFormat ClutFormat { get; set; }

internal byte[] Clut { get; set; }
internal byte[] Data { get; set; }
Expand All @@ -109,7 +110,7 @@ private class LocalImager : IImageRead
public byte[] GetData() => Data;
}

private static IImageRead ConvertFrom(BitmapOutput output)
private static IImage ConvertFrom(BitmapOutput output)
{
var size = new Size(output.Width, output.Height);
switch (output.BitsPixel)
Expand All @@ -119,6 +120,7 @@ private static IImageRead ConvertFrom(BitmapOutput output)
{
Size = size,
PixelFormat = PixelFormat.Indexed4,
ClutFormat = PixelFormat.Rgba8888,
Clut = ConvertClutFrom(output.Palette),
Data = ConvertDataFrom(output.PixelLines, (output.Width + 1) / 2, output.Height),
};
Expand All @@ -127,6 +129,7 @@ private static IImageRead ConvertFrom(BitmapOutput output)
{
Size = size,
PixelFormat = PixelFormat.Indexed8,
ClutFormat = PixelFormat.Rgba8888,
Clut = ConvertClutFrom(output.Palette),
Data = ConvertDataFrom(output.PixelLines, output.Width, output.Height),
};
Expand Down
6 changes: 4 additions & 2 deletions OpenKh.Command.TexFooter/Utils/SpriteImageUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace OpenKh.Command.TexFooter.Utils
{
internal static class SpriteImageUtil
{
public static IImageRead ToBitmap(
public static IImage ToBitmap(
int BitsPerPixel,
int SpriteWidth,
int SpriteHeight,
Expand Down Expand Up @@ -62,15 +62,17 @@ byte[] SpriteImage
{
Size = new Size(SpriteWidth, SpriteHeight * NumSpritesInImageData),
PixelFormat = pixFmt,
ClutFormat = PixelFormat.Rgba8888,
Clut = palette,
Data = bitmapData,
};
}

private class LocalBitmap : IImageRead
private class LocalBitmap : IImage
{
public Size Size { get; internal set; }
public PixelFormat PixelFormat { get; internal set; }
public PixelFormat ClutFormat { get; internal set; }

internal byte[] Clut { get; set; }
internal byte[] Data { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion OpenKh.Engine.MonoGame/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace OpenKh.Engine.MonoGame
{
public static class Extensions
{
public static Texture2D CreateTexture(this IImageRead image, GraphicsDevice graphicsDevice)
public static Texture2D CreateTexture(this IImage image, GraphicsDevice graphicsDevice)
{
var size = image.Size;
var texture = new Texture2D(graphicsDevice, size.Width, size.Height);
Expand Down
2 changes: 1 addition & 1 deletion OpenKh.Engine.MonoGame/MonoSpriteDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public void Dispose()
_indexBuffer.Dispose();
}

public ISpriteTexture CreateSpriteTexture(IImageRead image)
public ISpriteTexture CreateSpriteTexture(IImage image)
{
var size = image.Size;
var texture = new RenderTarget2D(_graphicsDevice, size.Width, size.Height);
Expand Down
4 changes: 2 additions & 2 deletions OpenKh.Engine/Extensions/ImageReadExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static class ImageReadExtensions
private static readonly byte[] Rgba = new byte[] { 2, 1, 0, 3 };

/// <returns>`BB GG RR AA`</returns>
public static byte[] AsBgra8888(this IImageRead image)
public static byte[] AsBgra8888(this IImage image)
{
switch (image.PixelFormat)
{
Expand All @@ -28,7 +28,7 @@ public static byte[] AsBgra8888(this IImageRead image)
}
}

public static byte[] AsRgba8888(this IImageRead image)
public static byte[] AsRgba8888(this IImage image)
{
switch (image.PixelFormat)
{
Expand Down
2 changes: 1 addition & 1 deletion OpenKh.Engine/Renders/ISpriteDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public interface ISpriteDrawing : IDisposable
{
ISpriteTexture DestinationTexture { get; set; }

ISpriteTexture CreateSpriteTexture(IImageRead image);
ISpriteTexture CreateSpriteTexture(IImage image);

ISpriteTexture CreateSpriteTexture(int width, int height);

Expand Down
14 changes: 7 additions & 7 deletions OpenKh.Engine/Renders/Kh2MessageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace OpenKh.Engine.Renders
{
public class RenderingMessageContext
{
public IImageRead Font { get; set; }
public IImageRead Font2 { get; set; }
public IImageRead Icon { get; set; }
public IImage Font { get; set; }
public IImage Font2 { get; set; }
public IImage Icon { get; set; }
public byte[] FontSpacing { get; set; }
public byte[] IconSpacing { get; set; }
public IMessageEncoder Encoder { get; set; }
Expand All @@ -31,9 +31,9 @@ public class Kh2MessageRenderer : IMessageRenderer, IDisposable

private readonly byte[] _fontSpacing;
private readonly byte[] _iconSpacing;
private readonly IImageRead _imageFont;
private readonly IImageRead _imageFont2;
private readonly IImageRead _imageIcon;
private readonly IImage _imageFont;
private readonly IImage _imageFont2;
private readonly IImage _imageIcon;
private readonly ISpriteTexture _spriteFont;
private readonly ISpriteTexture _spriteFont2;
private readonly ISpriteTexture _spriteIcon;
Expand Down Expand Up @@ -237,7 +237,7 @@ protected void DrawImage(ISpriteTexture texture, double x, double y, int sourceX
.Color(color)
.SpriteTexture(texture));

private void InitializeSurface(ref ISpriteTexture spriteTexture, IImageRead image)
private void InitializeSurface(ref ISpriteTexture spriteTexture, IImage image)
{
spriteTexture?.Dispose();
spriteTexture = _drawing?.CreateSpriteTexture(image);
Expand Down
21 changes: 20 additions & 1 deletion OpenKh.Imaging/IImage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Drawing;
using System.Drawing;

namespace OpenKh.Imaging
{
Expand All @@ -7,5 +7,24 @@ public interface IImage
Size Size { get; }

PixelFormat PixelFormat { get; }

PixelFormat ClutFormat { get; }

/// <summary>
/// Get pixels
/// </summary>
/// <returns>
/// Return bitmap pixels in these orders.
/// - `Indexed4`: The first pixel is high byte. The second pixel is low byte.
/// - `Indexed8`: No conversion, one byte is one pixel.
/// - `Rgba8888`: `BB GG RR AA` (same as Format32bppArgb)
/// </returns>
byte[] GetData();

/// <summary>
/// Get color look at table in this order: `RR GG BB AA`, for `Indexed4` or `Indexed8` images.
/// `AA` uses normal alpha range (0 to 255).
/// </summary>
byte[] GetClut();
}
}
22 changes: 0 additions & 22 deletions OpenKh.Imaging/IImageRead.cs

This file was deleted.

2 changes: 1 addition & 1 deletion OpenKh.Imaging/ImageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace OpenKh.Imaging
{
public static class ImageExtensions
{
public static byte[] ToBgra32(this IImageRead imageRead)
public static byte[] ToBgra32(this IImage imageRead)
{
switch (imageRead.PixelFormat)
{
Expand Down
16 changes: 11 additions & 5 deletions OpenKh.Imaging/PngImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace OpenKh.Imaging
{
public class PngImage : IImageRead
public class PngImage : IImage
{
private byte[] _data;
private byte[] _clut;
Expand Down Expand Up @@ -150,6 +150,7 @@ public PngImage(Stream stream)
if (bits == 4 && colorType == ColorType.Indexed)
{
PixelFormat = PixelFormat.Indexed4;
ClutFormat = PixelFormat.Rgba8888;
var stride = (1 + Size.Width) / 2;
_data = new byte[stride * Size.Height];
for (int y = 0; y < Size.Height; y++)
Expand All @@ -163,6 +164,7 @@ public PngImage(Stream stream)
else if (bits == 8 && colorType == ColorType.Indexed)
{
PixelFormat = PixelFormat.Indexed8;
ClutFormat = PixelFormat.Rgba8888;
var stride = Size.Width;
_data = new byte[stride * Size.Height];
for (int y = 0; y < Size.Height; y++)
Expand All @@ -176,6 +178,7 @@ public PngImage(Stream stream)
else if (bits == 8 && colorType == ColorType.TrueColor)
{
PixelFormat = PixelFormat.Rgb888;
ClutFormat = PixelFormat.Undefined;
var stride = 3 * Size.Width;
_data = new byte[stride * Size.Height];
for (int y = 0; y < Size.Height; y++)
Expand Down Expand Up @@ -204,6 +207,7 @@ public PngImage(Stream stream)
else if (bits == 8 && colorType == ColorType.AlphaTrueColor)
{
PixelFormat = PixelFormat.Rgba8888;
ClutFormat = PixelFormat.Undefined;
var stride = 4 * Size.Width;
_data = new byte[stride * Size.Height];
for (int y = 0; y < Size.Height; y++)
Expand Down Expand Up @@ -256,7 +260,7 @@ private void ApplyFilter(byte[] data, int ptr, int pixelSize, int stride, int fi
}
}
else if (filter == 2)
{
{
if (ptr != 0)
{
var endPtr = ptr + stride;
Expand Down Expand Up @@ -333,7 +337,7 @@ private byte[] PrepareClut(byte[] PLTE, byte[] tRNS, int count)
clut[4 * y + 1] = PLTE[3 * y + 1];
clut[4 * y + 2] = PLTE[3 * y + 2];
}
clut[4 * y + 3] = 255;
clut[4 * y + 3] = 255;
if (y + 1 <= tRNS?.Length)
{
clut[4 * y + 3] = tRNS[y];
Expand Down Expand Up @@ -380,15 +384,17 @@ public static bool IsValid(Stream stream)
public Size Size { get; internal set; }

public PixelFormat PixelFormat { get; internal set; }

public PixelFormat ClutFormat { get; internal set; }

public byte[] GetData() => _data;

public byte[] GetClut() => _clut;
public byte[] GetClut() => _clut;
#endregion

public void Write(Stream stream) => Write(stream, this);

public static void Write(Stream stream, IImageRead bitmap)
public static void Write(Stream stream, IImage bitmap)
{
BinaryMapping.WriteObject(stream, new Signature { Magic = Signature.Valid });
byte bits;
Expand Down
2 changes: 1 addition & 1 deletion OpenKh.Imaging/SimpleImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace OpenKh.Imaging
{
public class SimpleImage : IImageRead
public class SimpleImage : IImage
{
public Size Size { get; private set; }
public int Width { get => Size.Width; }
Expand Down
Loading