Example #1
0
 ImageData ApplyMaskToWipData(ImageMetaData info, byte[] image, string mask_name)
 {
     using (var mask_file = VFS.OpenBinaryStream(mask_name))
     {
         if (mask_file.Signature != Signature)
         {
             throw new InvalidFormatException();
         }
         var mask_info = ReadMetaData(mask_file) as WipMetaData;
         if (null == mask_info || 8 != mask_info.BPP ||
             info.Width != mask_info.Width || info.Height != mask_info.Height)
         {
             throw new InvalidFormatException();
         }
         using (var reader = new Reader(mask_file, mask_info))
         {
             reader.Unpack();
             var palette    = reader.Palette;
             int dst_stride = (int)info.Width * 4;
             var pixels     = new byte[dst_stride * (int)info.Height];
             int plane_size = (int)info.Width * (int)info.Height;
             var alpha      = reader.Data;
             int dst        = 0;
             for (int src = 0; src < plane_size; ++src)
             {
                 pixels[dst++] = image[src];
                 pixels[dst++] = image[src + plane_size];
                 pixels[dst++] = image[src + plane_size * 2];
                 var color = palette[alpha[src]];
                 pixels[dst++] = (byte)((color.B + color.G + color.R) / 3);
             }
             return(ImageData.Create(info, PixelFormats.Bgra32, null, pixels, dst_stride));
         }
     }
 }
Example #2
0
        public override ImageData Read(IBinaryStream stream, ImageMetaData info)
        {
            var         meta   = (DziMetaData)info;
            PixelFormat format = PixelFormats.Bgra32;
            var         bitmap = new WriteableBitmap((int)meta.Width, (int)meta.Height, ImageData.DefaultDpiX,
                                                     ImageData.DefaultDpiY, format, null);
            int actual_width  = 0;
            int actual_height = 0;

            byte[] pixels = null;
            foreach (var tile in meta.Tiles.First())
            {
                var image_entry = VFS.GetFiles(tile.FileName + ".*").FirstOrDefault();
                if (null == image_entry)
                {
                    throw new FileNotFoundException("Tile not found", tile.FileName);
                }
                using (var input = VFS.OpenBinaryStream(image_entry))
                {
                    var image = Read(input);
                    if (null == image)
                    {
                        throw new FileFormatException("Unknown DZI tile format");
                    }
                    var converted = image.Bitmap;
                    if (converted.Format != format)
                    {
                        converted = new FormatConvertedBitmap(converted, format, null, 0);
                    }
                    int stride    = converted.PixelWidth * 4;
                    int tile_size = stride * converted.PixelHeight;
                    if (null == pixels || pixels.Length < tile_size)
                    {
                        pixels = new byte[tile_size];
                    }
                    converted.CopyPixels(pixels, stride, 0);
                    var width  = Math.Min(converted.PixelWidth, bitmap.PixelWidth - tile.X);
                    var height = Math.Min(converted.PixelHeight, bitmap.PixelHeight - tile.Y);
                    var rect   = new Int32Rect(tile.X, tile.Y, width, height);
                    bitmap.WritePixels(rect, pixels, stride, 0);
                    if (tile.X + width > actual_width)
                    {
                        actual_width = tile.X + width;
                    }
                    if (tile.Y + height > actual_height)
                    {
                        actual_height = tile.Y + height;
                    }
                }
            }
            BitmapSource result = bitmap;

            if (actual_width < bitmap.PixelWidth || actual_height < bitmap.PixelHeight)
            {
                var rect = new Int32Rect(0, 0, actual_width, actual_height);
                result = new CroppedBitmap(bitmap, rect);
            }
            result.Freeze();
            return(new ImageData(result, meta));
        }
Example #3
0
        ImageData ApplyMaskToImage(RctMetaData info, byte[] image, string mask_name)
        {
            using (var mask_file = VFS.OpenBinaryStream(mask_name))
            {
                var mask_info = s_rc8_format.Value.ReadMetaData(mask_file);
                if (null == mask_info ||
                    info.Width != mask_info.Width || info.Height != mask_info.Height)
                {
                    throw new InvalidFormatException();
                }

                using (var reader = new Rc8Format.Reader(mask_file, mask_info))
                {
                    reader.Unpack();
                    var palette    = reader.Palette;
                    int dst_stride = (int)info.Width * 4;
                    var pixels     = new byte[dst_stride * (int)info.Height];
                    var alpha      = reader.Data;
                    int a_src      = 0;
                    int src        = 0;
                    for (int dst = 0; dst < pixels.Length; dst += 4)
                    {
                        pixels[dst]     = image[src++];
                        pixels[dst + 1] = image[src++];
                        pixels[dst + 2] = image[src++];
                        var color = palette[alpha[a_src++]];
                        pixels[dst + 3] = (byte)~((color.B + color.G + color.R) / 3);
                    }
                    return(ImageData.Create(info, PixelFormats.Bgra32, null, pixels, dst_stride));
                }
            }
        }
