10 Replies Latest reply on Jul 3, 2001 1:56 AM by 807556

    image manipulation

    807556
      Hi all,
      Looking for classes that enable image manipulation.
      I am trying to write an applet that displayes an image and and gives the user the option of changing the image brightness.
      I tried looking in to the graphics2D, but it was way to compicated for me.
      Is there anything simpler?
      Are there prepared classes to deal with these kind of image operation?
      Thanks.
      Dan.
        • 1. Re: image manipulation
          798701
          RGBImageFilter is one option. For instance the following class can be used to get a grayscale version of an image:
          import java.awt.image.*;
          
          class GrayFilter extends RGBImageFilter {
              public GrayFilter() {
               // The filter's operation does not depend on the
               // pixel's location, so IndexColorModels can be
               // filtered directly.
               canFilterIndexColorModel = true;
               }
          
              public int filterRGB(int x, int y, int rgb) {
                       // find out the red, green and blue color components
               int r = (rgb >> 16) & 0xff; 
               int g = (rgb >> 8)  & 0xff;
               int b = (rgb)       & 0xff;
          
                       // calculate the grayscale value
               int gray = (r*30 + g*59 + b*11)/100; 
          
                       // return the color code of the new color
               return (rgb & 0xff000000) | (gray<<16) | (gray<<8) | (gray);
              }
          }
          This class can be used with code like:

          Image src = getImage(getDocumentBase(), getParameter("source"));
          RGBImageFilter filter = new GrayFilter();
          Image new = createImage(new FilteredImageSource(src.getSource(), filter));

          You just need to implement the filterRGB method differently.
          • 2. Re: image manipulation
            807556
            Thanks,
            But How do I implement the "filterRGB()" function so it makes an image brighter or darker?
            Dan.
            • 3. Re: image manipulation
              798701
              You need to increment (decrement) the rgb-values in some way: eg. with multiplying them with a number > 1 (<1) or adding (substracting) some constant:
                  public int filterRGB(int x, int y, int rgb) {
                           // find out the red, green and blue color components
                   int r = (rgb >> 16) & 0xff; 
                   int g = (rgb >> 8)  & 0xff;
                   int b = (rgb)       & 0xff;
              
                           // brighten the color
                   r = (int) (r*1.5);
                   g = (int) (g*1.5);
                   b = (int) (b*1.5);
              
                   // clamp the values
                   if (r > 255) r = 255;
                   if (g > 255) g = 255;
                   if (b > 255) b = 255;
              
                           // return the color code of the new color
                   return (rgb & 0xff000000) | (r<<16) | (g<<8) | (b);
                  }
              • 4. Re: image manipulation
                807556
                Jsalonen,
                It looks exactly what I�m looking for.
                But how do I convert all pixels of the image to the desired pixel, using your function?
                It seems I would have to get the bit representation of each pixel, convert it using your function and then redraw the new image?
                Am I right?
                If so, how do I do this?
                Thanks.
                Dan.
                • 5. Re: image manipulation
                  798701
                  FilteredImageSource does it for you:
                  Image src = getImage(getDocumentBase(), getParameter("source"));
                  RGBImageFilter filter = new GrayFilter();
                  // create a new, filtered image
                  Image new = createImage(new FilteredImageSource(src.getSource(), filter));

                  For the record:
                  If you could use J2D, there are readymade classes to do this on BufferedImages, eg:
                  RescaleOp rop = new RescaleOp(1.5f, 1.0f, null);
                  BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                  rop.filter(src, dest);

                  But if you are writing an applet that will be run in a normal browser without the plugin this will be of no use.
                  • 6. Re: image manipulation
                    807556
                    It seems to work but the image is behaving strangely.
                    When I try and darken or brighten the image, I sometimes see the filter applied on only half the image.
                    Sometimes half the image seems to disappear.
                    This is the code I wrote:
                    --------------------------
                    public int filterRGB(int x, int y, int rgb)
                    {
                    // find out the red, green and blue color components
                    int r = (rgb >> 16) & 0xff;
                    int g = (rgb >> 8) & 0xff;
                    int b = (rgb) & 0xff;
                    // brighten the color
                    r = (int) (r*contrast);
                    g = (int) (g*contrast);
                    b = (int) (b*contrast);
                    // clamp the values
                    if (r > 255) r = 255;
                    if (g > 255) g = 255;
                    if (b > 255) b = 255;
                    // return the color code of the new color
                    return (rgb & 0xff000000) | (r<<16) | (g<<8) | (b);
                    }
                    --------------------------
                    //contrast is either 1.1 for brightening or 0.9 for darkening.

                    Do you have any idea why these strange affects are taking place?

                    Thanks.
                    Dan.
                    • 7. Re: image manipulation
                      798701
                      Sounds like the image isn't fully loaded. See java.awt.MediaTracker. Which part of the image is problematic, the top or the bottom?
                      • 8. Re: image manipulation
                        807556
                        I am using the media tracker to load the image:
                        --------------------
                        public void setPicture(Dimension Res,URL images_dir)
                             {
                                  this.ImageRes=Res;
                                       app.image = app.getToolkit().getImage(images_dir);
                                       MediaTracker tracker = new MediaTracker(this);
                                       tracker.addImage(app.image,0);
                                       try { tracker.waitForID(0);}
                                  catch (InterruptedException e) { ; }
                                       setImageDimentiones();
                             }
                        ------------------------
                        It is not quite clear which part is having the problem,although it seems like the bottom half is the problematic one.
                        when I brighten the image only the top half is affected (gets brighter).
                        after brightening the image a few times and scrolling the image (which is in a scrollpanel) the bottom half dissapears alltogether and I left with half an image!?!
                        But if I keep brightening it the whole image appears suddenly.
                        If I try darkening the image from this point, the same affect happens.
                        I forgot to mention I am using jpg images - if that makes any difference...

                        Thanks for your help.
                        Dan
                        • 9. Re: image manipulation
                          798701
                          How about the code that draws the image? You have written a compoent class that draws an image in its paint method so that it can be shown in a scroll pane - what's it like?
                          • 10. Re: image manipulation
                            807556
                            jsalonen,
                            First, Thanks for all your help.
                            I managed it finally, but not quite....
                            I needed to use the media tracker as you suggested.
                            The problem is that every time I press the dark or bright button, the image is reloaded from the server - somthing I am not interested in...
                            I am not shure which command is responsible for this, and if it can be changed so that the filter will be applyed on the local image and not a new one from the server...
                            This is my code...
                            -------------
                                 RGBImageFilter filter = new imageFilter(contrast);//my brighten or darken filter
                                 // create a new, filtered image
                                      ImageProducer newimg=new FilteredImageSource(image.getSource(), filter);
                            //unless the image is the source image,flush the image to save memory
                                      if(!app.image.equals(app.SrcImage))
                                      {
                                           image.flush();
                                           System.gc();
                                      }
                                      
                                 app.image =app.createImage(newimg);
                                 
                                      MediaTracker tracker = new MediaTracker(app.PicutrePanel);
                                           tracker.addImage(app.image,0);
                                           try { tracker.waitForID(0);}
                                      catch (InterruptedException e) { ; }
                                           
                                           app.PicutrePanel.setImageDimentiones();
                                           app.PicutrePanel.repaint();
                                      }
                            -------------


                            Is it reloading because of the flush() function?
                            Or mabey the image.getSource is reloading it?

                            Thanks.
                            Dan