Multidimensional Arrays

Slides:



Advertisements
Similar presentations
Cascading Style Sheets
Advertisements

TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
The Binary Numbering Systems
Tutorial 4: Designing a Web Page with Tables
Using HTML Tables.
First Bytes - LabVIEW. Today’s Session Introduction to LabVIEW Colors and computers Lab to create a color picker Lab to manipulate an image Visual ProgrammingImage.
Data starts with width and height of image Then an array of pixel values (colors) The number of elements in this array is width times height Colors can.
Chapter 3 Adding Images in HTML. Agenda Understanding Web Page Images Prepare Your Images for the Web Insert an Image Specify an Image Size Add Alternative.
Real Numbers and the Decimal Number System
Digital Images The digital representation of visual information.
CS 102 Computers In Context (Multimedia)‏ 01 / 28 / 2009 Instructor: Michael Eckmann.
Georgia Institute of Technology Introduction to Media Computation Barb Ericson Georgia Institute of Technology May 2006.
Image Processing & Perception Sec 9-11 Web Design.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
1 Perception, Illusion and VR HNRS 299, Spring 2008 Lecture 14 Introduction to Computer Graphics.
CS1315: Introduction to Media Computation Picture encoding and manipulation.
CS1Q Computer Systems Lecture 8
© 1999 Rochester Institute of Technology Introduction to Digital Imaging.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
Designing a Web Page with Tables. A text table: contains only text, evenly spaced on the Web page in rows and columns uses only standard word processing.
Computer Science 112 Fundamentals of Programming II Graphics Programming.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
CSC1401. Learning Goals Understand at a conceptual level What is media computation? How does color vision work? How can you make colors with red, green,
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
CS 325 Introduction to Computer Graphics 04 / 12 / 2010 Instructor: Michael Eckmann.
Data Representation. What is data? Data is information that has been translated into a form that is more convenient to process As information take different.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Multidimensional Arrays Eric Roberts CS 106A February 17, 2016.
AP CSP: Pixelation – B&W/Color Images
Multidimensional Arrays
Jerry Cain and Eric Roberts
Image Processing Objectives To understand pixel based image processing
David Meredith Aalborg University
Pixels, Colors and Shapes
Binary Notation and Intro to Computer Graphics
Spring 2010 EECS 110: Homework III.
Strings and Graphics Jerry Cain CS 106J April 12, 2017.
Tutorial 4 Topic: CSS 3.0 Li Xu
Image Processing & Perception
LAB Work 02 MBA 61062: E-Commerce
Building Java Programs
Week 13 - Monday CS 121.
JavaScript: Functions.
Chapter 5 Working with Images
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
CS320n –Visual Programming
Digital Image Processing using MATLAB
Jerry Cain and Eric Roberts
Representing Images 2.6 – Data Representation.
Ch2: Data Representation
Using HTML Tables SWBAT: - create tables using HTML
SSEA Computer Science: Track A
Cascading Style Sheets Color and Font Properties
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
CS 106A, Lecture 18 Practice with 1D and 2D Arrays
JavaScript Arrays.
Multidimensional Arrays and Image Manipulation
slides courtesy of Eric Roberts
Other displays Saving Arrays Using fors to process
slides courtesy of Eric Roberts
slides courtesy of Eric Roberts
Lecture 13: Two-Dimensional Arrays
Binary Representation
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
slides courtesy of Eric Roberts
Tutorial 3 Working with Cascading Style Sheets
Digital Image Processing
CS297 Graphics with Java and OpenGL
Web4Labels 3.1 Colour and logo definition features
Presentation transcript:

Multidimensional Arrays Jerry Cain CS 106AJ November 5, 2018 slides courtesy of Eric Roberts

Multidimensional Arrays Because the elements of an array can be of any JavaScript type, those elements can themselves be arrays. Arrays of arrays are called multidimensional arrays. In JavaScript, you can initialize a multidimensional array by using nested brackets in the initial value specification. For example, the following declaration creates a 3×3 array whose values form a magic square: let magic = [ [ 2, 9, 4 ], [ 7, 5, 3 ], [ 6, 1, 8 ] ]; This declaration creates a two-dimensional array conceptually organized like this: 2 9 4 7 5 3 6 1 8 magic[0][0] magic[0][1] magic[0][2] magic[1][0] magic[1][1] magic[1][2] magic[2][0] magic[2][1] magic[2][2]

Example: Initialize a Multiplication Table The following constant definition initializes a multiplication table for the digits 0 to 9 like this: const MULTIPLICATION_TABLE = [ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], [ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 ], [ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27 ], [ 0, 4, 8, 12, 16, 20, 24, 28, 32, 36 ], [ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45 ], [ 0, 6, 12, 18, 24, 30, 36, 42, 48, 56 ], [ 0, 7, 14, 21, 28, 35, 42, 49, 56, 63 ], [ 0, 8, 16, 24, 32, 40, 48, 56, 64, 72 ], [ 0, 9, 18, 27, 36, 45, 54, 63, 72, 81 ] ]; The product of the single-digit integers x and y appears in MULTIPLICATION_TABLE[x][y].

Exercise: Initialize a Chess Board q k p P R N B Q K r n b q k p P R N B Q K 1 2 3 4 5 6 7

Exercise: Crossword Numbering Write a program to number the squares in a crossword grid if they appear at the beginning of a word running either across or down. What types would you use to represent the data in this grid? How would you represent black squares? What rules can you supply to determine if a square is numbered?

