Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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?

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.

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?

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.

Hmm, adding this part on:

So I was looking at writing this in C, but since its my own C code, I don't have access to the Opencv Mat library and stuff.

the .get function is calling a native function, and I do not have access to that code:

private static native double[] nGet(long self, int row, int col);

Is the only way to get access to that to compile the opencv manager app myself and break compatibility with the appstore version? Or do I just figure out how the matrix is stored in memory and do a native call myself that basically accesses the memory directly? What's the best way to go about this?

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?

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.

Hmm, adding this part on:

So I was looking at writing this in C, but since its my own C code, I don't have access to the Opencv Mat library and stuff.

the .get function is calling a native function, and I do not have access to that code:

private static native double[] nGet(long self, int row, int col);

Is the only way to get access to that to compile the opencv manager app myself and break compatibility with the appstore version? Or do I just figure out how the matrix is stored in memory and do a native call myself that basically accesses the memory directly? What's the best way to go about this?

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?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.