コード例 #1
0
ファイル: Frame.cs プロジェクト: pleonex/ninoimager
        public EmguImage CreateBitmap(Image image, Palette palette)
        {
            EmguImage bitmap = new EmguImage(512, 256);
            List<Obj> sortedObjs = new List<Obj>(this.objects);
            sortedObjs.Sort((a, b) => b.GetPriority().CompareTo(a.GetPriority()));

            foreach (Obj obj in sortedObjs) {
                EmguImage objBitmap = obj.CreateBitmap(image, palette, this.tileSize);

                // Get first palette color
                Color transparent = palette.GetColor(obj.PaletteIndex, 0);

                // Set first palette color as transparent
                var mask = objBitmap.InRange(transparent, transparent);
                objBitmap.SetValue(0, mask);

                // Copy the object image to the frame
                Point position = obj.GetReferencePoint();
                position.Offset(256, 128);	// Only positive coordinate values
                bitmap.Overlay(position.X, position.Y, objBitmap);

                objBitmap.Dispose();
            }

            //Rectangle absArea = this.VisibleArea;
            //absArea.Offset(512, 128); // Only positive coordinate values
            //EmguImage roiBitmap = bitmap.Copy(absArea);
            //bitmap.Dispose();
            return bitmap;
        }
コード例 #2
0
ファイル: NpckFactory.cs プロジェクト: pleonex/ninoimager
        public static Npck ChangeTextureImages(EmguImage[] images, int[] frames, Palette palOut, Npck original)
        {
            // Get an original texture file to get images and the texture to modify
            long start = original[0].Position;

            Btx0 oriTexture = new Btx0(original[0]);
            original[0].Position = start;

            Btx0 newTexture = new Btx0(original[0]);
            original[0].Position = start;

            // Create importer and remove all images
            TextureImporter importer = new TextureImporter(newTexture);
            importer.RemoveImages();

            // Add images from arguments or original texture if not presents.
            List<int> frameList = frames.ToList();
            for (int i = 0; i < oriTexture.NumTextures; i++) {
                int idx = frameList.IndexOf(i);

                // Set quantization to original palette or argument palette
                Color[] originalPalette = oriTexture.GetPalette(i).GetPalette(0);
                if (idx != -1 && palOut != null)
                    importer.Quantization = new FixedPaletteQuantization(palOut.GetPalette(idx));
                else
                    importer.Quantization = new FixedPaletteQuantization(originalPalette);

                // Keep original color format and name
                string name = oriTexture.GetTextureName(i);
                importer.Format = oriTexture.GetImage(i).Format;

                // Keep original unknown values
                int[] texUnks = oriTexture.GetTextureUnknowns(i);
                int[] palUnks = oriTexture.GetPaletteUnknowns(i);

                // Import :D
                if (idx != -1)
                    importer.AddImage(images[idx], name, texUnks, palUnks, originalPalette);
                else
                    importer.AddImage(oriTexture.CreateBitmap(i), name, texUnks, palUnks);
            }

            // Write the new texture file
            MemoryStream textureStream = new MemoryStream();
            newTexture.Write(textureStream);
            textureStream.Position = 0;

            // Create a copy of the NPCK from the original
            Npck npck = new Npck();
            npck.AddSubfile(textureStream);
            for (int i = 1; i < 6; i++)
                npck.AddSubfile(original[i]);

            return npck;
        }
コード例 #3
0
ファイル: NpckFactory.cs プロジェクト: pleonex/ninoimager
        public static Npck ChangeTextureImages(string[] images, int[] frames, Palette palOut, Npck original)
        {
            // Creamos las imágenes
            EmguImage[] emguImages = new EmguImage[images.Length];
            for (int i = 0; i < images.Length; i++) {
                try {
                    emguImages[i] = new EmguImage(images[i]);
                } catch (Exception ex) {
                    throw new FormatException("Unknown exception with: " + images[i], ex);
                }
            }

            Npck npck = ChangeTextureImages(emguImages, frames, palOut, original);

            // Liberamos recursos
            foreach (EmguImage img in emguImages)
                img.Dispose();

            return npck;
        }
