java - Changing alpha of rgb bit -
i setting alpha of rgb of buffredimage in java. code changes alpha value can't retrieve same value after saving file. how overcome problem.
// ================ code setting alpha =============== int alpha=140; // alpha value set in rgb int b=alpha<<24; b=b|0x00ffffff; ialpha.setrgb(0, 0,ialpha.getrgb(0, 0)&b); // ialpha bufferedimage of type type_int_argb imageio.write(ialpha, "png", new file("c:/newimg.png")); system.out.println("\nfile saved !"); // ================ code getting alpha =============== int val=(ialpha.getrgb(0, 0)&0xff000000)>>24; if(val<0) val=256+val; system.out.println("returned alpha value:"+val);
this returns 255 alpha value. not return value set i.e 140.
please me retrieve alpha value set.
the problem in code getting alpha. in second bit shift operation, don't take sign bit consideration.
int val=(ialpha.getrgb(0, 0) & 0xff000000) >> 24;
this give value 0xffffff8c
(given initial alpha of 140
of 0x8c
). see bitwise , bit shift operators more detail. in particular:
the unsigned right shift operator ">>>" shifts 0 leftmost position, while leftmost position after ">>" depends on sign extension.
you need either either:
int val = (ialpha.getrgb(0, 0) & 0xff000000) >>> 24; // shift 0 left
or:
int val = ialpha.getrgb(0, 0) >> 24) & 0xff; // mask out sign part
ps: tend prefer latter, because people (myself included) never remember >>>
operator does.. ;-)
Comments
Post a Comment