From 6b4759b63af5d5724c40be516d886b6f9fb0026d Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Sun, 28 Jan 2024 00:28:11 +0100 Subject: WIP: Thread-safe colour management --- fiv-io-cmm.c | 128 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 64 insertions(+), 64 deletions(-) (limited to 'fiv-io-cmm.c') diff --git a/fiv-io-cmm.c b/fiv-io-cmm.c index 1336a22..f2d38f9 100644 --- a/fiv-io-cmm.c +++ b/fiv-io-cmm.c @@ -32,18 +32,16 @@ #endif // HAVE_LCMS2_FAST_FLOAT // --- Profiles ---------------------------------------------------------------- +#ifdef HAVE_LCMS2 struct _FivIoProfile { FivIoCmm *cmm; -#ifdef HAVE_LCMS2 cmsHPROFILE profile; -#endif }; GBytes * fiv_io_profile_to_bytes(FivIoProfile *profile) { -#ifdef HAVE_LCMS2 cmsUInt32Number len = 0; (void) cmsSaveProfileToMem(profile, NULL, &len); gchar *data = g_malloc0(len); @@ -52,14 +50,8 @@ fiv_io_profile_to_bytes(FivIoProfile *profile) return NULL; } return g_bytes_new_take(data, len); -#else - (void) profile; - return NULL; -#endif } -#ifdef HAVE_LCMS2 - static FivIoProfile * fiv_io_profile_new(FivIoCmm *cmm, cmsHPROFILE profile) { @@ -69,28 +61,29 @@ fiv_io_profile_new(FivIoCmm *cmm, cmsHPROFILE profile) return self; } -#endif - void fiv_io_profile_free(FivIoProfile *self) { -#ifdef HAVE_LCMS2 cmsCloseProfile(self->profile); -#endif g_clear_object(&self->cmm); g_free(self); } +#else // ! HAVE_LCMS2 + +GBytes *fiv_io_profile_to_bytes(FivIoProfile *) { return NULL; } +void fiv_io_profile_free(FivIoProfile *) {} + +#endif // ! HAVE_LCMS2 // --- Contexts ---------------------------------------------------------------- +#ifdef HAVE_LCMS2 struct _FivIoCmm { GObject parent_instance; -#ifdef HAVE_LCMS2 cmsContext context; // https://github.com/mm2/Little-CMS/issues/430 gboolean broken_premul; -#endif }; G_DEFINE_TYPE(FivIoCmm, fiv_io_cmm, G_TYPE_OBJECT) @@ -98,10 +91,8 @@ G_DEFINE_TYPE(FivIoCmm, fiv_io_cmm, G_TYPE_OBJECT) static void fiv_io_cmm_finalize(GObject *gobject) { -#ifdef HAVE_LCMS2 FivIoCmm *self = FIV_IO_CMM(gobject); cmsDeleteContext(self->context); -#endif G_OBJECT_CLASS(fiv_io_cmm_parent_class)->finalize(gobject); } @@ -118,9 +109,7 @@ fiv_io_cmm_init(FivIoCmm *self) { (void) self; -#ifdef HAVE_LCMS2 self->context = cmsCreateContext(NULL, self); -#endif #ifdef HAVE_LCMS2_FAST_FLOAT if (cmsPluginTHR(self->context, cmsFastFloatExtensions())) self->broken_premul = LCMS_VERSION <= 2160; @@ -148,14 +137,8 @@ fiv_io_cmm_get_profile(FivIoCmm *self, const void *data, size_t len) if (!self) self = fiv_io_cmm_get_default(); -#ifdef HAVE_LCMS2 return fiv_io_profile_new(self, cmsOpenProfileFromMemTHR(self->context, data, len)); -#else - (void) data; - (void) len; - return NULL; -#endif } FivIoProfile * @@ -164,12 +147,8 @@ fiv_io_cmm_get_profile_sRGB(FivIoCmm *self) if (!self) self = fiv_io_cmm_get_default(); -#ifdef HAVE_LCMS2 return fiv_io_profile_new(self, cmsCreate_sRGBProfileTHR(self->context)); -#else - return NULL; -#endif } FivIoProfile * @@ -179,7 +158,6 @@ fiv_io_cmm_get_profile_parametric(FivIoCmm *self, if (!self) self = fiv_io_cmm_get_default(); -#ifdef HAVE_LCMS2 const cmsCIExyY cmsWP = {whitepoint[0], whitepoint[1], 1.0}; const cmsCIExyYTRIPLE cmsP = { {primaries[0], primaries[1], 1.0}, @@ -195,14 +173,36 @@ fiv_io_cmm_get_profile_parametric(FivIoCmm *self, &cmsWP, &cmsP, (cmsToneCurve *[3]){curve, curve, curve}); cmsFreeToneCurve(curve); return fiv_io_profile_new(self, profile); -#else - (void) gamma; - (void) whitepoint; - (void) primaries; +} + +#else // ! HAVE_LCMS2 + +FivIoCmm * +fiv_io_cmm_get_default() +{ + return NULL; +} + +FivIoProfile * +fiv_io_cmm_get_profile(FivIoCmm *, const void *, size_t) +{ + return NULL; +} + +FivIoProfile * +fiv_io_cmm_get_profile_sRGB(FivIoCmm *) +{ + return NULL; +} + +FivIoProfile * +fiv_io_cmm_get_profile_parametric(FivIoCmm *, double, double[2], double[6]) +{ return NULL; -#endif } +#endif // ! HAVE_LCMS2 + FivIoProfile * fiv_io_cmm_get_profile_sRGB_gamma(FivIoCmm *self, double gamma) { @@ -221,17 +221,6 @@ fiv_io_cmm_get_profile_from_bytes(FivIoCmm *self, GBytes *bytes) // --- Image loading ----------------------------------------------------------- -// TODO(p): In general, try to use CAIRO_FORMAT_RGB30 or CAIRO_FORMAT_RGBA128F. -#ifndef HAVE_LCMS2 -#define FIV_IO_PROFILE_ARGB32 0 -#define FIV_IO_PROFILE_4X16LE 0 -#else -#define FIV_IO_PROFILE_ARGB32 \ - (G_BYTE_ORDER == G_LITTLE_ENDIAN ? TYPE_BGRA_8 : TYPE_ARGB_8) -#define FIV_IO_PROFILE_4X16LE \ - (G_BYTE_ORDER == G_LITTLE_ENDIAN ? TYPE_BGRA_16 : TYPE_BGRA_16_SE) -#endif - // CAIRO_STRIDE_ALIGNMENT is 4 bytes, so there will be no padding with // ARGB/BGRA/XRGB/BGRX. static void @@ -258,6 +247,14 @@ trivial_cmyk_to_host_byte_order_argb(unsigned char *p, int len) } } +#ifdef HAVE_LCMS2 + +// TODO(p): In general, try to use CAIRO_FORMAT_RGB30 or CAIRO_FORMAT_RGBA128F. +#define FIV_IO_PROFILE_ARGB32 \ + (G_BYTE_ORDER == G_LITTLE_ENDIAN ? TYPE_BGRA_8 : TYPE_ARGB_8) +#define FIV_IO_PROFILE_4X16LE \ + (G_BYTE_ORDER == G_LITTLE_ENDIAN ? TYPE_BGRA_16 : TYPE_BGRA_16_SE) + void fiv_io_cmm_cmyk(FivIoCmm *self, FivIoImage *image, FivIoProfile *source, FivIoProfile *target) @@ -265,11 +262,6 @@ fiv_io_cmm_cmyk(FivIoCmm *self, if (!self) self = fiv_io_cmm_get_default(); -#ifndef HAVE_LCMS2 - (void) self; - (void) source; - (void) target; -#else cmsHTRANSFORM transform = NULL; if (source && target) { transform = cmsCreateTransformTHR(self->context, @@ -282,7 +274,6 @@ fiv_io_cmm_cmyk(FivIoCmm *self, cmsDeleteTransform(transform); return; } -#endif trivial_cmyk_to_host_byte_order_argb( image->data, image->width * image->height); } @@ -295,17 +286,6 @@ fiv_io_cmm_rgb_direct(FivIoCmm *self, unsigned char *data, int w, int h, if (!self) self = fiv_io_cmm_get_default(); -#ifndef HAVE_LCMS2 - (void) self; - (void) data; - (void) w; - (void) h; - (void) source; - (void) source_format; - (void) target; - (void) target_format; - return false; -#else // TODO(p): We should make this optional. FivIoProfile *src_fallback = NULL; if (target && !source) @@ -324,7 +304,6 @@ fiv_io_cmm_rgb_direct(FivIoCmm *self, unsigned char *data, int w, int h, if (src_fallback) fiv_io_profile_free(src_fallback); return transform != NULL; -#endif } static void @@ -343,6 +322,27 @@ fiv_io_cmm_4x16le_direct(FivIoCmm *self, unsigned char *data, FIV_IO_PROFILE_4X16LE, FIV_IO_PROFILE_4X16LE); } +#else // ! HAVE_LCMS2 + +void +fiv_io_cmm_cmyk(FivIoCmm *, FivIoImage *image, FivIoProfile *, FivIoProfile *) +{ + trivial_cmyk_to_host_byte_order_argb( + image->data, image->width * image->height); +} + +static void +fiv_io_cmm_xrgb32(FivIoCmm *, FivIoImage *, FivIoProfile *, FivIoProfile *) +{ +} + +void +fiv_io_cmm_4x16le_direct( + FivIoCmm *, unsigned char *, int, int, FivIoProfile *, FivIoProfile *) +{ +} + +#endif // ! HAVE_LCMS2 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void -- cgit v1.2.3-70-g09d2