Class: Processing::Image

Inherits:
Object
  • Object
show all
Includes:
Xot::Inspectable
Defined in:
lib/processing/image.rb

Overview

Image object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pixelsArray (readonly)

An array of all pixels. Call loadPixels() before accessing the array.



113
114
115
# File 'lib/processing/image.rb', line 113

def pixels
  @pixels
end

Instance Method Details

#blend(sx, sy, sw, sh, dx, dy, dw, dh, mode) ⇒ nil #blend(img, sx, sy, sw, sh, dx, dy, dw, dh, mode) ⇒ nil

Blends image.

Parameters:

  • img (Image) (defaults to: nil)

    image for blend source

  • sx (Numeric)

    x position of source region

  • sy (Numeric)

    y position of source region

  • sw (Numeric)

    width of source region

  • sh (Numeric)

    height of source region

  • dx (Numeric)

    x position of destination region

  • dy (Numeric)

    y position of destination region

  • dw (Numeric)

    width of destination region

  • dh (Numeric)

    height of destination region

  • mode (BLEND, ADD, SUBTRACT, LIGHTEST, DARKEST, EXCLUSION, MULTIPLY, SCREEN, REPLACE)

    blend mode

Returns:

  • (nil)

    nil

See Also:



199
200
201
202
203
204
205
# File 'lib/processing/image.rb', line 199

def blend(img = nil, sx, sy, sw, sh, dx, dy, dw, dh, mode)
  img ||= self
  getInternal__.paint do |painter|
    img.drawImage__ painter, sx, sy, sw, sh, dx, dy, dw, dh, blend_mode: mode
  end
  nil
end

#copy(sx, sy, sw, sh, dx, dy, dw, dh) ⇒ nil #copy(img, sx, sy, sw, sh, dx, dy, dw, dh) ⇒ nil

Copies image.

Parameters:

  • img (Image) (defaults to: nil)

    image for copy source

  • sx (Numrtic)

    x position of source region

  • sy (Numrtic)

    y position of source region

  • sw (Numrtic)

    width of source region

  • sh (Numrtic)

    height of source region

  • dx (Numrtic)

    x position of destination region

  • dy (Numrtic)

    y position of destination region

  • dw (Numrtic)

    width of destination region

  • dh (Numrtic)

    height of destination region

Returns:

  • (nil)

    nil

See Also:



169
170
171
# File 'lib/processing/image.rb', line 169

def copy(img = nil, sx, sy, sw, sh, dx, dy, dw, dh)
  blend img, sx, sy, sw, sh, dx, dy, dw, dh, :normal
end

#filter(*args) ⇒ Object

Applies an image filter.

overload filter(shader) overload filter(type) overload filter(type, param)

Parameters:

  • shader (Shader)

    a fragment shader to apply

  • type (THRESHOLD, GRAY, INVERT, BLUR)

    filter type

  • param (Numeric)

    a parameter for each filter

See Also:



128
129
130
# File 'lib/processing/image.rb', line 128

def filter(*args)
  @filter = Shader.createFilter__(*args)
end

#get(x, y) ⇒ Integer

Returns the color of the pixel.

Returns:

  • (Integer)

    color value (0xAARRGGBB)

See Also:



75
76
77
78
79
# File 'lib/processing/image.rb', line 75

def get(x, y)
  getInternal__.bitmap[x, y]
    .map {|n| (n * 255).to_i.clamp 0, 255}
    .then {|r, g, b, a| self.class.toColor__ r, g, b, a}
end

#heightNumeric Also known as: h

Gets height of image.



37
38
39
# File 'lib/processing/image.rb', line 37

def height()
  @image&.height || (@error ? -1 : 0)
end

#loadPixelsnil

Loads all pixels to the ‘pixels’ array.



88
89
90
# File 'lib/processing/image.rb', line 88

def loadPixels()
  @pixels = getInternal__.pixels
end

#resize(width, height) ⇒ nil

Resizes image.

Parameters:

  • width (Numeric)

    width for resized image

  • height (Numeric)

    height for resized image

Returns:

  • (nil)

    nil

See Also:



142
143
144
145
146
147
# File 'lib/processing/image.rb', line 142

def resize(width, height)
  @image = Rays::Image.new(width, height).paint do |painter|
    painter.image getInternal__, 0, 0, width, height
  end
  nil
end

#save(filename) ⇒ nil

Saves image to file.

Parameters:

  • filename (String)

    file name to save image

Returns:

  • (nil)

    nil

See Also:



256
257
258
259
# File 'lib/processing/image.rb', line 256

def save(filename)
  getInternal__.save filename
  nil
end

#set(x, y, c) ⇒ nil

Sets the color of the pixel.

Parameters:

  • x (Integer)

    x position of the pixel

  • y (Integer)

    y position of the pixel

  • c (Integer)

    color value

Returns:

  • (nil)

    nil

See Also:



63
64
65
66
# File 'lib/processing/image.rb', line 63

def set(x, y, c)
  getInternal__.bitmap(true)[x, y] = self.class.fromColor__(c).map {|n| n / 255.0}
  nil
end

#sizeArray<Numeric>

Returns the width and height of image.

Returns:

  • (Array<Numeric>)
    width, height


48
49
50
# File 'lib/processing/image.rb', line 48

def size()
  [width, height]
end

#updatePixelsnil

Update the image pixels with the ‘pixels’ array.



99
100
101
102
103
# File 'lib/processing/image.rb', line 99

def updatePixels()
  return unless @pixels
  getInternal__.pixels = @pixels
  @pixels = nil
end

#widthNumeric Also known as: w

Gets width of image.



26
27
28
# File 'lib/processing/image.rb', line 26

def width()
  @image&.width || (@error ? -1 : 0)
end