Optimization of Mandelbrot Set Generator?

Hi all, I've got a quick question about creating a bitmap and the most efficient way to do it. I'm making a Mandelbrot Set generator, and right now I've got a buffer set up which is an integer array of color values that I write to the bitmap line by line. My question is, is it more efficient to write the values as an array of integers after I do calculations on one line or every time I calculate the values?

Here's the current version (other .cpp files are not included, but I can provide them if you wish)

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
//main.cpp

#include <stdio.h>
#include <iostream>
#include <cmath>
#include <new>

#include "MandelbrotGenerator.h"

using namespace std;

// MAIN FUNCTION
int main()
{
bool outOfBounds;
string proceed = "n";
char *colorBuffer;
unsigned int pixelCount = 0xFFFFFFFF, xRes, yRes, bufferLength, count, *iterationBuffer, percent, iterations;
double Z, Zi, Zp, Zip;
const float  xStart = -2.1, xEnd = 0.67, yStart = -1.5, yEnd = 1.5;
float xStep, yStep;

BMP bitmapData;	// Constructor is called from BMP.cpp

cout << "Opening data stream to 'MandelbrotSet.bmp'..." << endl;

ofstream dataFile; 

dataFile.open("MandelbrotSet.bmp", ofstream::out | ofstream::trunc | ofstream::binary);

if (!dataFile.is_open())
{
	cout << "Could not open MandelbrotSet.bmp!  Press 'enter' to exit." << endl;
	cin.sync();
	cin.ignore();
	return -1;
}

cout << "Done" << endl << "-----------------------" << endl << "Number of iterations (max is 4,294,967,295)? ";
cin >> iterations;

do
{

	do // This ensures the value of bufferLength cannot exceed max value of unsigned int
	{
		cout << endl << "Number of pixels wide (max is 1,073,741,824)? ";
		cin >> pixelCount;
	} while (pixelCount > 1073741824);
		xRes = pixelCount;
		yRes = floor(pixelCount * 1.1);
													
		// Set the image dimensions based on the input
		bitmapData = setDimensions(xRes, yRes, bitmapData);

		// Get the length of the buffer so the data is padded
		bufferLength = getBufferLength(xRes, bitmapData);

		// Set File and Image Sizes in bitmap struct
		bitmapData = fileSize(bufferLength, bitmapData);

		cout << endl << "Are you sure?  This will take up " << bitmapData.BITMAPFILEHEADER.bfFileSize << " bytes of disk space [y|n] ";
		cin.ignore();
		getline (cin, proceed);
		
	} while (proceed != "y" && proceed != "Y");

cout << endl << "Allocating memory for data arrays..." << endl;

colorBuffer = 		new (nothrow) char [bufferLength];
iterationBuffer =	new (nothrow) unsigned int [pixelCount];

if (colorBuffer == 0 || iterationBuffer == 0)
{
	cout << "Error in memory allocation for data array.  Execution terminated.  Press 'enter' to exit" << endl;
	cin.ignore();
	return -1;
}

// Initialize buffer with all zeroes.  This allows the padded bytes to be defined in the file.
for (int i = 0; i < bufferLength; ++i)
{
	colorBuffer[i] = 0x0;
}

cout << "Done." << endl;

cout << "Now executing calculations and compiling data into MandelbrotSet.bmp..." << endl;

// Write the file header
dataFile.seekp(ios_base::beg);
dataFile.write(reinterpret_cast <char*>(&bitmapData), sizeof(bitmapData));

// Set miscellaneous values needed for calculation
xStep = (float)(xEnd - xStart)/xRes;
yStep = (float)(yEnd - yStart)/yRes;

// Main calculation loop
for (float yPos = yStart; yPos < yEnd; yPos += yStep)
{
        count = 0;
	for (float xPos = xStart; xPos < xEnd; xPos += xStep)
	{
		outOfBounds = false;
		Z = 0;
		Zi = 0;
	
		for (unsigned int k = 0; k < iterations; ++k)
		{
			Zp = Z*Z - Zi*Zi + xPos;
			Zip = 2*Z*Zi + yPos;
			Z = Zp;
			Zi = Zip;
		
			if ((Z*Z + Zi*Zi) > 4)
			{
				k = iterations;
				outOfBounds = true;
			}
		}
		if (outOfBounds)
			iterationBuffer[count] = 0;
		else
			iterationBuffer[count] = iterations;
		
		count++;
	}

// Normalize all iteration data to 255 for RGB conversion
normalize (iterationBuffer, pixelCount, iterations);
	
// Set rgb values to the buffer array (right now this will just return black or white)
hueToRGB (colorBuffer, iterationBuffer, pixelCount, bufferLength);

// Call bitmap writer and write the data array to the file
writeBMP (colorBuffer, dataFile, bufferLength);
	
percent = int(fabs(yPos-yStart)/(yEnd - yStart)*100);
printf("\r%i%%",percent);

}
Topic archived. No new replies allowed.