Brent M. Dingle, Ph.D. 2015 Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout HTML5 – Canvas.

Slides:



Advertisements
Similar presentations
Cascading Style Sheets
Advertisements

JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Using Bitmap graphics Having looked at vector graphics in the canvas, we will now look at using bitmap graphics.
Neal Stublen Why Use a Canvas?  Flexibility… Use of drawing primitives (lines, arcs, text) Direct access to element display pixels.
JavaScript (Part 2) CS 183 4/13/2010. JavaScript (Part 2) Will cover: ◦ For.. In ◦ Events ◦ Try.. Catch, Throw ◦ Objects.
Aim: Use the given examples to record examples and our own ideas in our journal 1.Write technical examples in journal and/or participate in full.
Chapter 20 Thinking Big: Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Anatomy of a Function Functions are packages for.
Computer Science 103 Chapter 4 Advanced JavaScript.
Programming Games Computer science big ideas. Computer Science jargon. Show virtual dog Homework: [Catch up: dice game, credit card or other form.] Plan.
JQuery Page Slider. Our goal is to get to the functionality of the Panic Coda web site.Panic Coda web site.
CS 102 Computers In Context (Multimedia)‏ 01 / 28 / 2009 Instructor: Michael Eckmann.
HTML, HTML5 Canvas, JavaScript PART 3 LOOKING MORE CLOSELY AT FULL BLOWN GAME DESIGN A PATTERNED BACKGROUND (CREATED WTIH LOOPS) WITH A MOVING OBJECT CHALLENGES.
Image Processing & Perception Sec 9-11 Web Design.
CS 101: Arrays Abhiram Ranade. Computers manipulate many objects Given the position, masses, and velocities of all stars, where will they be after the.
1 Perception, Illusion and VR HNRS 299, Spring 2008 Lecture 14 Introduction to Computer Graphics.
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout HTML5 – Canvas.
1 CSC 121 Computers and Scientific Thinking David Reed Creighton University HTML and Web Pages.
Getting Started with Canvas IDIA Spring 2013 Bridget M. Blodgett.
HTML5 CANVAS. SAVING INFORMATION BETWEEN FUNCTION CALLS.
Session: 17. © Aptech Ltd. 2Canvas and JavaScript / Session 17  Describe Canvas in HTML5  Explain the procedure to draw lines  Explain the procedure.
Computer Science 101 Images in Web Pages. Image Files Two common formats, GIF and JPEG GIF images are more flexible for use as icons JPEG images are sharper.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.
CS 174: Web Programming September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout Filtering.
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout Quick Review.
CS112 Scientific Computation Department of Computer Science Wellesley College Numb3rs Number and image types.
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
CS 174: Web Programming October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Representation of Images You need to know: (k) explain the representation of an image as a series of pixels represented in binary (l) explain the need.
Digital Image Processing
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout HTML5 – Canvas.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout Edge Detection.
CS 376b Introduction to Computer Vision 02 / 11 / 2008 Instructor: Michael Eckmann.
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout Edge Detection:
02/05/2002 (C) University of Wisconsin 2002, CS 559 Last Time Color Quantization Mach Banding –Humans exaggerate sharp boundaries, but not fuzzy ones.
Programming games Context of what we are doing. Drawing on canvas. Homework: [Complete coin toss examples.] Do your own drawings. Upload files to website.
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout More on Colors.
Digital Image Processing
Programming games Review concepts. Crooked coin toss. Drawing on canvas. Homework: Complete [static] drawings. Upload files to website.
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:
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout Image Processing.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
08 – Canvas(2) Informatics Department Parahyangan Catholic University.
07 – HTML5 Canvas (1) Informatics Department Parahyangan Catholic University.
Digital Image: Rendering of a continuously varying scene with a finite array of picture elements, where each one has a discrete intensity or color 39.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
HTML5 and CSS3 Illustrated Unit F: Inserting and Working with Images.
HTML5 ProgLan HTML5 Making a Game on the Canvas Part 1.
Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout Color Image.
Multidimensional Arrays
That Gauge is COOL! 
What is a Function Expression?
SVG & DOM Manipulation via Javascript
David Meredith Aalborg University
Internet of the Past.
CMPE 280 Web UI Design and Development September 12 Class Meeting
Image Warping (Geometric Transforms)
Microprocessor and Assembly Language
ISC440: Web Programming II Ch14: HTML5 Canvas
The Canvas.
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
Looping through pixels.
Digital Image Processing
Multidimensional Arrays
HTML5 – Canvas JavaScript Simple Drawing
Multimedia System Image
JavaScript – Let’s Draw!
Presentation transcript:

