carray 1.3.6 → 1.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,495 +0,0 @@
1
- /* ---------------------------------------------------------------------------
2
-
3
- carray/carray_imagemap.c
4
-
5
- This file is part of Ruby/CArray extension library.
6
- You can redistribute it and/or modify it under the terms of
7
- the Ruby Licence.
8
-
9
- Copyright (C) 2005 Hiroki Motoyoshi
10
-
11
- ---------------------------------------------------------------------------- */
12
-
13
- #include "carray.h"
14
- #include <math.h>
15
-
16
- #define ca_ptr_1d(ap, i) \
17
- ca_ptr_at_addr((ap), (i))
18
- #define ca_ptr_2d(ap, i, j) \
19
- ca_ptr_at_addr((ap), ((CArray*)(ap))->dim[1]*(i) + (j))
20
- #define ca_ptr_3d(ap, i, j, k) \
21
- ca_ptr_at_addr(ap, \
22
- ((CArray*)(ap))->dim[2]*( ((CArray*)(ap))->dim[1]*(i) + (j) ) + (k))
23
-
24
- #define ca_ref_1d(type, ap, i) \
25
- (*(type*) ca_ptr_at_addr((ap), (i)))
26
- #define ca_ref_2d(type, ap, i, j) \
27
- (*(type*) ca_ptr_at_addr((ap), ((CArray*)(ap))->dim[1]*(i) + (j)))
28
- #define ca_ref_3d(type, ap, i, j, k) \
29
- (*(type*) ca_ptr_at_addr(ap, \
30
- ((CArray*)(ap))->dim[2]*( ((CArray*)(ap))->dim[1]*(i) + (j) ) + (k)))
31
-
32
- #define CA_REF(ca, i, j) ca_ref_2d(int8_t, (ca), (i), (j))
33
-
34
- #define round(x) (int)(((x)>0) ? floor((x)+0.5) : ((x)<0) ? ceil((x)-0.5) : 0)
35
-
36
- static void
37
- draw_linex (CArray *ca, float x0, float y0, float x1, float y1, char *ptr)
38
- {
39
- float x, y;
40
- ca_size_t ix, iy;
41
- ca_size_t dim0 = ca->dim[0];
42
- ca_size_t dim1 = ca->dim[1];
43
- float a = (y1-y0)/(x1-x0);
44
- for (x=x0; x<x1; x+=1) {
45
- y = a*(x-x0)+y0;
46
- ix = round(x);
47
- iy = round(y);
48
- if ( ix < 0 || ix >= dim0 || iy < 0 || iy >= dim1 ) {
49
- continue;
50
- }
51
- memcpy(ca_ptr_2d(ca, ix, iy), ptr, ca->bytes);
52
- }
53
- }
54
-
55
- static void
56
- draw_liney (CArray *ca, float x0, float y0, float x1, float y1, char *ptr)
57
- {
58
- float x, y;
59
- ca_size_t ix, iy;
60
- ca_size_t dim0 = ca->dim[0];
61
- ca_size_t dim1 = ca->dim[1];
62
- float a = (x1-x0)/(y1-y0);
63
- for (y=y0; y<y1; y+=1) {
64
- x = a*(y-y0)+x0;
65
- ix = round(x);
66
- iy = round(y);
67
- if ( ix < 0 || ix >= dim0 || iy < 0 || iy >= dim1 ) {
68
- continue;
69
- }
70
- memcpy(ca_ptr_2d(ca, ix, iy), ptr, ca->bytes);
71
- }
72
- }
73
-
74
- void
75
- draw_line (CArray *ca, float x0, float y0, float x1, float y1, char *ptr)
76
- {
77
- if ( fabs(y1-y0) < fabs(x1-x0) ) {
78
- if ( x1 > x0 ) {
79
- draw_linex(ca, x0, y0, x1, y1, ptr);
80
- }
81
- else if ( x1 < x0 ) {
82
- draw_linex(ca, x1, y1, x0, y0, ptr);
83
- }
84
- }
85
- else {
86
- if ( y1 > y0 ) {
87
- draw_liney(ca, x0, y0, x1, y1, ptr);
88
- }
89
- else if ( y1 < y0 ) {
90
- draw_liney(ca, x1, y1, x0, y0, ptr);
91
- }
92
- }
93
- }
94
-
95
- static VALUE
96
- rb_img_draw_line (VALUE self,
97
- VALUE vx0, VALUE vy0, VALUE vx1, VALUE vy1, volatile VALUE vz)
98
- {
99
- CArray *ca, *cz;
100
-
101
- Data_Get_Struct(self, CArray, ca);
102
- cz = ca_wrap_readonly(vz, ca->data_type);
103
-
104
- ca_attach(ca);
105
-
106
- draw_line(ca, NUM2DBL(vx0), NUM2DBL(vy0), NUM2DBL(vx1), NUM2DBL(vy1), cz->ptr);
107
-
108
- ca_detach(ca);
109
-
110
- return Qnil;
111
- }
112
-
113
- /*
114
- def self.fill_rect2(image, x, y, val)
115
- xmin, xmax = x.min, x.max
116
- ymin, ymax = y.min, y.max
117
- img = CArray.int8(xmax.round-xmin.round+1, ymax.round-ymin.round+1) { 0 }
118
- if (xmax + img.dim0 >= image.dim0 ) or
119
- (xmin < 0 ) or
120
- (ymax + img.dim1 >= image.dim1) or
121
- (ymin < 0)
122
- return
123
- end
124
- subimg = image[[xmin.round,img.dim0],[ymin.round,img.dim1]]
125
- gx = x-xmin.round
126
- gy = y-ymin.round
127
- img.draw_box(gx, gy)
128
- img.fill_out()
129
- subimg[img] = val
130
- end
131
- */
132
-
133
- static void
134
- fill_rect (CArray *image, float x[4], float y[4], char *ptr)
135
- {
136
- CArray *mask;
137
- float xmin, xmax, ymin, ymax;
138
- float gx[4], gy[4];
139
- ca_size_t dim[2];
140
- ca_size_t i, j, ixmin, iymin, ixmax, iymax;
141
- char one = 1;
142
-
143
- xmin = x[0];
144
- xmax = x[0];
145
- ymin = y[0];
146
- ymax = y[0];
147
-
148
- for (i=1; i<4; i++) {
149
- if ( isnan(x[i]) || isnan(y[i]) )
150
- return;
151
- if ( x[i] < xmin ) {
152
- xmin = x[i];
153
- }
154
- if ( x[i] > xmax ) {
155
- xmax = x[i];
156
- }
157
- if ( y[i] < ymin ) {
158
- ymin = y[i];
159
- }
160
- if ( y[i] > ymax ) {
161
- ymax = y[i];
162
- }
163
- }
164
-
165
- ixmin = (ca_size_t) floor(xmin);
166
- iymin = (ca_size_t) floor(ymin);
167
-
168
- ixmax = (ca_size_t) ceil(xmax) + 1;
169
- iymax = (ca_size_t) ceil(ymax) + 1;
170
-
171
- dim[0] = ixmax - ixmin + 1;
172
- dim[1] = iymax - iymin + 1;
173
-
174
- mask = carray_new(CA_BOOLEAN, 2, dim, 0, NULL);
175
- memset(mask->ptr, 0, mask->elements);
176
-
177
- for (i=0; i<4; i++) {
178
- gx[i] = x[i] - ixmin + 1;
179
- gy[i] = y[i] - iymin + 1;
180
- }
181
-
182
- draw_line(mask, gx[0], gy[0], gx[1], gy[1], &one);
183
- draw_line(mask, gx[1], gy[1], gx[3], gy[3], &one);
184
- draw_line(mask, gx[3], gy[3], gx[2], gy[2], &one);
185
- draw_line(mask, gx[2], gy[2], gx[0], gy[0], &one);
186
-
187
- for (i=0; i<dim[0]; i++) {
188
- for (j=0; j<dim[1]; j++) {
189
- if ( CA_REF(mask,i,j) ) {
190
- break;
191
- }
192
- CA_REF(mask,i,j) = -1;
193
- }
194
- for (j=dim[1]-1; j>=0; j--) {
195
- if ( CA_REF(mask,i,j) ) {
196
- break;
197
- }
198
- CA_REF(mask,i,j) = -1;
199
- }
200
- }
201
-
202
- for (i=0; i<dim[0]; i++) {
203
- if ( ixmin+i < 0 || ixmin+i >= image->dim[0] )
204
- continue;
205
- for (j=0; j<dim[1]; j++)
206
- if ( CA_REF(mask,i,j) >= 0 ) {
207
- if ( iymin+j < 0 || iymin+j >= image->dim[1] ) {
208
- continue;
209
- }
210
- memcpy(ca_ptr_2d(image, ixmin+i, iymin+j), ptr, image->bytes);
211
- }
212
- }
213
-
214
- ca_free(mask);
215
- }
216
-
217
-
218
- static VALUE
219
- rb_im_fill_rect (VALUE self,
220
- VALUE vimg,
221
- volatile VALUE vx, volatile VALUE vy, volatile VALUE vval)
222
- {
223
- CArray *image, *val, *cx, *cy;
224
-
225
- Data_Get_Struct(vimg, CArray, image);
226
- Data_Get_Struct(vx, CArray, cx);
227
- Data_Get_Struct(vy, CArray, cy);
228
-
229
- cx = ca_wrap_readonly(vx, CA_FLOAT);
230
- cy = ca_wrap_readonly(vy, CA_FLOAT);
231
- val = ca_wrap_readonly(vval, image->data_type);
232
-
233
- ca_attach_n(3, cx, cy, val);
234
-
235
- fill_rect(image, (float*)cx->ptr, (float*)cy->ptr, val->ptr);
236
-
237
- ca_detach_n(3, cx, cy, val);
238
-
239
- return Qnil;
240
- }
241
-
242
- void
243
- draw_hline_gradation (CArray *image, ca_size_t iy,
244
- float x0, float x1, float z0, float z1);
245
-
246
- static VALUE
247
- rb_img_draw_hline_gradation (VALUE self, VALUE viy,
248
- VALUE vx0, VALUE vx1, VALUE vz0, VALUE vz1)
249
- {
250
- CArray *image;
251
-
252
- Data_Get_Struct(self, CArray, image);
253
-
254
- draw_hline_gradation(image, NUM2SIZE(viy),
255
- NUM2DBL(vx0), NUM2DBL(vx1), NUM2DBL(vz0), NUM2DBL(vz1));
256
-
257
- return Qnil;
258
- }
259
-
260
- void
261
- draw_triangle_gradation (CArray *image, float y[3], float x[3], float z[3]);
262
-
263
- static VALUE
264
- rb_img_draw_triangle_gradation (VALUE self,
265
- VALUE vy, VALUE vx, VALUE vz)
266
- {
267
- CArray *image;
268
- float y[3], x[3], z[3];
269
- int i;
270
-
271
- Data_Get_Struct(self, CArray, image);
272
-
273
- for (i=0; i<3; i++) {
274
- y[i] = NUM2DBL(rb_ary_entry(vy, i));
275
- x[i] = NUM2DBL(rb_ary_entry(vx, i));
276
- z[i] = NUM2DBL(rb_ary_entry(vz, i));
277
- }
278
-
279
- draw_triangle_gradation(image, y, x, z);
280
-
281
- return Qnil;
282
- }
283
-
284
- void
285
- fill_rectangle (CArray *image, float y[4], float x[4], char *ptr);
286
-
287
- static VALUE
288
- rb_img_fill_rectangle (VALUE self,
289
- volatile VALUE vy, volatile VALUE vx, volatile VALUE vz)
290
- {
291
- CArray *image, *cy, *cx, *cz;
292
-
293
- Data_Get_Struct(self, CArray, image);
294
-
295
- cy = ca_wrap_readonly(vy, CA_FLOAT);
296
- cx = ca_wrap_readonly(vx, CA_FLOAT);
297
- cz = ca_wrap_readonly(vz, image->data_type);
298
-
299
- if ( cy->elements != 4 || cx->elements != 4 ) {
300
- rb_raise(rb_eRuntimeError, "invalid size of y or x");
301
- }
302
-
303
- ca_attach_n(4, image, cy, cx, cz);
304
-
305
- fill_rectangle(image, (float*)cy->ptr, (float*)cx->ptr, cz->ptr);
306
-
307
- ca_detach_n(4, image, cy, cx, cz);
308
-
309
- return Qnil;
310
- }
311
-
312
- void
313
- fill_rectangle_image (CArray *image, CArray *cy, CArray *cx, CArray *cv);
314
-
315
- static VALUE
316
- rb_img_fill_rectangle_image (VALUE self,
317
- volatile VALUE vy, volatile VALUE vx, volatile VALUE vz)
318
- {
319
- CArray *image, *cy, *cx, *cz;
320
-
321
- Data_Get_Struct(self, CArray, image);
322
-
323
- cy = ca_wrap_readonly(vy, CA_FLOAT);
324
- cx = ca_wrap_readonly(vx, CA_FLOAT);
325
- cz = ca_wrap_readonly(vz, image->data_type);
326
-
327
- ca_attach_n(4, image, cy, cx, cz);
328
-
329
- fill_rectangle_image(image, cy, cx, cz);
330
-
331
- ca_sync(image);
332
-
333
- ca_detach_n(4, image, cy, cx, cz);
334
-
335
- return Qnil;
336
- }
337
-
338
- void
339
- fill_rectangle_grid (CArray *image, CArray *cy, CArray *cx, CArray *cv);
340
-
341
- static VALUE
342
- rb_img_fill_rectangle_grid (VALUE self,
343
- volatile VALUE vy, volatile VALUE vx, volatile VALUE vz)
344
- {
345
- CArray *image, *cy, *cx, *cz;
346
-
347
- Data_Get_Struct(self, CArray, image);
348
-
349
- cy = ca_wrap_readonly(vy, CA_FLOAT);
350
- cx = ca_wrap_readonly(vx, CA_FLOAT);
351
- cz = ca_wrap_readonly(vz, image->data_type);
352
-
353
- ca_attach_n(4, image, cy, cx, cz);
354
-
355
- fill_rectangle_grid(image, cy, cx, cz);
356
-
357
- ca_detach_n(4, image, cy, cx, cz);
358
-
359
- return Qnil;
360
- }
361
-
362
- void
363
- draw_rectangle_gradation (CArray *image, float y[4], float x[4], float z[4]);
364
-
365
- static VALUE
366
- rb_img_draw_rectangle_gradation (VALUE self,
367
- volatile VALUE vy, volatile VALUE vx, volatile VALUE vz)
368
- {
369
- CArray *image, *cy, *cx, *cz;
370
-
371
- Data_Get_Struct(self, CArray, image);
372
-
373
- cy = ca_wrap_readonly(vy, CA_FLOAT);
374
- cx = ca_wrap_readonly(vx, CA_FLOAT);
375
- cz = ca_wrap_readonly(vz, CA_FLOAT);
376
-
377
- if ( cy->elements != 4 || cx->elements != 4 || cz->elements != 4) {
378
- rb_raise(rb_eRuntimeError, "invalid size of y or x or z");
379
- }
380
-
381
- ca_attach_n(4, image, cy, cx, cz);
382
-
383
- draw_rectangle_gradation(image,
384
- (float*)cy->ptr, (float*)cx->ptr, (float*)cz->ptr);
385
-
386
- ca_detach_n(4, image, cy, cx, cz);
387
-
388
- return Qnil;
389
- }
390
-
391
- void
392
- draw_rectangle_gradation_grid (CArray *image,
393
- CArray *cy, CArray *cx, CArray *cv);
394
-
395
- static VALUE
396
- rb_img_draw_rectangle_gradation_grid (VALUE self,
397
- volatile VALUE vy, volatile VALUE vx, volatile VALUE vz)
398
- {
399
- CArray *image, *cy, *cx, *cz;
400
-
401
- Data_Get_Struct(self, CArray, image);
402
-
403
- cy = ca_wrap_readonly(vy, CA_FLOAT);
404
- cx = ca_wrap_readonly(vx, CA_FLOAT);
405
- cz = ca_wrap_readonly(vz, image->data_type);
406
-
407
- ca_attach_n(4, image, cy, cx, cz);
408
-
409
- draw_rectangle_gradation_grid(image, cy, cx, cz);
410
-
411
- ca_detach_n(4, image, cy, cx, cz);
412
-
413
- return Qnil;
414
- }
415
-
416
- void
417
- draw_points (CArray *image,
418
- CArray *cy, CArray *cx, CArray *cz);
419
-
420
- static VALUE
421
- rb_img_draw_points (VALUE self,
422
- volatile VALUE vy, volatile VALUE vx, volatile VALUE vz)
423
- {
424
- CArray *image, *cy, *cx, *cz;
425
-
426
- Data_Get_Struct(self, CArray, image);
427
-
428
- cy = ca_wrap_readonly(vy, CA_FLOAT);
429
- cx = ca_wrap_readonly(vx, CA_FLOAT);
430
- cz = ca_wrap_readonly(vz, image->data_type);
431
-
432
- ca_attach_n(4, image, cy, cx, cz);
433
-
434
- draw_points(image, cy, cx, cz);
435
-
436
- ca_detach_n(4, image, cy, cx, cz);
437
-
438
- return Qnil;
439
- }
440
-
441
- void
442
- draw_polyline (CArray *image,
443
- CArray *cy, CArray *cx, char *ptr);
444
-
445
- static VALUE
446
- rb_img_draw_polyline (VALUE self,
447
- volatile VALUE vy, volatile VALUE vx, volatile VALUE vz)
448
- {
449
- CArray *image, *cy, *cx, *cz;
450
-
451
- Data_Get_Struct(self, CArray, image);
452
-
453
- cy = ca_wrap_readonly(vy, CA_FLOAT);
454
- cx = ca_wrap_readonly(vx, CA_FLOAT);
455
- cz = ca_wrap_readonly(vz, image->data_type);
456
-
457
- ca_attach_n(4, image, cy, cx, cz);
458
-
459
- draw_polyline(image, cy, cx, cz->ptr);
460
-
461
- ca_detach_n(4, image, cy, cx, cz);
462
-
463
- return Qnil;
464
- }
465
-
466
- void
467
- Init_carray_imagemap ()
468
- {
469
- VALUE rb_cImage = rb_const_get(rb_cObject, rb_intern("ImageMap"));
470
-
471
- rb_define_singleton_method(rb_cImage, "fill_rect", rb_im_fill_rect, 4);
472
-
473
- rb_define_method(rb_cImage, "fill_rectangle",
474
- rb_img_fill_rectangle, 3);
475
- rb_define_method(rb_cImage, "fill_rectangle_image",
476
- rb_img_fill_rectangle_image, 3);
477
- rb_define_method(rb_cImage, "fill_rectangle_grid",
478
- rb_img_fill_rectangle_grid, 3);
479
-
480
- rb_define_method(rb_cImage, "draw_line", rb_img_draw_line, 5);
481
- rb_define_method(rb_cImage, "draw_hline_gradation",
482
- rb_img_draw_hline_gradation, 5);
483
- rb_define_method(rb_cImage, "draw_triangle_gradation",
484
- rb_img_draw_triangle_gradation, 3);
485
- rb_define_method(rb_cImage, "draw_rectangle_gradation",
486
- rb_img_draw_rectangle_gradation, 3);
487
- rb_define_method(rb_cImage, "draw_rectangle_gradation_grid",
488
- rb_img_draw_rectangle_gradation_grid, 3);
489
-
490
- rb_define_method(rb_cImage, "draw_points", rb_img_draw_points, 3);
491
- rb_define_method(rb_cImage, "draw_polyline", rb_img_draw_polyline, 3);
492
-
493
- }
494
-
495
-