Example #4
0
        public override ArcFile TryOpen(ArcView file)
        {
            if (!file.Name.HasExtension(".BND"))
            {
                return(null);
            }
            var idx_name = Path.ChangeExtension(file.Name, "idx");

            if (!VFS.FileExists(idx_name))
            {
                return(null);
            }
            using (var idx = VFS.OpenBinaryStream(idx_name))
            {
                int count = (int)idx.Length / 0x18;
                if (!IsSaneCount(count) || count * 0x18 != idx.Length)
                {
                    return(null);
                }
                var dir = new List <Entry> (count);
                for (int i = 0; i < count; ++i)
                {
                    var name  = idx.ReadCString(0x10);
                    var entry = Create <Entry> (name);
                    entry.Size   = idx.ReadUInt32();
                    entry.Offset = idx.ReadUInt32();
                    if (!entry.CheckPlacement(file.MaxOffset))
                    {
                        return(null);
                    }
                    dir.Add(entry);
                }
                return(new ArcFile(file, this, dir));
            }
        }
Example #5
0
        void LoadPreviewText(PreviewFile preview)
        {
            Stream file = null;

            try
            {
                file = VFS.OpenBinaryStream(preview.Entry).AsStream;
                if (!TextView.IsTextFile(file))
                {
                    ResetPreviewPane();
                    return;
                }
                var enc = EncodingChoice.SelectedItem as Encoding;
                if (null == enc)
                {
                    enc = TextView.GuessEncoding(file);
                    EncodingChoice.SelectedItem = enc;
                }
                TextView.DisplayStream(file, enc);
                ActiveViewer     = TextView;
                CurrentTextInput = file;
                file             = null;
            }
            catch (Exception X)
            {
                SetStatusText(X.Message);
            }
            finally
            {
                if (file != null)
                {
                    file.Dispose();
                }
            }
        }
Example #6
0
        internal EriReader ReadImageData(IBinaryStream stream, EriMetaData meta)
        {
            stream.Position = meta.StreamPos;
            Color[] palette = null;
            using (var input = new EriFile(stream.AsStream))
            {
                for (;;) // ReadSection throws an exception in case of EOF
                {
                    var section = input.ReadSection();
                    if ("Stream  " == section.Id)
                    {
                        continue;
                    }
                    if ("ImageFrm" == section.Id)
                    {
                        break;
                    }
                    if ("Palette " == section.Id && meta.BPP <= 8 && section.Length <= 0x400)
                    {
                        palette = ReadPalette(stream.AsStream, (int)section.Length);
                        continue;
                    }
                    input.BaseStream.Seek(section.Length, SeekOrigin.Current);
                }
            }
            var reader = new EriReader(stream.AsStream, meta, palette);

            reader.DecodeImage();

            if (!string.IsNullOrEmpty(meta.Description))
            {
                var    tags = ParseTagInfo(meta.Description);
                string ref_file;
                if (tags.TryGetValue("reference-file", out ref_file))
                {
                    ref_file = ref_file.TrimEnd(null);
                    if (!string.IsNullOrEmpty(ref_file))
                    {
                        if ((meta.BPP + 7) / 8 < 3)
                        {
                            throw new InvalidFormatException();
                        }

                        ref_file = VFS.CombinePath(VFS.GetDirectoryName(meta.FileName), ref_file);
                        using (var ref_src = VFS.OpenBinaryStream(ref_file))
                        {
                            var ref_info = ReadMetaData(ref_src) as EriMetaData;
                            if (null == ref_info)
                            {
                                throw new FileNotFoundException("Referenced image not found", ref_file);
                            }
                            ref_info.FileName = ref_file;
                            var ref_reader = ReadImageData(ref_src, ref_info);
                            reader.AddImageBuffer(ref_reader);
                        }
                    }
                }
            }
            return(reader);
        }
Example #7
0
 byte[] ReadBaseImage(IBinaryStream file, RctMetaData meta)
 {
     file.Position = meta.DataOffset;
     byte[] name_bin = file.ReadBytes(meta.AddSize);
     if (name_bin.Length != meta.AddSize)
     {
         throw new EndOfStreamException();
     }
     try
     {
         string name     = Encodings.cp932.GetString(name_bin, 0, name_bin.Length - 1);
         string dir_name = VFS.GetDirectoryName(meta.FileName);
         name = VFS.CombinePath(dir_name, name);
         if (VFS.FileExists(name))
         {
             using (var base_file = VFS.OpenBinaryStream(name))
             {
                 var base_info = ReadMetaData(base_file) as RctMetaData;
                 if (null != base_info && 0 == base_info.AddSize &&
                     meta.Width == base_info.Width && meta.Height == base_info.Height)
                 {
                     base_info.FileName = name;
                     return(ReadPixelsData(base_file, base_info));
                 }
             }
         }
     }
     catch { /* ignore baseline image read errors */ }
     return(null);
 }
Example #8
0
        byte[] ReadBaseImage(string filename, AkbMetaData overlay_info)
        {
            var pattern = Path.GetFileNameWithoutExtension(filename) + ".*";

            pattern = VFS.CombinePath(VFS.GetDirectoryName(filename), pattern);
            foreach (var entry in VFS.GetFiles(pattern))
            {
                if (entry.Name == overlay_info.FileName)
                {
                    continue;
                }
                using (var base_file = VFS.OpenBinaryStream(entry))
                {
                    var base_info = ReadMetaData(base_file) as AkbMetaData;
                    if (null != base_info && base_info.BPP == overlay_info.BPP &&
                        base_info.Width == overlay_info.Width && base_info.Height == overlay_info.Height)
                    {
                        // FIXME what if baseline image is incremental itself?
                        var reader = new AkbReader(base_file.AsStream, base_info);
                        return(reader.Unpack());
                    }
                }
            }
            return(null);
        }