Brent M. Dingle, Ph.D Game Design and Development Program Mathematics, Statistics and Computer Science University of Wisconsin - Stout HTML5 – Canvas JavaScriptPixels Image Processing

Lecture Objectives Provide Examples – Basic HTML5 canvas – Pixel Manipulation – “Loading” Images

What this is Not To complete your projects – You must learn more about HTML5 and JavaScript than what is about to be shown This is an “on-your-own” activity – Instructor can help, but you must try on your own A prereq to this course is CS 244 – So you have programmed before – This stuff is “easy” compared to that =) – Likewise on the math topics In Sum: The following is just a place to start – More examples will follow throughout the course

Previously Digital Image Processing (DIP) Is computer manipulation of pictures, or images, that have been converted into numeric form A Digital Image Is a picture or image converted to numeric form In grey-scale the image can be thought of as – 2D function f(x, y) or a matrix – x, y, and f(x, y) are discrete and finite – Image size = (x max ) by (y max ), e.g x 768 – Pixel Intensity Value = f(x,y)  [0, 255]

HTML5/JS: Direct Pixel Manipulation Three basic things you can do – Create an array of empty pixels – Get an array of pixels from an existing canvas – Set the pixels of a canvas using a pixel array

Pixel Array JavaScript arrays work like C/C++/Java – Use the standard accessors to index into the array EX: – mya[0] is the first element in the array named mya – mya[k-1] is the k-th element in the array named mya – Pixels in the array are in row-major order with values of 0 to 255 where each four-integer group represents the four color channels: Red-Green-Blue-Alpha or RGBA illustration next slide

Pixel Order image from:

Creating a Noise Image Use context’s function: createImageData() Create a new function: SetPixel() Use context’s function: putImageData() createImage.html Your browser does NOT support canvas! Same basic HTML as previous examples JavaScript filename changed to createImage.js

Creating a Noise Image Use context’s function: createImageData() Create a new function: SetPixel() Use context’s function: putImageData() createImage.html Your browser does NOT support canvas! Questions on the HTML file ?

Creating a Noise Image Use context’s function: createImageData() Create a new function: SetPixel() Use context’s function: putImageData() createImage.js var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // Get the size of the canvas as declared in the HTML var width = theCanvas.width; var height = theCanvas.height; // Create an array of pixels the same size as the canvas imageData = ctx.createImageData(width, height);

Creating a Noise Image Use context’s function: createImageData() Create a new function: SetPixel() Use context’s function: putImageData() createImage.js var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // Get the size of the canvas as declared in the HTML var width = theCanvas.width; var height = theCanvas.height; // Create an array of pixels the same size as the canvas imageData = ctx.createImageData(width, height); Note the following imageData.width  width of the image data in PIXELS imageData.height  height of the image daa in PIXELS imageData.data  pixel data array of (width * height * 4) elements

Creating a Noise Image Use context’s function: createImageData() Create a new function: SetPixel() Use context’s function: putImageData() createImage.js var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // Get the size of the canvas as declared in the HTML var width = theCanvas.width; var height = theCanvas.height; // Create an array of pixels the same size as the canvas imageData = ctx.createImageData(width, height); SetPixel Function (x, y) is image coordinate r = red, g = green, b = blue, a = alpha for alpha --> 255 is opaque and 0 is transparent SetPixel: function(imageData, x, y, r, g, b, a) { var index = (x + y * imageData.width) * 4; imageData.data[index + 0] = r; imageData.data[index + 1] = g; imageData.data[index + 2] = b; imageData.data[index + 3] = a; },

