Class: Image

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(width, height, mipmaps, format) ⇒ Image

Creates a new instance of Image

Parameters:

  • width (Integer)
  • height (Integer)
  • mipmaps (Integer)
  • format (Integer)


8
9
10
11
12
# File 'mrb_doc/models/image.rb', line 8

def initialize(width, height, mipmaps, format)
  # mrb_Image_initialize
  # src/mruby_integration/models/image.cpp
  Image.new
end

Instance Attribute Details

#formatInteger

Returns:

  • (Integer)


4
5
6
# File 'src/ruby/models/image.rb', line 4

def format
  @format
end

#heightInteger

Returns:

  • (Integer)


4
5
6
# File 'src/ruby/models/image.rb', line 4

def height
  @height
end

#mipmapsInteger

Returns:

  • (Integer)


4
5
6
# File 'src/ruby/models/image.rb', line 4

def mipmaps
  @mipmaps
end

#widthInteger

Returns:

  • (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.

Parameters:

  • width (Integer)
  • height (Integer)
  • colour (Colour) (defaults to: RAYWHITE)

Returns:



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

Parameters:

  • path (String)

Returns:

Raises:



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

Parameters:

Returns:



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

Parameters:

  • brightness (Float)

    a value between -255 and 255

Returns:

  • (nil)

Raises:

  • (ArgumentError)

    If the brightness is outside of the allowed range



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

Parameters:

  • contrast (Float)

    a value between -100 and 100

Returns:

  • (nil)

Raises:

  • (ArgumentError)

    If the contrast is outside of the allowed range



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.

Parameters:

Returns:



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

Parameters:

Returns:



79
80
81
82
# File 'src/ruby/models/image.rb', line 79

def crop!(rectangle)
  image_crop!(self, rectangle)
  self
end

#dataArray<Colour>

Returns an array containing the image data as an array of Colour objects

Returns:



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.

Parameters:

  • image (Image)
  • source (Rectangle) (defaults to: nil)

    if left blank, will use the whole image

  • destination (Rectangle) (defaults to: nil)

    if left blank, will draw into the top left corner

  • colour (Colour) (defaults to: WHITE)

    if left blank, no tinting is applied

Returns:

  • (nil)


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

Parameters:

  • path (String)

Returns:

  • (nil)


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

Returns:



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

Returns:



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

Returns:



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

Returns:

  • (nil)


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

Returns:

  • (nil)


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

Returns:



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

Parameters:

Returns:

  • (nil)


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.

Parameters:

  • width (Integer)
  • height (Integer)
  • scaling (Symbol) (defaults to: :nearest_neighbour)

    Valid options are :nearest_neighbour and :bicubic

Returns:



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

Parameters:

  • direction (Symbol) (defaults to: :cw)

    Valid options are :cw and :ccw

Returns:

Raises:

  • (ArgumentError)

    If the direction is invalid



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

Parameters:

Returns:

  • (nil)


138
139
140
141
# File 'src/ruby/models/image.rb', line 138

def tint!(colour)
  image_colour_tint!(self, colour)
  self
end

#to_hHash

Return the object represented by a Hash

Returns:

  • (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_textureTexture2D

Returns a texture of the image

Returns:



53
54
55
# File 'src/ruby/models/image.rb', line 53

def to_texture
  load_texture_from_image(self)
end

#unloadnil

Unloads the texture from memory

Returns:

  • (nil)


28
29
30
# File 'src/ruby/models/image.rb', line 28

def unload
  unload_image(self)
end