mandelbrot 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,17 +32,40 @@ void Init_mandelbrot()
32
32
  Float_INFINITY = rb_const_get(rb_cFloat, i_INFINITY);
33
33
 
34
34
  Mandelbrot = rb_define_class("Mandelbrot", rb_cObject);
35
+ rb_define_alloc_func(Mandelbrot, Mandelbrot_alloc);
35
36
  rb_define_method(Mandelbrot, "initialize", Mandelbrot_initialize, -1);
36
37
  rb_define_method(Mandelbrot, "compute", Mandelbrot_compute, 2);
37
38
  rb_define_method(Mandelbrot, "bmp", Mandelbrot_bmp, 2);
38
- rb_define_attr(Mandelbrot, "min", true, false);
39
- rb_define_attr(Mandelbrot, "max", true, false);
39
+ rb_define_method(Mandelbrot, "min", Mandelbrot_min, 0);
40
+ rb_define_method(Mandelbrot, "max", Mandelbrot_max, 0);
40
41
  rb_define_attr(Mandelbrot, "max_iters", true, true);
41
42
  }
42
43
 
44
+ static VALUE Mandelbrot_alloc(VALUE class)
45
+ {
46
+ mandelbrot_args_t* info;
47
+ return Data_Make_Struct(class, mandelbrot_args_t, 0, 0, info);
48
+ }
49
+
50
+ static VALUE Mandelbrot_min(VALUE self)
51
+ {
52
+ mandelbrot_args_t* info;
53
+ Data_Get_Struct(self, mandelbrot_args_t, info);
54
+ return rb_funcall(rb_mKernel, i_Complex, 2, DBL2NUM(info->min_re), DBL2NUM(info->min_im));
55
+ }
56
+
57
+ static VALUE Mandelbrot_max(VALUE self)
58
+ {
59
+ mandelbrot_args_t* info;
60
+ Data_Get_Struct(self, mandelbrot_args_t, info);
61
+ return rb_funcall(rb_mKernel, i_Complex, 2, DBL2NUM(info->max_re), DBL2NUM(info->max_im));
62
+ }
63
+
43
64
  static VALUE Mandelbrot_initialize(int argc, VALUE* argv, VALUE self)
