Example usage for java.awt.image SampleModel createCompatibleSampleModel

List of usage examples for java.awt.image SampleModel createCompatibleSampleModel

Introduction

In this page you can find the example usage for java.awt.image SampleModel createCompatibleSampleModel.

Prototype

public abstract SampleModel createCompatibleSampleModel(int w, int h);

Source Link

Document

Creates a SampleModel which describes data in this SampleModel's format, but with a different width and height.

Usage

From source file:org.geotools.gce.imagemosaic.Utils.java

/**
 * Store a sample image from which we can derive the default SM and CM
 * //w  w w .  j a va  2 s  . co  m
 * @param sampleImageFile
 *            where we should store the image
 * @param defaultSM
 *            the {@link SampleModel} for the sample image.
 * @param defaultCM
 *            the {@link ColorModel} for the sample image.
 * @throws IOException
 *             in case something bad occurs during writing.
 */
public static void storeSampleImage(final File sampleImageFile, final SampleModel defaultSM,
        final ColorModel defaultCM) throws IOException {
    // create 1X1 image
    final SampleModel sm = defaultSM.createCompatibleSampleModel(1, 1);
    final WritableRaster raster = RasterFactory.createWritableRaster(sm, null);
    final BufferedImage sampleImage = new BufferedImage(defaultCM, raster, false, null);

    // serialize it
    OutputStream outStream = null;
    ObjectOutputStream ooStream = null;
    SerializableRenderedImage sri = null;
    try {
        outStream = new BufferedOutputStream(new FileOutputStream(sampleImageFile));
        ooStream = new ObjectOutputStream(outStream);
        sri = new SerializableRenderedImage(sampleImage, true);
        ooStream.writeObject(sri);
    } finally {
        try {
            if (ooStream != null)
                ooStream.close();
        } catch (Throwable e) {
            IOUtils.closeQuietly(ooStream);
        }
        try {
            if (outStream != null)
                outStream.close();
        } catch (Throwable e) {
            IOUtils.closeQuietly(outStream);
        }
        try {
            if (sri != null)
                sri.dispose();
        } catch (Throwable e) {
        }
    }
}