Looking for a way to speed up png file writing

Discussion in 'Java' started by kmsterrett, Feb 23, 2007.

  1. kmsterrett

    kmsterrett Guest

    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() + " );
    }
     
    kmsterrett, Feb 23, 2007
    #1
    1. Advertising

  2. On Feb 24, 2:14 am, "kmsterrett" <> wrote:
    ...
    > Does anyone know of a way for me to speed this up?


    Post an SSCCE?

    > I will need to write as many of 50-60 files ..


    Where is the loop in your code?

    Andrew T.
     
    Andrew Thompson, Feb 23, 2007
    #2
    1. Advertising

  3. kmsterrett

    kmsterrett Guest

    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];


    On Feb 23, 10:18 am, "Andrew Thompson" <> wrote:
    > On Feb 24, 2:14 am, "kmsterrett" <> wrote:
    > ..
    >
    > > Does anyone know of a way for me to speed this up?

    >
    > Post an SSCCE?
    >
    > > I will need towriteas many of 50-60 files ..

    >
    > Where is the loop in your code?
    >
    > Andrew T.
     
    kmsterrett, Feb 23, 2007
    #3
  4. On Feb 24, 2:57 am, "kmsterrett" <> wrote:

    > The loop ..


    What loop?
    <http://www.physci.org/codes/javafaq.html#netiquette>

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

    > > Post an SSCCE?


    <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.
     
    Andrew Thompson, Feb 23, 2007
    #4
  5. kmsterrett wrote:
    > 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?

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Feb 24, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tiewknvc9

    writing a "png" file with imageio

    tiewknvc9, Apr 4, 2006, in forum: Java
    Replies:
    3
    Views:
    703
    Oliver Wong
    Apr 4, 2006
  2. Replies:
    0
    Views:
    490
  3. Juls
    Replies:
    0
    Views:
    2,739
  4. Roedy Green
    Replies:
    4
    Views:
    552
    Tim Slattery
    Mar 18, 2010
  5. Weng Lei-QCH1840
    Replies:
    1
    Views:
    198
    Thomas
    Aug 15, 2003
Loading...

Share This Page