Module: ImageRuby::PureRubyImageMethods
- Defined in:
- lib/imageruby/pureruby.rb
Instance Method Summary collapse
-
#[](x, y) ⇒ Object
Returs the color of a pixel locate in the given x and y coordinates or a rectangle section of the image if a range is specified.
-
#[]=(x, y, obj) ⇒ Object
Set the color of a pixel locate in the given x and y coordinates or replace a rectangle section of the image with other image of equal dimensions if a range is specified.
-
#color_replace(color1, color2) ⇒ Object
Duplicate the image and then call color_replace! method with the given parameters.
-
#color_replace!(color1, color2) ⇒ Object
Replace the color given in the first argument by the color given in the second with alpha values.
-
#draw(x, y, image) ⇒ Object
Duplicates the image and draw into the duplicate with the given parameters (by calling draw!).
-
#draw!(x, y, image) ⇒ Object
Draw a given image to the given x and y coordinates (matching left-upper corner of the drawn image) when drawing, the method use the alpha channel of the origin image to properly implement alpha drawing (transparency).
-
#each_pixel ⇒ Object
Walks each pixel on the image, block is mandantory to call this function and return as yield block calls x,y and pixel color for each pixel of the image.
- #inspect ⇒ Object
-
#map_pixel ⇒ Object
Creates a new image of same dimensions in which each pixel is replaced with the value returned by the block passed as parameter, the block receives the coordinates and color of each pixel.
-
#map_pixel! ⇒ Object
Replace each pixel of the image with the value returned by the block passed as parameter, the block receives the coordinates and color of each pixel.
-
#mask(color1 = nil) ⇒ Object
Replace a color with %100 transparency, useful for mask drawing.
-
#on_chain {|_self| ... } ⇒ Object
For sugar syntax, yield the image and then return self.
-
#save(path, format) ⇒ Object
Save the image to a given file by path and format.
Instance Method Details
#[](x, y) ⇒ Object
Returs the color of a pixel locate in the given x and y coordinates or a rectangle section of the image if a range is specified
Example
image[0,0] # return a Color object representing the color of the pixel at 0,0
image[0..20,10..30] # return a image object with the cropped rectagle with x between 0 and 20 and y between 10 and 30
image[0..20,20] # return a image object cropped rectagle with x between 0 and 20 and y equals 15
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/imageruby/pureruby.rb', line 55 def [] (x,y) if x.instance_of? Fixnum and y.instance_of? Fixnum get_pixel(fixx(x),fixy(y)) else x = (fixx(x)..fixx(x)) if x.instance_of? Fixnum y = (fixy(y)..fixy(y)) if y.instance_of? Fixnum if x.instance_of? Range x = (fixx(x.first)..fixx(x.last)) end if y.instance_of? Range y = (fixy(y.first)..fixy(y.last)) end newimg = Image.new(x.last + 1 - x.first, y.last + 1 - y.first) width = x.count (0..y.count).each do |y_| destpointer = y_*width*3 origpointer = ((y.first + y_)*self.width + x.first)*3 newimg.pixel_data[destpointer..destpointer+width*3] = self.pixel_data[origpointer..origpointer+width*3] end newimg end end |
#[]=(x, y, obj) ⇒ Object
Set the color of a pixel locate in the given x and y coordinates or replace a rectangle section of the image with other image of equal dimensions if a range is specified
Example
image[0,0] = Color.red # set the color of pixel at 0,0 to Color.red
image[0..20,10..30] = other_image # replace the rectagle with x between 0 and 20 and y between 10 and 30 with other_image
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/imageruby/pureruby.rb', line 95 def []= (x,y,obj) if x.instance_of? Fixnum and y.instance_of? Fixnum set_pixel(x,y,obj) else x = (x..x) if x.instance_of? Fixnum y = (y..y) if y.instance_of? Fixnum width = x.count (0..y.count-1).each do |y_| origpointer = y_*obj.width*3 destpointer = ((y.first + y_)*self.width + x.first)*3 self.pixel_data[destpointer..destpointer+obj.width*3-1] = obj.pixel_data[origpointer..origpointer+obj.width*3-1] end end end |
#color_replace(color1, color2) ⇒ Object
Duplicate the image and then call color_replace! method with the given parameters
233 234 235 236 237 238 |
# File 'lib/imageruby/pureruby.rb', line 233 def color_replace(color1, color2) newimage = self.dup newimage.color_replace!(color1,color2) newimage end |
#color_replace!(color1, color2) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/imageruby/pureruby.rb', line 246 def color_replace!( color1, color2) strcolor1 = color1.to_s strcolor2 = color2.to_s a = color2.a.chr (0..width*height).each do |i| if pixel_data[i*3..i*3+2] == strcolor1 then pixel_data[i*3..i*3+2] = strcolor2 alpha_data[i] = a end end end |
#draw(x, y, image) ⇒ Object
Duplicates the image and draw into the duplicate with the given parameters (by calling draw!)
115 116 117 118 119 |
# File 'lib/imageruby/pureruby.rb', line 115 def draw(x,y,image) obj = self.dup() obj.draw!(x,y,image) obj end |
#draw!(x, y, image) ⇒ Object
Draw a given image to the given x and y coordinates (matching left-upper corner of the drawn image) when drawing, the method use the alpha channel of the origin image to properly implement alpha drawing (transparency)
Examples:
image.draw!(0,0,other_image)
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/imageruby/pureruby.rb', line 129 def draw!(x,y,image) draw_height = image.height draw_width = image.width if y+image.height > self.height draw_height = self.height - y end if x+image.width > self.width draw_width = self.width - x end dest_pixel_data = self.pixel_data orig_pixel_data = image.pixel_data (0..draw_height-1).each do |y_orig| y_dest = y_orig + y origpointer = y_orig*image.width*3 destpointer = (y_dest*width+x)*3 (0..draw_width-1).each do |x_orig| color = orig_pixel_data[origpointer..origpointer+2] alpha = image.alpha_data[y_orig*image.width+x_orig].ord if alpha < 255 if alpha > 0 dest_pixel_data[destpointer] = (( orig_pixel_data[origpointer].ord*(alpha) + dest_pixel_data[destpointer].ord*(255-alpha) ) / 255).chr origpointer = origpointer + 1 destpointer = destpointer + 1 dest_pixel_data[destpointer] = (( orig_pixel_data[origpointer].ord*(alpha) + dest_pixel_data[destpointer].ord*(255-alpha) ) / 255).chr origpointer = origpointer + 1 destpointer = destpointer + 1 dest_pixel_data[destpointer] = (( orig_pixel_data[origpointer].ord*(alpha) + dest_pixel_data[destpointer].ord*(255-alpha) ) / 255).chr origpointer = origpointer + 1 destpointer = destpointer + 1 else destpointer = destpointer + 3 origpointer = origpointer + 3 end else dest_pixel_data[destpointer..destpointer+2] = color destpointer = destpointer + 3 origpointer = origpointer + 3 end end end end |
#each_pixel ⇒ Object
Walks each pixel on the image, block is mandantory to call this function and return as yield block calls x,y and pixel color for each pixel of the image
This function does not allow to modify any color information of the image (use map_pixel for that)
Examples:
image.each_pixel do |x,y,color|
print "pixel at (#{x},#{y}): #{color.inspect}\n"
end
198 199 200 201 202 203 204 |
# File 'lib/imageruby/pureruby.rb', line 198 def each_pixel (0..@width-1).each do |x| (0..@height-1).each do |y| yield(x,y,get_pixel(x,y)) end end end |
#inspect ⇒ Object
283 284 285 |
# File 'lib/imageruby/pureruby.rb', line 283 def inspect() "#<ImageRuby::Image:#{object_id} @width=#{@width}, @height=#{@height}>" end |
#map_pixel ⇒ Object
Creates a new image of same dimensions in which each pixel is replaced with the value returned by the block passed as parameter, the block receives the coordinates and color of each pixel
Example
image_without_red = image.map_pixel{|x,y,color| color.r = 0; color } # remove color channel of all pixels
212 213 214 215 216 |
# File 'lib/imageruby/pureruby.rb', line 212 def map_pixel Image.new(@width, @height) do |x,y| yield(x,y,get_pixel(x,y)) end end |
#map_pixel! ⇒ Object
Replace each pixel of the image with the value returned by the block passed as parameter, the block receives the coordinates and color of each pixel
Example
image.map_pixel!{|x,y,color| color.r = 0; color } # remove color channel of all pixels
224 225 226 227 228 229 230 |
# File 'lib/imageruby/pureruby.rb', line 224 def map_pixel! each_pixel do |x,y,color| set_pixel(x,y, yield(x,y,get_pixel(x,y))) end self end |
#mask(color1 = nil) ⇒ Object
Replace a color with %100 transparency, useful for mask drawing
Example
masked = other_image.mask(Color.black)
image.draw(0,0,masked) # draw the image without the black pixels
266 267 268 269 270 271 272 |
# File 'lib/imageruby/pureruby.rb', line 266 def mask(color1 = nil) color1 = color1 || Color.from_rgb(255,255,255) color2 = color1.dup color2.a = 0 color_replace(color1,color2) end |
#on_chain {|_self| ... } ⇒ Object
279 280 281 |
# File 'lib/imageruby/pureruby.rb', line 279 def on_chain yield(self); self end |
#save(path, format) ⇒ Object
Save the image to a given file by path and format
Example
image.save("output.bmp", :bmp)
33 34 35 |
# File 'lib/imageruby/pureruby.rb', line 33 def save(path, format) FilePersistor.persist(self,path,format) end |