BmpCodec.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. //
  12. // Useful documentation about bitmaps
  13. //
  14. // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_4v1h.asp
  15. // http://www.csdn.net/Dev/Format/windows/Bmp.html
  16. // http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html
  17. //
  18. // Header structure
  19. // BITMAPFILEHEADER
  20. // BITMAPINFOHEADER or BITMAPV4HEADER or BITMAPV5HEADER or BITMAPCOREHEADER
  21. // RGBQUADS or RGBTRIPLE (optional)
  22. // Bitmap data
  23. //
  24. namespace System.Drawing.Imaging {
  25. using System;
  26. using System.IO;
  27. using System.Drawing.Imaging;
  28. using System.Runtime.InteropServices;
  29. internal struct BITMAPFILEHEADER { // File info header
  30. public ushort bfType; // Specifies the type of file. This member must be BM.
  31. public uint bfSize; // Specifies the size of the file, in bytes.
  32. public ushort bfReserved1; // Reserved; must be set to zero.
  33. public ushort bfReserved2; // Reserved; must be set to zero.
  34. public uint bfOffBits; // Specifies the byte offset from the BITMAPFILEHEADER
  35. // structure to the actual bitmap data in the file.
  36. public void DumpHeader()
  37. {
  38. Console.WriteLine("bfType:" + bfType);
  39. Console.WriteLine("bfSize:" + bfSize);
  40. Console.WriteLine("bfReserved1:" + bfReserved1);
  41. Console.WriteLine("bfReserved2:" + bfReserved2);
  42. Console.WriteLine("bfOffBits:" + bfOffBits);
  43. }
  44. }
  45. internal enum BitmapFileType : ushort {
  46. BFT_ICON = 0x4349, /* 'IC' */
  47. BFT_BITMAP = 0x4d42, /* 'BM' */
  48. BFT_CURSOR = 0x5450 /* 'PT' */
  49. }
  50. internal enum BitmapCompression : uint {
  51. BI_RGB = 0,
  52. BI_RLE8 = 1,
  53. BI_RLE4 = 2,
  54. BI_BITFIELDS = 3
  55. }
  56. [StructLayout(LayoutKind.Sequential)]
  57. internal struct CIEXYZ {
  58. int ciexyzX;
  59. int ciexyzY;
  60. int ciexyzZ;
  61. }
  62. [StructLayout(LayoutKind.Sequential)]
  63. internal struct CIEXYZTRIPLE {
  64. CIEXYZ ciexyzRed;
  65. CIEXYZ ciexyzGreen;
  66. CIEXYZ ciexyzBlue;
  67. }
  68. /* OS/2 BMP Format */
  69. [StructLayout(LayoutKind.Sequential)]
  70. internal struct BITMAPCOREHEADER {
  71. internal int bcSize;
  72. internal short bcWidth;
  73. internal short bcHeight;
  74. internal short bcPlanes;
  75. internal short bcBitCount;
  76. }
  77. /* Windows BMP formats */
  78. [StructLayout(LayoutKind.Sequential)]
  79. internal struct BITMAPINFOHEADER {
  80. internal int biSize;
  81. internal int biWidth;
  82. internal int biHeight;
  83. internal short biPlanes;
  84. internal short biBitCount;
  85. internal int biCompression;
  86. internal int biSizeImage;
  87. internal int biXPelsPerMeter;
  88. internal int biYPelsPerMeter;
  89. internal int biClrUsed;
  90. internal int biClrImportant;
  91. /* V4 */
  92. internal int biRedMask;
  93. internal int biGreenMask;
  94. internal int biBlueMask;
  95. internal int biAlphaMask;
  96. internal int biCSType;
  97. internal CIEXYZTRIPLE biEndpoints;
  98. internal int biGammaRed;
  99. internal int biGammaGreen;
  100. internal int biGammaBlue;
  101. /* V5 */
  102. internal int biIntent;
  103. internal int biProfileData;
  104. internal int biProfileSize;
  105. internal int biReserved;
  106. /* Variables not part of the struct*/
  107. internal bool upsidedown;
  108. internal bool os2;
  109. internal byte[] bmiColors;
  110. static int WIDTHBYTES(int i) {
  111. return ((i+31)&(~31))/8; /* ULONG aligned ! */
  112. }
  113. public int DibWidthBytesN(int n) {
  114. return WIDTHBYTES(biWidth * n);
  115. }
  116. public int DibWidthBytes() {
  117. return DibWidthBytesN(biBitCount);
  118. }
  119. public int DibSizeImage() {
  120. return biSizeImage == 0 ? DibWidthBytes() * biHeight : biSizeImage;
  121. }
  122. public int DibNumColors() {
  123. return biClrUsed == 0 && biBitCount <= 8 ? (1 << biBitCount) : biClrUsed;
  124. }
  125. public void FixBitmapInfo(){
  126. if (biSizeImage == 0)
  127. biSizeImage = DibSizeImage();
  128. if (biClrUsed == 0)
  129. biClrUsed = DibNumColors();
  130. }
  131. public void DumpHeader()
  132. {
  133. Console.WriteLine("biSize " + biSize);
  134. Console.WriteLine("biWidth " + biWidth);
  135. Console.WriteLine("biHeight " + biHeight);
  136. Console.WriteLine("biPlanes " + biPlanes);
  137. Console.WriteLine("biBitCount " + biBitCount);
  138. Console.WriteLine("biCompression " + biCompression);
  139. Console.WriteLine("biSizeImage " + biSizeImage);
  140. Console.WriteLine("biXPelsPerMeter " + biXPelsPerMeter);
  141. Console.WriteLine("biYPelsPerMeter " + biYPelsPerMeter);
  142. Console.WriteLine("biClrUsed " + biClrUsed);
  143. Console.WriteLine("biClrImportant " + biClrImportant);
  144. Console.WriteLine("colors: " + DibNumColors());
  145. }
  146. float HorizontalResolution {
  147. get {
  148. return (float)biXPelsPerMeter * 254.0F / 10000.0F;
  149. }
  150. }
  151. float VerticalResolution {
  152. get {
  153. return (float)biYPelsPerMeter * 254.0F / 10000.0F;
  154. }
  155. }
  156. public void Initialize (Image image, BitmapData info)
  157. {
  158. biSize = 40;
  159. biWidth = info.Width;
  160. biHeight = info.Height;
  161. biPlanes = 1;
  162. biBitCount = (short) System.Drawing.Image.GetPixelFormatSize (info.PixelFormat);
  163. biCompression = (int)BitmapCompression.BI_RGB;
  164. biSizeImage = 0; // Many tools expect this may be set to zero for BI_RGB bitmaps
  165. biXPelsPerMeter = (int) (0.5f + ((image.HorizontalResolution * 3937f) / 100f));
  166. biYPelsPerMeter = (int) (0.5f + ((image.VerticalResolution * 3937f) / 100f)); // 1 meter is = 39.37 inch
  167. biClrUsed = 0;
  168. biClrImportant = 0;
  169. }
  170. }
  171. internal class BMPCodec {
  172. static int BITMAPINFOHEADER_SIZE = 40;
  173. static int BITMAPINFOHEADER_V4_SIZE = 108;
  174. static int BITMAPINFOHEADER_V5_SIZE = 124;
  175. static int BITMAPFILEHEADER_SIZE = 14;
  176. static int BITMAPCOREHEADER_SIZE = 12;
  177. internal BMPCodec() {
  178. }
  179. internal static ImageCodecInfo CodecInfo {
  180. get {
  181. ImageCodecInfo info = new ImageCodecInfo ();
  182. info.Flags = ImageCodecFlags.Encoder | ImageCodecFlags.Decoder |
  183. ImageCodecFlags.Builtin | ImageCodecFlags.SupportBitmap;
  184. info.FormatDescription = "BITMAP file format";
  185. info.FormatID = System.Drawing.Imaging.ImageFormat.Bmp.Guid;
  186. info.MimeType = "image/bmp";
  187. info.Version = 1;
  188. byte[][] signaturePatterns = new byte[1][];
  189. signaturePatterns[0] = new byte[2];
  190. signaturePatterns[0][0] = 0x42;
  191. signaturePatterns[0][1] = 0x4d;
  192. info.SignaturePatterns = signaturePatterns;
  193. byte[][] signatureMasks = new byte[1][];
  194. signatureMasks[0] = new byte[2];
  195. signatureMasks[0][0] = 0xff;
  196. signatureMasks[0][1] = 0xff;
  197. info.SignatureMasks = signatureMasks;
  198. info.decode += new ImageCodecInfo.DecodeFromStream(BMPCodec.DecodeDelegate);
  199. info.encode += new ImageCodecInfo.EncodeToStream(BMPCodec.EncodeDelegate);
  200. return info;
  201. }
  202. }
  203. bool ReadFileHeader (Stream stream, out BITMAPFILEHEADER bmfh) {
  204. bmfh = new BITMAPFILEHEADER();
  205. BinaryReader bs = new BinaryReader(stream);
  206. bmfh.bfType = bs.ReadUInt16();
  207. if(bmfh.bfType != (ushort)BitmapFileType.BFT_BITMAP) return false;
  208. bmfh.bfSize = bs.ReadUInt32();
  209. bmfh.bfReserved1 = bs.ReadUInt16();
  210. bmfh.bfReserved2 = bs.ReadUInt16();
  211. bmfh.bfOffBits = bs.ReadUInt32();
  212. //bmfh.DumpHeader ();
  213. return true;
  214. }
  215. /* BITMAPINFOHEADER/BITMAPV4HEADER/BITMAPV5HEADER */
  216. bool ReadInfoHeader (Stream stream, out BITMAPINFOHEADER bmih) {
  217. bmih = new BITMAPINFOHEADER();
  218. try {
  219. BinaryReader bs = new BinaryReader(stream);
  220. bmih.biSize = bs.ReadInt32();
  221. if (bmih.biSize != BITMAPINFOHEADER_SIZE && bmih.biSize != BITMAPINFOHEADER_V4_SIZE &&
  222. bmih.biSize != BITMAPINFOHEADER_V5_SIZE && bmih.biSize != BITMAPCOREHEADER_SIZE)
  223. throw new Exception ("Invalid BITMAPINFOHEADER size");
  224. if (bmih.biSize == BITMAPCOREHEADER_SIZE) { // OS/2 Format
  225. bmih.biWidth = bs.ReadInt16();
  226. bmih.biHeight = bs.ReadInt16();
  227. bmih.biPlanes = bs.ReadInt16();
  228. bmih.biBitCount = bs.ReadInt16();
  229. bmih.biCompression = 0;
  230. bmih.biSizeImage = 0;
  231. bmih.biXPelsPerMeter = 0;
  232. bmih.biYPelsPerMeter = 0;
  233. bmih.biClrUsed = 0;
  234. bmih.biClrImportant = 0;
  235. bmih.os2 = true;
  236. }
  237. else {
  238. bmih.biWidth = bs.ReadInt32();
  239. bmih.biHeight = bs.ReadInt32();
  240. bmih.biPlanes = bs.ReadInt16();
  241. bmih.biBitCount = bs.ReadInt16();
  242. bmih.biCompression = bs.ReadInt32();
  243. bmih.biSizeImage = bs.ReadInt32();
  244. bmih.biXPelsPerMeter = bs.ReadInt32();
  245. bmih.biYPelsPerMeter = bs.ReadInt32();
  246. bmih.biClrUsed = bs.ReadInt32();
  247. bmih.biClrImportant = bs.ReadInt32();
  248. bmih.os2 = false;
  249. }
  250. if (bmih.biHeight < 0) {
  251. bmih.upsidedown = false;
  252. bmih.biHeight = -bmih.biHeight;
  253. }
  254. else
  255. bmih.upsidedown = true;
  256. //bmih.DumpHeader();
  257. bmih.FixBitmapInfo();
  258. }
  259. catch (Exception e) {
  260. Console.WriteLine("Exception: " + e.ToString());
  261. return false;
  262. }
  263. return true;
  264. }
  265. internal static void DecodeDelegate (Image image, Stream stream, BitmapData info)
  266. {
  267. BMPCodec bmp = new BMPCodec();
  268. bmp.Decode (image, stream, info);
  269. }
  270. internal bool Decode (Image image, Stream stream, BitmapData info)
  271. {
  272. long startPosition = stream.Position;
  273. BITMAPFILEHEADER bmfh;
  274. BITMAPINFOHEADER bmih;
  275. if (!ReadFileHeader (stream, out bmfh))
  276. return false;
  277. if (!ReadInfoHeader (stream, out bmih))
  278. return false;
  279. if (bmih.biCompression != (uint)BitmapCompression.BI_RGB)
  280. throw new Exception ("BmpCodec: The compression is not supported");
  281. /* Read RGB palette*/
  282. Color[] colorEntries = new Color[bmih.DibNumColors()];
  283. int index = 0;
  284. byte r,g,b,a;
  285. if (bmih.os2){ // RGBTRIPLE
  286. for (int colorEntryIdx = 0; colorEntryIdx < colorEntries.Length; colorEntryIdx++, index += 3) {
  287. b = (byte) stream.ReadByte();
  288. g = (byte) stream.ReadByte();
  289. r = (byte) stream.ReadByte();
  290. colorEntries[colorEntryIdx] = Color.FromArgb(r,g,b);
  291. }
  292. }
  293. else { // RGBSquads
  294. for (int colorEntryIdx = 0; colorEntryIdx < colorEntries.Length; colorEntryIdx++, index += 4) {
  295. b = (byte) stream.ReadByte();
  296. g = (byte) stream.ReadByte();
  297. r = (byte) stream.ReadByte();
  298. a = (byte) stream.ReadByte();
  299. colorEntries[colorEntryIdx] = Color.FromArgb(a, r,g,b);
  300. }
  301. }
  302. image.Palette = new ColorPalette(0, colorEntries);
  303. image.SetRawFormat (System.Drawing.Imaging.ImageFormat.Bmp);
  304. info.Width = bmih.biWidth;
  305. info.Height = bmih.biHeight;
  306. info.Stride = (int)bmih.DibWidthBytes();
  307. //Console.WriteLine ("Stride ->" + info.Stride + " width " + info.Width * bmih.biBitCount);
  308. switch (bmih.biBitCount) {
  309. case 24:
  310. info.PixelFormat = PixelFormat.Format24bppRgb;
  311. break;
  312. case 32:
  313. info.PixelFormat = PixelFormat.Format32bppArgb;
  314. break;
  315. case 8:
  316. info.PixelFormat = PixelFormat.Format8bppIndexed;
  317. break;
  318. case 4:
  319. info.PixelFormat = PixelFormat.Format4bppIndexed;
  320. break;
  321. default:
  322. throw new NotImplementedException(String.Format("This format is not yet supported : {0} bpp", bmih.biBitCount));
  323. }
  324. if (bmfh.bfOffBits != 0L)
  325. stream.Seek (startPosition + bmfh.bfOffBits,SeekOrigin.Begin);
  326. IntPtr lfBuffer = Marshal.AllocHGlobal(bmih.biSizeImage);
  327. byte[] bt = new byte[info.Stride];
  328. int offset = (info.Height-1) * info.Stride;
  329. int baseadr = lfBuffer.ToInt32();
  330. // If the height is positive the DIB are stored upside down. That means that the uppest row
  331. // which appears on the screen actually is the lowest row stored in the bitmap
  332. // if it is negative, if it stored in the way arround.
  333. if (bmih.upsidedown) {
  334. offset = (info.Height-1) * info.Stride;
  335. while(offset>=0){
  336. stream.Read(bt, 0, info.Stride);
  337. Marshal.Copy (bt, 0, (IntPtr)( baseadr + offset), info.Stride);
  338. offset -= info.Stride;
  339. }
  340. }
  341. else {
  342. offset = 0;
  343. for (int lines = 0; lines < info.Height ; lines++){
  344. stream.Read(bt, 0, info.Stride);
  345. Marshal.Copy (bt, 0, (IntPtr)( baseadr + offset), info.Stride);
  346. offset += info.Stride;
  347. }
  348. }
  349. info.Scan0 = lfBuffer;
  350. info.Allocated = true;
  351. return true;
  352. }
  353. internal static void EncodeDelegate (Image image, Stream stream)
  354. {
  355. BMPCodec bmp = new BMPCodec();
  356. BitmapData info = ((Bitmap)image).LockBits (new Rectangle (new Point (0,0), image.Size),
  357. ImageLockMode.ReadOnly, image.PixelFormat);
  358. bmp.Encode (image, stream, info);
  359. ((Bitmap)image).UnlockBits (info);
  360. }
  361. internal bool Encode (Image image, Stream stream, BitmapData info)
  362. {
  363. Console.WriteLine ("***Encode! " + info.PixelFormat);
  364. BITMAPFILEHEADER bmfh = new BITMAPFILEHEADER();
  365. bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
  366. bmfh.bfType = (ushort)BitmapFileType.BFT_BITMAP;
  367. bmfh.bfOffBits = (uint)(BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_SIZE + image.Palette.Entries.Length * 4);
  368. int line_size = info.Stride;
  369. bmfh.bfSize = (uint)(bmfh.bfOffBits + info.Height * line_size);
  370. BinaryWriter bw = new BinaryWriter(stream);
  371. bw.Write(bmfh.bfType);
  372. bw.Write(bmfh.bfSize);
  373. bw.Write(bmfh.bfReserved1);
  374. bw.Write(bmfh.bfReserved2);
  375. bw.Write(bmfh.bfOffBits);
  376. //bmfh.DumpHeader();
  377. BITMAPINFOHEADER bmih = new BITMAPINFOHEADER();
  378. bmih.Initialize(image, info);
  379. bw.Write(bmih.biSize);
  380. bw.Write(bmih.biWidth);
  381. bw.Write(bmih.biHeight);
  382. bw.Write(bmih.biPlanes);
  383. bw.Write(bmih.biBitCount);
  384. bw.Write(bmih.biCompression);
  385. bw.Write(bmih.biSizeImage);
  386. bw.Write(bmih.biXPelsPerMeter);
  387. bw.Write(bmih.biYPelsPerMeter);
  388. bw.Write(bmih.biClrUsed);
  389. bw.Write(bmih.biClrImportant);
  390. bmih.DumpHeader();
  391. // Write palette on disk on BGR
  392. Color[] colors = image.Palette.Entries;
  393. for (int i = 0; i < colors.Length; i++) {
  394. bw.Write(colors[i].B);
  395. bw.Write(colors[i].G);
  396. bw.Write(colors[i].R);
  397. bw.Write(colors[i].A);
  398. }
  399. //Console.WriteLine("Colors written: " + image.Palette.Entries.Length);
  400. byte [] line_buffer = new byte [line_size];
  401. int stride = info.Stride;
  402. int offset = (info.Height-1) * stride;
  403. int baseadr = info.Scan0.ToInt32();
  404. // We always store DIB upside down. That means that the uppest row which
  405. // appears on the screen actually is the lowest row stored in the bitmap
  406. while(offset>=0){
  407. Marshal.Copy ((IntPtr)( baseadr + offset), line_buffer, 0, line_size);
  408. stream.Write(line_buffer, 0, line_size);
  409. offset -= stride;
  410. }
  411. return true;
  412. }
  413. }
  414. }