Skip to content
This repository has been archived by the owner on Mar 25, 2023. It is now read-only.

Commit

Permalink
Merge branch 'NitroTexture'
Browse files Browse the repository at this point in the history
Yeah! :D
  • Loading branch information
pleonex committed Nov 6, 2014
2 parents e48bb7a + 387bb46 commit 3c979fc
Show file tree
Hide file tree
Showing 23 changed files with 1,873 additions and 505 deletions.
1 change: 1 addition & 0 deletions ninoimager.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ Global
$1.Text = @-----------------------------------------------------------------------\n<copyright file="${FileName}" company="none">\nCopyright (C) ${Year} \n\n This program is free software: you can redistribute it and/or modify\n it under the terms of the GNU General Public License as published by \n the Free Software Foundation, either version 3 of the License, or\n (at your option) any later version.\n\n This program is distributed in the hope that it will be useful, \n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU General Public License for more details. \n\n You should have received a copy of the GNU General Public License\n along with this program. If not, see "http://www.gnu.org/licenses/". \n</copyright>\n<author>pleoNeX</author>\n<email>benito356@gmail.com</email>\n<date>${Date}</date>\n-----------------------------------------------------------------------
$1.IncludeInNewFiles = True
version = 0.3
version = 0.4
EndGlobalSection
EndGlobal
101 changes: 83 additions & 18 deletions ninoimager/BackgroundImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Ninoimager.Format;
using Ninoimager.ImageProcessing;
using Size = System.Drawing.Size;
using Rectangle = System.Drawing.Rectangle;
using Color = Emgu.CV.Structure.Bgra;
using LabColor = Emgu.CV.Structure.Lab;
using EmguImage = Emgu.CV.Image<Emgu.CV.Structure.Bgra, System.Byte>;
Expand All @@ -47,16 +49,16 @@ public BackgroundImporter()
+ BG Mode will be "Text" (most used)
+ Transparent color will be magenta: (R:248, G:0, B:248)
*/
this.IncludePcmp = false;
this.IncludeCpos = false;
this.DispCnt = 0;
this.UnknownChar = 0;
this.BgMode = BgMode.Text;
this.Format = ColorFormat.Indexed_8bpp;
this.TileSize = new Size(8, 8);
this.PartialImage = false;
this.IncludePcmp = false;
this.IncludeCpos = false;
this.DispCnt = 0;
this.UnknownChar = 0;
this.BgMode = BgMode.Text;
this.Format = ColorFormat.Indexed_8bpp;
this.TileSize = new Size(8, 8);
this.PixelEncoding = PixelEncoding.HorizontalTiles;

this.Quantization = new NdsQuantization();
this.Quantization = new NdsQuantization();
}

#region Importer parameters
Expand Down Expand Up @@ -114,6 +116,11 @@ public PaletteMode PaletteMode {
get;
set;
}

public bool PartialImage {
get;
set;
}
#endregion