The GImage Class The GImage class is used to model an image from a file. The GImage function itself has the form: GImage(filename, x, y) where filename is the name of a file containing a stored image and x and y are the coordinates of the upper left corner of the image. Because GImage objects are read from the file system, you need to add a "load" event handler (without arguments) that executes once the image object has been configured from the file, as with: let image = GImage(filename, x, y); let callback = function() { code that further manipulates the loaded image }; image.addEventListener("load", callback);

Images and Copyrights Most images that you find on the web are protected by copyright under international law. Before you use a copyrighted image, you should make sure that you have the necessary permissions. For images that appear of the web, the hosting site often specifies what rules apply for the use of that image. For example, images from the www.nasa.gov site can be used freely as long as you include the following citation identifying the source: Courtesy NASA/JPL-Caltech In some cases, noncommercial use of an image may fall under the "fair use" doctrine, which allows some uses of proprietary material. Even in those cases, however, academic integrity and common courtesy both demand that you cite the source of any material that you have obtained from others.

Example of the GImage Class function EarthImage() { let gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT); let image = GImage("EarthImage.png"); image.addEventListener("load", function() { image.scale(GWINDOW_WIDTH / image.getWidth()); gw.add(image, 0, 0); addCitation(gw, "Courtesy NASA/JPL-Caltech "); }); } function EarthImage() { var gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT); var image = GImage("EarthImage.png"); image.scale(GWINDOW_WIDTH / image.getWidth()); gw.add(image, 0, 0); addCitation(gw, "Courtesy NASA/JPL-Caltech "); } function addCitation(gw, text) { var label = GLabel(text); var x = gw.getWidth() - label.getWidth(); var y = gw.getHeight() - CITATION_Y; gw.add(label, x, y); } EarthImage Courtesy NASA/JPL-Caltech

Multidimensional Arrays and Images One of the best examples of multidimensional arrays is an image, which is logically a two-dimensional array of pixels. Consider, for example, the logo for the Java Task Force at the top right. That logo is actually an array of pixels as shown in the expanded diagram at the bottom. The GImage class allows you to convert the data for the image into a two-dimensional array of pixel values. Once you have this array, you can work with the data to change the image.

Pixel Arrays If you have a GImage object, you can obtain the underlying pixel array by calling the getPixelArray method, which returns a two-dimensional array of numbers. For example, if you wanted to get the pixels from the image file JTFLogo.png, you could do so with the following code: let logo = GImage("JTFLogo.png"); logo.addEventEventListener("load", function () { let pixels = logo.getPixelArray(); code that manipulates those pixels and constructs a new image }); The first index in a pixel array selects a row in the image, beginning at the top. The height of the image is therefore given by the expression pixels.length. The second index in a pixel array selects an individual pixel within a row, from left to right. You can use the expression pixels[0].length to determine the width of the image.

Pixel Values Each individual element in a pixel array is an integer in which the 32 bits are interpreted as follows: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 transparency () red green blue The first byte of the pixel value specifies the transparency of the color, which is described in more detail on a later slide. The next three bytes indicate the amount of red, green, and blue in the pixel, in which each value varies from 0 to 255. Together, these three bytes form the RGB value of the color, which is typically expressed using six hexadecimal digits, as in the following examples: 0xFF0000 "Red" 0x0000FF "Blue" 0xFF00FF "Magenta" 0xFFA500 "Orange" 0x808080 "Gray"

Combining Colors of Light

Transparency The first byte of the pixel value specifies the transparency of the color, which indicates how much of the background shows through. This value is often denoted using the Greek letter alpha (). Transparency values vary from 0 to 255. The value 0 is used to indicate a completely transparent color in which only the background appears. The value 255 indicates an opaque color that completely obscures the background. The standard color constants all have alpha values of 255.

Image Manipulation You can use the facilities of the GImage class to manipulate images by executing the following steps: Read an existing image from a file into a GImage object. 1. Call getPixelArray to get the pixels. 2. Write the code to manipulate the pixel values in the array. 3. Call the GImage function to create a new image. Instead of supplying a filename, pass in the transformed pixel array instead. 4. The program on the next slide shows how you can apply this technique to flip an image vertically. The general strategy for inverting the image is simply to reverse the elements of the pixel array.

The flipVertical Function /* * Creates a new image which consists of the bits in the * original flipped vertically around the center line. */ function flipVertical(image) { let array = image.getPixelArray(); array.reverse(); return GImage(array); }

Creating a Grayscale Image As an illustration of how to use the bitwise operators to manipulate colors in an image, the text implements a method called createGrayscaleImage that converts a color image into a black-and-white image, as shown in the sample run at the bottom of this slide. The code to implement this method appears on the next slide. CreateGrayscale

The createGrayscaleImage Function /* Creates a grayscale version of the original image */ function createGrayscaleImage(image) { let array = image.getPixelArray(); let height = array.length; let width = array[0].length; for (let i = 0; i < height; i++) { for (let j = 0; j < width; j++) { let gray = luminance(array[i][j]); array[i][j] = GImage.createRGBPixel(gray, gray, gray); } return GImage(array); /* Calculates the luminance of a pixel using the NTSC formula */ function luminance(pixel) { let r = GImage.getRed(pixel); let g = GImage.getGreen(pixel); let b = GImage.getBlue(pixel); return Math.round(0.299 * r + 0.587 * g + 0.114 * b);

The End