Skip to content

Commit

Permalink
* Edge Bleeding on Non POT Images
Browse files Browse the repository at this point in the history
* Import/Export Color/Flags Metadata File (Text)
* Allow Changing Default Theme (default_body_lz.bin on Root)
* Fade-Out Non-Provided Image Drop-Zones
  • Loading branch information
usagirei committed May 27, 2017
1 parent 81d55b5 commit 4539c39
Show file tree
Hide file tree
Showing 9 changed files with 448 additions and 100 deletions.
6 changes: 3 additions & 3 deletions SharedAssemblyInfo.cs
Expand Up @@ -12,7 +12,7 @@
// [assembly: AssemblyVersion("1.0.*")]

// Shared (File) Version
[assembly: AssemblyFileVersion("1.0.11.0")]
[assembly: AssemblyFileVersion("1.0.12.0")]
[assembly: AssemblyCompany("Usagirei")]
[assembly: AssemblyCopyright("Copyright © Usagirei 2016-2017")]
[assembly: AssemblyCulture("")]
Expand All @@ -25,10 +25,10 @@

// ThemeEditor.Common
#if COMMON
[assembly: AssemblyVersion("1.0.11.0")]
[assembly: AssemblyVersion("1.0.12.0")]
#endif

// ThemeEditor.WPF
#if WPF_EDITOR
[assembly: AssemblyVersion("1.0.11.0")]
[assembly: AssemblyVersion("1.0.12.0")]
#endif
92 changes: 73 additions & 19 deletions ThemeEditor.Common/Graphics/RawTexture.cs
Expand Up @@ -27,7 +27,7 @@ public RawTexture(int width, int height, DataFormat format)
{
Width = width;
Height = height;
var tgtSize = (int) format * width * height;
var tgtSize = (int)format * width * height;
Format = format;
Data = new byte[tgtSize];
}
Expand Down Expand Up @@ -101,6 +101,7 @@ private static int GreatestCommonMultiple(int num, int mult)
return gcm;
}


private static int NextLargestPowerOfTwo(int x)
{
x--;
Expand Down Expand Up @@ -132,7 +133,7 @@ public void Encode(byte[] bgrData, int width, int height, DataFormat format)
{
Width = width;
Height = height;
var tgtSize = (int) format * width * height;
var tgtSize = (int)format * width * height;
Format = format;
Data = new byte[tgtSize];
}
Expand All @@ -157,6 +158,59 @@ public void Encode(byte[] bgrData)
}
}

public void EdgeBleed(int x, int y, int sx, int sy)
{
byte[] bgrData = Decode();
int eSize = 3; // BGR Data

void EdgeBleedY(int r, int y0, int y1)
{
if (y0 > y1)
{
int t = y1;
y1 = y0;
y0 = t;
}

int dSize = Width * 3;
byte[] line = new byte[dSize];
Buffer.BlockCopy(bgrData, r * dSize, line, 0, dSize);
for (int i = y0; i <= y1; i++)
Buffer.BlockCopy(line, 0, bgrData, i * dSize, dSize);
}

void EdgeBleedX(int r, int x0, int x1)
{
byte[] bd = bgrData;
int es = eSize;
int w = Width;

if (x0 > x1)
{
int t = x1;
x1 = x0;
x0 = t;
}

int dSize = Height * eSize;
byte[] line = new byte[dSize];
for (int j = 0; j < Height; j++)
Buffer.BlockCopy(bd, (j * w + r) * es, line, j * es, es);

for (int i = x0; i <= x1; i++)
for (int j = 0; j < Height; j++)
Buffer.BlockCopy(line, j * es, bd, (j * w + i) * es, es);

}

EdgeBleedX(x, 0, x);
EdgeBleedX(x + sx - 1, x + sx - 1, Width - 1);
EdgeBleedY(y, 0, y);
EdgeBleedY(y + sy - 1, y + sy - 1, Height - 1);

Encode(bgrData);
}

