10 Replies Latest reply: Apr 23, 2010 4:40 AM by 843789 RSS

    Streaming an image file (and rendering simultaneously)

    843789
      Hi, my first post here. I'm new to Java gui. I am trying to create an applet that loads a big image file (>10000kb) from a url and simultaneously paints it on the screen bit by bit (before it is entirely loaded). In other words, the effect I want is the same as when loading a huge image file from a browser by which the image is rendered line by line. I would appreciate any help regarding this issue and where I shall start looking at
        • 1. Re: Streaming an image file (and rendering simultaneously)
          843789
          You need a decoder for the file format that you are going to support--one that you can run each buffer full of data through and decode--so you can dispaly it in image object.

          You have to create the entire image object, then paint in each sequential block of data you fetch from your stream.
          • 2. Re: Streaming an image file (and rendering simultaneously)
            843789
            Thanks for you reply. Although I still I don't have any idea of what to do i'll start by searching some keywords that you mentioned. Should someone feels like eleborating in more simpler terms, I would appreciate the extra help.
            • 3. Re: Streaming an image file (and rendering simultaneously)
              843789
              I am dealing with JPEG images. It seems that what I'm trying to achieve in the manner you explained is impossible to carry out as JAVA's built-in JPEG decoder can't decode images progressively. I trid to find some opensource decoder but had no luck yet
              • 4. Re: Streaming an image file (and rendering simultaneously)
                843789
                Did you even read the post I made in your cross-post here? [http://www.java-forums.org/new-java/28016-streaming-image-byte-byte-similtaneosly-rendering-screen.html]

                Did you look at the link I provided? It's quite detailed and useful. Also acknowledging a post can actually encourage folks to help you more.
                • 5. Re: Streaming an image file (and rendering simultaneously)
                  843789
                  Sorry for my late reply. Actually I have managed to decode a partial image and feed it to a BufferedImage by following the documentation Java Image I/O API. However the source object used for this method was a file stored on my local machine. Now I want to apply the partial rendering technique by progessively streaming a huge jpeg hosted remotely on the web by using the the method mention earlier on by morgalr i.e. stream some bytes, decode byes, render partial image - stream some bytes, decode, render another partial image. May be it is a trivial task for the advanced programmer but as a beginner I'm finding it quite hard to accomplish. However I'm not going to give up as yet since it is also a learning experience. thanks your assistance
                  • 6. Re: Streaming an image file (and rendering simultaneously)
                    843789
                    gidra,

                    I went out and looked over the algo's for JPEG to refresh my memory on what it intails... oh, shades of nightmares gone by, and had a flash back to actually coding that up several years back. It is definetley not a trivial case. You may want to look into the tile load option of the ImageIO API.

                    Les
                    • 7. Re: Streaming an image file (and rendering simultaneously)
                      843789
                      I thoroughly agree with the "definitely not a trival case" bit as I have now been all over the net these last three days examining APIs and what not. I also tried to play around with the Incremental Display in this SWT tutorial http://book.javanb.com/swt-the-standard-widget-toolkit/ch19lev1sec4.html but still cannot achieve the results I want. To make sure we're on the right track here is a visual representation of what I'm trying to achieve http://codinghorror.typepad.com/.a/6a0120a85dcdae970b0128776fcab6970c-pi I shall try and take a deeper look at the ImagIO API tiling. I currently don't have the know how of writing an algo myself since I'm relatively new to JAVA (and to the programming world at large), so if there aren't available API of doing this I shall give up.
                      • 8. Re: Streaming an image file (and rendering simultaneously)
                        843789
                        Yep, the problem you are going to run into is that Java does not hand off a reference to the BufferedImage that the ImageIO is loading into, if id did, you'd have a very easy time of doing this.
                        • 9. Re: Streaming an image file (and rendering simultaneously)
                          843789
                          Just found this thread http://forums.sun.com/thread.jspa?threadID=5385548&start=0&tstart=0 Seems to carry quite some interesting bits of info. Will play around some more and update later. Thanks for your input morgair (regarding the reference part, I think there must be some ulterior reason why the ImageIO doesn't return intermediate data as it loads. Whatever the reason, I agree that it is the main barrier to solving this problem)
                          • 10. Re: Streaming an image file (and rendering simultaneously)
                            843789
                            I finally managed to render an image in intermediate steps using this code :
                                      for (int i=0; i<64; i++){
                                           //set partial image size
                                           param.setSourceRegion(new Rectangle (0,i*incrSize,imgWidth,incrSize));
                                           
                                           try {
                                                bi = reader.read(0, param);
                            
                                           } 
                                           catch (IOException e) {
                                                e.printStackTrace();
                                           }
                            where param is an ImageReadParam, reader is an ImageReader and bi is BufferedImage. I know it is not the most practical solution but it is the only one available at the moment and up to my knowledge. What is especially annoying is that if I decide to decrease the heights of the rectangles and increase the iteration (right now I just set it to 64), as the image rendering gets closer to the bottom, each iteration increases in time which results in a great delay on bigger files .