예제 #1
0
        private static void PatchImageImz(Context context, AssetFile assetFile, Stream stream)
        {
            var index  = 0;
            var images = Imgz.IsValid(stream) ? Imgz.Read(stream).ToList() : new List <Imgd>();

            foreach (var source in assetFile.Source)
            {
                if (source.Index > 0)
                {
                    index = source.Index;
                }

                var imd = CreateImageImd(context, source);
                if (images.Count <= index)
                {
                    images.Add(imd);
                }
                else
                {
                    images[index] = imd;
                }

                index++;
            }

            Imgz.Write(stream.SetPosition(0), images);
        }
예제 #2
0
파일: Program.cs 프로젝트: xorllc/OpenKh
            protected int OnExecute(CommandLineApplication app)
            {
                var inputFile = ImzFile;
                var outputDir = OutputDir ?? Environment.CurrentDirectory;

                using (var stream = File.OpenRead(inputFile))
                {
                    var pairs = Imgz.Read(stream).Select((imgd, index) => (imgd, index));

                    Directory.CreateDirectory(outputDir);

                    foreach (var(imgd, index) in pairs)
                    {
                        if (ExportImd)
                        {
                            var outputFile = Path.Combine(outputDir, $"{Path.GetFileNameWithoutExtension(inputFile)}-{1 + index}.imd");

                            var buffer = new MemoryStream();
                            imgd.Write(buffer);
                            File.WriteAllBytes(outputFile, buffer.ToArray());
                        }
                        else
                        {
                            var outputFile = Path.Combine(outputDir, $"{Path.GetFileNameWithoutExtension(inputFile)}-{1 + index}.png");

                            var bitmap = ImgdBitmapUtil.ToBitmap(imgd);
                            bitmap.Save(outputFile);
                        }
                    }
                }
                return(0);
            }
예제 #3
0
        public static IEnumerable <Imgd> FromFileToImgdList(string anyFile, int bitsPerPixel, Func <Bitmap, Bitmap> quantizer, bool swizzle)
        {
            switch (Path.GetExtension(anyFile).ToLowerInvariant())
            {
            case ".png":
            {
                yield return(new Bitmap(anyFile).Using(bitmap => ToImgd(bitmap, bitsPerPixel, quantizer, swizzle)));

                break;
            }

            case ".imd":
            {
                yield return(File.OpenRead(anyFile).Using(stream => Imgd.Read(stream)));

                break;
            }

            case ".imz":
            {
                foreach (var imgd in File.OpenRead(anyFile).Using(stream => Imgz.Read(stream)))
                {
                    yield return(imgd);
                }
                break;
            }
            }
        }
예제 #4
0
파일: Program.cs 프로젝트: xorllc/OpenKh
            protected int OnExecute(CommandLineApplication app)
            {
                OutputImz = OutputImz ?? Path.GetFullPath(Path.GetFileName(Path.ChangeExtension(InputFile.First(), ".imz")));

                Directory.CreateDirectory(Path.GetDirectoryName(OutputImz));

                var prependImgdList = (Append && File.Exists(OutputImz))
                    ? File.OpenRead(OutputImz).Using(stream => Imgz.Read(stream).ToArray())
                    : new Imgd[0];

                var buffer = new MemoryStream();

                Imgz.Write(
                    buffer,
                    prependImgdList
                    .Concat(
                        InputFile
                        .SelectMany(imdFile => ImgdBitmapUtil.FromFileToImgdList(imdFile, BitsPerPixel, QuantizerFactory.MakeFrom(this)))
                        )
                    .ToArray()
                    );
                File.WriteAllBytes(OutputImz, buffer.ToArray());

                return(0);
            }
예제 #5
0
        private void RepackFunction()
        {
            List <Imgd> _tList = new List <Imgd>();
            string      _rPath = "";

            foreach (var filePath in SelectedItemPaths)
            {
                if (_rPath == "")
                {
                    _rPath = Path.GetDirectoryName(filePath);
                }

                using (FileStream _cStream = new FileStream(filePath, FileMode.Open))
                {
                    Imgd _tImage = Imgd.Read(_cStream);
                    _tList.Add(_tImage);
                    _cStream.Close();
                }
            }

            using (FileStream _oStream = new FileStream(_rPath + "\\output.imz", FileMode.OpenOrCreate))
            {
                Imgz.Write(_oStream, _tList.ToArray());
                _oStream.Close();
            }
        }
