ruby-vips 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/ext/mask.h ADDED
@@ -0,0 +1,17 @@
1
+ #ifndef RUBY_VIPS_MASK_H
2
+ #define RUBY_VIPS_MASK_H
3
+
4
+ extern VALUE cVIPSMask;
5
+
6
+ struct _vipsMask {
7
+ INTMASK *imask;
8
+ DOUBLEMASK *dmask;
9
+ };
10
+ typedef struct _vipsMask vipsMask;
11
+
12
+ VALUE mask_alloc(VALUE);
13
+ VALUE imask2rb(INTMASK*);
14
+ VALUE dmask2rb(DOUBLEMASK*);
15
+ void mask_arg2mask(VALUE, INTMASK**, DOUBLEMASK**);
16
+
17
+ #endif
data/ext/reader.c ADDED
@@ -0,0 +1,315 @@
1
+ #include "ruby_vips.h"
2
+ #include "image.h"
3
+ #include "header.h"
4
+
5
+ ID id_iv__vips_fmt;
6
+
7
+ VipsFormatClass*
8
+ reader_get_fmt_class(VALUE klass)
9
+ {
10
+ VipsFormatClass *fmt_class;
11
+ VALUE nickname_v;
12
+ char *nickname;
13
+
14
+ nickname_v = rb_ivar_get(klass, id_iv__vips_fmt);
15
+ if (NIL_P(nickname_v))
16
+ return NULL;
17
+
18
+ nickname = StringValuePtr(nickname_v);
19
+
20
+ if (!(fmt_class = (VipsFormatClass*)vips_class_find("VipsFormat", nickname)))
21
+ return NULL;
22
+
23
+ return fmt_class;
24
+ }
25
+
26
+ /* :nodoc: */
27
+
28
+ static VALUE
29
+ reader_read_header(VALUE obj, VALUE path)
30
+ {
31
+ VipsImage *im_new;
32
+ VipsFormatClass *fmt_class = reader_get_fmt_class(CLASS_OF(obj));
33
+ GetImg(obj, data, im);
34
+
35
+ if (!fmt_class)
36
+ return Qfalse;
37
+
38
+ if (!(im_new = (VipsImage*)im_open("reader_read_header", "p")))
39
+ vips_lib_error();
40
+
41
+ if (fmt_class->header(StringValuePtr(path), im_new)) {
42
+ im_close(im_new);
43
+ vips_lib_error();
44
+ }
45
+
46
+ data->in = im_new;
47
+
48
+ return Qtrue;
49
+ }
50
+
51
+ /* :nodoc: */
52
+
53
+ static VALUE
54
+ reader_s_recognized_p(VALUE klass, VALUE path)
55
+ {
56
+ VipsFormatClass *fmt_class = reader_get_fmt_class(klass);
57
+
58
+ if (fmt_class && fmt_class->is_a && fmt_class->is_a(StringValuePtr(path)))
59
+ return Qtrue;
60
+
61
+ return Qfalse;
62
+ }
63
+
64
+ /* :nodoc: */
65
+
66
+ static VALUE
67
+ reader_read_internal(VALUE obj, VALUE path)
68
+ {
69
+ VipsImage *im;
70
+
71
+ if (!(im = im_open(StringValuePtr(path), "r")))
72
+ vips_lib_error();
73
+
74
+ return img_init(cVIPSImage, im);
75
+ }
76
+
77
+ /* :nodoc: */
78
+
79
+ static VALUE
80
+ jpeg_read_internal(VALUE obj, VALUE path)
81
+ {
82
+ OutPartial(new, data, im);
83
+
84
+ if (im_jpeg2vips(StringValuePtr(path), im))
85
+ vips_lib_error();
86
+
87
+ return new;
88
+ }
89
+
90
+ /* :nodoc: */
91
+
92
+ static VALUE
93
+ tiff_read_internal(VALUE obj, VALUE path)
94
+ {
95
+ OutPartial(new, data, im);
96
+
97
+ if (im_tiff2vips(StringValuePtr(path), im))
98
+ vips_lib_error();
99
+
100
+ return new;
101
+ }
102
+
103
+ /* :nodoc: */
104
+
105
+ static VALUE
106
+ magick_read_internal(VALUE obj, VALUE path)
107
+ {
108
+ OutPartial(new, data, im);
109
+
110
+ if (im_magick2vips(StringValuePtr(path), im))
111
+ vips_lib_error();
112
+
113
+ return new;
114
+ }
115
+
116
+ /* :nodoc: */
117
+
118
+ static VALUE
119
+ exr_read_internal(VALUE obj, VALUE path)
120
+ {
121
+ OutPartial(new, data, im);
122
+
123
+ if (im_exr2vips(StringValuePtr(path), im))
124
+ vips_lib_error();
125
+
126
+ return new;
127
+ }
128
+
129
+ /* :nodoc: */
130
+
131
+ static VALUE
132
+ ppm_read_internal(VALUE obj, VALUE path)
133
+ {
134
+ OutPartial(new, data, im);
135
+
136
+ if (im_ppm2vips(StringValuePtr(path), im))
137
+ vips_lib_error();
138
+
139
+ return new;
140
+ }
141
+
142
+ /* :nodoc: */
143
+
144
+ static VALUE
145
+ analyze_read_internal(VALUE obj, VALUE path)
146
+ {
147
+ OutPartial(new, data, im);
148
+
149
+ if (im_analyze2vips(StringValuePtr(path), im))
150
+ vips_lib_error();
151
+
152
+ return new;
153
+ }
154
+
155
+ /* :nodoc: */
156
+
157
+ static VALUE
158
+ csv_read_internal(VALUE obj, VALUE path)
159
+ {
160
+ OutPartial(new, data, im);
161
+
162
+ if (im_csv2vips(StringValuePtr(path), im))
163
+ vips_lib_error();
164
+
165
+ return new;
166
+ }
167
+
168
+ /* :nodoc: */
169
+
170
+ static VALUE
171
+ png_read_internal(VALUE obj, VALUE path)
172
+ {
173
+ OutPartial(new, data, im);
174
+
175
+ if (im_png2vips(StringValuePtr(path), im))
176
+ vips_lib_error();
177
+
178
+ return new;
179
+ }
180
+
181
+ /* :nodoc: */
182
+
183
+ static VALUE
184
+ raw_read_internal(VALUE obj, VALUE path, VALUE width, VALUE height, VALUE bpp,
185
+ VALUE offset)
186
+ {
187
+ OutPartial(new, data, im);
188
+
189
+ if (im_raw2vips(StringValuePtr(path), im, NUM2INT(width), NUM2INT(height),
190
+ NUM2INT(bpp), NUM2INT(offset)))
191
+ vips_lib_error();
192
+
193
+ return new;
194
+ }
195
+
196
+ /* :nodoc: */
197
+
198
+ static VALUE
199
+ vips_read_internal(VALUE obj, VALUE path)
200
+ {
201
+ VipsImage *im;
202
+
203
+ if (!(im = (VipsImage *)im_open(StringValuePtr(path),"r")))
204
+ vips_lib_error();
205
+
206
+ return img_init(cVIPSImage, im);
207
+ }
208
+
209
+ static void
210
+ reader_fmt_set(VALUE klass, const char* val)
211
+ {
212
+ rb_ivar_set(klass, id_iv__vips_fmt, rb_str_new_cstr(val));
213
+ }
214
+
215
+ /*
216
+ * Base class for image format readers.
217
+ */
218
+
219
+ void
220
+ init_Reader()
221
+ {
222
+ id_iv__vips_fmt = rb_intern("@_vips_fmt");
223
+
224
+ VALUE reader = rb_define_class_under(mVIPS, "Reader", rb_cObject);
225
+ rb_include_module(reader, mVIPSHeader);
226
+ rb_define_alloc_func(reader, img_alloc);
227
+
228
+ rb_define_singleton_method(reader, "recognized?", reader_s_recognized_p, 1);
229
+ rb_define_private_method(reader, "read_header", reader_read_header, 1);
230
+ rb_define_private_method(reader, "read_internal", reader_read_internal, 1);
231
+
232
+ /*
233
+ * Read JPEG images.
234
+ */
235
+
236
+ VALUE jpeg_reader = rb_define_class_under(mVIPS, "JPEGReader", reader);
237
+ rb_define_private_method(jpeg_reader, "read_internal", jpeg_read_internal, 1);
238
+ reader_fmt_set(jpeg_reader, "jpeg");
239
+
240
+ /*
241
+ * Read TIFF images.
242
+ */
243
+
244
+ VALUE tiff_reader = rb_define_class_under(mVIPS, "TIFFReader", reader);
245
+ rb_define_private_method(tiff_reader, "read_internal", tiff_read_internal, 1);
246
+ reader_fmt_set(tiff_reader, "tiff");
247
+
248
+ /*
249
+ * Read PPM images.
250
+ */
251
+
252
+ VALUE ppm_reader = rb_define_class_under(mVIPS, "PPMReader", reader);
253
+ rb_define_private_method(ppm_reader, "read_internal", ppm_read_internal, 1);
254
+ reader_fmt_set(ppm_reader, "ppm");
255
+
256
+ /*
257
+ * Read PNG images.
258
+ */
259
+
260
+ VALUE png_reader = rb_define_class_under(mVIPS, "PNGReader", reader);
261
+ rb_define_private_method(png_reader, "read_internal", png_read_internal, 1);
262
+ reader_fmt_set(png_reader, "png");
263
+
264
+ /*
265
+ * Read CSV images.
266
+ */
267
+
268
+ VALUE csv_reader = rb_define_class_under(mVIPS, "CSVReader", reader);
269
+ rb_define_private_method(csv_reader, "read_internal", csv_read_internal, 1);
270
+ reader_fmt_set(csv_reader, "csv");
271
+
272
+ /*
273
+ * Read EXR images.
274
+ */
275
+
276
+ VALUE exr_reader = rb_define_class_under(mVIPS, "EXRReader", reader);
277
+ rb_define_private_method(exr_reader, "read_internal", exr_read_internal, 1);
278
+ reader_fmt_set(exr_reader, "exr");
279
+
280
+ /*
281
+ * Read native VIPS images.
282
+ */
283
+
284
+ VALUE vips_reader = rb_define_class_under(mVIPS, "VIPSReader", reader);
285
+ rb_define_private_method(vips_reader, "read_internal", vips_read_internal, 1);
286
+ reader_fmt_set(vips_reader, "vips");
287
+
288
+ /*
289
+ * Read images using a magick library.
290
+ */
291
+
292
+ VALUE magick_reader = rb_define_class_under(mVIPS, "MagickReader", reader);
293
+ rb_define_private_method(magick_reader, "read_internal", magick_read_internal, 1);
294
+ reader_fmt_set(magick_reader, "magick");
295
+
296
+ /*
297
+ * Read Analyze images.
298
+ */
299
+
300
+ VALUE analyze_reader = rb_define_class_under(mVIPS, "AnalyzeReader", reader);
301
+ rb_define_private_method(analyze_reader, "read_internal", analyze_read_internal, 1);
302
+ reader_fmt_set(analyze_reader, "analyze");
303
+
304
+ /*
305
+ * Read RAW images.
306
+ */
307
+
308
+ VALUE raw_reader = rb_define_class_under(mVIPS, "RAWReader", reader);
309
+ rb_define_private_method(raw_reader, "read_internal", raw_read_internal, 5);
310
+ reader_fmt_set(raw_reader, "raw");
311
+
312
+ #if 0
313
+ VALUE mVIPS = rb_define_module("VIPS");
314
+ #endif
315
+ }
data/ext/ruby_vips.c ADDED
@@ -0,0 +1,131 @@
1
+ #include "ruby_vips.h"
2
+
3
+ VALUE mVIPS, eVIPSError;
4
+
5
+ void
6
+ vips_lib_error()
7
+ {
8
+ VALUE vips_error = rb_str_new2(im_error_buffer());
9
+ im_error_clear();
10
+ rb_raise(eVIPSError, "VIPS error: %s", RSTRING_PTR(vips_error));
11
+ }
12
+
13
+ static VALUE
14
+ vips_s_version_string()
15
+ {
16
+ return rb_str_new2(im_version_string());
17
+ }
18
+
19
+ static VALUE
20
+ vips_s_version_array()
21
+ {
22
+ VALUE arr = rb_ary_new2(3);
23
+ int i;
24
+
25
+ for(i = 0; i < 3; i++)
26
+ rb_ary_push(arr, INT2FIX(im_version(i)));
27
+
28
+ return arr;
29
+ }
30
+
31
+ /*
32
+ * call-seq:
33
+ * VIPS.debug_info -> string
34
+ *
35
+ * This will print internal debugging information from VIPS, including memory
36
+ * allocation information.
37
+ */
38
+ static VALUE
39
+ vips_s_debug_info(VALUE obj)
40
+ {
41
+ im__print_all();
42
+ return Qnil;
43
+ }
44
+
45
+ /*
46
+ * This method will kill the ruby interpreter and then invokes im__print_all()
47
+ * to hunt for dangling Vips allocations.
48
+ */
49
+ static VALUE
50
+ vips_exit_info(VALUE obj)
51
+ {
52
+ ruby_finalize();
53
+ im__print_all();
54
+ exit(0);
55
+ return Qnil;
56
+ }
57
+
58
+ /*
59
+ * Build a call to im_init_world() and pass command line options to vips. This
60
+ * sets some library wide options.
61
+ */
62
+
63
+ static void
64
+ init_vips_library()
65
+ {
66
+ GOptionContext *context;
67
+ GError *error = NULL;
68
+ int i, argc;
69
+ char **argv;
70
+ VALUE argv_0, argv_v;
71
+
72
+ argv_0 = rb_gv_get("0");
73
+
74
+ if (NIL_P(argv_0))
75
+ im_init_world("");
76
+ else
77
+ im_init_world(RSTRING_PTR(argv_0));
78
+
79
+ argv_v = rb_const_get(rb_mKernel, rb_intern("ARGV"));
80
+
81
+ if (!NIL_P(argv_v))
82
+ {
83
+ argc = RARRAY_LEN(argv_v) + 1;
84
+ argv = ALLOC_N(char*, argc);
85
+
86
+ argv[0] = RSTRING_PTR(argv_0);
87
+
88
+ for (i=0; i < argc - 1; i++)
89
+ argv[i+1] = RSTRING_PTR(RARRAY_PTR(argv_v)[i]);
90
+
91
+ im_init_world(argv[0]);
92
+
93
+ context = g_option_context_new("- ruby-vips");
94
+ g_option_context_set_ignore_unknown_options(context, TRUE);
95
+
96
+ g_option_context_add_group(context, im_get_option_group());
97
+
98
+ g_option_context_parse(context, &argc, &argv, &error);
99
+ g_option_context_free(context);
100
+
101
+ xfree(argv);
102
+ }
103
+ }
104
+
105
+ /*
106
+ * The VIPS module namespaces all ruby-vips objects.
107
+ */
108
+
109
+ void
110
+ Init_vips_ext()
111
+ {
112
+ init_vips_library();
113
+
114
+ mVIPS = rb_define_module("VIPS");
115
+ eVIPSError = rb_define_class_under(mVIPS, "Error", rb_eStandardError);
116
+
117
+ rb_define_singleton_method(mVIPS, "debug_info", vips_s_debug_info, 0);
118
+
119
+ /* Vips Library version string */
120
+ rb_define_const(mVIPS, "LIB_VERSION", vips_s_version_string());
121
+
122
+ /* Vips Library version as a 3-element array */
123
+ rb_define_const(mVIPS, "LIB_VERSION_ARRAY", vips_s_version_array());
124
+
125
+ init_Header();
126
+ init_Image();
127
+ init_Mask();
128
+ init_Interpolator();
129
+ init_Writer();
130
+ init_Reader();
131
+ }