Example #9
0
 byte[] ReadBaseImage(IBinaryStream file, RctMetaData meta)
 {
     try
     {
         file.Position = meta.DataOffset;
         var    name     = file.ReadCString(meta.BaseNameLength);
         string dir_name = VFS.GetDirectoryName(meta.FileName);
         name = VFS.CombinePath(dir_name, name);
         if (VFS.FileExists(name))
         {
             using (var base_file = VFS.OpenBinaryStream(name))
             {
                 var base_info = ReadMetaData(base_file) as RctMetaData;
                 if (null != base_info &&
                     meta.Width == base_info.Width && meta.Height == base_info.Height)
                 {
                     base_info.BaseRecursionDepth = meta.BaseRecursionDepth + 1;
                     base_info.FileName           = name;
                     return(ReadPixelsData(base_file, base_info));
                 }
             }
         }
     }
     catch { /* ignore baseline image read errors */ }
     return(null);
 }
Example #10
0
 void ReadBaseImage()
 {
     try
     {
         string dir_name  = VFS.GetDirectoryName(m_info.FileName);
         string base_name = Path.ChangeExtension(m_info.BaseName, "qnt");
         base_name = VFS.CombinePath(dir_name, base_name);
         using (var base_file = VFS.OpenBinaryStream(base_name))
         {
             var base_info = Qnt.ReadMetaData(base_file) as QntMetaData;
             if (null != base_info && m_info.Width == base_info.Width && m_info.Height == base_info.Height)
             {
                 base_info.FileName = base_name;
                 var reader = new QntFormat.Reader(base_file.AsStream, base_info);
                 reader.Unpack();
                 m_base_bpp = reader.BPP;
                 m_base     = reader.Data;
             }
         }
     }
     catch (Exception X)
     {
         Trace.WriteLine(X.Message, "[DCF]");
     }
 }
Example #11
0
        public override ImageData Read(IBinaryStream stream, ImageMetaData info)
        {
            var           meta     = (PgdIncMetaData)info;
            string        dir_name = VFS.GetDirectoryName(meta.FileName);
            string        name     = VFS.CombinePath(dir_name, meta.BaseName);
            PgdGeMetaData base_info;

            byte[]      image, overlay;
            PixelFormat format;

            using (var base_file = VFS.OpenBinaryStream(name))
            {
                base_info = PalFormat.Value.ReadMetaData(base_file) as PgdGeMetaData;
                if (null == base_info)
                {
                    throw new InvalidFormatException("Invalid baseline image format");
                }
                if (meta.OffsetX + meta.Width > base_info.Width ||
                    meta.OffsetY + meta.Height > base_info.Height)
                {
                    throw new InvalidFormatException("Incompatible baseline image dimensions");
                }
                base_info.FileName = name;
                using (var reader = new PgdReader(base_file, base_info))
                {
                    image  = reader.UnpackGE();
                    format = reader.Format;
                }
            }
            using (var reader = new PgdReader(stream, meta))
                overlay = reader.UnpackOverlay();

            int  overlay_bpp = meta.BPP / 8;
            int  base_bpp    = format.BitsPerPixel / 8;
            int  dst         = (meta.OffsetY * (int)base_info.Width + meta.OffsetX) * base_bpp;
            int  gap         = (int)(base_info.Width - meta.Width) * base_bpp;
            int  src         = 0;
            bool apply_alpha = overlay_bpp == 4 && base_bpp == 4;

            for (uint y = 0; y < meta.Height; ++y)
            {
                for (uint x = 0; x < meta.Width; ++x)
                {
                    image[dst]     ^= overlay[src];
                    image[dst + 1] ^= overlay[src + 1];
                    image[dst + 2] ^= overlay[src + 2];
                    if (apply_alpha)
                    {
                        image[dst + 3] ^= overlay[src + 3];
                    }
                    dst += base_bpp;
                    src += overlay_bpp;
                }
                dst += gap;
            }
            base_info.FileName = meta.FileName;
            return(ImageData.Create(base_info, format, null, image));
        }
