ach.image
Class EMemImageSource

java.lang.Object
  extended byach.image.EMemImageSource
All Implemented Interfaces:
java.awt.image.ImageConsumer, java.awt.image.ImageProducer, java.lang.Runnable

public class EMemImageSource
extends java.lang.Object
implements java.awt.image.ImageProducer, java.awt.image.ImageConsumer, java.lang.Runnable

EMemImageSource.java
Copyright (c) 1997-2003 Helge Hackbarth, All Rights Reserved.
EMemImageSource extends MemoryImageSource to allow direct access to pixels and colormodel. The class offers several methods to extract and scale regions of a memory image very fast.
Depending on the colormodel the image data will be stored either in an integer array or a byte array. If the mapsize of the colormodel is equal or less 16 (4 bit or less), the image data will be stored as packed bytes (4 or 1 bit per pixel) to reduce memory consumption.

THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.


Field Summary
 byte[] bytePixels
          Array containing the image data if created for a palette image with 8 bits per pixel (256 colors or gray levels)
 java.awt.image.ColorModel colorModel
          The colormodel of the image represented by this ImageProducer
 java.lang.String errorMsg
          The only way to communicate the last error to the rest of the world when this EMemImageSource consumed its image data from an ImageProducer.
 int height
          vertical number of pixels of the image represented by this ImageProducer
 int horizDpi
          Horizontal resolution in pixels per inch.
 int[] intPixels
          Array containing the image data if created for a true color image
 int numPages
          Indicates the number of pages of the image file that was producing this EMemImageSource (default = 1).
 java.awt.image.ColorModel origColorModel
          The colormodel used at consumer initialization (might be changed during pixel consumption due to memory optimization)
 byte[] packedPixels
          Array containing the packed image data if created for a palette image with 1 or 4 bits per pixel (2 or 16 colors or gray levels)
 int page
          Indicating current page of a multi document image file kept in memory by this instance of EMemImageSource (default = 1).
 int pixeloffset
          Offset to first valid pixel in the array
 int pixelscan
          Number of elements per row.
 java.awt.Rectangle printArea
          Rectangle to store parameters of last print operation
 boolean useProdGauge
          Flag if producer shall show a gauge during image production
 int vertDpi
          Vertical resolution in pixels per inch.
 int width
          horizontal number of pixels of the image represented by this ImageProducer
 
Fields inherited from interface java.awt.image.ImageConsumer
COMPLETESCANLINES, IMAGEABORTED, IMAGEERROR, RANDOMPIXELORDER, SINGLEFRAME, SINGLEFRAMEDONE, SINGLEPASS, STATICIMAGEDONE, TOPDOWNLEFTRIGHT
 
Constructor Summary
EMemImageSource(java.awt.image.ImageProducer producer, boolean useGauge)
          Constructor to be used if image will be created from ImageProducer through ImageConsumer interface.
EMemImageSource(java.awt.image.ImageProducer producer, ImgComponentIntf imgComponent, java.awt.Toolkit toolkit, int scaleHint)
          Constructor to be used if image will be created from ImageProducer through ImageConsumer interface.
EMemImageSource(int w, int h, java.awt.image.ColorModel cm, byte[] pixels, int off, int scan, int hDpi, int vDpi)
          Constructor to create a memory image for palette image data with byte pixels (8 bits per pixel).
EMemImageSource(int w, int h, java.awt.image.ColorModel cm, byte[] pPixels, int bpp, int off, int scan, int hDpi, int vDpi)
          Constructor to create a memory image for packed image data where pixels are stored in bytes packed on bit level (1 or 4 bits per pixel).
EMemImageSource(int w, int h, java.awt.image.ColorModel cm, int[] pixels, int off, int scan, int hDpi, int vDpi)
          Constructor to create a memory image for true color image data with int pixels (32 bits per pixel).
 