public void SetOriginalSettings(Stream mapStr, Stream imgStr, Stream palStr)
Expand Down Expand Up @@ -164,13 +171,38 @@ public void ImportBackground(EmguImage newImg, Stream mapStr, Stream imgStr, Str
int height = newImg.Height;
int maxColors = 1 << this.Format.Bpp();

// Quantizate image -> get pixels and palette
this.Quantization.Quantizate(newImg);
Pixel[] pixels = this.Quantization.GetPixels(this.PixelEncoding);
Color[] palette = this.Quantization.Palette;
if (palette.Length > maxColors)
throw new FormatException(string.Format("The image has more than {0} colors", maxColors));
Pixel[] pixels;
Color[] palette;
List<int> mapPalette = new List<int>();
bool is16ColFixed = (Format == ColorFormat.Indexed_4bpp) &&
(PaletteMode == PaletteMode.Palette16_16) && (Quantization is FixedPaletteQuantization);
if (!is16ColFixed) {
// Quantizate image -> get pixels and palette
this.Quantization.Quantizate(newImg);
pixels = this.Quantization.GetPixels(this.PixelEncoding);
palette = this.Quantization.Palette;
if (palette.Length > maxColors)
throw new FormatException(string.Format("The image has more than {0} colors", maxColors));
} else {
Console.Write("(16 Color fixed!) ");
palette = this.Quantization.Palette;
ManyFixedPaletteQuantization quant = new ManyFixedPaletteQuantization(
this.Quantization.Palette.Split(16).ToArray());

List<Pixel> pixelList = new List<Pixel>();
for (int y = 0; y < newImg.Height; y += this.TileSize.Height) {
for (int x = 0; x < newImg.Width; x += this.TileSize.Width) {
Rectangle subArea = new Rectangle(x, y, this.TileSize.Width, this.TileSize.Height);
EmguImage subImage = newImg.Copy(subArea);
quant.Quantizate(subImage);
mapPalette.Add(quant.SelectedPalette);
pixelList.AddRange(quant.GetPixels(PixelEncoding.Lineal));
}
}

pixels = pixelList.ToArray();
}

// Create palette format
Nclr nclr = new Nclr() {
Extended = this.ExtendedPalette
Expand All @@ -185,9 +217,42 @@ public void ImportBackground(EmguImage newImg, Stream mapStr, Stream imgStr, Str
BgMode = this.BgMode,
PaletteMode = this.PaletteMode
};
nscr.PaletteMode = (this.Format == ColorFormat.Indexed_4bpp) ?
PaletteMode.Palette16_16 : PaletteMode.Palette256_1;
pixels = nscr.CreateMap(pixels);

if (!is16ColFixed) {
nscr.PaletteMode = (this.Format == ColorFormat.Indexed_4bpp) ?
PaletteMode.Palette16_16 : PaletteMode.Palette256_1;
pixels = nscr.CreateMap(pixels);
} else {
nscr.PaletteMode = PaletteMode.Palette16_16;
pixels = nscr.CreateMap(pixels, mapPalette.ToArray());
}

if (this.PartialImage) {
// As the image won't expand to all the screen,
// The first tile must be transparent
int tilesizeLength = this.TileSize.Width * this.TileSize.Height;
Pixel[] newPixels = new Pixel[pixels.Length + tilesizeLength];

// New transparent pixels
for (int i = 0; i < tilesizeLength; i++)
newPixels[i] = new Pixel(0, 255, true);

// Image pixels
Array.Copy(pixels, 0, newPixels, tilesizeLength, pixels.Length);
pixels = newPixels;

// Update map info
MapInfo[] mapInfo = nscr.GetMapInfo();
for (int i = 0; i < mapInfo.Length; i++) {
mapInfo[i] = new MapInfo(
mapInfo[i].TileIndex + 1,
mapInfo[i].PaletteIndex,
mapInfo[i].FlipX,
mapInfo[i].FlipY);
}
nscr.SetMapInfo(mapInfo);
}


// Create image format
Ncgr ncgr = new Ncgr() {
Expand Down
39 changes: 39 additions & 0 deletions ninoimager/Extensions/ArrayExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// -----------------------------------------------------------------------
// <copyright file="ArrayExtension.cs" company="none">
// Copyright (C) 2014
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see "http://www.gnu.org/licenses/".
// </copyright>
// <author>pleoNeX</author>
// <email>benito356@gmail.com</email>
// <date>17/08/2014</date>
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;

namespace Ninoimager
{
public static class ArrayExtension
{
public static T[][] Split<T>(this T[] array, int size)
{
List<T[]> splitted = new List<T[]>();
for (int i = 0; i < array.Length; i += size)
splitted.Add(array.Skip(i).Take(size).ToArray());
return splitted.ToArray();
}
}
}

18 changes: 13 additions & 5 deletions ninoimager/Extensions/ColorExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ public static int CompareTo(this Color c1, Color c2)
/// <param name="c2">C2.</param>
public static double GetDistance(this LabColor c1, LabColor c2)
{
return Math.Sqrt(
(c2.X - c1.X) * (c2.X - c1.X) +
(c2.Y - c1.Y) * (c2.Y - c1.Y) +
(c2.Z - c1.Z) * (c2.Z - c1.Z)
);
return Math.Sqrt(c1.GetDistanceSquared(c2));
}

public static double GetDistanceSquared(this LabColor c1, LabColor c2)
Expand All @@ -62,6 +58,18 @@ public static double GetDistanceSquared(this LabColor c1, LabColor c2)
(c2.Z - c1.Z) * (c2.Z - c1.Z);
}

public static double GetDistance(this Color c1, Color c2)
{
return Math.Sqrt(c1.GetDistanceSquared(c2));
}

public static double GetDistanceSquared(this Color c1, Color c2)
{
return (c2.Red - c1.Red) * (c2.Red - c1.Red) +
(c2.Green - c1.Green) * (c2.Green - c1.Green) +
(c2.Blue - c1.Blue) * (c2.Blue - c1.Blue);
}

public static Color[] ToArgbColors(this uint[] argb)
{
Color[] colors = new Color[argb.Length];
Expand Down
Loading

0 comments on commit 3c979fc

Please sign in to comment.