예제 #6
0
        private void ExtractFunction()
        {
            foreach (var filePath in SelectedItemPaths)
            {
                using (FileStream _cStream = new FileStream(filePath, FileMode.Open))
                {
                    string _fPath    = filePath.Replace(".imz", "");
                    Imgz   _tArchive = new Imgz(_cStream);

                    Directory.CreateDirectory(_fPath);
                    int _i = 0;

                    foreach (var _tImage in _tArchive.Images)
                    {
                        using (FileStream _tStream = new FileStream(_fPath + string.Format("\\{0}.imd", _i.ToString("000")), FileMode.OpenOrCreate))
                        {
                            _tImage.Write(_tStream);
                            _tStream.Close();
                        }

                        _i++;
                    }

                    _cStream.Close();
                }
            }
        }
예제 #7
0
파일: Program.cs 프로젝트: xorllc/OpenKh
            protected int OnExecute(CommandLineApplication app)
            {
                OutputImz = OutputImz ?? InputFile;

                Directory.CreateDirectory(Path.GetDirectoryName(OutputImz));

                var images = File.OpenRead(OutputImz).Using(stream => Imgz.Read(stream).ToArray());

                var fixedCount = 0;

                var fixedImages = images
                                  .Select(
                    image =>
                {
                    var expetectedLen = ImgdSanityChecking.GetExpectedDataLen(image);
                    if (true &&
                        expetectedLen != null &&
                        expetectedLen < image.Data.Length &&
                        Imaging.PixelFormat.Indexed4 == image.PixelFormat
                        )
                    {
                        var fixedData = new byte[expetectedLen.Value];

                        Buffer.BlockCopy(image.Data, 0, fixedData, 0, expetectedLen.Value);

                        fixedCount++;

                        return(new Imgd(
                                   image.Size,
                                   image.PixelFormat,
                                   fixedData,
                                   image.Clut,
                                   isSwizzled: false
                                   ));
                    }
                    else
                    {
                        return(image);
                    }
                }
                    )
                                  .ToArray();

                var buffer = new MemoryStream();

                Imgz.Write(buffer, fixedImages);

                File.WriteAllBytes(OutputImz, buffer.ToArray());

                Console.WriteLine($"Fixed {fixedCount} images.");

                return(0);
            }
예제 #8
0
파일: ImgzTests.cs 프로젝트: xorllc/OpenKh
 public void IsValidTest()
 {
     using (var stream = new MemoryStream())
     {
         stream.WriteByte(0x49);
         stream.WriteByte(0x4d);
         stream.WriteByte(0x47);
         stream.WriteByte(0x5a);
         stream.Position = 0;
         Assert.True(Imgz.IsValid(stream));
     }
 }
예제 #9
0
        public void Kh2MergeImzTest()
        {
            var patcher = new PatcherProcessor();
            var patch   = new Metadata
            {
                Assets = new List <AssetFile>
                {
                    new AssetFile
                    {
                        Name   = "out.imz",
                        Method = "imgz",
                        Source = new List <AssetFile>
                        {
                            new AssetFile
                            {
                                Name  = "test.imd",
                                Index = 1,
                            }
                        }
                    }
                }
            };

            var tmpImd   = Imgd.Create(new System.Drawing.Size(16, 16), PixelFormat.Indexed4, new byte[16 * 16 / 2], new byte[4], false);
            var patchImd = Imgd.Create(new System.Drawing.Size(32, 16), PixelFormat.Indexed4, new byte[32 * 16 / 2], new byte[4], false);

            CreateFile(AssetsInputDir, "out.imz").Using(x =>
            {
                Imgz.Write(x, new Imgd[]
                {
                    tmpImd,
                    tmpImd,
                    tmpImd,
                });
            });
            CreateFile(ModInputDir, "test.imd").Using(patchImd.Write);

            patcher.Patch(AssetsInputDir, ModOutputDir, patch, ModInputDir);

            AssertFileExists(ModOutputDir, "out.imz");
            File.OpenRead(Path.Combine(ModOutputDir, "out.imz")).Using(x =>
            {
                var images = Imgz.Read(x).ToList();
                Assert.Equal(3, images.Count);
                Assert.Equal(16, images[0].Size.Width);
                Assert.Equal(32, images[1].Size.Width);
                Assert.Equal(16, images[2].Size.Width);
            });
        }
