-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakePNG.c
213 lines (174 loc) · 5.13 KB
/
makePNG.c
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// LibPNG example
// A.Greensted
// http://www.labbookpages.co.uk
// Version 2.0
// With some minor corrections to Mandlebrot code (thanks to Jan-Oliver)
// Version 1.0 - Initial release
#include <stdio.h>
#include <math.h>
#include <malloc.h>
#include <png.h>
// Creates a test image for saving. Creates a Mandelbrot Set fractal of size width x height
float *createMandelbrotImage(int width, int height, float xS, float yS, float rad, int maxIteration);
// This takes the float value 'val', converts it to red, green & blue values, then
// sets those values into the image memory buffer location pointed to by 'ptr'
void setRGB(png_byte *ptr, float val);
// This function actually writes out the PNG image file. The string 'title' is
// also written into the image file
int writeImage(char* filename, int width, int height, float *buffer, char* title);
int main(int argc, char *argv[])
{
// Make sure that the output filename argument has been provided
if (argc != 2) {
fprintf(stderr, "Please specify output file\n");
return 1;
}
// Specify an output image size
int width = 500;
int height = 300;
// Create a test image - in this case a Mandelbrot Set fractal
// The output is a 1D array of floats, length: width * height
printf("Creating Image\n");
float *buffer = createMandelbrotImage(width, height, -0.802, -0.177, 0.011, 110);
if (buffer == NULL) {
return 1;
}
// Save the image to a PNG file
// The 'title' string is stored as part of the PNG file
printf("Saving PNG\n");
int result = writeImage(argv[1], width, height, buffer, "This is my test image");
// Free up the memorty used to store the image
free(buffer);
return result;
}
void setRGB(png_byte *ptr, float val)
{
int v = (int)(val * 767);
if (v < 0) v = 0;
if (v > 767) v = 767;
int offset = v % 256;
if (v<256) {
ptr[0] = 0; ptr[1] = 0; ptr[2] = offset;
}
else if (v<512) {
ptr[0] = 0; ptr[1] = offset; ptr[2] = 255-offset;
}
else {
ptr[0] = offset; ptr[1] = 255-offset; ptr[2] = 0;
}
}
int writeImage(char* filename, int width, int height, float *buffer, char* title)
{
int code = 0;
FILE *fp = NULL;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_bytep row = NULL;
// Open file for writing (binary mode)
fp = fopen(filename, "wb");
if (fp == NULL) {
fprintf(stderr, "Could not open file %s for writing\n", filename);
code = 1;
goto finalise;
}
// Initialize write structure
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fprintf(stderr, "Could not allocate write struct\n");
code = 1;
goto finalise;
}
// Initialize info structure
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fprintf(stderr, "Could not allocate info struct\n");
code = 1;
goto finalise;
}
// Setup Exception handling
if (setjmp(png_jmpbuf(png_ptr))) {
fprintf(stderr, "Error during png creation\n");
code = 1;
goto finalise;
}
png_init_io(png_ptr, fp);
// Write header (8 bit colour depth)
png_set_IHDR(png_ptr, info_ptr, width, height,
8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
// Set title
if (title != NULL) {
png_text title_text;
title_text.compression = PNG_TEXT_COMPRESSION_NONE;
title_text.key = "Title";
title_text.text = title;
png_set_text(png_ptr, info_ptr, &title_text, 1);
}
png_write_info(png_ptr, info_ptr);
// Allocate memory for one row (3 bytes per pixel - RGB)
row = (png_bytep) malloc(3 * width * sizeof(png_byte));
// Write image data
int x, y;
for (y=0 ; y<height ; y++) {
for (x=0 ; x<width ; x++) {
//setRGB(&(row[x*3]), buffer[y*width + x]);
row[x] = 0;
}
png_write_row(png_ptr, row);
}
// End write
png_write_end(png_ptr, NULL);
finalise:
if (fp != NULL) fclose(fp);
if (info_ptr != NULL) png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
if (png_ptr != NULL) png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
if (row != NULL) free(row);
return code;
}
float *createMandelbrotImage(int width, int height, float xS, float yS, float rad, int maxIteration)
{
float *buffer = (float *) malloc(width * height * sizeof(float));
if (buffer == NULL) {
fprintf(stderr, "Could not create image buffer\n");
return NULL;
}
// Create Mandelbrot set image
int xPos, yPos;
float minMu = maxIteration;
float maxMu = 0;
for (yPos=0 ; yPos<height ; yPos++)
{
float yP = (yS-rad) + (2.0f*rad/height)*yPos;
for (xPos=0 ; xPos<width ; xPos++)
{
float xP = (xS-rad) + (2.0f*rad/width)*xPos;
int iteration = 0;
float x = 0;
float y = 0;
while (x*x + y*y <= 4 && iteration < maxIteration)
{
float tmp = x*x - y*y + xP;
y = 2*x*y + yP;
x = tmp;
iteration++;
}
if (iteration < maxIteration) {
float modZ = sqrt(x*x + y*y);
float mu = iteration - (log(log(modZ))) / log(2);
if (mu > maxMu) maxMu = mu;
if (mu < minMu) minMu = mu;
buffer[yPos * width + xPos] = mu;
}
else {
buffer[yPos * width + xPos] = 0;
}
}
}
// Scale buffer values between 0 and 1
int count = width * height;
while (count) {
count --;
buffer[count] = (buffer[count] - minMu) / (maxMu - minMu);
}
return buffer;
}