11.4. Sample Models

A raster's sample model knows how to pull pixel samples out of a data buffer. In the 2D API, sample models are represented by java.awt.image.SampleModel and its subclasses.

The SampleModel provides many methods for setting and retrieving pixel sample values. These methods are nearly identical to the methods you've already seen in the Raster and WritableRaster classes, except that the methods in SampleModel also accept a DataBuffer that will be used for storing or retrieving the sample data. Because the methods are so similar, I won't list them all here.

Like a Raster, a SampleModel has both a data type and a transfer type. The data type is the type of array contained in the matching DataBuffer. The transfer type is a type that is used for moving large amounts of pixel data from one place to another. Refer back to the section on rasters for more information.

SampleModel has three subclasses that represent common methods of storing image data.

11.4.1. ComponentSampleModel

This class represents a sample model where each pixel sample is stored in a separate array element. It has two subclasses, BandedSampleModel and Pixel-InterleavedSampleModel.

11.4.1.1. BandedSampleModel

This class is suitable for use with banded data in a DataBuffer. To create a BandedSampleModel, use one of its constructors:

public BandedSampleModel(int dataType, int w, int h, int numBands)

This constructor creates a BandedSampleModel suitable for use with the given data type, width, height, ...

Get Java 2D Graphics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.