210 likes | 295 Views
Using the JImageViewer classes. JImageViewer classes. JImageViewer class ImagePanel class Image class. JImageViewer classes. JImageViewer class main program entry point construct one w/ image file name and a new window w/ an image will appear menus & menu callbacks
E N D
JImageViewer classes • JImageViewer class • ImagePanel class • Image class
JImageViewer classes • JImageViewer class • main program entry point • construct one w/ image file name and a new window w/ an image will appear • menus & menu callbacks • implements ActionListener • contains: • mImagePanel • mImage
JImageViewer classes • ImagePanel class • displays image via paint method • double buffered • draws image into temporary buffer • draws location string into temporary buffer • then draws temporary buffer • contains reference to JImageViewer “parent” via mParent • implements MouseMotionListener • updates mMouseX and mMouseY • does NOT listen to clicks • must implement MouseListener to receive mouse clicks (and a whole bunch of other events)
JImageViewer classes • Image class • constructor & image data members • given an image file name, the ctor loads the image into the member data
JImageViewer classes • Image class • members: • mW & mH (width and height of image) • mMin & mMax (min and max scalar pixel value) • mIsColor • True if color • False if gray • mImage • 1D int array of pixel values • Each int is either • a single gray value or • an rgb (red/green/blue) value • Cannot be drawn by Java • mOriginalImage • original image data • mScreenImage • image data actually drawn in window
JImageViewer classes • Image class • mImage • 1D int array of pixel values • each int is either • a single gray value or • an rgb value • What is mImage.length? • If color, how can we get at the individual rgb values? • Each rgb int contains contains: • 8 bits for red • 8 bits for green • 8 bits for blue • sometimes an 8 bits alpha blending value is present as well • How many different colors can we represent? • What is gray in terms of rgb? • How many different shades of grary can we represent?
Representing color • So given an integer containing an argb value, how can we get at the individual component values? • B is the least significant (8 bit) byte. • Bits 7..0 • G is bits 15..8 • R is bits 23..16 • A is bits 31..24
Representing color • B is the least significant (8 bit) byte. • bits 7..0 • int b = mImage[i] & ?;
Representing color • B is the least significant (8 bit) byte. • bits 7..0 • int b = mImage[i] & 0xff;
Representing color • G is bits 15..8 • int g = mImage[i] ?;
Representing color • G is bits 15..8 • int g = (mImage[i] & 0xff00) >> 8;
Representing color • R is bits 23..16 • int r = mImage[i] ?;
Representing color • R is bits 23..16 • int r = (mImage[i] & 0xff0000) >> 16;
Color • Given individual rgb values (as 3 int/bytes), how do we “pack” them into a single int? • int r = 12; • int g = 14; • int b = 92; • int rgb = ?;
Color • Given individual rgb values (as 3 int/bytes), how do we “pack” them into a single int? • int r = 12; • int g = 14; • int b = 92; • int rgb = (r<<16) | (g<<8) | b;
Color • Say we process our image data and wish to change the image that appears on the screen? • Where is the image data? • Where is the displayed image?
Color • Say we process our image data and wish to change the image that appears on the screen? • Where is the image data? • In mImage in the Image class. • Where is the displayed image? • In mScreenImage in the Image class.
Displaying image data • mScreenImage in the Image class is of type BufferedImage. • It is what appears in the window. • Allocated by: mScreenImage = new BufferedImage( mW, mH, BufferedImage.TYPE_INT_RGB ); • It is set/changed by: mScreenImage.setRGB( 0, 0, mW, mH, mImage, 0, mW );
Displaying image data mScreenImage.setRGB( 0, 0, mW, mH, mImage, 0, mW ); • mImage must be in rgb format • What do we do for gray data? • Gray data may be more than 8 bits!