   Fading in and out of J2ME images containing transparency
  Hello everyone,I need your help with a problem I encountered while programming on J2ME, here's my story:I wanted to port a project I made in J2SE for an assignement in college to J2ME just for fun and practice. It features some animations, fading in and out of images as well as database access...etc (it's an ATM cash machine simulator)I'm using some images made in photoshop, that are of course transparent PNGs, having also some semi-transparent parts in them (outer glow or shadow).Now when I load those images in J2ME they load fine, with their perfect original appearance. But when I try to change to overall Alpha level of the image to fade it in and out, the transparent and semi-transparent parts become black.The code I used was one I found after so much time looking in google and experimenting:http://developer.sonyericsson.com/cws/devworld/article/techsupport-tipstrickscode-java-pjava0501?cc=gb&lc=enI have a sprite class in which I added the function blend():public static void blend(int[] raw, int alphaValue){    int len = raw.length;    // Start loop through all the pixels in the image.    for(int i=0; i<len; i++){   int a = 0;   int color = (raw & 0x00FFFFFF); // get the color of the pixel.   a = alphaValue;// set the alpha value we want to use 0-255.    a = (a<<24);    // left shift the alpha value 24 bits.   // if color = 00000000 11111111 11111111 00000000 (0xFFFF00 = Yellow)   // and alpha= 01111111 00000000 00000000 00000000   // then c+a = 01111111 11111111 11111111 00000000   // and the pixel will be blended.   color += a;   raw = color;    }}and I modified the draw() function of my class to draw the array of pixels that contains the image data (rawInt), which is altered in the blend() function.public void draw(Graphics g,int x,int y){    g.drawImage(Image.createRGBImage(rawInt, width, height, true) ,x,y, Graphics.TOP | Graphics.LEFT);}to illustrate, here is how things look:The image before applying the Alpha Blending (without calling the blend() function):http://img31.imageshack.us/img31/4893/61565643.pngAfter applying alpha blending with a level of 100:http://img109.imageshack.us/img109/9978/12712798.pngWith a level of 255 (fully opaque):http://img541.imageshack.us/img541/5981/33340170.pngIf anyone would like to see my original J2SE project to have an idea how things should look I can give it to you, just tell me ;)and thanks ;)
  Hey I FINALLYYY found the solution!!!Please read carefully I'll explain with as much detail as possible:okay I'm gonna explain how things are going inside the blend function.First of all, the rawInt array contains all the pixels of the image, each raw in the For loop is a pixel.Each pixels of these is composed of four elements (or variables if you want because they varie from pixel to pixel), these are A-R-G-B:A: Alpha, it's the transparency level of that pixelR: Red, the amount of redG: Green, the amount of greenB: Blue, the amount of blueeach raw is an integer have these four components, 8 bits (or 1 byte) eachnow why in your opinion, after applying a single alpha value for all pixels in the image, the transparent pixels become black ?we can distinguish between 3 types of pixels in termes of transparency:- fully opaque : alpha value = 255- semi-transparent : 0 < alpha value < 255- fully transparent : alpha value = 0After applying that single alpha value for all pixels, let's say for example 127 (semi-transparent), all pixels will have this same value, which is not what we want in reality. It may be suitable if the image had one level of transparency for all its pixels to begin with, but our image isn't like this!what needs to be done, is to keep the same difference of the alpha value between all pixels. Here to explain thing I'm going to make a little drawing:Let's suppose the image alpha levels of its pixels is like this, each column is a pixel:http://img20.imageshack.us/img20/2792/12601770.pngnow when we apply an alpha value with the old blend function here's what we get:http://img88.imageshack.us/img88/582/36464735.pngwhat happened ? all pixels got the same alpha value, which means that the transparent pixels are no longer transparent, but they are now black!I have an idea about how to solve this problem:I changed the parameter of the blend function to take a float instead of an integer, this float will have values between 0 and 1, now everytime I call the blend function, I have to ensure that the array has the original values, so I create a new one using the getRGB methodfrom my Image object, after that I take the original alpha value of each pixel (inside the for loop) and I multiply it by that alpha ratio, so that every pixel changes it's alpha by the same amount preserving the diffence between them, the transparent pixels will stay transparentand everything should go well.here's how the blend function have become:public void blend(float alphaValue){// I create a new array containing the original values of the image// used to be done only in the constrcutor of my Sprite classrawInt = new int[getWidth() * getHeight()];img.getRGB(rawInt, 0, getWidth(), 0, 0, getWidth(), getHeight()); // Start loop through all the pixels in the image.for(int i=0; i < rawInt.length; i++){// get the color of the pixel.int color = (rawInt & 0x00FFFFFF);// get the alpha value of the pixelint oldAlpha = (rawInt & 0xFF000000);// the new alpha value that gets calculatedint newAlpha = 0; // we shift the value from its position as the 8 most significant bits// to the least significant so that we can take it as a normal value (0-255)oldAlpha = (oldAlpha >> 24); // now we calculate the new alpha value by multiplying the old alpha value by the rationewAlpha = (int)(oldAlpha * alphaValue); // left shift the alpha value 24 bits.newAlpha = ((newAlpha)<<24);// Explanation:// if color = 00000000 11111111 11111111 00000000 (0xFFFF00 = Yellow)// and alpha= 01111111 00000000 00000000 00000000// then c+a = 01111111 11111111 11111111 00000000// and the pixel will be blended.color += newAlpha;rawInt = color;}}It still doesn't work!!After some debugging sessions, I found out that the old alpha value I get for pixels that I use to calculate the new value, doesn't range from 0 to 255 but from -128 to 127, but worse 0 still means fully transparent!!With this my mind really got confused! I no longer know which value is for fully opaque pixels and how the values are distributed on this scale.I went to sleep after being so much frustrated ...--------this morning I woke up really excitd to work on this problem again and try to find a solution...I went to google and looked for how we could represent unsigned numbers, since what I need is a variable that can store numbers ranging from 0 to 255 not from -128 to 127. I found out that signed variables have one of their bits as the signe bit, so when we want to use them on bitwise operationsand especially the right shift operator things may get a little messy.. the solution ?it's to use the logical operator >>> instead of the arithmetic operator >> . that's all!! the solution is one character away!!and finally i can use the fade in and out effects on my images with ease!!!I hope my solution will be usefull for some futur CodeWarrior!!