44
65
  {
45
66
  VALUE min, max;
67
+ mandelbrot_args_t* info;
68
+ Data_Get_Struct(self, mandelbrot_args_t, info);
46
69
  rb_scan_args(argc, argv, "02", &min, &max);
47
70
 
48
71
  if(NIL_P(min)) {
@@ -55,10 +78,13 @@ static VALUE Mandelbrot_initialize(int argc, VALUE* argv, VALUE self)
55
78
  rb_check_type(min, T_COMPLEX);
56
79
  rb_check_type(max, T_COMPLEX);
57
80
 
58
- rb_iv_set(self, "@min", min);
59
- rb_iv_set(self, "@max", max);
60
- rb_iv_set(self, "@max_iters", INT2NUM(256));
81
+ info->min_re = rb_complex_re(min);
82
+ info->min_im = rb_complex_im(min);
83
+ info->max_re = rb_complex_re(max);
84
+ info->max_im = rb_complex_im(max);
61
85
 
86
+ rb_iv_set(self, "@max_iters", INT2NUM(256));
87
+
62
88
  return Qnil;
63
89
  }
64
90
 
@@ -73,18 +99,18 @@ static VALUE Mandelbrot_compute(VALUE self, VALUE _width, VALUE _height)
73
99
  VALUE ary, col, tmp;
74
100
  VALUE r_min;
75
101
  VALUE r_max;
102
+ mandelbrot_args_t* info;
103
+ Data_Get_Struct(self, mandelbrot_args_t, info);
76
104
 
77
105
  rb_check_type(_width, T_FIXNUM);
78
106
  rb_check_type(_height, T_FIXNUM);
79
107
  width = NUM2INT(_width);
80
108
  height = NUM2INT(_height);
81
109
 
82
- r_min = rb_iv_get(self, "@min");
83
- r_max = rb_iv_get(self, "@max");
84
- min_re = rb_complex_re(r_min);
85
- min_im = rb_complex_im(r_min);
86
- max_re = rb_complex_re(r_max);
87
- max_im = rb_complex_im(r_max);
110
+ min_re = info->min_re;
111
+ min_im = info->min_im;
112
+ max_re = info->max_re;
113
+ max_im = info->max_im;
88
114
 
89
115
  max_iters = NUM2INT(rb_iv_get(self, "@max_iters"));
90
116
 
@@ -157,25 +183,25 @@ static VALUE Mandelbrot_bmp(VALUE self, VALUE _width, VALUE _height)
157
183
  int x, y, iter;
158
184
  int offset;
159
185
  int* grid;
186
+ size_t tmpsz;
160
187
  VALUE ary, col, tmp;
161
188
  VALUE r_min;
162
189
  VALUE r_max;
190
+ mandelbrot_args_t* info;
191
+ Data_Get_Struct(self, mandelbrot_args_t, info);
163
192
 
164
193
  rb_check_type(_width, T_FIXNUM);
165
194
  rb_check_type(_height, T_FIXNUM);
166
195
  width = NUM2INT(_width);
167
196
  height = NUM2INT(_height);
168
197
 
169
- r_min = rb_iv_get(self, "@min");
170
- r_max = rb_iv_get(self, "@max");
171
- min_re = rb_complex_re(r_min);
172
- min_im = rb_complex_im(r_min);
173
- max_re = rb_complex_re(r_max);
174
- max_im = rb_complex_im(r_max);
198
+ min_re = info->min_re;
199
+ min_im = info->min_im;
200
+ max_re = info->max_re;
201
+ max_im = info->max_im;
175
202
 
176
203
  max_iters = NUM2INT(rb_iv_get(self, "@max_iters"));
177
204
 
178
-
179
205
  bmp = malloc(54 + (((24 * width + 31)/32) * 4) * height);
180
206
  new_bmp_header(width, height, bmp);
181
207
  bmp_image_data = bmp + 54;
@@ -207,5 +233,7 @@ static VALUE Mandelbrot_bmp(VALUE self, VALUE _width, VALUE _height)
207
233
  }
208
234
  }
209
235
 
210
- return rb_str_new(bmp, 54 + (((24 * width + 31)/32) * 4) * height);
236
+ tmp = rb_str_new(bmp, 54 + (((24 * width + 31)/32) * 4) * height);
237
+ free(bmp);
238
+ return tmp;
211
239
  }
@@ -4,10 +4,19 @@
4
4
  #include "bmp.h"
5
5
  #include "ruby.h"
6
6
 
7
+ typedef struct mandelbrot_args {
8
+ double min_re, min_im;
9
+ double max_re, max_im;
10
+ } mandelbrot_args_t;
11
+
12
+ static VALUE Mandelbrot_alloc(VALUE class);
13
+
7
14
  static VALUE Float_INFINITY = Qundef;
8
15
  static VALUE Mandelbrot = Qundef;
9
16
  static VALUE Mandelbrot_initialize(int argc, VALUE* argv, VALUE self);
10
17
  static VALUE Mandelbrot_compute(VALUE self, VALUE _width, VALUE _height);
11
18
  static VALUE Mandelbrot_bmp(VALUE self, VALUE _width, VALUE _height);
19
+ static VALUE Mandelbrot_min(VALUE self);
20
+ static VALUE Mandelbrot_max(VALUE self);
12
21
 
13
- int* mandelbrot_compute(int w, int h, double min_re, double min_im, double max_re, double max_im, int max_iter);
22
+ int* mandelbrot_compute(int w, int h, double min_re, double min_im, double max_re, double max_im, int max_iter);
@@ -1 +1,7 @@
1
- require 'mandelbrot/mandelbrot'
1
+ require 'mandelbrot/mandelbrot'
2
+
3
+ class Mandelbrot
4
+ def inspect
5
+ "#<Mandelbrot min=#{min.inspect}, max=#{max.inspect}, max_iters=#{max_iters}>"
6
+ end
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mandelbrot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: