Skip to content

fix an off by one error in rank #4610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
3 changes: 3 additions & 0 deletions libvips/morphology/morphology.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,16 @@ 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;

vobject_class->nickname = "morphology";
vobject_class->description = _("morphological operations");

operation_class->flags = VIPS_OPERATION_SEQUENTIAL;

/* Inputs set by subclassess.
*/

Expand Down
70 changes: 30 additions & 40 deletions libvips/morphology/rank.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -175,62 +168,58 @@ 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;
}

/* 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;
Copy link
Member Author

@jcupitt jcupitt Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the fix, it was bands * (rank->width - 1)

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;
}
Expand Down Expand Up @@ -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;

Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
Loading
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