BmpCodec.cs 12 KB

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