Creating a Noise Image Use context’s function: createImageData() Create a new function: SetPixel() Use context’s function: putImageData() createImage.js var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // Get the size of the canvas as declared in the HTML var width = theCanvas.width; var height = theCanvas.height; // Create an array of pixels the same size as the canvas imageData = ctx.createImageData(width, height); // Draw random dots for (var i = 0; i < 15000; i++) // is arbitrary { var x = Math.random() * width | 0; var y = Math.random() * height | 0; var r = Math.random() * 256 | 0; var g = Math.random() * 256 | 0; var b = Math.random() * 256 | 0; theProgram.SetPixel(imageData, x, y, r, g, b, 255); } // Put the image data onto the canvas ctx.putImageData(imageData, 0, 0); }, SetPixel: function(imageData, x, y, r, g, b, a) { var index = (x + y * imageData.width) * 4; imageData.data[index + 0] = r; imageData.data[index + 1] = g; imageData.data[index + 2] = b; imageData.data[index + 3] = a; },

Creating a Noise Image Use context’s function: createImageData() Create a new function: SetPixel() Use context’s function: putImageData() createImage.js var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // Get the size of the canvas as declared in the HTML var width = theCanvas.width; var height = theCanvas.height; // Create an array of pixels the same size as the canvas imageData = ctx.createImageData(width, height); // Draw random dots for (var i = 0; i < 15000; i++) // is arbitrary { var x = Math.random() * width | 0; var y = Math.random() * height | 0; var r = Math.random() * 256 | 0; var g = Math.random() * 256 | 0; var b = Math.random() * 256 | 0; theProgram.SetPixel(imageData, x, y, r, g, b, 255); } // Put the image data onto the canvas ctx.putImageData(imageData, 0, 0); }, The “ | 0 ” is to truncate to integer value SetPixel: function(imageData, x, y, r, g, b, a) { var index = (x + y * imageData.width) * 4; imageData.data[index + 0] = r; imageData.data[index + 1] = g; imageData.data[index + 2] = b; imageData.data[index + 3] = a; },

Creating a Noise Image Use context’s function: createImageData() Create a new function: SetPixel() Use context’s function: putImageData() createImage.js var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // Get the size of the canvas as declared in the HTML var width = theCanvas.width; var height = theCanvas.height; // Create an array of pixels the same size as the canvas imageData = ctx.createImageData(width, height); // Draw random dots for (var i = 0; i < 15000; i++) // is arbitrary { var x = Math.random() * width | 0; var y = Math.random() * height | 0; var r = Math.random() * 256 | 0; var g = Math.random() * 256 | 0; var b = Math.random() * 256 | 0; theProgram.SetPixel(imageData, x, y, r, g, b, 255); } // Put the image data onto the canvas ctx.putImageData(imageData, 0, 0); }, 255 is opaque SetPixel: function(imageData, x, y, r, g, b, a) { var index = (x + y * imageData.width) * 4; imageData.data[index + 0] = r; imageData.data[index + 1] = g; imageData.data[index + 2] = b; imageData.data[index + 3] = a; },

Creating a Noise Image Use context’s function: createImageData() Create a new function: SetPixel() Use context’s function: putImageData() createImage.js var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // Get the size of the canvas as declared in the HTML var width = theCanvas.width; var height = theCanvas.height; // Create an array of pixels the same size as the canvas imageData = ctx.createImageData(width, height); // Draw random dots for (var i = 0; i < 15000; i++) // is arbitrary { var x = Math.random() * width | 0; var y = Math.random() * height | 0; var r = Math.random() * 256 | 0; var g = Math.random() * 256 | 0; var b = Math.random() * 256 | 0; theProgram.SetPixel(imageData, x, y, r, g, b, 255); } // Put the image data onto the canvas ctx.putImageData(imageData, 0, 0); }, SetPixel: function(imageData, x, y, r, g, b, a) { var index = (x + y * imageData.width) * 4; imageData.data[index + 0] = r; imageData.data[index + 1] = g; imageData.data[index + 2] = b; imageData.data[index + 3] = a; }, place data on the canvas starting at the upper left corner => (0, 0)

window.onload = function() { theProgram.Main(); }; Creating a Noise Image createImage.js var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // Get the size of the canvas as declared in the HTML var width = theCanvas.width; var height = theCanvas.height; // Create an array of pixels the same size as the canvas imageData = ctx.createImageData(width, height); // Draw random dots for (var i = 0; i < 15000; i++) // is arbitrary { var x = Math.random() * width | 0; var y = Math.random() * height | 0; var r = Math.random() * 256 | 0; var g = Math.random() * 256 | 0; var b = Math.random() * 256 | 0; theProgram.SetPixel(imageData, x, y, r, g, b, 255); } // Put the image data onto the canvas ctx.putImageData(imageData, 0, 0); }, SetPixel: function(imageData, x, y, r, g, b, a) { var index = (x + y * imageData.width) * 4; imageData.data[index + 0] = r; imageData.data[index + 1] = g; imageData.data[index + 2] = b; imageData.data[index + 3] = a; }, }; // end theProgram Variable

