void ReadPaletteChunk(BitStream bs) { byte[] new_palette = new byte[256 * 3]; bs.AssertAtByteBoundary(); bs.ZeroReadCount(); int paletteLength = bs.ReadByte(); Console.WriteLine("palette chunk is {0} bytes long", paletteLength * 4); // byte[] palBuf = new byte[paletteLength * 4 - 1]; // bs.Read (palBuf, 0, palBuf.Length); // MemoryStream palStream = new MemoryStream (palBuf, false); BitStream palStream = bs; int new_index = 0, index = 0; while (new_index < 256) { byte p = (byte)palStream.ReadByte(); if ((p & 0x80) == 0x80) { int count = (p & 0x7f) + 1; // Console.WriteLine ("copy1 {0} entries, from {1} to {2}", count, index, new_index); Array.Copy(palette, index * 3, new_palette, new_index * 3, count * 3); index += count; new_index += count; } else if ((p & 0x40) == 0x40) { int count = (p & 0x3f) + 1; int tmp_index = palStream.ReadByte(); // Console.WriteLine ("copy2 {0} entries, from {1} to {2}", count, tmp_index, new_index); Array.Copy(palette, tmp_index * 3, new_palette, new_index * 3, count * 3); new_index += count; } else if ((p & 0xc0) == 0x00) { int b = p & 0x3f; int g = palStream.ReadByte() & 0x3f; int r = palStream.ReadByte() & 0x3f; new_palette [new_index * 3] = palmap[r]; new_palette [new_index * 3 + 1] = palmap[g]; new_palette [new_index * 3 + 2] = palmap[b]; // Console.WriteLine ("Assign palette[{0}] = {1},{2},{3}", new_index, // palmap[r], palmap[g], palmap[b]); new_index++; } } palette = new_palette; // Console.WriteLine ("read {0} bytes toward filling the palette", palBuf.Length + 1); }
public override void Build(BitStream bs) { bs.ZeroReadCount(); #if false int tag = bs.ReadBit(); if (tag == 1) { #endif lowTree = new HuffmanTree(); Console.WriteLine("building low tree"); lowTree.Build(bs); Console.WriteLine("low tree ends at {0:x}", bs.ReadCount); #if false } tag = bs.ReadBit(); if (tag == 1) { #endif highTree = new HuffmanTree(); Console.WriteLine("building high tree"); highTree.Build(bs); Console.WriteLine("high tree ends at {0:x}", bs.ReadCount); #if false } #endif markers[0] = bs.ReadWord(); markers[1] = bs.ReadWord(); markers[2] = bs.ReadWord(); Console.WriteLine("markers = {0:x}, {1:x}, {2:x}", markers[0], markers[1], markers[2]); Read(bs); Console.WriteLine("shortest1 = {0}", shortest[0]); Console.WriteLine("shortest2 = {0}", shortest[1]); Console.WriteLine("shortest3 = {0}", shortest[2]); }
void ReadPaletteChunk (BitStream bs) { byte[] new_palette = new byte[256 * 3]; bs.AssertAtByteBoundary (); bs.ZeroReadCount (); int paletteLength = bs.ReadByte(); Console.WriteLine ("palette chunk is {0} bytes long", paletteLength * 4); // byte[] palBuf = new byte[paletteLength * 4 - 1]; // bs.Read (palBuf, 0, palBuf.Length); // MemoryStream palStream = new MemoryStream (palBuf, false); BitStream palStream = bs; int new_index = 0, index = 0; while (new_index < 256) { byte p = (byte)palStream.ReadByte(); if ((p & 0x80) == 0x80) { int count = (p & 0x7f) + 1; // Console.WriteLine ("copy1 {0} entries, from {1} to {2}", count, index, new_index); Array.Copy (palette, index * 3, new_palette, new_index * 3, count * 3); index += count; new_index += count; } else if ((p & 0x40) == 0x40) { int count = (p & 0x3f) + 1; int tmp_index = palStream.ReadByte(); // Console.WriteLine ("copy2 {0} entries, from {1} to {2}", count, tmp_index, new_index); Array.Copy (palette, tmp_index * 3, new_palette, new_index * 3, count * 3); new_index += count; } else if ((p & 0xc0) == 0x00) { int b = p & 0x3f; int g = palStream.ReadByte() & 0x3f; int r = palStream.ReadByte() & 0x3f; new_palette [new_index * 3] = palmap[r]; new_palette [new_index * 3 + 1] = palmap[g]; new_palette [new_index * 3 + 2] = palmap[b]; // Console.WriteLine ("Assign palette[{0}] = {1},{2},{3}", new_index, // palmap[r], palmap[g], palmap[b]); new_index ++; } } palette = new_palette; // Console.WriteLine ("read {0} bytes toward filling the palette", palBuf.Length + 1); }
void ReadTrees () { Console.WriteLine ("Building huffman trees (from position {0:x}, length {1:x}):", stream.Position, treesSize); byte[] tree_buf = new byte[treesSize]; stream.Read (tree_buf, 0, (int)treesSize); BitStream bs = new BitStream (tree_buf); int tag; BitStream.debug = true; #if false tag = bs.ReadBit (); if (tag == 1) { #endif Console.WriteLine (" + MMap"); mmap_Tree = new Dynamic16HuffmanTree (); mmap_Tree.Build (bs); Console.WriteLine (" + After MMap tree, read {0} bytes", bs.ReadCount); #if false } #endif BitStream.debug = false; bs.ZeroReadCount (); #if false tag = bs.ReadBit (); if (tag == 1) { #endif Console.WriteLine (" + MClr"); mclr_Tree = new Dynamic16HuffmanTree (); mclr_Tree.Build (bs); Console.WriteLine (" + After MClr tree, read {0} bytes", bs.ReadCount); #if false } bs.ZeroReadCount (); tag = bs.ReadBit (); if (tag == 1) { #endif Console.WriteLine (" + Full"); full_Tree = new Dynamic16HuffmanTree (); full_Tree.Build (bs); Console.WriteLine (" + After Full tree, read {0} bytes", bs.ReadCount); #if false } bs.ZeroReadCount (); tag = bs.ReadBit (); if (tag == 1) { #endif Console.WriteLine (" + Type"); type_Tree = new Dynamic16HuffmanTree (); type_Tree.Build (bs); Console.WriteLine (" + After Type tree, read {0} bytes", bs.ReadCount); #if false } #endif Console.WriteLine ("done (read {0} out of {1} bytes).", bs.ReadCount, treesSize); }
public override void Build (BitStream bs) { bs.ZeroReadCount (); #if false int tag = bs.ReadBit(); if (tag == 1) { #endif lowTree = new HuffmanTree (); Console.WriteLine ("building low tree"); lowTree.Build (bs); Console.WriteLine ("low tree ends at {0:x}", bs.ReadCount); #if false } tag = bs.ReadBit (); if (tag == 1) { #endif highTree = new HuffmanTree (); Console.WriteLine ("building high tree"); highTree.Build (bs); Console.WriteLine ("high tree ends at {0:x}", bs.ReadCount); #if false } #endif markers[0] = bs.ReadWord (); markers[1] = bs.ReadWord (); markers[2] = bs.ReadWord (); Console.WriteLine ("markers = {0:x}, {1:x}, {2:x}", markers[0], markers[1], markers[2]); Read (bs); Console.WriteLine ("shortest1 = {0}", shortest[0]); Console.WriteLine ("shortest2 = {0}", shortest[1]); Console.WriteLine ("shortest3 = {0}", shortest[2]); }
void ReadTrees() { Console.WriteLine("Building huffman trees (from position {0:x}, length {1:x}):", stream.Position, treesSize); byte[] tree_buf = new byte[treesSize]; stream.Read(tree_buf, 0, (int)treesSize); BitStream bs = new BitStream(tree_buf); int tag; BitStream.debug = true; #if false tag = bs.ReadBit(); if (tag == 1) { #endif Console.WriteLine(" + MMap"); mmap_Tree = new Dynamic16HuffmanTree(); mmap_Tree.Build(bs); Console.WriteLine(" + After MMap tree, read {0} bytes", bs.ReadCount); #if false } #endif BitStream.debug = false; bs.ZeroReadCount(); #if false tag = bs.ReadBit(); if (tag == 1) { #endif Console.WriteLine(" + MClr"); mclr_Tree = new Dynamic16HuffmanTree(); mclr_Tree.Build(bs); Console.WriteLine(" + After MClr tree, read {0} bytes", bs.ReadCount); #if false } bs.ZeroReadCount(); tag = bs.ReadBit(); if (tag == 1) { #endif Console.WriteLine(" + Full"); full_Tree = new Dynamic16HuffmanTree(); full_Tree.Build(bs); Console.WriteLine(" + After Full tree, read {0} bytes", bs.ReadCount); #if false } bs.ZeroReadCount(); tag = bs.ReadBit(); if (tag == 1) { #endif Console.WriteLine(" + Type"); type_Tree = new Dynamic16HuffmanTree(); type_Tree.Build(bs); Console.WriteLine(" + After Type tree, read {0} bytes", bs.ReadCount); #if false } #endif Console.WriteLine("done (read {0} out of {1} bytes).", bs.ReadCount, treesSize); }