Java Reference
In-Depth Information
The following snippet shows how to create a table that selects only colors
above a given threshold:
short[] threshold = new short[256];
for (int i = threshold - level; i < 256; i++)
threshold[i] = (short) i;
LookupTable threshold - table = new ShortLookupTable (0,
threshold);
LookupOp threshold - op = new LookupOp (threshold - table,
null);
BufferedImage dest - image = threshold - op.filter
(source - image, null);
To invert the colors you could create an array of size 256 and fill the first element
with the value 256 and then decrease each subsequent element by one until the
value reaches 0 in the last element.
If we want to apply the above threshold filter to only the red component and
leave the other components unchanged, we create a two-dimensional array to
hold the threshold array plus an identity array that leaves the other components
unchanged:
short[] identity = new short[256];
for (int i = 0; i < 256; i++) identity[i] = (short) i;
short[][] red - threshold ={ threshold, identity, identity } ;
LookupTable red - threshold - table = new ShortLookupTable (0,
red - threshold);
LookupOp red - threshold - op = new LookupOp
(red - threshold - table, null);
BufferedImage dest - image = red - threshold - op.filter
(source - image, null);
There are obviously many such lookup table transforms you can create. Note that
in-place filtering can be done with the LookupOp filter.
11.9.4 Rescaling
The RescaleOp filter applies a scaling factor and an offset to each color com-
ponent so as to brighten or dim an image:
dest - color = source - color * scale - factor + offset;
Yo u could do this also with a lookup table but you need less code with this filter.
Forexample,
RescaleOp brighten - op = new RescaleOp (2.0f, 32f, null);
BufferedImage dest - image = brighten - op.filter
(source - image, null);
Here each color component of each pixel is multiplied by 2.0 and then added to
32. If the value exceeds the maximum for that component, the maximum is used.
Search WWH ::




Custom Search