window.onload = function() { theProgram.Main(); }; Creating a Noise Image createImage.js var theProgram = { Main: function() { theCanvas = document.getElementById("myCanvas"); ctx = theCanvas.getContext("2d"); // Get the size of the canvas as declared in the HTML var width = theCanvas.width; var height = theCanvas.height; // Create an array of pixels the same size as the canvas imageData = ctx.createImageData(width, height); // Draw random dots for (var i = 0; i < 15000; i++) // is arbitrary { var x = Math.random() * width | 0; var y = Math.random() * height | 0; var r = Math.random() * 256 | 0; var g = Math.random() * 256 | 0; var b = Math.random() * 256 | 0; theProgram.SetPixel(imageData, x, y, r, g, b, 255); } // Put the image data onto the canvas ctx.putImageData(imageData, 0, 0); }, SetPixel: function(imageData, x, y, r, g, b, a) { var index = (x + y * imageData.width) * 4; imageData.data[index + 0] = r; imageData.data[index + 1] = g; imageData.data[index + 2] = b; imageData.data[index + 3] = a; }, }; // end theProgram Variable Questions on the JavaScript?

Challenge Modify the previous program to display an image that is not so random – make it make circles – or alternating colored lines – or…

Load Image We will now walk through a program that starts like:

Turn it Red And when the user clicks on the right side…

loadImage.html Example: Load Image Data Your browser does not support the HTML5 canvas tag. Same as previous examples… but using a more detailed id name for the canvas

loadImage.html Example: Load Image Data Your browser does not support the HTML5 canvas tag. Questions on the HTML file ?

loadImage.js // // theProgram (singleton) Object // var theProgram = { // // Pseudo-constants // IMAGE_CANVAS_ID: "mainImageCanvas", // canvas id used in html // // Variables // width: 400, // canvas width... likely will be reset height: 400, // canvas height... likely will be reset imageObj: null, xOffset: 5, yOffset: 5, Declare and initialize the Member variables of the singleton object theProgram

loadImage.js // Variables // width: 400, // canvas width... likely will be reset height: 400, // canvas height... likely will be reset imageObj: null, xOffset: 5, yOffset: 5, // // Functions // // Main // Main: function() { var canvas = document.getElementById(this.IMAGE_CANVAS_ID); canvas.addEventListener('click', theProgram.onClick, false); canvas.addEventListener('touch', theProgram.onClick, false); this.imageObj = new Image(); this.imageObj.onload = function() { theProgram.drawOrigImage(); }; this.imageObj.src = 'bobcatCactus.png'; }, // // EventHandler onClick Our desired Entry-point function

loadImage.js // Variables // width: 400, // canvas width... likely will be reset height: 400, // canvas height... likely will be reset imageObj: null, xOffset: 5, yOffset: 5, // // Functions // // Main // Main: function() { var canvas = document.getElementById(this.IMAGE_CANVAS_ID); canvas.addEventListener('click', theProgram.onClick, false); canvas.addEventListener('touch', theProgram.onClick, false); this.imageObj = new Image(); this.imageObj.onload = function() { theProgram.drawOrigImage(); }; this.imageObj.src = 'bobcatCactus.png'; }, // // EventHandler onClick Register a call-back function for click and touch events

loadImage.js // Variables // width: 400, // canvas width... likely will be reset height: 400, // canvas height... likely will be reset imageObj: null, xOffset: 5, yOffset: 5, // // Functions // // Main // Main: function() { var canvas = document.getElementById(this.IMAGE_CANVAS_ID); canvas.addEventListener('click', theProgram.onClick, false); canvas.addEventListener('touch', theProgram.onClick, false); this.imageObj = new Image(); this.imageObj.onload = function() { theProgram.drawOrigImage(); }; this.imageObj.src = 'bobcatCactus.png'; }, // // EventHandler onClick Load the image file named: bobcatCactus.png Register a callback function to draw it to the canvas when the file is loaded into the browser Yes it is important to set the onload function BEFORE setting the src

loadImage.js // EventHandler onClick // onClick: function(e) { theProgram.drawOrigImage(); theProgram.drawBWimage(); }, // // drawOrigImage // drawOrigImage: function() { var canvas = document.getElementById(this.IMAGE_CANVAS_ID); var ctx = canvas.getContext('2d'); ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.drawImage(this.imageObj, this.xOffset, this.yOffset); ctx.font="16px Georgia"; ctx.fillText("Click/tap to see the", this.imageObj.width+20, 80); ctx.fillText("black and white image", this.imageObj.width+20, 100); }, // // drawBWimage -- must call drawOrigImage before this Function that is called when the image file named: bobcatCactus.png is loaded into the browser

loadImage.js // EventHandler onClick // onClick: function(e) { theProgram.drawOrigImage(); theProgram.drawBWimage(); }, // // drawOrigImage // drawOrigImage: function() { var canvas = document.getElementById(this.IMAGE_CANVAS_ID); var ctx = canvas.getContext('2d'); ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.drawImage(this.imageObj, this.xOffset, this.yOffset); ctx.font="16px Georgia"; ctx.fillText("Click/tap to see the", this.imageObj.width+20, 80); ctx.fillText("black and white image", this.imageObj.width+20, 100); }, // // drawBWimage -- must call drawOrigImage before this Gets the canvas and context

loadImage.js // EventHandler onClick // onClick: function(e) { theProgram.drawOrigImage(); theProgram.drawBWimage(); }, // // drawOrigImage // drawOrigImage: function() { var canvas = document.getElementById(this.IMAGE_CANVAS_ID); var ctx = canvas.getContext('2d'); ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.drawImage(this.imageObj, this.xOffset, this.yOffset); ctx.font="16px Georgia"; ctx.fillText("Click/tap to see the", this.imageObj.width+20, 80); ctx.fillText("black and white image", this.imageObj.width+20, 100); }, // // drawBWimage -- must call drawOrigImage before this Clears the canvas

loadImage.js // EventHandler onClick // onClick: function(e) { theProgram.drawOrigImage(); theProgram.drawBWimage(); }, // // drawOrigImage // drawOrigImage: function() { var canvas = document.getElementById(this.IMAGE_CANVAS_ID); var ctx = canvas.getContext('2d'); ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.drawImage(this.imageObj, this.xOffset, this.yOffset); ctx.font="16px Georgia"; ctx.fillText("Click/tap to see the", this.imageObj.width+20, 80); ctx.fillText("black and white image", this.imageObj.width+20, 100); }, // // drawBWimage -- must call drawOrigImage before this Draws the loaded image

loadImage.js // EventHandler onClick // onClick: function(e) { theProgram.drawOrigImage(); theProgram.drawBWimage(); }, // // drawOrigImage // drawOrigImage: function() { var canvas = document.getElementById(this.IMAGE_CANVAS_ID); var ctx = canvas.getContext('2d'); ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.drawImage(this.imageObj, this.xOffset, this.yOffset); ctx.font="16px Georgia"; ctx.fillText("Click/tap to see the", this.imageObj.width+20, 80); ctx.fillText(“altered image", this.imageObj.width+20, 100); }, // // drawBWimage -- must call drawOrigImage before this Displays message telling user to click or tap on the area to see the altered version of the image

loadImage.js // EventHandler onClick // onClick: function(e) { theProgram.drawOrigImage(); theProgram.drawAlteredImage(); }, // // drawOrigImage // drawOrigImage: function() { var canvas = document.getElementById(this.IMAGE_CANVAS_ID); var ctx = canvas.getContext('2d'); ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.drawImage(this.imageObj, this.xOffset, this.yOffset); ctx.font="16px Georgia"; ctx.fillText("Click/tap to see the", this.imageObj.width+20, 80); ctx.fillText(“altered image", this.imageObj.width+20, 100); }, // // drawAlteredImage -- must call drawOrigImage before this This function is called when the user clicks or taps on the area It will draw the original image and then the altered image

loadImage.js // drawAlteredImage -- must call drawOrigImage before this // drawAlteredImage: function() { var canvas = document.getElementById(this.IMAGE_CANVAS_ID); var ctx = canvas.getContext('2d'); // Get the image data from the canvas context var imageData = ctx.getImageData(this.xOffset, this.yOffset, this.imageObj.width, this.imageObj.height); var data = imageData.data; // This gives us the image data in RGBA format - one byte for each // So to iterate over all pixels we would do: //for(var i = 0, n = data.length; i < n; i += 4) // { // var red = data[i]; // var green = data[i + 1]; // var blue = data[i + 2]; // var alpha = data[i + 3]; // } // We must 'create' a new image Function to draw the altered image

loadImage.js // drawAlteredImage -- must call drawOrigImage before this // drawAlteredImage: function() { var canvas = document.getElementById(this.IMAGE_CANVAS_ID); var ctx = canvas.getContext('2d'); // Get the image data from the canvas context var imageData = ctx.getImageData(this.xOffset, this.yOffset, this.imageObj.width, this.imageObj.height); var data = imageData.data; // This gives us the image data in RGBA format - one byte for each // So to iterate over all pixels we would do: //for(var i = 0, n = data.length; i < n; i += 4) // { // var red = data[i]; // var green = data[i + 1]; // var blue = data[i + 2]; // var alpha = data[i + 3]; // } // We must 'create' a new image Get the original image pixel data array CAUTION: This assumes the image is drawn on the canvas already, at (xOffset, yOffset) and is size width x height Hence the need for drawOrigImage to be called before this function There are other ways to do this and avoid this assumption

loadImage.js // drawAlteredImage -- must call drawOrigImage before this // drawAlteredImage: function() { var canvas = document.getElementById(this.IMAGE_CANVAS_ID); var ctx = canvas.getContext('2d'); // Get the image data from the canvas context var imageData = ctx.getImageData(this.xOffset, this.yOffset, this.imageObj.width, this.imageObj.height); var data = imageData.data; // This gives us the image data in RGBA format - one byte for each // So to iterate over all pixels we would do: //for(var i = 0, n = data.length; i < n; i += 4) // { // var red = data[i]; // var green = data[i + 1]; // var blue = data[i + 2]; // var alpha = data[i + 3]; // } // We must 'create' a new image Recall the ordering of the pixel data

loadImage.js // var alpha = data[i + 3]; // } // We must 'create' a new image var SecondImageData = ctx.createImageData(this.imageObj.width, this.imageObj.height); for (var pixelIndex=0; pixelIndex < SecondImageData.data.length; pixelIndex += 4) { SecondImageData.data[pixelIndex ] = data[pixelIndex] * 3; // More red SecondImageData.data[pixelIndex + 1] = data[pixelIndex +1] / 2; // Less green SecondImageData.data[pixelIndex + 2] = data[pixelIndex +2] / 2; // Less blue SecondImageData.data[pixelIndex + 3] = data[pixelIndex +3]; // alpha unchanged } // Want to draw the altered image to the right of original // Offset gives spacing between var SecondImageX = 2*this.xOffset + this.imageObj.width; var SecondImageY = this.yOffset; ctx.putImageData(SecondImageData, SecondImageX, SecondImageY); }, }; // end theProgram variable // // window.ONLOAD Create a new pixel data array of the same size as the original

loadImage.js // var alpha = data[i + 3]; // } // We must 'create' a new image var SecondImageData = ctx.createImageData(this.imageObj.width, this.imageObj.height); for (var pixelIndex=0; pixelIndex < SecondImageData.data.length; pixelIndex += 4) { SecondImageData.data[pixelIndex ] = data[pixelIndex] * 3; // More red SecondImageData.data[pixelIndex + 1] = data[pixelIndex +1] / 2; // Less green SecondImageData.data[pixelIndex + 2] = data[pixelIndex +2] / 2; // Less blue SecondImageData.data[pixelIndex + 3] = data[pixelIndex +3]; // alpha unchanged } // Want to draw the altered image to the right of original // Offset gives spacing between var SecondImageX = 2*this.xOffset + this.imageObj.width; var SecondImageY = this.yOffset; ctx.putImageData(SecondImageData, SecondImageX, SecondImageY); }, }; // end theProgram variable // // window.ONLOAD Loop through the pixel data Increasing the red values Decreasing the green and blue Alpha remains the same

loadImage.js // var alpha = data[i + 3]; // } // We must 'create' a new image var SecondImageData = ctx.createImageData(this.imageObj.width, this.imageObj.height); for (var pixelIndex=0; pixelIndex < SecondImageData.data.length; pixelIndex += 4) { SecondImageData.data[pixelIndex ] = data[pixelIndex] * 3; // More red SecondImageData.data[pixelIndex + 1] = data[pixelIndex +1] / 2; // Less green SecondImageData.data[pixelIndex + 2] = data[pixelIndex +2] / 2; // Less blue SecondImageData.data[pixelIndex + 3] = data[pixelIndex +3]; // alpha unchanged } // Want to draw the altered image to the right of original // Offset gives spacing between var SecondImageX = 2*this.xOffset + this.imageObj.width; var SecondImageY = this.yOffset; ctx.putImageData(SecondImageData, SecondImageX, SecondImageY); }, }; // end theProgram variable // // window.ONLOAD Calculate the offset position to draw the second image  calculate where the upper left corner of the new image is to be placed on the canvas Answer: at position (secondImageX, secondImageY) in canvas coordinates

loadImage.js // var alpha = data[i + 3]; // } // We must 'create' a new image var SecondImageData = ctx.createImageData(this.imageObj.width, this.imageObj.height); for (var pixelIndex=0; pixelIndex < SecondImageData.data.length; pixelIndex += 4) { SecondImageData.data[pixelIndex ] = data[pixelIndex] * 3; // More red SecondImageData.data[pixelIndex + 1] = data[pixelIndex +1] / 2; // Less green SecondImageData.data[pixelIndex + 2] = data[pixelIndex +2] / 2; // Less blue SecondImageData.data[pixelIndex + 3] = data[pixelIndex +3]; // alpha unchanged } // Want to draw the altered image to the right of original // Offset gives spacing between var SecondImageX = 2*this.xOffset + this.imageObj.width; var SecondImageY = this.yOffset; ctx.putImageData(SecondImageData, SecondImageX, SecondImageY); }, }; // end theProgram variable // // window.ONLOAD Draw the altered image on the canvas at the calculated offset

loadImage.js SecondImageData.data[pixelIndex + 2] = data[pixelIndex +2] / 2; // Less blue SecondImageData.data[pixelIndex + 3] = data[pixelIndex +3]; // alpha unchanged } // Want to draw the altered image to the right of original // Offset gives spacing between var SecondImageX = 2*this.xOffset + this.imageObj.width; var SecondImageY = this.yOffset; ctx.putImageData(SecondImageData, SecondImageX, SecondImageY); }, }; // end theProgram variable // // window.ONLOAD // window.onload = function() { // Initialize and Start the game theProgram.Main(); }; // // end of file And make sure when all the contents of the HTML page are loaded into the browser that our program begins i.e. call theProgram.Main()

loadImage.js SecondImageData.data[pixelIndex + 2] = data[pixelIndex +2] / 2; // Less blue SecondImageData.data[pixelIndex + 3] = data[pixelIndex +3]; // alpha unchanged } // Want to draw the altered image to the right of original // Offset gives spacing between var SecondImageX = 2*this.xOffset + this.imageObj.width; var SecondImageY = this.yOffset; ctx.putImageData(SecondImageData, SecondImageX, SecondImageY); }, }; // end theProgram variable // // window.ONLOAD // window.onload = function() { // Initialize and Start the game theProgram.Main(); }; // // end of file Any questions on the JavaScript?

Challenge Alter the above program to change the color image into a grey-scale image Apply a luminance algorithm of: – 0.3*red *green *blue – Also, come to the next class with an explanation of why the green would be such a higher weight

Questions? Beyond D2L – Examples and information can be found online at: Continue to more stuff as needed

Extra Reference Stuff Follows

Credits Much of the content derived/based on slides for use with the book: – Digital Image Processing, Gonzalez and Woods Some layout and presentation style derived/based on presentations by – Donald House, Texas A&M University, 1999 – Bernd Girod, Stanford University, 2007 – Shreekanth Mandayam, Rowan University, 2009 – Igor Aizenberg, TAMUT, 2013 – Xin Li, WVU, 2014 – George Wolberg, City College of New York, 2015 – Yao Wang and Zhu Liu, NYU-Poly, 2015 – Sinisa Todorovic, Oregon State, 2015 – Beej’s Bit Bucket / Tech and Programming Fun – w3schools.com