Fl_Shared_Image.H 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // "$Id: Fl_Shared_Image.H 12588 2017-12-12 09:23:48Z manolo $"
  3. //
  4. // Shared 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_Shared_Image class. */
  20. #ifndef Fl_Shared_Image_H
  21. # define Fl_Shared_Image_H
  22. # include "Fl_Image.H"
  23. // Test function for adding new formats
  24. typedef Fl_Image *(*Fl_Shared_Handler)(const char *name, uchar *header,
  25. int headerlen);
  26. // Shared images class.
  27. /**
  28. This class supports caching, loading, scaling, and drawing of image files.
  29. Most applications will also want to link against the fltk_images library
  30. and call the fl_register_images() function to support standard image
  31. formats such as BMP, GIF, JPEG, PNG, and SVG (unless the library was built
  32. with the option removing SVG support).
  33. Images can be requested (loaded) with Fl_Shared_Image::get(), find(),
  34. and some other methods. All images are cached in an internal list of
  35. shared images and should be released when they are no longer needed.
  36. A refcount is used to determine if a released image is to be destroyed
  37. with delete.
  38. \see Fl_Shared_Image::get()
  39. \see Fl_Shared_Image::find()
  40. \see Fl_Shared_Image::release()
  41. */
  42. class FL_EXPORT Fl_Shared_Image : public Fl_Image {
  43. friend class Fl_JPEG_Image;
  44. friend class Fl_PNG_Image;
  45. friend class Fl_Graphics_Driver;
  46. private:
  47. static Fl_RGB_Scaling scaling_algorithm_; // method used to rescale RGB source images
  48. Fl_Image *scaled_image_;
  49. protected:
  50. static Fl_Shared_Image **images_; // Shared images
  51. static int num_images_; // Number of shared images
  52. static int alloc_images_; // Allocated shared images
  53. static Fl_Shared_Handler *handlers_; // Additional format handlers
  54. static int num_handlers_; // Number of format handlers
  55. static int alloc_handlers_; // Allocated format handlers
  56. const char *name_; // Name of image file
  57. int original_; // Original image?
  58. int refcount_; // Number of times this image has been used
  59. Fl_Image *image_; // The image that is shared
  60. int alloc_image_; // Was the image allocated?
  61. static int compare(Fl_Shared_Image **i0, Fl_Shared_Image **i1);
  62. // Use get() and release() to load/delete images in memory...
  63. Fl_Shared_Image();
  64. Fl_Shared_Image(const char *n, Fl_Image *img = 0);
  65. virtual ~Fl_Shared_Image();
  66. void add();
  67. void update();
  68. public:
  69. /** Returns the filename of the shared image */
  70. const char *name() { return name_; }
  71. /** Returns the number of references of this shared image.
  72. When reference is below 1, the image is deleted.
  73. */
  74. int refcount() { return refcount_; }
  75. /** Returns whether this is an original image.
  76. Images loaded from a file or from memory are marked \p original as
  77. opposed to images created as a copy of another image with different
  78. size (width or height).
  79. \note This is useful for debugging (rarely used in user code).
  80. \since FLTK 1.4.0
  81. */
  82. int original() { return original_; }
  83. void release();
  84. void reload();
  85. virtual Fl_Image *copy(int W, int H);
  86. Fl_Image *copy() { return copy(w(), h()); }
  87. virtual void color_average(Fl_Color c, float i);
  88. virtual void desaturate();
  89. virtual void draw(int X, int Y, int W, int H, int cx = 0, int cy = 0);
  90. void draw(int X, int Y) { draw(X, Y, w(), h(), 0, 0); }
  91. void scale(int width, int height, int proportional = 1, int can_expand = 0);
  92. virtual void uncache();
  93. static Fl_Shared_Image *find(const char *name, int W = 0, int H = 0);
  94. static Fl_Shared_Image *get(const char *name, int W = 0, int H = 0);
  95. static Fl_Shared_Image *get(Fl_RGB_Image *rgb, int own_it = 1);
  96. static Fl_Shared_Image **images();
  97. static int num_images();
  98. static void add_handler(Fl_Shared_Handler f);
  99. static void remove_handler(Fl_Shared_Handler f);
  100. /** Sets what algorithm is used when resizing a source image.
  101. The default algorithm is FL_RGB_SCALING_BILINEAR.
  102. Drawing an Fl_Shared_Image is sometimes performed by first resizing the source image
  103. and then drawing the resized copy. This occurs, e.g., when drawing to screen under Linux
  104. after having called Fl_Shared_Image::scale().
  105. This function controls what method is used when the image to be resized is an Fl_RGB_Image.
  106. \version 1.3.4
  107. */
  108. static void scaling_algorithm(Fl_RGB_Scaling algorithm) {scaling_algorithm_ = algorithm; }
  109. DECLARE_CLASS_CHEAP_RTTI_2(Fl_Shared_Image, Fl_Image)
  110. };
  111. //
  112. // The following function is provided in the fltk_images library and
  113. // registers all of the "extra" image file formats that are not part
  114. // of the core FLTK library...
  115. //
  116. FL_EXPORT extern void fl_register_images();
  117. #endif // !Fl_Shared_Image_H
  118. //
  119. // End of "$Id: Fl_Shared_Image.H 12588 2017-12-12 09:23:48Z manolo $"
  120. //