public void Read(Stream s)
{
if (s.Length - s.Position < Data.Length)
Expand Down Expand Up @@ -185,8 +239,8 @@ private byte[] Decode_A8()
DecToCoord(i % 64, out x, out y);
uint tile = i / 64;

x += (uint) (tile % p) * 8;
y += (uint) (tile / p) * 8;
x += (uint)(tile % p) * 8;
y += (uint)(tile / p) * 8;

var idx = 3 * (y * Width + x);

Expand All @@ -210,16 +264,16 @@ private byte[] Decode_Bgr565()
{
int px = (Data[i + 1] << 8 | Data[i]);

byte r = (byte) (((px >> 11) & 0x1f) << 3); // 5
byte g = (byte) (((px >> 5) & 0x3f) << 2); // 6
byte b = (byte) (((px >> 0) & 0x1f) << 3); // 5
byte r = (byte)(((px >> 11) & 0x1f) << 3); // 5
byte g = (byte)(((px >> 5) & 0x3f) << 2); // 6
byte b = (byte)(((px >> 0) & 0x1f) << 3); // 5

uint x, y;
DecToCoord(j % 64, out x, out y);
uint tile = j / 64;

x += (uint) (tile % p) * 8;
y += (uint) (tile / p) * 8;
x += (uint)(tile % p) * 8;
y += (uint)(tile / p) * 8;

var idx = 3 * (y * Width + x);

Expand Down Expand Up @@ -249,8 +303,8 @@ private byte[] Decode_Bgr888()
DecToCoord(j % 64, out x, out y);
uint tile = j / 64;

x += (uint) (tile % p) * 8;
y += (uint) (tile / p) * 8;
x += (uint)(tile % p) * 8;
y += (uint)(tile / p) * 8;

var idx = 3 * (y * Width + x);

Expand Down Expand Up @@ -283,8 +337,8 @@ private void Encode_A8(byte[] bgrData)
DecToCoord(i % 64, out x, out y);
uint tile = i / 64;

x += (uint) (tile % p) * 8;
y += (uint) (tile / p) * 8;
x += (uint)(tile % p) * 8;
y += (uint)(tile / p) * 8;

var idx = 3 * (y * Width + x);
var g = gscData[idx + 0];
Expand All @@ -311,8 +365,8 @@ private void Encode_Rgb565(byte[] bgrData)
DecToCoord(j % 64, out x, out y);
uint tile = j / 64;

x += (uint) (tile % p) * 8;
y += (uint) (tile / p) * 8;
x += (uint)(tile % p) * 8;
y += (uint)(tile / p) * 8;

var idx = 3 * (y * Width + x);

Expand All @@ -322,8 +376,8 @@ private void Encode_Rgb565(byte[] bgrData)

int px = (r << 11 | g << 5 | b) & 0xffff;

Data[k + 0] = (byte) (px >> 0 & 0xFF);
Data[k + 1] = (byte) (px >> 8 & 0xFF);
Data[k + 0] = (byte)(px >> 0 & 0xFF);
Data[k + 1] = (byte)(px >> 8 & 0xFF);
}
}

Expand All @@ -342,8 +396,8 @@ private void Encode_Rgb888(byte[] bgrData)
DecToCoord(j % 64, out x, out y);
uint tile = j / 64;

x += (uint) (tile % p) * 8;
y += (uint) (tile / p) * 8;
x += (uint)(tile % p) * 8;
y += (uint)(tile / p) * 8;

var idx = 3 * (y * Width + x);

Expand Down
Binary file modified ThemeEditor.WPF/Effects/FxBin/WarpEffect.ps
Binary file not shown.
122 changes: 117 additions & 5 deletions ThemeEditor.WPF/Extensions.cs
Expand Up @@ -6,13 +6,16 @@
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using ThemeEditor.Common.Graphics;
using ThemeEditor.WPF.Themes;
using Color = System.Windows.Media.Color;
using PixelFormat = System.Drawing.Imaging.PixelFormat;

Expand Down Expand Up @@ -151,10 +154,10 @@ public static Color Blend(this Color bg, Color fg, float factor)

float newAlpha = alphaFg + alphaBg * (1 - alphaFg);

