From 10537b7143a06a4c0c1c4caba4f0150f9e290090 Mon Sep 17 00:00:00 2001
From: Albrecht Schlosser <albrechts.fltk@online.de>
Date: Sun, 20 Mar 2022 19:59:22 +0100
Subject: [PATCH] Make Fl_Image::copy() 'const', including all derived classes

Copying an image does not (and must not) change the original object,
hence copy() should always be 'const'.

This is *necessary* if the given Fl_Image object is 'const'.
---
 FL/Fl_Image.H                       |  8 ++++----
 FL/Fl_SVG_Image.H                   | 10 ++++++----
 FL/Fl_Shared_Image.H                |  6 ++++--
 FL/Fl_Tiled_Image.H                 |  6 ++++--
 documentation/src/migration_1_4.dox | 20 ++++++++++++++++++++
 src/Fl_Image.cxx                    |  4 ++--
 src/Fl_SVG_Image.cxx                |  6 +++---
 src/Fl_Shared_Image.cxx             |  2 +-
 src/Fl_Tiled_Image.cxx              |  8 ++++----
 9 files changed, 48 insertions(+), 22 deletions(-)

diff --git a/FL/Fl_Image.H b/FL/Fl_Image.H
index 31b005dc4..49b277740 100644
--- a/FL/Fl_Image.H
+++ b/FL/Fl_Image.H
@@ -234,7 +234,7 @@ public:
 
   Fl_Image(int W, int H, int D);
   virtual ~Fl_Image();
-  virtual Fl_Image *copy(int W, int H);
+  virtual Fl_Image *copy(int W, int H) const;
   /**
     Creates a copy of the specified image.
     The image should be released when you are done with it.
@@ -247,7 +247,7 @@ public:
     \see Fl_Image::release()
     \see Fl_Image::copy(int w, int h)
   */
-  Fl_Image *copy() { return copy(w(), h()); }
+  Fl_Image *copy() const { return copy(w(), h()); }
   virtual void color_average(Fl_Color c, float i);
   /**
    The inactive() method calls
@@ -340,8 +340,8 @@ public:
   Fl_RGB_Image(const uchar *bits, int W, int H, int D=3, int LD=0);
   Fl_RGB_Image(const Fl_Pixmap *pxm, Fl_Color bg=FL_GRAY);
   virtual ~Fl_RGB_Image();
-  virtual Fl_Image *copy(int W, int H);
-  Fl_Image *copy() { return Fl_Image::copy(); }
+  virtual Fl_Image *copy(int W, int H) const;
+  Fl_Image *copy() const { return Fl_Image::copy(); }
   virtual void color_average(Fl_Color c, float i);
   virtual void desaturate();
   virtual void draw(int X, int Y, int W, int H, int cx=0, int cy=0);
diff --git a/FL/Fl_SVG_Image.H b/FL/Fl_SVG_Image.H
index 2f98a2a46..d09f3882b 100644
--- a/FL/Fl_SVG_Image.H
+++ b/FL/Fl_SVG_Image.H
@@ -147,16 +147,18 @@ private:
   float svg_scaling_(int W, int H);
   void rasterize_(int W, int H);
   virtual void cache_size_(int &width, int &height);
-  void init_(const char *filename, const char *filedata, Fl_SVG_Image *copy_source);
-  Fl_SVG_Image(Fl_SVG_Image *source);
+  void init_(const char *filename, const char *filedata, const Fl_SVG_Image *copy_source);
+  Fl_SVG_Image(const Fl_SVG_Image *source);
 public:
   /** Set this to \c false to allow image re-scaling that alters the image aspect ratio.
    Upon object creation, proportional is set to \c true, and the aspect ratio is kept constant.*/
   bool proportional;
   Fl_SVG_Image(const char *filename, const char *svg_data = NULL);
   virtual ~Fl_SVG_Image();
