public override ImageMetaData ReadMetaData(Stream stream) { if (0xff != stream.ReadByte() || 0xd8 != stream.ReadByte()) { return(null); } using (var file = new ArcView.Reader(stream)) { while (-1 != file.PeekChar()) { ushort marker = Binary.BigEndian(file.ReadUInt16()); if ((marker & 0xff00) != 0xff00) { break; } int length = Binary.BigEndian(file.ReadUInt16()); if ((marker & 0x00f0) == 0xc0 && marker != 0xffc4) { if (length < 8) { break; } int bits = file.ReadByte(); uint height = Binary.BigEndian(file.ReadUInt16()); uint width = Binary.BigEndian(file.ReadUInt16()); int components = file.ReadByte(); return(new ImageMetaData { Width = width, Height = height, BPP = bits * components, }); } file.BaseStream.Seek(length - 2, SeekOrigin.Current); } return(null); } }
public override ImageMetaData ReadMetaData(Stream stream) { if (0xff != stream.ReadByte() || 0xd8 != stream.ReadByte()) return null; using (var file = new ArcView.Reader (stream)) { while (-1 != file.PeekChar()) { ushort marker = Binary.BigEndian (file.ReadUInt16()); if ((marker & 0xff00) != 0xff00) break; int length = Binary.BigEndian (file.ReadUInt16()); if ((marker & 0x00f0) == 0xc0 && marker != 0xffc4) { if (length < 8) break; int bits = file.ReadByte(); uint height = Binary.BigEndian (file.ReadUInt16()); uint width = Binary.BigEndian (file.ReadUInt16()); int components = file.ReadByte(); return new ImageMetaData { Width = width, Height = height, BPP = bits * components, }; } file.BaseStream.Seek (length-2, SeekOrigin.Current); } return null; } }
public override ImageMetaData ReadMetaData(Stream stream) { using (var file = new ArcView.Reader(stream)) { short id_length = file.ReadByte(); short colormap_type = file.ReadByte(); if (colormap_type > 1) { return(null); } short image_type = file.ReadByte(); ushort colormap_first = file.ReadUInt16(); ushort colormap_length = file.ReadUInt16(); short colormap_depth = file.ReadByte(); int pos_x = file.ReadInt16(); int pos_y = file.ReadInt16(); uint width = file.ReadUInt16(); uint height = file.ReadUInt16(); int bpp = file.ReadByte(); if (bpp != 32 && bpp != 24 && bpp != 16 && bpp != 15 && bpp != 8) { return(null); } short descriptor = file.ReadByte(); uint colormap_offset = (uint)(18 + id_length); switch (image_type) { default: return(null); case 1: // Uncompressed, color-mapped images. case 9: // Runlength encoded color-mapped images. case 32: // Compressed color-mapped data, using Huffman, Delta, and // runlength encoding. case 33: // Compressed color-mapped data, using Huffman, Delta, and // runlength encoding. 4-pass quadtree-type process. if (colormap_depth != 24 && colormap_depth != 32) { return(null); } break; case 2: // Uncompressed, RGB images. case 3: // Uncompressed, black and white images. case 10: // Runlength encoded RGB images. case 11: // Compressed, black and white images. break; } return(new TgaMetaData { OffsetX = pos_x, OffsetY = pos_y, Width = width, Height = height, BPP = bpp, ImageType = image_type, ColormapType = colormap_type, ColormapOffset = colormap_offset, ColormapFirst = colormap_first, ColormapLength = colormap_length, ColormapDepth = colormap_depth, Descriptor = descriptor, }); } }
public override ImageMetaData ReadMetaData(Stream stream) { ImageMetaData meta = null; var file = new ArcView.Reader(stream); try { file.ReadUInt32(); if (file.ReadUInt32() != 0x0a1a0a0d) { return(null); } uint chunk_size = Binary.BigEndian(file.ReadUInt32()); char[] chunk_type = new char[4]; file.Read(chunk_type, 0, 4); if (!chunk_type.SequenceEqual("IHDR")) { return(null); } meta = new ImageMetaData(); meta.Width = Binary.BigEndian(file.ReadUInt32()); meta.Height = Binary.BigEndian(file.ReadUInt32()); int bpp = file.ReadByte(); int color_type = file.ReadByte(); switch (color_type) { case 2: meta.BPP = bpp * 3; break; case 3: meta.BPP = 24; break; case 4: meta.BPP = bpp * 2; break; case 6: meta.BPP = bpp * 4; break; default: meta.BPP = bpp; break; } SkipBytes(file, 7); for (;;) { chunk_size = Binary.BigEndian(file.ReadUInt32()); file.Read(chunk_type, 0, 4); if (chunk_type.SequenceEqual("IDAT") || chunk_type.SequenceEqual("IEND")) { break; } if (chunk_type.SequenceEqual("oFFs")) { int x = Binary.BigEndian(file.ReadInt32()); int y = Binary.BigEndian(file.ReadInt32()); if (0 == file.ReadByte()) { meta.OffsetX = x; meta.OffsetY = y; } break; } SkipBytes(file, chunk_size + 4); } } catch { meta = null; } finally { file.Dispose(); if (stream.CanSeek) { stream.Position = 0; } } return(meta); }