Skip to content

Commit 162836a

Browse files
authored
Use correct bands for two band histograms (#9054)
2 parents 9a37051 + dc7d646 commit 162836a

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

Tests/test_image_histogram.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ def histogram(mode: str) -> tuple[int, int, int]:
1010

1111
assert histogram("1") == (256, 0, 10994)
1212
assert histogram("L") == (256, 0, 662)
13+
assert histogram("LA") == (512, 0, 16384)
14+
assert histogram("La") == (512, 0, 16384)
1315
assert histogram("I") == (256, 0, 662)
1416
assert histogram("F") == (256, 0, 662)
1517
assert histogram("P") == (256, 0, 1551)
18+
assert histogram("PA") == (512, 0, 16384)
1619
assert histogram("RGB") == (768, 4, 675)
1720
assert histogram("RGBA") == (1024, 0, 16384)
1821
assert histogram("CMYK") == (1024, 0, 16384)

src/libImaging/Histo.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,15 @@ ImagingGetHistogram(Imaging im, Imaging imMask, void *minmax) {
132132
ImagingSectionEnter(&cookie);
133133
for (y = 0; y < im->ysize; y++) {
134134
UINT8 *in = (UINT8 *)im->image[y];
135-
for (x = 0; x < im->xsize; x++) {
136-
h->histogram[(*in++)]++;
137-
h->histogram[(*in++) + 256]++;
138-
h->histogram[(*in++) + 512]++;
139-
h->histogram[(*in++) + 768]++;
135+
for (x = 0; x < im->xsize; x++, in += 4) {
136+
h->histogram[*in]++;
137+
if (im->bands == 2) {
138+
h->histogram[*(in + 3) + 256]++;
139+
} else {
140+
h->histogram[*(in + 1) + 256]++;
141+
h->histogram[*(in + 2) + 512]++;
142+
h->histogram[*(in + 3) + 768]++;
143+
}
140144
}
141145
}
142146
ImagingSectionLeave(&cookie);

0 commit comments

Comments
 (0)