Colour manage opaque, up to 8-bit images

This commit is contained in:
Přemysl Eric Janouch 2021-12-24 14:24:23 +01:00
parent 40c1f8327e
commit 7a4b5cd065
Signed by: p
GPG Key ID: A0420B94F92B9493
2 changed files with 84 additions and 36 deletions

View File

@ -5,7 +5,8 @@ fastiv
raw photos, HEIC, AVIF, WebP, SVG, X11 cursors and TIFF, or whatever gdk-pixbuf raw photos, HEIC, AVIF, WebP, SVG, X11 cursors and TIFF, or whatever gdk-pixbuf
loads. loads.
Its development status can be summarized as '`beta`'. Its development status can be summarized as '`beta`'. E.g., colour management
is partial, and certain GIFs animate wrong.
Non-goals Non-goals
--------- ---------
@ -25,8 +26,8 @@ Building and Running
Build dependencies: Meson, pkg-config + Build dependencies: Meson, pkg-config +
Runtime dependencies: gtk+-3.0, glib>=2.64, pixman-1, shared-mime-info, Runtime dependencies: gtk+-3.0, glib>=2.64, pixman-1, shared-mime-info,
spng>=0.7.0, libturbojpeg + spng>=0.7.0, libturbojpeg +
Optional dependencies: LibRaw, librsvg-2.0, xcursor, libwebp, libheif, libtiff, Optional dependencies: lcms2, LibRaw, librsvg-2.0, xcursor, libwebp, libheif,
gdk-pixbuf-2.0, ExifTool libtiff, gdk-pixbuf-2.0, ExifTool
$ git clone --recursive https://git.janouch.name/p/fastiv.git $ git clone --recursive https://git.janouch.name/p/fastiv.git
$ meson builddir $ meson builddir

113
fiv-io.c
View File

