BitmapData.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // System.Drawing.Imaging.BitmapData.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected]
  6. //
  7. // (C) 2002 Ximian, Inc. http://www.ximian.com
  8. //
  9. using System;
  10. using System.Runtime.InteropServices;
  11. using System.IO;
  12. namespace System.Drawing.Imaging
  13. {
  14. [StructLayout(LayoutKind.Sequential)]
  15. public sealed class BitmapData {
  16. internal int width, height, stride;
  17. internal PixelFormat pixel_format;
  18. internal IntPtr address;
  19. internal int reserved;
  20. private bool bAllocated = false;
  21. ~BitmapData()
  22. {
  23. if (bAllocated)
  24. Marshal.FreeHGlobal(address);
  25. }
  26. internal bool Allocated {
  27. set {bAllocated = value;}
  28. }
  29. public int Height {
  30. get {
  31. return height;
  32. }
  33. set {
  34. height = value;
  35. }
  36. }
  37. public int Width {
  38. get {
  39. return width;
  40. }
  41. set {
  42. width = value;
  43. }
  44. }
  45. public PixelFormat PixelFormat {
  46. get {
  47. return pixel_format;
  48. }
  49. set {
  50. pixel_format = value;
  51. }
  52. }
  53. public int Reserved {
  54. get {
  55. return reserved;
  56. }
  57. set {
  58. reserved = value;
  59. }
  60. }
  61. public IntPtr Scan0 {
  62. get {
  63. return address;
  64. }
  65. set {
  66. if (bAllocated)
  67. {
  68. Marshal.FreeHGlobal(address);
  69. bAllocated = false;
  70. }
  71. address = value;
  72. }
  73. }
  74. public int Stride {
  75. get {
  76. return stride;
  77. }
  78. set {
  79. stride = value;
  80. }
  81. }
  82. internal unsafe void swap_red_blue_bytes ()
  83. {
  84. byte *start = (byte *) (void *) this.Scan0;
  85. int stride = this.Stride;
  86. for (int line = 0; line < this.Height; line++){
  87. // Exchange red <=> blue bytes
  88. // fixed (byte *pbuf = start) {
  89. byte* curByte = start;
  90. for (int i = 0; i < this.Width; i++) {
  91. byte t = *(curByte+2);
  92. *(curByte+2) = *curByte;
  93. *curByte = t;
  94. curByte += 3;
  95. }
  96. // }
  97. start += stride;
  98. }
  99. }
  100. }
  101. }