org.hd.d.pg2k.svrCore
Class ImageUtils

java.lang.Object
  extended by org.hd.d.pg2k.svrCore.ImageUtils

public final class ImageUtils
extends java.lang.Object

Basic image-handling utilities.


Constructor Summary
private ImageUtils()
          Prevent instantiation.
 
Method Summary
static boolean canBeOwnThumbnail(ExhibitStaticAttr esa)
          Returns true if exhibit might be used as own thumbnail (in an HTML page for example).
static boolean canInlineInHTMLPageSimple(ExhibitMIME.ExhibitTypeParameters exhibitType)
          Returns true if the given MIME-type can always be inlined in an HTML page.
private static int computeIndexBitsForByteIndexColorMap(int paletteSize)
          Compute the number of bits to store the pixel of an indexed image given the size of the palette; strictly positive power of two up to 8.
static java.awt.image.BufferedImage convertRenderedImageToBufferedImage(java.awt.image.RenderedImage ri)
          Method to convert a RenderedImage to a BufferedImage.
static java.awt.image.BufferedImage convertToTrueColourARGB(java.awt.image.BufferedImage src, boolean forceCopy)
          Return true-colour ARGB format image if src is indexed, else return original.
static int countDistinct24BitRGBColours(java.awt.image.BufferedImage im)
          Count distinct colours in 24-bit RGB colour space.
static java.awt.image.ColorModel extractColorModelOrRGB(java.awt.image.BufferedImage imageIn)
          Extract the ColorModel of the inpuit image, or return the default RGB model; never null.
static java.awt.image.BufferedImage makeColourReducedBufferedImage(java.awt.image.BufferedImage in, int maxColours, boolean forceByteIndexModel)
          Create colour reduced image; never null A new image is created that is potentially colour-reduced from the original; no new colours should be introduced unless during the conversion to or from the RGB colour space in which reduction is done.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ImageUtils

private ImageUtils()
Prevent instantiation.

Method Detail

countDistinct24BitRGBColours

public static int countDistinct24BitRGBColours(java.awt.image.BufferedImage im)
Count distinct colours in 24-bit RGB colour space. Counts the number of distinct colours in the sRGB colour model in the given image, ignoring alpha, with each the depth of each band being 8 bits, for a total of 24 bits for each pixel.

If the source image is not in RGB format then colour conversion will take place.

Is slow but tries to be reasonably memory efficient.

The input image is not altered.


convertRenderedImageToBufferedImage

public static java.awt.image.BufferedImage convertRenderedImageToBufferedImage(java.awt.image.RenderedImage ri)
Method to convert a RenderedImage to a BufferedImage. Thanks to Jim Moore (06 Apr 2003) for his routine to "do it right"! Very slightly adjusted by DHD.

Parameters:
ri - the image to be converted (if already a BufferedImage then it is returned intact); must not be null
Returns:
the original if already a BufferedImage, else a copy of the data

makeColourReducedBufferedImage

public static java.awt.image.BufferedImage makeColourReducedBufferedImage(java.awt.image.BufferedImage in,
                                                                          int maxColours,
                                                                          boolean forceByteIndexModel)
Create colour reduced image; never null A new image is created that is potentially colour-reduced from the original; no new colours should be introduced unless during the conversion to or from the RGB colour space in which reduction is done. The R, G and B quantities are each 8 bits.

By default the new image is returned in the same ColorModel as the original, but forcing the result to an indexed (palette-based) model such as used by GIF can requested, in which case the palette size is capped appropriately, and the bits-per-pixel may be chosen to be 1, 2, 4, or 8 depending on the palette size.

Any input image that can be converted to RGB should be acceptable.

If no colour reduction is achieved and no ColorModel change is requested then the original image is returned; in no case is the original image altered.

This may return a single-colour image if the input image is too tricky and the maximum number of colours it can return is too small, but that will be avoided if possible.

Parameters:
in - the original image; must not be null
maxColours - maximum number of distinct colours allowed in the result image (as measured in a 24-bit (8,8,8) sRGB colour space); must be at least 2 and will be capped to 256 if an indexed model is forced for the result
forceByteIndexModel - if true, force the result to be a byte-indexed palette-based color model (maximum 256 colours) that may be (a) suitable for formats such as GIF and (b) more compressable
Returns:
image same dimensions as the input image, possibly with fewer colours; never null though might be single colour

computeIndexBitsForByteIndexColorMap

private static int computeIndexBitsForByteIndexColorMap(int paletteSize)
Compute the number of bits to store the pixel of an indexed image given the size of the palette; strictly positive power of two up to 8. The default behaviour is to return 8, for a byte-indexed palette, but this may return 4 if the palette has no more than 16 entries, 2 if the palette has no more than 4 entries, or 1 if the palette has no more than 2 entries, ie widths that will pack neatly into a byte.

Returns:
number of pits needed to store palette, as submultiple of 8

convertToTrueColourARGB

public static java.awt.image.BufferedImage convertToTrueColourARGB(java.awt.image.BufferedImage src,
                                                                   boolean forceCopy)
Return true-colour ARGB format image if src is indexed, else return original. The returned image should be easy to draw on.

The src image is not altered.

Parameters:
src - input image, is not altered; must not be null
Returns:
non-indexed image

extractColorModelOrRGB

public static java.awt.image.ColorModel extractColorModelOrRGB(java.awt.image.BufferedImage imageIn)
Extract the ColorModel of the inpuit image, or return the default RGB model; never null. This is needed because BufferedImage.getColorModel() can return null.

Parameters:
imageIn - input image from which to extract the ColorModel; must not be null
Returns:
a non-null ColorModel

canBeOwnThumbnail

public static boolean canBeOwnThumbnail(ExhibitStaticAttr esa)
Returns true if exhibit might be used as own thumbnail (in an HTML page for example). If true then we don't count downloads of this exhibit (and may adjust computation of "goodness" to take account of this).

An exhibit is potentially inlinable if:

Parameters:
esa - the exhibit details; never null
Returns:
true if the exhibit is potentially inlinable

canInlineInHTMLPageSimple

public static boolean canInlineInHTMLPageSimple(ExhibitMIME.ExhibitTypeParameters exhibitType)
Returns true if the given MIME-type can always be inlined in an HTML page. This only tests for very common and widely-supported image types.

This does not dynamically test the capabilities of the user agent that content is to be shown to.

If the exhibitType argument is null, this returns false.


DHD Multimedia Gallery V1.57.21

Copyright (c) 1996-2011, Damon Hart-Davis. All rights reserved.