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

Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
Daniel
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
Maciej
characteristics of micro business
Abigail
for teaching engĺish at school how nano technology help us
Anassong
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
NANO
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
s.
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
Tarell
what is the actual application of fullerenes nowadays?
Damian
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
Tarell
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
Virgil
is Bucky paper clear?
CYNTHIA
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
NANO
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Harper
Do you know which machine is used to that process?
s.
how to fabricate graphene ink ?
SUYASH Reply
for screen printed electrodes ?
SUYASH
What is lattice structure?
s. Reply
of graphene you mean?
Ebrahim
or in general
Ebrahim
in general
s.
Graphene has a hexagonal structure
tahir
On having this app for quite a bit time, Haven't realised there's a chat room in it.
Cied
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
China
Cied
types of nano material
abeetha Reply
I start with an easy one. carbon nanotubes woven into a long filament like a string
Porter
many many of nanotubes
Porter
what is the k.e before it land
Yasmin
what is the function of carbon nanotubes?
Cesar
I'm interested in nanotube
Uday
what is nanomaterials​ and their applications of sensors.
Ramkumar Reply
what is nano technology
Sravani Reply
what is system testing?
AMJAD
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Good
Got questions? Join the online conversation and get instant answers!
QuizOver.com Reply

Get the best Algebra and trigonometry course in your pocket!





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