Example #12
0
        public override ArcFile TryOpen(ArcView file)
        {
            if (!file.Name.HasExtension("DAT"))
            {
                return(null);
            }
            var hed_name = Path.ChangeExtension(file.Name, "HED");

            if (!VFS.FileExists(hed_name))
            {
                return(null);
            }
            using (var hed = VFS.OpenBinaryStream(hed_name))
            {
                var base_name   = Path.GetFileNameWithoutExtension(file.Name);
                var dir         = new List <Entry>();
                var name_buffer = new byte[0x100];
                while (hed.PeekByte() != -1)
                {
                    int    name_length = hed.ReadUInt8();
                    string name;
                    if (name_length != 0)
                    {
                        if (hed.Read(name_buffer, 0, name_length) != name_length)
                        {
                            return(null);
                        }
                        for (int i = 0; i < name_length; ++i)
                        {
                            name_buffer[i] ^= 0xFF;
                        }
                        name = Binary.GetCString(name_buffer, 0, name_length);
                    }
                    else
                    {
                        name = string.Format("{0}#{1:D4}", base_name, dir.Count);
                    }
                    var entry = new Entry {
                        Name   = name,
                        Offset = hed.ReadUInt32(),
                    };
                    if (entry.Offset > file.MaxOffset)
                    {
                        return(null);
                    }
                    dir.Add(entry);
                }
                if (0 == dir.Count)
                {
                    return(null);
                }
                AdjustSizes(dir, file.MaxOffset);
                DetectFileTypes(dir, file);
                return(new ArcFile(file, this, dir));
            }
        }
Example #13
0
        public override ArcFile TryOpen(ArcView file)
        {
            string lst_name = IndexExtensions.Select(ext => file.Name + ext)
                              .FirstOrDefault(name => VFS.FileExists(name));

            if (null == lst_name)
            {
                return(null);
            }
            using (var lst = VFS.OpenBinaryStream(lst_name))
            {
                const int name_length = 0x41;
                var       dir         = new List <Entry>();
                var       name_buffer = new byte[name_length];
                while (lst.Read(name_buffer, 0, name_length) == name_length)
                {
                    int name_end;
                    for (name_end = 0; name_end < name_length; ++name_end)
                    {
                        if (0 == name_buffer[name_end])
                        {
                            break;
                        }
                        name_buffer[name_end] ^= DefaultKey;
                    }
                    var  name   = Binary.GetCString(name_buffer, 0, name_end);
                    uint offset = lst.ReadUInt32();
                    if (offset > file.MaxOffset)
                    {
                        return(null);
                    }
                    byte type = lst.ReadUInt8();
                    if (type < KnownTypes.Length)
                    {
                        name = Path.ChangeExtension(name, KnownTypes[type]);
                    }
                    var entry = FormatCatalog.Instance.Create <YuEntry> (name);
                    entry.Offset      = offset;
                    entry.ContentType = type;
                    dir.Add(entry);
                }
                if (0 == dir.Count)
                {
                    return(null);
                }
                for (int i = 0; i < dir.Count; ++i)
                {
                    long next_offset = i + 1 == dir.Count ? file.MaxOffset : dir[i + 1].Offset;
                    dir[i].Size = (uint)(next_offset - dir[i].Offset);
                }
                return(new ArcFile(file, this, dir));
            }
        }
Example #14
0
        public override ImageData Read(IBinaryStream stream, ImageMetaData info)
        {
            var          meta = (DifMetaData)info;
            BitmapSource base_bitmap;

            using (var input = VFS.OpenBinaryStream(meta.BaseEntry))
            {
                var image = meta.BaseFormat.Read(input, meta.BaseInfo);
                base_bitmap = image.Bitmap;
            }
            stream.Position = 0x7C;
            var index = new byte[meta.IndexSize];

            using (var input = new LzssStream(stream.AsStream, LzssMode.Decompress, true))
                if (index.Length != input.Read(index, 0, index.Length))
                {
                    throw new EndOfStreamException();
                }

            if (base_bitmap.Format.BitsPerPixel != 24)
            {
                base_bitmap = new FormatConvertedBitmap(base_bitmap, PixelFormats.Bgr24, null, 0);
            }

            int src_stride = base_bitmap.PixelWidth * 3; // XXX
            int dst_stride = (src_stride + 3) & ~3;
            var pixels     = new byte[dst_stride * base_bitmap.PixelHeight];
            int row_offset = 0;
            var rect       = new Int32Rect(0, base_bitmap.PixelHeight, base_bitmap.PixelWidth, 1);

            for (rect.Y = base_bitmap.PixelHeight - 1; rect.Y >= 0; --rect.Y)
            {
                base_bitmap.CopyPixels(rect, pixels, src_stride, row_offset);
                row_offset += dst_stride;
            }

            stream.Position = 0x7C + meta.PackedIndexSize;
            using (var diff = new LzssStream(stream.AsStream, LzssMode.Decompress, true))
            {
                int index_src = 0;
                for (int i = 0; i < meta.DiffCount; ++i)
                {
                    int offset = LittleEndian.ToInt32(index, index_src);
                    int size   = LittleEndian.ToInt32(index, index_src + 4);
                    index_src += 8;
                    diff.Read(pixels, offset, size);
                }
                return(ImageData.CreateFlipped(info, PixelFormats.Bgr24, null, pixels, dst_stride));
            }
        }
