rawbmp.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: rawbmp.h
  3. // Purpose: interface of wxPixelData
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxPixelData
  9. A class template with ready to use implementations for getting
  10. direct and efficient access to wxBitmap's internal data and
  11. wxImage's internal data through a standard interface. It is
  12. possible to extend this class (interface) to other types of
  13. image content.
  14. Implemented on Windows, GTK+ and OS X:
  15. @li wxNativePixelData: Class to access to wxBitmap's internal data
  16. without alpha channel (RGB).
  17. @li wxAlphaPixelData: Class to access to wxBitmap's internal data with
  18. alpha channel (RGBA).
  19. Implemented everywhere:
  20. @li wxImagePixelData: Class to access to wxImage's internal data with
  21. alpha channel (RGBA).
  22. Example:
  23. @code
  24. wxBitmap bmp;
  25. wxNativePixelData data(bmp);
  26. if ( !data )
  27. {
  28. // ... raw access to bitmap data unavailable, do something else ...
  29. return;
  30. }
  31. if ( data.GetWidth() < 20 || data.GetHeight() < 20 )
  32. {
  33. // ... complain: the bitmap it too small ...
  34. return;
  35. }
  36. wxNativePixelData::Iterator p(data);
  37. // we draw a (10, 10)-(20, 20) rect manually using the given r, g, b
  38. p.Offset(data, 10, 10);
  39. for ( int y = 0; y < 10; ++y )
  40. {
  41. wxNativePixelData::Iterator rowStart = p;
  42. for ( int x = 0; x < 10; ++x, ++p )
  43. {
  44. p.Red() = r;
  45. p.Green() = g;
  46. p.Blue() = b;
  47. }
  48. p = rowStart;
  49. p.OffsetY(data, 1);
  50. }
  51. @endcode
  52. @library{wxcore}
  53. @category{gdi}
  54. @see wxBitmap, wxImage
  55. */
  56. template <class Image, class PixelFormat = wxPixelFormatFor<Image> >
  57. class wxPixelData :
  58. public wxPixelDataOut<Image>::template wxPixelDataIn<PixelFormat>
  59. {
  60. public:
  61. /**
  62. The type of the class we're working with.
  63. */
  64. typedef Image ImageType;
  65. /**
  66. Create pixel data object representing the entire image.
  67. */
  68. wxPixelData(Image& image);
  69. /**
  70. Create pixel data object representing the area of the image defined by
  71. @a rect.
  72. */
  73. wxPixelData(Image& i, const wxRect& rect);
  74. /**
  75. Create pixel data object representing the area of the image defined by
  76. @a pt and @a sz.
  77. */
  78. wxPixelData(Image& i, const wxPoint& pt, const wxSize& sz);
  79. /**
  80. Return @true of if we could get access to bitmap data successfully.
  81. */
  82. operator bool() const;
  83. /**
  84. Return the iterator pointing to the origin of the image.
  85. */
  86. Iterator GetPixels() const;
  87. /**
  88. Returns origin of the rectangular region this wxPixelData represents.
  89. */
  90. wxPoint GetOrigin() const;
  91. /**
  92. Return width of the region this wxPixelData represents.
  93. */
  94. int GetWidth() const;
  95. /**
  96. Return height of the region this wxPixelData represents.
  97. */
  98. int GetHeight() const;
  99. /**
  100. Return the area which this wxPixelData represents in the image.
  101. */
  102. wxSize GetSize() const;
  103. /**
  104. Return the distance between two rows.
  105. */
  106. int GetRowStride() const;
  107. /**
  108. The iterator of class wxPixelData.
  109. */
  110. class Iterator
  111. {
  112. public:
  113. /**
  114. Reset the iterator to point to (0, 0).
  115. */
  116. void Reset(const PixelData& data);
  117. /**
  118. Initializes the iterator to point to the origin of the given pixel
  119. data.
  120. */
  121. Iterator(PixelData& data);
  122. /**
  123. Initializes the iterator to point to the origin of the given Bitmap.
  124. */
  125. Iterator(wxBitmap& bmp, PixelData& data);
  126. /**
  127. Default constructor.
  128. */
  129. Iterator();
  130. /**
  131. Return @true if this iterator is valid.
  132. */
  133. bool IsOk() const;
  134. /**
  135. Advance the iterator to the next pixel, prefix version.
  136. */
  137. Iterator& operator++();
  138. /**
  139. Advance the iterator to the next pixel, postfix (hence less
  140. efficient -- don't use it unless you absolutely must) version.
  141. */
  142. Iterator operator++(int);
  143. /**
  144. Move @a x pixels to the right and @a y down.
  145. @note The rows won't wrap automatically.
  146. */
  147. void Offset(const PixelData& data, int x, int y);
  148. /**
  149. Move @a x pixels to the right.
  150. @note The rows won't wrap automatically.
  151. */
  152. void OffsetX(const PixelData&data, int x);
  153. /**
  154. Move @a y rows to the bottom
  155. */
  156. void OffsetY(const PixelData& data, int y);
  157. /**
  158. Go to the given position
  159. */
  160. void MoveTo(const PixelData& data, int x, int y);
  161. //@{
  162. /**
  163. Data Access: Access to individual colour components.
  164. */
  165. ChannelType& Red();
  166. ChannelType& Green();
  167. ChannelType& Blue();
  168. ChannelType& Alpha();
  169. //@}
  170. };
  171. };