tinyimg 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 167b894e73f7dad09bfff8a20d4374d2dcd822f2
4
- data.tar.gz: f92b7f089edb704b6d60cc0c3d58420b444b9d10
3
+ metadata.gz: 974d95b03df2d7a90c238049b4083912464b8616
4
+ data.tar.gz: ee647517f46360743859de3c5aca8b02be3ee59c
5
5
  SHA512:
6
- metadata.gz: 56d0aa8dd6ca5fab4572ee9bf04416c8d6a90b0a0c5046f23f45fd9d03008aa71acabec8f5c10a5d702e3c0190a42b758e22438189ecb934d5c8d938a1f852f1
7
- data.tar.gz: 5533db6f10236e0eff9a4edb42758a354779ccf5339b22184a24c853a02d37f512bd0d03a4af5f0d41ffa095a0e7930c3bc1f89b2b66fa0c5e6b0f0e3804a9c8
6
+ metadata.gz: fe80301f446341e671ec3d116f912fea97744817c1ddf4d9923b7c95e50c32d5df3b1e9dde81da975e318b1de49042ee516f37adce89b15eb7a89b518c9bf31e
7
+ data.tar.gz: 4fffdcd8475a7f547ec3fdd1d9856ce078cbbda8920f2ac357237e6f750ca9c6488e52056c7727eb2a5c1cc33c6b628d50806ffe5083334ad2606c9a13220e26
@@ -1,6 +1,11 @@
1
1
  #include <ruby.h>
2
2
  #include <gd.h>
3
3
 
4
+ VALUE get_error_class(VALUE self)
5
+ {
6
+ return rb_const_get_at(rb_class_of(self), rb_intern("Error"));
7
+ }
8
+
4
9
  gdImagePtr get_image_data(VALUE self)
5
10
  {
6
11
  gdImagePtr image;
@@ -52,11 +57,11 @@ VALUE load_from_string(VALUE self, VALUE input, VALUE type)
52
57
  image = gdImageCreateFromJpegPtr(RSTRING_LEN(input), RSTRING_PTR(input));
53
58
  }
54
59
  else {
55
- rb_raise(rb_eArgError, "type must be a supported image type");
60
+ rb_raise(get_error_class(self), "type must be a supported image type");
56
61
  }
57
62
 
58
63
  if (!image) {
59
- rb_raise(rb_eRuntimeError, "Error loading image data");
64
+ rb_raise(get_error_class(self), "Error loading image data");
60
65
  }
61
66
 
62
67
  set_image_data(self, image);
@@ -76,7 +81,7 @@ VALUE load_from_file(VALUE self, VALUE filename)
76
81
 
77
82
  image = gdImageCreateFromFile(StringValueCStr(filename));
78
83
  if (!image) {
79
- rb_raise(rb_eRuntimeError, "Error loading image data");
84
+ rb_raise(get_error_class(self), "Error loading image data");
80
85
  }
81
86
 
82
87
  set_image_data(self, image);
@@ -124,7 +129,7 @@ VALUE save_to_file(VALUE self, VALUE filename)
124
129
  result = gdImageFile(image, StringValueCStr(filename));
125
130
 
126
131
  if (result == GD_FALSE) {
127
- rb_raise(rb_eRuntimeError, "Unknown error occurred while trying to save the file; check it is using a known filename");
132
+ rb_raise(get_error_class(self), "Unknown error occurred while trying to save the file; check it is using a known filename");
128
133
  }
129
134
 
130
135
  return self;
@@ -149,14 +154,14 @@ VALUE to_jpeg(int argc, VALUE *argv, VALUE self)
149
154
  }
150
155
 
151
156
  if (quality < -1 || quality > 100) {
152
- rb_raise(rb_eArgError, "Quality must be between 0 and 100, or -1 for default");
157
+ rb_raise(get_error_class(self), "Quality must be between 0 and 100, or -1 for default");
153
158
  }
154
159
 
155
160
  image = get_image_data(self);
156
161
 
157
162
  image_data = (char *) gdImageJpegPtr(image, &size, quality);
