Forums > Java

[Bug?] copyBufferedImage method extremely slow


June 23, 2008 | 6:08 pm

Is it normal that the copyBufferedImage method takes 100 milliseconds to run?

(tested in max5, couldn’t test in my max4 app because jitter is not authorized in it.)

Thanks,
Alexandre

to reproduce :

– Pasted Max Patch, click to expand. –

import com.cycling74.max.*;
import com.cycling74.jitter.*;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;

public class test_copyBufferedImage extends MaxObject {
test_copyBufferedImage (Atom[] a) {
declareInlets(new int[]{DataTypes.ALL});
declareOutlets(new int[]{DataTypes.ALL});
createInfoOutlet(false);
}
int xwcurve= 1200 ;
int ywcurve= 700 ;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration gconf = ge.getDefaultScreenDevice().getDefaultConfiguration() ;
BufferedImage myImageCurv = gconf.createCompatibleImage(xwcurve, ywcurve) ;
Graphics2D gc = myImageCurv.createGraphics();
JitterMatrix jim = new JitterMatrix();

public void bang() { // this bang takes 1 second to go out
gc.setColor(Color.WHITE) ;
gc.fillRect(0, 0, xwcurve, ywcurve) ;
gc.setColor(Color.BLACK);
gc.drawLine( 10, 10, 25, 35);

jim.copyBufferedImage(myImageCurv) ; // erase these ten lines and
jim.copyBufferedImage(myImageCurv) ; // and then the bang goes instantaneously.
jim.copyBufferedImage(myImageCurv) ;
jim.copyBufferedImage(myImageCurv) ;
jim.copyBufferedImage(myImageCurv) ;
jim.copyBufferedImage(myImageCurv) ;
jim.copyBufferedImage(myImageCurv) ;
jim.copyBufferedImage(myImageCurv) ;
jim.copyBufferedImage(myImageCurv) ;
jim.copyBufferedImage(myImageCurv) ;

outlet(0, "bang" ) ;
}
}

June 23, 2008 | 11:22 pm

nevermind…

i’m not going to use the normal graphics java classes again. i found out an other surprising way to draw the faster graphics in java, see my new topic : "Draw graphics 15 times faster than with jit.lcd or any other method"

Thanks,

Alexandre

June 23, 2008 | 11:50 pm

I dont know why it is so slow. I havent really looked at that code.
Perhaps you could use copyPackedIntArray with
BufferedImage.getRaster().getPixels() and see if that is faster than
copyBufferedImage.I guess you would have to make sure the format of the
raster matches the jit matrix format. I generally draw into a matrix in
the relatively raw way you describe but it is nice to have a real
graphics api sometimes!
t

Alexandre wrote:
> nevermind…
>
> i’m not going to use the normal graphics java classes again. i found out an other surprising way to draw the faster graphics in java, see my new topic : "Draw graphics 15 times faster than with jit.lcd or any other method"
>
> Thanks,
>
> Alexandre
>
>

June 24, 2008 | 12:48 pm

Thanks Topher,
Well, mmh… BufferedImage.getRaster().getPixels() would be nice if i need to use tha java graphic api in the future.
But for now, to draw linesegments, as this "raw way" ("pixels-by-pixels-in-an-array way") is in fact more efficient for in my case (and i think, most of the cases), i just already began to use copyPackedIntArrayToMatrix anyway… …but oho.. i just realised that – i’m sorry – this is also slow to me!! (14 milliseconds, not as much as copyBufferedImage but still)

Topher, do you remember about this discussion i posted on the list, on sending int[] or float[] arrays from java to max, two years ago ? ->
http://www.cycling74.com/forums/index.php?t=msg&rid=2682&S=84b4a9974767c6e196147dbca8137681&th=16528&goto=55741#msg_55741
You finally came out, at the end of it, with these GREAT new "outlet(…, int/float[] values)" methods.. (sending lots of values at one time, without crossing the java-c bridge for each one)

So, with the example below i’m wondering if you could perhaps improve something here too :

I mesure 14 milliseconds* to run _m.copyPackedIntArrayToMatrix(image), on an image array of [1200*700].

That a lot for what i expected to do in max and mxj, with graphics moving very fast and lots of audio going on, vst effects, etc.

I tried the copyArrayToMatrix() method used with a 1 plane matrix (i don’t need colors), and it’s more.. around 20 milliseconds.

To fill the whole image array one by one with values (1200*700=840000), only 1,4 milliseconds** are needed… So i’m wondering what’s taking so much time in these two routines to fill a jittermatrix with an int array :

- Are you crossing the java-c bridge for writing each row in the matrix ?
– Are you testing if each value from the array is fitting in the matrix ? -> if it’s the case, then i’d love to have a "without-clipping-values" version of the method, i don’t mind if max5 crashes because of mismatch mistake in my java code, i’ll be carefull..

- Do you think you could do something to improve either copyPackedIntArrayToMatrix or copyArrayToMatrix ?
or inventing an other way to pass an array to jitter ?

Thanks!
Alexandre

* that’s around 36 cycles of my 2,2Ghz intel processor to copy one pixel value in the matrix.
** that’s around 3,6 cycles of my 2,2Ghz intel processor to fill one value in the array.

