×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Regular Contributor
    Join Date
    May 2003
    Posts
    151

    Image created from Inputstream slooooow

    Hi,

    Recently I packed all my data files together to save space in the Jar (and boy did it save a lot of space, like 30%!) and changed the Image creation routine to read in the image and then create the image from the bytes in memory.

    This works but is slow as molasses on the 7210 - I'm talking like 5 seconds to create each image.

    Am I doing anything wrong ? or can anyone suggest a better way of doing this ?

    TIA

    Dan



    public Image CreateImage( int nResourceID )
    {
    Image newImage;

    int nLength, nOffset;

    try
    {
    InputStream inputStream = getClass().getResourceAsStream( "/pak.dat" );

    nLength = FileResource.FileInfo[ 2 * nResourceID ];
    nOffset = FileResource.FileInfo[ (2 * nResourceID) + 1 ];

    inputStream.skip( nOffset );
    inputStream.read( fileReadBuffer, 0, nLength );

    inputStream.close();

    newImage = Image.createImage( fileReadBuffer, 0, nLength );

    return newImage;
    }
    catch( Exception e )
    {
    System.err.println( "CreateImage went bad\n" );
    System.err.println( e.toString() );

    try
    {
    Thread.sleep( 5000 );
    }
    catch(Exception eNew)
    {
    }
    }

    return null;
    }

  2. #2
    Registered User
    Join Date
    Jul 2003
    Location
    Finland, Tampere
    Posts
    1,113
    Why don't you use Image.createImage(String name) method?
    As I understood you use standard PNG images

  3. #3
    Regular Contributor
    Join Date
    May 2003
    Posts
    151
    Because when you have separate files in a Jar there is an overhead of something like 250 bytes per file.


    When I packed all my files together the Jar size went from 70k (too big to fit on Series 40) to 50k. Which is a nice saving....

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    Dan,

    This technique is especially effective with 256 colour images that all have the same palette - the palette takes an additional 768 bytes in the png, and compression will remove all except one.

    I think your code is slow because you are re-reading the file for each image. You cannot randomly-access data in a jar, so skip() has to read all the data upto that point, unzipping as it goes. So you are repeatedly unzipping the same data. Can you load the entire binary file into memory, then extract the icons using:

    newImage = Image.createImage( fileReadBuffer, nOffset, nLength );

    ? That way you only unzip the data once. Also, you create one big piece of garbage rather than many smaller ones.

    Graham.

  5. #5
    Regular Contributor
    Join Date
    May 2003
    Posts
    151
    >I think your code is slow because you are re-reading the file for
    >each image. You cannot randomly-access data in a jar, so skip() >has to read all the data upto that point, unzipping as it goes.

    Of course - I'd forgotten that the packed data file was also compressed and so needs to be decompressed before the data can be read out of it.


    >Can you load the entire binary file into memory, then extract the icons using:

    Not quite - I've got too much data to load in at once, but I'm sure I can split the packed file into the different chunks that will be needed at the same time and load those bits into memory - which should be just as good

    cheers for the help
    dan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×