コード例 #4
0
ファイル: Map.cs プロジェクト: pleonex/ninoimager
        public EmguImage CreateBitmap(Image image, Palette palette)
        {
            // UNDONE: Support Text mode and pixel areas
            if (this.bgMode == BgMode.Text && (this.width > 256 || this.height > 256))
                throw new NotSupportedException("Text modes with multiple pixel ares not supported.");

            // TODO: Try to change the tile size of the image
            if (this.tileSize != image.TileSize)
                throw new FormatException("Image with different tile size");

            // TODO: Try to convert image to tiled
            if (!image.PixelEncoding.IsTiled())
                throw new FormatException("Image not tiled.");

            Pixel[] mapImage = new Pixel[this.width * this.height];
            uint[] tmpIndex = new uint[this.width * this.height];

            int count = 0;
            foreach (MapInfo info in this.info) {
                Pixel[] tile = image.GetTile(info.TileIndex);
                if (info.FlipX)
                    tile.FlipX(this.tileSize);
                if (info.FlipY)
                    tile.FlipY(this.tileSize);

                tile.CopyTo(mapImage, count);

                for (int i = 0; i < tile.Length; i++)
                    tmpIndex[count + i] = (uint)info.PaletteIndex;

                count += tile.Length;
            }

            // Palette Index must be lineal but it's tiled, convert it.
            uint[] palIndex = new uint[this.width * this.height];
            image.PixelEncoding.Codec(tmpIndex, palIndex, true, this.width, this.height, image.TileSize);

            Image finalImg = new Image(
                mapImage,
                this.width,
                this.height,
                image.PixelEncoding,
                image.Format,
                image.TileSize
            );
            return finalImg.CreateBitmap(palette, palIndex);
        }
コード例 #5
0
ファイル: Sprite.cs プロジェクト: pleonex/ninoimager
 public EmguImage CreateBitmap(int numFrame, Image image, Palette palette)
 {
     return this.frames[numFrame].CreateBitmap(image, palette);
 }
コード例 #6
0
ファイル: Obj.cs プロジェクト: pleonex/ninoimager
        public EmguImage CreateBitmap(Image image, Palette palette, int tileSize)
        {
            Size size = this.GetSize();
            Image objImage = image.CreateSubImage(this.TileNumber * tileSize, size);

            EmguImage bitmap = null;
            if (this.Mode == ObjMode.Bitmap)
                bitmap = objImage.CreateBitmap();	// TODO: Add alpha support
            else
                bitmap = objImage.CreateBitmap(palette, this.PaletteIndex);

            if (!this.RotSca && this.HorizontalFlip)
                bitmap = bitmap.Flip(Emgu.CV.CvEnum.FLIP.HORIZONTAL);
            if (!this.RotSca && this.VerticalFlip)
                bitmap = bitmap.Flip(Emgu.CV.CvEnum.FLIP.VERTICAL);

            return bitmap;
        }
コード例 #7
0
ファイル: Sprite.cs プロジェクト: MetalFan1988/ninoimager
 public EmguImage CreateBitmap(int numFrame, Image image, Palette palette)
 {
     return(this.frames[numFrame].CreateBitmap(image, palette));
 }
