Fix HAVE_* config values checks
[geeqie.git] / src / color-man.cc
index b8cadd8..f73080c 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include "main.h"
 #include "color-man.h"
 
-#include "image.h"
-#include "ui-fileops.h"
-
+#include <config.h>
 
-#ifdef HAVE_LCMS
+#if HAVE_LCMS
 /*** color support enabled ***/
 
-#ifdef HAVE_LCMS2
-#include <lcms2.h>
+#include <cstdint>
+#include <cstring>
+#include <vector>
+
+#include <glib-object.h>
+
+#if HAVE_LCMS2
+#  include <lcms2.h>
 #else
-#include <lcms.h>
+#  include <lcms.h>
 #endif
 
+#include "debug.h"
+#include "filedata.h"
+#include "image.h"
+#include "intl.h"
+#include "options.h"
+#include "ui-fileops.h"
 
-typedef struct _ColorManCache ColorManCache;
-struct _ColorManCache {
+struct ColorManCache {
        cmsHPROFILE   profile_in;
        cmsHPROFILE   profile_out;
        cmsHTRANSFORM transform;
@@ -57,22 +65,22 @@ struct _ColorManCache {
 #define COLOR_MAN_CHUNK_SIZE 81900
 
 
-static void color_man_lib_init(void)
+static void color_man_lib_init()
 {
        static gboolean init_done = FALSE;
 
        if (init_done) return;
        init_done = TRUE;
 
-#ifndef HAVE_LCMS2
+#if !HAVE_LCMS2
        cmsErrorAction(LCMS_ERROR_IGNORE);
 #endif
 }
 
-static cmsHPROFILE color_man_create_adobe_comp(void)
+static cmsHPROFILE color_man_create_adobe_comp()
 {
        /* ClayRGB1998 is AdobeRGB compatible */
-#include "ClayRGB1998_icc.h"
+#include "ClayRGB1998_icc.h" // IWYU pragma: keep
        return cmsOpenProfileFromMem(ClayRGB1998_icc, ClayRGB1998_icc_len);
 }
 
@@ -82,7 +90,7 @@ static cmsHPROFILE color_man_create_adobe_comp(void)
  *-------------------------------------------------------------------
  */
 
-static GList *cm_cache_list = NULL;
+static GList *cm_cache_list = nullptr;
 
 
 static void color_man_cache_ref(ColorManCache *cc)
@@ -113,7 +121,7 @@ static void color_man_cache_unref(ColorManCache *cc)
 static cmsHPROFILE color_man_cache_load_profile(ColorManProfileType type, const gchar *file,
                                                guchar *data, guint data_len)
 {
-       cmsHPROFILE profile = NULL;
+       cmsHPROFILE profile = nullptr;
 
        switch (type)
                {
@@ -181,7 +189,7 @@ static ColorManCache *color_man_cache_new(ColorManProfileType in_type, const gch
                                  (!cc->profile_in) ? cc->profile_in_file : cc->profile_out_file);
 
                color_man_cache_unref(cc);
-               return NULL;
+               return nullptr;
                }
 
        cc->transform = cmsCreateTransform(cc->profile_in,
@@ -195,7 +203,7 @@ static ColorManCache *color_man_cache_new(ColorManProfileType in_type, const gch
                DEBUG_1("failed to create color profile transform");
 
                color_man_cache_unref(cc);
-               return NULL;
+               return nullptr;
                }
 
        if (cc->profile_in_type != COLOR_PROFILE_MEM && cc->profile_out_type != COLOR_PROFILE_MEM )
@@ -215,13 +223,13 @@ static void color_man_cache_free(ColorManCache *cc)
        color_man_cache_unref(cc);
 }
 
-static void color_man_cache_reset(void)
+static void color_man_cache_reset()
 {
        while (cm_cache_list)
                {
                ColorManCache *cc;
 
-               cc = cm_cache_list->data;
+               cc = static_cast<ColorManCache *>(cm_cache_list->data);
                color_man_cache_free(cc);
                }
 }
@@ -238,7 +246,7 @@ static ColorManCache *color_man_cache_find(ColorManProfileType in_type, const gc
                ColorManCache *cc;
                gboolean match = FALSE;
 
-               cc = work->data;
+               cc = static_cast<ColorManCache *>(work->data);
                work = work->next;
 
                if (cc->profile_in_type == in_type &&
@@ -262,7 +270,7 @@ static ColorManCache *color_man_cache_find(ColorManProfileType in_type, const gc
                if (match) return cc;
                }
 
-       return NULL;
+       return nullptr;
 }
 
 static ColorManCache *color_man_cache_get(ColorManProfileType in_type, const gchar *in_file,
@@ -291,13 +299,16 @@ static ColorManCache *color_man_cache_get(ColorManProfileType in_type, const gch
  *-------------------------------------------------------------------
  */
 
-static void color_man_done(ColorMan *cm, ColorManReturnType type)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-function"
+static void color_man_done_unused(ColorMan *cm, ColorManReturnType type)
 {
        if (cm->func_done)
                {
                cm->func_done(cm, type, cm->func_done_data);
                }
 }
+#pragma GCC diagnostic pop
 
 void color_man_correct_region(ColorMan *cm, GdkPixbuf *pixbuf, gint x, gint y, gint w, gint h)
 {
@@ -305,13 +316,14 @@ void color_man_correct_region(ColorMan *cm, GdkPixbuf *pixbuf, gint x, gint y, g
        guchar *pix;
        gint rs;
        gint i;
-       gint pixbuf_width, pixbuf_height;
+       gint pixbuf_width;
+       gint pixbuf_height;
 
 
        pixbuf_width = gdk_pixbuf_get_width(pixbuf);
        pixbuf_height = gdk_pixbuf_get_height(pixbuf);
 
-       cc = cm->profile;
+       cc = static_cast<ColorManCache *>(cm->profile);
 
        pix = gdk_pixbuf_get_pixels(pixbuf);
        rs = gdk_pixbuf_get_rowstride(pixbuf);
@@ -335,10 +347,13 @@ void color_man_correct_region(ColorMan *cm, GdkPixbuf *pixbuf, gint x, gint y, g
 
 }
 
-static gboolean color_man_idle_cb(gpointer data)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-function"
+static gboolean color_man_idle_cb_unused(gpointer data)
 {
-       ColorMan *cm = static_cast<ColorMan *>(data);
-       gint width, height;
+       auto *cm = static_cast<ColorMan *>(data);
+       gint width;
+       gint height;
        gint rh;
 
        if (!cm->pixbuf) return FALSE;
@@ -347,7 +362,7 @@ static gboolean color_man_idle_cb(gpointer data)
            cm->pixbuf != image_get_pixbuf(cm->imd))
                {
                cm->idle_id = 0;
-               color_man_done(cm, COLOR_RETURN_IMAGE_CHANGED);
+               color_man_done_unused(cm, COLOR_RETURN_IMAGE_CHANGED);
                return FALSE;
                }
 
@@ -362,7 +377,7 @@ static gboolean color_man_idle_cb(gpointer data)
                        }
 
                cm->idle_id = 0;
-               color_man_done(cm, COLOR_RETURN_SUCCESS);
+               color_man_done_unused(cm, COLOR_RETURN_SUCCESS);
                return FALSE;
                }
 
@@ -373,6 +388,7 @@ static gboolean color_man_idle_cb(gpointer data)
 
        return TRUE;
 }
+#pragma GCC diagnostic pop
 
 static ColorMan *color_man_new_real(ImageWindow *imd, GdkPixbuf *pixbuf,
                                    ColorManProfileType input_type, const gchar *input_file,
@@ -397,7 +413,7 @@ static ColorMan *color_man_new_real(ImageWindow *imd, GdkPixbuf *pixbuf,
        if (!cm->profile)
                {
                color_man_free(cm);
-               return NULL;
+               return nullptr;
                }
 
        return cm;
@@ -409,16 +425,19 @@ ColorMan *color_man_new(ImageWindow *imd, GdkPixbuf *pixbuf,
                        guchar *screen_data, guint screen_data_len)
 {
        return color_man_new_real(imd, pixbuf,
-                                 input_type, input_file, NULL, 0,
+                                 input_type, input_file, nullptr, 0,
                                  screen_type, screen_file, screen_data, screen_data_len);
 }
 
-//void color_man_start_bg(ColorMan *cm, ColorManDoneFunc done_func, gpointer done_data)
-//{
-       //cm->func_done = done_func;
-       //cm->func_done_data = done_data;
-       //cm->idle_id = g_idle_add(color_man_idle_cb, cm);
-//}
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-function"
+void color_man_start_bg_unused(ColorMan *cm, ColorMan::DoneFunc done_func, gpointer done_data)
+{
+       cm->func_done = done_func;
+       cm->func_done_data = done_data;
+       cm->idle_id = g_idle_add(color_man_idle_cb_unused, cm);
+}
+#pragma GCC diagnostic pop
 
 ColorMan *color_man_new_embedded(ImageWindow *imd, GdkPixbuf *pixbuf,
                                 guchar *input_data, guint input_data_len,
@@ -426,7 +445,7 @@ ColorMan *color_man_new_embedded(ImageWindow *imd, GdkPixbuf *pixbuf,
                                 guchar *screen_data, guint screen_data_len)
 {
        return color_man_new_real(imd, pixbuf,
-                                 COLOR_PROFILE_MEM, NULL, input_data, input_data_len,
+                                 COLOR_PROFILE_MEM, nullptr, input_data, input_data_len,
                                  screen_type, screen_file, screen_data, screen_data_len);
 }
 
@@ -443,7 +462,7 @@ static gchar *color_man_get_profile_name(ColorManProfileType type, cmsHPROFILE p
                case COLOR_PROFILE_FILE:
                        if (profile)
                                {
-#ifdef HAVE_LCMS2
+#if HAVE_LCMS2
                                char buffer[20];
                                buffer[0] = '\0';
                                cmsGetProfileInfoASCII(profile, cmsInfoDescription, "en", "US", buffer, 20);
@@ -466,7 +485,7 @@ gboolean color_man_get_status(ColorMan *cm, gchar **image_profile, gchar **scree
        ColorManCache *cc;
        if (!cm) return FALSE;
 
-       cc = cm->profile;
+       cc = static_cast<ColorManCache *>(cm->profile);
 
        if (image_profile) *image_profile = color_man_get_profile_name(cc->profile_in_type, cc->profile_in);
        if (screen_profile) *screen_profile = color_man_get_profile_name(cc->profile_out_type, cc->profile_out);
@@ -480,34 +499,32 @@ void color_man_free(ColorMan *cm)
        if (cm->idle_id) g_source_remove(cm->idle_id);
        if (cm->pixbuf) g_object_unref(cm->pixbuf);
 
-       color_man_cache_unref(cm->profile);
+       color_man_cache_unref(static_cast<ColorManCache *>(cm->profile));
 
        g_free(cm);
 }
 
-void color_man_update(void)
+void color_man_update()
 {
        color_man_cache_reset();
 }
 
-#ifdef HAVE_HEIF
+#if HAVE_HEIF
+#include <cmath>
 #include <libheif/heif.h>
-#include <math.h>
 
 static cmsToneCurve* colorspaces_create_transfer(int32_t size, double (*fct)(double))
 {
-       float *values = static_cast<float *>(g_malloc(sizeof(float) * size));
-
+       std::vector<float> values;
+       values.reserve(size);
        for(int32_t i = 0; i < size; ++i)
                {
-               const double x = (float)i / (size - 1);
-               const double y = MIN(fct(x), 1.0f);
-               values[i] = (float)y;
+               const double x = static_cast<float>(i) / (size - 1);
+               const double y = MIN(fct(x), 1.0F);
+               values.push_back(static_cast<float>(y));
                }
 
-       cmsToneCurve* result = cmsBuildTabulatedToneCurveFloat(NULL, size, values);
-       g_free(values);
-       return result;
+       return cmsBuildTabulatedToneCurveFloat(nullptr, size, values.data());
 }
 
 // https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.2100-2-201807-I!!PDF-F.pdf
@@ -572,12 +589,12 @@ static guchar *nclx_to_lcms_profile(const struct heif_color_profile_nclx *nclx,
 {
        const gchar *primaries_name = "";
        const gchar *trc_name = "";
-       cmsHPROFILE *profile = NULL;
+       cmsHPROFILE *profile = nullptr;
        cmsCIExyY whitepoint;
        cmsCIExyYTRIPLE primaries;
        cmsToneCurve *curve[3];
        cmsUInt32Number size;
-       guint8 *data = NULL;
+       guint8 *data = nullptr;
 
        cmsFloat64Number srgb_parameters[5] =
        { 2.4, 1.0 / 1.055,  0.055 / 1.055, 1.0 / 12.92, 0.04045 };
@@ -585,31 +602,31 @@ static guchar *nclx_to_lcms_profile(const struct heif_color_profile_nclx *nclx,
        cmsFloat64Number rec709_parameters[5] =
        { 2.2, 1.0 / 1.099,  0.099 / 1.099, 1.0 / 4.5, 0.081 };
 
-       if (nclx == NULL)
+       if (nclx == nullptr)
                {
-               return NULL;
+               return nullptr;
                }
 
        if (nclx->color_primaries == heif_color_primaries_unspecified)
                {
-               return NULL;
+               return nullptr;
                }
 
        whitepoint.x = nclx->color_primary_white_x;
        whitepoint.y = nclx->color_primary_white_y;
-       whitepoint.Y = 1.0f;
+       whitepoint.Y = 1.0F;
 
        primaries.Red.x = nclx->color_primary_red_x;
        primaries.Red.y = nclx->color_primary_red_y;
-       primaries.Red.Y = 1.0f;
+       primaries.Red.Y = 1.0F;
 
        primaries.Green.x = nclx->color_primary_green_x;
        primaries.Green.y = nclx->color_primary_green_y;
-       primaries.Green.Y = 1.0f;
+       primaries.Green.Y = 1.0F;
 
        primaries.Blue.x = nclx->color_primary_blue_x;
        primaries.Blue.y = nclx->color_primary_blue_y;
-       primaries.Blue.Y = 1.0f;
+       primaries.Blue.Y = 1.0F;
 
        switch (nclx->color_primaries)
                {
@@ -648,7 +665,7 @@ static guchar *nclx_to_lcms_profile(const struct heif_color_profile_nclx *nclx,
                        break;
                default:
                        log_printf("nclx unsupported color_primaries value: %d\n", nclx->color_primaries);
-                       return NULL;
+                       return nullptr;
                        break;
                }
 
@@ -657,25 +674,25 @@ static guchar *nclx_to_lcms_profile(const struct heif_color_profile_nclx *nclx,
        switch (nclx->transfer_characteristics)
                {
                case heif_transfer_characteristic_ITU_R_BT_709_5:
-                       curve[0] = curve[1] = curve[2] = cmsBuildParametricToneCurve(NULL, 4, rec709_parameters);
+                       curve[0] = curve[1] = curve[2] = cmsBuildParametricToneCurve(nullptr, 4, rec709_parameters);
                        profile = static_cast<cmsHPROFILE *>(cmsCreateRGBProfile(&whitepoint, &primaries, curve));
                        cmsFreeToneCurve(curve[0]);
                        trc_name = "Rec709 RGB";
                        break;
                case heif_transfer_characteristic_ITU_R_BT_470_6_System_M:
-                       curve[0] = curve[1] = curve[2] = cmsBuildGamma (NULL, 2.2f);
+                       curve[0] = curve[1] = curve[2] = cmsBuildGamma (nullptr, 2.2F);
                        profile = static_cast<cmsHPROFILE *>(cmsCreateRGBProfile(&whitepoint, &primaries, curve));
                        cmsFreeToneCurve(curve[0]);
                        trc_name = "Gamma2.2 RGB";
                        break;
                case heif_transfer_characteristic_ITU_R_BT_470_6_System_B_G:
-                       curve[0] = curve[1] = curve[2] = cmsBuildGamma (NULL, 2.8f);
+                       curve[0] = curve[1] = curve[2] = cmsBuildGamma (nullptr, 2.8F);
                        profile = static_cast<cmsHPROFILE *>(cmsCreateRGBProfile(&whitepoint, &primaries, curve));
                        cmsFreeToneCurve(curve[0]);
                        trc_name = "Gamma2.8 RGB";
                        break;
                case heif_transfer_characteristic_linear:
-                       curve[0] = curve[1] = curve[2] = cmsBuildGamma (NULL, 1.0f);
+                       curve[0] = curve[1] = curve[2] = cmsBuildGamma (nullptr, 1.0F);
                        profile = static_cast<cmsHPROFILE *>(cmsCreateRGBProfile(&whitepoint, &primaries, curve));
                        cmsFreeToneCurve(curve[0]);
                        trc_name = "linear RGB";
@@ -695,7 +712,7 @@ static guchar *nclx_to_lcms_profile(const struct heif_color_profile_nclx *nclx,
                case heif_transfer_characteristic_IEC_61966_2_1:
                /* same as default */
                default:
-                       curve[0] = curve[1] = curve[2] = cmsBuildParametricToneCurve(NULL, 4, srgb_parameters);
+                       curve[0] = curve[1] = curve[2] = cmsBuildParametricToneCurve(nullptr, 4, srgb_parameters);
                        profile = static_cast<cmsHPROFILE *>(cmsCreateRGBProfile(&whitepoint, &primaries, curve));
                        cmsFreeToneCurve(curve[0]);
                        trc_name = "sRGB-TRC RGB";
@@ -706,7 +723,7 @@ static guchar *nclx_to_lcms_profile(const struct heif_color_profile_nclx *nclx,
 
        if (profile)
                {
-               if (cmsSaveProfileToMem(profile, NULL, &size))
+               if (cmsSaveProfileToMem(profile, nullptr, &size))
                        {
                        data = static_cast<guint8 *>(g_malloc(size));
                        if (cmsSaveProfileToMem(profile, data, &size))
@@ -714,18 +731,14 @@ static guchar *nclx_to_lcms_profile(const struct heif_color_profile_nclx *nclx,
                                *profile_len = size;
                                }
                        cmsCloseProfile(profile);
-                       return (guchar *)data;
+                       return static_cast<guchar *>(data);
                        }
-               else
-                       {
-                       cmsCloseProfile(profile);
-                       return NULL;
-                       }
-               }
-       else
-               {
-               return NULL;
+
+               cmsCloseProfile(profile);
+               return nullptr;
                }
+
+       return nullptr;
 }
 
 guchar *heif_color_profile(FileData *fd, guint *profile_len)
@@ -737,16 +750,16 @@ guchar *heif_color_profile(FileData *fd, guint *profile_len)
        gint profile_type;
        guchar *profile;
        cmsUInt32Number size;
-       guint8 *data = NULL;
+       guint8 *data = nullptr;
 
        ctx = heif_context_alloc();
-       error_code = heif_context_read_from_file(ctx, fd->path, NULL);
+       error_code = heif_context_read_from_file(ctx, fd->path, nullptr);
 
        if (error_code.code)
                {
                log_printf("warning: heif reader error: %s\n", error_code.message);
                heif_context_free(ctx);
-               return NULL;
+               return nullptr;
                }
 
        error_code = heif_context_get_primary_image_handle(ctx, &handle);
@@ -754,7 +767,7 @@ guchar *heif_color_profile(FileData *fd, guint *profile_len)
                {
                log_printf("warning: heif reader error: %s\n", error_code.message);
                heif_context_free(ctx);
-               return NULL;
+               return nullptr;
                }
 
        nclxcp = heif_nclx_color_profile_alloc();
@@ -771,36 +784,34 @@ guchar *heif_color_profile(FileData *fd, guint *profile_len)
                        log_printf("warning: heif reader error: %s\n", error_code.message);
                        heif_context_free(ctx);
                        heif_nclx_color_profile_free(nclxcp);
-                       return NULL;
+                       return nullptr;
                        }
 
                DEBUG_1("heif color profile type: prof");
                heif_context_free(ctx);
                heif_nclx_color_profile_free(nclxcp);
 
-               return (guchar *)data;
+               return static_cast<guchar *>(data);
                }
-       else
-               {
-               error_code = heif_image_handle_get_nclx_color_profile(handle, &nclxcp);
-               if (error_code.code)
-                       {
-                       log_printf("warning: heif reader error: %s\n", error_code.message);
-                       heif_context_free(ctx);
-                       heif_nclx_color_profile_free(nclxcp);
-                       return NULL;
-                       }
 
-               profile = nclx_to_lcms_profile(nclxcp, profile_len);
+       error_code = heif_image_handle_get_nclx_color_profile(handle, &nclxcp);
+       if (error_code.code)
+               {
+               log_printf("warning: heif reader error: %s\n", error_code.message);
+               heif_context_free(ctx);
+               heif_nclx_color_profile_free(nclxcp);
+               return nullptr;
                }
 
+       profile = nclx_to_lcms_profile(nclxcp, profile_len);
+
        heif_context_free(ctx);
        heif_nclx_color_profile_free(nclxcp);
 
-       return (guchar *)profile;
+       return profile;
 }
 #else
-guchar *heif_color_profile(FileData *UNUSED(fd), guint *UNUSED(profile_len))
+guchar *heif_color_profile(FileData *, guint *)
 {
        return NULL;
 }
@@ -810,52 +821,47 @@ guchar *heif_color_profile(FileData *UNUSED(fd), guint *UNUSED(profile_len))
 /*** color support not enabled ***/
 
 
-ColorMan *color_man_new(ImageWindow *UNUSED(imd), GdkPixbuf *UNUSED(pixbuf),
-                       ColorManProfileType UNUSED(input_type), const gchar *UNUSED(input_file),
-                       ColorManProfileType UNUSED(screen_type), const gchar *UNUSED(screen_file),
-                       guchar *UNUSED(screen_data), guint UNUSED(screen_data_len))
-{
-       /* no op */
-       return NULL;
-}
-
-ColorMan *color_man_new_embedded(ImageWindow *UNUSED(imd), GdkPixbuf *UNUSED(pixbuf),
-                                guchar *UNUSED(input_data), guint UNUSED(input_data_len),
-                                ColorManProfileType UNUSED(screen_type), const gchar *UNUSED(screen_file),
-                                guchar *UNUSED(screen_data), guint UNUSED(screen_data_len))
+ColorMan *color_man_new(ImageWindow *, GdkPixbuf *,
+                       ColorManProfileType, const gchar *,
+                       ColorManProfileType, const gchar *,
+                       guchar *, guint)
 {
        /* no op */
-       return NULL;
+       return nullptr;
 }
 
-void color_man_free(ColorMan *UNUSED(cm))
+ColorMan *color_man_new_embedded(ImageWindow *, GdkPixbuf *,
+                                guchar *, guint,
+                                ColorManProfileType, const gchar *,
+                                guchar *, guint)
 {
        /* no op */
+       return nullptr;
 }
 
-void color_man_update(void)
+void color_man_free(ColorMan *)
 {
        /* no op */
 }
 
-void color_man_correct_region(ColorMan *UNUSED(cm), GdkPixbuf *UNUSED(pixbuf), gint UNUSED(x), gint UNUSED(y), gint UNUSED(w), gint UNUSED(h))
+void color_man_update()
 {
        /* no op */
 }
 
-void color_man_start_bg(ColorMan *UNUSED(cm), ColorManDoneFunc UNUSED(done_func), gpointer UNUSED(done_data))
+void color_man_correct_region(ColorMan *, GdkPixbuf *, gint, gint, gint, gint)
 {
        /* no op */
 }
 
-gboolean color_man_get_status(ColorMan *UNUSED(cm), gchar **UNUSED(image_profile), gchar **UNUSED(screen_profile))
+gboolean color_man_get_status(ColorMan *, gchar **, gchar **)
 {
        return FALSE;
 }
 
-guchar *heif_color_profile(FileData *UNUSED(fd), guint *UNUSED(profile_len))
+guchar *heif_color_profile(FileData *, guint *)
 {
-       return NULL;
+       return nullptr;
 }
 
 #endif /* define HAVE_LCMS */