BmpCodec.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. static int BITMAPINFOHEADER_SIZE = 40;
  97. internal BMPCodec() {
  98. }
  99. internal static ImageCodecInfo CodecInfo {
  100. get {
  101. ImageCodecInfo info = new ImageCodecInfo ();
  102. info.Flags = ImageCodecFlags.Encoder | ImageCodecFlags.Decoder |
  103. ImageCodecFlags.Builtin | ImageCodecFlags.SupportBitmap;
  104. info.FormatDescription = "BITMAP file format";
  105. info.FormatID = System.Drawing.Imaging.ImageFormat.Bmp.Guid;
  106. info.MimeType = "image/bmp";
  107. info.Version = 1;
  108. byte[][] signaturePatterns = new byte[1][];
  109. signaturePatterns[0] = new byte[2];
  110. signaturePatterns[0][0] = 0x42;
  111. signaturePatterns[0][1] = 0x4d;
  112. info.SignaturePatterns = signaturePatterns;
  113. byte[][] signatureMasks = new byte[1][];
  114. signatureMasks[0] = new byte[2];
  115. signatureMasks[0][0] = 0xff;
  116. signatureMasks[0][1] = 0xff;
  117. info.SignatureMasks = signatureMasks;
  118. info.decode += new ImageCodecInfo.DecodeFromStream(BMPCodec.DecodeDelegate);
  119. info.encode += new ImageCodecInfo.EncodeToStream(BMPCodec.EncodeDelegate);
  120. return info;
  121. }
  122. }
  123. bool ReadFileHeader (Stream stream, out BITMAPFILEHEADER bmfh) {
  124. bmfh = new BITMAPFILEHEADER();
  125. BinaryReader bs = new BinaryReader(stream);
  126. bmfh.bfType = bs.ReadUInt16();
  127. if(bmfh.bfType != (ushort)BitmapFileType.BFT_BITMAP) return false;
  128. bmfh.bfSize = bs.ReadUInt32();
  129. bmfh.bfReserved1 = bs.ReadUInt16();
  130. bmfh.bfReserved2 = bs.ReadUInt16();
  131. bmfh.bfOffBits = bs.ReadUInt32();
  132. return true;
  133. }
  134. bool ReadInfoHeader (Stream stream, out BITMAPINFOHEADER_FLAT bmih) {
  135. bmih = new BITMAPINFOHEADER_FLAT();
  136. try {
  137. BinaryReader bs = new BinaryReader(stream);
  138. bmih.biSize = bs.ReadInt32();
  139. bmih.biWidth = bs.ReadInt32();
  140. bmih.biHeight = bs.ReadInt32();
  141. bmih.biPlanes = bs.ReadInt16();
  142. bmih.biBitCount = bs.ReadInt16();
  143. bmih.biCompression = bs.ReadInt32();
  144. bmih.biSizeImage = bs.ReadInt32();
  145. bmih.biXPelsPerMeter = bs.ReadInt32();
  146. bmih.biYPelsPerMeter = bs.ReadInt32();
  147. bmih.biClrUsed = bs.ReadInt32();
  148. bmih.biClrImportant = bs.ReadInt32();
  149. // Currently only BITMAPINFOHEADER
  150. if (bmih.biSize != BITMAPINFOHEADER_SIZE) return false;
  151. bmih.FixBitmapInfo();
  152. int numColors = bmih.DibNumColors();
  153. int index = 0;
  154. for (int i = 0; i < numColors; i++) {
  155. bmih.bmiColors[index++] = (byte)stream.ReadByte();
  156. bmih.bmiColors[index++] = (byte)stream.ReadByte();
  157. bmih.bmiColors[index++] = (byte)stream.ReadByte();
  158. bmih.bmiColors[index++] = (byte)stream.ReadByte();
  159. }
  160. }
  161. catch (Exception e) {
  162. return false;
  163. }
  164. return true;
  165. }
  166. internal static void DecodeDelegate (Image image, Stream stream, BitmapData info)
  167. {
  168. BMPCodec bmp = new BMPCodec();
  169. bmp.Decode (image, stream, info);
  170. }
  171. internal bool Decode (Image image, Stream stream, BitmapData info)
  172. {
  173. #if false
  174. if (stream.Length < 14 + BITMAPINFOHEADER_SIZE/* sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)*/)
  175. return false;
  176. long startPosition = stream.Position;
  177. BITMAPFILEHEADER bmfh;
  178. BITMAPINFOHEADER_FLAT bmih;
  179. if (!ReadFileHeader (stream, out bmfh))
  180. return false;
  181. if (!ReadInfoHeader (stream, out bmih))
  182. return false;
  183. Color[] colorEntries = new Color[bmih.DibNumColors()];
  184. int index = 0;
  185. for (int colorEntryIdx = 0; colorEntryIdx < colorEntries.Length; colorEntryIdx++) {
  186. // FIXME: is alpha can be used here
  187. colorEntries[colorEntryIdx] = Color.FromArgb(bmih.bmiColors[index+3], bmih.bmiColors[index+2], bmih.bmiColors[index+1], bmih.bmiColors[index]);
  188. index += 4;
  189. colorEntryIdx++;
  190. }
  191. image.Palette = new ColorPalette(1, colorEntries);
  192. image.SetRawFormat (System.Drawing.Imaging.ImageFormat.Bmp);
  193. info.Width = bmih.biWidth;
  194. info.Height = bmih.biHeight;
  195. info.Stride = (int)bmih.DibWidthBytes();
  196. switch (bmih.biBitCount) {
  197. case 24:
  198. info.PixelFormat = PixelFormat.Format24bppRgb;
  199. if (bmfh.bfOffBits != 0L)
  200. stream.Seek (startPosition + bmfh.bfOffBits,SeekOrigin.Begin);
  201. if (bmih.biCompression == (uint)BitmapCompression.BI_RGB) {
  202. info.RawImageBytes = new byte[bmih.biSizeImage];
  203. stream.Read(info.RawImageBytes, 0, (int)bmih.biSizeImage);
  204. } else {
  205. //
  206. // FIXME
  207. //
  208. Console.WriteLine ("BmpCodec: The {0} compression is not supported", bmih.biCompression);
  209. }
  210. break;
  211. case 32:
  212. info.PixelFormat = PixelFormat.Format32bppArgb;
  213. if (bmfh.bfOffBits != 0L)
  214. stream.Seek (startPosition + bmfh.bfOffBits,SeekOrigin.Begin);
  215. if (bmih.biCompression == (uint)BitmapCompression.BI_RGB) {
  216. info.RawImageBytes = new byte[bmih.biSizeImage];
  217. stream.Read(info.RawImageBytes, 0, (int)bmih.biSizeImage);
  218. } else {
  219. //
  220. // FIXME
  221. //
  222. Console.WriteLine ("BmpCodec: The {0} compression is not supported", bmih.biCompression);
  223. }
  224. break;
  225. default:
  226. throw new NotImplementedException(String.Format("This format is not yet supported : {0} bpp", bmih.biBitCount));
  227. }
  228. #endif
  229. return true;
  230. }
  231. internal static void EncodeDelegate (Image image, Stream stream)
  232. {
  233. BMPCodec bmp = new BMPCodec();
  234. BitmapData info = ((Bitmap)image).LockBits (new Rectangle (new Point (0,0), image.Size),
  235. ImageLockMode.ReadOnly, image.PixelFormat);
  236. bmp.Encode (image, stream, info);
  237. ((Bitmap)image).UnlockBits (info);
  238. }
  239. internal bool Encode (Image image, Stream stream, BitmapData info)
  240. {
  241. BITMAPFILEHEADER bmfh = new BITMAPFILEHEADER();
  242. bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
  243. bmfh.bfType = (ushort)BitmapFileType.BFT_BITMAP;
  244. bmfh.bfOffBits = (uint)(14 + BITMAPINFOHEADER_SIZE + image.Palette.Entries.Length * 4);
  245. int line_size = info.Stride;
  246. bmfh.bfSize = (uint)(bmfh.bfOffBits + info.Height * line_size);
  247. BinaryWriter bw = new BinaryWriter(stream);
  248. bw.Write(bmfh.bfType);
  249. bw.Write(bmfh.bfSize);
  250. bw.Write(bmfh.bfReserved1);
  251. bw.Write(bmfh.bfReserved2);
  252. bw.Write(bmfh.bfOffBits);
  253. BITMAPINFOHEADER_FLAT bmih = new BITMAPINFOHEADER_FLAT();
  254. bmih.Initialize(info);
  255. bw.Write(bmih.biSize);
  256. bw.Write(bmih.biWidth);
  257. bw.Write(bmih.biHeight);
  258. bw.Write(bmih.biPlanes);
  259. bw.Write(bmih.biBitCount);
  260. bw.Write(bmih.biCompression);
  261. bw.Write(bmih.biSizeImage);
  262. bw.Write(bmih.biXPelsPerMeter);
  263. bw.Write(bmih.biYPelsPerMeter);
  264. bw.Write(bmih.biClrUsed);
  265. bw.Write(bmih.biClrImportant);
  266. Console.WriteLine ("FIXME: BmpCodec: Write palette here");
  267. Console.WriteLine ("biWidth ->" + bmih.biWidth);
  268. Console.WriteLine ("Height->" + bmih.biHeight);
  269. Console.WriteLine ("LineSize ->" + line_size);
  270. Console.WriteLine ("Address ->" + info.Scan0.ToInt32());
  271. Console.WriteLine ("Stride ->" + info.Stride);
  272. Console.WriteLine ("Planes ->" + bmih.biPlanes);
  273. byte [] line_buffer = new byte [line_size];
  274. int stride = info.Stride;
  275. int offset = (info.Height-1) * stride;
  276. int baseadr = info.Scan0.ToInt32();
  277. Console.WriteLine ("Offset ->" + offset);
  278. // DIB are stored upside down. That means that the uppest row which
  279. // appears on the screen actually is the lowest row stored in the
  280. // bitmap.
  281. while(offset>=0){
  282. //FIXME: not an optimal way to specify starting address
  283. //FIXME: Bitmaps are stored in DWORD alignments
  284. Marshal.Copy ((IntPtr)( baseadr + offset), line_buffer, 0, line_size);
  285. stream.Write(line_buffer, 0, line_size);
  286. offset -= stride;
  287. }
  288. return true;
  289. }
  290. }
  291. }