public class

JPEGImageReader

extends ImageReader
java.lang.Object
   ↳ javax.imageio.ImageReader
     ↳ com.sun.imageio.plugins.jpeg.JPEGImageReader

Summary

Constants
int WARNING_IGNORE_INVALID_ICC Warning code to be passed to warningOccurred to indicate that embedded ICC profile is invalid and will be ignored.
int WARNING_NO_EOI Warning code to be passed to warningOccurred to indicate that the EOI marker is missing from the end of the stream.
int WARNING_NO_JFIF_IN_THUMB Warning code to be passed to warningOccurred to indicate that a JFIF segment was encountered inside a JFXX JPEG thumbnail and is being ignored.
[Expand]
Inherited Fields
From class javax.imageio.ImageReader
Public Constructors
JPEGImageReader(ImageReaderSpi originator)
Public Methods
void abort()
Requests that any current read operation be aborted.
boolean canReadRaster()
Returns true if this plug-in supports reading just a Raster of pixel data.
void dispose()
Allows any resources held by this object to be released.
ImageReadParam getDefaultReadParam()
Returns a default ImageReadParam object appropriate for this format.
int getHeight(int imageIndex)
Returns the height in pixels of the given image within the input source.
IIOMetadata getImageMetadata(int imageIndex)
Returns an IIOMetadata object containing metadata associated with the given image, or null if the reader does not support reading metadata, is set to ignore metadata, or if no metadata is available.
Iterator getImageTypes(int imageIndex)
Returns an Iterator containing possible image types to which the given image may be decoded, in the form of ImageTypeSpecifierss.
int getNumImages(boolean allowSearch)
Returns the number of images, not including thumbnails, available from the current input source.
int getNumThumbnails(int imageIndex)
Returns the number of thumbnail preview images associated with the given image.
ImageTypeSpecifier getRawImageType(int imageIndex)
Returns an ImageTypeSpecifier indicating the SampleModel and ColorModel which most closely represents the "raw" internal format of the image.
IIOMetadata getStreamMetadata()
Returns an IIOMetadata object representing the metadata associated with the input source as a whole (i.e., not associated with any particular image), or null if the reader does not support reading metadata, is set to ignore metadata, or if no metadata is available.
int getThumbnailHeight(int imageIndex, int thumbnailIndex)
Returns the height of the thumbnail preview image indexed by thumbnailIndex, associated with the image indexed by ImageIndex.
int getThumbnailWidth(int imageIndex, int thumbnailIndex)
Returns the width of the thumbnail preview image indexed by thumbnailIndex, associated with the image indexed by ImageIndex.
int getWidth(int imageIndex)
Returns the width in pixels of the given image within the input source.
BufferedImage read(int imageIndex, ImageReadParam param)
Reads the image indexed by imageIndex and returns it as a complete BufferedImage, using a supplied ImageReadParam.
Raster readRaster(int imageIndex, ImageReadParam param)
Returns a new Raster object containing the raw pixel data from the image stream, without any color conversion applied.
BufferedImage readThumbnail(int imageIndex, int thumbnailIndex)
Returns the thumbnail preview image indexed by thumbnailIndex, associated with the image indexed by ImageIndex as a BufferedImage.
boolean readerSupportsThumbnails()
Returns true if the image format understood by this reader supports thumbnail preview images associated with it.
void reset()
Restores the ImageReader to its initial state.
void setInput(Object input, boolean seekForwardOnly, boolean ignoreMetadata)
Sets the input source to use to the given ImageInputStream or other Object.
Protected Methods
void warningOccurred(int code)
Called by the native code or other classes to signal a warning.
void warningWithMessage(String msg)
The library has it's own error facility that emits warning messages.
[Expand]
Inherited Methods
From class javax.imageio.ImageReader
From class java.lang.Object

Constants

protected static final int WARNING_IGNORE_INVALID_ICC

Warning code to be passed to warningOccurred to indicate that embedded ICC profile is invalid and will be ignored.

Constant Value: 2 (0x00000002)

protected static final int WARNING_NO_EOI

Warning code to be passed to warningOccurred to indicate that the EOI marker is missing from the end of the stream. This usually signals that the stream is corrupted, but everything up to the last MCU should be usable.

Constant Value: 0 (0x00000000)

protected static final int WARNING_NO_JFIF_IN_THUMB

