<< Chapter < Page Chapter >> Page >

The runner class named Pr0140aRunner

Aside from the specific implementation of the pixel modification algorithm, the major difference between this sketch and the sketches since Pr0120-Image Explorer is that I have made the code more modular.

For example, I have attempted to isolate those portions of the template that are likely to change from one algorithm go the next from those portions that areless likely to change. That will make it easier to explain how the code for one algorithm differs from the code for other algorithms.

The beginning of the class and the run method

Listing 1 shows the beginning of the class and the modified run method. It is unlikely that this code will change much from one algorithm tothe next.

Listing 1. beginning of the class and the run method.

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
Listing 1. Beginning of the class and the run method.

Major differences in the run method

The first major difference between this and previous versions of the run method is the delegation of pixel manipulation operations to the method named processPixels . Moving those operations to a separate method will decrease the likelihood that it will be necessary to modify the code in Listing 1 to implement different algorithms.

I will identify the second major change later.

Beginning of the processPixels method

The processPixels method, which begins in Listing 2 , applies a pixel modification algorithm that causes the green and blue color values to be scaled on a linearbasis moving from left to right across the image.

Listing 2. beginning of the processpixels method.

void processPixels(){ loadPixels();//requiredimg.loadPixels();//required float reD,greeN,bluE;//store color values herectr = 0;//initialize output pixel array counter
Listing 2. Beginning of the processPixels method.

The code in Listing 2 :

  • Does some preliminary housekeeping regarding pixels, (which you have seen before) .
  • Declares local variables for storage of red, green, and blue color values.
  • Initializes the counter that is used to position color values in the output pixel array.

Beginning of a for loop

Listing 3 shows the beginning of a for loop that is used to process every pixel in the input pixel array.

Listing 3. beginning of a for loop.

//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]);
Listing 3. Beginning of a for loop.

Get and save color values

The code in Listing 3 gets and saves the RGB color values for the current pixel.

Compute the column number

This algorithm applies the same scale factor to every pixel in each column. Therefore, it is necessary to identify the column to which a pixel belongs whenthe colors for the pixel are retrieved from the pixel array. However, it is not necessary to identify the row. (We will get to that in later modules.)

The code in Listing 4 uses the modulus operator to compute the column number based on the loop counter, (which is the same as the input pixel array index) .

Listing 4. compute the scale factor for the column.

//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;
Listing 4. Compute the scale factor for the column.

Compute the scale factor for the column

The code in Listing 4 uses the column number to compute the two required scale factors.

If case you haven't recognized it, the expression for greenScale is the equation for a straight line that goes through 1.0 on the left side ofthe image and goes through 0.0 on the right side of the image.

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, The processing programming environment. OpenStax CNX. Feb 26, 2013 Download for free at http://cnx.org/content/col11492/1.5
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'The processing programming environment' conversation and receive update notifications?

Ask