@ -241,19 +241,19 @@ trivial_cmyk_to_host_byte_order_argb(unsigned char *p, int len)
static void static void
fiv_io_profile_cmyk( fiv_io_profile_cmyk(
cairo_surface_t *surface, FivIoProfile src, FivIoProfile dst) cairo_surface_t *surface, FivIoProfile source, FivIoProfile target)
{ {
unsigned char *data = cairo_image_surface_get_data(surface); unsigned char *data = cairo_image_surface_get_data(surface);
int w = cairo_image_surface_get_width(surface); int w = cairo_image_surface_get_width(surface);
int h = cairo_image_surface_get_height(surface); int h = cairo_image_surface_get_height(surface);
#ifndef HAVE_LCMS2 #ifndef HAVE_LCMS2
(void) src; (void) source;
(void) dst; (void) target;
#else #else
cmsHTRANSFORM transform = NULL; cmsHTRANSFORM transform = NULL;
if (src && dst) { if (source && target) {
transform = cmsCreateTransform(src, TYPE_CMYK_8_REV, dst, transform = cmsCreateTransform(source, TYPE_CMYK_8_REV, target,
FIV_IO_LCMS2_ARGB, INTENT_PERCEPTUAL, 0); FIV_IO_LCMS2_ARGB, INTENT_PERCEPTUAL, 0);
} }
if (transform) { if (transform) {
@ -267,12 +267,12 @@ fiv_io_profile_cmyk(
static void static void
fiv_io_profile_xrgb( fiv_io_profile_xrgb(
cairo_surface_t *surface, FivIoProfile src, FivIoProfile dst) cairo_surface_t *surface, FivIoProfile source, FivIoProfile target)
{ {
#ifndef HAVE_LCMS2 #ifndef HAVE_LCMS2
(void) surface; (void) surface;
(void) src; (void) source;
(void) dst; (void) target;
#else #else
unsigned char *data = cairo_image_surface_get_data(surface); unsigned char *data = cairo_image_surface_get_data(surface);
int w = cairo_image_surface_get_width(surface); int w = cairo_image_surface_get_width(surface);
@ -280,12 +280,12 @@ fiv_io_profile_xrgb(
// TODO(p): We should make this optional. // TODO(p): We should make this optional.
cmsHPROFILE src_fallback = NULL; cmsHPROFILE src_fallback = NULL;
if (dst && !src) if (target && !source)
src = src_fallback = cmsCreate_sRGBProfile(); source = src_fallback = cmsCreate_sRGBProfile();
cmsHTRANSFORM transform = NULL; cmsHTRANSFORM transform = NULL;
if (src && dst) { if (source && target) {
transform = cmsCreateTransform(src, FIV_IO_LCMS2_ARGB, dst, transform = cmsCreateTransform(source, FIV_IO_LCMS2_ARGB, target,
FIV_IO_LCMS2_ARGB, INTENT_PERCEPTUAL, 0); FIV_IO_LCMS2_ARGB, INTENT_PERCEPTUAL, 0);
} }
if (transform) { if (transform) {
@ -297,6 +297,47 @@ fiv_io_profile_xrgb(
#endif #endif
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
fiv_io_profile_finalize_page(cairo_surface_t *page, FivIoProfile target)
{
GBytes *bytes = NULL;
gsize len = 0;
gconstpointer p = NULL;
FivIoProfile source = NULL;
if ((bytes = cairo_surface_get_user_data(page, &fiv_io_key_icc)) &&
(p = g_bytes_get_data(bytes, &len)))
source = fiv_io_profile_new(p, len);
// TODO(p): Animations need to be composited in a linear colour space.
for (cairo_surface_t *frame = page; frame != NULL;
frame = cairo_surface_get_user_data(frame, &fiv_io_key_frame_next))
fiv_io_profile_xrgb(frame, source, target);
if (source)
fiv_io_profile_free(source);
}
// TODO(p): Offer better integration, upgrade the bit depth if appropriate.
static cairo_surface_t *
fiv_io_profile_finalize(cairo_surface_t *image, FivIoProfile target)
{
if (!image || !target)
return image;
for (cairo_surface_t *page = image; page != NULL;
page = cairo_surface_get_user_data(page, &fiv_io_key_page_next)) {
// TODO(p): 1. un/premultiply ARGB, 2. do colour management
// early enough, so that no avoidable increase of quantization error
// occurs beforehands, and also for correct alpha compositing.
// FIXME: This assumes that if the first frame is opaque, they all are.
if (cairo_image_surface_get_format(page) == CAIRO_FORMAT_RGB24)
fiv_io_profile_finalize_page(page, target);
}
return image;
}
// --- Wuffs ------------------------------------------------------------------- // --- Wuffs -------------------------------------------------------------------
// From libwebp, verified to exactly match [x * a / 255]. // From libwebp, verified to exactly match [x * a / 255].
@ -738,7 +779,7 @@ fail:
static cairo_surface_t * static cairo_surface_t *
open_wuffs_using(wuffs_base__image_decoder *(*allocate)(), open_wuffs_using(wuffs_base__image_decoder *(*allocate)(),
const gchar *data, gsize len, GError **error) const gchar *data, gsize len, FivIoProfile profile, GError **error)
{ {
wuffs_base__image_decoder *dec = allocate(); wuffs_base__image_decoder *dec = allocate();
if (!dec) { if (!dec) {
@ -749,7 +790,7 @@ open_wuffs_using(wuffs_base__image_decoder *(*allocate)(),
cairo_surface_t *surface = open_wuffs( cairo_surface_t *surface = open_wuffs(
dec, wuffs_base__ptr_u8__reader((uint8_t *) data, len, TRUE), error); dec, wuffs_base__ptr_u8__reader((uint8_t *) data, len, TRUE), error);
free(dec); free(dec);
return surface; return fiv_io_profile_finalize(surface, profile);
} }
// --- JPEG -------------------------------------------------------------------- // --- JPEG --------------------------------------------------------------------
@ -886,7 +927,8 @@ open_libjpeg_turbo(
return NULL; return NULL;
} }
int pixel_format = (colorspace == TJCS_CMYK || colorspace == TJCS_YCCK) bool use_cmyk = colorspace == TJCS_CMYK || colorspace == TJCS_YCCK;
int pixel_format = use_cmyk
? TJPF_CMYK ? TJPF_CMYK
: (G_BYTE_ORDER == G_LITTLE_ENDIAN ? TJPF_BGRX : TJPF_XRGB); : (G_BYTE_ORDER == G_LITTLE_ENDIAN ? TJPF_BGRX : TJPF_XRGB);
@ -917,8 +959,7 @@ open_libjpeg_turbo(
} }
} }
load_jpeg_finalize( load_jpeg_finalize(surface, use_cmyk, profile, data, len);
surface, (pixel_format == TJPF_CMYK), profile, data, len);
tjDestroy(dec); tjDestroy(dec);
return surface; return surface;
} }
@ -961,8 +1002,10 @@ open_libjpeg_enhanced(
jpeg_create_decompress(&cinfo); jpeg_create_decompress(&cinfo);
jpeg_mem_src(&cinfo, (const unsigned char *) data, len); jpeg_mem_src(&cinfo, (const unsigned char *) data, len);
(void) jpeg_read_header(&cinfo, true); (void) jpeg_read_header(&cinfo, true);
if (cinfo.jpeg_color_space == JCS_CMYK ||
cinfo.jpeg_color_space == JCS_YCCK) bool use_cmyk = cinfo.jpeg_color_space == JCS_CMYK ||
cinfo.jpeg_color_space == JCS_YCCK;
if (use_cmyk)
cinfo.out_color_space = JCS_CMYK; cinfo.out_color_space = JCS_CMYK;
else if (G_BYTE_ORDER == G_BIG_ENDIAN) else if (G_BYTE_ORDER == G_BIG_ENDIAN)
cinfo.out_color_space = JCS_EXT_XRGB; cinfo.out_color_space = JCS_EXT_XRGB;
@ -1003,8 +1046,7 @@ open_libjpeg_enhanced(
surface_data, cinfo.output_width * cinfo.output_height); surface_data, cinfo.output_width * cinfo.output_height);
(void) jpegqs_finish_decompress(&cinfo); (void) jpegqs_finish_decompress(&cinfo);
load_jpeg_finalize( load_jpeg_finalize(surface, use_cmyk, profile, data, len);
surface, (cinfo.out_color_space == JCS_CMYK), profile, data, len);
jpeg_destroy_decompress(&cinfo); jpeg_destroy_decompress(&cinfo);
return surface; return surface;
} }
@ -1353,6 +1395,8 @@ open_xcursor(const gchar *data, gsize len, GError **error)
static cairo_user_data_key_t key = {}; static cairo_user_data_key_t key = {};
cairo_surface_set_user_data( cairo_surface_set_user_data(
pages, &key, images, (cairo_destroy_func_t) XcursorImagesDestroy); pages, &key, images, (cairo_destroy_func_t) XcursorImagesDestroy);
// Do not bother doing colour correction, there is no correct rendering.
return pages; return pages;
} }
@ -1492,7 +1536,8 @@ fail:
} }
static cairo_surface_t * static cairo_surface_t *
open_libwebp(const gchar *data, gsize len, const gchar *path, GError **error) open_libwebp(const gchar *data, gsize len, const gchar *path,
FivIoProfile profile, GError **error)
{ {
// It is wholly zero-initialized by libwebp. // It is wholly zero-initialized by libwebp.
WebPDecoderConfig config = {}; WebPDecoderConfig config = {};
@ -1556,7 +1601,7 @@ open_libwebp(const gchar *data, gsize len, const gchar *path, GError **error)
fail: fail:
WebPFreeDecBuffer(&config.output); WebPFreeDecBuffer(&config.output);
return result; return fiv_io_profile_finalize(result, profile);
} }
#endif // HAVE_LIBWEBP -------------------------------------------------------- #endif // HAVE_LIBWEBP --------------------------------------------------------
@ -1703,7 +1748,8 @@ load_libheif_aux_images(const gchar *path, struct heif_image_handle *top,
} }
static cairo_surface_t * static cairo_surface_t *
open_libheif(const gchar *data, gsize len, const gchar *path, GError **error) open_libheif(const gchar *data, gsize len, const gchar *path,
FivIoProfile profile, GError **error)
{ {
// libheif will throw C++ exceptions on allocation failures. // libheif will throw C++ exceptions on allocation failures.
// The library is generally awful through and through. // The library is generally awful through and through.
@ -1747,7 +1793,7 @@ open_libheif(const gchar *data, gsize len, const gchar *path, GError **error)
g_free(ids); g_free(ids);
fail_read: fail_read:
heif_context_free(ctx); heif_context_free(ctx);
return result; return fiv_io_profile_finalize(result, profile);
} }
#endif // HAVE_LIBHEIF -------------------------------------------------------- #endif // HAVE_LIBHEIF --------------------------------------------------------
@ -2003,7 +2049,8 @@ fail:
#ifdef HAVE_GDKPIXBUF // ------------------------------------------------------ #ifdef HAVE_GDKPIXBUF // ------------------------------------------------------
static cairo_surface_t * static cairo_surface_t *
open_gdkpixbuf(const gchar *data, gsize len, GError **error) open_gdkpixbuf(
const gchar *data, gsize len, FivIoProfile profile, GError **error)
{ {
// gdk-pixbuf controls the playback itself, there is no reliable method of // gdk-pixbuf controls the playback itself, there is no reliable method of
// extracting individual frames (due to loops). // extracting individual frames (due to loops).
@ -2036,7 +2083,7 @@ open_gdkpixbuf(const gchar *data, gsize len, GError **error)
} }
g_object_unref(pixbuf); g_object_unref(pixbuf);
return surface; return fiv_io_profile_finalize(surface, profile);
} }
#endif // HAVE_GDKPIXBUF ------------------------------------------------------ #endif // HAVE_GDKPIXBUF ------------------------------------------------------
@ -2097,17 +2144,17 @@ fiv_io_open_from_data(const char *data, size_t len, const gchar *path,
// which is so far unsupported here. // which is so far unsupported here.
surface = open_wuffs_using( surface = open_wuffs_using(
wuffs_bmp__decoder__alloc_as__wuffs_base__image_decoder, data, len, wuffs_bmp__decoder__alloc_as__wuffs_base__image_decoder, data, len,
error); profile, error);
break; break;
case WUFFS_BASE__FOURCC__GIF: case WUFFS_BASE__FOURCC__GIF:
surface = open_wuffs_using( surface = open_wuffs_using(
wuffs_gif__decoder__alloc_as__wuffs_base__image_decoder, data, len, wuffs_gif__decoder__alloc_as__wuffs_base__image_decoder, data, len,
error); profile, error);
break; break;
case WUFFS_BASE__FOURCC__PNG: case WUFFS_BASE__FOURCC__PNG:
surface = open_wuffs_using( surface = open_wuffs_using(
wuffs_png__decoder__alloc_as__wuffs_base__image_decoder, data, len, wuffs_png__decoder__alloc_as__wuffs_base__image_decoder, data, len,
error); profile, error);
break; break;
case WUFFS_BASE__FOURCC__JPEG: case WUFFS_BASE__FOURCC__JPEG:
surface = enhance surface = enhance
@ -2146,7 +2193,7 @@ fiv_io_open_from_data(const char *data, size_t len, const gchar *path,
#endif // HAVE_XCURSOR -------------------------------------------------------- #endif // HAVE_XCURSOR --------------------------------------------------------
#ifdef HAVE_LIBWEBP //--------------------------------------------------------- #ifdef HAVE_LIBWEBP //---------------------------------------------------------
// TODO(p): https://github.com/google/wuffs/commit/4c04ac1 // TODO(p): https://github.com/google/wuffs/commit/4c04ac1
if ((surface = open_libwebp(data, len, path, error))) if ((surface = open_libwebp(data, len, path, profile, error)))
break; break;
if (error) { if (error) {
g_debug("%s", (*error)->message); g_debug("%s", (*error)->message);
@ -2154,7 +2201,7 @@ fiv_io_open_from_data(const char *data, size_t len, const gchar *path,
} }
#endif // HAVE_LIBWEBP -------------------------------------------------------- #endif // HAVE_LIBWEBP --------------------------------------------------------
#ifdef HAVE_LIBHEIF //--------------------------------------------------------- #ifdef HAVE_LIBHEIF //---------------------------------------------------------
if ((surface = open_libheif(data, len, path, error))) if ((surface = open_libheif(data, len, path, profile, error)))
break; break;
if (error) { if (error) {
g_debug("%s", (*error)->message); g_debug("%s", (*error)->message);
@ -2172,7 +2219,7 @@ fiv_io_open_from_data(const char *data, size_t len, const gchar *path,
#endif // HAVE_LIBTIFF -------------------------------------------------------- #endif // HAVE_LIBTIFF --------------------------------------------------------
#ifdef HAVE_GDKPIXBUF // ------------------------------------------------------ #ifdef HAVE_GDKPIXBUF // ------------------------------------------------------
// This is only used as a last resort, the rest above is special-cased. // This is only used as a last resort, the rest above is special-cased.
if ((surface = open_gdkpixbuf(data, len, error))) if ((surface = open_gdkpixbuf(data, len, profile, error)))
break; break;
if (error && (*error)->code != GDK_PIXBUF_ERROR_UNKNOWN_TYPE) if (error && (*error)->code != GDK_PIXBUF_ERROR_UNKNOWN_TYPE)
break; break;