-  virtual Fl_Image *copy(int W, int H);
-  Fl_Image *copy() { return Fl_Image::copy(); }
+  virtual Fl_Image *copy(int W, int H) const;
+  Fl_Image *copy() const {
+    return Fl_Image::copy();
+  }
   void resize(int width, int height);
   virtual void desaturate();
   virtual void color_average(Fl_Color c, float i);
diff --git a/FL/Fl_Shared_Image.H b/FL/Fl_Shared_Image.H
index d109014e5..88a3050be 100644
--- a/FL/Fl_Shared_Image.H
+++ b/FL/Fl_Shared_Image.H
@@ -145,8 +145,10 @@ public:
     return this;
   }
 
-  virtual Fl_Image *copy(int W, int H);
-  Fl_Image *copy() { return Fl_Image::copy(); }
+  virtual Fl_Image *copy(int W, int H) const;
+  Fl_Image *copy() const {
+    return Fl_Image::copy();
+  }
   virtual void color_average(Fl_Color c, float i);
   virtual void desaturate();
   virtual void draw(int X, int Y, int W, int H, int cx = 0, int cy = 0);
diff --git a/FL/Fl_Tiled_Image.H b/FL/Fl_Tiled_Image.H
index 66814a8fd..599de4b4e 100644
--- a/FL/Fl_Tiled_Image.H
+++ b/FL/Fl_Tiled_Image.H
@@ -40,8 +40,10 @@ class FL_EXPORT Fl_Tiled_Image : public Fl_Image {
   Fl_Tiled_Image(Fl_Image *i, int W = 0, int H = 0);
   virtual ~Fl_Tiled_Image();
 
-  virtual Fl_Image *copy(int W, int H);
-  Fl_Image *copy() { return Fl_Image::copy(); }
+  virtual Fl_Image *copy(int W, int H) const;
+  Fl_Image *copy() const {
+    return Fl_Image::copy();
+  }
   virtual void color_average(Fl_Color c, float i);
   virtual void desaturate();
   virtual void draw(int X, int Y, int W, int H, int cx = 0, int cy = 0);
diff --git a/documentation/src/migration_1_4.dox b/documentation/src/migration_1_4.dox
index 37420eab3..350453611 100644
--- a/documentation/src/migration_1_4.dox
+++ b/documentation/src/migration_1_4.dox
@@ -73,6 +73,26 @@ file from its old location to the new location as documented in
 Fl_Preferences::Fl_Preferences(Root, const char*, const char*) .
 
 
+\section migration_1_4_copy_image Fl_Image::copy() 'const'
+
+Since FLTK 1.4.0 the virtual method Fl_Image::copy() has been declared
+'const' so read-only ('const') images can be copied w/o casts.
+
+This will very likely not affect user code. However, if you derived your
+own class from any of the Fl_*_Image variants \b and you overrode
+'Your'_Image::copy() then you must declare this 'const' as well, i.e.
+you must add the keyword 'const' to your declaration of copy() in your
+header file and the implementation.
+
+Code example in header file:
+\code
+  class Your_Image {
+    // ...
+    copy() const;
+    copy(int w, int h) const;
+  };
+
+
 \htmlonly
 <hr>
 <table summary="navigation bar" width="100%" border="0">
diff --git a/src/Fl_Image.cxx b/src/Fl_Image.cxx
index eba5b2001..3ff657b92 100644
--- a/src/Fl_Image.cxx
+++ b/src/Fl_Image.cxx
@@ -88,7 +88,7 @@ void Fl_Image::draw_empty(int X, int Y) {
 
   \param[in] W,H  Requested width and height of the copied image
 */
-Fl_Image *Fl_Image::copy(int W, int H) {
+Fl_Image *Fl_Image::copy(int W, int H) const {
   return new Fl_Image(W, H, d());
 }
 
@@ -401,7 +401,7 @@ void Fl_RGB_Image::uncache() {
   Fl_Graphics_Driver::default_driver().uncache(this, id_, mask_);
 }
 
-Fl_Image *Fl_RGB_Image::copy(int W, int H) {
+Fl_Image *Fl_RGB_Image::copy(int W, int H) const {
   Fl_RGB_Image  *new_image;     // New RGB image
   uchar         *new_array;     // New array for image data
 
diff --git a/src/Fl_SVG_Image.cxx b/src/Fl_SVG_Image.cxx
index 5af4723b3..58dd48b34 100644
--- a/src/Fl_SVG_Image.cxx
+++ b/src/Fl_SVG_Image.cxx
@@ -64,7 +64,7 @@ Fl_SVG_Image::Fl_SVG_Image(const char *filename, const char *svg_data) : Fl_RGB_
 
 
 // private constructor
-Fl_SVG_Image::Fl_SVG_Image(Fl_SVG_Image *source) : Fl_RGB_Image(NULL, 0, 0, 4) {
+Fl_SVG_Image::Fl_SVG_Image(const Fl_SVG_Image *source) : Fl_RGB_Image(NULL, 0, 0, 4) {
   init_(NULL, NULL, source);
 }
 
@@ -124,7 +124,7 @@ static char *svg_inflate(const char *fname) {
 }
 #endif
 
-void Fl_SVG_Image::init_(const char *filename, const char *in_filedata, Fl_SVG_Image *copy_source) {
+void Fl_SVG_Image::init_(const char *filename, const char *in_filedata, const Fl_SVG_Image *copy_source) {
   if (copy_source) {
     filename = in_filedata = NULL;
     counted_svg_image_ = copy_source->counted_svg_image_;
@@ -206,7 +206,7 @@ void Fl_SVG_Image::rasterize_(int W, int H) {
 }
 
 
-Fl_Image *Fl_SVG_Image::copy(int W, int H) {
+Fl_Image *Fl_SVG_Image::copy(int W, int H) const {
   Fl_SVG_Image *svg2 = new Fl_SVG_Image(this);
   svg2->to_desaturate_ = to_desaturate_;
   svg2->average_weight_ = average_weight_;
diff --git a/src/Fl_Shared_Image.cxx b/src/Fl_Shared_Image.cxx
index 8b3a34ded..91e093b6f 100644
--- a/src/Fl_Shared_Image.cxx
+++ b/src/Fl_Shared_Image.cxx
@@ -300,7 +300,7 @@ void Fl_Shared_Image::reload() {
 // For doxygen docs see Fl_Image::copy().
 
 Fl_Image *
-Fl_Shared_Image::copy(int W, int H) {
+Fl_Shared_Image::copy(int W, int H) const {
   Fl_Image              *temp_image;    // New image file
   Fl_Shared_Image       *temp_shared;   // New shared image
 
diff --git a/src/Fl_Tiled_Image.cxx b/src/Fl_Tiled_Image.cxx
index a97081e6a..da1ed5531 100644
--- a/src/Fl_Tiled_Image.cxx
+++ b/src/Fl_Tiled_Image.cxx
@@ -1,7 +1,7 @@
 //
 // Tiled image code for the Fast Light Tool Kit (FLTK).
 //
-// Copyright 1998-2020 by Bill Spitzak and others.
+// Copyright 1998-2022 by Bill Spitzak and others.
 //
 // This library is free software. Distribution and use rights are outlined in
 // the file "COPYING" which should have been included with this file.  If this
@@ -77,9 +77,9 @@ Fl_Tiled_Image::Fl_Tiled_Image(Fl_Image *i,     // I - Image to tile
 // 'Fl_Tiled_Image::copy()' - Copy and resize a tiled image...
 //
 
-Fl_Image *                      // O - New image
-Fl_Tiled_Image::copy(int W,     // I - New width
-                     int H) {   // I - New height
+Fl_Image *                            // O - New image
+Fl_Tiled_Image::copy(int W,           // I - New width
+                     int H) const {   // I - New height
   return new Fl_Tiled_Image(image_, W, H);
 }
 
-- 
GitLab