Skip to content

Commit dbd4950

Browse files
committed
FIX: better error when setting image's alpha value using path notation
+ allowing edge cases (0 and 255) + added range check for CHAR argument Fixes: metaeducation/rebol-issues#2343
1 parent 8211424 commit dbd4950

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

src/core/c-do.c

+2
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ void Trace_Arg(REBINT num, REBVAL *arg, REBVAL *path)
421421
Trap_Range(pvs->path);
422422
case PE_BAD_SET_TYPE:
423423
Trap2(RE_BAD_FIELD_SET, pvs->path, Of_Type(pvs->setval));
424+
case PE_BAD_ARGUMENT:
425+
Trap1(RE_INVALID_ARG, pvs->setval);
424426
}
425427

426428
if (NOT_END(pvs->path+1)) Next_Path(pvs);

src/core/t-image.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1284,9 +1284,10 @@ INLINE REBCNT ARGB_To_BGR(REBCNT i)
12841284
}
12851285

12861286
// Set the alpha only:
1287-
if (IS_INTEGER(val) && VAL_INT64(val) > 0 && VAL_INT64(val) < 255) n = VAL_INT32(val);
1288-
else if (IS_CHAR(val)) n = VAL_CHAR(val);
1289-
else return PE_BAD_RANGE;
1287+
if (IS_INTEGER(val) && VAL_INT64(val) >= 0 && VAL_INT64(val) <= 255) n = VAL_INT32(val);
1288+
else if (IS_CHAR(val) && VAL_CHAR(val) <= 255) n = VAL_CHAR(val); // char in R3 may be larger that 255, not like in R2
1289+
// Do we really want to be able change the alpha using a CHAR value? I don't think so, but keep it there so far; Oldes
1290+
else return PE_BAD_ARGUMENT;
12901291

12911292
dp = (REBCNT*)QUAD_SKIP(series, index);
12921293
*dp = (*dp & 0xffffff) | (n << 24);

src/include/sys-value.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,8 @@ enum Path_Eval_Result {
10051005
PE_BAD_SELECT,
10061006
PE_BAD_SET,
10071007
PE_BAD_RANGE,
1008-
PE_BAD_SET_TYPE
1008+
PE_BAD_SET_TYPE,
1009+
PE_BAD_ARGUMENT
10091010
};
10101011

10111012
typedef REBINT (*REBPEF)(REBPVS *pvs); // Path evaluator function

src/tests/units/image-test.r3

+21
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,27 @@ Rebol [
6060
--assert error? try [img/(3x2): 1.2.3]
6161
===end-group===
6262

63+
===start-group=== "set alpha using path"
64+
;@@ https://github.com/rebol/rebol-issues/issues/2343
65+
img: make image! 2x2
66+
--test-- "set alpha using path and integer"
67+
img/1: 0
68+
--assert img/1 = 255.255.255.0
69+
img/1: 127
70+
--assert img/1 = 255.255.255.127
71+
72+
--test-- "set alpha using path and char"
73+
img/1: #"^@"
74+
--assert img/1 = 255.255.255.0
75+
img/1: to char! 127
76+
--assert img/1 = 255.255.255.127
77+
78+
--test-- "set alpha using path with out of range value"
79+
--assert error? try [img/1: -1]
80+
--assert error? try [img/1: 256]
81+
--assert error? try [img/1: #"^(260)"]
82+
===end-group===
83+
6384
===start-group=== "image pixel assignment validity"
6485
--test-- "image pixel 3-tuple assignment"
6586
--assert 255.255.255 = img/1: 255.255.255

0 commit comments

Comments
 (0)