Example #15
0
        public override ArcFile TryOpen(ArcView file)
        {
            int count = file.View.ReadInt32(4);

            if (!IsSaneCount(count))
            {
                return(null);
            }
            var pac_name = Path.GetFileNameWithoutExtension(file.Name);
            int pac_num;

            if (!Int32.TryParse(pac_name, out pac_num))
            {
                return(null);
            }
            var hdr_name = string.Format("{0:D3}.dat", pac_num - 1);

            hdr_name = VFS.ChangeFileName(file.Name, hdr_name);
            if (!VFS.FileExists(hdr_name))
            {
                return(null);
            }
            using (var index = VFS.OpenBinaryStream(hdr_name))
            {
                var header = index.ReadHeader(8);
                if (!header.AsciiEqual("\x89HDR"))
                {
                    return(null);
                }
                if (header.ToInt32(4) != count)
                {
                    return(null);
                }
                var dir = new List <Entry> (count);
                for (int i = 0; i < count; ++i)
                {
                    var name  = index.ReadCString(0x10);
                    var entry = FormatCatalog.Instance.Create <Entry> (name);
                    entry.Size   = index.ReadUInt32();
                    entry.Offset = index.ReadUInt32();
                    if (!entry.CheckPlacement(file.MaxOffset))
                    {
                        return(null);
                    }
                    dir.Add(entry);
                }
                return(new ArcFile(file, this, dir));
            }
        }
Example #16
0
        public virtual LinkEncryption GetEncryption()
        {
            var params_dir = VFS.GetDirectoryName(m_input.Name);
            var params_dat = VFS.CombinePath(params_dir, "params.dat");

            if (!VFS.FileExists(params_dat))
            {
                return(null);
            }

            using (var input = VFS.OpenBinaryStream(params_dat))
            {
                var param = ParamsDeserializer.Create(input);
                return(param.GetEncryption());
            }
        }
Example #17
0
        BitmapSource BlendBaseLine(BitmapSource overlay, GdtMetaData meta)
        {
            string base_name = VFS.ChangeFileName(meta.FileName, meta.BaseLine);

            if (!VFS.FileExists(base_name))
            {
                base_name += ".gdt";
                if (!VFS.FileExists(base_name))
                {
                    return(overlay);
                }
            }
            using (var base_file = VFS.OpenBinaryStream(base_name))
            {
                var base_info = ReadMetaData(base_file) as GdtMetaData;
                if (null == base_info)
                {
                    return(overlay);
                }
                var base_image = ReadBitmapSource(base_file, base_info);
                if (base_image.Format.BitsPerPixel < 24)
                {
                    base_image = new FormatConvertedBitmap(base_image, PixelFormats.Bgr32, null, 0);
                }
                var canvas     = new WriteableBitmap(base_image);
                int canvas_bpp = canvas.Format.BitsPerPixel;
                if (canvas_bpp != overlay.Format.BitsPerPixel)
                {
                    overlay = new FormatConvertedBitmap(overlay, canvas.Format, null, 0);
                }
                canvas.Lock();
                unsafe
                {
                    byte *buffer         = (byte *)canvas.BackBuffer;
                    int   canvas_stride  = canvas.BackBufferStride;
                    int   canvas_size    = canvas_stride * canvas.PixelHeight;
                    int   overlay_stride = (overlay.PixelWidth * canvas_bpp + 7) / 8;
                    int   overlay_size   = overlay_stride * overlay.PixelHeight;
                    int   pos            = meta.OffsetY * canvas_stride + meta.OffsetX * canvas_bpp / 8;
                    overlay.CopyPixels(Int32Rect.Empty, (IntPtr)(buffer + pos), canvas_size - pos, canvas_stride);
                }
                var rect = new Int32Rect(meta.OffsetX, meta.OffsetY, overlay.PixelWidth, overlay.PixelHeight);
                canvas.AddDirtyRect(rect);
                canvas.Unlock();
                return(canvas);
            }
        }
Example #18
0
        public override ImageData Read(IBinaryStream file, ImageMetaData info)
        {
            var reader   = new CbfReader(file, (CbfMetaData)info);
            var pixels   = reader.Unpack();
            var alp_name = Path.ChangeExtension(file.Name, "alp");

            if (VFS.FileExists(alp_name))
            {
                try
                {
                    using (var alp = VFS.OpenBinaryStream(alp_name))
                        return(ReadAlpha(alp, info, pixels));
                }
                catch { /* ignore mask read errors */ }
            }
            return(ImageData.Create(info, PixelFormats.Bgr24, null, pixels));
        }
Example #19
0
 BitmapSource ReadBitmapFromFile(string filename)
 {
     using (var input = VFS.OpenBinaryStream(filename))
     {
         var image = Read(input);
         if (null == image)
         {
             throw new InvalidFormatException("Invalid IPT tile format.");
         }
         var bitmap = image.Bitmap;
         if (bitmap.Format.BitsPerPixel != 32)
         {
             bitmap = new FormatConvertedBitmap(bitmap, PixelFormats.Bgr32, null, 0);
         }
         return(bitmap);
     }
 }
Example #20
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),
                });
            }
        }
