Is there a way to grab all the pixels of a matrix in Java, operate on every pixel, and then put them all back into the image, using only two JNI calls?
I'm trying to do an operation on every pixel (brightness and contrast) and am wondering if I'm missing something. Is there no way to do it smartly in the java?
I'd love my code to look like this:
byte[imagesize*pixels] imagepixels = mRgba.toPixels();
for(int k = 0; k < imagepixels.size; k++
{
imagepixels[k] = imagepixels[k]*alpha + beta
}
mRgba.put(imagepixels);
But instead it looks like this:
for( int y = 0; y < mRgba.rows(); y++)
{
for(int x = 0; x < mRgba.cols(); x++)
{
dbl = mRgba.get(y, x);
pix[0] = roundProperly(contrast*(dbl[0]) + brightness);
pix[1] = roundProperly(contrast*(dbl[1]) + brightness);
pix[2] = roundProperly(contrast*(dbl[2]) + brightness);
pix[3] = 0;
mRgba.put(y, x, pix);
dbl = null;
}
}
There are tons of memory issues because dbl is being malloced extremely frequently and making the garbage collector block, and I'm doing WAY more JNI calls than I'd like.
I plan on porting this into a single C call, but I'm curious to see the straight java performance of this vs the C performance, and I'd prefer there to be a way to do the java this inefficiently.