byte newR = (byte) ((preRfg + preRbg * (1 - alphaFg)) / newAlpha).Clamp(0, 255);
byte newG = (byte) ((preGfg + preGbg * (1 - alphaFg)) / newAlpha).Clamp(0, 255);
byte newB = (byte) ((preBfg + preBbg * (1 - alphaFg)) / newAlpha).Clamp(0, 255);
byte newA = (byte) (newAlpha * 255).Clamp(0, 255);
byte newR = (byte)((preRfg + preRbg * (1 - alphaFg)) / newAlpha).Clamp(0, 255);
byte newG = (byte)((preGfg + preGbg * (1 - alphaFg)) / newAlpha).Clamp(0, 255);
byte newB = (byte)((preBfg + preBbg * (1 - alphaFg)) / newAlpha).Clamp(0, 255);
byte newA = (byte)(newAlpha * 255).Clamp(0, 255);

return Color.FromArgb(newA, newR, newG, newB);
}
Expand Down Expand Up @@ -194,7 +197,7 @@ public static byte[] GetBgr24Data(this BitmapSource bmp)
var dict = new Dictionary<string, object>();
foreach (DictionaryEntry res in reader)
{
var path = (string) res.Key;
var path = (string)res.Key;
if (Regex.IsMatch(path, pattern))
dict.Add(path, res.Value);
}
Expand All @@ -220,5 +223,114 @@ public static Color ToMediaColor(this ColorArgb8888 c)
{
return Color.FromArgb(c.A, c.R, c.G, c.B);
}


public static Dictionary<string, string> GetMetadata(this ViewModelBase arg, params string[] basePath)
{
Dictionary<string, string> meta = new Dictionary<string, string>();
Stack<string> pathStack = new Stack<string>(basePath);
void GetMetadataInner(ViewModelBase ivm)
{
var props = ivm.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
foreach (var p in props)
{
object value = p.GetValue(ivm);
if (value is ViewModelBase)
{
pathStack.Push(p.Name);
GetMetadataInner((ViewModelBase)value);
pathStack.Pop();
}
else if(p.CanRead && p.CanWrite)
{
var pathStr = string.Join(".", pathStack.Reverse()) + "." + p.Name;
if (value is Color || value is Boolean || value is Enum || value is double)
{
meta.Add(pathStr, Convert.ToString(value, CultureInfo.InvariantCulture));

}
}
}
}

foreach (string p in basePath)
{
var type = arg.GetType();
var info = type.GetProperty(p);
if (!typeof(ViewModelBase).IsAssignableFrom(info.PropertyType))
return meta;
arg = (ViewModelBase)info.GetValue(arg);
}
GetMetadataInner(arg);
return meta;
}

public static void SetMetadata(this ViewModelBase arg, Dictionary<string, string> metadata, params string[] basePath)
{

void SetMetadataInner(string path, string value)
{
object cur = arg;
var parts = path.Split('.');
for (var i = 0; i < parts.Length; i++)
{
var part = parts[i];
var type = cur.GetType();
var info = type.GetProperty(part, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
if (info == null)
return;
if (i == parts.Length - 1)
{
try
{
if (info.PropertyType == typeof(Color))
{

var col = System.Windows.Media.ColorConverter.ConvertFromString(value);
info.SetValue(cur, col);

}
else if (info.PropertyType == typeof(double))
{
if (double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var res))
info.SetValue(cur, res);
}
else if (info.PropertyType == typeof(bool))
{
if (bool.TryParse(value, out var res))
info.SetValue(cur, res);
}
else if (info.PropertyType.IsEnum)
{
if (!Enum.IsDefined(info.PropertyType, value))
return;
info.SetValue(cur, Enum.Parse(info.PropertyType, value));
}
}
catch (Exception ex)
{
if (!(ex is FormatException) &&
!(ex is ArgumentException))
throw;
}

}
else
{
cur = info.GetValue(cur);
}
}
}

var append = string.Join(".", basePath);
if (append.Length > 0)
append += ".";

foreach (var pair in metadata)
{
SetMetadataInner(append + pair.Key, pair.Value);
}
}

}
}

0 comments on commit 4539c39

Please sign in to comment.