Method Summary
 void abort()
          Image consumption will abort at next call to setpixels
 void addConsumer(java.awt.image.ImageConsumer ic)
          Adds an ImageConsumer to the list of consumers interested in data for this image.
 int calcPixelSize()
          Calculate number of bits per pixel according to mapSize of ColorModel For IndexColorModels this will be 2 ^ getMapSize(), for other ColorModels this will be equal to ColorModel.getPixelSize()
 EMemImageSource extractMemImgRegion(int x, int y, int w, int h)
          Extract memory image region as a new ImageProducer from an EMemImageSource without scaling.
 java.awt.image.ImageProducer extractMemImgRegion(int x, int y, int w, int h, int wNew, int hNew)
          Extract memory image region as a new ImageProducer from an EMemImageSource and scale to new dimension (with automatic gray scaling (antialiasing) for bilevel images).
 void flipVertical()
          flip image vertical (self-inverse)
 EMemImageSource getRotated090()
          Rotate image by 90 degree and return a instance of EMemImageSource; for 180 degree rotation see self inverse method rotate180()
 EMemImageSource getRotated270()
          Rotate image by 270 degree and return a instance of EMemImageSource; for 180 degree rotation see self inverse method rotate180()
static java.awt.image.ImageProducer getToolkitImageProducer_VerGreater12(java.awt.Toolkit toolkit, java.lang.String imgName, java.net.URL imgURL)
          Get an ImageProducer with methods of Java 1.2 (and higher) that saves some resources compared to methods available with Java 1.1.
 void imageComplete(int status)
          ImageConsumers call back method to be informed about ImageProducers completion status.
 void invert()
          invert image (if palette bitmap: invert colormap, else: invert each pixel)
 boolean isComplete()
          Determine whether consumption of an image from an ImageProducer to memory is completed as this is carried out asynchronously in an independent thread.
 boolean isConsumer(java.awt.image.ImageConsumer ic)
          Determine if an ImageConsumer is on the list of consumers currently interested in data for this image.
 boolean isInitialized()
          Determine whether initialization of memory image is completed
 void mirrorHorizontal()
          mirror image horizontal (self-inverse)
 void removeConsumer(java.awt.image.ImageConsumer ic)
          Remove an ImageConsumer from the list of consumers interested in data for this image.
 void requestTopDownLeftRightResend(java.awt.image.ImageConsumer ic)
          Requests that a given ImageConsumer have the image data delivered one more time in top-down, left-right order.
 void rotate180()
          rotate image by 180 degree (self-inverse function); see also getRotated090() and getRotated270()
 void run()
          Can be used to call an ImageProducers startProduction method via an asynchronous thread.
 void setColorModel(java.awt.image.ColorModel model)
          The ColorModel object used for the majority of the pixels reported using the setPixels method calls.
 void setDimensions(int width, int height)
          The dimensions of the source image are reported using the setDimensions method call.
 void setHints(int hints)
          The ImageProducer can deliver the pixels in any order, but the ImageConsumer may be able to scale or convert the pixels to the destination ColorModel more efficiently or with higher quality if it knows some information about how the pixels will be delivered up front.
 void setPixels(int x, int y, int w, int h, java.awt.image.ColorModel model, byte[] pixels, int off, int scansize)
          Set an array of bytes.
 void setPixels(int x, int y, int w, int h, java.awt.image.ColorModel model, int[] pixels, int off, int scansize)
          Set an array of integers.
 void setProperties(java.util.Hashtable props)
          Sets the extensible list of properties associated with this image.
 void startProduction(java.awt.image.ImageConsumer ic)
          Adds an ImageConsumer to the list of consumers interested in data for this image, and immediately start delivery of the image data through the ImageConsumer interface.
 java.lang.String toString()
           
 EMemImageSource unskew()
          unskew image if horizontal and vertical resolution is different image will be scaled so that both resolutions are equal to the higher resolution.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

width

public int width
horizontal number of pixels of the image represented by this ImageProducer


height

public int height
vertical number of pixels of the image represented by this ImageProducer


colorModel

