carray-rmagick 1.0.0
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.
- checksums.yaml +7 -0
- data/README.md +14 -0
- data/carray-rmagick.gemspec +23 -0
- data/carray_rmagick.c +353 -0
- data/extconf.rb +37 -0
- data/lib/autoload/autoload_io_rmagick.rb +17 -0
- data/lib/autoload/autoload_object_magick_image.rb +9 -0
- data/lib/carray-rmagick.rb +5 -0
- data/lib/io/rmagick.rb +147 -0
- data/lib/object/ca_obj_magick_image.rb +84 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dfffcd1dc6cdda77cc5a3089c45ca836268f45ca
|
4
|
+
data.tar.gz: e9a804e2d79801be3c96eb04b014f90b480bc9d3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8c557df77610e1616e9faf53457dec1c5421ff42fb9756d2ef52c0ff0098516837a01dc2ba7d12962810cea29e655dc354ba86ab7c22e67dac5c848388f22349
|
7
|
+
data.tar.gz: 8d5031a7552de0eacaa1d2d5f84b508af3a34c7f1c697c66e527f4bc95d1b1ca6a400f7cdde6be77c482e3bdc34db1059f14928b770c73ecf35b0e4966588899
|
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
|
2
|
+
Gem::Specification::new do |s|
|
3
|
+
version = "1.0.0"
|
4
|
+
|
5
|
+
files = Dir.glob("**/*") - [
|
6
|
+
Dir.glob("carray*.gem"),
|
7
|
+
].flatten
|
8
|
+
|
9
|
+
s.platform = Gem::Platform::RUBY
|
10
|
+
s.name = "carray-rmagick"
|
11
|
+
s.summary = "Extension for converting Magick::Image to/from CArray"
|
12
|
+
s.description = <<-HERE
|
13
|
+
Extension for converting Magick::Image to/from CArray
|
14
|
+
HERE
|
15
|
+
s.version = version
|
16
|
+
s.author = "Hiroki Motoyoshi"
|
17
|
+
s.email = ""
|
18
|
+
s.homepage = 'https://github.com/himotoyoshi/carray_rmagick'
|
19
|
+
s.files = files
|
20
|
+
s.extensions = [ "extconf.rb" ]
|
21
|
+
s.has_rdoc = false
|
22
|
+
s.required_ruby_version = ">= 1.8.1"
|
23
|
+
end
|
data/carray_rmagick.c
ADDED
@@ -0,0 +1,353 @@
|
|
1
|
+
/* ---------------------------------------------------------------------------
|
2
|
+
|
3
|
+
carray/rmagick/carray_rmagick.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-2008 Hiroki Motoyoshi
|
10
|
+
|
11
|
+
---------------------------------------------------------------------------- */
|
12
|
+
|
13
|
+
#include "magick/api.h"
|
14
|
+
|
15
|
+
#undef PACKAGE_VERSION
|
16
|
+
#undef PACKAGE_NAME
|
17
|
+
#undef PACKAGE_STRING
|
18
|
+
#undef PACKAGE_BUGREPORT
|
19
|
+
#undef PACKAGE_TARNAME
|
20
|
+
|
21
|
+
#include "carray.h"
|
22
|
+
|
23
|
+
static VALUE
|
24
|
+
Image_fetch_index (VALUE self, VALUE vidx)
|
25
|
+
{
|
26
|
+
volatile VALUE out;
|
27
|
+
Image *image;
|
28
|
+
PixelPacket *pixel;
|
29
|
+
int32_t irow, icol, imem;
|
30
|
+
|
31
|
+
Data_Get_Struct(self, Image, image);
|
32
|
+
|
33
|
+
Check_Type(vidx, T_ARRAY);
|
34
|
+
irow = NUM2INT(rb_ary_entry(vidx, 0));
|
35
|
+
icol = NUM2INT(rb_ary_entry(vidx, 1));
|
36
|
+
imem = NUM2INT(rb_ary_entry(vidx, 2));
|
37
|
+
|
38
|
+
pixel = (PixelPacket *) GetVirtualPixels(image,
|
39
|
+
icol, irow, 1, 1, &image->exception);
|
40
|
+
|
41
|
+
switch ( imem ) {
|
42
|
+
case 0: out = ULONG2NUM(pixel->red); break;
|
43
|
+
case 1: out = ULONG2NUM(pixel->green); break;
|
44
|
+
case 2: out = ULONG2NUM(pixel->blue); break;
|
45
|
+
case 3: out = ULONG2NUM(pixel->opacity); break;
|
46
|
+
}
|
47
|
+
|
48
|
+
return out;
|
49
|
+
}
|
50
|
+
|
51
|
+
static VALUE
|
52
|
+
Image_store_index (VALUE self, VALUE vidx, VALUE vval)
|
53
|
+
{
|
54
|
+
Image *image;
|
55
|
+
PixelPacket *pixel;
|
56
|
+
int32_t irow, icol, imem;
|
57
|
+
|
58
|
+
Data_Get_Struct(self, Image, image);
|
59
|
+
|
60
|
+
Check_Type(vidx, T_ARRAY);
|
61
|
+
irow = NUM2INT(rb_ary_entry(vidx, 0));
|
62
|
+
icol = NUM2INT(rb_ary_entry(vidx, 1));
|
63
|
+
imem = NUM2INT(rb_ary_entry(vidx, 2));
|
64
|
+
|
65
|
+
pixel = (PixelPacket *) GetVirtualPixels(image,
|
66
|
+
icol, irow, 1, 1, &image->exception);
|
67
|
+
|
68
|
+
switch ( imem ) {
|
69
|
+
case 0: pixel->red = NUM2ULONG(vval); break;
|
70
|
+
case 1: pixel->green = NUM2ULONG(vval); break;
|
71
|
+
case 2: pixel->blue = NUM2ULONG(vval); break;
|
72
|
+
case 3: pixel->opacity = NUM2ULONG(vval); break;
|
73
|
+
}
|
74
|
+
|
75
|
+
SyncAuthenticPixels(image, &image->exception);
|
76
|
+
|
77
|
+
return vval;
|
78
|
+
}
|
79
|
+
|
80
|
+
|
81
|
+
static VALUE
|
82
|
+
Image_copy_data_to_ca (VALUE self, VALUE vca)
|
83
|
+
{
|
84
|
+
Image *image;
|
85
|
+
PixelPacket *pixels;
|
86
|
+
long columns, rows;
|
87
|
+
long size, n;
|
88
|
+
CArray *ca;
|
89
|
+
int32_t dim[3];
|
90
|
+
|
91
|
+
Data_Get_Struct(self, Image, image);
|
92
|
+
columns = image->columns;
|
93
|
+
rows = image->rows;
|
94
|
+
size = columns * rows;
|
95
|
+
dim[0] = rows;
|
96
|
+
dim[1] = columns;
|
97
|
+
dim[2] = 4;
|
98
|
+
|
99
|
+
rb_check_carray_object(vca);
|
100
|
+
Data_Get_Struct(vca, CArray, ca);
|
101
|
+
ca_check_shape(ca, 3, dim);
|
102
|
+
|
103
|
+
switch ( sizeof(Quantum) ) {
|
104
|
+
case 1:
|
105
|
+
ca_check_data_type(ca, CA_UINT8);
|
106
|
+
break;
|
107
|
+
case 2:
|
108
|
+
ca_check_data_type(ca, CA_UINT16);
|
109
|
+
break;
|
110
|
+
case 4:
|
111
|
+
ca_check_data_type(ca, CA_UINT32);
|
112
|
+
break;
|
113
|
+
default:
|
114
|
+
rb_raise(rb_eRuntimeError, "invalid data_type of given carray");
|
115
|
+
}
|
116
|
+
|
117
|
+
SetImageType(image, TrueColorType);
|
118
|
+
|
119
|
+
pixels = (PixelPacket *) GetVirtualPixels(image,
|
120
|
+
0, 0, columns, rows, &image->exception);
|
121
|
+
|
122
|
+
if ( ! pixels ) {
|
123
|
+
rb_raise(rb_eRuntimeError, "failed to get image pixels");
|
124
|
+
}
|
125
|
+
|
126
|
+
ca_attach(ca);
|
127
|
+
|
128
|
+
switch ( sizeof(Quantum) ) {
|
129
|
+
case 1: {
|
130
|
+
PixelPacket *p;
|
131
|
+
u_int8_t *q;
|
132
|
+
p = pixels;
|
133
|
+
q = (u_int8_t *)ca->ptr;
|
134
|
+
for (n=0; n<size; n++) {
|
135
|
+
*q++ = p->red;
|
136
|
+
*q++ = p->green;
|
137
|
+
*q++ = p->blue;
|
138
|
+
*q++ = p->opacity;
|
139
|
+
p++;
|
140
|
+
}
|
141
|
+
break;
|
142
|
+
}
|
143
|
+
case 2: {
|
144
|
+
PixelPacket *p;
|
145
|
+
u_int16_t *q;
|
146
|
+
p = pixels;
|
147
|
+
q = (u_int16_t *)ca->ptr;
|
148
|
+
for (n=0; n<size; n++) {
|
149
|
+
*q++ = p->red;
|
150
|
+
*q++ = p->green;
|
151
|
+
*q++ = p->blue;
|
152
|
+
*q++ = p->opacity;
|
153
|
+
p++;
|
154
|
+
}
|
155
|
+
break;
|
156
|
+
}
|
157
|
+
case 4: {
|
158
|
+
PixelPacket *p;
|
159
|
+
u_int32_t *q;
|
160
|
+
p = pixels;
|
161
|
+
q = (u_int32_t *)ca->ptr;
|
162
|
+
for (n=0; n<size; n++) {
|
163
|
+
*q++ = p->red;
|
164
|
+
*q++ = p->green;
|
165
|
+
*q++ = p->blue;
|
166
|
+
*q++ = p->opacity;
|
167
|
+
p++;
|
168
|
+
}
|
169
|
+
break;
|
170
|
+
}
|
171
|
+
}
|
172
|
+
|
173
|
+
ca_sync(ca);
|
174
|
+
ca_detach(ca);
|
175
|
+
|
176
|
+
return vca;
|
177
|
+
}
|
178
|
+
|
179
|
+
static VALUE
|
180
|
+
Image_sync_data_from_ca (VALUE self, VALUE vca)
|
181
|
+
{
|
182
|
+
Image *image;
|
183
|
+
PixelPacket *pixels;
|
184
|
+
long columns, rows;
|
185
|
+
long size, n;
|
186
|
+
int okay;
|
187
|
+
CArray *ca;
|
188
|
+
int32_t dim[3];
|
189
|
+
|
190
|
+
Data_Get_Struct(self, Image, image);
|
191
|
+
columns = image->columns;
|
192
|
+
rows = image->rows;
|
193
|
+
size = columns * rows;
|
194
|
+
dim[0] = rows;
|
195
|
+
dim[1] = columns;
|
196
|
+
dim[2] = 4;
|
197
|
+
|
198
|
+
rb_check_carray_object(vca);
|
199
|
+
Data_Get_Struct(vca, CArray, ca);
|
200
|
+
ca_check_shape(ca, 3, dim);
|
201
|
+
|
202
|
+
switch ( sizeof(Quantum) ) {
|
203
|
+
case 1:
|
204
|
+
ca_check_data_type(ca, CA_UINT8);
|
205
|
+
break;
|
206
|
+
case 2:
|
207
|
+
ca_check_data_type(ca, CA_UINT16);
|
208
|
+
break;
|
209
|
+
case 4:
|
210
|
+
ca_check_data_type(ca, CA_UINT32);
|
211
|
+
break;
|
212
|
+
default:
|
213
|
+
rb_raise(rb_eRuntimeError, "invalid data_type of given carray");
|
214
|
+
}
|
215
|
+
|
216
|
+
SetImageType(image, TrueColorType);
|
217
|
+
|
218
|
+
pixels = (PixelPacket *) GetVirtualPixels(image,
|
219
|
+
0, 0, columns, rows, &image->exception);
|
220
|
+
|
221
|
+
if ( pixels ) {
|
222
|
+
PixelPacket *p = pixels;
|
223
|
+
ca_attach(ca);
|
224
|
+
switch ( sizeof(Quantum) ) {
|
225
|
+
case 1: {
|
226
|
+
u_int8_t *q = (u_int8_t *) ca->ptr;
|
227
|
+
for (n=0; n<size; n++) {
|
228
|
+
p->red = *q++;
|
229
|
+
p->green = *q++;
|
230
|
+
p->blue = *q++;
|
231
|
+
p->opacity = *q++;
|
232
|
+
p++;
|
233
|
+
}
|
234
|
+
break;
|
235
|
+
}
|
236
|
+
case 2: {
|
237
|
+
u_int16_t *q = (u_int16_t *) ca->ptr;
|
238
|
+
for (n=0; n<size; n++) {
|
239
|
+
p->red = *q++;
|
240
|
+
p->green = *q++;
|
241
|
+
p->blue = *q++;
|
242
|
+
p->opacity = *q++;
|
243
|
+
p++;
|
244
|
+
}
|
245
|
+
break;
|
246
|
+
}
|
247
|
+
case 4: {
|
248
|
+
u_int32_t *q = (u_int32_t *) ca->ptr;
|
249
|
+
for (n=0; n<size; n++) {
|
250
|
+
p->red = *q++;
|
251
|
+
p->green = *q++;
|
252
|
+
p->blue = *q++;
|
253
|
+
p->opacity = *q++;
|
254
|
+
p++;
|
255
|
+
}
|
256
|
+
break;
|
257
|
+
}
|
258
|
+
}
|
259
|
+
|
260
|
+
ca_detach(ca);
|
261
|
+
|
262
|
+
okay = SyncAuthenticPixels(image, &image->exception);
|
263
|
+
|
264
|
+
if ( ! okay ) {
|
265
|
+
rb_raise(rb_eRuntimeError, "image pixels could not be synced");
|
266
|
+
}
|
267
|
+
|
268
|
+
}
|
269
|
+
|
270
|
+
return self;
|
271
|
+
}
|
272
|
+
|
273
|
+
|
274
|
+
static VALUE
|
275
|
+
Image_fill_data (VALUE self, VALUE vval)
|
276
|
+
{
|
277
|
+
Image *image;
|
278
|
+
PixelPacket *pixels;
|
279
|
+
unsigned long val;
|
280
|
+
long columns, rows;
|
281
|
+
long size, n;
|
282
|
+
int okay;
|
283
|
+
|
284
|
+
Data_Get_Struct(self, Image, image);
|
285
|
+
columns = image->columns;
|
286
|
+
rows = image->rows;
|
287
|
+
size = columns * rows;
|
288
|
+
|
289
|
+
val = NUM2ULONG(vval);
|
290
|
+
|
291
|
+
SetImageType(image, TrueColorType);
|
292
|
+
|
293
|
+
pixels = (PixelPacket *) GetAuthenticPixels(image, 0, 0, columns, rows, &image->exception);
|
294
|
+
|
295
|
+
if ( pixels ) {
|
296
|
+
PixelPacket *p = pixels;
|
297
|
+
switch ( sizeof(Quantum) ) {
|
298
|
+
case 1: {
|
299
|
+
for (n=0; n<size; n++) {
|
300
|
+
p->red = val;
|
301
|
+
p->green = val;
|
302
|
+
p->blue = val;
|
303
|
+
p->opacity = val;
|
304
|
+
p++;
|
305
|
+
}
|
306
|
+
break;
|
307
|
+
}
|
308
|
+
case 2: {
|
309
|
+
for (n=0; n<size; n++) {
|
310
|
+
p->red = val;
|
311
|
+
p->green = val;
|
312
|
+
p->blue = val;
|
313
|
+
p->opacity = val;
|
314
|
+
p++;
|
315
|
+
}
|
316
|
+
break;
|
317
|
+
}
|
318
|
+
case 4: {
|
319
|
+
for (n=0; n<size; n++) {
|
320
|
+
p->red = val;
|
321
|
+
p->green = val;
|
322
|
+
p->blue = val;
|
323
|
+
p->opacity = val;
|
324
|
+
p++;
|
325
|
+
}
|
326
|
+
break;
|
327
|
+
}
|
328
|
+
}
|
329
|
+
|
330
|
+
okay = SyncAuthenticPixels(image, &image->exception);
|
331
|
+
|
332
|
+
if ( ! okay ) {
|
333
|
+
rb_raise(rb_eRuntimeError, "image pixels could not be synced");
|
334
|
+
}
|
335
|
+
|
336
|
+
}
|
337
|
+
|
338
|
+
return self;
|
339
|
+
}
|
340
|
+
|
341
|
+
void
|
342
|
+
Init_carray_rmagick ()
|
343
|
+
{
|
344
|
+
VALUE mMagick = rb_const_get(rb_cObject, rb_intern("Magick"));
|
345
|
+
VALUE cImage = rb_const_get(mMagick, rb_intern("Image"));
|
346
|
+
|
347
|
+
rb_define_method(cImage, "_fetch_index", Image_fetch_index, 1);
|
348
|
+
rb_define_method(cImage, "_store_index", Image_store_index, 2);
|
349
|
+
rb_define_method(cImage, "_copy_data_to_ca", Image_copy_data_to_ca, 1);
|
350
|
+
rb_define_method(cImage, "_sync_data_from_ca", Image_sync_data_from_ca, 1);
|
351
|
+
rb_define_method(cImage, "_fill_data", Image_fill_data, 1);
|
352
|
+
}
|
353
|
+
|
data/extconf.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'mkmf'
|
2
|
+
require 'carray/mkmf'
|
3
|
+
|
4
|
+
begin
|
5
|
+
have_image_magick = true
|
6
|
+
$CPPFLAGS << " " << `Magick-config --cflags --cppflags`.split("\n").join(" ")
|
7
|
+
`Magick-config --libs`.chomp.split(/\s+/).uniq.each do |opt|
|
8
|
+
case opt
|
9
|
+
when /\-L(.*)/
|
10
|
+
$LDFLAGS << " " << opt
|
11
|
+
end
|
12
|
+
end
|
13
|
+
`Magick-config --libs`.chomp.split(/\s+/).uniq.each do |opt|
|
14
|
+
case opt
|
15
|
+
when /\-l(.*)/
|
16
|
+
unless have_library($1)
|
17
|
+
have_image_magick = false
|
18
|
+
break
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
rescue
|
23
|
+
end
|
24
|
+
|
25
|
+
if have_carray()
|
26
|
+
if have_header("magick/api.h") and have_image_magick
|
27
|
+
create_makefile("carray/carray_rmagick")
|
28
|
+
else
|
29
|
+
open("Makefile", "w") { |io|
|
30
|
+
io << "all:" << "\n"
|
31
|
+
io << "install:" << "\n"
|
32
|
+
io << "clean:" << "\n"
|
33
|
+
io << "distclean:" << "\n"
|
34
|
+
io << "\trm -rf mkmf.log Makefile" << "\n"
|
35
|
+
}
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Magick
|
2
|
+
class Image
|
3
|
+
rmagick_rb = "carray/io/rmagick"
|
4
|
+
autoload_method "export_pixels_to_ca", rmagick_rb
|
5
|
+
autoload_method "import_pixels_from_ca", rmagick_rb
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class CArray
|
10
|
+
rmagick_rb = "carray/io/rmagick"
|
11
|
+
autoload_method "self.load_image", rmagick_rb
|
12
|
+
autoload_method "save_image", rmagick_rb
|
13
|
+
autoload_method "display_image", rmagick_rb
|
14
|
+
autoload_method "to_image", rmagick_rb
|
15
|
+
end
|
16
|
+
|
17
|
+
|
data/lib/io/rmagick.rb
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
# ----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# carray/io/rmagick.rb
|
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-2008 Hiroki Motoyoshi
|
10
|
+
#
|
11
|
+
# ----------------------------------------------------------------------------
|
12
|
+
|
13
|
+
require "RMagick"
|
14
|
+
require "carray/carray_rmagick"
|
15
|
+
|
16
|
+
class Magick::Image
|
17
|
+
|
18
|
+
def export_pixels_to_ca (ca, map="RGB",x=0,y=0)
|
19
|
+
case ca.data_type
|
20
|
+
when CA_FIXLEN
|
21
|
+
case ca.bytes
|
22
|
+
when map.size
|
23
|
+
stype = Magick::CharPixel
|
24
|
+
when 2*map.size
|
25
|
+
stype = Magick::ShortPixel
|
26
|
+
when 4*map.size
|
27
|
+
stype = Magick::IntegerPixel
|
28
|
+
else
|
29
|
+
raise
|
30
|
+
end
|
31
|
+
when CA_UINT8, CA_INT8
|
32
|
+
stype = Magick::CharPixel
|
33
|
+
when CA_UINT16, CA_INT16
|
34
|
+
stype = Magick::ShortPixel
|
35
|
+
when CA_UINT32, CA_INT32
|
36
|
+
stype = Magick::IntegerPixel
|
37
|
+
when CA_FLOAT32
|
38
|
+
stype = Magick::FloatPixel
|
39
|
+
when CA_FLOAT64
|
40
|
+
stype = Magick::DoublePixel
|
41
|
+
else
|
42
|
+
raise "invalid strage type"
|
43
|
+
end
|
44
|
+
case ca.rank
|
45
|
+
when 2
|
46
|
+
if map.size != 1
|
47
|
+
raise
|
48
|
+
end
|
49
|
+
when 3
|
50
|
+
if map.size != ca.dim.last
|
51
|
+
raise
|
52
|
+
end
|
53
|
+
else
|
54
|
+
raise "invalid rank"
|
55
|
+
end
|
56
|
+
ca.load_binary(export_pixels_to_str(x,y,ca.dim1,ca.dim0,map,stype))
|
57
|
+
return ca
|
58
|
+
end
|
59
|
+
|
60
|
+
def import_pixels_from_ca (ca,map="RGBO",x=0,y=0)
|
61
|
+
case ca.data_type
|
62
|
+
when CA_FIXLEN
|
63
|
+
case ca.bytes
|
64
|
+
when map.size
|
65
|
+
stype = Magick::CharPixel
|
66
|
+
when 2*map.size
|
67
|
+
stype = Magick::ShortPixel
|
68
|
+
when 4*map.size
|
69
|
+
stype = Magick::IntegerPixel
|
70
|
+
else
|
71
|
+
raise
|
72
|
+
end
|
73
|
+
when CA_UINT8, CA_INT8
|
74
|
+
stype = Magick::CharPixel
|
75
|
+
when CA_UINT16, CA_INT16
|
76
|
+
stype = Magick::ShortPixel
|
77
|
+
when CA_UINT32, CA_INT32
|
78
|
+
stype = Magick::IntegerPixel
|
79
|
+
when CA_FLOAT32
|
80
|
+
stype = Magick::FloatPixel
|
81
|
+
when CA_FLOAT64
|
82
|
+
stype = Magick::DoublePixel
|
83
|
+
else
|
84
|
+
raise "invalid strage type"
|
85
|
+
end
|
86
|
+
import_pixels(x,y,ca.dim1,ca.dim0,map,ca.dump_binary,stype)
|
87
|
+
return self
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
class CArray
|
93
|
+
|
94
|
+
def self.load_image (file, map="RGBO", data_type = nil)
|
95
|
+
img = Magick::Image.read(file).first
|
96
|
+
rows, cols = img.rows, img.columns
|
97
|
+
unless data_type
|
98
|
+
case img.quantum_depth
|
99
|
+
when 8
|
100
|
+
data_type = CA_UINT8
|
101
|
+
when 16
|
102
|
+
data_type = CA_UINT16
|
103
|
+
when 32
|
104
|
+
data_type = CA_UINT32
|
105
|
+
end
|
106
|
+
end
|
107
|
+
case map.size
|
108
|
+
when 1
|
109
|
+
out = CArray.new(data_type, [rows, cols])
|
110
|
+
else
|
111
|
+
out = CArray.new(data_type, [rows, cols, map.size])
|
112
|
+
end
|
113
|
+
img.export_pixels_to_ca(out, map)
|
114
|
+
return out
|
115
|
+
end
|
116
|
+
|
117
|
+
def to_image (map="RGBO", ch=nil)
|
118
|
+
img = Magick::Image.new(dim1, dim0)
|
119
|
+
case data_type
|
120
|
+
when CA_FIXLEN
|
121
|
+
img.import_pixels_from_ca(self, map)
|
122
|
+
else
|
123
|
+
case rank
|
124
|
+
when 2
|
125
|
+
map = "I"
|
126
|
+
ca = self[:%, :%, 1]
|
127
|
+
when 3
|
128
|
+
unless ch
|
129
|
+
ch = Array.new(map.size){|i| i}
|
130
|
+
end
|
131
|
+
ca = self[nil,nil,CA_INT(ch)]
|
132
|
+
end
|
133
|
+
img.import_pixels_from_ca(ca, map)
|
134
|
+
end
|
135
|
+
return img
|
136
|
+
end
|
137
|
+
|
138
|
+
def save_image (file, map="RGBO", ch=nil)
|
139
|
+
to_image(map, ch).write(file)
|
140
|
+
return nil
|
141
|
+
end
|
142
|
+
|
143
|
+
def display_image (map="RGBO", ch=nil)
|
144
|
+
to_image(map, ch).display
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# ----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# carray/object/ca_obj_magick_image.rb
|
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-2008 Hiroki Motoyoshi
|
10
|
+
#
|
11
|
+
# ----------------------------------------------------------------------------
|
12
|
+
|
13
|
+
require "RMagick"
|
14
|
+
require "carray/carray_rmagick"
|
15
|
+
|
16
|
+
class CAMagickImage < CAObject
|
17
|
+
|
18
|
+
DEPTH_TO_TYPE = {
|
19
|
+
8 => CA_UINT8,
|
20
|
+
16 => CA_UINT16,
|
21
|
+
32 => CA_UINT32
|
22
|
+
}
|
23
|
+
|
24
|
+
def initialize (image)
|
25
|
+
@image = image
|
26
|
+
if Magick.const_defined?(:QuantumDepth)
|
27
|
+
magick_quantum_depth = Magick::QuantumDepth
|
28
|
+
else
|
29
|
+
magick_quantum_depth = Magick::MAGICKCORE_QUANTUM_DEPTH
|
30
|
+
end
|
31
|
+
@qtype = DEPTH_TO_TYPE[magick_quantum_depth]
|
32
|
+
type = DEPTH_TO_TYPE[@image.quantum_depth]
|
33
|
+
@scale = 0
|
34
|
+
(magick_quantum_depth / @image.quantum_depth).times do |i|
|
35
|
+
@scale += 1 << @image.quantum_depth * i
|
36
|
+
end
|
37
|
+
super(type, [@image.rows, @image.columns, 4])
|
38
|
+
end
|
39
|
+
|
40
|
+
def fetch_index (idx)
|
41
|
+
return @image._fetch_index(idx)/@scale
|
42
|
+
end
|
43
|
+
|
44
|
+
def store_index (idx, val)
|
45
|
+
return @image._store_index(idx, val*@scale)
|
46
|
+
end
|
47
|
+
|
48
|
+
def copy_data (data)
|
49
|
+
qdata = CArray.wrap_writable(data, @qtype)
|
50
|
+
qdata.attach! {
|
51
|
+
@image._copy_data_to_ca(qdata)
|
52
|
+
if @scale != 1
|
53
|
+
qdata.div!(@scale)
|
54
|
+
end
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
def sync_data (data)
|
59
|
+
qdata = CArray.wrap_readonly(data, @qtype)
|
60
|
+
qdata.attach {
|
61
|
+
if @scale != 1
|
62
|
+
qdata.mul!(@scale)
|
63
|
+
end
|
64
|
+
@image._sync_data_from_ca(qdata)
|
65
|
+
}
|
66
|
+
end
|
67
|
+
|
68
|
+
def fill_data (val)
|
69
|
+
return @image._fill_data(val*@scale)
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
class Magick::Image
|
75
|
+
|
76
|
+
def ca
|
77
|
+
return CAMagickImage.new(self)
|
78
|
+
end
|
79
|
+
|
80
|
+
def to_ca
|
81
|
+
return ca.to_ca
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: carray-rmagick
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hiroki Motoyoshi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |2
|
14
|
+
Extension for converting Magick::Image to/from CArray
|
15
|
+
email: ''
|
16
|
+
executables: []
|
17
|
+
extensions:
|
18
|
+
- extconf.rb
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- README.md
|
22
|
+
- carray-rmagick.gemspec
|
23
|
+
- carray_rmagick.c
|
24
|
+
- extconf.rb
|
25
|
+
- lib/autoload/autoload_io_rmagick.rb
|
26
|
+
- lib/autoload/autoload_object_magick_image.rb
|
27
|
+
- lib/carray-rmagick.rb
|
28
|
+
- lib/io/rmagick.rb
|
29
|
+
- lib/object/ca_obj_magick_image.rb
|
30
|
+
homepage: https://github.com/himotoyoshi/carray_rmagick
|
31
|
+
licenses: []
|
32
|
+
metadata: {}
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 1.8.1
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 2.6.4
|
50
|
+
signing_key:
|
51
|
+
specification_version: 4
|
52
|
+
summary: Extension for converting Magick::Image to/from CArray
|
53
|
+
test_files: []
|
54
|
+
has_rdoc: false
|