Multidimensional Arrays

Slides:



Advertisements
Similar presentations
Pixels and Digital Images Yrd. Doc. Dr. Ahmet Sayar Kocaeli Universitesi Bilgisayar Muhendisligi Ileri Bilgisayar Grafikleri.
Advertisements

L.Ghadah R. Hadba CT1514-L1.  Computer Graphics :refers to processing of creating a new image from Geometry, Lighting parameters, Materials and Textures.Using.
1 Cascading Style Sheets Continued Different kinds of selectors in a style sheet –Simple- Pseudo-Class –Contextual- Pseudo-Element –Class Image Styles.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 4 Key Concepts 1 Copyright © Terry Felke-Morris.
Multimedia for the Web: Creating Digital Excitement Multimedia Element -- Graphics.
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.
Graphics in the web Digital Media: Communication and Design
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.
WEB GRAPHICS. The Pixel Computer displays are comprised of rows and columns of pixels. Pixels are indivisible. Some common screen resolutions are:, 600.
Digital Images Chapter 8, Exploring the Digital Domain.
Lab #5-6 Follow-Up: More Python; Images Images ● A signal (e.g. sound, temperature infrared sensor reading) is a single (one- dimensional) quantity that.
THE COLORS OF LIGHT RED, GREEN and BLUE
Copyright © 2003 Pearson Education, Inc. Slide 4-1 Created by, Stephanie Ludi, Rochester Institute of Technology—NY Basic Web Page Construction Graphics.
1 Perception, Illusion and VR HNRS 299, Spring 2008 Lecture 14 Introduction to Computer Graphics.
Digital Image: Representation & Processing (2/2) Lecture-3
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
Graphics and Animation Multimedia Projects Part 2.
1 Web Developer Foundations: Using XHTML Chapter 4 Key Concepts.
Adobe Dreamweaver CS3 Revealed CHAPTER THREE: WORKING WITH TEXT AND IMAGES.
1 Ethics of Computing MONT 113G, Spring 2012 Session 10 HTML Tables Graphics on the Web.
Information Processes and Technology Multimedia: Graphics.
Computer Images Can store color info about each pixel, but makes file BIG Compression for Web 15.
Module 4- Build a Game Look at Me Mod 4 Lesson 3 Graphics.
Multimedia Basics (1) Hongli Luo CEIT, IPFW. Topics r Image data type r Color Model : m RGB, CMY, CMYK, YUV, YIQ, YCbCr r Analog Video – NTSC, PAL r Digital.
Digital Images are represented by manipulating this…
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.
Computer Science 121 Scientific Computing Winter 2014 Chapter 14 Images.
Image File Formats By Dr. Rajeev Srivastava 1. Image File Formats Header and Image data. A typical image file format contains two fields namely Dr. Rajeev.
Graphics and Image Data Representations 1. Q1 How images are represented in a computer system? 2.
Multidimensional Arrays Eric Roberts CS 106A February 17, 2016.
BITMAPPED IMAGES & VECTOR DRAWN GRAPHICS
Representation of image data
CNIT 131 Graphics.
Image Processing Objectives To understand pixel based image processing
More HTML: Tables etc. CSC 102 Lecture 6.
David Meredith Aalborg University
Pixels, Colors and Shapes
Binary Representation in Audio and Images
Chapter 3 Graphics and Image Data Representations
Introduction To Photo Editing SHIELA MAE A. AQUINO SRNHS.
Week 13 - Monday CS 121.
Raster Images CPSC 1030.
"Digital Media Primer" Yue-Ling Wong, Copyright (c)2013 by Pearson Education, Inc. All rights reserved.
JavaScript: Functions.
A computer display is made up of small squares, called pixels.
Graphics and Animation
Chapter 5 Working with Images
Graphics and Animation
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
Web Development & Design Foundations with HTML5 7th Edition
Colors.
CS320n –Visual Programming
Representing Images 2.6 – Data Representation.
Ch2: Data Representation
Using HTML Tables SWBAT: - create tables using HTML
CS 106A, Lecture 18 Practice with 1D and 2D Arrays
Multidimensional Arrays and Image Manipulation
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
GRAPHICAL DATA EXCHANGE FORMATS .jpg .gif .tif.
Multidimensional Arrays
Attribute of heading, <p> and <hr> tag
Attribute of heading, <p> and <hr> tag
Color and Images.
Digital Image Processing
Web4Labels 3.1 Colour and logo definition features
"Digital Media Primer" Yue-Ling Wong, Copyright (c)2013 by Pearson Education, Inc. All rights reserved.
Attribute of heading, <p> and <hr> tag
Graphics and Animation
Presentation transcript:

Multidimensional Arrays Jerry Cain and Eric Roberts CS 106A May 15, 2017

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: var 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 display 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. When JavaScript creates a GImage, it looks for the file in the current directory and then in a subdirectory named images. To make sure that your programs will run on a wide variety of platforms, it is best to use one of the most common image formats: the Graphical Interchange Format (GIF), the Joint Photographic Experts Group (JPEG) format, or the Portable Network Graphics (PNG) format. Image files using these formats typically have the suffixes .gif, .jpg, and .png.

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() { 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 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: var logo = GImage("JTFLogo.png"); var pixels = logo.getPixelArray(); 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) { var 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) { var array = image.getPixelArray(); var height = array.length; var width = array[0].length; for (var i = 0; i < height; i++) { for (var j = 0; j < width; j++) { var 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) { var r = GImage.getRed(pixel); var g = GImage.getGreen(pixel); var b = GImage.getBlue(pixel); return Math.round(0.299 * r + 0.587 * g + 0.114 * b);

The End