public override ImageData Read(IBinaryStream stream, ImageMetaData info) { using (var reader = new PgdReader(stream, (PgdGeMetaData)info)) { var pixels = reader.UnpackGE(); return(ImageData.Create(info, reader.Format, null, pixels)); } }
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)); }