How are images represented in a computer? Image Objects CS111 Computer Programming Department of Computer Science Wellesley College Image Objects Digital Images Digital Images Images are often represented in a computer as a 2-dimensional (2D) array of pixels. Images are often represented in a computer as a 2-dimensional (2D) array of pixels. 20-2 38 pixels high 44 pixels wide 50 x 50 Image Objects 20-3 Image Objects 20-4 1 Representing Pixel Colors Image Formats Instances of the Java Color class have red/green/blue (RGB) components ,each in the range 0 to 255.* Images can be encoded in different ways (e.g., JPEG, GIF, BMP, PNG). One way to represent images is to encode each pixel as an integer. An RGB triple can also be represented as a negative integer that falls in the range -16777216 to -1. * You can play with these representations in the ColorPad applet. Color R G B int -1 0 0 -1 -1 0 0 -1 -1 0 0 0 0 0 0 -1 -1 0 0 -1 -1 0 0 -1 -1 0 0 0 0 0 0 -1 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 black 0 0 0 -16777216 or 0† -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 white 255 255 255 -1 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 -1 0 0 0 0 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 -1 0 0 0 0 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 red 255 0 0 -65536 green 0 255 0 -16711936 blue 0 0 255 -16776961 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 yellow 255 255 0 -256 magenta 255 0 255 -65281 cyan 0 255 255 -16711681 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 bagel brown 200 100 50 -3644366 -1 0 0 -1 -1 0 0 -1 -1 0 0 0 0 0 0 -1 -1 0 0 -1 -1 0 0 -1 -1 0 0 0 0 0 0 -1 * Take CS240 to understand these ranges! † O is a Color synonym for -16777216. Image Objects 16 x 16 20-5 Mirror Image Image Objects 20-6 Checkerboard as 2D array How would you mirror (i.e., flipHorizontally) an image for which each pixel was represented as an integer? -1 0 0 -1 -1 0 0 -1 -1 0 0 0 0 0 0 -1 0 0 0 -1 -1 0 0 -1 -1 0 0 -1 -1 0 0 0 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 0 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 0 0 0 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 -1 0 0 0 0 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 -1 0 0 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 -1 -1 0 0 -1 -1 0 0 -1 -1 0 0 0 0 0 0 -1 -1 0 0 -1 -1 0 0 -1 -1 0 0 0 0 0 0 -1 0 0 ? -65536 -65536 -256 -256 -65536 -65536 -256 -65536 -65536 -256 -256 -65536 -65536 -256 -256 -256 -256 -256 -65536 -65536 -256 -256 -65536 -65536 -256 -256 -65536 -65536 -256 -256 -65536 -65536 -65536 -65536 -256 -256 -65536 -65536 -256 -256 -65536 -65536 -256 -256 -65536 -65536 -256 -256 -256 -256 -65536 -65536 -256 -256 -65536 -65536 -256 -256 -65536 -65536 -256 -256 -65536 -65536 8x8 0 -1 -1 -1 0 -1 -1 -1 Image Objects 20-7 Image Objects 20-8 2 Mona Lisa as 2D array Creating a Class We want to define the data and operations relevant to an image object. What would such a class look like? Pixels starting from the upper left corner: -10389929 -10522028 -10720177 -11049399 … -10324649 -10522028 -10785970 -11049912 … -10259372 -10456751 -10720436 -10921927 … -10259633 -10391732 -10589881 -10853310 … . . . . . . . . . . . . 520 rows x 334 columns Image Objects 20-9 Some Helpful Methods We ve provided a few methods in the class PicOps that may be useful when designing and testing your image class. /* Returns a String representing the path to the * location of the source/class files. */ public static String getFolder(); /* In a new frame with the given name, * displays the corresponding RGB image * specified by the 2D array of ints. */ public static void display (String name, int[][] pix); /* In a new frame, displays the corresponding RGB image * specified by the 2D array of ints. */ public static void display (int[][] pix); /* In a new frame, displays the corresponding RGB image * specified by the given image file name. The specified * image file can be of type JPEG, GIF, PNG, BMP, or a * TXT file containing a 2D integer array representation * of an image. */ public static void display (String imageFileName); Image Objects 20-11 Image Objects 20-10 Some Helpful Methods (2) We ve provided a few methods in the class PicOps that may be useful when designing and testing your image class. /* Given the name of a TXT file containing a 2D integer * array representation of an RGB image, returns a new * 2D integer array that corresponds to the RGB image * represented in the TXT file. */ public static int[][] readInTextFile(String textFileName) throws FileNotFoundException; /* Outputs the given 2D integer array to a TXT file with * the given name, so that the TXT file contains a 2D * integer array representation of an RGB image. */ public static void outputTextFile(String textFileName, int[][] pix) throws FileNotFoundException; /* Given an image file of type JPEG, GIF, PNG, or BMP, * outputs a TXT file containing a 2D integer array * representation of the RGB image. */ public static void toTextFile (String imageFileName); /* Outputs a JPEG file corresponding to the RGB image represented in * the given TXT file as a 2D integer array. */ Image Objects 20-12 public static void textFileToJPEG(String textFileName); 3
© Copyright 2026 Paperzz