<< Chapter < Page | Chapter >> Page > |
Similarly, the expression for blueScale is the equation for a straight line that goes through 0.0 on the left and 1.0 on the right.
Listing 5 computes a new color based on scaled versions of the green and blue input color values. The red color value is notmodified.
color colr =
color(reD, greenScale*greeN, blueScale*bluE);//Enable the following statement to override the
// color modification and display the raw image in// the output window. Disable it to display the
// modified image.//colr = color(reD, greeN, bluE);
If you enable the last statement in Listing 5 , the new output color will be identical to the old input color. This isuseful when you need to produce an output image showing the unmodified input image as in Image 1 .
The second major difference between this and previous versions of the run method is the delegation of the code that stores the modified pixel color in theoutput pixel array to a separate method named setOutputPixelColor .
This was done because it is unlikely that the code needed to perform this operation will need to change from one algorithm to the next. As a result, mostof the code that is likely to change from one algorithm to the next is confined to the method named processPixels .
The method named setOutputPixelColor is called in Listing 6 , which also signals the end of the processPixels method
setOutputPixelColor(cnt,colr);
}//end for loopupdatePixels();//required
}//end processPixels
The setOutputPixelColor method is shown in Listing 8 . The code in that method is the same as code that I have explained in earlier modules, so I won't repeat thatexplanation here.
The remainder of the Pr0140aRunner class shown in Listing 8 is the same as code that I have explained in earlier modules. Therefore, there is nothing more toexplain in this module.
I encourage you to copy the code from Listing 7 and Listing 8 and paste it into your PDE. Be sure to put the code from Listing 7 in the leftmost tab.
Don't forget to put an image file of your choice in a folder named data that is a child of the folder that contains the files with the .pde extension. You will need to edit the code to change the name of the image file in two different places .
Run the sketch and observe the results. Experiment with the code. Make changes, run the sketch again, and observe the results of your changes. Make certain that youcan explain why your changes behave as they do.
Don't forget to also create and run the JavaScript version of your sketch in your HTML 5 compatible browser.
Click here to view the JavaScript version of the sketch discussed in this module in your HTML 5 compatible browser.
If you have a programmable Android device , try creating and running the Android version of your sketch in your Android device.
Also try creating and running the stand-alone version of the sketch by selecting Export Application from the File menu while in Java mode.
In this module, you learned:
Click here to view the JavaScript version of the sketch discussed in this module in your HTML 5 compatible browser.
This section contains a variety of miscellaneous information.
Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.
I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.
Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.
Complete listings of the classes discussed in this module are provided in Listing 7 and Listing 8 .
/*Pr0140a.pde
Copyright 2013, R.G.BaldwinThis sketch can be used as a template for writing other
pixel modification algorithms.The sketch illustrates a linear space wise pixel
modification algorithm in which the green and blue pixelcolor values are scaled linearly as a function of the
distance of the pixel from the left side of the image.The output is displayed in an image explorer.
The image explorer displays the coordinates of the mousepointer along with the RGB color values of the pixel at
the mouse pointer. It also displays the width and heightof the image.
The image explorer displays an error message in place ofthe image if the image is wider or taller than the output
display window.**********************************************************/
//@pjs preload required for JavaScript version in browser./* @pjs preload="Pr0140a.jpg"; */
PImage img;PFont font;
Pr0140aRunner obj;void setup(){
//This size matches the width of the image and allows// space below the image to display the text information.
size(365,344);frameRate(30);
img = loadImage("Pr0140a.jpg");obj = new Pr0140aRunner();
font = createFont("Arial",16,true);}//end setup
//-------------------------------------------------------//void draw(){
obj.run();}//end draw
class Pr0140aRunner{
//The following instance variable is used to set the// color of the appropriate pixel in the output display
// window.int ctr = 0;//output pixel array countervoid run(){
background(255);//whitetextFont(font,16);//Set the font size
fill(255,0,0);//Set the text color to red//Display error message in place of image if the// image won't fit in the display window.
if(img.width>width){
text("--Image too wide--",10,20);text("Image width: " + img.width,10,40);
text("Display width: " + width,10,60);}else if(img.height>height){
text("--Image too tall--",10,20);text("Image height: " + img.height,10,40);
text("Display height: "+ height,10,60);
}else{//The image will fit in the output window.//Call a method that will apply a specific
// pixel-modification algorithm and write the// modified pixel colors into the output window.
processPixels();}//end else//Display the author's name on the output in the font
// size and text color defined above.text("Dick Baldwin",10,20);//Display information about the pixel being pointed
// to with the mouse. Display near the bottom of the// output window.
displayPixelInfo(img);}//end run
//-----------------------------------------------------////Apply a pixel modification algorithm that causes the// green and blue color values to be scaled on a linear
// basis moving from left to right across the image.void processPixels(){
loadPixels();//requiredimg.loadPixels();//required
float reD,greeN,bluE;//store color values herectr = 0;//initialize output pixel array counter//Process each pixel in the input image.
for(int cnt = 0;cnt<img.pixels.length;cnt++){
//Get and save RGB color values for current pixel.reD = red(img.pixels[cnt]);greeN = green(img.pixels[cnt]);bluE = blue(img.pixels[cnt]);//Compute the column number and use it to compute
// the linear scale factors that will be applied to// the green and blue color values.
int col = cnt%img.width;float greenScale = (float)(width - col)/width;
float blueScale = (float)(col)/width;//Compute a new color based on scaled versions of// the input color values. Don't modify the red
// color value.color colr =
color(reD, greenScale*greeN, blueScale*bluE);//Enable the following statement to override the
// color modification and display the raw image in// the output window. Disable it to display the
// modified image.//colr = color(reD, greeN, bluE);//Store the modified pixel color in the output pixel
// array.setOutputPixelColor(cnt,colr);
}//end for loopupdatePixels();//required
}//end processPixels//-----------------------------------------------------////Method to set the color of a pixel in the output image
// based on the input pixel counter, the output pixel// counter, the widths of the input and output images,
// and the desired color. Deals with the possibility that// the output display window is wider than the image being
// processed.void setOutputPixelColor(int cnt,color colr){
if(width>= img.width){
if((cnt % img.width == 0)&&(cnt != 0)){
//Compensate for excess display width by// increasing the output counter.
ctr += (width - img.width);}//end if
//Store the pixel in the output pixel array// and increment the output pixel counter.
pixels[ctr]= colr;
ctr++;}//end if
}//end setOutputPixelColor//-----------------------------------------------------//
//Method to display coordinate and pixel color info at// the current mouse pointer location. Also displays
// width and height information about the image.void displayPixelInfo(PImage image){
//Protect against mouse being outside the frameif((mouseX<width)&&(mouseY<height)&&(mouseX>= 0)&&(mouseY>= 0)){//Get and display the width and height of the
// image.text("Width: " + image.width + " Height: " +
image.height,10,height - 50);//Get and display coordinates of mouse pointer.text("X: " + mouseX + ", Y: " + mouseY,10,
height - 30);//Get and display color data for the pixel at the// mouse pointer.
text("R: " + red(pixels[mouseY*width+mouseX]) +
" G: " + green(pixels[mouseY*width+mouseX]) +
" B: " + blue(pixels[mouseY*width+mouseX]),
10,height - 10);}//end if
}//end displayPixelInfo}//end class Pr0140aRunner
-end-
Notification Switch
Would you like to follow the 'The processing programming environment' conversation and receive update notifications?