From 2bc60c5e764b3de3af8a9146f17c8e67e6e0cf3e Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Wed, 16 Jul 2025 05:02:24 +0100 Subject: [PATCH 1/3] fix an off by one error in rank only there for three years sigh see https://github.com/libvips/libvips/issues/4609 --- ChangeLog | 1 + libvips/morphology/rank.c | 72 +++++++++++++++++---------------------- 2 files changed, 32 insertions(+), 41 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3a46e366ee..206a2f3d54 100644 --- a/ChangeLog +++ b/ChangeLog @@ -9,6 +9,7 @@ - cache: suppress invalidation errors in release builds [kleisauke] - dzsave: IIIF: use named region of 'full' when no crop takes place [lovell] - pdfload: fix potential crash with pdfium < 6633 [zhifengzhuang] +- rank: fix an off-by-one error [larsmaxfield] 5/6/25 8.17.0 diff --git a/libvips/morphology/rank.c b/libvips/morphology/rank.c index 430b21580f..8f375a94aa 100644 --- a/libvips/morphology/rank.c +++ b/libvips/morphology/rank.c @@ -115,12 +115,9 @@ vips_rank_stop(void *vseq, void *a, void *b) VIPS_FREE(seq->sort); if (seq->hist && - in) { - int i; - - for (i = 0; i < in->Bands; i++) + in) + for (int i = 0; i < in->Bands; i++) VIPS_FREE(seq->hist[i]); - } VIPS_FREE(seq->hist); return 0; @@ -147,17 +144,13 @@ vips_rank_start(VipsImage *out, void *a, void *b) } if (rank->hist_path) { - int i; - - if (!(seq->hist = - VIPS_ARRAY(NULL, in->Bands, unsigned int *))) { + if (!(seq->hist = VIPS_ARRAY(NULL, in->Bands, unsigned int *))) { vips_rank_stop(seq, in, rank); return NULL; } - for (i = 0; i < in->Bands; i++) - if (!(seq->hist[i] = - VIPS_ARRAY(NULL, 256, unsigned int))) { + for (int i = 0; i < in->Bands; i++) + if (!(seq->hist[i] = VIPS_ARRAY(NULL, 256, unsigned int))) { vips_rank_stop(seq, in, rank); return NULL; } @@ -175,29 +168,26 @@ vips_rank_generate_uchar(VipsRegion *out_region, VipsImage *in = seq->ir->im; VipsRect *r = &out_region->valid; const int bands = in->Bands; - const int last = bands * (rank->width - 1); + const int lsk = VIPS_REGION_LSKIP(seq->ir); /* Get input and output pointers for this line. */ - VipsPel *restrict p = - VIPS_REGION_ADDR(seq->ir, r->left, r->top + y); - VipsPel *restrict q = - VIPS_REGION_ADDR(out_region, r->left, r->top + y); + VipsPel *restrict p = VIPS_REGION_ADDR(seq->ir, r->left, r->top + y); + VipsPel *restrict q = VIPS_REGION_ADDR(out_region, r->left, r->top + y); VipsPel *restrict p1; - int lsk; - int x, i, j, b; - - lsk = VIPS_REGION_LSKIP(seq->ir); /* Find histogram for the first output pixel. */ - for (b = 0; b < bands; b++) + for (int b = 0; b < bands; b++) memset(seq->hist[b], 0, 256 * sizeof(unsigned int)); p1 = p; - for (j = 0; j < rank->height; j++) { - for (i = 0, x = 0; x < rank->width; x++) - for (b = 0; b < bands; b++, i++) + for (int j = 0; j < rank->height; j++) { + int i; + + i = 0; + for (int x = 0; x < rank->width; x++) + for (int b = 0; b < bands; b++, i++) seq->hist[b][p1[i]] += 1; p1 += lsk; @@ -205,32 +195,31 @@ vips_rank_generate_uchar(VipsRegion *out_region, /* Loop for output pels. */ - for (x = 0; x < r->width; x++) { - for (b = 0; b < bands; b++) { + for (int x = 0; x < r->width; x++) { + for (int b = 0; b < bands; b++) { /* Calculate cumulative histogram -- the value is the * index at which we pass the rank. */ unsigned int *restrict hist = seq->hist[b]; int sum; - int i; - + int value; sum = 0; - for (i = 0; i < 256; i++) { - sum += hist[i]; + for (value = 0; value < 256; value++) { + sum += hist[value]; if (sum > rank->index) break; } - q[b] = i; + q[b] = value; - /* Adapt histogram -- remove the pels from - * the left hand column, add in pels for a - * new right-hand column. + /* Adapt histogram -- remove the pels from the left hand column, + * add in pels for a new right-hand column. */ + const int next = bands * rank->width; p1 = p + b; - for (j = 0; j < rank->height; j++) { + for (int j = 0; j < rank->height; j++) { hist[p1[0]] -= 1; - hist[p1[last]] += 1; + hist[p1[next]] += 1; p1 += lsk; } @@ -437,7 +426,7 @@ vips_rank_generate(VipsRegion *out_region, VipsRect s; int ls; - int x, y; + int x; int i, j, k; int upper, lower, mid; @@ -451,7 +440,7 @@ vips_rank_generate(VipsRegion *out_region, return -1; ls = VIPS_REGION_LSKIP(ir) / VIPS_IMAGE_SIZEOF_ELEMENT(in); - for (y = 0; y < r->height; y++) { + for (int y = 0; y < r->height; y++) { if (rank->hist_path) vips_rank_generate_uchar(out_region, seq, rank, y); else if (rank->index == 0) @@ -492,7 +481,8 @@ vips_rank_build(VipsObject *object) return -1; } rank->n = rank->width * rank->height; - if (rank->index < 0 || rank->index > rank->n - 1) { + if (rank->index < 0 || + rank->index > rank->n - 1) { vips_error(class->nickname, "%s", _("index out of range")); return -1; } @@ -527,7 +517,7 @@ vips_rank_build(VipsObject *object) * too many recalculations on overlaps. */ if (vips_image_pipelinev(rank->out, - VIPS_DEMAND_STYLE_FATSTRIP, in, NULL)) + VIPS_DEMAND_STYLE_SMALLTILE, in, NULL)) return -1; rank->out->Xsize -= rank->width - 1; rank->out->Ysize -= rank->height - 1; From 33762dca7ad346a0a6dea2cca7e5a71ca8795a71 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Wed, 16 Jul 2025 05:10:25 +0100 Subject: [PATCH 2/3] bump version to 8.17.2 --- ChangeLog | 5 ++++- meson.build | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 206a2f3d54..369782db79 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +8.17.2 + +- rank: fix an off-by-one error [larsmaxfield] + 7/7/25 8.17.1 - fix API docs build with meson < 0.60 [lovell] @@ -9,7 +13,6 @@ - cache: suppress invalidation errors in release builds [kleisauke] - dzsave: IIIF: use named region of 'full' when no crop takes place [lovell] - pdfload: fix potential crash with pdfium < 6633 [zhifengzhuang] -- rank: fix an off-by-one error [larsmaxfield] 5/6/25 8.17.0 diff --git a/meson.build b/meson.build index f34c3628da..d36bdd87f7 100644 --- a/meson.build +++ b/meson.build @@ -1,5 +1,5 @@ project('vips', 'c', 'cpp', - version: '8.17.1', + version: '8.17.2', meson_version: '>=0.55', default_options: [ # this is what glib uses (one of our required deps), so we use it too @@ -23,7 +23,7 @@ version_patch = version_parts[2] # binary interface changed: increment current, reset revision to 0 # binary interface changes backwards compatible?: increment age # binary interface changes not backwards compatible?: reset age to 0 -library_revision = 1 +library_revision = 2 library_current = 61 library_age = 19 library_version = '@0@.@1@.@2@'.format(library_current - library_age, library_age, library_revision) From 11e02daa66c241b7969bd6bf48b69e1bcacf1f53 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Wed, 16 Jul 2025 12:24:44 +0100 Subject: [PATCH 3/3] reset rank demand hint, enable seq for morph I'd left the demand hint for rank as smalltile from some debugging. I've set the whole of morphology to sequential mode, which should be safe, cf. the convolution base class. --- libvips/morphology/morphology.c | 3 +++ libvips/morphology/rank.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/libvips/morphology/morphology.c b/libvips/morphology/morphology.c index 7704cd5e18..92b51cc9d0 100644 --- a/libvips/morphology/morphology.c +++ b/libvips/morphology/morphology.c @@ -57,6 +57,7 @@ vips_morphology_class_init(VipsMorphologyClass *class) { GObjectClass *gobject_class = G_OBJECT_CLASS(class); VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS(class); + VipsOperationClass *operation_class = VIPS_OPERATION_CLASS(class); gobject_class->set_property = vips_object_set_property; gobject_class->get_property = vips_object_get_property; @@ -64,6 +65,8 @@ vips_morphology_class_init(VipsMorphologyClass *class) vobject_class->nickname = "morphology"; vobject_class->description = _("morphological operations"); + operation_class->flags = VIPS_OPERATION_SEQUENTIAL; + /* Inputs set by subclassess. */ diff --git a/libvips/morphology/rank.c b/libvips/morphology/rank.c index 8f375a94aa..94f2e189aa 100644 --- a/libvips/morphology/rank.c +++ b/libvips/morphology/rank.c @@ -517,7 +517,7 @@ vips_rank_build(VipsObject *object) * too many recalculations on overlaps. */ if (vips_image_pipelinev(rank->out, - VIPS_DEMAND_STYLE_SMALLTILE, in, NULL)) + VIPS_DEMAND_STYLE_FATSTRIP, in, NULL)) return -1; rank->out->Xsize -= rank->width - 1; rank->out->Ysize -= rank->height - 1; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy