BitmapData.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. internal bool own_scan0;
  22. ~BitmapData()
  23. {
  24. if (bAllocated)
  25. Marshal.FreeHGlobal(address);
  26. }
  27. internal bool Allocated {
  28. set {bAllocated = value;}
  29. }
  30. public int Height {
  31. get {
  32. return height;
  33. }
  34. set {
  35. height = value;
  36. }
  37. }
  38. public int Width {
  39. get {
  40. return width;
  41. }
  42. set {
  43. width = value;
  44. }
  45. }
  46. public PixelFormat PixelFormat {
  47. get {
  48. return pixel_format;
  49. }
  50. set {
  51. pixel_format = value;
  52. }
  53. }
  54. public int Reserved {
  55. get {
  56. return reserved;
  57. }
  58. set {
  59. reserved = value;
  60. }
  61. }
  62. public IntPtr Scan0 {
  63. get {
  64. return address;
  65. }
  66. set {
  67. if (bAllocated)
  68. {
  69. Marshal.FreeHGlobal(address);
  70. bAllocated = false;
  71. }
  72. address = value;
  73. }
  74. }
  75. public int Stride {
  76. get {
  77. return stride;
  78. }
  79. set {
  80. stride = value;
  81. }
  82. }
  83. internal unsafe void swap_red_blue_bytes ()
  84. {
  85. byte *start = (byte *) (void *) this.Scan0;
  86. int stride = this.Stride;
  87. for (int line = 0; line < this.Height; line++){
  88. // Exchange red <=> blue bytes
  89. // fixed (byte *pbuf = start) {
  90. byte* curByte = start;
  91. for (int i = 0; i < this.Width; i++) {
  92. byte t = *(curByte+2);
  93. *(curByte+2) = *curByte;
  94. *curByte = t;
  95. curByte += 3;
  96. }
  97. // }
  98. start += stride;
  99. }
  100. }
  101. }
  102. }