Class: Ray::Image

Inherits:
Object
  • Object
show all
Extended by:
ResourceSet
Includes:
Enumerable
Defined in:
ext/image.c,
lib/ray/image.rb,
lib/ray/turtle.rb,
ext/image.c

Overview

An image is only the resource used when drawing. It is not the object you can directly draw on the screen.

Instance Method Summary collapse

Methods included from ResourceSet

add_set, clear, missing_pattern, reject!, select!, set_hash

Constructor Details

#initialize(io) ⇒ Object #initialize(filename) ⇒ Object #initialize(size) ⇒ Object

Overloads:

  • #initialize(io) ⇒ Object

    Examples:

    open("test.png") { |io| Ray::Image.new(io) }

    Parameters:

    • io (IO, #read)

      An object containing the image.

  • #initialize(filename) ⇒ Object

    Examples:

    Ray::Image.new "test.png"

    Parameters:

    • filename (String)

      Name of a file to load the image from.

  • #initialize(size) ⇒ Object

    Examples:

    Ray::Image.new [64, 128]

    Parameters:

    • size (Vector2, #to_vector2)

      Size of the image to create



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'ext/image.c', line 37

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

Returns Color of the pixel.

Parameters:

  • x (Integer)

    X position of the pixel

  • y (Integer)

    Y position of the pixel

Returns:

  • (Color)

    Color of the pixel



163
164
165
166
167
168
169
170
171
172
173
# File 'ext/image.c', line 163

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

Parameters:

  • x (Integer)

    X position of the pixel

  • y (Integer)

    Y position of the pixel

  • color (Color)

    New color of the pixel



182
183
184
185
186
187
188
189
190
191
192
193
# File 'ext/image.c', line 182

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;
}

#bindObject

Binds the texture, which will be used as GL_TEXTURE_2D when drawing with OpenGL



212
213
214
215
# File 'ext/image.c', line 212

VALUE ray_image_bind(VALUE self) {
  say_image_bind(ray_rb2image(self));
  return self;
}

#each {|pixel| ... } ⇒ Object

Yields:

  • (pixel)

Yield Parameters:



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ray/image.rb', line 17

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

Same as each, but also yields the position of each point.

Yields:

  • (pixel, x, y)


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ray/image.rb', line 31

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

#heightInteger Also known as: h

Returns Height of the image in pixels.

Returns:

  • (Integer)

    Height of the image in pixels



133
134
135
136
# File 'ext/image.c', line 133

static
VALUE ray_image_height(VALUE self) {
  return INT2FIX(say_image_get_height(ray_rb2image(self)));
}

#initialize_copy(other) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'ext/image.c', line 62

static
VALUE ray_image_init_copy(VALUE self, VALUE other) {
  say_image *orig = ray_rb2image(other);

  say_image_load_raw(ray_rb2image(self),
                     say_image_get_width(orig), say_image_get_height(orig),
                     say_image_get_buffer(orig));

  return self;
}

#inspectObject



5
6
7
# File 'lib/ray/image.rb', line 5

def inspect
  "#<#{self.class} size=#{size}>"
end

#map(&block) ⇒ Ray::Image

Returns New image created according to a block.

Returns:

  • (Ray::Image)

    New image created according to a block.



70
71
72
# File 'lib/ray/image.rb', line 70

def map(&block)
  dup.map!(&block)
end

#map! {|pixel| ... } ⇒ Object

Yields:

  • (pixel)

    Block returning the new color of this pixel.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ray/image.rb', line 44

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(&block) ⇒ Ray::Image

Returns New image created according to a block.

Returns:

  • (Ray::Image)

    New image created according to a block.



75
76
77
# File 'lib/ray/image.rb', line 75

def map_with_pos(&block)
  dup.map_with_pos!(&block)
end

#map_with_pos! {|pixel, x, y| ... } ⇒ Object

Yields:

  • (pixel, x, y)

    Block returning the new color of this pixel



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ray/image.rb', line 57

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

#sizeRay::Vector2

Returns Size of the image in pixels.

Returns:



139
140
141
142
# File 'ext/image.c', line 139

static
VALUE ray_image_size(VALUE self) {
  return ray_vector2_to_rb(say_image_get_size(ray_rb2image(self)));
}

#smooth=(val) ⇒ Object



226
227
228
229
230
# File 'ext/image.c', line 226

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?true, false

Returns True if the image uses linear interplation when it’s drawn with a different size than the normal one. Uses the color of the nearest pixel otherwise.

Returns:

  • (true, false)

    True if the image uses linear interplation when it’s drawn with a different size than the normal one. Uses the color of the nearest pixel otherwise.



222
223
224
# File 'ext/image.c', line 222

VALUE ray_image_is_smooth(VALUE self) {
  return say_image_is_smooth(ray_rb2image(self)) ? Qtrue : Qfalse;
}

#tex_rect(rect) ⇒ Rect

Converts a rect of pixel coordinates to a rect of texture coordinates, which may be needed to generate vertices using this image.

Parameters:

  • rect (Rect)

    Rect in pixel coordinates

Returns:

  • (Rect)

    Rect in texture coordinates



203
204
205
206
# File 'ext/image.c', line 203

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)));
}

#turtle(&block) ⇒ Ray::Turtle

Creates a turtle operating on the receiver. Instance evaluates the block it is given if any.

Returns:



207
208
209
# File 'lib/ray/turtle.rb', line 207

def turtle(&block)
  Ray::ImageTarget.new(self).turtle(&block)
end

#widthInteger Also known as: w

Returns Width of the image in pixels.

Returns:

  • (Integer)

    Width of the image in pixels



127
128
129
130
# File 'ext/image.c', line 127

static
VALUE ray_image_width(VALUE self) {
  return INT2FIX(say_image_get_width(ray_rb2image(self)));
}

#write(filename) ⇒ Object

Saves the image, tyring to guess the format. If it cannot be guessed, BMP will be used.



117
118
119
120
121
122
123
124
# File 'ext/image.c', line 117

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

Saves the image as a BMP.



77
78
79
80
81
82
83
84
# File 'ext/image.c', line 77

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

Saves the image as a PNG.



90
91
92
93
94
95
96
97
# File 'ext/image.c', line 90

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

Saves the image as a TGA.



103
104
105
106
107
108
109
110
# File 'ext/image.c', line 103

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;
}