JPEG: Can't set the alpha

Hi there,
I’m trying to change the transparency of an image by setting the 4 channel (the alpha).

Here is my code (simplified)

  
img.loadImage("b.jpg");  
img.setImageType(OF_IMAGE_COLOR_ALPHA);  
for(int i =3; i < img.height*img.width*4; i=i+4)  
{  
	img.getPixels()[i] = 127;  
}  
img.update();  

Everything works fine if I load a PNG-file. But exact same image loaded as JPG shows no transparency (BTW: I’m on WinXP).

Mo

have you enabled alpha blending before drawing?

  
ofEnableAlphaBlending();  

Sure, I’m setting ofEnableAlphaBlending(); in the Draw-routine.
And everything is fine if I load a PNG-image.
But a JPEG (exported from Phostoshop) shows no transparency.
If I check img.bpp (after setting the image type to OF_IMAGE_COLOR_ALPHA) it says 32, so there should be an alpha-channel.

jpeg doesn’t support alpha channel i think, so when you set the type to COLOR_ALPHA, it converts the image to 32 bits and adds an alpha channel but there’s no information to generate the alpha so all of it is set to 255.

Right - the image on the disk jpeg is 24bit (RGB).
But after it has been loaded - and the imagetype changed to OF_IMAGE_COLOR_ALPHA, I thought I should be able to set the values in the fourth channel:

  
for(int i =3; i < img.height*img.width*4; i=i+4)  
{  
   img.getPixels()[i] = 127;  
}  
img.update();  

You are right, it looks like setImageType(OF_IMAGE_COLOR_ALPHA) should work, maybe it’s a bug. On a side note, this isn’t the same obviously, but if you want to draw the jpg transparently you can just do:

ofSetColor(255, 255, 255, 127);
image.draw();

Thanx Memo,
I’ll use the setColor solution for fading images.
And use PNG’s where I need to manipulate areas within images.
Mo