예제 #10
0
        static ImageFormatService()
        {
            imageFormat = new IImageFormat[]
            {
                GetImageFormat("PNG", "png", true, Png.IsValid, Png.Read, (stream, image) => Png.Write(stream, image)),
                GetImageFormat("BMP", "bmp", true, Bmp.IsValid, Bmp.Read, (stream, image) => Bmp.Write(stream, image)),
                GetImageFormat("TIFF", "tiff", true, Tiff.IsValid, Tiff.Read, (stream, image) => Tiff.Write(stream, image)),

                GetImageFormat("FAC", "fac", true, Imgd.IsFac, s => Imgd.ReadAsFac(s), (stream, images) =>
                               Imgd.WriteAsFac(stream, images.Select(x => x.AsImgd()))),

                GetImageFormat("IMGD", "imd", true, Imgd.IsValid, Imgd.Read, (stream, image) => image.AsImgd().Write(stream)),

                GetImageFormat("IMGZ", "imz", true, Imgz.IsValid, s => Imgz.Read(s), (stream, images) =>
                               Imgz.Write(stream, images.Select(x => x.AsImgd()))),

                GetImageFormat("KH2 Font", "bar", true, IsKh2Font, ReadKh2Font, WriteKh2Font),

                GetImageFormat("Font ARC", "arc", false, FontsArc.IsValid, s =>
                {
                    var fonts = FontsArc.Read(s);
                    return(new[]
                    {
                        fonts.FontCmd.Image1,
                        fonts.FontCmd.Image2,
                        fonts.FontHelp.Image1,
                        fonts.FontHelp.Image2,
                        fonts.FontMenu.Image1,
                        fonts.FontMenu.Image2,
                        fonts.FontMes.Image1,
                        fonts.FontMes.Image2,
                        fonts.FontNumeral.Image1,
                        fonts.FontNumeral.Image2,
                        fonts.FontIcon,
                    });
                }, (stream, images) =>
                               throw new NotImplementedException()),

                GetImageFormat("TIM2", "tm2", false, Tm2.IsValid, s => Tm2.Read(s), (stream, images) =>
                               throw new NotImplementedException()),

                GetImageFormat("KH2TIM", "tex", false, _ => true,
                               s => ModelTexture.Read(s).Images.Cast <IImageRead>(),
                               (stream, images) => throw new NotImplementedException()),
            };
예제 #11
0
        static ImageFormatService()
        {
            imageFormat = new IImageFormat[]
            {
                GetImageFormat("PNG", "png", true, Png.IsValid, Png.Read, (stream, image) => Png.Write(stream, image)),
                GetImageFormat("BMP", "bmp", true, Bmp.IsValid, Bmp.Read, (stream, image) => Bmp.Write(stream, image)),
                GetImageFormat("TIFF", "tiff", true, Tiff.IsValid, Tiff.Read, (stream, image) => Tiff.Write(stream, image)),
                GetImageFormat("IMGD", "imd", true, Imgd.IsValid, Imgd.Read, (stream, image) => image.AsImgd().Write(stream)),

                GetImageFormat("IMGZ", "imz", true, Imgz.IsValid, s => Imgz.Read(s), (stream, images) =>
                               Imgz.Write(stream, images.Select(x => x.AsImgd()))),

                GetImageFormat("TIM2", "tm2", false, Tm2.IsValid, s => Tm2.Read(s), (stream, images) =>
                               throw new NotImplementedException()),

                GetImageFormat("KH2TIM", "tex", true, _ => true,
                               s => ModelTexture.Read(s).Images.Cast <IImageRead>(),
                               (stream, images) => throw new NotImplementedException()),
            };
예제 #12
0
파일: Program.cs 프로젝트: xorllc/OpenKh
            protected int OnExecute(CommandLineApplication app)
            {
                var targets = File.OpenRead(InputFile).Using(stream => Imgz.Read(stream).ToArray());

                Console.WriteLine(
                    FormatterFactory.GetFormatterPairs()
                    .First()
                    .FormatToString(
                        targets
                        .Select(one => ImgdSummary.From(one))
                        .Select((one, index) => (one, index))
                        .ToDictionary(
                            pair => pair.index.ToString(),
                            pair => pair.one
                            )
                        )
                    );

                return(0);
            }
예제 #13
0
파일: Program.cs 프로젝트: xorllc/OpenKh
            protected int OnExecute(CommandLineApplication app)
            {
                OutputImz = OutputImz ?? InputFile;

                Directory.CreateDirectory(Path.GetDirectoryName(OutputImz));

                var images = File.OpenRead(OutputImz).Using(stream => Imgz.Read(stream).ToArray());

                var fixedCount = 0;

                var fixedImages = images
                                  .Select(
                    image =>
                {
                    if (Imaging.PixelFormat.Indexed4 == image.PixelFormat)
                    {
                        var data = image.Data;

                        for (var x = 0; x < data.Length; x++)
                        {
                            var swap = data[x];
                            data[x]  = (byte)((swap << 4) | (swap >> 4));
                        }

                        fixedCount++;
                    }
                    return(image);
                }
                    )
                                  .ToArray();

                var buffer = new MemoryStream();

                Imgz.Write(buffer, fixedImages);

                File.WriteAllBytes(OutputImz, buffer.ToArray());

                Console.WriteLine($"Applied to {fixedCount} images.");

                return(0);
            }
예제 #14
0
        public void Kh2MergeImzInsideBarTest()
        {
            var patcher = new PatcherProcessor();
            var patch   = new Metadata
            {
                Assets = new List <AssetFile>
                {
                    new AssetFile
                    {
                        Name   = "out.bar",
                        Method = "binarc",
                        Source = new List <AssetFile>
                        {
                            new AssetFile
                            {
                                Name   = "test",
                                Type   = "imgz",
                                Method = "imgz",
                                Source = new List <AssetFile>
                                {
                                    new AssetFile
                                    {
                                        Name  = "test.imd",
                                        Index = 1
                                    }
                                },
                            }
                        }
                    }
                }
            };

            var tmpImd   = Imgd.Create(new System.Drawing.Size(16, 16), PixelFormat.Indexed4, new byte[16 * 16 / 2], new byte[4], false);
            var patchImd = Imgd.Create(new System.Drawing.Size(32, 16), PixelFormat.Indexed4, new byte[32 * 16 / 2], new byte[4], false);

            CreateFile(AssetsInputDir, "out.bar").Using(x =>
            {
                using var memoryStream = new MemoryStream();
                Imgz.Write(memoryStream, new Imgd[]
                {
                    tmpImd,
                    tmpImd,
                    tmpImd,
                });

                Bar.Write(x, new Bar
                {
                    new Bar.Entry
                    {
                        Name   = "test",
                        Type   = Bar.EntryType.Imgz,
                        Stream = memoryStream
                    }
                });
            });
            CreateFile(ModInputDir, "test.imd").Using(patchImd.Write);

            patcher.Patch(AssetsInputDir, ModOutputDir, patch, ModInputDir);

            AssertFileExists(ModOutputDir, "out.bar");
            AssertBarFile("test", x =>
            {
                var images = Imgz.Read(x.Stream).ToList();
                Assert.Equal(3, images.Count);
                Assert.Equal(16, images[0].Size.Width);
                Assert.Equal(32, images[1].Size.Width);
                Assert.Equal(16, images[2].Size.Width);
            }, ModOutputDir, "out.bar");
        }