image_voodoo 0.8.4 → 0.8.5

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: 154df8437b68b194e1d1493c6bf592511702f246
4
- data.tar.gz: 1ac972b37e7af0729453effebe9afeb3cddf9be6
3
+ metadata.gz: a9c58e8e0f8b9c9388e30b313dc18e6cc3b9bea0
4
+ data.tar.gz: bcf41a5a7f38c576b3c95323ca0e486e3288223a
5
5
  SHA512:
6
- metadata.gz: f131451e413fd9cbcd1a6d326c5931865034cfc010f470a3cb83b4d0d79e9e2137d350ea2bc11a041bec27299ac5eb8c877a0bfdd503c5aa1d7d9b0c56f47879
7
- data.tar.gz: 33c22b496910d12e4d6caf5bfcbecbb4a433dc52d2ff991e14ae76f9cc9ef124489cbc61f70a30755238fda3cccd45415c1d8b9e44d6ebd26ef38ba88214b10e
6
+ metadata.gz: 0e6ea0d28e142e9092454ea8e40ab5d87c8a099924611423660e8c496802854df38c5c7b403512d20a9039a6addd14178f0076819af09b1e59cdfafd6dbabe77
7
+ data.tar.gz: dee71d6424b06ae4fd2f19557ad4374eb8edc6763c7425a47c9b347d742c8c16dd6b421309310942c40d6def9c6ab208c5099e03cd1759ce467ccec314a8e01c
data/History.txt CHANGED
@@ -19,7 +19,7 @@
19
19
 
20
20
  == 0.7
21
21
 
22
- - add alias for image science method with_image_from_memory to our existing
22
+ - add alias for image science method with_image_from_memory to our existing
23
23
  with_bytes
24
24
 
25
25
  == 0.6
data/bin/image_voodoo CHANGED
@@ -103,6 +103,10 @@ opts = OptionParser.new do |opts|
103
103
  actions << lambda {|img| img.resize(width,height) }
104
104
  end
105
105
 
106
+ opts.on("-f", "--format", "Print the image format") do
107
+ actions << lambda {|img| puts img.format; img }
108
+ end
109
+
106
110
  opts.on_tail("-h", "--help", "Show this message") do
107
111
  puts opts
108
112
  exit 0
data/lib/image_voodoo.rb CHANGED
@@ -3,8 +3,8 @@
3
3
  # = ImageVoodoo
4
4
  # == Description
5
5
  #
6
- # ImageVoodoo is an ImageScience-API-compatible image manipulation library for
7
- # JRuby.
6
+ # ImageVoodoo is an ImageScience-API-compatible image manipulation library for
7
+ # JRuby.
8
8
  #
9
9
  # == Examples
10
10
  #
@@ -27,7 +27,7 @@
27
27
  #
28
28
  class ImageVoodoo
29
29
  attr_accessor :quality
30
-
30
+
31
31
  include Java
32
32
 
33
33
  JFile = java.io.File
@@ -44,8 +44,9 @@ class ImageVoodoo
44
44
  require 'image_voodoo/awt'
45
45
  end
46
46
 
47
- def initialize(src)
47
+ def initialize(src, format=nil)
48
48
  @src = src
49
+ @format = format
49
50
  @quality = nil # nil means no specific quality ever specified
50
51
  end
51
52
 
@@ -58,10 +59,10 @@ class ImageVoodoo
58
59
  image = guard { adjust_brightness_impl(scale, offset) }
59
60
  block_given? ? yield(image) : image
60
61
  end
61
-
62
+
62
63
  ##
63
64
  #
64
- # Converts rgb hex color value to an alpha value an yields/returns the new
65
+ # Converts rgb hex color value to an alpha value an yields/returns the new
65
66
  # image.
66
67
  #
67
68
  def alpha(rgb)
@@ -70,12 +71,12 @@ class ImageVoodoo
70
71
  end
71
72
 
72
73
  ##
73
- #
74
+ #
74
75
  # Get current image bytes as a String using provided format. Format parameter
75
76
  # is the informal name of an image type - for instance,
76
77
  # "bmp" or "jpg". If the backend is AWT the types available are listed in
77
78
  # javax.imageio.ImageIO.getWriterFormatNames()
78
- #
79
+ #
79
80
  def bytes(format)
