Looking for a way to speed up png file writing

K

kmsterrett

Hello,

I have a pixel array that contains RGB data.
This code correctly creates that png file, but it take a long time.
About 700ms.
Does anyone know of a way for me to speed this up?
I will need to write as many of 50-60 files, so I do not want my user
to wait.
Thanks in advance for any advice.
Kim
-----this is the code

WritableRaster wr;
DataBuffer db = new DataBufferUShort(pixels,720*504);
BandedSampleModel sm = new BandedSampleModel
(DataBuffer.TYPE_USHORT,720,504,3);
wr = Raster.createWritableRaster(sm,db,new Point(0,0));
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new ComponentColorModel(cs,false,
false,Transparency.OPAQUE,DataBuffer.TYPE_USHORT);

final BufferedImage bi = new BufferedImage(cm, wr, false, null);
RenderedImage renImage = (RenderedImage)bi;

//Write generated image to a file
try {
// Save as PNG
if (renImage != null){
File filImg2d = new File(fileName + ".png");
ImageIO.write(renImage, "png", filImg2d);
}
}
catch (Exception e){
System.out.println("Error in writing image to file " +
e.getMessage() + " );
}
 
K

kmsterrett

The loop for multiple files is in another method, that calls this
WriteToFile with the filename string.

The pixels array is 2d array class variable,
short [][] pixels = new short [3][720*504];
 
A

Andrew Thompson

The loop ..

What loop?
..for multiple files is in another method, that calls this
WriteToFile with the filename string.

The pixels array is 2d array class variable,
short [][] pixels = new short [3][720*504];

...good. Now getting back to what I actually
suggested.

<http://www.physci.org/codes/sscce.html>

The processing can be sped up by moving the
try/catch, as well as possibly some variable
declaration/initialisation, outside the loop.
But for the best answer, post an SSCCE
(as opposed to code snippets and descriptions).

Andrew T.
 
K

Knute Johnson

kmsterrett said:
Hello,

I have a pixel array that contains RGB data.
This code correctly creates that png file, but it take a long time.
About 700ms.
Does anyone know of a way for me to speed this up?
I will need to write as many of 50-60 files, so I do not want my user
to wait.
Thanks in advance for any advice.
Kim
-----this is the code

WritableRaster wr;
DataBuffer db = new DataBufferUShort(pixels,720*504);
BandedSampleModel sm = new BandedSampleModel
(DataBuffer.TYPE_USHORT,720,504,3);
wr = Raster.createWritableRaster(sm,db,new Point(0,0));
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new ComponentColorModel(cs,false,
false,Transparency.OPAQUE,DataBuffer.TYPE_USHORT);

final BufferedImage bi = new BufferedImage(cm, wr, false, null);
RenderedImage renImage = (RenderedImage)bi;

//Write generated image to a file
try {
// Save as PNG
if (renImage != null){
File filImg2d = new File(fileName + ".png");
ImageIO.write(renImage, "png", filImg2d);
}
}
catch (Exception e){
System.out.println("Error in writing image to file " +
e.getMessage() + " );
}

You could buffer the output stream. Do you actually need to write
faster or just make it appear to the user that it went quickly?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top