9 Replies Latest reply: Mar 25, 2009 6:07 AM by masijade RSS

    Graphics <--> int[] array

    807588
      hello NG!

      i am working on a class that manipulates graphics (independend from swt or swing)
      therefore i pass the data as array to my class (int[] color values)
      internal i want to use Graphics (this way i could easily draw lines etc)

      i did not find a way (already tried google) to convert an array to graphics and vice versa...

      note: i already implemented some image filters, which work pretty well with even more than 10000 elements moving @ high fps but for further gimmicks in the filters i could use the graphics functions instead of reinventing the wheel and implementing drawLine functions for array-manipulation...

      thanks in advance
      lumo
        • 1. Re: Graphics <--> int[] array
          masijade
          Take a look at the API docs for the BufferedImage class.
          • 2. Re: Graphics <--> int[] array
            807588
            ok, this way i get
                      BufferedImage bi = new BufferedImage(width, array.length / width,
                                BufferedImage.TYPE_INT_ARGB);
                      return bi.getGraphics();
            from array to graphics, but the other way is still open

            second part:
            getRGBArray()

            THANKS
            • 3. Re: Graphics <--> int[] array
              masijade
              luke2000@gmx.at wrote:
              second part:
              getRGBArray()
              See the API docs for ImageIO (to create a BufferedImage from a Stream).
              Then take another look at BufferedImage.
              • 4. Re: Graphics <--> int[] array
                807588
                why do i want to create an buffered image from a stream?
                note: getrgb drops Alpha
                • 5. Re: Graphics <--> int[] array
                  masijade
                  luke2000@gmx.at wrote:
                  why do i want to create an buffered image from a stream?
                  I assume you are reading your image from somewhere, right? A file, a db, a socket?
                  And, if you drawing your image, create a BufferedImage first, then use that Graphics2D to draw it, then you can still call getRGB after drawing.
                  note: getrgb drops Alpha
                  Also, to tell you the truth, I have no idea why you feel the need to pass the data around as a color array, anyway. Why are you not simply passing around the actual Image, or Graphics.
                  • 6. Re: Graphics <--> int[] array
                    807588
                    my image is already loaded on startup and passed to my panel (which can be implemented in swing or swt)

                    why array ? -> cause i use my filters as "plugins" for a panel (implemented in SWT and Swing)
                    and swt and swing use different Image constructs (i was not able to transfer one into the other - have to admit that i did not really tried it..)
                    i am passing the array to my plugin, return the array and simply draw the graphics ONCE on the panel (which is way faster than drawing thousand times image on image...)
                    • 7. Re: Graphics <--> int[] array
                      masijade
                      You are better off working with the actual images. If they are subclasses of Image, you can use getGraphics directly, if they are not a subclass of Image, then, almost guaranteed they have a way of getting an Image based class from it, with which, you can, once again, call getGraphics.


                      And, since it is probably with Graphics with which it is to be modified, that seems the better way.
                      • 8. Re: Graphics <--> int[] array
                        807588
                        yeah i know its better to work with the image itself,
                        but converting between the formats swt awt is too slow.

                        i compared the image vs my array stuff (realtime high fps)
                        cpu % with image: 67%
                        cpu % with array: 57%
                        and to note (sleep 15ms between calculation) the fps dropped (could be balanced by removing the sleep, but still the array would be faster)
                        (i do not have a really fast computer here so therefore the high precentages are ok...)

                        Edited by: luke2000@gmx.at on Mar 25, 2009 2:38 AM
                        • 9. Re: Graphics <--> int[] array
                          masijade
                          cpu% is not speed, so I don't know what you're getting at. Did you actually measure times (including time to convert from image to array and back to array to image as that time will also come into play, of course)?