80
81
  java_bytes = guard { bytes_impl(format) }
81
82
  String.from_java_bytes java_bytes
@@ -83,8 +84,8 @@ class ImageVoodoo
83
84
 
84
85
  ##
85
86
  #
86
- # Creates a square thumbnail of the image cropping the longest edge to
87
- # match the shortest edge, resizes to size, and yields/returns the new image.
87
+ # Creates a square thumbnail of the image cropping the longest edge to
88
+ # match the shortest edge, resizes to size, and yields/returns the new image.
88
89
  #
89
90
  def cropped_thumbnail(size)
90
91
  l, t, r, b, half = 0, 0, width, height, (width - height).abs / 2
@@ -114,7 +115,7 @@ class ImageVoodoo
114
115
  end
115
116
 
116
117
  ##
117
- #
118
+ #
118
119
  # Creates a grayscale version of image and yields/returns the new image.
119
120
  #
120
121
  def greyscale
@@ -124,7 +125,7 @@ class ImageVoodoo
124
125
  alias_method :grayscale, :greyscale
125
126
 
126
127
  ##
127
- #
128
+ #
128
129
  # Creates a negative and yields/returns the new image.
129
130
  #
130
131
  def negative
@@ -149,7 +150,7 @@ class ImageVoodoo
149
150
 
150
151
  ##
151
152
  #
152
- # Resizes the image to width and height and yields/returns the new image.
153
+ # Resizes the image to width and height and yields/returns the new image.
153
154
  #
154
155
  def resize(width, height)
155
156
  target = guard { resize_impl(width, height) }
@@ -159,9 +160,9 @@ class ImageVoodoo
159
160
  end
160
161
 
161
162
  ##
162
- #
163
- # Saves the image out to path. Changing the file extension will convert
164
- # the file type to the appropriate format.
163
+ #
164
+ # Saves the image out to path. Changing the file extension will convert
165
+ # the file type to the appropriate format.
165
166
  #
166
167
  def save(file)
167
168
  format = File.extname(file)
@@ -184,8 +185,8 @@ class ImageVoodoo
184
185
 
185
186
  ##
186
187
  #
187
- # Creates a proportional thumbnail of the image scaled so its longest
188
- # edge is resized to size and yields/returns the new image.
188
+ # Creates a proportional thumbnail of the image scaled so its longest
189
+ # edge is resized to size and yields/returns the new image.
189
190
  #
190
191
  def thumbnail(size)
191
192
  target = scale(size.to_f / (width > height ? width : height))
@@ -194,8 +195,8 @@ class ImageVoodoo
194
195
 
195
196
  ##
196
197
  #
197
- # Crops an image to left, top, right, and bottom and then yields/returns the
198
- # new image.
198
+ # Crops an image to left, top, right, and bottom and then yields/returns the
199
+ # new image.
199
200
  #
200
201
  def with_crop(left, top, right, bottom)
201
202
  image = guard { with_crop_impl(left, top, right, bottom) }
@@ -203,7 +204,7 @@ class ImageVoodoo
203
204
  end
204
205
 
205
206
  ##
206
- #
207
+ #
207
208
  # A top-level image loader opens path and then yields/returns the image.
208
209
  #
209
210
  def self.with_image(path)
@@ -213,7 +214,7 @@ class ImageVoodoo
213
214
  end
214
215
 
215
216
  ##
216
- #
217
+ #
217
218
  # A top-level image loader reads bytes and then yields/returns the image.
218
219
  #
219
220
  def self.with_bytes(bytes)
@@ -229,7 +230,7 @@ class ImageVoodoo
229
230
  ##
230
231
  #
231
232
  # *_impl providers only need provide the implementation if it can
232
- # support it. Otherwise, this method will detect that the method is
233
+ # support it. Otherwise, this method will detect that the method is
233
234
  # missing.
234
235
  #
235
236
  def self.guard(&block)
@@ -245,7 +246,7 @@ class ImageVoodoo
245
246
 
246
247
  ##
247
248
  #
248
- # Returns the height of the image, in pixels.
249
+ # Returns the height of the image, in pixels.
249
250
  #
250
251
  def height
251
252
  @src.height
@@ -253,7 +254,7 @@ class ImageVoodoo
253
254
 
254
255
  ##
255
256
  #
