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