ruby-epeg 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ data.tar.gz: a74556fb134df892052b76328aea0e4f96d54c40
4
+ metadata.gz: 0c96cb9a81aa3fbb2f2ad4cfb9c00e0e80ce84ea
5
+ SHA512:
6
+ data.tar.gz: 33908fd54a508260a450943c79e033f268122ddd9e19155a7535594b08bec957c0b1f24771bb62ef81df12822f76789716697d57ffb21899872b8b67a843a531
7
+ metadata.gz: 43dd7ceaad8db8ae820edbcf6a1b2e0465e934f87906e2ce0b223afbe329fe4981b8c4befe2141ea7ec32ac0537aaec001137119b3de3759604a11a17db7b512
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ Makefile
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.3
5
+ - 2.0
6
+ - 2.1
7
+ script: "bundle exec rake"
8
+ gemfile:
9
+ - Gemfile
10
+ before_install:
11
+ - uname -a
12
+ - sudo apt-get install libexif-dev libjpeg8-dev
13
+ - gem -v
14
+ - gem install bundler -v '~> 1.7'
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in epeg.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Nelson Darkwah Oppong
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ # ruby-epeg ![Travis CI - build status](https://travis-ci.org/nelsond/ruby-epeg.svg?branch=master)
2
+
3
+ Ruby extension for the epeg library which provides facilities for scaling JPEG images very quickly.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ruby-epeg'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ruby-epeg
20
+
21
+ ## Example
22
+
23
+ ```ruby
24
+ require "epeg"
25
+
26
+ # Set the global default jpeg quality
27
+ Epeg::Image.quality = 90 # default is 85
28
+
29
+ # Create an image object from the file some-image.jpg
30
+ image = Epeg::Image.open("./some-image.jpg")
31
+
32
+ # You can get the width and height without loading any pixels
33
+ "some-image.jpeg is #{image.width}x#{image.height}px"
34
+ # => some-image.jpeg is 1000x500
35
+
36
+ # Resize the image to 50x50, set the quality to 50 and save it
37
+ image.resize(50,50)
38
+ image.quality = 50
39
+ image.write("./some-other-image.jpg")
40
+
41
+ # The image is now closed and you can't do any further operations
42
+ image.closed? # => true
43
+
44
+ # Load the same image from a buffer
45
+ File.open("./some-image.jpg") do |file|
46
+ image = Epeg::Image.from_blob(file.read)
47
+ end
48
+
49
+ # Resize to fill 50x50px (i. e. width and height are at
50
+ # least 50px with preserving the aspect ratio) and return the blob
51
+ image.resize_to_fill(50,50).to_blob
52
+
53
+ # Let's crop the resized image
54
+ image = Epeg::Image.open("./some-other-image.jpg")
55
+ image.crop(50, 50).write("./final-image.jpg")
56
+ ```
@@ -0,0 +1,11 @@
1
+ ROOT = File.expand_path(File.dirname(__FILE__))
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ require "rspec/core/rake_task"
6
+ RSpec::Core::RakeTask.new(:spec => [:compile])
7
+ task :default => :spec
8
+ task :test => :spec
9
+
10
+ require "rake/extensiontask"
11
+ Rake::ExtensionTask.new("epeg")
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'epeg/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby-epeg"
8
+ spec.version = Epeg::VERSION
9
+ spec.authors = ["Nelson Darkwah Oppong"]
10
+ spec.email = ["ndo@felixnelson.de"]
11
+ spec.summary = %q{Ruby extension for the epeg library which provides facilities for scaling JPEG images very quickly.}
12
+ spec.homepage = "http://github.com/nelsond/ruby-epeg"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+ spec.extensions = ["ext/epeg/extconf.rb"]
20
+
21
+ spec.add_dependency "rake-compiler", "~> 0.9"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
@@ -0,0 +1,7 @@
1
+ Copyright (C) 2000 Carsten Haitzler and various contributors (Jerome Foucher, Michal Kowalczuk, Wirtualna Polska)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies of the Software and its Copyright notices. In addition publicly documented acknowledgment must be given that this software has been used if no source code of this software is made available publicly. This includes acknowledgments in either Copyright notices, Manuals, Publicity and Marketing documents or any documentation provided with any product containing this software. This License does not apply to any software that links to the libraries provided by this software (statically or dynamically), but only to the software provided.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,76 @@
1
+ #ifndef _EPEG_H
2
+ #define _EPEG_H
3
+
4
+ #ifdef EAPI
5
+ #undef EAPI
6
+ #endif
7
+ #ifdef WIN32
8
+ # ifdef BUILDING_DLL
9
+ # define EAPI __declspec(dllexport)
10
+ # else
11
+ # define EAPI __declspec(dllimport)
12
+ # endif
13
+ #else
14
+ # ifdef __GNUC__
15
+ # if __GNUC__ >= 4
16
+ # define EAPI __attribute__ ((visibility("default")))
17
+ # else
18
+ # define EAPI
19
+ # endif
20
+ # else
21
+ # define EAPI
22
+ # endif
23
+ #endif
24
+
25
+ #ifdef __cplusplus
26
+ extern "C" {
27
+ #endif
28
+
29
+ typedef enum _Epeg_Colorspace
30
+ {
31
+ EPEG_GRAY8,
32
+ EPEG_YUV8,
33
+ EPEG_RGB8,
34
+ EPEG_BGR8,
35
+ EPEG_RGBA8,
36
+ EPEG_BGRA8,
37
+ EPEG_ARGB32,
38
+ EPEG_CMYK
39
+ }
40
+ Epeg_Colorspace;
41
+
42
+ typedef struct _Epeg_Image Epeg_Image;
43
+ typedef struct _Epeg_Thumbnail_Info Epeg_Thumbnail_Info;
44
+
45
+ struct _Epeg_Thumbnail_Info
46
+ {
47
+ char *uri;
48
+ unsigned long long int mtime;
49
+ int w, h;
50
+ char *mimetype;
51
+ };
52
+
53
+ EAPI Epeg_Image *epeg_file_open (const char *file);
54
+ EAPI Epeg_Image *epeg_memory_open (unsigned char *data, int size);
55
+ EAPI void epeg_size_get (Epeg_Image *im, int *w, int *h);
56
+ EAPI void epeg_decode_size_set (Epeg_Image *im, int w, int h);
57
+ EAPI void epeg_colorspace_get (Epeg_Image *im, int *space);
58
+ EAPI void epeg_decode_colorspace_set (Epeg_Image *im, Epeg_Colorspace colorspace);
59
+ EAPI const void *epeg_pixels_get (Epeg_Image *im, int x, int y, int w, int h);
60
+ EAPI void epeg_pixels_free (Epeg_Image *im, const void *data);
61
+ EAPI const char *epeg_comment_get (Epeg_Image *im);
62
+ EAPI void epeg_thumbnail_comments_get (Epeg_Image *im, Epeg_Thumbnail_Info *info);
63
+ EAPI void epeg_comment_set (Epeg_Image *im, const char *comment);
64
+ EAPI void epeg_quality_set (Epeg_Image *im, int quality);
65
+ EAPI void epeg_thumbnail_comments_enable (Epeg_Image *im, int onoff);
66
+ EAPI void epeg_file_output_set (Epeg_Image *im, const char *file);
67
+ EAPI void epeg_memory_output_set (Epeg_Image *im, unsigned char **data, int *size);
68
+ EAPI int epeg_encode (Epeg_Image *im);
69
+ EAPI int epeg_trim (Epeg_Image *im);
70
+ EAPI void epeg_close (Epeg_Image *im);
71
+
72
+ #ifdef __cplusplus
73
+ }
74
+ #endif
75
+
76
+ #endif
@@ -0,0 +1,414 @@
1
+ #include <epeg_image.h>
2
+
3
+ void Init_epeg()
4
+ {
5
+ VALUE mEpeg = rb_define_module("Epeg"),
6
+ cEpegImage = rb_define_class_under(mEpeg, "Image", rb_cObject);
7
+
8
+ rb_cv_set(cEpegImage, "@@quality", INT2NUM(85));
9
+
10
+ rb_define_singleton_method(cEpegImage, "from_blob", rb_epeg_image_from_blob, 1);
11
+ rb_define_singleton_method(cEpegImage, "open", rb_epeg_image_open, 1);
12
+ rb_define_singleton_method(cEpegImage, "quality=", rb_epeg_image_set_default_quality, 1);
13
+ rb_define_singleton_method(cEpegImage, "quality", rb_epeg_image_get_default_quality, 0);
14
+
15
+ rb_define_method(cEpegImage, "initialize", rb_epeg_image_initialize, 0);
16
+
17
+ rb_define_method(cEpegImage, "resize", rb_epeg_image_resize, 2);
18
+ rb_define_method(cEpegImage, "resize_to_fit", rb_epeg_image_resize_to_fit, 2);
19
+ rb_define_method(cEpegImage, "resize_to_fill", rb_epeg_image_resize_to_fill, 2);
20
+ rb_define_method(cEpegImage, "crop", rb_epeg_image_crop, -1);
21
+ rb_define_method(cEpegImage, "write", rb_epeg_image_write, 1);
22
+ rb_define_method(cEpegImage, "to_blob", rb_epeg_image_to_blob, 0);
23
+ rb_define_method(cEpegImage, "close", rb_epeg_image_close, 0);
24
+ rb_define_method(cEpegImage, "closed?", rb_epeg_image_is_closed, 0);
25
+ rb_define_method(cEpegImage, "quality=", rb_epeg_image_set_quality, 1);
26
+
27
+ rb_define_attr(cEpegImage, "width", 1, 0);
28
+ rb_define_attr(cEpegImage, "height", 1, 0);
29
+ rb_define_attr(cEpegImage, "quality", 1, 0);
30
+ rb_define_attr(cEpegImage, "file_path", 1, 0);
31
+ }
32
+
33
+ /*
34
+ * call-seq:
35
+ * open(file_path)
36
+ *
37
+ * Creates a new Image from +file_path+
38
+ *
39
+ * img = Epeg::Image.open("./example.jpg")
40
+ */
41
+ static VALUE rb_epeg_image_open(VALUE klass, VALUE file_path)
42
+ {
43
+ Check_Type(file_path, T_STRING);
44
+
45
+ Epeg_Image *image = epeg_file_open(StringValueCStr(file_path));
46
+
47
+ if(!image) {
48
+ rb_raise(rb_eRuntimeError, "Error: unable to read file \"%s\"\n",
49
+ StringValueCStr(file_path));
50
+ }
51
+
52
+ VALUE image_obj = Data_Wrap_Struct(klass, NULL, rb_epeg_image_destroy, image);
53
+ rb_obj_call_init(image_obj, 0, NULL);
54
+
55
+ return image_obj;
56
+ }
57
+
58
+ static VALUE rb_epeg_image_from_blob(VALUE klass, VALUE blob)
59
+ {
60
+ Epeg_Image *image = epeg_memory_open(RSTRING_PTR(blob), RSTRING_LEN(blob));
61
+
62
+ if(!image) { rb_raise(rb_eRuntimeError, "Error: unable to read blob"); }
63
+
64
+ VALUE image_obj = Data_Wrap_Struct(klass, NULL, rb_epeg_image_destroy, image);
65
+
66
+ rb_obj_call_init(image_obj, 0, NULL);
67
+
68
+ return image_obj;
69
+ }
70
+
71
+ /*
72
+ * call-seq:
73
+ * quality=(q)
74
+ *
75
+ * Sets the default quality (+q+ >= 0 and +q+ <= 100)
76
+ * See Epeg::Image#quality=
77
+ *
78
+ * Epeg::Image.quality = 10
79
+ */
80
+ static VALUE rb_epeg_image_set_default_quality(VALUE klass, VALUE q)
81
+ {
82
+ Check_Type(q, T_FIXNUM);
83
+ int quality = NUM2INT(q);
84
+
85
+ rb_cv_set(klass, "@@quality", q);
86
+
87
+ return Qnil;
88
+ }
89
+
90
+ /*
91
+ * call-seq:
92
+ * quality
93
+ *
94
+ * Returns the current default quality for all images.
95
+ * See Epeg::Image#quality
96
+ *
97
+ * Epeg::Image.quality #=> 85
98
+ */
99
+ static VALUE rb_epeg_image_get_default_quality(VALUE klass)
100
+ {
101
+ return rb_cv_get(klass, "@@quality");
102
+ }
103
+
104
+ /*
105
+ * call-seq:
106
+ * intialize(file_path)
107
+ *
108
+ * See Epeg::Image.open
109
+ */
110
+ static VALUE rb_epeg_image_initialize(VALUE self)
111
+ {
112
+ Epeg_Image *image;
113
+ Data_Get_Struct(self, Epeg_Image, image);
114
+
115
+ VALUE q = rb_cv_get(CLASS_OF(self), "@@quality");
116
+ epeg_quality_set(image, NUM2UINT(q));
117
+ rb_iv_set(self, "@quality", q);
118
+
119
+ unsigned int w, h;
120
+ epeg_size_get(image, &w, &h);
121
+
122
+ rb_iv_set(self, "@width", UINT2NUM(w));
123
+ rb_iv_set(self, "@height", UINT2NUM(h));
124
+
125
+ rb_iv_set(self, "epeg_file_closed", Qfalse);
126
+ rb_iv_set(self, "epeg_trimmed", Qfalse);
127
+
128
+ return self;
129
+ }
130
+
131
+ /*
132
+ * call-seq:
133
+ * resize(w, h)
134
+ *
135
+ * Resizes image to +w+ x +h+. Does not keep aspect ratio of the image
136
+ *
137
+ * img = Epeg::Image.open("./example.jpg")
138
+ * img.resize(width, height)
139
+ */
140
+ static VALUE rb_epeg_image_resize(VALUE self, VALUE w, VALUE h)
141
+ {
142
+ Check_Type(w, T_FIXNUM);
143
+ Check_Type(h, T_FIXNUM);
144
+
145
+ Epeg_Image *image;
146
+ Data_Get_Struct(self, Epeg_Image, image);
147
+
148
+ epeg_decode_bounds_set(image, 0, 0, NUM2UINT(w), NUM2UINT(h));
149
+ rb_iv_set(self, "epeg_trimmed", Qfalse);
150
+
151
+ return self;
152
+ }
153
+
154
+ /*
155
+ * call-seq:
156
+ * resize_to_fit(w, h)
157
+ *
158
+ * Resizes image to fit +w+ x +h+. Please not that the actual width or
159
+ * height of the image can be smaller than +w+ respectively +h+.
160
+ * See Epeg::Image.resize_to_fill
161
+ */
162
+ static VALUE rb_epeg_image_resize_to_fit(VALUE self, VALUE w, VALUE h)
163
+ {
164
+ Check_Type(w, T_FIXNUM);
165
+ Check_Type(h, T_FIXNUM);
166
+
167
+ const double image_width = (double)NUM2INT(rb_iv_get(self, "@width"));
168
+ const double image_height = (double)NUM2INT(rb_iv_get(self, "@height"));
169
+
170
+ const double fit_width = (double)NUM2INT(w);
171
+ const double fit_height = (double)NUM2INT(w);
172
+
173
+ const double width_ratio = image_width/fit_width;
174
+ const double height_ratio = image_height/fit_height;
175
+
176
+ float width, height;
177
+
178
+ if(width_ratio == height_ratio) {
179
+ width = fit_width;
180
+ height = fit_width;
181
+ } else {
182
+ if(width_ratio > height_ratio) {
183
+ width = fit_width;
184
+ height = (fit_height/width_ratio);
185
+ } else {
186
+ width = (fit_height/height_ratio);
187
+ height = fit_height;
188
+ }
189
+ }
190
+
191
+ return rb_epeg_image_resize( self, INT2NUM( ceil(width) ), INT2NUM( ceil(height) ) );
192
+ }
193
+
194
+ /*
195
+ * call-seq:
196
+ * resize_to_fill(w, h)
197
+ *
198
+ * Resizes image to fill +w+ x +h+. Please not that the actual width or
199
+ * height of the image can be larger than +w+ respectively +h+.
200
+ * See Epeg::Image.resize_to_fit
201
+ */
202
+ static VALUE rb_epeg_image_resize_to_fill(VALUE self, VALUE w, VALUE h)
203
+ {
204
+ Check_Type(w, T_FIXNUM);
205
+ Check_Type(h, T_FIXNUM);
206
+
207
+ const double image_width = (double)NUM2INT(rb_iv_get(self, "@width"));
208
+ const double image_height = (double)NUM2INT(rb_iv_get(self, "@height"));
209
+
210
+ const double fit_width = (double)NUM2INT(w);
211
+ const double fit_height = (double)NUM2INT(w);
212
+
213
+ const double width_ratio = image_width/fit_width;
214
+ const double height_ratio = image_height/fit_height;
215
+
216
+ float width, height;
217
+
218
+ if(width_ratio == height_ratio) {
219
+ width = fit_width;
220
+ height = fit_width;
221
+ } else {
222
+ if(width_ratio > height_ratio) {
223
+ width = fit_width*(width_ratio/height_ratio);
224
+ height = fit_height;
225
+ } else {
226
+ width = fit_width;
227
+ height = fit_height*(height_ratio/width_ratio);
228
+ }
229
+ }
230
+
231
+ return rb_epeg_image_resize( self, INT2NUM( ceil(width) ), INT2NUM( ceil(height) ) );
232
+ }
233
+
234
+ /*
235
+ * call-seq:
236
+ * cropp(w, h, (x, y))
237
+ *
238
+ * Crops image to +w+ x +h+. If +x+ and +y+ are not specified the image is
239
+ * cropped with the center gravity.
240
+ */
241
+ static VALUE rb_epeg_image_crop(int argc, VALUE *argv, VALUE self)
242
+ {
243
+ if(argc < 2 || argc == 3 || argc > 4) {
244
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 2 or 4)", argc);
245
+ }
246
+
247
+ unsigned int w, h, x, y, i;
248
+
249
+ if(argc == 2) {
250
+ for(i = 0; i < 2; i++) { Check_Type(argv[i], T_FIXNUM); }
251
+
252
+ w = NUM2UINT(argv[0]);
253
+ h = NUM2UINT(argv[1]);
254
+
255
+ x = (unsigned int)( ceil( (double)NUM2UINT( rb_iv_get(self, "@width") )/2 ) - (double)w/2 );
256
+ y = (unsigned int)( ceil( (double)NUM2UINT( rb_iv_get(self, "@height") )/2 ) - (double)w/2 );
257
+ }
258
+
259
+ if (argc == 4) {
260
+ for(i = 0; i < 4; i++) { Check_Type(argv[i], T_FIXNUM); }
261
+
262
+ w = NUM2UINT(argv[0]);
263
+ h = NUM2UINT(argv[1]);
264
+
265
+ x = NUM2UINT(argv[2]);
266
+ y = NUM2UINT(argv[3]);
267
+ }
268
+
269
+ Epeg_Image *image;
270
+ Data_Get_Struct(self, Epeg_Image, image);
271
+
272
+ epeg_decode_bounds_set(image, x, y, w, h);
273
+ rb_iv_set(self, "epeg_trimmed", Qtrue);
274
+
275
+ return self;
276
+ }
277
+
278
+ /*
279
+ * internal use
280
+ */
281
+ static void rb_epeg_image_encode_or_trim(VALUE obj, Epeg_Image *image) {
282
+ int status;
283
+
284
+ if(rb_iv_get(obj, "epeg_trimmed") == Qtrue) {
285
+ status = epeg_trim(image);
286
+ } else {
287
+ status = epeg_encode(image);
288
+ }
289
+
290
+ if(status != 0) { rb_raise(rb_eRuntimeError, "Error: can't encode"); }
291
+ }
292
+
293
+ /*
294
+ * call-seq:
295
+ * write(file_path)
296
+ *
297
+ * Writes the image to +file_path+ and closes the image stream.
298
+ * Please note that you can't do any further operations on the image
299
+ * after using this method.
300
+ */
301
+ static VALUE rb_epeg_image_write(VALUE self, VALUE file_path)
302
+ {
303
+ Check_Type(file_path, T_STRING);
304
+
305
+ if(rb_iv_get(self, "epeg_file_closed") != Qfalse) {
306
+ rb_raise(rb_eRuntimeError, "Error: closed file");
307
+ }
308
+
309
+ Epeg_Image *image;
310
+ Data_Get_Struct(self, Epeg_Image, image);
311
+
312
+ epeg_file_output_set(image, StringValueCStr(file_path));
313
+ rb_epeg_image_encode_or_trim(self, image);
314
+
315
+ rb_iv_set(self, "epeg_file_closed", Qtrue);
316
+ rb_epeg_image_close(self);
317
+
318
+ return Qnil;
319
+ }
320
+
321
+ /*
322
+ * call-seq:
323
+ * to_blob()
324
+ *
325
+ * Returns the image data as blob and closes the image stream.
326
+ * Please note that you can't do any further operations on the image
327
+ * after using this method.
328
+ */
329
+ static VALUE rb_epeg_image_to_blob(VALUE self)
330
+ {
331
+ if(rb_iv_get(self, "epeg_file_closed") != Qfalse) {
332
+ rb_raise(rb_eRuntimeError, "Error: closed file");
333
+ }
334
+
335
+ Epeg_Image *image;
336
+ Data_Get_Struct(self, Epeg_Image, image);
337
+
338
+ unsigned char *data;
339
+ int size;
340
+
341
+ epeg_memory_output_set(image, &data, &size);
342
+ rb_epeg_image_encode_or_trim(self, image);
343
+
344
+ VALUE blob = rb_str_new(data, size);
345
+ free(data);
346
+
347
+ rb_iv_set(self, "epeg_file_closed", Qtrue);
348
+ rb_epeg_image_close(self);
349
+
350
+ return blob;
351
+ }
352
+
353
+ /*
354
+ * call-seq:
355
+ * quality=(q)
356
+ *
357
+ * Sets the quality of the image to +q+ (+q+ >= 0 and +q+ <= 100).
358
+ * See Epeg::Image.quality=
359
+ */
360
+ static VALUE rb_epeg_image_set_quality(VALUE self, VALUE q)
361
+ {
362
+ Check_Type(q, T_FIXNUM);
363
+
364
+ int quality = NUM2INT(q);
365
+
366
+ if (quality < 0) { rb_raise(rb_eRuntimeError, "Error: quality must be >= 0"); }
367
+ if (quality > 100) { rb_raise(rb_eRuntimeError, "Error: quality must be <= 100"); }
368
+
369
+ Epeg_Image *image;
370
+ Data_Get_Struct(self, Epeg_Image, image);
371
+
372
+ epeg_quality_set(image, quality);
373
+ rb_iv_set(self, "@quality", INT2NUM(quality));
374
+
375
+ return Qnil;
376
+ }
377
+
378
+ /*
379
+ * call-seq:
380
+ * close()
381
+ *
382
+ * Closes the image stream.
383
+ * Please note that you can't do any furhter operations on the image
384
+ * after using this method. See Epeg::Image#closed?
385
+ */
386
+ static VALUE rb_epeg_image_close(VALUE self)
387
+ {
388
+ if(rb_iv_get(self, "epeg_file_closed") == Qfalse) {
389
+ Epeg_Image *image;
390
+ Data_Get_Struct(self, Epeg_Image, image);
391
+
392
+ epeg_close(image);
393
+ rb_iv_set(self, "epeg_file_closed", Qtrue);
394
+ }
395
+
396
+ return Qnil;
397
+ }
398
+
399
+ /*
400
+ * call-seq:
401
+ * closed?()
402
+ *
403
+ * Returns true if the image stream is already closed.
404
+ * See Epeg::Image#close
405
+ */
406
+ static VALUE rb_epeg_image_is_closed(VALUE self)
407
+ {
408
+ return rb_iv_get(self, "epeg_file_closed");
409
+ }
410
+
411
+ static void rb_epeg_image_destroy(void *image)
412
+ {
413
+ if(!image) { epeg_close((Epeg_Image *)image); }
414
+ }