256
- # Returns the width of the image, in pixels.
257
+ # Returns the width of the image, in pixels.
257
258
  #
258
259
  def width
259
260
  @src.width
@@ -268,4 +269,16 @@ class ImageVoodoo
268
269
  def to_java
269
270
  @src
270
271
  end
272
+
273
+ ##
274
+ #
275
+ # Returns detected image format from binary representation of input data
276
+ # as upper case string. Eg. JPEG, BMP, PNG. For GWT image representation
277
+ # compatibility method name is :format. It also accepts block and returns
278
+ # format as first block argument. When format not detected or not set it
279
+ # returns nil
280
+ #
281
+ def format
282
+ @format && block_given? ? yield(@format) : @format
283
+ end
271
284
  end
@@ -16,16 +16,16 @@ class ImageVoodoo
16
16
  java_import javax.imageio.ImageIO
17
17
  java_import javax.imageio.IIOImage
18
18
  java_import javax.imageio.ImageWriteParam
19
- java_import javax.imageio.stream.FileImageOutputStream
19
+ java_import javax.imageio.stream.FileImageOutputStream
20
20
  java_import javax.swing.JFrame
21
21
 
22
22
  # FIXME: Move and rewrite in terms of new shape
23
23
  ##
24
24
  #
25
- # *AWT* (experimental) Add a border to the image and yield/return a new
25
+ # *AWT* (experimental) Add a border to the image and yield/return a new
26
26
  # image. The following options are supported:
27
27
  # - width: How thick is the border (default: 3)
28
- # - color: Which color is the border (in rrggbb hex value)
28
+ # - color: Which color is the border (in rrggbb hex value)
29
29
  # - style: etched, raised, plain (default: plain)
30
30
  #
31
31
  def add_border(options = {})
@@ -48,7 +48,7 @@ class ImageVoodoo
48
48
  end
49
49
 
50
50
  ##
51
- #
51
+ #
52
52
  # A simple swing wrapper around an image voodoo object.
53
53
  #
54
54
  class JImagePanel < javax.swing.JPanel
@@ -71,7 +71,7 @@ class ImageVoodoo
71
71
  end
72
72
  end
73
73
 
74
- ImageVoodoo::JImagePanel.__persistent__ = true
74
+ ImageVoodoo::JImagePanel.__persistent__ = true
75
75
 
76
76
  # Internal class for closing preview window
77
77
  class WindowClosed
@@ -96,11 +96,11 @@ class ImageVoodoo
96
96
 
97
97
  ##
98
98
  # *AWT* paint/render to the source
99
- #
99
+ #
100
100
  def paint(src=dup_src)
101
101
  yield src.graphics
102
102
  src.graphics.dispose
103
- ImageVoodoo.new src
103
+ ImageVoodoo.new(src, @format)
104
104
  end
105
105
 
106
106
  ##
@@ -115,7 +115,7 @@ class ImageVoodoo
115
115
  tracker = java.awt.MediaTracker.new(java.awt.Label.new(""))
116
116
  tracker.addImage(image, 0);
117
117
  tracker.waitForID(0)
118
- target = paint(BufferedImage.new(image.width, image.height, RGB)) do |g|
118
+ target = paint(BufferedImage.new(image.width, image.height, RGB)) do |g|
119
119
  g.draw_image image, 0, 0, nil
120
120
  end
121
121
  block_given? ? yield(target) : target
@@ -140,12 +140,21 @@ class ImageVoodoo
140
140
  SCALE_SMOOTH = java.awt.Image::SCALE_SMOOTH
141
141
 
142
142
  def self.with_image_impl(file)
143
+ format = detect_format_from_input(file)
143
144
  buffered_image = ImageIO.read(file)
144
- buffered_image ? ImageVoodoo.new(buffered_image) : nil
145
+ buffered_image ? ImageVoodoo.new(buffered_image, format) : nil
146
+ end
147
+
148
+ def self.detect_format_from_input(input)
149
+ stream = ImageIO.createImageInputStream(input)
150
+ readers = ImageIO.getImageReaders(stream)
151
+ readers.has_next ? readers.next.format_name.upcase : nil
145
152
  end
146
153
 
147
154
  def self.with_bytes_impl(bytes)