public java.awt.image.ColorModel colorModel
The colormodel of the image represented by this ImageProducer

See Also:
ColorModel

origColorModel

public java.awt.image.ColorModel origColorModel
The colormodel used at consumer initialization (might be changed during pixel consumption due to memory optimization)

See Also:
ColorModel

intPixels

public int[] intPixels
Array containing the image data if created for a true color image


bytePixels

public byte[] bytePixels
Array containing the image data if created for a palette image with 8 bits per pixel (256 colors or gray levels)


packedPixels

public byte[] packedPixels
Array containing the packed image data if created for a palette image with 1 or 4 bits per pixel (2 or 16 colors or gray levels)


pixeloffset

public int pixeloffset
Offset to first valid pixel in the array


pixelscan

public int pixelscan
Number of elements per row.
For packed pixels:
1 Bit: pixelscan = ceil((width + 7) / 8) 4 Bit: pixelscan = ceil((width + 1) / 2)


horizDpi

public int horizDpi
Horizontal resolution in pixels per inch.


vertDpi

public int vertDpi
Vertical resolution in pixels per inch.


numPages

public int numPages
Indicates the number of pages of the image file that was producing this EMemImageSource (default = 1). E.g. TIFF files might contain more than one page. Note: The EMemImageSource can hold only one of these images!


page

public int page
Indicating current page of a multi document image file kept in memory by this instance of EMemImageSource (default = 1).


printArea

public java.awt.Rectangle printArea
Rectangle to store parameters of last print operation


errorMsg

public java.lang.String errorMsg
The only way to communicate the last error to the rest of the world when this EMemImageSource consumed its image data from an ImageProducer. Null if no error occured.


useProdGauge

public boolean useProdGauge
Flag if producer shall show a gauge during image production

Constructor Detail

EMemImageSource

public EMemImageSource(java.awt.image.ImageProducer producer,
                       boolean useGauge)
Constructor to be used if image will be created from ImageProducer through ImageConsumer interface. If process was successful, calling isComplete() returns true and height > 0 If an error occured, the message can be retrieved from errorMsg

Parameters:
producer - an ImageProducer
useGauge - set true, if a gauge should be displayed during image consumption

EMemImageSource

public EMemImageSource(java.awt.image.ImageProducer producer,
                       ImgComponentIntf imgComponent,
                       java.awt.Toolkit toolkit,
                       int scaleHint)
Constructor to be used if image will be created from ImageProducer through ImageConsumer interface. Image data will be displayed directly during loading process on an ImgComponent. If an error occured, the message can be retrieved from errorMsg.

Parameters:
producer - an ImageProducer
imgComponent - implementing ImgComponentIntf to display image during loading process
toolkit - to provide an awt.image.Toolkit to load images
scaleHint - to conrol scaling behavior (e.g. Image.SCALE_FAST or Image.SCALE_SMOOTH)
See Also:
ImgCanvas

EMemImageSource

public EMemImageSource(int w,
                       int h,
                       java.awt.image.ColorModel cm,
                       byte[] pixels,
                       int off,
                       int scan,
                       int hDpi,
                       int vDpi)
Constructor to create a memory image for palette image data with byte pixels (8 bits per pixel).

Parameters:
w - width of the image
h - height of the image
cm - ColorModel for this image
pixels - byte-array containing the image data
off - offset to the first valid pixel in the array
scan - number of elements per row
hDpi - horizontal resolution in pixels per inch
vDpi - vertical resolution in pixels per inch

EMemImageSource

public EMemImageSource(int w,
                       int h,
                       java.awt.image.ColorModel cm,
                       int[] pixels,
                       int off,
                       int scan,
                       int hDpi,
                       int vDpi)
Constructor to create a memory image for true color image data with int pixels (32 bits per pixel).

Parameters:
w - width of the image
h - height of the image
cm - ColorModel for this image
pixels - int-array containing the image data
off - offset to the first valid pixel in the array
scan - number of elements per row
hDpi - horizontal resolution in pixels per inch
vDpi - vertical resolution in pixels per inch