Example #21
0
        static public RioReader Create(ArcView file)
        {
            if (CRioArchive.RioSignature == file.View.ReadUInt32(0))
            {
                return(new RioReader(file));
            }

            if (file.Name.HasExtension(".ici"))
            {
                return(null);
            }
            var ici_name = file.Name + ".ici";

            if (!VFS.FileExists(ici_name))
            {
                ici_name = Path.ChangeExtension(file.Name, ".ici");
                if (!VFS.FileExists(ici_name))
                {
                    return(null);
                }
            }
            byte[] ici_data;
            using (var ici = VFS.OpenBinaryStream(ici_name))
                ici_data = ReadIci(ici, IciKey);

            CObjectArcMan arc_man;

            using (var ici = new BinMemoryStream(ici_data))
            {
                var rio = new CRioArchive(ici);
                arc_man = rio.DeserializeRoot() as CObjectArcMan;
                if (null == arc_man)
                {
                    return(null);
                }
            }
            var arc_object = arc_man.ArcList.FirstOrDefault();

            if (null == arc_object || !VFS.IsPathEqualsToFileName(file.Name, arc_object.RioName))
            {
                return(null);
            }
            return(new RioReader(arc_man, file));
        }
Example #22
0
        byte[] LoadBaseImage(string name)
        {
            // judging by the code, files with "pb3" extension could as well contain PNG or BMP images,
            // so we couldn't just shortcut to another instance of Pb3Reader here.

            var path = VFS.GetDirectoryName(m_info.FileName);

            name = VFS.CombinePath(path, name);
            if (name.Equals(m_info.FileName, StringComparison.InvariantCultureIgnoreCase))
            {
                throw new InvalidFormatException();
            }
            // two files referencing each other still could create infinite recursion
            using (var base_file = VFS.OpenBinaryStream(name))
            {
                var image_data = ImageFormat.Read(base_file);
                int stride     = image_data.Bitmap.PixelWidth * 4;
                var pixels     = new byte[stride * image_data.Bitmap.PixelHeight];
                image_data.Bitmap.CopyPixels(pixels, stride, 0);
                return(pixels);
            }
        }
Example #23
0
        public override ArcFile TryOpen(ArcView file)
        {
            if (!file.Name.HasExtension(".dat"))
            {
                return(null);
            }
            var idx_name = VFS.ChangeFileName(file.Name, "index.idx");

            if (!VFS.FileExists(idx_name))
            {
                return(null);
            }
            using (var index = VFS.OpenBinaryStream(idx_name))
            {
                var dir = new List <Entry>();
                while (index.PeekByte() != -1)
                {
                    var name = index.ReadCString(0x34);
                    if (string.IsNullOrEmpty(name))
                    {
                        break;
                    }
                    var entry = Create <Entry> (name);
                    entry.Offset = index.ReadUInt32();
                    entry.Size   = index.ReadUInt32();
                    if (!entry.CheckPlacement(file.MaxOffset))
                    {
                        return(null);
                    }
                    dir.Add(entry);
                }
                if (0 == dir.Count)
                {
                    return(null);
                }
                return(new ArcFile(file, this, dir));
            }
        }
Example #24
0
        uint WriteAmiEntry(PackedEntry entry, Stream output)
        {
            uint packed_size = 0;

            using (var input = VFS.OpenBinaryStream(entry))
            {
                long file_size = input.Length;
                if (file_size > uint.MaxValue)
                {
                    throw new FileSizeException();
                }
                entry.UnpackedSize = (uint)file_size;
                if ("image" == entry.Type)
                {
                    packed_size = WriteImageEntry(entry, input, output);
                }
                else
                {
                    input.AsStream.CopyTo(output);
                }
            }
            return(packed_size);
        }
Example #25
0
        public override ImageData Read(IBinaryStream file, ImageMetaData info)
        {
            var meta   = (TxtMetaData)info;
            var bitmap = new WriteableBitmap(meta.iWidth, meta.iHeight, ImageData.DefaultDpiX,
                                             ImageData.DefaultDpiY, PixelFormats.Bgra32, null);
            var dir = VFS.GetDirectoryName(meta.FileName);

            foreach (var tile in meta.Tiles)
            {
                var filename = VFS.CombinePath(dir, tile.FileName);
                using (var input = VFS.OpenBinaryStream(filename))
                {
                    var tile_info = DetFormat.Value.ReadMetaData(input) as DetBmpMetaData;
                    if (null == tile_info)
                    {
                        throw new InvalidFormatException("Invalid uGOS tile bitmap.");
                    }
                    if (tile.X >= meta.iWidth || tile.Y >= meta.iHeight)
                    {
                        continue;
                    }
                    var reader = new DetBmpFormat.Reader(input, tile_info);
                    reader.Unpack();
                    int width    = Math.Min(tile_info.iWidth, meta.iWidth - tile.X);
                    int height   = Math.Min(tile_info.iHeight, meta.iHeight - tile.Y);
                    var src_rect = new Int32Rect(0, 0, width, height);
                    bitmap.WritePixels(src_rect, reader.Data, reader.Stride, tile.X, tile.Y);
                }
            }
            var flipped = new TransformedBitmap(bitmap, new ScaleTransform {
                ScaleY = -1
            });

            flipped.Freeze();
            return(new ImageData(flipped, meta));
        }
