Skip to content

Commit 93d5eb4

Browse files
author
mattip
committed
use pgm
1 parent 0f6d37b commit 93d5eb4

File tree

6 files changed

+954
-176
lines changed

6 files changed

+954
-176
lines changed

create_fractal.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
int mandel(int x, int y, int max_iters, unsigned char * val);
1+
int mandel(int x, int y, int max_iters, unsigned int * val);
22

3-
void create_fractal(unsigned char * image, int width, int height,
3+
void create_fractal(unsigned int * image, int width, int height,
44
int iters) {
55
float pixel_size_x = 3.0 / width;
66
float pixel_size_y = 2.0 / height;
@@ -9,7 +9,7 @@ void create_fractal(unsigned char * image, int width, int height,
99
int yy = y * width;
1010
for (int x=0; x<width; x++) {
1111
float real = x * pixel_size_x - 2;
12-
unsigned char color;
12+
unsigned int color;
1313
int ret = mandel(real, imag, iters, &color);
1414
image[yy + x] = color;
1515
}

create_fractal.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from __future__ import print_function, division
2+
3+
from mandel import mandel
4+
5+
def create_fractal(image, width, height, iters):
6+
pixel_size_x = 3.0 / width
7+
pixel_size_y = 2.0 / height
8+
for y in range(height):
9+
imag = y * pixel_size_y - 1
10+
yy = y*width
11+
for x in range(width):
12+
real = x * pixel_size_x - 2
13+
color = mandel(real, imag, iters)
14+
image[yy+x] = color
15+
16+
if __name__ == '__main__':
17+
from timeit import default_timer as timer
18+
import numpy as np
19+
from PIL import Image
20+
width = 1500
21+
height = 1000
22+
image = np.empty(width*height, dtype=np.uint8)
23+
s = timer()
24+
create_fractal(image, width, height, 20)
25+
e = timer()
26+
print('pure python required {}f.2 secs'.format(e - s))
27+
im = Image.frombuffer('L', (width, height), image, 'raw', 'L', 0, 1)
28+
im.save('python_numpy.png')

main.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
#include <stdio.h>
2-
#include <pbm.h>
2+
#include <pgm.h>
3+
#include <time.h>
34

4-
int mandel(int x, int y, int max_iters, unsigned char * val);
5+
// call this function to start a nanosecond-resolution timer
6+
struct timespec timer_start(){
7+
struct timespec start_time;
8+
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
9+
return start_time;
10+
}
11+
12+
// call this function to end a timer, returning nanoseconds elapsed as a long
13+
long timer_end(struct timespec start_time){
14+
struct timespec end_time;
15+
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
16+
long diffInNanos = end_time.tv_nsec - start_time.tv_nsec;
17+
return diffInNanos;
18+
}
19+
20+
int mandel(int x, int y, int max_iters, unsigned int * val);
521

6-
void create_fractal(unsigned char * image, int width, int height,
22+
void create_fractal(unsigned int * image, int width, int height,
723
int iters);
824

925
int main(int argc, const char *argv[], const char * env[])
@@ -12,16 +28,21 @@ int main(int argc, const char *argv[], const char * env[])
1228
int height = 1000;
1329
int iters = 20;
1430
FILE * fid = NULL;
15-
unsigned char * image = (unsigned char*)malloc(width*height);
31+
struct timespec vartime;
32+
long time_elapsed_nanos;
33+
unsigned int * image = (unsigned int*)malloc(width * height * sizeof(unsigned int));
1634
if (NULL == image)
1735
return -1;
18-
fid = fopen("c.pbm", "wb");
36+
fid = fopen("c.pgm", "wb");
1937
if (NULL == fid)
2038
return -2;
39+
vartime = timer_start();
2140
create_fractal(image, width, height, iters);
22-
pbm_writepbminit(fid, width, height, 0);
41+
time_elapsed_nanos = timer_end(vartime);
42+
fprintf(stdout, "create_fractal required %ld millisecs\n", time_elapsed_nanos / 1000000);
43+
pgm_writepgminit(fid, width, height, 255, 0);
2344
for (int i=0; i<height; i++)
24-
pbm_writepbmrow(fid, image + width*i, width, 0);
45+
pgm_writepgmrow(fid, image + width*i, width, 255, 0);
2546
fclose(fid);
2647
return 0;
2748
}

mandel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def mandel(x, y, max_iters):
1818

1919
#include <complex.h>
2020

21-
int mandel(int x, int y, int max_iters, unsigned char * val)
21+
int mandel(int x, int y, int max_iters, unsigned int * val)
2222
{
2323
int i = 0;
2424
_Complex float c = CMPLX(x, y);

mandel.py

Lines changed: 5 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,5 @@
1-
#! /usr/bin/env python
2-
# -*- coding: utf-8 -*-
3-
from __future__ import print_function, division, absolute_import
41

5-
'''
6-
Taken from https://github.com/numba/numba/blob/master/examples/mandel/mandel_jit.py
7-
On June 3, 2017, then modified by mattip
8-
Original copyright:
9-
10-
Copyright (c) 2012, Continuum Analytics, Inc.
11-
All rights reserved.
12-
13-
Redistribution and use in source and binary forms, with or without
14-
modification, are permitted provided that the following conditions are
15-
met:
16-
17-
Redistributions of source code must retain the above copyright notice,
18-
this list of conditions and the following disclaimer.
19-
20-
Redistributions in binary form must reproduce the above copyright
21-
notice, this list of conditions and the following disclaimer in the
22-
documentation and/or other materials provided with the distribution.
23-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24-
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25-
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26-
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27-
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28-
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29-
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30-
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31-
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34-
35-
Contact GitHub API Training Shop Blog About
36-
37-
'''
38-
39-
from timeit import default_timer as timer
40-
41-
import sys
42-
fname = 'test'
43-
if len(sys.argv) > 1 and 'nojit' in sys.argv[1]:
44-
def jit(f):
45-
print('pure python')
46-
return f
47-
fname = 'test_nojit'
48-
else:
49-
from numba import jit
50-
fname = 'test_numba'
51-
52-
53-
@jit
54-
def mandel(x, y, max_iters):
2+
def mandel(x, y, max_iters, value):
553
"""
564
Given the real and imaginary parts of a complex number,
575
determine if it is a candidate for membership in the Mandelbrot
@@ -63,51 +11,8 @@ def mandel(x, y, max_iters):
6311
for i in range(max_iters):
6412
z = z*z + c
6513
if (z.real*z.real + z.imag*z.imag) >= 4:
66-
return i
67-
68-
return 255
69-
70-
@jit
71-
def create_fractal(image, width, height, iters,
72-
min_x=-2.0, max_x=1.0, min_y=-1.0, max_y=1.0):
73-
pixel_size_x = (max_x - min_x) / width
74-
pixel_size_y = (max_y - min_y) / height
75-
for y in range(height):
76-
imag = min_y + y * pixel_size_y
77-
yy = y*width
78-
for x in range(width):
79-
real = min_x + x * pixel_size_x
80-
color = mandel(real, imag, iters)
81-
image[yy+x] = color
82-
83-
return image
14+
value[0] = i
15+
return 0
16+
value[i] = 255
17+
return 1
8418

85-
width = 1500
86-
height = 1000
87-
if 'cffi' in sys.argv:
88-
import cffi
89-
ffi = cffi.FFI()
90-
image = ffi.new('unsigned char[{}]'.format(width*height))
91-
img_as_bytes = bytes(ffi.buffer(image, width*height))
92-
fname += '_cffi'
93-
else:
94-
import numpy as np
95-
image = np.empty(width*height, dtype=np.uint8)
96-
fname += '_numpy'
97-
img_as_bytes = image
98-
s = timer()
99-
create_fractal(image, width, height, 20)
100-
e = timer()
101-
print(e - s)
102-
if 0:
103-
from matplotlib.pylab import imshow, jet, show, ion
104-
imshow(image)
105-
show()
106-
else:
107-
from PIL import Image
108-
try:
109-
im = Image.frombuffer('L', (width, height), img_as_bytes, 'raw', 'L', 0, 1)
110-
im.save(fname + '.png')
111-
except:
112-
import traceback;traceback.print_exc()
113-
import pdb;pdb.set_trace()

pycon2017_cffi.ipynb

Lines changed: 889 additions & 65 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)