Java Reference
In-Depth Information
Lookup tables are described by concrete subclasses of the abstract
java.awt.image.LookupTable
class, specifically
java.awt.image.ByteLookupTable
and
java.awt.image.ShortLookupTable
, which store byte integers and short in-
tegers, respectively. Either subclass can be used, although you'll probably use
ShortLookupTable
because you can easily represent unsigned byte integers. In
contrast, you would have to use negative values to represent byte values ranging from
128 to 255 when choosing
ByteLookupTable
.
To create a short integer lookup table that applies to all components, first create its
underlying array, as follows:
short[] invert = new short[256];
for (int i = 0; i < invert.length; i++)
invert[i] = (short) 255-i;
Thisarrayisdesignedtoinvertapixel'scolor(and,whenpresent,alpha)components,
and is intended for use with
ShortLookupTable
.
Continue by instantiating
ShortLookupTable
, as follows:
LookupTable table = new ShortLookupTable(0, invert);
The first argument is an offset to subtract from component values prior to indexing
intothearray.Ipass
0
becauseIdon'twanttosubtractanoffset.Thesecondargument
is the array itself.
Finally,instantiate
LookupOp
bycallingits
LookupOp(LookupTable look-
up, RenderingHints hints)
constructor, as follows:
BufferedImageOp invertOp = new LookupOp(new ShortLookupT-
able(0, invert), null);
I've chosen to not specify rendering hints by passing
null
as the second argument.
There'saproblemwithusingasinglearraywhendealing withanARGBimage be-
causeitcanscrewupthealphachannel—thelookuptablealsoappliestoalpha.Toad-
dressthissituation,youcanprocessthealphachannelseparatelybyprovidingonearray
for each component, as demonstrated here:
short[] alpha = new short[256];
short[] red = new short[256];
short[] green = new short[256];
short[] blue = new short[256];
for (int i = 0; i < alpha.length; i++)
{