Ejemplo n.º 1
0
        private static void Import(BinaryFormat input, string pngPath, string output, ColorFormat format)
        {
            MmTex texture;

            if (format == ColorFormat.Indexed_A5I3)
            {
                texture = input.ConvertWith <Binary2MmTex, BinaryFormat, MmTex>();
            }
            else
            {
                texture = input.ConvertWith <Binary2MmTexA3, BinaryFormat, MmTex>();
            }

            input.Dispose();

            // Import the new PNG file
            Bitmap newImage     = (Bitmap)Image.FromFile(pngPath);
            var    quantization = new FixedPaletteQuantization(texture.Palette.GetPalette(0));

            Texim.Media.Image.ImageConverter importer = new Texim.Media.Image.ImageConverter
            {
                Format        = format,
                PixelEncoding = PixelEncoding.Lineal,
                Quantization  = quantization
            };
            (Palette _, PixelArray pixelInfo) = importer.Convert(newImage);
            texture.Pixels = pixelInfo;

            // Save the texture
            texture.ConvertWith <Binary2MmTex, MmTex, BinaryFormat>()
            .Stream.WriteTo(output);
        }
Ejemplo n.º 2
0
        private static void ImportDig(Node nDIG, Node nATM, Node nPNG, string outputPath)
        {
            log.Info("Importing " + nDIG.Name + ", " + nATM.Name + " and " + nPNG.Name);

            DIG  originalDig = nDIG.TransformWith <Binary2DIG>().GetFormatAs <DIG>();
            ALMT originalAtm = nATM.TransformWith <Binary2ALMT>().GetFormatAs <ALMT>();

            // New stream
            var stream     = new MemoryStream();
            var dataStream = DataStreamFactory.FromStreamKeepingOwnership(stream, 0, 0);

            // Copy the png to the bitmap stream
            nPNG.Stream.WriteTo(dataStream);

            // Import the new PNG file
            Bitmap newImage = (Bitmap)Image.FromStream(stream);

            var         quantization = new FixedPaletteQuantization(originalDig.Palette.GetPalette(0));
            ColorFormat format;

            if (originalDig.PaletteType == 16)
            {
                format = ColorFormat.Indexed_4bpp;
            }
            else
            {
                format = ColorFormat.Indexed_8bpp;
            }

            Texim.ImageMapConverter importer = new Texim.ImageMapConverter
            {
                Format        = format,
                PixelEncoding = PixelEncoding.HorizontalTiles,
                Quantization  = quantization,
                //Mapable = new MatchMapping(originalDig.Pixels.GetPixels())
            };

            (Palette _, PixelArray pixelInfo, MapInfo[] mapInfos) = importer.Convert(newImage);

            originalDig.Pixels = pixelInfo;
            originalAtm.Info   = mapInfos;

            BinaryFormat bDig = (BinaryFormat)ConvertFormat.With <Binary2DIG>(originalDig);
            BinaryFormat bAtm = (BinaryFormat)ConvertFormat.With <Binary2ALMT>(originalAtm);

            Utils.Lzss(bDig, "-evn").Stream.WriteTo(Path.Combine(outputPath, nDIG.Name + ".evn.dig"));
            bAtm.Stream.WriteTo(Path.Combine(outputPath, nATM.Name + ".atm"));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Generate pixels, palette and update objects in frames.
        /// </summary>
        /// <param name="pixels">Pixels of frames.</param>
        /// <param name="palettes">Palettes of frames.</param>
        private void CreateData(out Pixel[] pixelsLin, out Pixel[] pixelsHori, out Color[][] palettes)
        {
            int tileSize     = 128;
            int fullTileSize = (this.PaletteMode == PaletteMode.Palette16_16) ? tileSize * 2 : tileSize;
            int maxColors    = this.Format.MaxColors();
            int numPalettes  = (maxColors <= 16) ? 16 : 1;

            // Create the ObjData. Quantizate images.
            List <Color[]>    palettesList = new List <Color[]>();
            List <ObjectData> data         = new List <ObjectData>();

            foreach (Tuple <Frame, EmguImage> frame in this.frameData)
            {
                EmguImage frameImg = frame.Item2;
                Obj[]     objects  = frame.Item1.GetObjects();

                foreach (Obj obj in objects)
                {
                    ObjectData objData = new ObjectData();
                    objData.Object = obj;

                    Rectangle area = obj.GetArea();
                    area.Offset(256, 128);
                    objData.Image = frameImg.Copy(area);

                    // Update object
                    objData.Object.Mode        = this.ObjectMode;
                    objData.Object.PaletteMode = this.PaletteMode;

                    // Quantizate
                    this.Quantization.Quantizate(objData.Image);
                    objData.PixelsLineal     = this.Quantization.GetPixels(PixelEncoding.Lineal);
                    objData.PixelsHorizontal = this.Quantization.GetPixels(PixelEncoding.HorizontalTiles);
                    objData.Palette          = this.Quantization.Palette;
                    if (objData.Palette.Length > maxColors)
                    {
                        throw new FormatException(string.Format("The image has more than {0} colors", maxColors));
                    }

                    ManyFixedPaletteQuantization manyFixed = this.Quantization as ManyFixedPaletteQuantization;
                    if (this.OriginalPalettes != null && manyFixed != null)
                    {
                        objData.Object.PaletteIndex = (byte)manyFixed.SelectedPalette;
                    }

                    palettesList.Add(objData.Palette);

                    data.Add(objData);
                }
            }

            // Reduce palettes if necessary
            if (this.OriginalPalettes == null)
            {
                this.Reducer.Clear();
                this.Reducer.MaxColors = maxColors;
                this.Reducer.AddPaletteRange(palettesList.ToArray());
                this.Reducer.Reduce(numPalettes);
                palettes = this.Reducer.ReducedPalettes;

                // Approximate palettes removed
                for (int i = 0; i < data.Count; i++)
                {
                    int paletteIdx = this.Reducer.PaletteApproximation[i];
                    if (paletteIdx >= 0)
                    {
                        // Quantizate again the image with the new palette
                        Color[] newPalette = palettes[paletteIdx];
                        FixedPaletteQuantization quantization = new FixedPaletteQuantization(newPalette);
                        quantization.Quantizate(data[i].Image);

                        // Get the pixel
                        data[i].PixelsLineal     = quantization.GetPixels(PixelEncoding.Lineal);
                        data[i].PixelsHorizontal = quantization.GetPixels(PixelEncoding.HorizontalTiles);
                    }
                    else
                    {
                        paletteIdx = (paletteIdx + 1) * -1;
                    }

                    // Update object
                    data[i].Object.PaletteIndex = (byte)paletteIdx;
                }
            }
            else
            {
                if (this.OriginalPalettes.Length > numPalettes)
                {
                    throw new FormatException("More original palettes than allowed");
                }

                palettes = this.OriginalPalettes;
            }

            List <Pixel> pixelLinList  = new List <Pixel>();
            List <Pixel> pixelHoriList = new List <Pixel>();

            for (int i = 0; i < data.Count; i++)
            {
                // Search the data into the array to ensure there is no duplicated tiles
                int tileNumber = SearchTile(pixelLinList, data[i].PixelsLineal, tileSize);
                data[i].Object.TileNumber = (ushort)tileNumber;

                // Add pixels to the list
                if (tileNumber == -1)
                {
                    data[i].Object.TileNumber = (ushort)(pixelLinList.Count / tileSize);
                    pixelLinList.AddRange(data[i].PixelsLineal);
                    pixelHoriList.AddRange(data[i].PixelsHorizontal);

                    // Pad to tilesize to increment at least one tilenumber next time
                    while (pixelLinList.Count % fullTileSize != 0)
                    {
                        pixelLinList.Add(new Pixel(0, 0, true));
                    }

                    while (pixelHoriList.Count % fullTileSize != 0)
                    {
                        pixelHoriList.Add(new Pixel(0, 0, true));
                    }
                }
            }

            pixelsLin  = pixelLinList.ToArray();
            pixelsHori = pixelHoriList.ToArray();
        }
Ejemplo n.º 4
0
        private static void Import(string format, Node n, string dirToSave, string dataToInsert)
        {
            log.Info("Importing...");

            switch (format)
            {
            case FORMATPREFIX + "BinInfoTitle":

                Po2BinInfoTitle p2b = new Po2BinInfoTitle()
                {
                    OriginalFile = new Yarhl.IO.DataReader(n.Stream)
                    {
                        DefaultEncoding = Encoding.GetEncoding(932)
                    }
                };
                Node nodePo = NodeFactory.FromFile(dataToInsert);

                nodePo.TransformWith <Po2Binary>();
                Node nodeBin = nodePo.TransformWith(p2b)
                               .TransformWith <BinInfoTitle2Bin>();
                nodeBin.Stream.WriteTo(Path.Combine(dirToSave, n.Name.Remove(n.Name.Length - 4) + "_new.bin"));

                break;

            case FORMATPREFIX + "ALAR.ALAR3":

                // Alar original
                Node original = n.TransformWith <BinaryFormat2Alar3>();

                // Contenedor con los ficheros a insertar
                Node newContainer = NodeFactory.FromDirectory(dataToInsert, "*.*");
                foreach (var child in newContainer.Children)
                {
                    log.Info("Importing " + child.Name);
                }
                //var newAlar = newContainer.Transform<Alar3ToNodes, NodeContainerFormat, ALAR3>();

                // Modificamos el Alar original con los nuevos ficheros a insertar
                original.GetFormatAs <ALAR3>().InsertModification(newContainer);

                original.TransformWith <BinaryFormat2Alar3>()
                .Stream.WriteTo(Path.Combine(dirToSave, n.Name + "new.aar"));

                break;

            case FORMATPREFIX + "DIG":

                DIG originalDig = n.TransformWith <Binary2DIG>().GetFormatAs <DIG>();

                // Import the new PNG file
                Bitmap newImage               = (Bitmap)Image.FromFile(dataToInsert);
                var    quantization           = new FixedPaletteQuantization(originalDig.Palette.GetPalette(0));
                Texim.ImageConverter importer = new Texim.ImageConverter {
                    Format        = ColorFormat.Indexed_4bpp,
                    PixelEncoding = PixelEncoding.HorizontalTiles,
                    Quantization  = quantization
                };

                (Palette _, PixelArray pixelInfo) = importer.Convert(newImage);

                originalDig.Pixels = pixelInfo;

                BinaryFormat b = (BinaryFormat)ConvertFormat.With <Binary2DIG>(originalDig);

                Utils.Lzss(b, "-evn").Stream.WriteTo(Path.Combine(dirToSave, n.Name + "evn.dig"));

                break;
            }
            log.Info("Finished importing");
        }