Re: Improving large image drawing

From: ak (spam_at_imagero.com)
Date: 02/10/04


Date: Tue, 10 Feb 2004 15:47:45 +0100


> I'm trying to draw a very large image with Swing.
> My input is an int array, that I would like to display as an image.
> (each int is a RGB value)
> The array is quite large, and I want to avoid creating a temporary
> image to draw to the offscreen buffer of a jpanel...

for large images you should use JAI.

another way is NOT to create one big image from array, but only part it.

something like this (simplified):

int tileSize = 200;
MemoryImageSource mis;
Image img;

public MyPanel(int w, int h, int[] pixels) {
    mis = new MemoryImageSource(tileSize, tileSize , pixels, 0, w);
    mis.setAnimated(true);
    img = Toolkit.getDefaultToolkit().createImage(mis);
}

//for repainting you should make loop over all tiles
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    int tile = 0;
    int xTileCount = w/ tileSize;
    int yTileCount = w/ tileSize;

    for(int i = 0; i< xTileCount; i++) {
        for(int j = 0; j < yTileCount; j++) {
            int offset = j * tileSize * w + i * tileSize;
            mis.newPixels(pixels, offset, <ColorModel>, offset, w);
            //paint img now;
            g.drawImage(img, i*tileSize, j*tileSize, null);
        }
    }
}

--
____________
http://reader.imagero.com the best java image reader.
"oneforall-allforone" <first.contact@netcourrier.com> schrieb im Newsbeitrag
news:b490297f.0402100554.52e05df8@posting.google.com...
> Hi.
>
> I'm trying to draw a very large image with Swing.
> My input is an int array, that I would like to display as an image.
> (each int is a RGB value)
> The array is quite large, and I want to avoid creating a temporary
> image
> to draw to the offscreen buffer of a jpanel...
> Here is the code I want to improve:
> -----------------------------------------------------------
> final class MyPanel extends JPanel {
>
> private final MemoryImageSource mis;
> private final FilteredImageSource fis;
> private final AreaAveragingScaleFilter aas = new
> AreaAveragingScaleFilter(100, 100);
>
> // this is the constructor for my class.
> // w is the image desired width
> // h is the image desired height
> // pixels is a w*h array with an rgb value.
> public MyPanel(int w, int h, int[] pixels) {
> // MemoryImageSource is a producer that feeds each
> // of it's registered consumer with values of 'pixels'
> mis = new MemoryImageSource(w, h, pixels, 0, w);
> }
>
> /**
> * @param g
> * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
> */
> protected void paintComponent(Graphics g) {
> super.paintComponent(g);
> Graphics2D g2 = (Graphics2D) g;
> // The method create image creates a temporary image
> // which is drawn to the offscreen buffer of this panel.
> g2.drawImage(getToolkit().createImage(mis), 0, 0, Color.BLACK,null);
> }
> }
> -----------------------------------------------------------
>
> This is not optimal, since the tempo image creation leads to an
> OutOfMemory when
> the pixels array is big.
>
> To solve that problem, I would like to bypass the creation of a tempo
> image.
> This could be done if I had the offscreen image for the instance of
> MyPanel.
> I tryed using the RepaintManager, but w/o the results I hopped.
> So if sbdy has any idea, I would greatly approciate.


Relevant Pages

  • (patch for Bash) regex case statement
    ... Following up on my previous patch for regex conditional tests, ... /* Return an array of strings; ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: Strategy or Iterator?
    ... It would be possible to write a class that returns the variations ... GNU General Public License for more details. ... protected CombinatoricOperator(Telements, int r) { ... An integer array backing up the original one to keep track of the ...
    (comp.lang.java.programmer)
  • (patch for Bash) regex conditional tests
    ... 'regex' are returned in array variable SUBMATCH. ... Skipping of positional parameters, array elements, string ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: CPoint Help
    ... >I cannot get my CPoint pointarray to draw lines based on the criteria ... > int y = 0; ... > dResult = sin; ...
    (microsoft.public.vc.mfc)
  • Re: Warning on assigning a function-returning-a-pointer-to-arrays
    ... This declares pfunc as a function taking no arguments and returning ... int x, y; ... Presumably pfuncwill return a pointer to a single int, ... or the first of a sequence of "array 5 of int"s. ...
    (comp.lang.c)