sobakasu-image_science 1.1.5 → 1.1.6

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.
data/bin/image_science CHANGED
@@ -48,6 +48,12 @@
48
48
  # * flip_vertical
49
49
  # * flip_horizontal
50
50
  # * rotate <angle> [<x_shift> <y_shift> <x_origin> <y_origin> <use_mask>]
51
+ # * dpi
52
+ # * dpi_x
53
+ # * dpi_y
54
+ # * dpm_x
55
+ # * dpm_y
56
+ # * resize_with_dpm <width> <height> <dpm_x> <dpm_y>
51
57
  #
52
58
  # Refer to the ImageScience documentation for allowed value ranges.
53
59
  #
@@ -68,8 +74,9 @@
68
74
  # # display effects of gamma adjustments on pixel color (writes to 'output'):
69
75
  # > image_science get_pixel_color 0 0 pix.jpg | image_science adjust_gamma 0.5 | image_science get_pixel_color 0 0
70
76
 
77
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
71
78
  require 'rubygems'
72
- require File.dirname(__FILE__) + '/../lib/image_science'
79
+ require 'image_science'
73
80
  require 'getoptlong'
74
81
 
75
82
  ARG_SPEC = [ [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
@@ -210,6 +217,11 @@ EOM
210
217
  when 'crop'
211
218
  l, r, t, b = expect_params(%W{<left> <top> <right> <bottom>})
212
219
  process_data(opts) { |i| i.crop(l, r, t, b); @changed = true }
220
+ when 'dpi_x', 'dpi_y', 'dpm_x', 'dpm_y', 'dpi'
221
+ process_data(opts) { |i| warn i.send(@command) }
222
+ when 'resize_with_dpm'
223
+ w, h, x, y = expect_params(%W{<width> <height> <dpm_x> <dpm_y>})
224
+ process_data(opts) { |i| i.resize_with_dpm(w, h, x, y); @changed = true }
213
225
  when 'rotate'
214
226
  a, *args = expect_params("<angle>", %W{<x_shift> <y_shift> <x_origin>
215
227
  <y_origin> <use_mask>})
@@ -719,6 +719,71 @@ static VALUE rotate(int argc, VALUE *argv, VALUE self) {
719
719
  }
720
720
  }
721
721
 
722
+ // dpi methods from tcaddy
723
+
724
+ /*
725
+ * Returns to dots-per-meter of x-axis (width) of image
726
+ */
727
+ static VALUE dpm_x(VALUE self) {
728
+ FIBITMAP *bitmap;
729
+ GET_BITMAP(bitmap);
730
+
731
+ int dpm_x = FreeImage_GetDotsPerMeterX(bitmap);
732
+ return INT2FIX(FreeImage_GetDotsPerMeterX(bitmap));
733
+ }
734
+
735
+ /*
736
+ * Returns to dots-per-meter of y-axis (height) of image
737
+ */
738
+ static VALUE dpm_y(VALUE self) {
739
+ FIBITMAP *bitmap;
740
+ GET_BITMAP(bitmap);
741
+
742
+ return INT2FIX(FreeImage_GetDotsPerMeterY(bitmap));
743
+ }
744
+
745
+ /*
746
+ * call-seq:
747
+ * resize_with_dpm(width, height, dpm_x, dpm_y)
748
+ *
749
+ * Resizes the image to +width+ and +height+ using a cubic-bspline
750
+ * filter, and converts to Dots-per-Meter.
751
+ * If a block is given, yields the new image, else returns
752
+ * true on success.
753
+ */
754
+ static VALUE resize_with_dpm(VALUE self, VALUE wv, VALUE hv, VALUE dpm_xv, VALUE dpm_yv) {
755
+ int w, h, dpm_x, dpm_y;
756
+ FIBITMAP *bitmap, *image;
757
+ FREE_IMAGE_FORMAT fif;
758
+
759
+ w = FIX2INT(wv);
760
+ h = FIX2INT(hv);
761
+ dpm_x = FIX2INT(dpm_xv);
762
+ dpm_y = FIX2INT(dpm_yv);
763
+
764
+ if (w <= 0) rb_raise(rb_eArgError, "Width <= 0");
765
+ if (h <= 0) rb_raise(rb_eArgError, "Height <= 0");
766
+ if (dpm_x <= 0) rb_raise(rb_eArgError, "Dots-per-Meter X-Axis <= 0");
767
+ if (dpm_y <= 0) rb_raise(rb_eArgError, "Dots-per-Meter Y-Axis <= 0");
768
+
769
+ GET_BITMAP(bitmap);
770
+ fif = get_fif(self);
771
+ image = FreeImage_Rescale(bitmap, w, h, FILTER_CATMULLROM);
772
+ FreeImage_SetDotsPerMeterX(image, dpm_x);
773
+ FreeImage_SetDotsPerMeterY(image, dpm_y);
774
+ if (image) {
775
+ copy_icc_profile(bitmap, image, fif);
776
+
777
+ if(rb_block_given_p()) {
778
+ return wrap_and_yield(image, self, 0);
779
+ } else {
780
+ FreeImage_Unload(bitmap);
781
+ DATA_PTR(self) = image;
782
+ }
783
+ }
784
+ return image ? Qtrue : Qfalse;
785
+ }
786
+
722
787
  /*
723
788
  * call-seq:
724
789
  * ImageScience.new(filename, flags = 0)
@@ -778,6 +843,9 @@ void Init_image_science_ext(void)
778
843
  rb_define_method(isc, "flip_horizontal", flip_horizontal, 0);
779
844
  rb_define_method(isc, "flip_vertical", flip_vertical, 0);
780
845
  rb_define_method(isc, "rotate", rotate, -1);
846
+ rb_define_method(isc, "dpm_x", dpm_x, 0);
847
+ rb_define_method(isc, "dpm_y", dpm_x, 0);
848
+ rb_define_method(isc, "resize_with_dpm", resize_with_dpm, 4);
781
849
 
782
850
  /* FREE_IMAGE_TYPE constants */
783
851
  isc_image_types = rb_define_module_under(isc, "ImageTypes");
data/lib/image_science.rb CHANGED
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../ext/image_science_ext'
2
2
 
3
3
  class ImageScience
4
4
 
5
- VERSION = "1.1.5"
5
+ VERSION = "1.1.6"
6
6
 
7
7
  ##
8
8
  # Returns the type of the image as a string.
@@ -145,6 +145,37 @@ class ImageScience
145
145
  set_pixel_color(x, y, *args)
146
146
  end
147
147
 
148
+ # methods from tcaddy (https://github.com/tcaddy/image_science) :nodoc:
149
+
150
+ # Convert a number from dots-per-meter to dots-per-inch
151
+ def self.dpm_to_dpi(dpm)
152
+ (dpm.to_f * (1.to_f/100.to_f) * ((2.54).to_f/1.to_f)).to_f
153
+ end
154
+
155
+ ##
156
+ # Convert a number from dots-per-inch to dots-per-meter
157
+ def self.dpi_to_dpm(dpi)
158
+ (dpi.to_f * (1.to_f/(2.54).to_f) * (100.to_f/1.to_f)).to_f
159
+ end
160
+
161
+ ##
162
+ # Returns the DPI of x-axis (width) of the image, in dots-per-inch as float
163
+ def dpi_x
164
+ self.class.dpm_to_dpi(dpm_x)
165
+ end
166
+
167
+ ##
168
+ # Returns the DPI of y-axis (height) of the image, in dots-per-inch as float
169
+ def dpi_y
170
+ self.class.dpm_to_dpi(dpm_y)
171
+ end
172
+
173
+ ##
174
+ # Returns the DPI of image, as hash of float values
175
+ def dpi
176
+ {:x=>dpi_x,:y=>dpi_y}
177
+ end
178
+
148
179
  private
149
180
 
150
181
  def self.fif_to_string(fif)
@@ -245,7 +245,7 @@ describe ImageScience do
245
245
 
246
246
  describe "get_pixel_color" do
247
247
  expected = {
248
- :jpg => [[62, 134, 122, 0], [0, 14, 7, 0]],
248
+ :jpg => [[62, 134, 122, 0], [0, 11, 6, 0]],
249
249
  :png => [[62, 134, 121, 0], [1, 2, 2, 0]],
250
250
  :gif => [[59, 135, 119, 0], [0, 2, 0, 0]],
251
251
  :bmp => [[62, 134, 121, 0], [1, 2, 2, 0]],
@@ -512,6 +512,34 @@ describe ImageScience do
512
512
  end
513
513
  end
514
514
 
515
+ # dpi methods (from tcaddy)
516
+
517
+ describe "dpm_x" do
518
+ it "should return the dpm on the x axis" do
519
+ ImageScience.with_image image_path(ext) do |img|
520
+ img.dpi_x.should == 72.009
521
+ end
522
+ end
523
+ end
524
+
525
+ describe "dpm_y" do
526
+ it "should return the dpm on the y axis" do
527
+ ImageScience.with_image image_path(ext) do |img|
528
+ img.dpi_y.should == 72.009
529
+ end
530
+ end
531
+ end
532
+
533
+ describe "resize_with_dpm" do
534
+ it "should resize the image to the specified dpm" do
535
+ ImageScience.with_image image_path(ext) do |img|
536
+ img.resize_with_dpm(30, 30, 100, 100).should be_true
537
+ img.dpm_x.should == 100
538
+ img.dpm_y.should == 100
539
+ end
540
+ end
541
+ end
542
+
515
543
  end
516
544
  end
517
545
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 1
8
- - 5
9
- version: 1.1.5
8
+ - 6
9
+ version: 1.1.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ryan Davis
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-16 00:00:00 +10:30
18
+ date: 2011-01-10 00:00:00 +10:30
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency