image_voodoo 0.4 → 0.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.
File without changes
@@ -30,11 +30,11 @@ class ImageVoodoo
30
30
  import java.awt.RenderingHints
31
31
  import java.awt.color.ColorSpace
32
32
  import java.awt.geom.AffineTransform
33
+ import java.awt.image.BufferedImage
33
34
  import java.awt.image.ByteLookupTable
34
35
  import java.awt.image.ColorConvertOp
35
36
  import java.awt.image.LookupOp
36
37
  import java.awt.image.RescaleOp
37
- import java.awt.image.BufferedImage
38
38
  JFile = java.io.File
39
39
  import java.io.ByteArrayInputStream
40
40
  import java.io.ByteArrayOutputStream
@@ -43,6 +43,9 @@ class ImageVoodoo
43
43
 
44
44
  NEGATIVE_OP = LookupOp.new(ByteLookupTable.new(0, (0...254).to_a.reverse.to_java(:byte)), nil)
45
45
  GREY_OP = ColorConvertOp.new(ColorSpace.getInstance(ColorSpace::CS_GRAY), nil)
46
+ ARGB = BufferedImage::TYPE_INT_ARGB
47
+ RGB = BufferedImage::TYPE_INT_RGB
48
+ SCALE_SMOOTH = java.awt.Image::SCALE_SMOOTH
46
49
 
47
50
  def initialize(src)
48
51
  @src = src
@@ -61,18 +64,16 @@ class ImageVoodoo
61
64
  style = options[:style]
62
65
  style = nil if style.to_sym == :plain
63
66
  new_width, new_height = width + 2*border_width, height + 2*border_width
64
- target = BufferedImage.new(new_width, new_height, color_type)
65
- graphics = target.graphics
66
- graphics.color = color
67
- if style
68
- raised = style.to_sym == :raised ? true : false
69
- graphics.fill3DRect(0, 0, new_width, new_height, raised)
70
- else
71
- graphics.fill_rect(0, 0, new_width, new_height)
67
+ target = paint(BufferedImage.new(new_width, new_height, color_type)) do |g|
68
+ g.color = color
69
+ if style
70
+ raised = style.to_sym == :raised ? true : false
71
+ g.fill3DRect(0, 0, new_width, new_height, raised)
72
+ else
73
+ g.fill_rect(0, 0, new_width, new_height)
74
+ end
75
+ g.draw_image(@src, nil, border_width, border_width)
72
76
  end
73
- graphics.draw_image(@src, nil, border_width, border_width)
74
- graphics.dispose
75
- target = ImageVoodoo.new target
76
77
  block_given? ? yield(target) : target
77
78
  end
78
79
 
@@ -81,7 +82,7 @@ class ImageVoodoo
81
82
  # new_pixel = pixel * scale + offset
82
83
  #
83
84
  def adjust_brightness(scale, offset)
84
- image = ImageVoodoo.new internal_transform(RescaleOp.new(scale, offset, nil))
85
+ image = internal_transform(RescaleOp.new(scale, offset, nil))
85
86
  block_given? ? yield(image) : image
86
87
  end
87
88
 
@@ -91,17 +92,15 @@ class ImageVoodoo
91
92
  #
92
93
  def alpha(rgb)
93
94
  color = hex_to_color(rgb)
94
- target = BufferedImage.new(width, height, BufferedImage::TYPE_INT_ARGB)
95
- graphics = target.graphics
96
- graphics.set_composite(java.awt.AlphaComposite::Src)
97
- graphics.draw_image(@src, nil, 0, 0)
98
- graphics.dispose
99
- 0.upto(height-1) do |i|
100
- 0.upto(width-1) do |j|
101
- target.setRGB(j, i, 0x8F1C1C) if target.getRGB(j, i) == color.getRGB
95
+ target = paint(BufferedImage.new(width, height, ARGB)) do |g|
96
+ g.set_composite(java.awt.AlphaComposite::Src)
97
+ g.draw_image(@src, nil, 0, 0)
98
+ 0.upto(height-1) do |i|
99
+ 0.upto(width-1) do |j|
100
+ target.setRGB(j, i, 0x8F1C1C) if target.getRGB(j, i) == color.getRGB
101
+ end
102
102
  end
103
103
  end
104
- target = ImageVoodoo.new target
105
104
  block_given? ? yield(target) : target
106
105
  end
107
106
 
@@ -131,11 +130,9 @@ class ImageVoodoo
131
130
  # Flips the image horizontally and yields/returns the new image.
132
131
  #
133
132
  def flip_horizontally
134
- target = BufferedImage.new(width, height, color_type)
135
- graphics = target.graphics
136
- graphics.draw_image(@src, 0, 0, width, height, width, 0, 0, height, nil)
137
- graphics.dispose
138
- target = ImageVoodoo.new target
133
+ target = paint do |g|
134
+ g.draw_image @src, 0, 0, width, height, width, 0, 0, height, nil
135
+ end
139
136
  block_given? ? yield(target) : target
140
137
  end
141
138
 
@@ -143,11 +140,9 @@ class ImageVoodoo
143
140
  # Flips the image vertically and yields/returns the new image.
144
141
  #
145
142
  def flip_vertically
146
- target = BufferedImage.new(width, height, color_type)
147
- graphics = target.graphics
148
- graphics.draw_image(@src, 0, 0, width, height, 0, height, width, 0, nil)
149
- graphics.dispose
150
- target = ImageVoodoo.new target
143
+ target = paint do |g|
144
+ g.draw_image @src, 0, 0, width, height, 0, height, width, 0, nil
145
+ end
151
146
  block_given? ? yield(target) : target
152
147
  end
153
148
 
