BmpCodec.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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( InternalImageInfo info) {
  81. biSize = 40;
  82. biWidth = info.Size.Width;
  83. biHeight = info.Size.Height;
  84. biPlanes = 1;
  85. biBitCount = (short)System.Drawing.Image.GetPixelFormatSize(info.PixelFormat);
  86. biCompression = (int)BitmapCompression.BI_RGB;
  87. biSizeImage = (int)info.RawImageBytes.Length;
  88. biXPelsPerMeter = 0;
  89. biYPelsPerMeter = 0;
  90. biClrUsed = 0;
  91. biClrImportant = 0;
  92. }
  93. }
  94. internal class BMPCodec {
  95. internal BMPCodec() {
  96. }
  97. internal static ImageCodecInfo CodecInfo {
  98. get {
  99. ImageCodecInfo info = new ImageCodecInfo();
  100. info.Flags = ImageCodecFlags.Encoder | ImageCodecFlags.Decoder | ImageCodecFlags.Builtin | ImageCodecFlags.SupportBitmap;
  101. info.FormatDescription = "BITMAP file format";
  102. info.FormatID = System.Drawing.Imaging.ImageFormat.Bmp.Guid;
  103. info.MimeType = "image/bmp";
  104. info.Version = 1;
  105. byte[][] signaturePatterns = new byte[1][];
  106. signaturePatterns[0] = new byte[2];
  107. signaturePatterns[0][0] = 0x42;
  108. signaturePatterns[0][1] = 0x4d;
  109. info.SignaturePatterns = signaturePatterns;
  110. byte[][] signatureMasks = new byte[1][];
  111. signatureMasks[0] = new byte[2];
  112. signatureMasks[0][0] = 0xff;
  113. signatureMasks[0][1] = 0xff;
  114. info.SignatureMasks = signatureMasks;
  115. info.decode += new ImageCodecInfo.DecodeFromStream(BMPCodec.DecodeDelegate);
  116. info.encode += new ImageCodecInfo.EncodeToStream(BMPCodec.EncodeDelegate);
  117. return info;
  118. }
  119. }
  120. bool ReadFileHeader( Stream stream, out BITMAPFILEHEADER bmfh) {
  121. bmfh = new BITMAPFILEHEADER();
  122. BinaryReader bs = new BinaryReader(stream);
  123. bmfh.bfType = bs.ReadUInt16();
  124. if(bmfh.bfType != (ushort)BitmapFileType.BFT_BITMAP) return false;
  125. bmfh.bfSize = bs.ReadUInt32();
  126. bmfh.bfReserved1 = bs.ReadUInt16();
  127. bmfh.bfReserved2 = bs.ReadUInt16();
  128. bmfh.bfOffBits = bs.ReadUInt32();
  129. return true;
  130. }
  131. bool ReadInfoHeader( Stream stream, out BITMAPINFOHEADER_FLAT bmih) {
  132. bmih = new BITMAPINFOHEADER_FLAT();
  133. try {
  134. BinaryReader bs = new BinaryReader(stream);
  135. bmih.biSize = bs.ReadInt32();
  136. bmih.biWidth = bs.ReadInt32();
  137. bmih.biHeight = bs.ReadInt32();
  138. bmih.biPlanes = bs.ReadInt16();
  139. bmih.biBitCount = bs.ReadInt16();
  140. bmih.biCompression = bs.ReadInt32();
  141. bmih.biSizeImage = bs.ReadInt32();
  142. bmih.biXPelsPerMeter = bs.ReadInt32();
  143. bmih.biYPelsPerMeter = bs.ReadInt32();
  144. bmih.biClrUsed = bs.ReadInt32();
  145. bmih.biClrImportant = bs.ReadInt32();
  146. // Currently only BITMAPINFOHEADER
  147. if( bmih.biSize != 40) return false;
  148. bmih.FixBitmapInfo();
  149. int numColors = bmih.DibNumColors();
  150. int index = 0;
  151. for (int i = 0; i < numColors; i++) {
  152. bmih.bmiColors[index++] = (byte)stream.ReadByte();
  153. bmih.bmiColors[index++] = (byte)stream.ReadByte();
  154. bmih.bmiColors[index++] = (byte)stream.ReadByte();
  155. bmih.bmiColors[index++] = (byte)stream.ReadByte();
  156. }
  157. }
  158. catch( Exception e) {
  159. return false;
  160. }
  161. return true;
  162. }
  163. internal static void DecodeDelegate (Stream stream, InternalImageInfo info) {
  164. BMPCodec bmp = new BMPCodec();
  165. bmp.Decode (stream, info);
  166. }
  167. internal bool Decode( Stream stream, InternalImageInfo info) {
  168. if( stream.Length < 14 + 40/* sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)*/)
  169. return false;
  170. long startPosition = stream.Position;
  171. BITMAPFILEHEADER bmfh;
  172. BITMAPINFOHEADER_FLAT bmih;
  173. if (!ReadFileHeader (stream, out bmfh)) return false;
  174. if (!ReadInfoHeader (stream, out bmih)) return false;
  175. Color[] colorEntries = new Color[bmih.DibNumColors()];
  176. int index = 0;
  177. for( int colorEntryIdx = 0; colorEntryIdx < colorEntries.Length; colorEntryIdx++) {
  178. // FIXME: is alpha can be used here
  179. colorEntries[colorEntryIdx] = Color.FromArgb(bmih.bmiColors[index+3], bmih.bmiColors[index+2], bmih.bmiColors[index+1], bmih.bmiColors[index]);
  180. index += 4;
  181. colorEntryIdx++;
  182. }
  183. info.Palette = new ColorPalette(1, colorEntries);
  184. info.Size = new Size(bmih.biWidth, bmih.biHeight);
  185. info.Stride = (int)bmih.DibWidthBytes();
  186. info.RawFormat = System.Drawing.Imaging.ImageFormat.Bmp;
  187. switch (bmih.biBitCount) {
  188. case 24:
  189. info.PixelFormat = PixelFormat.Format24bppRgb;
  190. if (bmfh.bfOffBits != 0L) stream.Seek (startPosition + bmfh.bfOffBits,SeekOrigin.Begin);
  191. if (bmih.biCompression == (uint)BitmapCompression.BI_RGB) {
  192. info.RawImageBytes = new byte[bmih.biSizeImage];
  193. stream.Read(info.RawImageBytes, 0, (int)bmih.biSizeImage);
  194. }
  195. else {
  196. }
  197. break;
  198. case 32:
  199. info.PixelFormat = PixelFormat.Format32bppArgb;
  200. if (bmfh.bfOffBits != 0L) 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. }
  205. else {
  206. }
  207. break;
  208. default:
  209. throw new NotImplementedException(String.Format("This format is not yet supported : {0} bpp", bmih.biBitCount));
  210. break;
  211. }
  212. return true;
  213. }
  214. internal static void EncodeDelegate (Stream stream, InternalImageInfo info) {
  215. BMPCodec bmp = new BMPCodec();
  216. bmp.Encode (stream, info);
  217. }
  218. internal bool Encode( Stream stream, InternalImageInfo info) {
  219. BITMAPFILEHEADER bmfh = new BITMAPFILEHEADER();
  220. bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
  221. bmfh.bfType = (ushort)BitmapFileType.BFT_BITMAP;
  222. bmfh.bfOffBits = (uint)(14 + 40 + info.Palette.Entries.Length * 4);
  223. bmfh.bfSize = (uint)(bmfh.bfOffBits + info.RawImageBytes.Length);
  224. BinaryWriter bw = new BinaryWriter(stream);
  225. bw.Write(bmfh.bfType);
  226. bw.Write(bmfh.bfSize);
  227. bw.Write(bmfh.bfReserved1);
  228. bw.Write(bmfh.bfReserved2);
  229. bw.Write(bmfh.bfOffBits);
  230. BITMAPINFOHEADER_FLAT bmih = new BITMAPINFOHEADER_FLAT();
  231. bmih.Initialize(info);
  232. bw.Write(bmih.biSize);
  233. bw.Write(bmih.biWidth);
  234. bw.Write(bmih.biHeight);
  235. bw.Write(bmih.biPlanes);
  236. bw.Write(bmih.biBitCount);
  237. bw.Write(bmih.biCompression);
  238. bw.Write(bmih.biSizeImage);
  239. bw.Write(bmih.biXPelsPerMeter);
  240. bw.Write(bmih.biYPelsPerMeter);
  241. bw.Write(bmih.biClrUsed);
  242. bw.Write(bmih.biClrImportant);
  243. // FIXME: write palette here
  244. stream.Write(info.RawImageBytes, 0, info.RawImageBytes.Length);
  245. return true;
  246. }
  247. }
  248. }