1 Images Complex Numbers Mandelbrot & Julia Sets Image File Format (.ppm) C/C++ system() function...

Post on 14-Dec-2015

217 views 1 download

Transcript of 1 Images Complex Numbers Mandelbrot & Julia Sets Image File Format (.ppm) C/C++ system() function...

1

Images Complex Numbers Mandelbrot & Julia Sets Image File Format (.ppm) C/C++ system() function g++, make & makefiles Debuggers

CSE 30331CSE 30331Lecture 6 – Complex Numbers & Lecture 6 – Complex Numbers &

ImagesImages

2

Quick Aside

Group Project Guidelines … are posted on web page

Due: Tuesday, September 22nd

Initial Group membership Brief description of project you plan to complete Initial references you have found …

3

Images (photographic)

4

Images (fractal)

5

Image Representation

An image is a rectangular grid of pixels

A pixel is a single picture element

Each pixel has a value representing the color (or intensity) of a single point in the image

Image size is in pixels (640 x 480, etc.)

Image resolution is in pixels / inch

6

Pixel Color

Pixel size # of colors possible

1 bit 2 (Black or White)

Binary image

1 byte (8 bit) 256 shades of gray or

256 distinct colors

3 byte (24 bit) 224 colors (true color)

(millions of colors)

7

Color Maps

If each pixel is a single byte, its value is often used as an index into a color map

(a table of actual 3 byte color codes)

pixel red green blue

0 0 0 0

1 20 0 0

2 100 100 0

3 0 255 255

255 255 255 255

Black

Dark Red

Medium Yellow

Bright Cyan

White

8