@@ -155,8 +150,8 @@ class ImageVoodoo
155
150
  # Creates a grayscale version of image and yields/returns the new image.
156
151
  #
157
152
  def greyscale
158
- image = ImageVoodoo.new internal_transform(GREY_OP)
159
- block_given? ? yield(image) : image
153
+ target = internal_transform(GREY_OP)
154
+ block_given? ? yield(target) : target
160
155
  end
161
156
  alias_method :grayscale, :greyscale
162
157
 
@@ -164,8 +159,8 @@ class ImageVoodoo
164
159
  # Creates a negative and yields/returns the new image.
165
160
  #
166
161
  def negative
167
- image = ImageVoodoo.new internal_transform(NEGATIVE_OP)
168
- block_given? ? yield(image) : image
162
+ target = internal_transform(NEGATIVE_OP)
163
+ block_given? ? yield(target) : target
169
164
  end
170
165
 
171
166
  #
@@ -173,17 +168,10 @@ class ImageVoodoo
173
168
  # yields/returns the new image.
174
169
  #
175
170
  def resize(width, height)
176
- target = BufferedImage.new(width, height, color_type)
177
- graphics = target.graphics
178
- graphics.set_rendering_hint(RenderingHints::KEY_INTERPOLATION,
179
- RenderingHints::VALUE_INTERPOLATION_BICUBIC)
180
- w_scale = width.to_f / @src.width
181
- h_scale = height.to_f / @src.height
182
- transform = AffineTransform.get_scale_instance w_scale, h_scale
183
- graphics.draw_rendered_image @src, transform
184
- graphics.dispose
185
-
186
- target = ImageVoodoo.new target
171
+ target = paint(BufferedImage.new(width, height, color_type)) do |g|
172
+ scaled_image = @src.get_scaled_instance width, height, SCALE_SMOOTH
173
+ g.draw_image scaled_image, 0, 0, nil
174
+ end
187
175
  block_given? ? yield(target) : target
188
176
  rescue NativeException => ne
189
177
  raise ArgumentError, ne.message
@@ -207,7 +195,6 @@ class ImageVoodoo
207
195
  #
208
196
  def scale(ratio)
209
197
  new_width, new_height = (width * ratio).to_i, (height * ratio).to_i
210
-
211
198
  target = resize(new_width, new_height)
212
199
  block_given? ? yield(target) : target
213
200
  end
@@ -249,7 +236,7 @@ class ImageVoodoo
249
236
  end
250
237
 
251
238
  def paintComponent(graphics)
252
- graphics.drawImage(@image.to_java, @x, @y, nil)
239
+ graphics.draw_image(@image.to_java, @x, @y, nil)
253
240
  end
254
241
  end
255
242
 
@@ -284,11 +271,9 @@ class ImageVoodoo
284
271
  tracker = java.awt.MediaTracker.new(java.awt.Label.new(""))
285
272
  tracker.addImage(image, 0);
286
273
  tracker.waitForID(0)
287
- target = BufferedImage.new(image.getWidth, image.getHeight, BufferedImage::TYPE_INT_RGB)
288
- graphics = target.graphics
289
- graphics.drawImage(image, 0, 0, nil)
290
- graphics.dispose
291
- target = ImageVoodoo.new target
274
+ target = paint(BufferedImage.new(image.width, image.height, RGB)) do |g|
275
+ g.draw_image image, 0, 0, nil
276
+ end
292
277
  block_given? ? yield(target) : target
293
278
  rescue java.io.IOException, java.net.MalformedURLException
294
279
  raise ArgumentError.new "Trouble retrieving image: #{$!.message}"
@@ -351,18 +336,32 @@ class ImageVoodoo
351
336
  # existing image contains an alpha channel or not.
352
337
  #
353
338
  def color_type
354
- return BufferedImage::TYPE_INT_ARGB if @src.color_model.has_alpha
355
- BufferedImage::TYPE_INT_RGB
339
+ @src.color_model.has_alpha ? ARGB : RGB
340
+ end
341
+
342
+ #
343
+ # DRY up drawing setup+teardown
344
+ #
345
+ def paint(src=dup_src)
346
+ yield src.graphics
347
+ src.graphics.dispose
348
+ ImageVoodoo.new src
349
+ end
350
+
351
+ #
352
+ # Make a duplicate of the underlying Java src image
353
+ #
354
+ def dup_src
355
+ BufferedImage.new width, height, color_type
356
356
  end
357
357
 
358
358
  #
359
359
  # Do simple AWT operation transformation to target.
360
360
  #
361
- def internal_transform(operation, target=BufferedImage.new(width, height, color_type))
362
- graphics = target.graphics
363
- graphics.drawImage(@src, 0, 0, nil)
364
- graphics.drawImage(operation.filter(target, nil), 0, 0, nil)
365
- graphics.dispose
366
- target
361
+ def internal_transform(operation, target=dup_src)
362
+ paint(target) do |g|
363
+ g.draw_image(@src, 0, 0, nil)
364
+ g.draw_image(operation.filter(target, nil), 0, 0, nil)
365
+ end
367
366
  end
368
367
  end
@@ -1,3 +1,3 @@
1
1
  class ImageVoodoo
2
- VERSION = "0.4"
2
+ VERSION = "0.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: image_voodoo
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.4"
4
+ version: "0.5"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Enebo, Charles Nutter and JRuby contributors
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-28 00:00:00 +09:00
12
+ date: 2009-04-13 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  requirements: []
65
65
 
66
66
  rubyforge_project: jruby-extras
67
- rubygems_version: 1.0.1
67
+ rubygems_version: 1.3.1
68
68
  signing_key:
69
69
  specification_version: 2
70
70
  summary: Image manipulation in JRuby with ImageScience compatible API