Class BufferedImageFactory

java.lang.Object
com.twelvemonkeys.image.BufferedImageFactory

public final class BufferedImageFactory extends Object
A faster, lighter and easier way to convert an Image to a BufferedImage than using a PixelGrabber. Clients may provide progress listeners to monitor conversion progress.

Supports source image subsampling and source region extraction. Supports source images with 16 bit ColorModel and DataBuffer.TYPE_USHORT transfer type, without converting to 32 bit/TYPE_INT.

NOTE: Does not support images with more than one ColorModel or different types of pixel data. This is not very common.

Version:
$Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/image/BufferedImageFactory.java#1 $
  • Field Details

    • listeners

    • percentageDone

      private int percentageDone
    • producer

      private ImageProducer producer
    • consumerException

      private ImageConversionException consumerException
    • fetching

      private volatile boolean fetching
    • readColorModelOnly

      private boolean readColorModelOnly
    • x

      private int x
    • y

      private int y
    • width

      private int width
    • height

      private int height
    • xSub

      private int xSub
    • ySub

      private int ySub
    • offset

      private int offset
    • scanSize

      private int scanSize
    • sourceColorModel

      private ColorModel sourceColorModel
    • sourceProperties

      private Hashtable<?,?> sourceProperties
    • sourcePixels

      private Object sourcePixels
    • buffered

      private BufferedImage buffered
    • colorModel

      private ColorModel colorModel
    • consumer

      private final BufferedImageFactory.Consumer consumer
  • Constructor Details

    • BufferedImageFactory

      public BufferedImageFactory(Image source)
      Creates a BufferedImageFactory.
      Parameters:
      source - the source image
      Throws:
      IllegalArgumentException - if source == null
    • BufferedImageFactory

      public BufferedImageFactory(ImageProducer source)
      Creates a BufferedImageFactory.
      Parameters:
      source - the source image producer
      Throws:
      IllegalArgumentException - if source == null
  • Method Details

    • getBufferedImage

      public BufferedImage getBufferedImage() throws ImageConversionException
      Returns the BufferedImage extracted from the given ImageSource. Multiple requests will return the same image.
      Returns:
      the BufferedImage
      Throws:
      ImageConversionException - if the given ImageSource cannot be converted for some reason.
    • getColorModel

      public ColorModel getColorModel() throws ImageConversionException
      Returns the ColorModel extracted from the given ImageSource. Multiple requests will return the same model.
      Returns:
      the ColorModel
      Throws:
      ImageConversionException - if the given ImageSource cannot be converted for some reason.
    • dispose

      public void dispose()
      Frees resources used by this BufferedImageFactory.
    • abort

      public void abort()
      Aborts the image production.
    • setSourceRegion

      public void setSourceRegion(Rectangle region)
      Sets the source region (AOI) for the new image.
      Parameters:
      region - the source region
    • setSourceSubsampling

      public void setSourceSubsampling(int xSubsampling, int ySubsampling)
      Sets the source subsampling for the new image.
      Parameters:
      xSubsampling - horizontal subsampling factor
      ySubsampling - vertical subsampling factor
    • doFetch

      private void doFetch(boolean colorModelOnly) throws ImageConversionException
      Throws:
      ImageConversionException
    • createColorModel

      private void createColorModel()
    • createBuffered

      private void createBuffered()
    • freeResources

      private void freeResources()
    • processProgress

      private void processProgress(int scanline)
    • addProgressListener

      public void addProgressListener(BufferedImageFactory.ProgressListener listener)
      Adds a progress listener to this factory.
      Parameters:
      listener - the progress listener
    • removeProgressListener

      public void removeProgressListener(BufferedImageFactory.ProgressListener listener)
      Removes a progress listener from this factory.
      Parameters:
      listener - the progress listener
    • removeAllProgressListeners

      public void removeAllProgressListeners()
      Removes all progress listeners from this factory.
    • toShortPixels

      private static short[] toShortPixels(int[] inputPixels)
      Converts an array of int pixels to an array of short pixels. The conversion is done, by masking out the higher 16 bits of the int.

      For any given int, the short value is computed as follows:

      short value = (short) (intValue & 0x0000ffff);

      Parameters:
      inputPixels - the pixel data to convert
      Returns:
      an array of shorts, same length as inputPixels