Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    puting a png image with other image data in the file

    Hey all, I would like to implement something a bit complicated but I'm not sure if the Image.CreateImage(InputStream) can handle it.

    I have an Image which is made of many smaller Images which are the Images of the entire application. now it is not completely necessary I could save the data in one file and the Image in another, but as a principle I would like to put them both in one file as such: Data chunk, Data chunk....., Data chunk, Image byteArray of an Image on the computer, Data chunk, Data chunk....., Data chunk, Image byteArray of another Image on the computer.

    do you think it could read it if when the stream gets to the Image and I would use the Image.CreateImage(InputStream) would I get the desired result?
    Thanks,
    Adam Zehavi.

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

    Re: puting a png image with other image data in the file

    If I understand you right, you're talking about the same thing I do with fonts - packing the PNG data stream, and other data (like characters widths, the list of characters in the font, and so on) into a single file.

    Never tried this wit createImage(InputStream)... I avoid that method, as it's MIDP-2 only, and I can live without it easily enough. But loading the PNG portion of the file into a byte[], then createImage(byte[]) is simple enough (and basically what createImage(InputStream) would do).

    (Worth knowing: at least one LG has a bug where createImage(byte[], int, int) doesn't work if the offset into the byte[] is not zero.)

    There's sense in packing data like this, as there's a per-file overhead in the JAR. Fewer files = smaller JAR.

    Graham.

  3. #3
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Re: puting a png image with other image data in the file

    ok so if you don't use create image how do you create the image...?? and easily enough?

    with the (byte[],int offset,int length)??

    I hoped there was another way to create images I didn't know about...
    Thanks,
    Adam Zehavi.

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

    Re: puting a png image with other image data in the file

    Yes, createImage(byteArray, offset, length). You can try createImage(InputStream), it might work exactly as you want. I just have too little experience with it to tell you how reliably it will behave. It should be fine, though I'm not sure it will reliably leave the InputStream at the right point for you to read the following data. (Though, I have no reason to suggest it won't work as you want, I just treat all MIDP functionality with automatic mistrust!)

    I'd prefix the PNG stream with a file length. Read the entire stream with a DataInputStream, then you can use something like:

    PHP Code:
    int pngDataLength in.readInt();
    byte[] pngData = new byte[pngDataLength];
    in.readFully(pngData);
    Image img Image.createImage(pngData0pngDataLength); 
    Graham.

  5. #5
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Re: puting a png image with other image data in the file

    yes great that's what I had in mind too, I was thinking to go even a bit further and to hold sets of Images and data, this way I can have, lets say keyboard keys background in different sizes, if I keep them in the file with their data as a prefix I could easily make an object to retrieve the specific ones I would like to use according to the screen sizes for example.
    Thanks,
    Adam Zehavi.

  6. #6
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Re: puting a png image with other image data in the file

    do you create a PC UI to design and make the files??
    Thanks,
    Adam Zehavi.

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

    Re: puting a png image with other image data in the file

    Quote Originally Posted by TacB0sS View Post
    yes great that's what I had in mind too, I was thinking to go even a bit further and to hold sets of Images and data, this way I can have, lets say keyboard keys background in different sizes, if I keep them in the file with their data as a prefix I could easily make an object to retrieve the specific ones I would like to use according to the screen sizes for example.
    It is common (especially when developing for very tight JAR limits, like 64k for old Series 40s) to pack multiple images into a single file. For example, all images for a specific game level. One thing to know: getResourceAsStream() often causes the entire file to loaded into the heap immediately. You reduce JAR size, but you can increase the heap footprint at the same time.

    Quote Originally Posted by TacB0sS View Post
    do you create a PC UI to design and make the files??
    I never have. I tend to build tools that plug into Ant, so they run as part of the build process.

    One thing about reading with DataInput is that you can build files from a J2SE app using DataOutput, and they're nice and easy to read in the MIDlet.

    Graham.

  8. #8
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Re: puting a png image with other image data in the file

    well I'm almost done with the file creator tool but I found out now that the only way I can save the image into the file is by its full int[] or byte[], so isn't this kind of loosing all the compressing privileges of using pngout for example or even a png file, isn't that so? how can I get around this, are there decoders or something like that?
    Thanks,
    Adam Zehavi.

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

    Re: puting a png image with other image data in the file

    Hmmm... are you creating an Image object, then using a PixelGrabber to get the data? You're better just to load the byte[] from the file, and keep it in that format. Or are you creating new images in the process (not just loading from a file and writing back out again)?

    To answer your question, yes, you can use the Java Advanced Imaging API (JAI) in J2SE for image processing - this includes PNG encoding, though it's not very efficient. If you're feeling more adventurous, GUMP also contains a PNG encoder (with source code), which is fairly efficient (though, less so than PNGOUT), but that supports only 8 bit paletted format.

    Graham.

  10. #10
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Re: puting a png image with other image data in the file

    I see, well I create new images. the process is, I have Images I would like to have on the phone in files that I created with an image editor, and I want to crop regions of that image to use for animations and stuff, I have a few images in the same file, which is blocking me because then I have to create new images with the cropped images, what I could do is have each image/frame in a separate file and stream it out, from the file which I would pngout first. but I don't think this is a good solution for me, not very user friendly. perhaps I could make a palette to share all the images for example, most common ones such as a transparent pixel or others, to have a code to inspect all the images for most common int pixel Value, and then to save bytes or shorts instead of ints? what do you think?
    Thanks,
    Adam Zehavi.

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

    Re: puting a png image with other image data in the file

    I understand.

    I'd still suggest you create PNGs. In fact, if you start looking for common colours, building a palatte, converting to an array of palette indexes, and so on, you've almost built a PNG encoder. A PNG just consists of a series of chunks:

    1. A standard, 8 bytes signature (always the same).

    2. A header chunk - contains stuff like width and height, and the colour format.

    3. If the image uses a palette, there's a palette chunk, basically an array of RGB triplets.

    4. If the image uses a palette with an alpha channel, then there is a transparency chunk, which acts like an alpha column for the palette table.

    5. The image data - either a list of colour (RGB or ARGB) values (for non-palette images) or a list of palette-indexes. This is compressed, for which you can use a DeflaterOutputStream in J2SE.

    6. The last chunk is a special "end" chunk, which contains no data, it just marks the end of the file.

    Apart from the signature, all the chunks follow a specific format, with a size, a type (four characters, "IHDR", "PLTE", "tRNS", "IDAT" or "IEND" respectively - yes, the "t" in "tRNS" has to be lower case), the data itself and a CRC.

    The format specification is here. You can find one Java-implemented encoder here.

    Otherwise, you'll end up having to load large arrays on the device (and possibly expand them into large int[] from which you can create an Image), which is going to threaten your heap usage.

    Graham.

  12. #12
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Re: puting a png image with other image data in the file

    hey Graham,
    thank you for all this, when I read the post the first time I was wondering how do you knew in such details, about the png, then I saw it was your implementation, nice.

    so after taking it all into consideration I have two options, as far as I see, one would be to save each Image by itself with its details, kind like saving multiple single png images in one file, and read them one by one, I'm not sure if its a waste, I mean what if the image size is 17x17 or 20x20, wouldn't the palette be more efficient if the image was bigger?

    the other option is to put all the images in one png file and define a cropper file with the data on where to crop the images from the bigger image.

    what do you think?
    Thanks,
    Adam Zehavi.

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

    Re: puting a png image with other image data in the file

    Ah, well, when you've tried to squeeze games into 64k, for MIDP-1 devices that can't rotate or flip images, you learn a few tricks with PNGs...

    Small images are always less efficient. Partly because of the overhead in the file (the header and end chunks), and because of the overhead in the JAR (which stores information about each file it contains). Large files also compress better.

    Two main ways of making fewer, larger files.

    1. Combine small images into one larger image (like the "film strip" technique).

    2. Simply concatenate files together, inserting some kind of information to help you separate them again later, such as the length of each part. You can do this for non-image data as well as images.

    The latter tends to be memory-hungry, since many devices will load the entire file as soon as it's opened. However, it can create good opportunities for data compression in the JAR. Remember that compression in the JAR works by identifying repeated sequences of bytes, so if you concatenate two PNG files that contain identical palettes, the JAR will store only one copy of the palette, reconstructing the second copy when the file is decompressed.

    Both techniques can reduce your ability to manage memory - it's important to combine units of data that you want to use all at the same time. For example, all the frames of the same animation, all the images for a single game level, etc. Otherwise you'll end up loading stuff you don't need.

    Combining images is easiest when all the component-images are the same size and shape, and you simply fit them into a grid. You can also combine many, differently-shaped images into one, and have some list of (x,y,width,height) data for each component (probably in a separate file, though you could combine it). This is common for more advanced animation. If you feel really adventurous, you could add this information into the PNG, as a "custom" data chunk (the PNG format is designed to be extensible), you just have to make sure that PNGOUT doesn't rip your custom chunk back out again! A big part of what PNGOUT does is to remove chunks other than the essential ones I listed previously.

    Graham.

  14. #14
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Re: puting a png image with other image data in the file

    well then, first thanks again for all the info.
    I was thinking about all this and concluded I would use the last option you described, but I would add the data in the same file as the image, as a separate data chunk that I would add after I will run the file through pngout, this would result the most easy to make yet efficient solution.

    I'm going to get right on it.

    Thanks again.
    Thanks,
    Adam Zehavi.

  15. #15
    Regular Contributor
    Join Date
    Jun 2009
    Location
    Tel-Aviv Israel
    Posts
    410

    Re: puting a png image with other image data in the file

    you know, moreover I think this makes my platform more flexible comparing to other methods we discussed, for example if I would like to have an update option, that people might download new animation to represent the hero for example, they might want a new avatar, this way the end user may download a new animation file and the only thing I must do is add this file to a list of appearance option the user can choose and he may change it.

    yes I think this is the bast solution for me.
    Thanks.
    Thanks,
    Adam Zehavi.

Similar Threads

  1. Creating image file from bitmap.
    By mahavirjain in forum Symbian
    Replies: 3
    Last Post: 2006-06-13, 06:48
  2. Replies: 1
    Last Post: 2005-05-26, 14:22
  3. write and read a TInt data from the file
    By billqu in forum Symbian
    Replies: 2
    Last Post: 2004-12-27, 16:10
  4. Loading Image data from 'raw' bytes
    By LongSteve in forum Mobile Java General
    Replies: 2
    Last Post: 2002-11-20, 18:38
  5. Replies: 0
    Last Post: 2002-06-10, 12:24

Posting Permissions

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