コード例 #8
0
        public EmguImage CreateBitmap(Image image, Palette palette)
        {
            // UNDONE: Support Text mode and pixel areas
            if (this.bgMode == BgMode.Text && (this.width > 256 || this.height > 256))
            {
                throw new NotSupportedException("Text modes with multiple pixel ares not supported.");
            }

            // TODO: Try to change the tile size of the image
            if (this.tileSize != image.TileSize)
            {
                throw new FormatException("Image with different tile size");
            }

            // TODO: Try to convert image to tiled
            if (!image.PixelEncoding.IsTiled())
            {
                throw new FormatException("Image not tiled.");
            }

            Pixel[] mapImage = new Pixel[this.width * this.height];
            uint[]  tmpIndex = new uint[this.width * this.height];

            int count = 0;

            foreach (MapInfo info in this.info)
            {
                Pixel[] tile = image.GetTile(info.TileIndex);
                if (info.FlipX)
                {
                    tile.FlipX(this.tileSize);
                }
                if (info.FlipY)
                {
                    tile.FlipY(this.tileSize);
                }

                tile.CopyTo(mapImage, count);

                for (int i = 0; i < tile.Length; i++)
                {
                    tmpIndex[count + i] = (uint)info.PaletteIndex;
                }

                count += tile.Length;
            }

            // Palette Index must be lineal but it's tiled, convert it.
            uint[] palIndex = new uint[this.width * this.height];
            image.PixelEncoding.Codec(tmpIndex, palIndex, true, this.width, this.height, image.TileSize);

            Image finalImg = new Image(
                mapImage,
                this.width,
                this.height,
                image.PixelEncoding,
                image.Format,
                image.TileSize
                );

            return(finalImg.CreateBitmap(palette, palIndex));
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: pleonex/ninoimager
        private static void MultiImport(string baseDir, string outputDir, 
			List<ImageInfo> importedList, string xml, bool filterDate)
        {
            if (string.IsNullOrEmpty(xml))
                return;

            XDocument doc = XDocument.Load(xml);
            foreach (XElement entry in doc.Root.Elements("Pack")) {
                // Get mode
                string mode = entry.Element("Mode").Value;

                // Get paths
                bool existImages = true;
                List<string> images   = new List<string>();
                List<ImageInfo> infos = new List<ImageInfo>();
                foreach (XElement ximg in entry.Element("Images").Elements("Image")) {
                    string frame = (ximg.Attribute("frame") != null) ?
                        "_" + ximg.Attribute("frame").Value : "" ;

                    ImageInfo info = new ImageInfo();
                    info.AbsoluteImage = Path.Combine(baseDir, ximg.Value) + frame + M + ".png";
                    info.Filename      = Path.GetFileNameWithoutExtension(ximg.Value);
                    info.PackExtension = (mode != "TextureWithPalette") ? ".n2d" : ".n3d";
                    info.RelativePath  = Path.GetDirectoryName(ximg.Value) + Path.DirectorySeparatorChar;
                    infos.Add(info);

                    images.Add(info.AbsoluteImage);

                    if (!File.Exists(info.AbsoluteImage)) {
                        existImages = false;
                        break;
                    }
                }

                // Import
                Console.Write("|-Importing {0,-45} | ", infos[0].RelativeImage);
                for (int i = 1; i < infos.Count; i++) {
                    Console.WriteLine();
                    Console.Write("            {0,-45} | ", infos[i].RelativeImage);
                }

                // Images still not translated
                if (!existImages) {
                    Console.WriteLine("Skipped: Images not found");
                    continue;
                }

                // If don't match the filter, skip
                if (filterDate) {
                    DateTime referenceDate = File.GetLastWriteTime(outputDir + infos[0].RelativeNewPack);
                    if (!images.Any(f => File.GetLastWriteTime(f) > referenceDate)) {
                        Console.WriteLine("Skipped: date filter");
                        continue;
                    }
                }

                Npck originalPack = new Npck(outputDir + infos[0].RelativePack);
                Npck[] packs = null;
                if (mode == "SharePalette")
                    packs = NpckFactory.FromBackgroundImageSharePalette(images.ToArray(), originalPack);
                else if (mode == "ChangePalette")
                    packs = new Npck[1] { NpckFactory.FromBackgroundImage(images[0], originalPack, true) };
                else if (mode == "ShareImage")
                    packs = NpckFactory.FromBackgroundImageShareImage(images.ToArray(), originalPack);
                else if (mode == "SharePaletteChangeDepth")
                    packs = NpckFactory.FromBackgroundImageSharePaletteChangeDepth(images.ToArray(), originalPack, true);
                else if (mode == "TextureWithPalette") {
                    // Get frames
                    string frame = entry.Element("Images").Elements("Image").First().Attribute("frame").Value;
                    List<int> frames = new List<int>() { Convert.ToInt32(frame) };

                    // Create palette
                    XElement[] xcolors = entry.Element("Palette").Elements("Color").ToArray();
                    Color[] colors = new Color[xcolors.Length];
                    for (int i = 0; i < colors.Length; i++) {
                        colors[i] = new Color();
                        colors[i].Red   = Convert.ToInt32(xcolors[i].Attribute("red").Value);
                        colors[i].Green = Convert.ToInt32(xcolors[i].Attribute("green").Value);
                        colors[i].Blue  = Convert.ToInt32(xcolors[i].Attribute("blue").Value);
                    }
                    Palette palette = new Palette(colors);

                    // Generate pack file
                    packs = new Npck[1];
                    packs[0] = NpckFactory.ChangeTextureImages(images.ToArray(), frames.ToArray(), palette, originalPack);
                } else
                    throw new FormatException(string.Format("Unsopported mode \"{0}\"", mode));

                // Write output
                originalPack.CloseAll();
                for (int i = 0; i < infos.Count; i++) {
                    packs[i].Write(outputDir + infos[i].RelativeNewPack);
                    packs[i].CloseAll();
                }

                importedList.AddRange(infos);
                Console.WriteLine("Successfully");
            }
        }
コード例 #10
0
ファイル: Program.cs プロジェクト: pleonex/ninoimager
        private static void ExportTextureWithColors(string n3dPath, string outPath, string colors)
        {
            string name = Path.GetFileNameWithoutExtension(n3dPath);
            string path = Path.Combine(outPath, name);

            // Get texture file
            Npck pack = new Npck(n3dPath);
            Btx0 texture = new Btx0(pack[0]);

            // Parse colors
            string[] strColors = colors.Split(' ');
            Color[] newColors = new Color[strColors.Length];
            for (int i = 0; i < strColors.Length; i++) {
                int hexColor = Convert.ToInt32(strColors[i], 16);
                newColors[i] = new Color();
                newColors[i].Alpha = 255;
                newColors[i].Red   = (hexColor >> 00) & 0xFF;
                newColors[i].Green = (hexColor >> 08) & 0xFF;
                newColors[i].Blue  = (hexColor >> 16) & 0xFF;
            }

            // Create and export palette
            Palette palette = new Palette(newColors);
            palette.ToWinPaletteFormat(path + "_palGimp.pal", 0, true);
            palette.ToWinPaletteFormat(path + "_pal.pal", 0, false);
            palette.CreateBitmap(0).Save(path + "_pal.png");

            // For each image, set new palette and export it
            for (int i = 0; i < texture.NumTextures; i++)
                texture.CreateBitmap(i, palette).Save(path + "_" + i.ToString() + ".png");
        }
コード例 #11
0
ファイル: Image.cs プロジェクト: pleonex/ninoimager
        public EmguImage CreateBitmap(Palette palette, uint[] paletteIndex)
        {
            if (!this.Format.IsIndexed()) {
                Console.WriteLine("##WARNING## The palette is not required.");
                return this.CreateBitmap();
            }

            EmguImage bmp = new EmguImage(this.width, this.height);
            bmp.SetPixels(
                0,
                0,
                this.width,
                this.height,
                InfoToIndexedColors(this.data, palette.GetPalettes(), paletteIndex)
            );

            return bmp;
        }
コード例 #12
0
ファイル: TextureImporter.cs プロジェクト: pleonex/ninoimager
        public void AddImage(EmguImage newImg, string name, Color[] palOut = null)
        {
            if (newImg == null)
                throw new ArgumentNullException();

            // First, let's be sure the dimension are power of 2.
            if (!IsPowerOfTwo((uint)newImg.Width)) {
                int width = 1 << (int)(Math.Log(newImg.Width, 2) + 1);
                EmguImage blankVertical = new EmguImage(width - newImg.Width, newImg.Height);
                newImg = newImg.ConcateHorizontal(blankVertical);
                Console.Write("(Warning: Width not power of 2) ");
            }

            if (!IsPowerOfTwo((uint)newImg.Height)) {
                int height = 1 << (int)(Math.Log(newImg.Height, 2) + 1);
                EmguImage blankHorizontal = new EmguImage(newImg.Width, height - newImg.Height);
                newImg = newImg.ConcateVertical(blankHorizontal);
                Console.Write("(Warning: Height not power of 2) ");
            }

            // Quantizate image -> get pixels and palette
            this.Quantization.Quantizate(newImg);
            Pixel[] pixels = this.Quantization.GetPixels(PixelEncoding.Lineal);
            Color[] colors = (palOut == null) ? this.Quantization.Palette : palOut;

            int maxColors = this.Format.MaxColors();
            if (colors.Length > maxColors)
                throw new FormatException(string.Format("The image has more than {0} colors", maxColors));

            // Create image format
            Image image = new Image();
            image.Width = newImg.Width;
            image.Height = newImg.Height;
            image.SetData(pixels, PixelEncoding.Lineal, this.Format, new Size(1, 1));

            // Create the palette
            Palette palette = new Palette(colors);

            // Add the image
            this.Texture.AddImage(image, palette, name);
        }