Pixel Classclass pixel{public:

pixel (unsigned char r = 0, unsigned char g = 0, unsigned char b = 0)

: red(r), green(g), blue(b) { }setColor (unsigned char r, unsigned char g,

unsigned char b){ red = r; green = g; blue = b; }getColor(unsigned char &r, unsigned char &g,

unsigned char &b)( r = red; g = green; b = blue; }

private:unsigned char

red, green, blue; // true color components};

9

Image in memory

// matrix template class found in Ford & Topp Ch 5// is a 2-D grid using a vector of vectors#include “d_matrix.h”

// declare white image of 500 x 500 pixelsmatrix<pixel> image(500,500,pixel(255,255,255));

// set color of pixel3,4

image[3][4].setColor(100,20,255);

// get color (r,g,b) of pixeli,j unsigned char r,g,b;image[i][j].getColor(r,g,b);

10

Complex Numbers

External format: a + b j a and b are real coefficients j is sqrt(-1)

Represents a point on a 2D Cartesian plane Real (horizontal) axis Imaginary (vertical) axis

Addition x1 + x2 =

(a1 + b1 j) + (a2 + b2 j) = (a1 + a2) + (b1 + b2) j

Subtraction x1 - x2 =

(a1 + b1 j) - (a2 + b2 j) = (a1 - a2) + (b1 - b2) j

11

Complex Numbers

Multiplication x1 * x2 =

(a1 + b1 j) * (a2 + b2 j) = (a1a2 - b1b2) + (a1b2 + a2b1) j

Division x1 / x2 =

(Note: multiply top & bottom by complex conjugate) (a1 + b1 j) / (a2 + b2 j) =

((a1 + b1 j) * (a2 - b2 j)) / ((a2 + b2 j) * (a2 - b2 j)) =

((a1a2+b1b2) / (a2a2+b2b2)) + ((a2b1-a1b2) / (a2a2+b2b2)) j

12

Complex Number Plane

13

Mandelbrot & Julia Sets

Both based on repeated (recursive) application of the following function, where C and Z are both complex numbers

Zn = Zn-1*Zn-1 + C

If the distance of Zn from the origin never exceeds 2.0 then the original point is a member of the set

100 applications of the function is a sufficient test

14

Mandelbrot Set Images

15

Mandelbrot Sets There is ONLY ONE Mandelbrot Set Initial conditions are ….

Z1 = 0 + 0 j and C = a complex number corresponding to a point on the complex

number plane in the range -2.25 .. +0.75 real and -1.5 .. +1.5 imaginary (also corresponding to a pixel in the image being produced)

Zn = Zn-1*Zn-1 + C

For each pixel (complex number C) apply the function and count the number of applications before the magnitude(Zn) > 2.0

If count == 100 then C is in the set, color it Black If count < 100 then C is not in the set, color it based on the count,

indicating the “speed” with which it departed

16

Julia Set Images

17

Julia Sets There are infinitely many Julia Sets (one for each constant C) Initial conditions are ….

Z1 = a complex number corresponding to a point on the complex number plane in the range -1.5 .. +1.5 real and -1.5 .. +1.5 imaginary (also corresponding to a pixel in the image)

C is another complex number chosen and held constant during tests of all other points

Zn = Zn-1*Zn-1 + C

For each pixel (complex number Z1) apply the function and count the number of applications before the magnitude(Zn) > 2.0

If count == 100 C is in the set, color it Black If count < 100 C is not in the set, color it based on the count, indicating

the “speed” with which it departed

18

Suggested Ranges

Images at least 600 x 600 pixels

Mandelbrot sets (Complex plane)-2.25 .. +0.75 real

-1.5 .. + 1.5 imaginary

Julia Sets (Complex Plane)-1.5 .. +1.5 real

-1.5 .. + 1.5 imaginary

19

Portable Pix Map (PPM)

File format for *.ppm image files

<magic number>

<comment>

<width & height>

<max color value>

<data bytes>

P6

# creator: JHS 9/4/2004

640 480

255

d0^g%8%#$.......<EOF>

20

C++ system() function

Requests operating system to run a command Command may be system command or another

program Examples:

// List all PPM image files in current directorysystem(“ls *.ppm”);

// Start Eye of Gnome (eog) to display m1.pp imagesystem(“eog m1.ppm”);

Requires literal string or C-style char array as argument

21

System()

Example of building and executing command with string class

string prog, filename, command;

cout << “Which viewer (gimp, eog)? ”;

cin >> prog;

system(“ls *.ppm”);

cout << “Enter name of file to display: “;

cin >> filename;

command = prog + “ “ + filename + “ &”;

system(command.c_str());

22

G++

g++ is the GNU C++ compiler Command line options

-c compiles to object file

-o <name> creates named executable

-g compiles to allow debugging

-lm links to math library Examples:

g++ -g -c ctester.cpp

g++ -g -c complex.cpp

g++ -g -o ctester ctester.o complex.o –lm

23

Make & Makefiles Make reads instruction in makefile or Makefile and performs

indicated actions, by recursive application of rules Rules based on targets, dependency lists, and time stamps on files

Rule format:

<target> : <list of files target depends on><tab> <command to build target>

Rule example:

ctester: ctester.o complex.og++ -g -o ctester ctester.o complex.o –lm

24

Using make

To make 1st target in makefile or Makefile make

To make 1st target in some other file make –f <makefile_name>

To make specific target make <target>

25

Phony Targets

Some targets are used to invoke commands BUT NOT actually build a target

They are identified using the term “phony”

Example: note comments beginning with #

# phony target for use in clearing directory# of all object files# use: “make clean”phony: cleanclean:

rm *.o

26

Makefile for programs # 2# CSE 331 Program 2 makefile (JHS 9/10/2004 Notre Dame)

all: prog2_1 ctester prog2_2

prog2_1: prog2_1.cpp myVector.h myMatrix.hg++ -g -o prog2_1 prog2_1.cpp

ctester: ctester.o complex.og++ -g -o ctester ctester.o complex.o

complex.o: complex.cpp complex.hg++ -g -c complex.cpp

ctester.o: ctester.cpp complex.hg++ -g -c ctester.cpp

prog2_2: prog2_2.o complex.og++ -g -o prog2_2 prog2_2.o complex.o

prog2_2.o: prog2_2.cpp complex.h pixel.h myVector.h myMatrix.hg++ -g -c prog2_2.cpp

phony: cleanclean:

rm *.o

27

Debugging in Linux/Unix

Gdb is the command line debugger

ddd is a GUI version of gdb for Linux xxgdb is a GUI version of gdb for Unix/X11

All versions support breakpoints, steps into and out of functions, data value examination, etc.

Program must be compiled with –g option to use debuggers

28

Summary

Image 2D matrix of pixels

Pixel Picture Element Index into color table or true (RGB) color

ColorTable Indexed list or true (RGB) color values for pixels

Complex numbers Real and imaginary components Represent points on 2D complex plane

29

Summary 2

Mandelbrot & Julia Sets Complex numbers attracted to origin (Mandelbrot Set) or to

another point C in the complex plane (Julia Set) Based on recursive function Zn = Zn-1*Zn-1 + C

Mandelbrot Set Initially, Z1 is origin and C is point being tested in plane

Julia Set Initially, Z1 point being tested in plane and C is some point

held constant for entire Julia Set Non-member points are color coded based on value of

n when Zn moves more than 2.0 units from origin, escaping the strange attractor

30

Summary 3

Portable Pix Map (simple image file format)

P6

#comment (creator and date)

width height

max_color

image data in bytes (rgbrgb....)

31

Summary 4

G++ GNU C++ compiler Command line options (-g -o –c ....)

Make Executes rules in makefiles to build targets and perform

other tasks Recursively follows rules back through dependency lists Phony rules execute commands; do not build targets

Debugging ddd, xxgdb, gdb