Blame view

src/jsky/image/ImageUtil.java 4.19 KB
fe0fb87e   Elodie Bourrec   First push to cre...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * ESO Archive
 *
 * $Id: ImageUtil.java,v 1.2 2009/02/20 23:10:11 abrighto Exp $
 *
 * who             when        what
 * --------------  ----------  ----------------------------------------
 * Allan Brighton  1999/12/06  Created
 */

package jsky.image;

import java.lang.*;
import java.awt.*;
import java.awt.image.*;
import javax.media.jai.*;


/**
 * Contains static convenience and utility methods
 * for dealing with JAI images.
 *
 * @author Allan Brighton
 * @version $Revision: 1.2 $
 */
public class ImageUtil {

    /**
     * Return the value of the given property for the given image as a double,
     * or return the given default value, if the property was not defined.
     */
    public static double getImageProperty(PlanarImage im, String name, double defaultValue) {
        Object o = im.getProperty(name);
        if (o != null && o != java.awt.Image.UndefinedProperty) {
            return Double.parseDouble(o.toString());
        }
        return defaultValue;
    }

    /**
     * Return the value of the given property for the given image as an int,
     * or return the given default value, if the property was not defined.
     */
    public static int getImageProperty(PlanarImage im, String name, int defaultValue) {
        Object o = im.getProperty(name);
        if (o != null && o != java.awt.Image.UndefinedProperty) {
            return Integer.parseInt(o.toString());
        }
        return defaultValue;
    }

    /**
     * Return the value of the given property for the given image as a String,
     * or return the given default value, if the property was not defined.
     */
    public static String getImageProperty(PlanarImage im, String name, String defaultValue) {
        Object o = im.getProperty(name);
        if (o != null && o != java.awt.Image.UndefinedProperty) {
            //System.out.println("XXX " + name + " = " + o.toString());
            return o.toString();
        }
        return defaultValue;
    }


    /**
     * Return a RenderingHints object defining a tile cache with the given number of tiles
     * of the given size. This can be passed to JAI.create(...) to specify the tile cache
     * for a given operation.
     */
    public static RenderingHints getTileCacheHint(int numTiles, int tileWidth, int tileHeight) {
        // from: Lincoln Perry <lincoln.perry@ENG.SUN.COM>:
        // Set the destination image tile size in an ImageLayout
        // and add to a RenderingHint.
        // Having smaller destination tiles for zooms is usually a
        // good idea, since it minimizes source data cobbling and "wasted"
        // space in partially-filled destination tiles.
        //
        ImageLayout layout = new ImageLayout();
        layout.setTileWidth(tileWidth);
        layout.setTileHeight(tileHeight);
        RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);

        // Create our own tile cache for this OpImage's output tiles only
        // XXX JAI 1.0.2 TileCache tileCache = JAI.createTileCache(numTiles, (long)(numTiles*tileWidth*tileHeight));
        TileCache tileCache = JAI.createTileCache((long) (numTiles * tileWidth * tileHeight));
        hints.put(JAI.KEY_TILE_CACHE, tileCache);
        return hints;
    }


    /**
     * Return a RenderingHints object defining the sample model (data type) of the resulting
     * image of an operation.
     */
    public static RenderingHints getSampleModelHint(int tileWidth, int tileHeight, int dataType) {
        int[] bandOffsets = new int[1];
        bandOffsets[0] = 0;
        int pixelStride = 1;
        SampleModel sampleModel = RasterFactory.createPixelInterleavedSampleModel(dataType,
                tileWidth, tileHeight,
                pixelStride,
                tileWidth,
                bandOffsets);
        ImageLayout layout = new ImageLayout();
        layout.setTileWidth(tileWidth);
        layout.setTileHeight(tileHeight);
        layout.setSampleModel(sampleModel);

        // Create our own tile cache for this OpImage's output tiles only
        //TileCache tileCache = JAI.createTileCache(numTiles, (long)(numTiles*tileWidth*tileHeight));
        //hints.put(JAI.KEY_TILE_CACHE, tileCache);

        return new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
    }
}