148
- ImageVoodoo.new ImageIO.read(ByteArrayInputStream.new(bytes))
155
+ input_stream = ByteArrayInputStream.new(bytes)
156
+ format = detect_format_from_input(input_stream)
157
+ ImageVoodoo.new(ImageIO.read(input_stream), format)
149
158
  end
150
159
 
151
160
  #
@@ -158,7 +167,7 @@ class ImageVoodoo
158
167
  java.awt.Color.new(rgb[0,2].to_i(16), rgb[2,2].to_i(16), rgb[4,2].to_i(16))
159
168
  end
160
169
 
161
- #
170
+ #
162
171
  # Determines the best colorspace for a new image based on whether the
163
172
  # existing image contains an alpha channel or not.
164
173
  #
@@ -166,7 +175,7 @@ class ImageVoodoo
166
175
  @src.color_model.has_alpha ? ARGB : RGB
167
176
  end
168
177
 
169
- #
178
+ #
170
179
  # Make a duplicate of the underlying Java src image
171
180
  #
172
181
  def dup_src
@@ -238,7 +247,7 @@ class ImageVoodoo
238
247
  end
239
248
 
240
249
  def with_crop_impl(left, top, right, bottom)
241
- ImageVoodoo.new @src.get_subimage(left, top, right-left, bottom-top)
250
+ ImageVoodoo.new(@src.get_subimage(left, top, right-left, bottom-top), @format)
242
251
  end
243
252
 
244
253
  def write_new_image(format, stream)
@@ -6,7 +6,7 @@ class ImageVoodoo
6
6
  def square(x, y, dim, rgb, fill=true)
7
7
  square_rounded(x, y, dim, rgb, 0, fill)
8
8
  end
9
-
9
+
10
10
  ##
11
11
  # *AWT* Draw a rectangle
12
12
  #
@@ -35,7 +35,7 @@ class ImageVoodoo
35
35
  end
36
36
 
37
37
  def as_color(color)
38
- paint do |g|
38
+ paint do |g|
39
39
  old_color = g.color
40
40
  g.color = color
41
41
  yield g
@@ -21,7 +21,7 @@ class ImageVoodoo
21
21
  #++
22
22
 
23
23
  private
24
-
24
+
25
25
  def flip_horizontally_impl
26
26
  transform(ImagesServiceFactory.make_horizontal_flip)
27
27
  end
@@ -40,14 +40,15 @@ class ImageVoodoo
40
40
  end
41
41
 
42
42
  def self.with_bytes_impl(bytes)
43
- ImageVoodoo.new ImageServicesFactory.make_image(bytes)
43
+ image = ImageServicesFactory.make_image(bytes)
44
+ ImageVoodoo.new image, image.format.to_s.upcase
44
45
  end
45
46
 
46
47
  def from_java_bytes
47
48
  String.from_java_bytes @src.image_data
48
49
  end
49
50
 
50
- #
51
+ #
51
52
  # Make a duplicate of the underlying src image
52
53
  #
53
54
  def dup_src
@@ -1,3 +1,3 @@
1
1
  class ImageVoodoo
2
- VERSION = "0.8.4"
2
+ VERSION = "0.8.5"
3
3
  end
@@ -130,4 +130,23 @@ class TestImageScience < Test::Unit::TestCase
130
130
 
131
131
  deny File.exists?(@tmppath)
132
132
  end
133
+
134
+ def test_image_format_retrieval
135
+ ImageScience.with_image @path do |img|
136
+ assert_equal 'PNG', img.format
137
+ end
138
+ end
139
+
140
+ def test_image_format_retrieval_from_bytes
141
+ ImageScience.with_image @path do |img|
142
+ bytes_string = img.bytes('JPEG')
143
+ image = ImageScience.with_bytes(bytes_string)
144
+ assert_equal 'JPEG', image.format
145
+ end
146
+ end
147
+
148
+ def test_image_format_retrieval_fail_when_invalid_bytes
149
+ image = ImageScience.with_bytes("some invalid image bytes")
150
+ assert_equal nil, image.format
151
+ end
133
152
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: image_voodoo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.4
4
+ version: 0.8.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas E. Enebo, Charles Nutter, Nick Sieger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-16 00:00:00.000000000 Z
11
+ date: 2014-08-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Image manipulation in JRuby with ImageScience compatible API
14
14
  email: tom.enebo@gmail.com