예제 #1
0
 void ExtractImage(ArcFile arc, Entry entry, ImageFormat target_format)
 {
     using (var file = arc.OpenSeekableEntry(entry))
     {
         var src_format = ImageFormat.FindFormat(file, entry.Name);
         if (null == src_format)
         {
             throw new InvalidFormatException(string.Format("{1}: {0}", guiStrings.MsgUnableInterpretImage, entry.Name));
         }
         file.Position = 0;
         string target_ext = target_format.Extensions.FirstOrDefault() ?? "";
         string outname    = FindUniqueFileName(entry.Name, target_ext);
         if (src_format.Item1 == target_format)
         {
             // source format is the same as a target, copy file as is
             using (var output = ArchiveFormat.CreateFile(outname))
                 file.CopyTo(output);
             return;
         }
         ImageData image = src_format.Item1.Read(file, src_format.Item2);
         if (m_adjust_image_offset)
         {
             image = AdjustImageOffset(image);
         }
         using (var outfile = ArchiveFormat.CreateFile(outname))
         {
             target_format.Write(outfile, image);
         }
     }
 }
예제 #2
0
        void ConvertImage(string filename)
        {
            string source_ext  = Path.GetExtension(filename).TrimStart('.').ToLowerInvariant();
            string target_name = Path.GetFileName(filename);
            string target_ext  = m_image_format.Extensions.FirstOrDefault();

            target_name = Path.ChangeExtension(target_name, target_ext);
            using (var file = BinaryStream.FromFile(filename))
            {
                var src_format = ImageFormat.FindFormat(file);
                if (null == src_format)
                {
                    return;
                }
                Stream output = null;
                try
                {
                    if (src_format.Item1 == m_image_format && m_image_format.Extensions.Any(ext => ext == source_ext))
                    {
                        if (AreSamePaths(filename, target_name))
                        {
                            return;
                        }
                        output        = CreateNewFile(target_name);
                        file.Position = 0;
                        file.AsStream.CopyTo(output);
                    }
                    else
                    {
                        file.Position = 0;
                        var image = src_format.Item1.Read(file, src_format.Item2);
                        output = CreateNewFile(target_name);
                        m_image_format.Write(output, image);
                    }
                }
                catch // delete destination file on conversion failure
                {
                    // FIXME if user chooses to overwrite file, and conversion results in error,
                    // then original file will be lost.
                    output.Dispose();
                    output = null;
                    File.Delete(target_name);
                    throw;
                }
                finally
                {
                    if (output != null)
                    {
                        output.Dispose();
                    }
                }
            }
        }
예제 #3
0
 void PrintMetaData(string filename)
 {
     using (var file = File.Open(filename, FileMode.Open, FileAccess.Read))
     {
         var format = ImageFormat.FindFormat(file);
         if (null == format)
         {
             Console.Error.WriteLine("{0}: file format not recognized", filename);
             return;
         }
         var image = format.Item2;
         Console.WriteLine("{0,16} [{4}] {1}x{2} {3}bpp", filename, image.Width, image.Height, image.BPP, format.Item1.Tag);
     }
 }
예제 #4
0
        public override ImageMetaData ReadMetaData(Stream stream)
        {
            var header = new byte[0x7C];

            if (header.Length != stream.Read(header, 0, header.Length))
            {
                return(null);
            }
            var base_name = Binary.GetCString(header, 4, 100);

            if (string.IsNullOrEmpty(base_name))
            {
                return(null);
            }
            var files = VFS.GetFiles(base_name + ".*");

            if (!files.Any())
            {
                throw new FileNotFoundException(string.Format("Base image '{0}' not found", base_name));
            }
            var base_entry = files.First();

            using (var input = VFS.OpenSeekableStream(base_entry))
            {
                // ReadMetaData isn't supplied with a filename being processed, so infinite recursion can't be
                // prevented here unless we save state in a static member.
                var format = ImageFormat.FindFormat(input, base_entry.Name);
                if (null == format)
                {
                    throw new InvalidFormatException(string.Format("Unable to interpret base image '{0}'", base_name));
                }
                format.Item2.FileName = base_entry.Name;
                return(new DifMetaData
                {
                    Width = format.Item2.Width,
                    Height = format.Item2.Height,
                    BPP = 24,
                    BaseEntry = base_entry,
                    BaseFormat = format.Item1,
                    BaseInfo = format.Item2,
                    PackedIndexSize = LittleEndian.ToInt32(header, 0x68),
                    IndexSize = LittleEndian.ToInt32(header, 0x6C),
                    PackedDiffSize = LittleEndian.ToInt32(header, 0x70),
                    DiffDataSize = LittleEndian.ToInt32(header, 0x74),
                    DiffCount = LittleEndian.ToInt32(header, 0x78),
                });
            }
        }
예제 #5
0
        public override ImageMetaData ReadMetaData(IBinaryStream stream)
        {
            var header    = stream.ReadHeader(0x7C);
            var base_name = header.GetCString(4, 100);

            if (string.IsNullOrEmpty(base_name))
            {
                return(null);
            }
            var files = VFS.GetFiles(base_name + ".*");

            if (!files.Any())
            {
                throw new FileNotFoundException(string.Format("Base image '{0}' not found", base_name));
            }
            var base_entry = files.First();

            if (base_entry.Name.Equals(stream.Name, StringComparison.InvariantCultureIgnoreCase))
            {
                throw new InvalidFormatException("DIF image references itself");
            }
            using (var input = VFS.OpenBinaryStream(base_entry))
            {
                // infinite recursion still possible in case of two files referencing each other.
                var format = ImageFormat.FindFormat(input);
                if (null == format)
                {
                    throw new InvalidFormatException(string.Format("Unable to interpret base image '{0}'", base_name));
                }
                format.Item2.FileName = base_entry.Name;
                return(new DifMetaData
                {
                    Width = format.Item2.Width,
                    Height = format.Item2.Height,
                    BPP = 24,
                    BaseEntry = base_entry,
                    BaseFormat = format.Item1,
                    BaseInfo = format.Item2,
                    PackedIndexSize = header.ToInt32(0x68),
                    IndexSize = header.ToInt32(0x6C),
                    PackedDiffSize = header.ToInt32(0x70),
                    DiffDataSize = header.ToInt32(0x74),
                    DiffCount = header.ToInt32(0x78),
                });
            }
        }