provided by: 
Originally published at Internet.comJava Programming Notes # 462 * Preface * General * Viewing tip * Figures * Listings * Supplementary material * General background information * Constructing images * The framework program named ImgMod05 * RescaleOp examples * ColorConvertOp example * Preview * Discussion and sample code * Run the program * Summary * Complete program listing * Copyright * Resources * About the author -----------------------------------
Preface
General
In an earlier lesson titled "A Framework for Experimenting with Java 2D Image-Processing Filters" (see Resources), I taught you how to write a framework program that makes it easy to use the image-filtering classes of the Java 2D API to process the pixels in an image and to display the processed image.
At the close of that lesson, I told you that future lessons would teach you how to use the following image-filtering classes from the Java 2DAPI: * LookupOp * AffineTransformOp * BandCombineOp * ConvolveOp * RescaleOp * ColorConvertOp
In several of the previous lessons listed in the Resources section, I taught you how to use the LookupOp, AffineTransformOp, BandCombineOp, and ConvolveOp image-filtering classes.
In this final lesson of the series, I will teach you how to use the RescaleOp and ColorConvertOp image-filtering classes.
Viewing tip
I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the figures and listings while you are reading about them.
Figures
* Figure 1. Image with all three colors inverted. * Figure 2. Image with only one color inverted. * Figure 3. Use of RescaleOp to adjust contrast and brightness. * Figure 4. Conversion of color space to CS_GRAY. * Figure 5. User input GUI with the ColorConvertOp tab selected. * Figure 6. User input GUI with the RescaleOp tab selected.
Listings
* Listing 1. Beginning of the class definition. * Listing 2. Creation of components used to construct the ColorConvertOp page. * Listing 3. Creation of components used to construct the RescaleOp page. * Listing 4. The primary constructor. * Listing 5. The constructColorConvertPage method. * Listing 6. The processColorConvertPage method. * Listing 7. The constructColorRescalePagee method. * Listing 8. The processColorRescalePage method. * Listing 9. The processImg method. * Listing 10. Complete listing of the ImgMod43 class.
Supplementary material
I recommend that you also study the other lessons in my extensive collection of online Java tutorials. You will find a consolidated index at www.DickBaldwin.com.
General background information
Constructing images
Before getting into the programming details, it may be useful for you to review the concept of how images are constructed, stored, transported, and rendered in Java (and in most modern computers for that matter). I provided a great deal of information on these topics in the earlier lesson titled "Processing Image Pixels using Java, Getting Started" (see Resources). Therefore, I won't repeat that information here. Rather, I will simply refer you back to the earlier lesson.
The framework program named ImgMod05
It will also be useful for you to understand the behavior of the framework program named ImgMod05. Therefore, I strongly recommend that you study the earlier lesson titled "A Framework for Experimenting with Java 2D Image-Processing Filters" (see Resources).
However, if you don't have the time to do that, you should take a look at the earlier lesson titled "Using the Java 2D LookupOp Filter Class to Process Images" (see Resources), in which I summarized the behavior of the framework program named ImgMod05.
RescaleOp examples
The RescaleOp class can be used to multiply the color value for each pixel by a user-specified scale factor, and then to add a user-specified constant to the product. Separate scale factors and additive constants are provided for each of the red, green, and blue colors.
Color values that fall outside the allowable range from 0 to 255 are simply clipped to 0 and 255.
Color inversion
Several previous lessons have discussed inverting the colors in an image. This is another class that makes it easy to invert the colors. In this case, the scale factor for all three bands would be set to -1 and the additive constant would be set to 255. This would produce an inverted output image like that shown in Figure 1.
Figure 1. Image with all three colors inverted.
Just in case you have a need to do so, each color band can be inverted separately producing results like those shown in Figure 2. Figure 2 shows the result of inverting only the blue pixels in an image without modifying the red and green pixels.
Figure 2. Image with only one color inverted.
Adjusting the contrast and brightness
Perhaps more importantly, the RescaleOp class can also be used to adjust the contrast and brightness of an image using the concepts that I explained in the earlier lesson titled "Processing Image Pixels Using Java: Controlling Contrast and Brightness" (see
Author: Richard G. Baldwin
Read article at Internet.com site