Example #26
0
        public override ImageData Read(IBinaryStream stream, ImageMetaData info)
        {
            var           meta      = (CrxdMetaData)info;
            IBinaryStream base_file = OpenByOffset(meta.BaseOffset);

            if (null == base_file)
            {
                var dir_name = VFS.GetDirectoryName(meta.FileName);
                var name     = VFS.CombinePath(dir_name, meta.BaseFileName);
                if (!VFS.FileExists(name))
                {
                    throw new FileNotFoundException("Base image not found", meta.BaseFileName);
                }
                base_file = VFS.OpenBinaryStream(name);
            }
            using (base_file)
            {
                var base_info = base.ReadMetaData(base_file) as CrxMetaData;
                if (null == base_info || base_info.BPP != info.BPP)
                {
                    throw new InvalidFormatException("Invalid base image");
                }
                using (var reader = new Reader(base_file, base_info))
                    using (var crx = OpenDiffStream(stream, meta))
                        using (var diff_reader = new Reader(crx, meta.DiffInfo))
                        {
                            reader.Unpack(true);
                            diff_reader.Unpack(true);
                            var diff_rect = new Rectangle(meta.OffsetX, meta.OffsetY, (int)meta.Width, (int)meta.Height);
                            var base_rect = new Rectangle(base_info.OffsetX, base_info.OffsetY,
                                                          (int)base_info.Width, (int)base_info.Height);
                            diff_rect = Rectangle.Intersect(diff_rect, base_rect);
                            if (diff_rect.IsEmpty)
                            {
                                return(ImageData.Create(base_info, reader.Format, reader.Palette, reader.Data, reader.Stride));
                            }

                            int pixel_size = base_info.BPP / 8;
                            int x          = diff_rect.X - base_rect.X;
                            int y          = diff_rect.Y - base_rect.Y;
                            int dst        = y * reader.Stride + pixel_size * x;
                            var image      = reader.Data;

                            int dx   = diff_rect.X - meta.OffsetX;
                            int dy   = diff_rect.Y - meta.OffsetY;
                            int src  = dy * diff_reader.Stride + pixel_size * dx;
                            var diff = diff_reader.Data;

                            int blend_stride = diff_rect.Width * pixel_size;
                            for (int row = 0; row < diff_rect.Height; ++row)
                            {
                                for (int i = 0; i < blend_stride; i += pixel_size)
                                {
                                    image[dst + i]     += diff[src + i];
                                    image[dst + i + 1] += diff[src + i + 1];
                                    image[dst + i + 2] += diff[src + i + 2];
                                    if (4 == pixel_size)
                                    {
                                        image[dst + i + 3] -= diff[src + i + 3];
                                    }
                                }
                                dst += reader.Stride;
                                src += diff_reader.Stride;
                            }
                            return(ImageData.Create(base_info, reader.Format, reader.Palette, image, reader.Stride));
                        }
            }
        }
Example #27
0
        public override ArcFile TryOpen(ArcView file)
        {
            if (!file.Name.HasExtension(".bin"))
            {
                return(null);
            }
            var idx_name = Path.ChangeExtension(file.Name, "idx");

            if (!VFS.FileExists(idx_name))
            {
                return(null);
            }
            var scheme = QueryScheme(file.Name);

            if (null == scheme)
            {
                return(null);
            }
            var dir = new List <Entry>();

            using (var idx = VFS.OpenBinaryStream(idx_name))
                using (var aes = Aes.Create())
                {
                    aes.Padding = PaddingMode.PKCS7;
                    aes.Mode    = CipherMode.CBC;
                    aes.KeySize = 128;
                    aes.Key     = scheme.Key;
                    aes.IV      = scheme.IV;
                    var input_buffer = new byte[0x100];
                    var unpacker     = new BinDeserializer();
                    while (idx.PeekByte() != -1)
                    {
                        int length = idx.ReadInt32();
                        if (length <= 0)
                        {
                            return(null);
                        }
                        if (length > input_buffer.Length)
                        {
                            input_buffer = new byte[length];
                        }
                        if (idx.Read(input_buffer, 0, length) < length)
                        {
                            return(null);
                        }
                        using (var decryptor = aes.CreateDecryptor())
                            using (var encrypted = new MemoryStream(input_buffer, 0, length))
                                using (var input = new InputCryptoStream(encrypted, decryptor))
                                {
                                    var info     = unpacker.DeserializeEntry(input);
                                    var filename = info["fileName"] as string;
                                    if (string.IsNullOrEmpty(filename))
                                    {
                                        return(null);
                                    }
                                    filename = filename.TrimStart('/', '\\');
                                    var entry = Create <Entry> (filename);
                                    entry.Offset = Convert.ToInt64(info["index"]);
                                    entry.Size   = Convert.ToUInt32(info["size"]);
                                    if (!entry.CheckPlacement(file.MaxOffset))
                                    {
                                        return(null);
                                    }
                                    dir.Add(entry);
                                }
                    }
                }
            if (0 == dir.Count)
            {
                return(null);
            }
            var arc_aes = Aes.Create();

            arc_aes.Padding = PaddingMode.PKCS7;
            arc_aes.Mode    = CipherMode.CBC;
            arc_aes.KeySize = 256;
            arc_aes.Key     = scheme.Key;
            arc_aes.IV      = scheme.IV;
            return(new BinArchive(file, this, dir, arc_aes));
        }
Example #28
0
 public IniReader(string ini_name)
 {
     m_ini = VFS.OpenBinaryStream(ini_name);
 }
