public override void SetData(object Data) { Bitmap _Data = Data as Bitmap; if (_Data.PixelFormat != PixelFormat.Format32bppArgb && _Data.PixelFormat != PixelFormat.Format8bppIndexed) { throw new FormatException("지원하지 않는 포맷의 이미지입니다."); } enum_graphic_format NewFormat = enum_graphic_format.Unknown; switch (_Data.PixelFormat) { case PixelFormat.Format8bppIndexed: NewFormat = enum_graphic_format.WithPalette; break; case PixelFormat.Format32bppArgb: NewFormat = enum_graphic_format.General; break; } if (m_format != NewFormat) { throw new FormatException("포맷이 일치하지 않습니다."); } if (m_width_actual != _Data.Width || m_height != _Data.Height) { throw new FormatException("이미지 크기가 일치하지 않습니다."); } m_graphic = _Data; m_generated_graphic = false; }
public cv2() : base() { m_type = cvnType.Graphic; m_format = enum_graphic_format.Unknown; m_width_actual = m_height = m_width_data = 0; m_graphic = null; }
public void Open(Stream fp, Stream PalettePath) { fp.Seek(0, SeekOrigin.Begin); if (PalettePath != null) { PalettePath.Seek(0, SeekOrigin.Begin); } byte[] header = new byte[0x11]; fp.Read(header, 0, 0x11); m_raw_format = header[0]; switch (m_raw_format) { case 0x08: m_format = enum_graphic_format.WithPalette; break; case 0x18: case 0x20: m_format = enum_graphic_format.General; break; default: m_format = enum_graphic_format.Unknown; break; } byte[] buf = new byte[4]; Array.Copy(header, 1, buf, 0, 4); m_width_actual = LittleEndian.FromEndian(buf); Array.Copy(header, 5, buf, 0, 4); m_height = LittleEndian.FromEndian(buf); Array.Copy(header, 9, buf, 0, 4); m_width_data = LittleEndian.FromEndian(buf); Array.Copy(header, 13, buf, 0, 4); m_unknown_field = LittleEndian.FromEndian(buf); PixelFormat image_format; switch (m_format) { case enum_graphic_format.General: image_format = PixelFormat.Format32bppArgb; break; case enum_graphic_format.WithPalette: image_format = PixelFormat.Format16bppArgb1555; break; default: throw new FormatException("지원되지 않는 포맷의 cv2입니다."); } byte[] m_raw = new byte[m_length]; fp.Read(m_raw, 0, m_length); m_graphic = new Bitmap(m_width_actual, m_height, image_format); m_generated_graphic = true; BitmapData raw_data; raw_data = m_graphic.LockBits( new Rectangle(0, 0, m_width_actual, m_height), ImageLockMode.WriteOnly, image_format); if (m_format == enum_graphic_format.WithPalette) { if (PalettePath == null) { m_graphic.Dispose(); throw new ArgumentException("팔레트 파일이 지정되지 않았습니다.", "PalettePath"); } if (PalettePath.ReadByte() != 0x10) { m_graphic.Dispose(); throw new FormatException("팔레트 파일이 올바르지 않습니다."); } m_palette = new ushort[256]; byte[] palette_data = new byte[512]; PalettePath.Read(palette_data, 0, 512); for (int i = 0; i < 512; i += 2) { int t = palette_data[i] + (palette_data[i + 1] << 8); m_palette[i / 2] = (ushort)t; } } int bitmap_len = raw_data.Stride * raw_data.Height; byte[] bitmap_buffer = new byte[bitmap_len]; System.Runtime.InteropServices.Marshal.Copy(raw_data.Scan0, bitmap_buffer, 0, bitmap_len); for (int i = 0; i < m_height; i++) { int startAt_buf = i * m_width_in_bytes; int startAt_image = i * raw_data.Stride; for (int j = 0; j < m_actual_width_in_bytes; j++) { switch (m_format) { case enum_graphic_format.General: bitmap_buffer[startAt_image + j] = m_raw[startAt_buf + j]; break; case enum_graphic_format.WithPalette: byte index = m_raw[startAt_buf + j]; bitmap_buffer[startAt_image + j * 2] = (byte)(m_palette[index] & 0xff); bitmap_buffer[startAt_image + j * 2 + 1] = (byte)(m_palette[index] >> 8); break; } } } System.Runtime.InteropServices.Marshal.Copy(bitmap_buffer, 0, raw_data.Scan0, bitmap_len); m_graphic.UnlockBits(raw_data); }