Class: Image
- Inherits:
-
Object
- Object
- Image
- Defined in:
- src/ruby/models/image.rb,
mrb_doc/models/image.rb
Overview
A class for holding image data
Defined Under Namespace
Classes: NotFound
Instance Attribute Summary collapse
Class Method Summary collapse
-
.generate(width:, height:, colour: RAYWHITE) ⇒ Image
Generates a new image of width by height in the specified colour.
-
.load(path) ⇒ Image
Loads an image from the specified path.
Instance Method Summary collapse
-
#alpha_mask=(mask) ⇒ Image
Applies the alpha of the mask to the image.
-
#brightness!(brightness) ⇒ nil
Change the brightness of the image.
-
#contrast!(contrast) ⇒ nil
Change the contrast of the image.
-
#copy(source: nil) ⇒ Image
Copies the image to a new object.
-
#crop!(rectangle) ⇒ Image
Crops the image to the section in the rectangle.
-
#data ⇒ Array<Colour>
Returns an array containing the image data as an array of Colour objects.
-
#draw!(image:, source: nil, destination: nil, colour: WHITE) ⇒ nil
Draws the specified portion of the image into the specified region of the this image.
-
#export(path) ⇒ nil
Exports an image to a file.
-
#flip_horizontal! ⇒ Image
Flips the image sideways.
-
#flip_vertical! ⇒ Image
Flips the image upside down.
-
#generate_mipmaps! ⇒ Image
Generates mipmaps for the image.
-
#grayscale! ⇒ nil
Converts the image to grayscale.
-
#initialize(width, height, mipmaps, format) ⇒ Image
constructor
Creates a new instance of Image.
-
#invert! ⇒ nil
Inverts the colours of the image.
-
#premultiply_alpha! ⇒ Image
Pre-multiplies the alpha for the image.
-
#replace!(old_colour, new_colour) ⇒ nil
Replace the old Colour with the new Colour.
-
#resize!(width:, height:, scaling: :nearest_neighbour) ⇒ Image
Resizes the image, defaults to using the nearest neighbour algorithm which is useful for pixel art.
-
#rotate!(direction = :cw) ⇒ Image
Rotates the image either clockwise or counter-clockwise.
-
#tint!(colour) ⇒ nil
Tints the image with the specified colour.
-
#to_h ⇒ Hash
Return the object represented by a Hash.
-
#to_texture ⇒ Texture2D
Returns a texture of the image.
-
#unload ⇒ nil
Unloads the texture from memory.
Constructor Details
Instance Attribute Details
#format ⇒ Integer
4 5 6 |
# File 'src/ruby/models/image.rb', line 4 def format @format end |
#height ⇒ Integer
4 5 6 |
# File 'src/ruby/models/image.rb', line 4 def height @height end |
#mipmaps ⇒ Integer
4 5 6 |
# File 'src/ruby/models/image.rb', line 4 def mipmaps @mipmaps end |
#width ⇒ Integer
4 5 6 |
# File 'src/ruby/models/image.rb', line 4 def width @width end |
Class Method Details
.generate(width:, height:, colour: RAYWHITE) ⇒ Image
Generates a new image of width by height in the specified colour.
208 209 210 |
# File 'src/ruby/models/image.rb', line 208 def self.generate(width:, height:, colour: RAYWHITE) generate_image_colour(width, height, colour) end |
.load(path) ⇒ Image
Loads an image from the specified path
21 22 23 24 |
# File 'src/ruby/models/image.rb', line 21 def self.load(path) raise Image::NotFound.new("Could not find file at path \"#{path}\"") unless File.exist?(path) load_image(path) end |
Instance Method Details
#alpha_mask=(mask) ⇒ Image
Applies the alpha of the mask to the image
87 88 89 90 |
# File 'src/ruby/models/image.rb', line 87 def alpha_mask=(mask) image_alpha_mask!(self, mask) self end |
#brightness!(brightness) ⇒ nil
Change the brightness of the image
181 182 183 184 185 186 |
# File 'src/ruby/models/image.rb', line 181 def brightness!(brightness) raise ArgumentError.new('Must be within (-255..255)') if brightness < -255 || brightness > 255 image_colour_brightness!(self, brightness) self end |
#contrast!(contrast) ⇒ nil
Change the contrast of the image
161 162 163 164 165 166 |
# File 'src/ruby/models/image.rb', line 161 def contrast!(contrast) raise ArgumentError.new('Must be within (-100..100)') if contrast < -100 || contrast > 100 image_colour_contrast!(self, contrast) self end |
#copy(source: nil) ⇒ Image
Copies the image to a new object. If source
is specified it’ll only be that section of the image that is returned.
43 44 45 46 47 48 49 |
# File 'src/ruby/models/image.rb', line 43 def copy(source: nil) if source image_from_image(self, source) else image_copy(self) end end |
#crop!(rectangle) ⇒ Image
Crops the image to the section in the rectangle
79 80 81 82 |
# File 'src/ruby/models/image.rb', line 79 def crop!(rectangle) image_crop!(self, rectangle) self end |
#data ⇒ Array<Colour>
Returns an array containing the image data as an array of Colour objects
40 41 42 43 44 |
# File 'mrb_doc/models/image.rb', line 40 def data # mrb_Image_get_data # src/mruby_integration/models/image.cpp [Colour.new] end |
#draw!(image:, source: nil, destination: nil, colour: WHITE) ⇒ nil
Draws the specified portion of the image into the specified region of the this image.
195 196 197 198 199 200 201 |
# File 'src/ruby/models/image.rb', line 195 def draw!(image:, source: nil, destination: nil, colour: WHITE) source ||= Rectangle.new(0, 0, image.width, image.height) destination ||= Rectangle.new(0, 0, image.width, image.height) image_draw!(self, image, source, destination, colour) self end |
#export(path) ⇒ nil
Exports an image to a file
35 36 37 |
# File 'src/ruby/models/image.rb', line 35 def export(path) export_image(self, path) end |
#flip_horizontal! ⇒ Image
Flips the image sideways
115 116 117 118 |
# File 'src/ruby/models/image.rb', line 115 def flip_horizontal! image_flip_horizontal!(self) self end |
#flip_vertical! ⇒ Image
Flips the image upside down
108 109 110 111 |
# File 'src/ruby/models/image.rb', line 108 def flip_vertical! image_flip_vertical!(self) self end |
#generate_mipmaps! ⇒ Image
Generates mipmaps for the image
101 102 103 104 |
# File 'src/ruby/models/image.rb', line 101 def generate_mipmaps! image_mipmaps!(self) self end |
#grayscale! ⇒ nil
Converts the image to grayscale
152 153 154 155 |
# File 'src/ruby/models/image.rb', line 152 def grayscale! image_colour_grayscale!(self) self end |
#invert! ⇒ nil
Inverts the colours of the image
145 146 147 148 |
# File 'src/ruby/models/image.rb', line 145 def invert! image_colour_invert!(self) self end |
#premultiply_alpha! ⇒ Image
Pre-multiplies the alpha for the image
94 95 96 97 |
# File 'src/ruby/models/image.rb', line 94 def premultiply_alpha! image_alpha_premultiply!(self) self end |
#replace!(old_colour, new_colour) ⇒ nil
Replace the old Colour with the new Colour
172 173 174 175 |
# File 'src/ruby/models/image.rb', line 172 def replace!(old_colour, new_colour) image_colour_replace!(self, old_colour, new_colour) self end |
#resize!(width:, height:, scaling: :nearest_neighbour) ⇒ Image
Resizes the image, defaults to using the nearest neighbour algorithm which is useful for pixel art.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'src/ruby/models/image.rb', line 63 def resize!(width:, height:, scaling: :nearest_neighbour) case scaling when :bicubic image_resize!(self, width, height) when :nearest_neighbour image_resize_nearest_neighbour!(self, width, height) else raise ArgumentError.new("Unknown scaler \"#{scaling}\", valid options are: :bicubic, :nearest_neighbour") end self end |
#rotate!(direction = :cw) ⇒ Image
Rotates the image either clockwise or counter-clockwise
124 125 126 127 128 129 130 131 132 133 |
# File 'src/ruby/models/image.rb', line 124 def rotate!(direction = :cw) raise ArgumentError.new('Value must be :ccw, :cw, or nil') unless [:ccw, :cw, nil].include?(direction) if direction == :ccw image_rotate_ccw!(self) else image_rotate_cw!(self) end self end |
#tint!(colour) ⇒ nil
Tints the image with the specified colour
138 139 140 141 |
# File 'src/ruby/models/image.rb', line 138 def tint!(colour) image_colour_tint!(self, colour) self end |
#to_h ⇒ Hash
Return the object represented by a Hash
8 9 10 11 12 13 14 15 |
# File 'src/ruby/models/image.rb', line 8 def to_h { width: width, height: height, mipmaps: mipmaps, format: format, } end |
#to_texture ⇒ Texture2D
Returns a texture of the image
53 54 55 |
# File 'src/ruby/models/image.rb', line 53 def to_texture load_texture_from_image(self) end |
#unload ⇒ nil
Unloads the texture from memory
28 29 30 |
# File 'src/ruby/models/image.rb', line 28 def unload unload_image(self) end |