Example #29
0
        ImageData TryBlendImage(string base_name, GsaReader overlay, ImageMetaData overlay_info)
        {
            int ovl_x      = overlay_info.OffsetX;
            int ovl_y      = overlay_info.OffsetY;
            int ovl_width  = (int)overlay_info.Width;
            int ovl_height = (int)overlay_info.Height;

            if (ovl_x < 0)
            {
                ovl_width += ovl_x;
                ovl_x      = 0;
            }
            if (ovl_y < 0)
            {
                ovl_height += ovl_y;
                ovl_y       = 0;
            }
            using (var input = VFS.OpenBinaryStream(base_name))
            {
                var base_info = ReadMetaData(input) as GsaMetaData;
                if (null == base_info)
                {
                    return(null);
                }
                int base_width  = (int)base_info.Width;
                int base_height = (int)base_info.Height;
                if (checked (ovl_x + ovl_width) > base_width)
                {
                    ovl_width = base_width - ovl_x;
                }
                if (checked (ovl_y + ovl_height) > base_height)
                {
                    ovl_height = base_height - ovl_y;
                }
                if (ovl_height <= 0 || ovl_width <= 0)
                {
                    return(null);
                }

                input.Position = 0;
                var reader      = new GsaReader(input, base_info);
                var base_pixels = reader.Unpack();

                int src_pixel_size = overlay.PixelSize;
                int dst_pixel_size = reader.PixelSize;
                int dst            = ovl_y * reader.Stride + ovl_x * dst_pixel_size;
                int src            = 0;
                for (int y = 0; y < ovl_height; ++y)
                {
                    int src_pixel = src;
                    int dst_pixel = dst;
                    for (int x = 0; x < ovl_width; ++x)
                    {
                        int src_alpha = overlay.Data[src_pixel + 3];
                        if (src_alpha > 0)
                        {
                            if (0xFF == src_alpha)
                            {
                                Buffer.BlockCopy(overlay.Data, src_pixel, base_pixels, dst_pixel, dst_pixel_size);
                            }
                            else // assume destination has no alpha channel
                            {
                                base_pixels[dst_pixel + 0] = (byte)((overlay.Data[src_pixel + 0] * src_alpha
                                                                     + base_pixels[dst_pixel + 0] * (0xFF - src_alpha)) / 0xFF);
                                base_pixels[dst_pixel + 1] = (byte)((overlay.Data[src_pixel + 1] * src_alpha
                                                                     + base_pixels[dst_pixel + 1] * (0xFF - src_alpha)) / 0xFF);
                                base_pixels[dst_pixel + 2] = (byte)((overlay.Data[src_pixel + 2] * src_alpha
                                                                     + base_pixels[dst_pixel + 2] * (0xFF - src_alpha)) / 0xFF);
                            }
                        }
                        src_pixel += src_pixel_size;
                        dst_pixel += dst_pixel_size;
                    }
                    src += overlay.Stride;
                    dst += reader.Stride;
                }
                return(ImageData.CreateFlipped(base_info, reader.Format, null, base_pixels, reader.Stride));
            }
        }
Example #30
0
        public override ImageData Read(IBinaryStream input, ImageMetaData info)
        {
            var meta   = (AgfMetaData)info;
            var pixels = new byte[meta.Width * meta.Height * 4];
            int dst    = 0;

            input.Position = meta.DataOffset;
            while (dst < pixels.Length)
            {
                uint op    = input.ReadUInt32();
                int  count = (int)(op >> 8);
                switch (op & 0xFF)
                {
                case 1:
                    count *= 4;
                    input.Read(pixels, dst, count);
                    break;

                case 2:
                    input.Read(pixels, dst, 4);
                    count *= 4;
                    Binary.CopyOverlapped(pixels, dst, dst + 4, count - 4);
                    break;

                case 3:
                    int chunk_size = (count >> 8) * 4;
                    count = (count & 0xFF) * chunk_size;
                    input.Read(pixels, dst, chunk_size);
                    Binary.CopyOverlapped(pixels, dst, dst + chunk_size, count - chunk_size);
                    break;

                case 4:
                    int offset = (count & 0xFFF) * 4;
                    count = (count >> 12) * 4;
                    Binary.CopyOverlapped(pixels, dst - offset, dst, count);
                    break;

                case 5:
                    count = (count >> 8) & 0xFF;
                    input.Seek((count - count / 4) * 4, SeekOrigin.Current);
                    count *= 4;
                    break;

                default:
                    throw new InvalidFormatException();
                }
                dst += count;
            }
            if (0 != (meta.Flags & 0x10) && 0 != meta.BaseNameOffset)
            {
                try
                {
                    var base_name = ReadBaseName(input, meta);
                    if (VFS.FileExists(base_name))
                    {
                        using (var base_file = VFS.OpenBinaryStream(base_name))
                        {
                            var base_image = Read(base_file);
                            BlendImage(meta, pixels, base_image.Bitmap);
                        }
                    }
                }
                catch (Exception X)
                {
                    Trace.WriteLine(string.Format("{0}: baseline image read error: {1}",
                                                  meta.FileName, X.Message), "[AGF]");
                }
            }
            return(ImageData.Create(info, PixelFormats.Bgra32, null, pixels));
        }