1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
const int width = 1024;
const int height = 768;
//const int width = 1024;
//const int height = 768;
bool outputFile(char***); //output the contents of pixel to a file
void calculatePixel(char***, int, double, double, int, int);
//should be passed: pic, iter, x, y, i, j //calculate the value of a single pixel
int main()
{
double x,y;
double xstart,xstep,ystart,ystep;
double xend, yend;
int iter = 0;
long col = 0;
// char pic[height][width][3]; //non dynamic pic
int i,j,k;
int fd = 0;
char*** pic; //dynamic pic
// get data
cout << "Enter xstart, xend, ystart, yend, iterations: " << endl;
cin >> xstart >> xend >> ystart >> yend >> iter;
//START allocation block
pic = new char**[height];
for (int i=0; i < width; i++)
{
pic[i] = new char*[height];
for (int a=0; a<height; a++)
pic[i][a] = new char[3];
}
//END allocation block
// compute the step relative to the resolution of the image
xstep = (xend-xstart)/width;
ystep = (yend-ystart)/height;
// do the calculation
x = xstart;
y = ystart;
for (i=0; i<height; i++)
{
for (j=0; j<width; j++)
{
calculatePixel(pic, iter, x, y, i, j);
x += xstep;
}
y += ystep;
x = xstart;
}
if ( !outputFile(pic) )
{
cerr << "file could not be written. terminating." << endl;
return 0;
}
//START deallocation block
for (int i=0; i < height; i++)
{
for (int a=0; a<width; a++)
delete pic[i][a];
delete pic[i];
}
delete pic;
//END deallocation block
return 0;
}
void calculatePixel(char*** pic, int iter, double x, double y, int i, int j) //calculates a single pixel
{
double z,zi,newz,newzi;
int inset = 0;
double colour;
z = 0;
zi = 0;
inset = 1;
for (int k=0; k<iter; k++)
{
newz = (z*z)-(zi*zi) + x;
newzi = 2*z*zi + y;
z = newz;
zi = newzi;
if(((z*z)+(zi*zi)) > 4)
{
inset = 0;
colour = k;
k = iter;
}
}
if (inset)
{
pic[i][j][0] = 0;
pic[i][j][1] = 0;
pic[i][j][2] = 0;
}
else
{
pic[i][j][0] = (char) (colour / iter * 255);
pic[i][j][1] = (char) (colour / iter * 255 / 2);
pic[i][j][2] = (char) (colour / iter * 255 / 2);
}
}
bool outputFile(char*** pic) //writes image to a file, returns true on success
{
char buffer[100] = {0};
// create an output stream for the file
ofstream ofs;
ofs.open("image.tga", ios::out);
if (ofs.fail())
{
cout << "Error Opening File" << endl;
return false;
}
// prepare the buffer for the TGA format.
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 2;
buffer[8] = 0; buffer[9] = 0;
buffer[10] = 0; buffer[11] = 0;
buffer[12] = (width & 0x00FF); buffer[13] = (width & 0xFF00) >> 8;
buffer[14] = (height & 0x00FF); buffer[15] = (height & 0xFF00) >> 8;
buffer[16] = 24;
buffer[17] = 0;
// write the entire buffer out i.e. tga header
ofs.write((char*)buffer, 18);
// now write the pic out which is stored in pic
ofs.write((char*)pic, width*height*3);
ofs.close();
return true;
}
|