158
163
  if (!image_data) {
159
- rb_raise(rb_eRuntimeError, "Unknown error occurred while trying to build a JPEG");
164
+ rb_raise(get_error_class(self), "Unknown error occurred while trying to build a JPEG");
160
165
  }
161
166
 
162
167
  VALUE output = rb_str_new(image_data, size);
@@ -184,7 +189,7 @@ VALUE to_png(int argc, VALUE *argv, VALUE self)
184
189
  compression = FIX2INT(compression_value);
185
190
 
186
191
  if (compression < 0 || compression > 9) {
187
- rb_raise(rb_eArgError, "Compression must be between 0 and 9");
192
+ rb_raise(get_error_class(self), "Compression must be between 0 and 9");
188
193
  }
189
194
 
190
195
  #ifdef HAVE_GDIMAGEPNGPTREX
@@ -195,7 +200,7 @@ VALUE to_png(int argc, VALUE *argv, VALUE self)
195
200
  }
196
201
 
197
202
  if (!image_data) {
198
- rb_raise(rb_eRuntimeError, "Unknown error occurred while trying to build a PNG");
203
+ rb_raise(get_error_class(self), "Unknown error occurred while trying to build a PNG");
199
204
  }
200
205
 
201
206
  VALUE output = rb_str_new(image_data, size);
@@ -215,7 +220,7 @@ VALUE resize_exact_bang(VALUE self, VALUE width_value, VALUE height_value)
215
220
  height = FIX2INT(height_value);
216
221
 
217
222
  if (width <= 0 || height <= 0) {
218
- rb_raise(rb_eArgError, "width and height must both be positive integers");
223
+ rb_raise(get_error_class(self), "width and height must both be positive integers");
219
224
  }
220
225
 
221
226
  image_in = get_image_data(self);