Warning code to be passed to warningOccurred to indicate that a JFIF segment was encountered inside a JFXX JPEG thumbnail and is being ignored.

Constant Value: 1 (0x00000001)

Public Constructors

public JPEGImageReader (ImageReaderSpi originator)

Public Methods

public void abort ()

Requests that any current read operation be aborted. The contents of the image following the abort will be undefined.

Readers should call clearAbortRequest at the beginning of each read operation, and poll the value of abortRequested regularly during the read.

public boolean canReadRaster ()

Returns true if this plug-in supports reading just a Raster of pixel data. If this method returns false, calls to readRaster or readTileRaster will throw an UnsupportedOperationException.

The default implementation returns false.

Returns
  • true if this plug-in supports reading raw Rasters.

public void dispose ()

Allows any resources held by this object to be released. The result of calling any other method (other than finalize) subsequent to a call to this method is undefined.

It is important for applications to call this method when they know they will no longer be using this ImageReader. Otherwise, the reader may continue to hold on to resources indefinitely.

The default implementation of this method in the superclass does nothing. Subclass implementations should ensure that all resources, especially native resources, are released.

public ImageReadParam getDefaultReadParam ()

Returns a default ImageReadParam object appropriate for this format. All subclasses should define a set of default values for all parameters and return them with this call. This method may be called before the input source is set.

