BmpCodec.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // System.Drawing.Imaging.BMPCodec.cs
  3. //
  4. // Author:
  5. // Alexandre Pigolkine ([email protected])
  6. // BITMAPINFOHEADER,Decode functions implemented using code/ideas from
  7. // CxImage (c) 07/Aug/2001 <[email protected]>
  8. //
  9. // (C) 2002/2003 Ximian, Inc.
  10. namespace System.Drawing.Imaging {
  11. using System;
  12. using System.IO;
  13. using System.Drawing.Imaging;
  14. using System.Runtime.InteropServices;
  15. internal struct BITMAPFILEHEADER { // File info header
  16. public ushort bfType; // Specifies the type of file. This member must be BM.
  17. public uint bfSize; // Specifies the size of the file, in bytes.
  18. public ushort bfReserved1; // Reserved; must be set to zero.
  19. public ushort bfReserved2; // Reserved; must be set to zero.
  20. public uint bfOffBits; // Specifies the byte offset from the BITMAPFILEHEADER
  21. // structure to the actual bitmap data in the file.
  22. }
  23. internal enum BitmapFileType : ushort {
  24. BFT_ICON = 0x4349, /* 'IC' */
  25. BFT_BITMAP = 0x4d42, /* 'BM' */
  26. BFT_CURSOR = 0x5450 /* 'PT' */
  27. }
  28. internal enum BitmapCompression : uint {
  29. BI_RGB = 0,
  30. BI_RLE8 = 1,
  31. BI_RLE4 = 2,
  32. BI_BITFIELDS = 3
  33. }
  34. [StructLayout(LayoutKind.Sequential)]
  35. internal struct BITMAPINFOHEADER_FLAT {
  36. internal int biSize;
  37. internal int biWidth;
  38. internal int biHeight;
  39. internal short biPlanes;
  40. internal short biBitCount;
  41. internal int biCompression;
  42. internal int biSizeImage;
  43. internal int biXPelsPerMeter;
  44. internal int biYPelsPerMeter;
  45. internal int biClrUsed;
  46. internal int biClrImportant;
  47. [MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=1024)]
  48. internal byte[] bmiColors;
  49. static int WIDTHBYTES(int i) {
  50. return ((i+31)&(~31))/8; /* ULONG aligned ! */
  51. }
  52. public int DibWidthBytesN(int n) {
  53. return WIDTHBYTES(biWidth * n);
  54. }
  55. public int DibWidthBytes() {
  56. return DibWidthBytesN(biBitCount);
  57. }
  58. public int DibSizeImage() {
  59. return biSizeImage == 0 ? DibWidthBytes() * biHeight : biSizeImage;
  60. }
  61. public int DibNumColors() {
  62. return biClrUsed == 0 && biBitCount <= 8 ? (1 << biBitCount) : biClrUsed;
  63. }
  64. public void FixBitmapInfo(){
  65. if (biSizeImage == 0)
  66. biSizeImage = DibSizeImage();
  67. if (biClrUsed == 0)
  68. biClrUsed = DibNumColors();
  69. }
  70. float HorizontalResolution {
  71. get {
  72. return (float)biXPelsPerMeter * 254.0F / 10000.0F;
  73. }
  74. }
  75. float VerticalResolution {
  76. get {
  77. return (float)biYPelsPerMeter * 254.0F / 10000.0F;
  78. }
  79. }
  80. public void Initialize (BitmapData info)
  81. {
  82. biSize = 40;
  83. biWidth = info.Width;
  84. biHeight = info.Height;
  85. biPlanes = 1;
  86. biBitCount = (short)System.Drawing.Image.GetPixelFormatSize (info.PixelFormat);
  87. biCompression = (int)BitmapCompression.BI_RGB;
  88. biSizeImage = (int) info.Height * info.Width * Image.GetPixelFormatSize (info.PixelFormat) / 8;
  89. biXPelsPerMeter = 0;
  90. biYPelsPerMeter = 0;
  91. biClrUsed = 0;
  92. biClrImportant = 0;
  93. }
  94. }
  95. internal class BMPCodec {
  96. internal BMPCodec() {
  97. }
  98. internal static ImageCodecInfo CodecInfo {
  99. get {
  100. ImageCodecInfo info = new ImageCodecInfo ();
  101. info.Flags = ImageCodecFlags.Encoder | ImageCodecFlags.Decoder |
  102. ImageCodecFlags.Builtin | ImageCodecFlags.SupportBitmap;
  103. info.FormatDescription = "BITMAP file format";
  104. info.FormatID = System.Drawing.Imaging.ImageFormat.Bmp.Guid;
  105. info.MimeType = "image/bmp";
  106. info.Version = 1;
  107. byte[][] signaturePatterns = new byte[1][];
  108. signaturePatterns[0] = new byte[2];
  109. signaturePatterns[0][0] = 0x42;
  110. signaturePatterns[0][1] = 0x4d;
  111. info.SignaturePatterns = signaturePatterns;
  112. byte[][] signatureMasks = new byte[1][];
  113. signatureMasks[0] = new byte[2];
  114. signatureMasks[0][0] = 0xff;
  115. signatureMasks[0][1] = 0xff;
  116. info.SignatureMasks = signatureMasks;
  117. info.decode += new ImageCodecInfo.DecodeFromStream(BMPCodec.DecodeDelegate);
  118. info.encode += new ImageCodecInfo.EncodeToStream(BMPCodec.EncodeDelegate);
  119. return info;
  120. }
  121. }
  122. bool ReadFileHeader (Stream stream, out BITMAPFILEHEADER bmfh) {
  123. bmfh = new BITMAPFILEHEADER();
  124. BinaryReader bs = new BinaryReader(stream);
  125. bmfh.bfType = bs.ReadUInt16();
  126. if(bmfh.bfType != (ushort)BitmapFileType.BFT_BITMAP) return false;
  127. bmfh.bfSize = bs.ReadUInt32();
  128. bmfh.bfReserved1 = bs.ReadUInt16();
  129. bmfh.bfReserved2 = bs.ReadUInt16();
  130. bmfh.bfOffBits = bs.ReadUInt32();
  131. return true;
  132. }
  133. bool ReadInfoHeader (Stream stream, out BITMAPINFOHEADER_FLAT bmih) {
  134. bmih = new BITMAPINFOHEADER_FLAT();
  135. try {
  136. BinaryReader bs = new BinaryReader(stream);
  137. bmih.biSize = bs.ReadInt32();
  138. bmih.biWidth = bs.ReadInt32();
  139. bmih.biHeight = bs.ReadInt32();
  140. bmih.biPlanes = bs.ReadInt16();
  141. bmih.biBitCount = bs.ReadInt16();
  142. bmih.biCompression = bs.ReadInt32();
  143. bmih.biSizeImage = bs.ReadInt32();
  144. bmih.biXPelsPerMeter = bs.ReadInt32();
  145. bmih.biYPelsPerMeter = bs.ReadInt32();
  146. bmih.biClrUsed = bs.ReadInt32();
  147. bmih.biClrImportant = bs.ReadInt32();
  148. // Currently only BITMAPINFOHEADER
  149. if (bmih.biSize != 40) return false;
  150. bmih.FixBitmapInfo();
  151. int numColors = bmih.DibNumColors();
  152. int index = 0;
  153. for (int i = 0; i < numColors; i++) {
  154. bmih.bmiColors[index++] = (byte)stream.ReadByte();
  155. bmih.bmiColors[index++] = (byte)stream.ReadByte();
  156. bmih.bmiColors[index++] = (byte)stream.ReadByte();
  157. bmih.bmiColors[index++] = (byte)stream.ReadByte();
  158. }
  159. }
  160. catch (Exception e) {
  161. return false;
  162. }
  163. return true;
  164. }
  165. internal static void DecodeDelegate (Image image, Stream stream, BitmapData info)
  166. {
  167. BMPCodec bmp = new BMPCodec();
  168. bmp.Decode (image, stream, info);
  169. }
  170. internal bool Decode (Image image, Stream stream, BitmapData info)
  171. {
  172. #if false
  173. if (stream.Length < 14 + 40/* sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)*/)
  174. return false;
  175. long startPosition = stream.Position;
  176. BITMAPFILEHEADER bmfh;
  177. BITMAPINFOHEADER_FLAT bmih;
  178. if (!ReadFileHeader (stream, out bmfh))
  179. return false;
  180. if (!ReadInfoHeader (stream, out bmih))
  181. return false;
  182. Color[] colorEntries = new Color[bmih.DibNumColors()];
  183. int index = 0;
  184. for (int colorEntryIdx = 0; colorEntryIdx < colorEntries.Length; colorEntryIdx++) {
  185. // FIXME: is alpha can be used here
  186. colorEntries[colorEntryIdx] = Color.FromArgb(bmih.bmiColors[index+3], bmih.bmiColors[index+2], bmih.bmiColors[index+1], bmih.bmiColors[index]);
  187. index += 4;
  188. colorEntryIdx++;
  189. }
  190. image.Palette = new ColorPalette(1, colorEntries);
  191. image.SetRawFormat (System.Drawing.Imaging.ImageFormat.Bmp);
  192. info.Width = bmih.biWidth;
  193. info.Height = bmih.biHeight;
  194. info.Stride = (int)bmih.DibWidthBytes();
  195. switch (bmih.biBitCount) {
  196. case 24:
  197. info.PixelFormat = PixelFormat.Format24bppRgb;
  198. if (bmfh.bfOffBits != 0L)
  199. stream.Seek (startPosition + bmfh.bfOffBits,SeekOrigin.Begin);
  200. if (bmih.biCompression == (uint)BitmapCompression.BI_RGB) {
  201. info.RawImageBytes = new byte[bmih.biSizeImage];
  202. stream.Read(info.RawImageBytes, 0, (int)bmih.biSizeImage);
  203. } else {
  204. //
  205. // FIXME
  206. //
  207. Console.WriteLine ("BmpCodec: The {0} compression is not supported", bmih.biCompression);
  208. }
  209. break;
  210. case 32:
  211. info.PixelFormat = PixelFormat.Format32bppArgb;
  212. if (bmfh.bfOffBits != 0L)
  213. stream.Seek (startPosition + bmfh.bfOffBits,SeekOrigin.Begin);
  214. if (bmih.biCompression == (uint)BitmapCompression.BI_RGB) {
  215. info.RawImageBytes = new byte[bmih.biSizeImage];
  216. stream.Read(info.RawImageBytes, 0, (int)bmih.biSizeImage);
  217. } else {
  218. //
  219. // FIXME
  220. //
  221. Console.WriteLine ("BmpCodec: The {0} compression is not supported", bmih.biCompression);
  222. }
  223. break;
  224. default:
  225. throw new NotImplementedException(String.Format("This format is not yet supported : {0} bpp", bmih.biBitCount));
  226. }
  227. #endif
  228. return true;
  229. }
  230. internal static void EncodeDelegate (Image image, Stream stream)
  231. {
  232. BMPCodec bmp = new BMPCodec();
  233. BitmapData info = ((Bitmap)image).LockBits (new Rectangle (new Point (0,0), image.Size),
  234. ImageLockMode.ReadOnly, image.PixelFormat);
  235. bmp.Encode (image, stream, info);
  236. ((Bitmap)image).UnlockBits (info);
  237. }
  238. internal bool Encode (Image image, Stream stream, BitmapData info)
  239. {
  240. BITMAPFILEHEADER bmfh = new BITMAPFILEHEADER();
  241. bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
  242. bmfh.bfType = (ushort)BitmapFileType.BFT_BITMAP;
  243. bmfh.bfOffBits = (uint)(14 + 40 + image.Palette.Entries.Length * 4);
  244. int line_size = info.Stride;
  245. bmfh.bfSize = (uint)(bmfh.bfOffBits + info.Height * line_size);
  246. BinaryWriter bw = new BinaryWriter(stream);
  247. bw.Write(bmfh.bfType);
  248. bw.Write(bmfh.bfSize);
  249. bw.Write(bmfh.bfReserved1);
  250. bw.Write(bmfh.bfReserved2);
  251. bw.Write(bmfh.bfOffBits);
  252. BITMAPINFOHEADER_FLAT bmih = new BITMAPINFOHEADER_FLAT();
  253. bmih.Initialize(info);
  254. bw.Write(bmih.biSize);
  255. bw.Write(bmih.biWidth);
  256. bw.Write(bmih.biHeight);
  257. bw.Write(bmih.biPlanes);
  258. bw.Write(bmih.biBitCount);
  259. bw.Write(bmih.biCompression);
  260. bw.Write(bmih.biSizeImage);
  261. bw.Write(bmih.biXPelsPerMeter);
  262. bw.Write(bmih.biYPelsPerMeter);
  263. bw.Write(bmih.biClrUsed);
  264. bw.Write(bmih.biClrImportant);
  265. Console.WriteLine ("FIXME: BmpCodec: Write palette here");
  266. byte [] line_buffer = new byte [line_size];
  267. int stride = info.Stride;
  268. int start = 0;
  269. for (int line = 0; line < info.Height; line++){
  270. //FIXME: not an optimal way to specify starting address
  271. Marshal.Copy ((IntPtr)( info.Scan0.ToInt32() + start), line_buffer, 0, line_size);
  272. stream.Write(line_buffer, 0, line_size);
  273. start += stride;
  274. }
  275. return true;
  276. }
  277. }
  278. }