EMemImageSource

public EMemImageSource(int w,
                       int h,
                       java.awt.image.ColorModel cm,
                       byte[] pPixels,
                       int bpp,
                       int off,
                       int scan,
                       int hDpi,
                       int vDpi)
Constructor to create a memory image for packed image data where pixels are stored in bytes packed on bit level (1 or 4 bits per pixel).

Parameters:
w - width of the image
h - height of the image
cm - ColorModel for this image
pPixels - byte-array containing packed image data
bpp - bits per pixel (supported values: 1 or 4)
off - offset to the first valid pixel in the array (counted in bytes)
scan - number of elements per row
hDpi - horizontal resolution in pixels per inch
vDpi - vertical resolution in pixels per inch
Method Detail

run

public void run()
Can be used to call an ImageProducers startProduction method via an asynchronous thread.

Specified by:
run in interface java.lang.Runnable

addConsumer

public void addConsumer(java.awt.image.ImageConsumer ic)
Adds an ImageConsumer to the list of consumers interested in data for this image.

Specified by:
addConsumer in interface java.awt.image.ImageProducer
See Also:
ImageConsumer

isConsumer

public boolean isConsumer(java.awt.image.ImageConsumer ic)
Determine if an ImageConsumer is on the list of consumers currently interested in data for this image.

Specified by:
isConsumer in interface java.awt.image.ImageProducer
Returns:
true if the ImageConsumer is on the list; false otherwise
See Also:
ImageConsumer

removeConsumer

public void removeConsumer(java.awt.image.ImageConsumer ic)
Remove an ImageConsumer from the list of consumers interested in data for this image.

Specified by:
removeConsumer in interface java.awt.image.ImageProducer
See Also:
ImageConsumer

startProduction

public void startProduction(java.awt.image.ImageConsumer ic)
Adds an ImageConsumer to the list of consumers interested in data for this image, and immediately start delivery of the image data through the ImageConsumer interface.

Specified by:
startProduction in interface java.awt.image.ImageProducer
See Also:
ImageConsumer

requestTopDownLeftRightResend

public void requestTopDownLeftRightResend(java.awt.image.ImageConsumer ic)
Requests that a given ImageConsumer have the image data delivered one more time in top-down, left-right order.

Specified by:
requestTopDownLeftRightResend in interface java.awt.image.ImageProducer
See Also:
ImageConsumer

setDimensions

public void setDimensions(int width,
                          int height)
The dimensions of the source image are reported using the setDimensions method call.

Specified by:
setDimensions in interface java.awt.image.ImageConsumer

setProperties

public void setProperties(java.util.Hashtable props)
Sets the extensible list of properties associated with this image.

Specified by:
setProperties in interface java.awt.image.ImageConsumer

setColorModel

public void setColorModel(java.awt.image.ColorModel model)
The ColorModel object used for the majority of the pixels reported using the setPixels method calls.

Specified by:
setColorModel in interface java.awt.image.ImageConsumer
See Also:
ColorModel

setHints

public void setHints(int hints)
The ImageProducer can deliver the pixels in any order, but the ImageConsumer may be able to scale or convert the pixels to the destination ColorModel more efficiently or with higher quality if it knows some information about how the pixels will be delivered up front.

Specified by:
setHints in interface java.awt.image.ImageConsumer

setPixels

public void setPixels(int x,
                      int y,
                      int w,
                      int h,
                      java.awt.image.ColorModel model,
                      byte[] pixels,
                      int off,
                      int scansize)
Set an array of bytes. ColorModel changes will be ignored!

Specified by:
setPixels in interface java.awt.image.ImageConsumer
See Also:
ImageConsumer.setPixels(int, int, int, int, java.awt.image.ColorModel, byte[], int, int)

setPixels

public void setPixels(int x,
                      int y,
                      int w,
                      int h,
                      java.awt.image.ColorModel model,
                      int[] pixels,
                      int off,
                      int scansize)
