Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 14: Nov 21-25, 2011 Aditya Mathur Department of Computer Science Purdue.

Similar presentations


Presentation on theme: "CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 14: Nov 21-25, 2011 Aditya Mathur Department of Computer Science Purdue."— Presentation transcript:

1 CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 14: Nov 21-25, 2011 Aditya Mathur Department of Computer Science Purdue University West Lafayette, IN, USA http://www.cs.purdue.edu/homes/apm/courses/CS180Fall2011/ This Week: 11/21 1.Review 2.Class BufferedImage 3.Pixel operations 4.Project 5

2 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 ©Aditya Mathur. CS 180. Fall 2011. Week 14

3 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. 4-7pm. LWSN 3102 ©Aditya Mathur. CS 180. Fall 2011. Week 14

4 Review: Thread interleaving, shared data, race, synchronization ©Aditya Mathur. CS 180. Fall 2011. Week 14

5 Interleaving ©Aditya Mathur. CS 180. Fall 2011. Week 14 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.

6 Synchronization ©Aditya Mathur. CS 180. Fall 2011. Week 14 Use synchronized methods to allow sequential access to shared data that may be over written by threads. No need to synchronize access to shared data that is read-only. In CS 180 we have only covered synchronized methods; other ways of synchronization are also possible.

7 ©Aditya Mathur. CS 180. Fall 2011. Week 14 Project 5: Basics

8 ©Aditya Mathur. CS 180. Fall 2011. Week 14 Class BufferedImage import java.awt.Image; // Declare im to be an object of type BufferedImage BufferedImage im;

9 ©Aditya Mathur. CS 180. Fall 2011. Week 14 A BufferedImage int w=im.getWidth() int h=im.getHeight() An array of pixels: height (h) width (w) pixel (0,0) pixel (w,h) pixel (i, j) 0<=i<width 0<=j<height

10 ©Aditya Mathur. CS 180. Fall 2011. Week 14 A pixel A collection 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 8-bit integer with values ranging from 0 to 255. A combination of values of R, G, and B gives a certain color to a pixel.

11 ©Aditya Mathur. CS 180. Fall 2011. Week 14 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(Exception e){ System.out.println("File read unsuccessful"); }

12 ©Aditya Mathur. CS 180. Fall 2011. Week 14 Displaying a BufferedImage JFrame f=new JFrame(“A Flower”); // Create a frame f.add(l, BorderLayout.CENTER); // Add label JLabel l=new JLabel(); // Create a label l.setHorizontalAlignment(JLabel.CENTER); // Set alignment; l.setIcon(new ImageIcon(im));// Create and add an ImageIcon

13 ©Aditya Mathur. CS 180. Fall 2011. Week 14 Extracting 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();

14 ©Aditya Mathur. CS 180. Fall 2011. Week 14 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);

15 ©Aditya Mathur. CS 180. Fall 2011. Week 14 Image filtering: Removing colors See a Java program to obtain the filtered images at the course Web site. R, G, B denote the color components of RGB. R=x, G=0, B=0R=0, G=0, B=x R=0, G=x, B0 x: color value from the image.

16 ©Aditya Mathur. CS 180. Fall 2011. Week 14 Project 5: The Program

17 ©Aditya Mathur. CS 180. Fall 2011. Week 14 Tasks Image mirroring Image reflection Image adjustment Only one task performed at a time. Pixel rotation

18 ©Aditya Mathur. CS 180. Fall 2011. Week 14 Image as an array of pixels x x x …. x.... 0 1 h-1 01w-1 pixels

19 ©Aditya Mathur. CS 180. Fall 2011. Week 14 Threads, Image, and the Dispatcher Th 1Th 2Th 3 Dispatcher x x x …. x.... 0 1 h-1 01w-1 Dispatcher gives a row or a column to a thread until the entire image is processed. Each thread reads “its” pixel, processes it, an writes it back.

20 ©Aditya Mathur. CS 180. Fall 2011. Week 14 Synchronization What should be synchronized? Access to a pixel? Access to the image? Access to dispatcher?

21 Week 14: November 22-26, 2010 Hope you enjoyed this week! Questions? Contact your recitation instructor. Make full use of our office hours. ©Aditya Mathur. CS 180. Fall 2011. Week 14


Download ppt "CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 14: Nov 21-25, 2011 Aditya Mathur Department of Computer Science Purdue."

Similar presentations


Ads by Google