Class: Ray::Image
- Inherits:
-
Object
- Object
- Ray::Image
- Extended by:
- ResourceSet
- Includes:
- Enumerable, PP
- Defined in:
- ext/image.c,
lib/ray/image.rb,
lib/ray/turtle.rb,
ext/image.c
Overview
An image is a bidimensonial array of pixels used to texture rendering. It is not an object you can draw per se. See Sprite or more generically Drawable for this.
You can directly access pixels of an image, but an image target (ImageTarget) must be used to be able to draw more complex objects on it.
Iterating over an image collapse
-
#each {|pixel| ... } ⇒ Object
Iterates over all the pixels of an image.
-
#each_with_pos {|pixel, x, y| ... } ⇒ Object
Yields all the pixels and their positions.
-
#map {|pixel| ... } ⇒ Ray::Image
Creates a new image using a block.
-
#map! {|pixel| ... } ⇒ Object
Modifies each pixel of the image in-place.
-
#map_with_pos {|pixel, x, y| ... } ⇒ Ray::Image
Creates a new image using a block, passing the position of each point to it.
-
#map_with_pos! {|pixel, x, y| ... } ⇒ Object
Modifies each pixel of the image in-place, passing their position to the block.
Instance Method Summary collapse
-
#[](x, y) ⇒ Color
Reads the color of a pixel.
-
#[]=(x, y, color) ⇒ Object
Changes the color of a pixel.
-
#bind ⇒ Object
Binds the texture, which will be used as GL_TEXTURE_2D when drawing with OpenGL.
-
#bind_to(unit) ⇒ Object
Binds the texture to a given unit (0 <= unit < 32).
-
#height ⇒ Integer
(also: #h)
Height of the image in pixels.
- #initialize(arg) ⇒ Object constructor
- #initialize_copy(other) ⇒ Object
- #inspect ⇒ Object
- #pretty_print(q) ⇒ Object
-
#size ⇒ Ray::Vector2
Size of the image in pixels.
-
#smooth=(val) ⇒ Object
Enables or disables smoothing.
- #smooth? ⇒ Boolean
-
#tex_rect(rect) ⇒ Rect
Converts a rect of pixel coordinates to a rect of texture coordinates.
-
#texture ⇒ Integer
Identifer of the image’s texture.
-
#turtle { ... } ⇒ Ray::Turtle
Creates a turtle operating on the receiver.
-
#width ⇒ Integer
(also: #w)
Width of the image in pixels.
-
#write(filename) ⇒ Object
Saves the image in any format supported by Ray.
-
#write_bmp(filename) ⇒ Object
Saves the image as a BMP.
-
#write_png(filename) ⇒ Object
Saves the image as a PNG.
-
#write_tga(filename) ⇒ Object
Saves the image as a TGA.
Methods included from ResourceSet
add_set, clear, missing_pattern, reject!, select!, set_hash
Methods included from PP
Constructor Details
#initialize(io) ⇒ Object #initialize(filename) ⇒ Object #initialize(size) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'ext/image.c', line 42
static
VALUE ray_image_init(VALUE self, VALUE arg) {
say_image *img = ray_rb2image(self);
if (rb_respond_to(arg, RAY_METH("read"))) {
arg = rb_funcall(arg, RAY_METH("read"), 0);
if (!say_image_load_from_memory(img, RSTRING_LEN(arg),
StringValuePtr(arg))) {
rb_raise(rb_eRuntimeError, "%s", say_error_get_last());
}
}
else if (rb_respond_to(arg, RAY_METH("to_str"))) {
if (!say_image_load_file(img, StringValuePtr(arg))) {
rb_raise(rb_eRuntimeError, "%s", say_error_get_last());
}
}
else {
say_vector2 size = ray_convert_to_vector2(arg);
if (!say_image_create_with_size(img, size.x, size.y))
rb_raise(rb_eRuntimeError, "%s", say_error_get_last());
}
return self;
}
|
Instance Method Details
#[](x, y) ⇒ Color
178 179 180 181 182 183 184 185 186 187 188 |
# File 'ext/image.c', line 178
static
VALUE ray_image_get(VALUE self, VALUE rb_x, VALUE rb_y) {
say_image *img = ray_rb2image(self);
size_t x = NUM2ULONG(rb_x), y = NUM2ULONG(rb_y);
if (x >= say_image_get_width(img) ||
y >= say_image_get_height(img))
return Qnil;
return ray_col2rb(say_image_get(img, x, y));
}
|
#[]=(x, y, color) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'ext/image.c', line 199
static
VALUE ray_image_set(VALUE self, VALUE rb_x, VALUE rb_y, VALUE color) {
rb_check_frozen(self);
say_image *img = ray_rb2image(self);
size_t x = NUM2ULONG(rb_x), y = NUM2ULONG(rb_y);
ray_image_assert_pos(img, x, y);
say_image_set(img, x, y, ray_rb2col(color));
return color;
}
|
#bind ⇒ Object
Binds the texture, which will be used as GL_TEXTURE_2D when drawing with OpenGL
This is equivalent to calling #bind_to with unit set to 0.
236 237 238 239 |
# File 'ext/image.c', line 236 VALUE ray_image_bind(VALUE self) { say_image_bind(ray_rb2image(self)); return self; } |
#bind_to(unit) ⇒ Object
246 247 248 249 250 251 252 253 254 |
# File 'ext/image.c', line 246
VALUE ray_image_bind_to(VALUE self, VALUE unit) {
int c_unit = NUM2INT(unit);
if (c_unit >= 32 || c_unit < 0)
rb_raise(rb_eRangeError, "texture unit %d out of bounds", c_unit);
say_image_bind_to(ray_rb2image(self), c_unit);
return self;
}
|
#each {|pixel| ... } ⇒ Object
Iterates over all the pixels of an image
Iteration is done over each pixel from a line from left to right, and each line from top to bottom.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ray/image.rb', line 18 def each return Enumerator.new(self, :each) unless block_given? (0...h).each do |y| (0...w).each do |x| yield self[x, y] end end self end |
#each_with_pos {|pixel, x, y| ... } ⇒ Object
Yields all the pixels and their positions
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ray/image.rb', line 36 def each_with_pos return Enumerator.new(self, :each_with_pos) unless block_given? (0...h).each do |y| (0...w).each do |x| yield self[x, y], x, y end end self end |
#height ⇒ Integer Also known as: h
Returns Height of the image in pixels.
146 147 148 149 |
# File 'ext/image.c', line 146 static VALUE ray_image_height(VALUE self) { return INT2FIX(say_image_get_height(ray_rb2image(self))); } |
#initialize_copy(other) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'ext/image.c', line 67
static
VALUE ray_image_init_copy(VALUE self, VALUE other) {
say_image *orig = ray_rb2image(other);
say_image_load_flipped_raw(ray_rb2image(self),
say_image_get_width(orig),
say_image_get_height(orig),
say_image_get_buffer(orig));
return self;
}
|
#inspect ⇒ Object
109 110 111 |
# File 'lib/ray/image.rb', line 109 def inspect "#<#{self.class} size=#{size}>" end |
#map {|pixel| ... } ⇒ Ray::Image
Creates a new image using a block
92 93 94 |
# File 'lib/ray/image.rb', line 92 def map(&block) dup.map!(&block) end |
#map! {|pixel| ... } ⇒ Object
Modifies each pixel of the image in-place
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ray/image.rb', line 54 def map! return Enumerator.new(self, :map!) unless block_given? (0...h).each do |y| (0...w).each do |x| self[x, y] = yield self[x, y] end end self end |
#map_with_pos {|pixel, x, y| ... } ⇒ Ray::Image
Creates a new image using a block, passing the position of each point to it
103 104 105 |
# File 'lib/ray/image.rb', line 103 def map_with_pos(&block) dup.map_with_pos!(&block) end |
#map_with_pos! {|pixel, x, y| ... } ⇒ Object
Modifies each pixel of the image in-place, passing their position to the block
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ray/image.rb', line 73 def map_with_pos! return Enumerator.new(self, :map_with_pos!) unless block_given? (0...h).each do |y| (0...w).each do |x| self[x, y] = yield self[x, y], x, y end end self end |
#pretty_print(q) ⇒ Object
113 114 115 |
# File 'lib/ray/image.rb', line 113 def pretty_print(q) pretty_print_attributes q, ["size", "smooth?"] end |
#size ⇒ Ray::Vector2
Returns Size of the image in pixels.
152 153 154 155 |
# File 'ext/image.c', line 152 static VALUE ray_image_size(VALUE self) { return ray_vector2_to_rb(say_image_get_size(ray_rb2image(self))); } |
#smooth=(val) ⇒ Object
283 284 285 286 287 |
# File 'ext/image.c', line 283
VALUE ray_image_set_smooth(VALUE self, VALUE val) {
rb_check_frozen(self);
say_image_set_smooth(ray_rb2image(self), RTEST(val));
return val;
}
|
#smooth? ⇒ Boolean
266 267 268 |
# File 'ext/image.c', line 266 VALUE ray_image_is_smooth(VALUE self) { return say_image_is_smooth(ray_rb2image(self)) ? Qtrue : Qfalse; } |
#tex_rect(rect) ⇒ Rect
225 226 227 228 |
# File 'ext/image.c', line 225
VALUE ray_image_tex_rect(VALUE self, VALUE rect) {
return ray_rect2rb(say_image_get_tex_rect(ray_rb2image(self),
ray_convert_to_rect(rect)));
}
|
#texture ⇒ Integer
Returns Identifer of the image’s texture.
259 260 261 |
# File 'ext/image.c', line 259 VALUE ray_image_texture(VALUE self) { return ULONG2NUM(say_image_get_texture(ray_rb2image(self))); } |
#turtle { ... } ⇒ Ray::Turtle
Creates a turtle operating on the receiver
If a block is passed, it is instance-evaluated (self becoming the turtle) and the receiver gets updated automatically.
207 208 209 |
# File 'lib/ray/turtle.rb', line 207 def turtle(&block) Ray::ImageTarget.new(self).turtle(&block) end |
#width ⇒ Integer Also known as: w
Returns Width of the image in pixels.
140 141 142 143 |
# File 'ext/image.c', line 140 static VALUE ray_image_width(VALUE self) { return INT2FIX(say_image_get_width(ray_rb2image(self))); } |
#write(filename) ⇒ Object
130 131 132 133 134 135 136 137 |
# File 'ext/image.c', line 130
static
VALUE ray_image_write(VALUE self, VALUE filename) {
if (!say_image_write(ray_rb2image(self), StringValuePtr(filename))) {
rb_raise(rb_eRuntimeError, "%s", say_error_get_last());
}
return self;
}
|
#write_bmp(filename) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'ext/image.c', line 86
static
VALUE ray_image_write_bmp(VALUE self, VALUE filename) {
if (!say_image_write_bmp(ray_rb2image(self), StringValuePtr(filename))) {
rb_raise(rb_eRuntimeError, "%s", say_error_get_last());
}
return self;
}
|
#write_png(filename) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'ext/image.c', line 100
static
VALUE ray_image_write_png(VALUE self, VALUE filename) {
if (!say_image_write_png(ray_rb2image(self), StringValuePtr(filename))) {
rb_raise(rb_eRuntimeError, "%s", say_error_get_last());
}
return self;
}
|
#write_tga(filename) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'ext/image.c', line 113
static
VALUE ray_image_write_tga(VALUE self, VALUE filename) {
if (!say_image_write_tga(ray_rb2image(self), StringValuePtr(filename))) {
rb_raise(rb_eRuntimeError, "%s", say_error_get_last());
}
return self;
}
|