CSE Lecture 6 – Complex Numbers & Images

Presentation on theme: "CSE Lecture 6 – Complex Numbers & Images"— Presentation transcript:

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

Quick Aside Group Project Guidelines Due: Tuesday, September 22nd
… 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

Images (photographic)

Images (fractal)

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

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)

Color Maps red green blue 1 20 2 100 3 255 … pixel
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 1 20 2 100 3 255 Black Dark Red Medium Yellow Bright Cyan White

Pixel Class class 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 };

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 pixels matrix<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);

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

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

Complex Number Plane

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

Mandelbrot Set Images

Mandelbrot Sets There is ONLY ONE Mandelbrot Set
Initial conditions are …. Z1 = j and C = a complex number corresponding to a point on the complex number plane in the range real and 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

Julia Set Images

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 real and 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

Suggested Ranges Images at least 600 x 600 pixels
Mandelbrot sets (Complex plane) real imaginary Julia Sets (Complex Plane) real

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 255 d0^g%8%#\$ <EOF>

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 directory system(“ls *.ppm”); // Start Eye of Gnome (eog) to display m1.pp image system(“eog m1.ppm”); Requires literal string or C-style char array as argument

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());

G++ g++ is the GNU C++ compiler Command line options Examples:
-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

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.o g++ -g -o ctester ctester.o complex.o –lm

Using make To make 1st target in makefile or Makefile
To make 1st target in some other file make –f <makefile_name> To make specific target make <target>

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: clean clean: rm *.o

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.h g++ -g -o prog2_1 prog2_1.cpp ctester: ctester.o complex.o g++ -g -o ctester ctester.o complex.o complex.o: complex.cpp complex.h g++ -g -c complex.cpp ctester.o: ctester.cpp complex.h g++ -g -c ctester.cpp prog2_2: prog2_2.o complex.o g++ -g -o prog2_2 prog2_2.o complex.o prog2_2.o: prog2_2.cpp complex.h pixel.h myVector.h myMatrix.h g++ -g -c prog2_2.cpp phony: clean clean: rm *.o

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

Summary Image Pixel ColorTable Complex numbers 2D matrix of pixels
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

Summary 2 Mandelbrot & Julia Sets Mandelbrot Set Julia Set
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

Summary 3 Portable Pix Map (simple image file format) P6
#comment (creator and date) width height max_color image data in bytes (rgbrgb....)

Summary 4 G++ Make Debugging 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