Skip to content

Commit 5e90e1e

Browse files
authored
Fix #8795 gmt image -I inversion (#8837)
* Fix -I invert option for 2-color palette images (#8795) - Detect 2-color (1-bit) images and swap colormap entries when -I is used - Apply -G color replacements to colormap before expansion to RGB - Ensures -I option works correctly for 1-bit PNG images Fixes #8795 * Add test for -I invert option on 2-color iages (#8795) - Tests normal image, inverted image, and inverted with color changes - Verifies fix for issue #8795
1 parent 53ed782 commit 5e90e1e

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/psimage.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -500,16 +500,28 @@ EXTERN_MSC int GMT_psimage (void *V_API, int mode, void *args) {
500500
if (I->colormap != NULL) { /* Image has a color map */
501501
/* Convert colormap from integer to unsigned char and count colors */
502502
n = gmt_unpack_rgbcolors (GMT, I, colormap); /* colormap will be RGBARGBA... */
503-
if (n == 2 && Ctrl->G.active) { /* Replace back or fore-ground color with color given in -G, or catch selection for transparency */
504-
if (Ctrl->G.rgb[PSIMAGE_TRA][0] != -2) {
505-
GMT_Report (API, GMT_MSG_WARNING, "Your -G<color>+t is ignored for 1-bit images; see +b/+f modifiers instead\n");
503+
if (n == 2) { /* 2-color palette image */
504+
/* Handle -I (invert) by swapping colormap entries */
505+
if (Ctrl->I.active) {
506+
unsigned char tmp[4];
507+
for (size_t m = 0; m < 4; m++) {
508+
tmp[m] = colormap[m];
509+
colormap[m] = colormap[4+m];
510+
colormap[4+m] = tmp[m];
511+
}
506512
}
507-
for (unsigned int k = PSIMAGE_BGD; k <= PSIMAGE_FGD; k++) {
508-
if (Ctrl->G.rgb[k][0] == -1) { /* Want this color to be transparent */
509-
has_trans = 1; r = colormap[4*k]; g = colormap[1+4*k]; b = colormap[2+4*k];
513+
/* Replace back or fore-ground color with color given in -G, or catch selection for transparency */
514+
if (Ctrl->G.active) {
515+
if (Ctrl->G.rgb[PSIMAGE_TRA][0] != -2) {
516+
GMT_Report (API, GMT_MSG_WARNING, "Your -G<color>+t is ignored for 1-bit images; see +b/+f modifiers instead\n");
517+
}
518+
for (unsigned int k = PSIMAGE_BGD; k <= PSIMAGE_FGD; k++) {
519+
if (Ctrl->G.rgb[k][0] == -1) { /* Want this color to be transparent */
520+
has_trans = 1; r = colormap[4*k]; g = colormap[1+4*k]; b = colormap[2+4*k];
521+
}
522+
else if (Ctrl->G.rgb[k][0] >= 0) /* If we changed this color, update it, else use what was given in the colormap */
523+
for (size_t m = 0; m < 3; m++) colormap[m+4*k] = gmt_M_u255(Ctrl->G.rgb[k][m]); /* Do not override the A entry, just R/G/B */
510524
}
511-
else if (Ctrl->G.rgb[k][0] >= 0) /* If we changed this color, update it, else use what was given in the colormap */
512-
for (n = 0; n < 3; n++) colormap[n+4*k] = gmt_M_u255(Ctrl->G.rgb[k][n]); /* Do not override the A entry, just R/G/B */
513525
}
514526
}
515527
if (!Ctrl->G.active) has_trans = psimage_find_unique_color (GMT, colormap, n, &r, &g, &b);

test/psimage/bitimage_invert.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
# Test -I option for inverting 2-color (1-bit) images
3+
# This tests the fix for issue #8795
4+
5+
ps=bitimage_invert.ps
6+
7+
# Normal image (black Vader on white background)
8+
gmt psimage @vader1.png -P -Dx0/0+w2i -F+pfaint -K > $ps
9+
10+
# Inverted image (white Vader on black background) using -I
11+
gmt psimage @vader1.png -I -Dx2.5i/0+w2i -F+pfaint -O -K >> $ps
12+
13+
# Inverted with color change: red background, yellow foreground
14+
gmt psimage @vader1.png -I -Gred+b -Gyellow+f -Dx5i/0+w2i -F+pfaint -O >> $ps

0 commit comments

Comments
 (0)