Compare commits

...

3 Commits

3 changed files with 131 additions and 80 deletions

View File

@ -342,23 +342,22 @@ static void
monitor_apply(enum monitor_event event, GPtrArray *target, int index, monitor_apply(enum monitor_event event, GPtrArray *target, int index,
FivIoModelEntry *new_entry) FivIoModelEntry *new_entry)
{ {
// The file used to be filtered out but isn't anymore.
// TODO(p): Handle the inverse transition as well.
if (event == MONITOR_RENAMING && index < 0) if (event == MONITOR_RENAMING && index < 0)
// The file used to be filtered out but isn't anymore.
event = MONITOR_ADDING; event = MONITOR_ADDING;
else if (!new_entry && index >= 0)
// The file wasn't filtered out but now it is.
event = MONITOR_REMOVING;
if (event == MONITOR_CHANGING) { if (event == MONITOR_CHANGING) {
g_assert(new_entry != NULL);
fiv_io_model_entry_unref(target->pdata[index]); fiv_io_model_entry_unref(target->pdata[index]);
target->pdata[index] = fiv_io_model_entry_ref(new_entry); target->pdata[index] = fiv_io_model_entry_ref(new_entry);
} }
if (event == MONITOR_REMOVING || event == MONITOR_RENAMING) if (event == MONITOR_REMOVING || event == MONITOR_RENAMING)
g_ptr_array_remove_index(target, index); g_ptr_array_remove_index(target, index);
if (event == MONITOR_RENAMING || event == MONITOR_ADDING) { if (event == MONITOR_RENAMING || event == MONITOR_ADDING)
g_assert(new_entry != NULL);
g_ptr_array_add(target, fiv_io_model_entry_ref(new_entry)); g_ptr_array_add(target, fiv_io_model_entry_ref(new_entry));
} }
}
static void static void
on_monitor_changed(G_GNUC_UNUSED GFileMonitor *monitor, GFile *file, on_monitor_changed(G_GNUC_UNUSED GFileMonitor *monitor, GFile *file,
@ -370,25 +369,25 @@ on_monitor_changed(G_GNUC_UNUSED GFileMonitor *monitor, GFile *file,
gint files_index = model_find(self->files, file, &old_entry); gint files_index = model_find(self->files, file, &old_entry);
gint subdirs_index = model_find(self->subdirs, file, &old_entry); gint subdirs_index = model_find(self->subdirs, file, &old_entry);
enum monitor_event action = MONITOR_NONE; enum monitor_event event = MONITOR_NONE;
GFile *new_entry_file = NULL; GFile *new_entry_file = NULL;
switch (event_type) { switch (event_type) {
case G_FILE_MONITOR_EVENT_CHANGED: case G_FILE_MONITOR_EVENT_CHANGED:
case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED: case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
action = MONITOR_CHANGING; event = MONITOR_CHANGING;
new_entry_file = file; new_entry_file = file;
break; break;
case G_FILE_MONITOR_EVENT_RENAMED: case G_FILE_MONITOR_EVENT_RENAMED:
action = MONITOR_RENAMING; event = MONITOR_RENAMING;
new_entry_file = other_file; new_entry_file = other_file;
break; break;
case G_FILE_MONITOR_EVENT_DELETED: case G_FILE_MONITOR_EVENT_DELETED:
case G_FILE_MONITOR_EVENT_MOVED_OUT: case G_FILE_MONITOR_EVENT_MOVED_OUT:
action = MONITOR_REMOVING; event = MONITOR_REMOVING;
break; break;
case G_FILE_MONITOR_EVENT_CREATED: case G_FILE_MONITOR_EVENT_CREATED:
case G_FILE_MONITOR_EVENT_MOVED_IN: case G_FILE_MONITOR_EVENT_MOVED_IN:
action = MONITOR_ADDING; event = MONITOR_ADDING;
old_entry = NULL; old_entry = NULL;
new_entry_file = file; new_entry_file = file;
break; break;
@ -432,12 +431,12 @@ run:
fiv_io_model_entry_ref(old_entry); fiv_io_model_entry_ref(old_entry);
if (files_index != -1 || new_target == self->files) { if (files_index != -1 || new_target == self->files) {
monitor_apply(action, self->files, files_index, new_entry); monitor_apply(event, self->files, files_index, new_entry);
g_signal_emit(self, model_signals[FILES_CHANGED], g_signal_emit(self, model_signals[FILES_CHANGED],
0, old_entry, new_entry); 0, old_entry, new_entry);
} }
if (subdirs_index != -1 || new_target == self->subdirs) { if (subdirs_index != -1 || new_target == self->subdirs) {
monitor_apply(action, self->subdirs, subdirs_index, new_entry); monitor_apply(event, self->subdirs, subdirs_index, new_entry);
g_signal_emit(self, model_signals[SUBDIRECTORIES_CHANGED], g_signal_emit(self, model_signals[SUBDIRECTORIES_CHANGED],
0, old_entry, new_entry); 0, old_entry, new_entry);
} }

182
fiv-io.c
View File

@ -286,10 +286,15 @@ fiv_io_profile_free(FivIoProfile self)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// TODO(p): In general, try to use CAIRO_FORMAT_RGB30 or CAIRO_FORMAT_RGBA128F. // TODO(p): In general, try to use CAIRO_FORMAT_RGB30 or CAIRO_FORMAT_RGBA128F.
#define FIV_IO_LCMS2_ARGB32 \ #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) (G_BYTE_ORDER == G_LITTLE_ENDIAN ? TYPE_BGRA_8 : TYPE_ARGB_8)
#define FIV_IO_LCMS2_4X16LE \ #define FIV_IO_PROFILE_4X16LE \
(G_BYTE_ORDER == G_LITTLE_ENDIAN ? TYPE_BGRA_16 : TYPE_BGRA_16_SE) (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 // CAIRO_STRIDE_ALIGNMENT is 4 bytes, so there will be no padding with
// ARGB/BGRA/XRGB/BGRX. // ARGB/BGRA/XRGB/BGRX.
@ -332,7 +337,7 @@ fiv_io_profile_cmyk(
cmsHTRANSFORM transform = NULL; cmsHTRANSFORM transform = NULL;
if (source && target) { if (source && target) {
transform = cmsCreateTransform(source, TYPE_CMYK_8_REV, target, transform = cmsCreateTransform(source, TYPE_CMYK_8_REV, target,
FIV_IO_LCMS2_ARGB32, INTENT_PERCEPTUAL, 0); FIV_IO_PROFILE_ARGB32, INTENT_PERCEPTUAL, 0);
} }
if (transform) { if (transform) {
cmsDoTransform(transform, data, data, w * h); cmsDoTransform(transform, data, data, w * h);
@ -343,16 +348,20 @@ fiv_io_profile_cmyk(
trivial_cmyk_to_host_byte_order_argb(data, w * h); trivial_cmyk_to_host_byte_order_argb(data, w * h);
} }
static void static bool
fiv_io_profile_xrgb32_direct(unsigned char *data, int w, int h, fiv_io_profile_rgb_direct(unsigned char *data, int w, int h,
FivIoProfile source, FivIoProfile target) FivIoProfile source, FivIoProfile target,
uint32_t source_format, uint32_t target_format)
{ {
#ifndef HAVE_LCMS2 #ifndef HAVE_LCMS2
(void) data; (void) data;
(void) w; (void) w;
(void) h; (void) h;
(void) source; (void) source;
(void) source_format;
(void) target; (void) target;
(void) target_format;
return false;
#else #else
// TODO(p): We should make this optional. // TODO(p): We should make this optional.
cmsHPROFILE src_fallback = NULL; cmsHPROFILE src_fallback = NULL;
@ -361,8 +370,8 @@ fiv_io_profile_xrgb32_direct(unsigned char *data, int w, int h,
cmsHTRANSFORM transform = NULL; cmsHTRANSFORM transform = NULL;
if (source && target) { if (source && target) {
transform = cmsCreateTransform(source, FIV_IO_LCMS2_ARGB32, target, transform = cmsCreateTransform(
FIV_IO_LCMS2_ARGB32, INTENT_PERCEPTUAL, 0); source, source_format, target, target_format, INTENT_PERCEPTUAL, 0);
} }
if (transform) { if (transform) {
cmsDoTransform(transform, data, data, w * h); cmsDoTransform(transform, data, data, w * h);
@ -370,9 +379,18 @@ fiv_io_profile_xrgb32_direct(unsigned char *data, int w, int h,
} }
if (src_fallback) if (src_fallback)
cmsCloseProfile(src_fallback); cmsCloseProfile(src_fallback);
return transform != NULL;
#endif #endif
} }
static void
fiv_io_profile_xrgb32_direct(
unsigned char *data, int w, int h, FivIoProfile source, FivIoProfile target)
{
fiv_io_profile_rgb_direct(data, w, h, source, target,
FIV_IO_PROFILE_ARGB32, FIV_IO_PROFILE_ARGB32);
}
static void static void
fiv_io_profile_xrgb32( fiv_io_profile_xrgb32(
cairo_surface_t *surface, FivIoProfile source, FivIoProfile target) cairo_surface_t *surface, FivIoProfile source, FivIoProfile target)
@ -387,36 +405,15 @@ static void
fiv_io_profile_4x16le_direct( fiv_io_profile_4x16le_direct(
unsigned char *data, int w, int h, FivIoProfile source, FivIoProfile target) unsigned char *data, int w, int h, FivIoProfile source, FivIoProfile target)
{ {
#ifndef HAVE_LCMS2 fiv_io_profile_rgb_direct(data, w, h, source, target,
(void) data; FIV_IO_PROFILE_4X16LE, FIV_IO_PROFILE_4X16LE);
(void) w;
(void) h;
(void) source;
(void) target;
#else
// TODO(p): We should make this optional.
cmsHPROFILE src_fallback = NULL;
if (target && !source)
source = src_fallback = cmsCreate_sRGBProfile();
cmsHTRANSFORM transform = NULL;
if (source && target) {
transform = cmsCreateTransform(source, FIV_IO_LCMS2_4X16LE, target,
FIV_IO_LCMS2_4X16LE, INTENT_PERCEPTUAL, 0);
}
if (transform) {
cmsDoTransform(transform, data, data, w * h);
cmsDeleteTransform(transform);
}
if (src_fallback)
cmsCloseProfile(src_fallback);
#endif
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void static void
fiv_io_profile_xrgb32_page(cairo_surface_t *page, FivIoProfile target) fiv_io_profile_page(cairo_surface_t *page, FivIoProfile target,
void (*frame_cb) (cairo_surface_t *, FivIoProfile, FivIoProfile))
{ {
GBytes *bytes = NULL; GBytes *bytes = NULL;
FivIoProfile source = NULL; FivIoProfile source = NULL;
@ -426,31 +423,12 @@ fiv_io_profile_xrgb32_page(cairo_surface_t *page, FivIoProfile target)
// TODO(p): All animations need to be composited in a linear colour space. // TODO(p): All animations need to be composited in a linear colour space.
for (cairo_surface_t *frame = page; frame != NULL; for (cairo_surface_t *frame = page; frame != NULL;
frame = cairo_surface_get_user_data(frame, &fiv_io_key_frame_next)) frame = cairo_surface_get_user_data(frame, &fiv_io_key_frame_next))
fiv_io_profile_xrgb32(frame, source, target); frame_cb(frame, source, target);
if (source) if (source)
fiv_io_profile_free(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_xrgb32_page(page, target);
}
return image;
}
static void static void
fiv_io_premultiply_argb32(cairo_surface_t *surface) fiv_io_premultiply_argb32(cairo_surface_t *surface)
{ {
@ -473,14 +451,94 @@ fiv_io_premultiply_argb32(cairo_surface_t *surface)
} }
} }
#if defined HAVE_LCMS2 && LCMS_VERSION >= 2130
#define FIV_IO_PROFILE_ARGB32_PREMUL \
(G_BYTE_ORDER == G_LITTLE_ENDIAN ? TYPE_BGRA_8_PREMUL : TYPE_ARGB_8_PREMUL)
static void static void
fiv_io_premultiply_argb32_page(cairo_surface_t *page) fiv_io_profile_argb32(cairo_surface_t *surface,
FivIoProfile source, FivIoProfile target)
{ {
g_return_if_fail(
cairo_image_surface_get_format(surface) == CAIRO_FORMAT_ARGB32);
unsigned char *data = cairo_image_surface_get_data(surface);
int w = cairo_image_surface_get_width(surface);
int h = cairo_image_surface_get_height(surface);
fiv_io_profile_rgb_direct(data, w, h, source, target,
FIV_IO_PROFILE_ARGB32_PREMUL, FIV_IO_PROFILE_ARGB32_PREMUL);
}
static void
fiv_io_profile_argb32_premultiply(cairo_surface_t *surface,
FivIoProfile source, FivIoProfile target)
{
unsigned char *data = cairo_image_surface_get_data(surface);
int w = cairo_image_surface_get_width(surface);
int h = cairo_image_surface_get_height(surface);
if (cairo_image_surface_get_format(surface) != CAIRO_FORMAT_ARGB32) {
fiv_io_profile_xrgb32_direct(data, w, h, source, target);
} else if (!fiv_io_profile_rgb_direct(data, w, h, source, target,
FIV_IO_PROFILE_ARGB32, FIV_IO_PROFILE_ARGB32_PREMUL)) {
g_debug("failed to create a premultiplying transform");
fiv_io_premultiply_argb32(surface);
}
}
#define fiv_io_profile_argb32_premultiply_page(page, target) \
fiv_io_profile_page((page), (target), fiv_io_profile_argb32_premultiply)
#else // ! HAVE_LCMS2 || LCMS_VERSION < 2130
// TODO(p): Unpremultiply, transform, repremultiply. Or require lcms2>=2.13.
#define fiv_io_profile_argb32(surface, source, target)
static void
fiv_io_profile_argb32_premultiply_page(
cairo_surface_t *page, FivIoProfile target)
{
fiv_io_profile_page(page, target, fiv_io_profile_xrgb32);
for (cairo_surface_t *frame = page; frame != NULL; for (cairo_surface_t *frame = page; frame != NULL;
frame = cairo_surface_get_user_data(frame, &fiv_io_key_frame_next)) frame = cairo_surface_get_user_data(frame, &fiv_io_key_frame_next))
fiv_io_premultiply_argb32(frame); fiv_io_premultiply_argb32(frame);
} }
#endif // ! HAVE_LCMS2 || LCMS_VERSION < 2130
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
fiv_io_profile_any(cairo_surface_t *surface,
FivIoProfile source, FivIoProfile target)
{
// TODO(p): Ensure we do colour management early enough, so that
// no avoidable increase of quantization error occurs beforehands,
// and also for correct alpha compositing.
switch (cairo_image_surface_get_format(surface)) {
break; case CAIRO_FORMAT_RGB24:
fiv_io_profile_xrgb32(surface, source, target);
break; case CAIRO_FORMAT_ARGB32:
fiv_io_profile_argb32(surface, source, target);
break; default:
g_debug("CM attempted on an unsupported surface format");
}
}
// 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 (!target)
return image;
for (cairo_surface_t *page = image; page != NULL;
page = cairo_surface_get_user_data(page, &fiv_io_key_page_next))
fiv_io_profile_page(page, target, fiv_io_profile_any);
return image;
}
// --- Wuffs ------------------------------------------------------------------- // --- Wuffs -------------------------------------------------------------------
static bool static bool
@ -1387,7 +1445,7 @@ load_jpeg_finalize(cairo_surface_t *surface, bool cmyk,
if (cmyk) if (cmyk)
fiv_io_profile_cmyk(surface, source, ctx->screen_profile); fiv_io_profile_cmyk(surface, source, ctx->screen_profile);
else else
fiv_io_profile_xrgb32(surface, source, ctx->screen_profile); fiv_io_profile_any(surface, source, ctx->screen_profile);
if (source) if (source)
fiv_io_profile_free(source); fiv_io_profile_free(source);
@ -1831,10 +1889,8 @@ open_libwebp(
} }
WebPDemuxDelete(demux); WebPDemuxDelete(demux);
if (ctx->screen_profile) { if (ctx->screen_profile)
fiv_io_profile_xrgb32_page(result, ctx->screen_profile); fiv_io_profile_argb32_premultiply_page(result, ctx->screen_profile);
fiv_io_premultiply_argb32_page(result);
}
fail: fail:
WebPFreeDecBuffer(&config.output); WebPFreeDecBuffer(&config.output);
@ -3102,12 +3158,10 @@ open_gdkpixbuf(
} }
g_object_unref(pixbuf); g_object_unref(pixbuf);
if (custom_argb32) { if (custom_argb32)
fiv_io_profile_xrgb32_page(surface, ctx->screen_profile); fiv_io_profile_argb32_premultiply_page(surface, ctx->screen_profile);
fiv_io_premultiply_argb32_page(surface); else
} else {
surface = fiv_io_profile_finalize(surface, ctx->screen_profile); surface = fiv_io_profile_finalize(surface, ctx->screen_profile);
}
return surface; return surface;
} }

View File

@ -24,9 +24,7 @@
// --- Colour management ------------------------------------------------------- // --- Colour management -------------------------------------------------------
// TODO(p): Make it possible to use Skia's skcms, // TODO(p): Make it also possible to use Skia's skcms.
// which also supports premultiplied alpha.
// NOTE: Little CMS 2.13 already supports premultiplied alpha, too.
typedef void *FivIoProfile; typedef void *FivIoProfile;
FivIoProfile fiv_io_profile_new(const void *data, size_t len); FivIoProfile fiv_io_profile_new(const void *data, size_t len);
FivIoProfile fiv_io_profile_new_sRGB(void); FivIoProfile fiv_io_profile_new_sRGB(void);