NotesWeek14.Nov 22-26

NotesWeek14.Nov 22-26 - CS 180 Problem Solving and Object...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 180 Problem Solving and Object Oriented Programming Fall 2010 hMp://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/ This Week: Notes for Week 14: Nov 2226, 2010 Aditya Mathur 11/22 1. 2. 3. 4. Review Class BufferedImage Pixel operaXons Project 5 11/29, 12/1: Department of Computer Science Classes Cancelled in lieu of Exams Purdue University West LafayeMe, IN, USA Readings and Exercises for Week 14 Readings: Chapter: 13.3, 13.4, 13.5; 14.3, 14.4 [Carry over from Week 13] Exercises: 13.11, 13.13 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 2 Revised Project Schedule Project 5: Assigned on Wednesday November 17. Deadline: Tuesday December 7. Special help sessions on: Dec 4, 5, 6, and 7 [To be announced] Special class: Sunday Dec 12, 2010. 47pm. LWSN 3102 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 3 Review: Thread interleaving, shared data, race, synchronizaXon 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 4 Interleaving There is usually no guarantee regarding the sequence in which statements in two or more threads will be executed. Write access to shared data may lead to unexpected results. 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 5 SynchronizaXon Use synchronized methods to allow sequenXal access to shared data that may be over wriMen by threads. No need to synchronize access to shared data that is readonly. In CS 180 we have only covered synchronized methods; other ways of synchronizaXon are also possible. 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 6 Project 5: Basics 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 7 Class BufferedImage import java.awt.Image; // Declare im to be an object of type BufferedImage BufferedImage im; 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 8 A BufferedImage An array of pixels: pixel (0,0) int w=im.getWidth() int h=im.getHeight() height (h) pixel (i, j) 0<=i<width 0<=j<height width (w) pixel (w,h) 9 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 A pixel A collecXon of RGB values in the default color model that we will use [R: Red, G: Green, B: Blue]. Each R, G, and B is an 8bit integer with values ranging from 0 to 255. A combinaXon of values of R, G, and B gives a certain color to a pixel. 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 10 Reading a BufferedImage import java.awt.Image; import javax.imageio.ImageIO; // Declare im to be an object of type BufferedImage // and read from a file BufferedImage im; try{ im=ImageIO.read(new File("Flower.jpg")); }catch(ExcepXon e){ System.out.println("File read unsuccessful"); } 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 11 Displaying a BufferedImage JLabel l=new JLabel(); // Create a label l.setHorizontalAlignment(JLabel.CENTER); // Set alignment; l.setIcon(new ImageIcon(im));// Create and add an ImageIcon JFrame f=new JFrame("A Flower"); // Create a frame f.add(l, BorderLayout.CENTER); // Add label 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 12 ExtracXng RGB from a pixel // 1. Get RGB of pixel (i,j) int rgb=im.getRGB(i,j); // 2. Make a color object Color pixel=new Color(rgb); // 3. Extract colors from pixel int red=pixel.getRed(); int green=pixel.getGreen(); int blue=pixel.getBlue(); 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 13 Modifying a pixel // 1. Make a Color object with red, green, and blue Color newPixel=new Color(red, green, blue); // 2. Get RGB of the new pixel int rgb=newPixel.getRGB(); // 3. Change pixel (i,j) of image im im.setRGB(i, j, rgb); 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 14 Image filtering: Removing colors x: color value from the image. R=x, G=0, B=0 R=0, G=0, B=x See a Java program to obtain the filtered images at the course Web site. R, G, B denote the color components of RGB. 11/22/2010 R=0, G=x, B0 Aditya Mathur. CS 180. Fall 2010. Week 14 15 Project 5: The Program 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 16 Tasks Image mirroring Image reflecXon Image adjustment Pixel rotaXon Only one task performed at a Xme. 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 17 Image as an array of pixels 0 0 1 1 w1 x x x .... x x x x .... x . . h1 x x x .... x pixels 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 18 Threads, Image, and the Dispatcher Each thread reads "its" pixel, processes it, an writes it back. 0 1 0 1 w1 x x x .... x x x x .... x . . h1 Th 1 Th 2 Th 3 x x x .... x Dispatcher Dispatcher gives a row or a column to a thread unXl the enXre image is processed. 19 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 SynchronizaXon What should be synchronized? Access to a pixel? Access to the image? Access to dispatcher? 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 20 Week 14: November 2226, 2010 Hope you enjoyed this week! QuesXons? Contact your recitaXon instructor. Make full use of our office hours. 11/22/2010 Aditya Mathur. CS 180. Fall 2010. Week 14 21 ...
View Full Document

This note was uploaded on 02/05/2012 for the course CS 180 taught by Professor Staff during the Fall '08 term at Purdue.

Ask a homework question - tutors are online