Fl_Image.H 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // "$Id: Fl_Image.H 12443 2017-09-11 18:54:10Z AlbrechtS $"
  3. //
  4. // Image header file for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2017 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. #include "Fl_Widget.H" // for fl_uintptr_t
  25. class Fl_Widget;
  26. class Fl_Pixmap;
  27. struct Fl_Menu_Item;
  28. struct Fl_Label;
  29. class Fl_RGB_Image;
  30. /** \enum Fl_RGB_Scaling
  31. The scaling algorithm to use for RGB images.
  32. */
  33. enum Fl_RGB_Scaling {
  34. FL_RGB_SCALING_NEAREST = 0, ///< default RGB image scaling algorithm
  35. FL_RGB_SCALING_BILINEAR ///< more accurate, but slower RGB image scaling algorithm
  36. };
  37. /**
  38. \brief Base class for image caching and drawing.
  39. Fl_Image is the base class used for caching and drawing all kinds of images
  40. in FLTK. This class keeps track of common image data such as the pixels,
  41. colormap, width, height, and depth. Virtual methods are used to provide
  42. type-specific image handling.
  43. Since the Fl_Image class does not support image
  44. drawing by itself, calling the draw() method results in
  45. a box with an X in it being drawn instead.
  46. */
  47. class FL_EXPORT Fl_Image {
  48. friend class Fl_Graphics_Driver;
  49. public:
  50. static const int ERR_NO_IMAGE = -1;
  51. static const int ERR_FILE_ACCESS = -2;
  52. static const int ERR_FORMAT = -3;
  53. private:
  54. int w_, h_, d_, ld_, count_;
  55. const char * const *data_;
  56. static Fl_RGB_Scaling RGB_scaling_;
  57. // Forbid use of copy constructor and assign operator
  58. Fl_Image & operator=(const Fl_Image &);
  59. Fl_Image(const Fl_Image &);
  60. protected:
  61. /**
  62. Sets the current image width in pixels.
  63. */
  64. void w(int W) {w_ = W;}
  65. /**
  66. Sets the current image height in pixels.
  67. */
  68. void h(int H) {h_ = H;}
  69. /**
  70. Sets the current image depth.
  71. */
  72. void d(int D) {d_ = D;}
  73. /**
  74. Sets the current line data size in bytes.
  75. Color images may contain extra data that is included after every
  76. line of color image data and is normally not present.
  77. If \p LD is zero, then line data size is assumed to be w() * d() bytes.
  78. If \p LD is non-zero, then it must be positive and larger than w() * d()
  79. to account for the extra data per line.
  80. */
  81. void ld(int LD) {ld_ = LD;}
  82. /**
  83. Sets the current array pointer and count of pointers in the array.
  84. */
  85. void data(const char * const *p, int c) {data_ = p; count_ = c;}
  86. void draw_empty(int X, int Y);
  87. static void labeltype(const Fl_Label *lo, int lx, int ly, int lw, int lh, Fl_Align la);
  88. static void measure(const Fl_Label *lo, int &lw, int &lh);
  89. virtual int draw_scaled(int X, int Y, int W, int H);
  90. public:
  91. /**
  92. Returns the current image width in pixels.
  93. */
  94. int w() const {return w_;}
  95. /**
  96. Returns the current image height in pixels.
  97. */
  98. int h() const {return h_;}
  99. /**
  100. Returns the current image depth.
  101. The return value will be 0 for bitmaps, 1 for
  102. pixmaps, and 1 to 4 for color images.</P>
  103. */
  104. int d() const {return d_;}
  105. /**
  106. Returns the current line data size in bytes.
  107. \see ld(int)
  108. */
  109. int ld() const {return ld_;}
  110. /**
  111. The count() method returns the number of data values
  112. associated with the image. The value will be 0 for images with
  113. no associated data, 1 for bitmap and color images, and greater
  114. than 2 for pixmap images.
  115. */
  116. int count() const {return count_;}
  117. /**
  118. Returns a pointer to the current image data array.
  119. Use the count() method to find the size of the data array.
  120. */
  121. const char * const *data() const {return data_;}
  122. int fail();
  123. Fl_Image(int W, int H, int D);
  124. virtual ~Fl_Image();
  125. virtual Fl_Image *copy(int W, int H);
  126. /**
  127. The copy() method creates a copy of the specified
  128. image. If the width and height are provided, the image is
  129. resized to the specified size. The image should be deleted (or in
  130. the case of Fl_Shared_Image, released) when you are done
  131. with it.
  132. */
  133. Fl_Image *copy() { return copy(w(), h()); }
  134. virtual void color_average(Fl_Color c, float i);
  135. /**
  136. The inactive() method calls
  137. color_average(FL_BACKGROUND_COLOR, 0.33f) to produce
  138. an image that appears grayed out.
  139. An internal copy is made of the original image before
  140. changes are applied, to avoid modifying the original image.
  141. */
  142. void inactive() { color_average(FL_GRAY, .33f); }
  143. virtual void desaturate();
  144. virtual void label(Fl_Widget*w);
  145. virtual void label(Fl_Menu_Item*m);
  146. /**
  147. Draws the image to the current drawing surface with a bounding box.
  148. Arguments <tt>X,Y,W,H</tt> specify
  149. a bounding box for the image, with the origin
  150. (upper-left corner) of the image offset by the \c cx
  151. and \c cy arguments.
  152. In other words: <tt>fl_push_clip(X,Y,W,H)</tt> is applied,
  153. the image is drawn with its upper-left corner at <tt>X-cx,Y-cy</tt> and its own width and height,
  154. <tt>fl_pop_clip</tt><tt>()</tt> is applied.
  155. */
  156. virtual void draw(int X, int Y, int W, int H, int cx=0, int cy=0); // platform dependent
  157. /**
  158. Draws the image to the current drawing surface.
  159. \param X, Y specify the upper-lefthand corner of the image.
  160. */
  161. void draw(int X, int Y) {draw(X, Y, w(), h(), 0, 0);} // platform dependent
  162. virtual void uncache();
  163. // used by fl_define_FL_IMAGE_LABEL() to avoid 'friend' declaration
  164. static Fl_Labeltype define_FL_IMAGE_LABEL();
  165. // set RGB image scaling method
  166. static void RGB_scaling(Fl_RGB_Scaling);
  167. // get RGB image scaling method
  168. static Fl_RGB_Scaling RGB_scaling();
  169. /** Use this method if you have an Fl_Image object and want to know whether it is derived
  170. from class Fl_RGB_Image.
  171. If the method returns non-NULL, then the image in question is
  172. derived from Fl_RGB_Image, and the returned value is a pointer to this image.
  173. */
  174. virtual Fl_RGB_Image *as_rgb_image() {return NULL;}
  175. DECLARE_CLASS_CHEAP_RTTI_1(Fl_Image)
  176. };
  177. /**
  178. The Fl_RGB_Image class supports caching and drawing
  179. of full-color images with 1 to 4 channels of color information.
  180. Images with an even number of channels are assumed to contain
  181. alpha information, which is used to blend the image with the
  182. contents of the screen.
  183. Fl_RGB_Image is defined in
  184. &lt;FL/Fl_Image.H&gt;, however for compatibility reasons
  185. &lt;FL/Fl_RGB_Image.H&gt; should be included.
  186. */
  187. class FL_EXPORT Fl_RGB_Image : public Fl_Image {
  188. friend class Fl_Graphics_Driver;
  189. static size_t max_size_;
  190. public:
  191. /** Points to the start of the object's data array
  192. */
  193. const uchar *array;
  194. /** If non-zero, the object's data array is delete[]'d when deleting the object.
  195. */
  196. int alloc_array;
  197. private:
  198. // These two variables are used to cache the image and mask for the main display graphics driver
  199. fl_uintptr_t id_;
  200. fl_uintptr_t mask_;
  201. float cache_scale_; // graphics scaling value when id_ was computed
  202. protected:
  203. virtual int draw_scaled(int X, int Y, int W, int H);
  204. public:
  205. Fl_RGB_Image(const uchar *bits, int W, int H, int D=3, int LD=0);
  206. Fl_RGB_Image(const Fl_Pixmap *pxm, Fl_Color bg=FL_GRAY);
  207. virtual ~Fl_RGB_Image();
  208. virtual Fl_Image *copy(int W, int H);
  209. Fl_Image *copy() { return copy(w(), h()); }
  210. virtual void color_average(Fl_Color c, float i);
  211. virtual void desaturate();
  212. virtual void draw(int X, int Y, int W, int H, int cx=0, int cy=0);
  213. void draw(int X, int Y) {draw(X, Y, w(), h(), 0, 0);}
  214. virtual void label(Fl_Widget*w);
  215. virtual void label(Fl_Menu_Item*m);
  216. virtual void uncache();
  217. /** Sets the maximum allowed image size in bytes when creating an Fl_RGB_Image object.
  218. The image size in bytes of an Fl_RGB_Image object is the value of the product w() * h() * d().
  219. If this product exceeds size, the created object of a derived class of Fl_RGB_Image
  220. won't be loaded with the image data.
  221. This does not apply to direct RGB image creation with
  222. Fl_RGB_Image::Fl_RGB_Image(const uchar *bits, int W, int H, int D, int LD).
  223. The default max_size() value is essentially infinite.
  224. */
  225. static void max_size(size_t size) { max_size_ = size;}
  226. /** Returns the maximum allowed image size in bytes when creating an Fl_RGB_Image object.
  227. \sa void Fl_RGB_Image::max_size(size_t)
  228. */
  229. static size_t max_size() {return max_size_;}
  230. virtual Fl_RGB_Image *as_rgb_image() {return this;}
  231. DECLARE_CLASS_CHEAP_RTTI_2(Fl_RGB_Image, Fl_Image)
  232. };
  233. #endif // !Fl_Image_H
  234. //
  235. // End of "$Id: Fl_Image.H 12443 2017-09-11 18:54:10Z AlbrechtS $".
  236. //