Fl_Image.H 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // "$Id: Fl_Image.H 10192 2014-06-12 13:28:04Z ossman $"
  3. //
  4. // Image header file for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2011 by Bill Spitzak and others.
  7. //
  8. // This library is free software. Distribution and use rights are outlined in
  9. // the file "COPYING" which should have been included with this file. If this
  10. // file is missing or damaged, see the license at:
  11. //
  12. // http://www.fltk.org/COPYING.php
  13. //
  14. // Please report all bugs and problems on the following page:
  15. //
  16. // http://www.fltk.org/str.php
  17. //
  18. /* \file
  19. Fl_Image, Fl_RGB_Image classes . */
  20. #ifndef Fl_Image_H
  21. # define Fl_Image_H
  22. # include "Enumerations.H"
  23. #include <stdlib.h>
  24. #ifndef Fl_Widget_H
  25. #include "Fl_Widget.H"
  26. #endif
  27. class Fl_Widget;
  28. class Fl_Pixmap;
  29. class Fl_Menu_Item;
  30. class Fl_Label;
  31. /**
  32. Fl_Image is the base class used for caching and
  33. drawing all kinds of images in FLTK. This class keeps track of
  34. common image data such as the pixels, colormap, width, height,
  35. and depth. Virtual methods are used to provide type-specific
  36. image handling.</P>
  37. <P>Since the Fl_Image class does not support image
  38. drawing by itself, calling the draw() method results in
  39. a box with an X in it being drawn instead.
  40. */
  41. class FL_EXPORT Fl_Image {
  42. int w_, h_, d_, ld_, count_;
  43. const char * const *data_;
  44. // Forbid use of copy contructor and assign operator
  45. Fl_Image & operator=(const Fl_Image &);
  46. Fl_Image(const Fl_Image &);
  47. protected:
  48. /**
  49. Sets the current image width in pixels.
  50. */
  51. void w(int W) {w_ = W;}
  52. /**
  53. Sets the current image height in pixels.
  54. */
  55. void h(int H) {h_ = H;}
  56. /**
  57. Sets the current image depth.
  58. */
  59. void d(int D) {d_ = D;}
  60. /**
  61. Sets the current line data size in bytes.
  62. */
  63. void ld(int LD) {ld_ = LD;}
  64. /**
  65. Sets the current array pointer and count of pointers in the array.
  66. */
  67. void data(const char * const *p, int c) {data_ = p; count_ = c;}
  68. void draw_empty(int X, int Y);
  69. static void labeltype(const Fl_Label *lo, int lx, int ly, int lw, int lh, Fl_Align la);
  70. static void measure(const Fl_Label *lo, int &lw, int &lh);
  71. public:
  72. /**
  73. Returns the current image width in pixels.
  74. */
  75. int w() const {return w_;}
  76. /** Returns the current image height in pixels.
  77. */
  78. int h() const {return h_;}
  79. /**
  80. Returns the current image depth.
  81. The return value will be 0 for bitmaps, 1 for
  82. pixmaps, and 1 to 4 for color images.</P>
  83. */
  84. int d() const {return d_;}
  85. /**
  86. Returns the current line data size in bytes.
  87. Line data is extra data that is included
  88. after each line of color image data and is normally not present.
  89. */
  90. int ld() const {return ld_;}
  91. /**
  92. The count() method returns the number of data values
  93. associated with the image. The value will be 0 for images with
  94. no associated data, 1 for bitmap and color images, and greater
  95. than 2 for pixmap images.
  96. */
  97. int count() const {return count_;}
  98. /**
  99. Returns a pointer to the current image data array.
  100. Use the count() method to find the size of the data array.
  101. */
  102. const char * const *data() const {return data_;}
  103. /**
  104. The constructor creates an empty image with the specified
  105. width, height, and depth. The width and height are in pixels.
  106. The depth is 0 for bitmaps, 1 for pixmap (colormap) images, and
  107. 1 to 4 for color images.
  108. */
  109. Fl_Image(int W, int H, int D) {w_ = W; h_ = H; d_ = D; ld_ = 0; count_ = 0; data_ = 0;}
  110. virtual ~Fl_Image();
  111. virtual Fl_Image *copy(int W, int H);
  112. /**
  113. The copy() method creates a copy of the specified
  114. image. If the width and height are provided, the image is
  115. resized to the specified size. The image should be deleted (or in
  116. the case of Fl_Shared_Image, released) when you are done
  117. with it.
  118. */
  119. Fl_Image *copy() { return copy(w(), h()); }
  120. virtual void color_average(Fl_Color c, float i);
  121. /**
  122. The inactive() method calls
  123. color_average(FL_BACKGROUND_COLOR, 0.33f) to produce
  124. an image that appears grayed out. <I>This method does not
  125. alter the original image data.</I>
  126. */
  127. void inactive() { color_average(FL_GRAY, .33f); }
  128. virtual void desaturate();
  129. virtual void label(Fl_Widget*w);
  130. virtual void label(Fl_Menu_Item*m);
  131. /**
  132. Draws the image with a bounding box.
  133. This form specifies
  134. a bounding box for the image, with the origin
  135. (upper-lefthand corner) of the image offset by the cx
  136. and cy arguments.
  137. */
  138. virtual void draw(int X, int Y, int W, int H, int cx=0, int cy=0); // platform dependent
  139. /**
  140. Draws the image.
  141. This form specifies the upper-lefthand corner of the image.
  142. */
  143. void draw(int X, int Y) {draw(X, Y, w(), h(), 0, 0);} // platform dependent
  144. virtual void uncache();
  145. DECLARE_CLASS_CHEAP_RTTI_1(Fl_Image)
  146. };
  147. /**
  148. The Fl_RGB_Image class supports caching and drawing
  149. of full-color images with 1 to 4 channels of color information.
  150. Images with an even number of channels are assumed to contain
  151. alpha information, which is used to blend the image with the
  152. contents of the screen.</P>
  153. <P>Fl_RGB_Image is defined in
  154. &lt;FL/Fl_Image.H&gt;, however for compatibility reasons
  155. &lt;FL/Fl_RGB_Image.H&gt; should be included.
  156. */
  157. class FL_EXPORT Fl_RGB_Image : public Fl_Image {
  158. friend class Fl_Quartz_Graphics_Driver;
  159. friend class Fl_GDI_Graphics_Driver;
  160. friend class Fl_Xlib_Graphics_Driver;
  161. static size_t max_size_;
  162. public:
  163. const uchar *array;
  164. int alloc_array; // Non-zero if array was allocated
  165. private:
  166. #if defined(__APPLE__) || defined(WIN32)
  167. void *id_; // for internal use
  168. void *mask_; // for internal use (mask bitmap)
  169. #else
  170. unsigned id_; // for internal use
  171. unsigned mask_; // for internal use (mask bitmap)
  172. #endif // __APPLE__ || WIN32
  173. public:
  174. /**
  175. The constructor creates a new image from the specified data.
  176. \param[in] bits The image data array.
  177. \param[in] W The width of the image in pixels
  178. \param[in] H The height of the image in pixels
  179. \param[in] D The image depth, or 'number of channels'. Default=3<br>
  180. If D=1, each uchar in bits[] is a grayscale pixel value.<br>
  181. If D=2, each uchar pair in bits[] is a grayscale + alpha pixel value.<br>
  182. If D=3, each uchar triplet in bits[] is an R/G/B pixel value<br>
  183. If D=4, each uchar quad in bits[] is an R/G/B/A pixel value.
  184. \param[in] LD Line data size (default=0).<br>
  185. Line data is extra data that is included after each line
  186. of color image data and is normally not present.
  187. \see Fl_Image::data(), Fl_Image::w(), Fl_Image::h(), Fl_Image::d(), Fl_Image::ld()
  188. */
  189. Fl_RGB_Image(const uchar *bits, int W, int H, int D=3, int LD=0) :
  190. Fl_Image(W,H,D), array(bits), alloc_array(0), id_(0), mask_(0) {data((const char **)&array, 1); ld(LD);}
  191. Fl_RGB_Image(const Fl_Pixmap *pxm, Fl_Color bg=FL_GRAY);
  192. virtual ~Fl_RGB_Image();
  193. virtual Fl_Image *copy(int W, int H);
  194. Fl_Image *copy() { return copy(w(), h()); }
  195. virtual void color_average(Fl_Color c, float i);
  196. virtual void desaturate();
  197. virtual void draw(int X, int Y, int W, int H, int cx=0, int cy=0);
  198. void draw(int X, int Y) {draw(X, Y, w(), h(), 0, 0);}
  199. virtual void label(Fl_Widget*w);
  200. virtual void label(Fl_Menu_Item*m);
  201. virtual void uncache();
  202. /** Sets the maximum allowed image size in bytes when creating an Fl_RGB_Image object.
  203. The image size in bytes of an Fl_RGB_Image object is the value of the product w() * h() * d().
  204. If this product exceeds size, the created object of a derived class of Fl_RGB_Image
  205. won't be loaded with the image data.
  206. This does not apply to direct RGB image creation with
  207. Fl_RGB_Image::Fl_RGB_Image(const uchar *bits, int W, int H, int D, int LD).
  208. The default max_size() value is essentially infinite.
  209. */
  210. static void max_size(size_t size) { max_size_ = size;}
  211. /** Returns the maximum allowed image size in bytes when creating an Fl_RGB_Image object.
  212. \sa void Fl_RGB_Image::max_size(size_t)
  213. */
  214. static size_t max_size() {return max_size_;}
  215. DECLARE_CLASS_CHEAP_RTTI_2(Fl_RGB_Image, Fl_Image)
  216. };
  217. #endif // !Fl_Image_H
  218. //
  219. // End of "$Id: Fl_Image.H 10192 2014-06-12 13:28:04Z ossman $".
  220. //