public static long FindChunk(Stream stream, string chunk) { long found_offset = -1; var file = new ArcView.Reader(stream); try { char[] buf = new char[4]; file.BaseStream.Position = 8; while (-1 != file.PeekChar()) { long chunk_offset = file.BaseStream.Position; uint chunk_size = Binary.BigEndian(file.ReadUInt32()); if (4 != file.Read(buf, 0, 4)) { break; } if (chunk.SequenceEqual(buf)) { found_offset = chunk_offset; break; } file.BaseStream.Position += chunk_size + 4; } } catch { // ignore errors } finally { file.Dispose(); } return(found_offset); }
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); }