As a point, the original image does not have an alpha channel. However, if I ignore the fact that I’m loading an image and simply create a surface and populate each pixel with random values, it seems to work. For example:
mSurfaceOut = Surface16u::create(1024,768,true); // changed from Surface::create
int w = mSurface16->getWidth();
int h = mSurface16->getHeight();
for (int i = 0; i < w ; i ++)
{
for (int j = 0; j < h ; j ++)
{
int16_t r = randInt(0, 65535);
int16_t g = randInt(0, 65535);
int16_t b = randInt(0, 65535);
int16_t a = randInt(0, 65535);
mSurfaceOut->setPixel(ivec2(i,j), ColorA(r, g, b, a));
//cout << "pixels new alpha: " << pixel.a << endl;
}
}
Something to note here is that if I declare any of the r,g,b, or a variables as int32_t this does not work.
However, when I then load this image in using: mSurface16 = Surface16u::create( loadImage( path ) );
The r,g,b, and a values are either 0 or 65535. The image I’ve included above is definitely a 16-bit image and contains an alpha channel… I’m just getting a bit lost as to why it’s maxing/zeroing out the values, and why it’s not preserving any changes to the alpha channel.
You’re quite right Rich, it is that way in my code, I wrote this reply away from my laptop and didn’t have the code on hand. The line in the code is in fact:
So yeah, make sure that the original PNG actually has an alpha channel, otherwise neither the getPixel or setPixel function will do anything meaningful. Also, use uint16_t (unsigned), otherwise you’ll get invalid results for values above 32767.
Thanks Paul, I’ll give this a try and let you know. Is a viable workaround for loading an image that has no alpha channel, like a JPG, creating a new Surface!6u with an alpha channel ( mSurfaceOut = Surface16u::create(1024, 768, true) ) and manually setting RGB values based on the original image’s color data, then giving it an alpha value of my choosing?
I guess so, but JPG’s are always 8-bit as far as I know, so you’d have to only set the hi byte of the 16-bit value and set the lo byte to zero (in other words: convert 8-bit to 16-bit). Be careful to properly skip/set the 16-bit alpha word, based on whether you’re using ARGB or RGBA. And of course be aware of endianness. You’ll probably have to experiment a little.