Set an array of integers. ColorModel changes will be ignored!

Specified by:
setPixels in interface java.awt.image.ImageConsumer
See Also:
ImageConsumer.setPixels(int, int, int, int, java.awt.image.ColorModel, byte[], int, int)

imageComplete

public void imageComplete(int status)
ImageConsumers call back method to be informed about ImageProducers completion status.

Specified by:
imageComplete in interface java.awt.image.ImageConsumer
See Also:
ImageConsumer.imageComplete(int)

abort

public void abort()
Image consumption will abort at next call to setpixels


isInitialized

public boolean isInitialized()
Determine whether initialization of memory image is completed


isComplete

public boolean isComplete()
Determine whether consumption of an image from an ImageProducer to memory is completed as this is carried out asynchronously in an independent thread.


calcPixelSize

public int calcPixelSize()
Calculate number of bits per pixel according to mapSize of ColorModel For IndexColorModels this will be 2 ^ getMapSize(), for other ColorModels this will be equal to ColorModel.getPixelSize()

Returns:
Number of bits per pixel according to mapSize

toString

public java.lang.String toString()
Returns:
Information about the image kept in memory as a String.

extractMemImgRegion

public EMemImageSource extractMemImgRegion(int x,
                                           int y,
                                           int w,
                                           int h)
Extract memory image region as a new ImageProducer from an EMemImageSource without scaling. Version with scaling see below...

Parameters:
x - horizontal offset of extracted image relative to source image
y - vertical offset of extracted image relative to source image
w - width of extracted image
h - height of extracted image

extractMemImgRegion

public java.awt.image.ImageProducer extractMemImgRegion(int x,
                                                        int y,
                                                        int w,
                                                        int h,
                                                        int wNew,
                                                        int hNew)
Extract memory image region as a new ImageProducer from an EMemImageSource and scale to new dimension (with automatic gray scaling (antialiasing) for bilevel images). Version without scaling see above...

Parameters:
x - horizontal offset of extracted image relative to source image
y - vertical offset of extracted image relative to source image
w - horizontal number of pixels extracted from source image
h - vertical number of pixels extracted from source image
wNew - scaled width of extracted image
hNew - scaled height of extracted image
Returns:
ImageProducer for scaled/cropped image or null when parameters invalid

unskew

public EMemImageSource unskew()
unskew image if horizontal and vertical resolution is different image will be scaled so that both resolutions are equal to the higher resolution. E.g. a fax with resolution 203x98 dpi will be scale to 203x203 dpi.

Returns:
ImageProducer representing the scaled image

invert

public void invert()
invert image (if palette bitmap: invert colormap, else: invert each pixel)


rotate180

public void rotate180()
rotate image by 180 degree (self-inverse function); see also getRotated090() and getRotated270()


getRotated090

public EMemImageSource getRotated090()
Rotate image by 90 degree and return a instance of EMemImageSource; for 180 degree rotation see self inverse method rotate180()


getRotated270

public EMemImageSource getRotated270()
Rotate image by 270 degree and return a instance of EMemImageSource; for 180 degree rotation see self inverse method rotate180()


flipVertical

public void flipVertical()
flip image vertical (self-inverse)


mirrorHorizontal

public void mirrorHorizontal()
mirror image horizontal (self-inverse)


getToolkitImageProducer_VerGreater12

public static java.awt.image.ImageProducer getToolkitImageProducer_VerGreater12(java.awt.Toolkit toolkit,
                                                                                java.lang.String imgName,
                                                                                java.net.URL imgURL)
Get an ImageProducer with methods of Java 1.2 (and higher) that saves some resources compared to methods available with Java 1.1. Image can be located through pathname or URL.

Parameters:
toolkit - any kind of toolkit compatible to java.awt.Toolkit
imgName - Name of image (used only if imgURL parameter is null)
imgURL - URL of image (if null, imgName will be used
Returns:
ImageProducer for image