QImage, parsing raw char from image to get alpha?
-
@Dariusz said in QImage, parsing raw char from image to get alpha?:
run but sadly no luck...
What does this mean?
-
That this
uchar *temp = (uchar *) img.constBits(); ... pixelOffset = imageWidth * he + (wi * 4); alpha = temp[pixelOffset];//qAlpha(img.pixel(wi, he)); /// img.pixel work but slow due to different access function
does not return alpha int. as far as I understand img.constBits() return 1d array, 4 values equal to a color, esentially A-R-G-B, so every 4th item is the alpha value and pixelOffset uses/calculates indexOffset in to the 1d array from 2d array of height*width of image.
The loop function in 1st post shows it.
I don't understand why the value I get from the loop above is not alpha.
-
Your offset calculation is wrong, every pixel has 4 bytes.
-
@Christian-Ehrlicher said in QImage, parsing raw char from image to get alpha?:
Your offset calculation is wrong, every pixel has 4 bytes.
Hmm did I not address that in (wi*4) ? You say bytes but isn't uchar *temp an array of ints? I'm lost o.o
-
@Dariusz said in QImage, parsing raw char from image to get alpha?:
uchar *
I don't see int here... only (unsigned) char
-
@Christian-Ehrlicher Yeah, sorry just used to ints.
So the:
uchar *temp = (uchar *) img.constBits();
Should be something like
temp = [alpha, red, green, blue, alpha, red, green, blue, alpha, red, green, blue, alpha, red, green, blue, alpha, red, green, blue ]
Each value being char. 0-255 - not quite sure how to deal with 32 bit+ arrays tho... are they then int *temp ? constBits return only chars... I'm lost I never dig in to 16/32+ image formats...so if I were to do temp[0] / temp[4],temp[8] then they all should be alphas?
So what am I missing in
imageWidth * imageHeightIndex + (imageWidthIndex *4)
Which if gives correct numbers would result for 300x400 image (w x h)300 * 0 + (0*4) = 0 300 * 0 + (1*4) = 4 300 * 0 + (2*4) = 8
etc etc so I'm getting correct index to retrieve alpha ?
What am I missing ? o.O
-
@Dariusz said in QImage, parsing raw char from image to get alpha?:
What am I missing ? o.O
Simple math I would say:
4 bytes per pixel, row is 300 pixels = 1200 bytes per row -
@Christian-Ehrlicher Hmm char is 2 byte big... so do I offset by 8 instead and add up 2 items together?
int alpha = temp[0]<<temp[1] ?
-
@Dariusz said in QImage, parsing raw char from image to get alpha?:
char is 2 byte big
Ok, now I'm giving up...
-
@Christian-Ehrlicher said in QImage, parsing raw char from image to get alpha?:
@Dariusz said in QImage, parsing raw char from image to get alpha?:
char is 2 byte big
Ok, now I'm giving up...
Eh crap its short thats 2 byte big... char is 1 byte... ehhhhhhhh I forgot about shorts :/. Ok so esentially I have to grab 4 chars and add them up to int?
-
Hi,
Why do you want to combine them back into and int ?
If you have a ARGB image, basically you have:
uchar *start = (uchar *) img.constBits(); uchar *alpha = start + 0; uchar *red = start + 1; uchar *green = start + 2; uchar *blue = start + 3; // First alpha value: uint8 alphaValue = *alpha; // Second value of alpha alphaValue = *(alpha + 4)
if you want to sum them, then make alphaValue an int and that's all.