The default implementation constructs and returns a new ImageReadParam object that does not allow source scaling (i.e., it returns new ImageReadParam().

Returns
  • an ImageReadParam object which may be used to control the decoding process using a set of default settings.

public int getHeight (int imageIndex)

Returns the height in pixels of the given image within the input source.

If the image can be rendered to a user-specified size, then this method returns the default height.

Parameters
imageIndex the index of the image to be queried.
Returns
  • the height of the image, as an int.
Throws
IOException

public IIOMetadata getImageMetadata (int imageIndex)

Returns an IIOMetadata object containing metadata associated with the given image, or null if the reader does not support reading metadata, is set to ignore metadata, or if no metadata is available.

Parameters
imageIndex the index of the image whose metadata is to be retrieved.
Returns
  • an IIOMetadata object, or null.
Throws
IOException

public Iterator getImageTypes (int imageIndex)

Returns an Iterator containing possible image types to which the given image may be decoded, in the form of ImageTypeSpecifierss. At least one legal image type will be returned.

The first element of the iterator should be the most "natural" type for decoding the image with as little loss as possible. For example, for a JPEG image the first entry should be an RGB image, even though the image data is stored internally in a YCbCr color space.

Parameters
imageIndex the index of the image to be retrieved.
Returns
  • an Iterator containing at least one ImageTypeSpecifier representing suggested image types for decoding the current given image.
Throws
IOException

public int getNumImages (boolean allowSearch)

Returns the number of images, not including thumbnails, available from the current input source.

Note that some image formats (such as animated GIF) do not specify how many images are present in the stream. Thus determining the number of images will require the entire stream to be scanned and may require memory for buffering. If images are to be processed in order, it may be more efficient to simply call read with increasing indices until an IndexOutOfBoundsException is thrown to indicate that no more images are available. The allowSearch parameter may be set to false to indicate that an exhaustive search is not desired; the return value will be -1 to indicate that a search is necessary. If the input has been specified with seekForwardOnly set to true, this method throws an IllegalStateException if allowSearch is set to true.

Parameters
allowSearch if true, the true number of images will be returned even if a search is required. If false, the reader may return -1 without performing the search.
Returns
  • the number of images, as an int, or -1 if allowSearch is false and a search would be required.
Throws
IOException

public int getNumThumbnails (int imageIndex)

Returns the number of thumbnail preview images associated with the given image. If the format does not support thumbnails, (readerSupportsThumbnails returns false), 0 will be returned regardless of whether an input source has been set or whether imageIndex is in bounds.

The default implementation returns 0 without checking its argument.

Parameters
imageIndex the index of the image being queried.
Returns
  • the number of thumbnails associated with the given image.
Throws
IOException

public ImageTypeSpecifier getRawImageType (int imageIndex)

Returns an ImageTypeSpecifier indicating the SampleModel and ColorModel which most closely represents the "raw" internal format of the image. For example, for a JPEG image the raw type might have a YCbCr color space even though the image would conventionally be transformed into an RGB color space prior to display. The returned value should also be included in the list of values returned by getImageTypes.

The default implementation simply returns the first entry from the list provided by getImageType.

Parameters
imageIndex the index of the image to be queried.
Returns
  • an ImageTypeSpecifier.
Throws
IOException

public IIOMetadata getStreamMetadata ()

Returns an IIOMetadata object representing the metadata associated with the input source as a whole (i.e., not associated with any particular image), or null if the reader does not support reading metadata, is set to ignore metadata, or if no metadata is available.

Returns
  • an IIOMetadata object, or null.
Throws
IOException

public int getThumbnailHeight (int imageIndex, int thumbnailIndex)

Returns the height of the thumbnail preview image indexed by thumbnailIndex, associated with the image indexed by ImageIndex.

If the reader does not support thumbnails, (readerSupportsThumbnails returns false), an UnsupportedOperationException will be thrown.

The default implementation simply returns readThumbnail(imageindex, thumbnailIndex).getHeight(). Subclasses should therefore override this method if possible in order to avoid forcing the thumbnail to be read.

Parameters
imageIndex the index of the image to be retrieved.
thumbnailIndex the index of the thumbnail to be retrieved.
Returns
  • the height of the desired thumbnail as an int.
Throws
IOException

public int getThumbnailWidth (int imageIndex, int thumbnailIndex)

Returns the width of the thumbnail preview image indexed by thumbnailIndex, associated with the image indexed by ImageIndex.

If the reader does not support thumbnails, (readerSupportsThumbnails returns false), an UnsupportedOperationException will be thrown.

The default implementation simply returns readThumbnail(imageindex, thumbnailIndex).getWidth(). Subclasses should therefore override this method if possible in order to avoid forcing the thumbnail to be read.

Parameters
imageIndex the index of the image to be retrieved.
thumbnailIndex the index of the thumbnail to be retrieved.
Returns
  • the width of the desired thumbnail as an int.
Throws
IOException

public int getWidth (int imageIndex)

Returns the width in pixels of the given image within the input source.

If the image can be rendered to a user-specified size, then this method returns the default width.

Parameters
imageIndex the index of the image to be queried.
Returns
  • the width of the image, as an int.
Throws
IOException

public BufferedImage read (int imageIndex, ImageReadParam param)

Reads the image indexed by imageIndex and returns it as a complete BufferedImage, using a supplied ImageReadParam.

The actual BufferedImage returned will be chosen using the algorithm defined by the getDestination method.

Any registered IIOReadProgressListener objects will be notified by calling their imageStarted method, followed by calls to their imageProgress method as the read progresses. Finally their imageComplete method will be called. IIOReadUpdateListener objects may be updated at other times during the read as pixels are decoded. Finally, IIOReadWarningListener objects will receive notification of any non-fatal warnings that occur during decoding.

The set of source bands to be read and destination bands to be written is determined by calling getSourceBands and getDestinationBands on the supplied ImageReadParam. If the lengths of the arrays returned by these methods differ, the set of source bands contains an index larger that the largest available source index, or the set of destination bands contains an index larger than the largest legal destination index, an IllegalArgumentException is thrown.

If the supplied ImageReadParam contains optional setting values not supported by this reader (e.g. source render size or any format-specific settings), they will be ignored.

Parameters
imageIndex the index of the image to be retrieved.
param an ImageReadParam used to control the reading process, or null.
Returns
  • the desired portion of the image as a BufferedImage.
Throws
IOException

public Raster readRaster (int imageIndex, ImageReadParam param)

Returns a new Raster object containing the raw pixel data from the image stream, without any color conversion applied. The application must determine how to interpret the pixel data by other means. Any destination or image-type parameters in the supplied ImageReadParam object are ignored, but all other parameters are used exactly as in the read method, except that any destination offset is used as a logical rather than a physical offset. The size of the returned Raster will always be that of the source region clipped to the actual image. Logical offsets in the stream itself are ignored.

This method allows formats that normally apply a color conversion, such as JPEG, and formats that do not normally have an associated colorspace, such as remote sensing or medical imaging data, to provide access to raw pixel data.

Any registered readUpdateListeners are ignored, as there is no BufferedImage, but all other listeners are called exactly as they are for the read method.

If canReadRaster() returns false, this method throws an UnsupportedOperationException.

If the supplied ImageReadParam contains optional setting values not supported by this reader (e.g. source render size or any format-specific settings), they will be ignored.

The default implementation throws an UnsupportedOperationException.

Parameters
imageIndex the index of the image to be read.
param an ImageReadParam used to control the reading process, or null.
Returns
  • the desired portion of the image as a Raster.
Throws
IOException

public BufferedImage readThumbnail (int imageIndex, int thumbnailIndex)

Returns the thumbnail preview image indexed by thumbnailIndex, associated with the image indexed by ImageIndex as a BufferedImage.

Any registered IIOReadProgressListener objects will be notified by calling their thumbnailStarted, thumbnailProgress, and thumbnailComplete methods.

If the reader does not support thumbnails, (readerSupportsThumbnails returns false), an UnsupportedOperationException will be thrown regardless of whether an input source has been set or whether the indices are in bounds.

The default implementation throws an UnsupportedOperationException.

Parameters
imageIndex the index of the image to be retrieved.
thumbnailIndex the index of the thumbnail to be retrieved.
Returns
  • the desired thumbnail as a BufferedImage.
Throws
IOException

public boolean readerSupportsThumbnails ()

Returns true if the image format understood by this reader supports thumbnail preview images associated with it. The default implementation returns false.

If this method returns false, hasThumbnails and getNumThumbnails will return false and 0, respectively, and readThumbnail will throw an UnsupportedOperationException, regardless of their arguments.

A reader that does not support thumbnails need not implement any of the thumbnail-related methods.

Returns
  • true if thumbnails are supported.

public void reset ()

Restores the ImageReader to its initial state.

The default implementation calls setInput(null, false), setLocale(null), removeAllIIOReadUpdateListeners(), removeAllIIOReadWarningListeners(), removeAllIIOReadProgressListeners(), and clearAbortRequest.

public void setInput (Object input, boolean seekForwardOnly, boolean ignoreMetadata)

Sets the input source to use to the given ImageInputStream or other Object. The input source must be set before any of the query or read methods are used. If input is null, any currently set input source will be removed. In any case, the value of minIndex will be initialized to 0.

The seekForwardOnly parameter controls whether the value returned by getMinIndex will be increased as each image (or thumbnail, or image metadata) is read. If seekForwardOnly is true, then a call to read(index) will throw an IndexOutOfBoundsException if index < this.minIndex; otherwise, the value of minIndex will be set to index. If seekForwardOnly is false, the value of minIndex will remain 0 regardless of any read operations.

The ignoreMetadata parameter, if set to true, allows the reader to disregard any metadata encountered during the read. Subsequent calls to the getStreamMetadata and getImageMetadata methods may return null, and an IIOImage returned from readAll may return null from their getMetadata method. Setting this parameter may allow the reader to work more efficiently. The reader may choose to disregard this setting and return metadata normally.

Subclasses should take care to remove any cached information based on the previous stream, such as header information or partially decoded image data.

Use of a general Object other than an ImageInputStream is intended for readers that interact directly with a capture device or imaging protocol. The set of legal classes is advertised by the reader's service provider's getInputTypes method; most readers will return a single-element array containing only ImageInputStream.class to indicate that they accept only an ImageInputStream.

The default implementation checks the input argument against the list returned by originatingProvider.getInputTypes() and fails if the argument is not an instance of one of the classes in the list. If the originating provider is set to null, the input is accepted only if it is an ImageInputStream.

Parameters
input the ImageInputStream or other Object to use for future decoding.
seekForwardOnly if true, images and metadata may only be read in ascending order from this input source.
ignoreMetadata if true, metadata may be ignored during reads.

Protected Methods

protected void warningOccurred (int code)

Called by the native code or other classes to signal a warning. The code is used to lookup a localized message to be used when sending warnings to listeners.

protected void warningWithMessage (String msg)

The library has it's own error facility that emits warning messages. This routine is called by the native code when it has already formatted a string for output. XXX For truly complete localization of all warning messages, the sun_jpeg_output_message routine in the native code should send only the codes and parameters to a method here in Java, which will then format and send the warnings, using localized strings. This method will have to deal with all the parameters and formats (%u with possibly large numbers, %02d, %02x, etc.) that actually occur in the JPEG library. For now, this prevents library warnings from being printed to stderr.