浏览代码

Mandelbrot: png renderization

cmte 5 年之前
父节点
当前提交
38b83278ee
共有 2 个文件被更改,包括 34 次插入10 次删除
  1. 6 3
      mandelbrot/Makefile
  2. 28 7
      mandelbrot/mandelbrot.c

+ 6 - 3
mandelbrot/Makefile

@@ -1,18 +1,21 @@
 CC=/usr/bin/gcc
-CFLAGS=-g -Werror -pthread -pedantic -pedantic-errors -fno-fast-math -fno-builtin -m64 -std=iso9899:1999 -I${PWD} -lX11 -lm
+CFLAGS=-g -Werror -O0 -pthread -pedantic -pedantic-errors -fno-fast-math -fno-builtin -m64 -std=iso9899:1999 -I${PWD} -I${PWD}/../libpng 
 OUT=bin
 OBJ=$(OUT)/obj
-OBJS=$(OBJ)/mandelbrot.o
+OBJS=$(OBJ)/mandelbrot.o $(OBJ)/img.o
 
 
 all: pre bin/mandelbrot
 
 bin/mandelbrot: $(OBJS)
-	$(CC) $(CFLAGS) $(OBJS) -o $(OUT)/mandelbrot
+	$(CC) $(CFLAGS) $(OBJS) -o $(OUT)/mandelbrot -lpng
 
 bin/obj/mandelbrot.o: mandelbrot.c
 	$(CC) $(CFLAGS) -c mandelbrot.c -o $(OBJ)/mandelbrot.o
 
+bin/obj/img.o:
+	$(CC) $(CFLAGS) -c ../libpng/img.c -o $(OBJ)/img.o -lpng
+
 pre:
 	mkdir -p bin/obj
 

+ 28 - 7
mandelbrot/mandelbrot.c

@@ -1,11 +1,15 @@
 #include <stdio.h>
+#include "../libpng/img.h"
 
-#define BAIL_OUT 16
-#define MAX_ITER 100
+
+#define BAIL_OUT 64
+#define MAX_ITER 8192
 
 #define WIDTH 800
 #define HEIGHT 800
 
+
+
 double map(int value, int min, int max, int map_min, int map_max)
 {
 	double R = (double) (map_max - map_min) / (double) (max - min);
@@ -45,7 +49,7 @@ void mandelbrot(double *field, int w, int h, int bail_out, int max_iter)
 			}
 
 			if (k == max_iter) {
-				field[x * h + y] = 0;
+				field[x * h + y] = max_iter;
 			} else {
 				field[x * h + y] = k;
 			}
@@ -61,15 +65,32 @@ int main(int argc, char **argv)
 	mandelbrot((double *) pix_field, WIDTH, HEIGHT, BAIL_OUT,
 		   MAX_ITER);
 
+	pix_row rows[WIDTH];
+	pix p;
+
+	char filename[30];
+	image img;
+	p.r = 200;
+	p.g = 200;
+	p.b = 200;
+
 	for (int i = 0; i < WIDTH; i++) {
+		rows[i].p = malloc(HEIGHT * sizeof(pix));
 		for (int j = 0; j < HEIGHT; j++) {
-			printf("[%.2lf]", pix_field[i][j]);
+			p.r = map(pix_field[i][j], 0, MAX_ITER, 0, 255);
+			p.g = map(pix_field[i][j], 0, MAX_ITER, 0, 255);
+			p.b = map(pix_field[i][j], 0, MAX_ITER, 0, 255);
+			rows[i].p[j] = p;
 		}
+	}
+	sprintf(filename, "mandel.png");
+	img = initialize_png("image000", filename, WIDTH, HEIGHT);
+	write_image(&img, rows);
+	finish_image(&img);
+	for (int i = 0; i < WIDTH; i++) {
 
-		printf("\n");
-
+		free(rows[i].p);
 	}
 
 	return (0);
-
 }