<< 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.

Compute a new color

Listing 5 computes a new color based on scaled versions of the green and blue input color values. The red color value is notmodified.

Listing 5. compute a new color.

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);
Listing 5. Compute a new color.

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 .

Store modified pixel color in the output pixel array

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

Listing 6. store modified pixel color in the output pixel array.

setOutputPixelColor(cnt,colr); }//end for loopupdatePixels();//required }//end processPixels
Listing 6. Store modified pixel color in the output pixel array.

The setOutputPixelColor method

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 Pr0140aRunnerclass

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.

Run the sketch

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.

Summary

In this module, you learned:

  1. How to develop a template sketch for implementing pixel modification algorithms, and
  2. How to implement a space-wise linear pixel modification algorithm.

Click here to view the JavaScript version of the sketch discussed in this module in your HTML 5 compatible browser.

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Pr0140-A space-wise linear pixel-modification algorithm
  • File: Pr0140.htm
  • Published: 02/26/13
Disclaimers:

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 program listing

Complete listings of the classes discussed in this module are provided in Listing 7 and Listing 8 .

Listing 7. pr0140a.pde.

/*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
Listing 7. Pr0140a.pde.

Listing 8. pr0140arunner.pde.

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
Listing 8. Pr0140aRunner.pde.

-end-

Questions & Answers

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
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