예제 #1
0
        public IKanvasImage Load(Stream input)
        {
            using var br = new BinaryReaderX(input);

            // Read header
            var header = br.ReadType <PckHeader>();

            _texHeader = br.ReadType <TexHeader>();

            // Read data
            var imgData = new SubStream(input, input.Position, _texHeader.dataSize);

            var buffer = new byte[4];

            imgData.Read(buffer);
            imgData.Position -= 4;

            IKanvasImage image;

            switch (_magic = BinaryPrimitives.ReadUInt32BigEndian(buffer))
            {
            case 0x89504E47:        // PNG
                var img = (Bitmap)Image.FromStream(imgData);
                image = new BitmapKanvasImage(img);

                break;

            case 0x4C5A3737:        // LZ77

                // Decompress image data buffer
                var decompressedData = new MemoryStream();
                Compressions.StingLz.Build().Decompress(imgData, decompressedData);

                decompressedData.Position = 0;
                var dataBr = new BinaryReaderX(decompressedData);

                // Prepare image info
                var dataSize  = _texHeader.width * _texHeader.height;
                var imageInfo = new ImageInfo(dataBr.ReadBytes(dataSize), 0, new Size(_texHeader.width, _texHeader.height))
                {
                    PaletteData   = dataBr.ReadBytes(256 * 4),
                    PaletteFormat = 0
                };

                image = new KanvasImage(TexSupport.GetEncodingDefinition(), imageInfo);

                break;

            default:
                throw new InvalidOperationException("Unknown data type.");
            }

            // Read unknown region
            input.Position = (header.size + 3) & ~3;
            _unkRegion     = br.ReadBytes((int)(input.Length - input.Position));

            return(image);
        }
예제 #2
0
        private void ExtractWith(IFileSystem sourceFileSystem, UPath filePath, Guid pluginId)
        {
            // Load file
            LoadResult loadResult;

            try
            {
                loadResult = pluginId == Guid.Empty ?
                             _pluginManager.LoadFile(sourceFileSystem, filePath).Result :
                             _pluginManager.LoadFile(sourceFileSystem, filePath, pluginId).Result;
            }
            catch (Exception e)
            {
                Console.WriteLine($"Batch Error: {filePath}: {e.Message}");
                return;
            }

            if (!loadResult.IsSuccessful)
            {
                Console.WriteLine($"Batch Error: {filePath}: {loadResult.Message}");
                return;
            }

            var absolutePath          = (UPath)sourceFileSystem.ConvertPathToInternal(filePath);
            var destinationDirectory  = absolutePath.GetDirectory() / absolutePath.GetName().Replace('.', '_');
            var destinationFileSystem = FileSystemFactory.CreatePhysicalFileSystem(destinationDirectory, new StreamManager());

            switch (loadResult.LoadedState.PluginState)
            {
            case IArchiveState archiveState:
                foreach (var afi in archiveState.Files)
                {
                    var newFileStream = destinationFileSystem.OpenFile(afi.FilePath, FileMode.Create, FileAccess.Write);
                    afi.GetFileData().Result.CopyTo(newFileStream);

                    newFileStream.Close();
                }
                break;

            case IImageState imageState:
                var index = 0;
                foreach (var img in imageState.Images)
                {
                    var kanvasImage = new KanvasImage(imageState, img);
                    kanvasImage.GetImage().Save(destinationDirectory + "/" + (img.Name ?? $"{index:00}") + ".png");

                    index++;
                }
                break;

            default:
                Console.WriteLine($"Batch Error: {filePath}: '{loadResult.LoadedState.PluginState.GetType().Name}' is not supported.");
                break;
            }

            _pluginManager.Close(loadResult.LoadedState);
        }
예제 #3
0
        public EncodeImageViewModel(PluginManager pluginManager, IImageState state, ImageInfo imageInfo)
        {
            _pluginManager = pluginManager;
            _state         = state;
            _kanvasImage   = new KanvasImage(state, imageInfo);

            SelectedEncoding = state.SupportedEncodings[_kanvasImage.ImageFormat];
            SourceImage      = _kanvasImage.GetImage().ToBitmapImage();
            OutputImage      = SourceImage;
        }
예제 #4
0
        public async Task Load(IFileSystem fileSystem, UPath filePath, LoadContext loadContext)
        {
            var ncgrStream = await fileSystem.OpenFileAsync(filePath);

            var nclrStream = await fileSystem.OpenFileAsync(filePath.ChangeExtension("NCLR"));

            var kanvasImage = new KanvasImage(EncodingDefinition, _ncgr.Load(ncgrStream, nclrStream));

            Images = new IKanvasImage[] { kanvasImage };
        }
예제 #5
0
        private void ExtractImageInternal(KanvasImage image, IFileSystem destinationFileSystem, string fileName)
        {
            var newFileStream = destinationFileSystem.OpenFile(fileName, FileMode.Create, FileAccess.Write);

            // TODO: Allow progress?
            var imageStream = new MemoryStream();

            image.GetImage().Save(imageStream, ImageFormat.Png);

            imageStream.Position = 0;
            imageStream.CopyTo(newFileStream);

            imageStream.Close();
            newFileStream.Close();
        }
예제 #6
0
        private void InjectImage(string imageIndexArgument, UPath injectPath)
        {
            if (!int.TryParse(imageIndexArgument, out var imageIndex))
            {
                Console.WriteLine($"'{imageIndexArgument}' is not a valid number.");
                return;
            }

            if (imageIndex >= _imageState.Images.Count)
            {
                Console.WriteLine($"Index '{imageIndex}' was out of bounds.");
                return;
            }

            var kanvasImage = new KanvasImage(_imageState, _imageState.Images[imageIndex]);

            kanvasImage.SetImage((Bitmap)Image.FromFile(injectPath.FullName));
        }
예제 #7
0
        private void PrintImage(string imageIndexArgument)
        {
            if (!int.TryParse(imageIndexArgument, out var imageIndex))
            {
                Console.WriteLine($"'{imageIndexArgument}' is not a valid number.");
                return;
            }

            if (imageIndex >= _imageState.Images.Count)
            {
                Console.WriteLine($"Index '{imageIndex}' was out of bounds.");
                return;
            }

            var image = new KanvasImage(_imageState, _imageState.Images[imageIndex]);

            var newSize      = new Size(Console.WindowWidth, Console.WindowHeight);
            var resizedImage = ResizeImage(image.GetImage(), newSize);

            var asciiImage = ConvertAscii(resizedImage);

            Console.WriteLine(asciiImage);
        }