public Bitmap BitmapFromFrame(int frameNo) { Bitmap bmp = null; if (!IsEmpty() && (frameNo >= 0) && (frameNo < m_frames.Count)) { spPalette pal = m_palettes[0]; spPixels pix = m_frames[frameNo]; int width = pix.m_width * 2; int height = pix.m_height; bmp = new Bitmap(width, height); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { byte px = pix.m_pixels[(y * pix.m_width) + (x >> 1)]; if (0 == (x & 1)) { px >>= 4; } px &= 0xF; Color c = pal.colors[px]; if (0 == px) { c = Color.FromArgb(0, c.R, c.G, c.B); } bmp.SetPixel(x, y, c); } } } return(bmp); }
public spData(string pathName) { Console.WriteLine("import: {0}", pathName); using (BinaryReader b = new BinaryReader( File.Open(pathName, FileMode.Open))) { // Use BaseStream. long fileLength = b.BaseStream.Length; UInt32 header = b.ReadUInt32(); UInt16 v0 = 0; UInt16 v1 = 0; while (0xFFFFFFFF != header) { UInt16 length = b.ReadUInt16(); Console.WriteLine("0x{0:X8} {1}{2}{3}{4} - {5}", header, (char)((header >> 0) & 0xFF), (char)((header >> 8) & 0xFF), (char)((header >> 16) & 0xFF), (char)((header >> 24) & 0xFF), length ); switch (header) { // SPRITE FILE HEADER case 0x41454853: // 'SHEA' Debug.Assert(4 == length); v0 = b.ReadUInt16(); v1 = b.ReadUInt16(); Console.WriteLine("v = {0:X4}", v0); Console.WriteLine("v = {0:X4}", v1); break; // SPRITE PALETTE ENTRIES case 0x45505353: // 'SSPE' { // 50 // 99 // byte pal num // 16 color (3 bytes each) // pal num = 0xFF end while (true) { byte palNo = b.ReadByte(); if (0xFF == palNo) { break; } Console.WriteLine("Read palNo {0}", palNo); spPalette pal = new spPalette(); byte red; byte green; byte blue; for (int idx = 0; idx < 16; ++idx) { red = b.ReadByte(); green = b.ReadByte(); blue = b.ReadByte(); pal.colors.Add(Color.FromArgb(255, red, green, blue)); } m_palettes.Add(pal); } } break; // SPRITE PACKED PIXELS case 0x50505353: // 'SSPP' { // 10 byte header // b 0 // b 0 // b width in bytes (2x this for pixels) // b height in bytes // b 0 // w xoff // w yoff // b 0 byte ub = 0; ub = b.ReadByte(); // Discard Debug.Assert(0 == ub); ub = b.ReadByte(); // Discard Debug.Assert(0 == ub); byte width = b.ReadByte(); byte height = b.ReadByte(); b.ReadByte(); // Discard Int16 xoff = b.ReadInt16(); Int16 yoff = b.ReadInt16(); if (v1 > 0x100) { ub = b.ReadByte(); // Discard } //Debug.Assert(0 == ub); Console.WriteLine(" {0}x{1} {2},{3}", width, height, xoff, yoff ); if (v1 > 0x100) { length -= 10; } else { length -= 9; } // Read in the pixels List <byte> pixels = new List <byte>(); // pixel data (w x h) while (0 != length) { pixels.Add(b.ReadByte()); length--; } // Create a pixel frame spPixels pixelFrame = new spPixels(width, height, pixels); pixelFrame.m_offset_x = xoff; pixelFrame.m_offset_y = yoff; // Add it to the list m_frames.Add(pixelFrame); } break; case 0x4D4E4153: // 'SANM' /* * dw <size in bytes> ; including these 2 bytes * db #of lines in the seqence * * db <frameNo> or 0xFF means command * * ; Loop * 01 * ; Goto * 02, targetLo, TargetHi * * ; SetSpeed * 06, speedlo, speed hi * ; Loop * 0A * ; Process * 0E, <byte>processNum * ; NOP * 0D, NOP * ; GotoLast (return) * 10 * * GotoSeq * MultiOp * Sound XX * HFlip * VFlip * SetFlag * BRK */ while (0 != length) { b.ReadByte(); length--; } break; default: { // Skip the Chunk, since we don't know what it is while (0 != length) { b.ReadByte(); length--; } break; } } header = b.ReadUInt32(); } #if false switch (header) { // SPRITE FILE HEADER case 'SHEA': break; case 'SSPE': break; case 'SSPP': case 'SOBJ': case 'SANM': case 'SRCT': } #endif } }