The problem is in the code for getting the alpha. In the second bit shift operation, you don't take the sign bit into consideration.
int val=(ialpha.getRGB(0, 0) & 0xff000000) >> 24;
This will give the value 0xffffff8c
(given your initial alpha of 140
of 0x8c
).
See Bitwise and Bit Shift Operators for more detail. In particular:
The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.
You need to either do either:
int val = (ialpha.getRGB(0, 0) & 0xff000000) >>> 24; // shift zero into left
Or:
int val = ialpha.getRGB(0, 0) >> 24) & 0xff; // mask out the sign part
PS: I tend to prefer the latter, because most people (myself included) never remember what the >>>
operator actually does.. ;-)