Here are my copyPackedIntArrayToMatrix_test and copyArrayToMatrix_1_plane_test methods showing the efficiency :

– Pasted Max Patch, click to expand. –

import com.cycling74.max.*;
import com.cycling74.jitter.*;

public class mxj test_arraytomatrix extends MaxObject {
mxj test_arraytomatrix (Atom[] a) {
declareInlets(new int[]{DataTypes.ALL});
declareOutlets(new int[]{DataTypes.ALL});
createInfoOutlet(false);
}

int image [] = new int [1200*700] ;
JitterMatrix _m = new JitterMatrix("my_display",4,"char",1200,700);
JitterMatrix _m2 = new JitterMatrix("my_display2",1,"char",1200,700);

public void copyPackedIntArrayToMatrix_test() {
for (int i=1;i

June 26, 2008 | 9:36 pm

very interesting Alexander, please add more info- if you have on-
that technique.
especially of interest to me is fast ways to fill a matrix

On Tue, Jun 24, 2008 at 3:48 PM, Alexandre wrote:

>
> Thanks Topher,
> Well, mmh… BufferedImage.getRaster().getPixels() would be nice if i need
> to use tha java graphic api in the future.
> But for now, to draw linesegments, as this "raw way"
> ("pixels-by-pixels-in-an-array way") is in fact more efficient for in my
> case (and i think, most of the cases), i just already began to use
> copyPackedIntArrayToMatrix anyway… …but oho.. i just realised that -
> i’m sorry – this is also slow to me!! (14 milliseconds, not as much as
> copyBufferedImage but still)
>
> Topher, do you remember about this discussion i posted on the list, on
> sending int[] or float[] arrays from java to max, two years ago ? ->
>
> http://www.cycling74.com/forums/index.php?t=msg&rid=2682&S=84b4a9974767c6e196147dbca8137681&th=16528&goto=55741#msg_55741
> You finally came out, at the end of it, with these GREAT new "outlet(…,
> int/float[] values)" methods.. (sending lots of values at one time, without
> crossing the java-c bridge for each one)
>
> So, with the example below i’m wondering if you could perhaps improve
> something here too :
>
> I mesure 14 milliseconds* to run _m.copyPackedIntArrayToMatrix(image), on
> an image array of [1200*700].
>
> That a lot for what i expected to do in max and mxj, with graphics moving
> very fast and lots of audio going on, vst effects, etc.
>
> I tried the copyArrayToMatrix() method used with a 1 plane matrix (i don’t
> need colors), and it’s more.. around 20 milliseconds.
>
> To fill the whole image array one by one with values (1200*700=840000),
> only 1,4 milliseconds** are needed… So i’m wondering what’s taking so much
> time in these two routines to fill a jittermatrix with an int array :
>
> – Are you crossing the java-c bridge for writing each row in the matrix ?
> – Are you testing if each value from the array is fitting in the matrix ?
> -> if it’s the case, then i’d love to have a "without-clipping-values"
> version of the method, i don’t mind if max5 crashes because of mismatch
> mistake in my java code, i’ll be carefull..
>
> – Do you think you could do something to improve either
> copyPackedIntArrayToMatrix or copyArrayToMatrix ?
> or inventing an other way to pass an array to jitter ?
>
> Thanks!
> Alexandre
>
>
> * that’s around 36 cycles of my 2,2Ghz intel processor to copy one pixel
> value in the matrix.
> ** that’s around 3,6 cycles of my 2,2Ghz intel processor to fill one value
> in the array.
>
>
>
> Here are my copyPackedIntArrayToMatrix_test and
> copyArrayToMatrix_1_plane_test methods showing the efficiency :
>
>
>
>

– Pasted Max Patch, click to expand. –

>
>
>
> import com.cycling74.max.*;
> import com.cycling74.jitter.*;
>
> public class mxj test_arraytomatrix extends MaxObject {
> mxj test_arraytomatrix (Atom[] a) {
> declareInlets(new int[]{DataTypes.ALL});
> declareOutlets(new int[]{DataTypes.ALL});
> createInfoOutlet(false);
> }
>
> int image [] = new int [1200*700] ;
> JitterMatrix _m = new JitterMatrix("my_display",4,"char",1200,700);
> JitterMatrix _m2 = new
> JitterMatrix("my_display2",1,"char",1200,700);
>
> public void copyPackedIntArrayToMatrix_test() {
> for (int i=1;i _m.copyPackedIntArrayToMatrix(image) ;
> }
> outlet(0, "Done 70 iterations on : ‘
> _m.copyPackedIntArrayToMatrix(image);’ " ) ;
> }
>
> public void copyArrayToMatrix_1_plane_test() {
> for (int i=1;i _m2.copyArrayToMatrix(image) ;
> }
> outlet(0, "Done 50 iterations on : ‘
> _m2.copyArrayToMatrix(image);’ " ) ;
> }
>
> public void fill_the_array_test() {
> for (int i=0;i for( int jj=1; jj image[jj] = 3 ;
> }
> }
> outlet(0, "Done 700 iterations on: ‘for (int jj=0;
> jj

Viewing 5 posts - 1 through 5 (of 5 total)

Forums > Java