@@ -251,21 +256,21 @@ VALUE internal_crop_bang(VALUE self, VALUE x_value, VALUE y_value, VALUE width_v
251
256
  height = FIX2INT(height_value);
252
257
 
253
258
  if (x < 0 || y < 0) {
254
- rb_raise(rb_eArgError, "x, y must both be zero or positive integers");
259
+ rb_raise(get_error_class(self), "x, y must both be zero or positive integers");
255
260
  }
256
261
 
257
262
  if (width <= 0 || height <= 0) {
258
- rb_raise(rb_eArgError, "width and height must both be positive integers");
263
+ rb_raise(get_error_class(self), "width and height must both be positive integers");
259
264
  }
260
265
 
261
266
  image_in = get_image_data(self);
262
267
 
263
268
  if (x + width > gdImageSX(image_in)) {
264
- rb_raise(rb_eArgError, "x + width is greater than the original image's width");
269
+ rb_raise(get_error_class(self), "x + width is greater than the original image's width");
265
270
  }
266
271
 
267
272
  if (y + height > gdImageSY(image_in)) {
268
- rb_raise(rb_eArgError, "y + height is greater than the original image's height");
273
+ rb_raise(get_error_class(self), "y + height is greater than the original image's height");
269
274
  }
270
275
 
271
276
  image_out = gdImageCreateTrueColor(width, height);
@@ -284,6 +289,7 @@ void Init_tinyimg()
284
289
  {
285
290
  VALUE cTinyimg = rb_define_class("Tinyimg", rb_cObject);
286
291
  rb_define_class_under(cTinyimg, "Image", rb_cObject);
292
+ rb_define_class_under(cTinyimg, "Error", rb_const_get(rb_cObject, rb_intern("StandardError")));
287
293
 
288
294
  rb_define_method(cTinyimg, "resize_exact!", resize_exact_bang, 2);
289
295
  rb_define_method(cTinyimg, "to_jpeg", to_jpeg, -1);
@@ -33,7 +33,7 @@ class Tinyimg
33
33
  width, height = convert_hash_to_exact_dimensions(args.first)
34
34
  resize_exact!(width, height)
35
35
  else
36
- raise ArgumentError, "#resize and #resize! accept either (width, height) or a hash with :width and/or :height keys"
36
+ raise Error, "#resize and #resize! accept either (width, height) or a hash with :width and/or :height keys"
37
37
  end
38
38
  end
39
39
 
@@ -138,7 +138,7 @@ class Tinyimg
138
138
  elsif data[0, 8].unpack("C*") == [137, 80, 78, 71, 13, 10, 26, 10]
139
139
  :png
140
140
  else
141
- raise ArgumentError, "Only JPEG and PNG files are supported"
141
+ raise Error, "Only JPEG and PNG files are supported"
142
142
  end
143
143
  end
144
144
 
@@ -147,17 +147,17 @@ class Tinyimg
147
147
  when 'jpeg', 'jpg' then :jpeg
148
148
  when 'png' then :png
149
149
  else
150
- raise ArgumentError, "Cannot determine image type based on the filename"
150
+ raise Error, "Cannot determine image type based on the filename"
151
151
  end
152
152
  end
153
153
 
154
154
  def convert_hash_to_exact_dimensions(opts)
155
155
  if opts.empty? || !(opts.keys - [:width, :height]).empty?
156
- raise ArgumentError, "expected either :width or :height or both keys"
156
+ raise Error, "expected either :width or :height or both keys"
157
157
  end
158
158
 
159
159
  if opts.values.any? { |v| !v.is_a?(Fixnum) }
160
- raise ArgumentError, ":width and :height values must be integers"
160
+ raise Error, ":width and :height values must be integers"
161
161
  end
162
162
 
163
163
  new_width, new_height = opts[:width], opts[:height]
@@ -94,19 +94,19 @@ RSpec.describe Tinyimg do
94
94
  end
95
95
 
96
96
  it "raises if other keys are provided" do
97
- expect { sample.resize!(something: 123) }.to raise_error(ArgumentError)
97
+ expect { sample.resize!(something: 123) }.to raise_error(Tinyimg::Error)
98
98
  end
99
99
 
100
100
  it "raises if non-integer values are provided" do
101
- expect { sample.resize!(width: "123") }.to raise_error(ArgumentError)
101
+ expect { sample.resize!(width: "123") }.to raise_error(Tinyimg::Error)
102
102
  end
103
103
 
104
104
  it "raises if no keys are provided" do
105
- expect { sample.resize! }.to raise_error(ArgumentError)
105
+ expect { sample.resize! }.to raise_error(Tinyimg::Error)
106
106
  end
107
107
 
108
108
  it "raises if only one argument is provided" do
109
- expect { sample.resize!(123) }.to raise_error(ArgumentError)
109
+ expect { sample.resize!(123) }.to raise_error(Tinyimg::Error)
110
110
  end
111
111
  end
112
112
 
@@ -153,9 +153,9 @@ RSpec.describe Tinyimg do
153
153
  end
154
154
 
155
155
  it "raises if the requested area is not inside the original image" do
156
- expect { sample.crop!(x: 10, y: 20, width: 195, height: 40) }.to raise_error(ArgumentError)
157
- expect { sample.crop!(x: 10, y: 20, width: 95, height: 140) }.to raise_error(ArgumentError)
158
- expect { sample.crop!(width: 95, height: 160) }.to raise_error(ArgumentError)
156
+ expect { sample.crop!(x: 10, y: 20, width: 195, height: 40) }.to raise_error(Tinyimg::Error)
157
+ expect { sample.crop!(x: 10, y: 20, width: 95, height: 140) }.to raise_error(Tinyimg::Error)
158
+ expect { sample.crop!(width: 95, height: 160) }.to raise_error(Tinyimg::Error)
159
159
  end
160
160
  end
161
161
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = 'tinyimg'
3
- gem.version = '0.1.1'
3
+ gem.version = '0.1.2'
4
4
  gem.summary = "Tiny and fast JPEG/PNG cropping and resizing"
5
5
  gem.description = "Convert between JPEG/PNG, crop and resize images, either all in memory or via disk. Only requires libgd to function."
6
6
  gem.has_rdoc = false
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tinyimg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roger Nesbitt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-18 00:00:00.000000000 Z
11
+ date: 2015-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec