Class: Texel::Image

Inherits:
Object
  • Object
show all
Includes:
ImageTransformations
Defined in:
lib/texel/image.rb

Constant Summary collapse

DTYPE_SIZES =
{ u8: 1, u16: 2, f32: 4 }.freeze
COLOR_SPACES =
%i[srgb linear unknown].freeze
DUPLICABLE_TAGS =
%i[width height channels dtype color_space premultiplied].freeze

Constants included from ImageTransformations

Texel::ImageTransformations::EDGES, Texel::ImageTransformations::FILTERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ImageTransformations

#convert, #flip_y, #mipmaps, #premultiply, #resize, #resize_to_fit, #to_linear, #to_srgb, #unpremultiply

Constructor Details

#initialize(width:, height:, channels:, dtype:, color_space:, data:, premultiplied: false) ⇒ Image

Returns a new instance of Image.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/texel/image.rb', line 11

def initialize(width:, height:, channels:, dtype:, color_space:, data:, premultiplied: false)
  validate_dimensions!(width, height)
  validate_channels!(channels)
  validate_dtype!(dtype)
  validate_color_space!(color_space)

  @width = width
  @height = height
  @channels = channels
  @dtype = dtype
  @color_space = color_space
  source = String(data)
  @data = if source.encoding == Encoding::BINARY && source.frozen?
            source
          else
            source.dup.force_encoding(Encoding::BINARY).freeze
          end
  @premultiplied = !!premultiplied
  validate_data_size!
end

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



9
10
11
# File 'lib/texel/image.rb', line 9

def channels
  @channels
end

#color_spaceObject (readonly)

Returns the value of attribute color_space.



9
10
11
# File 'lib/texel/image.rb', line 9

def color_space
  @color_space
end

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/texel/image.rb', line 9

def data
  @data
end

#dtypeObject (readonly)

Returns the value of attribute dtype.



9
10
11
# File 'lib/texel/image.rb', line 9

def dtype
  @dtype
end

#heightObject (readonly)

Returns the value of attribute height.



9
10
11
# File 'lib/texel/image.rb', line 9

def height
  @height
end

#premultipliedObject

Returns the value of attribute premultiplied.



9
10
11
# File 'lib/texel/image.rb', line 9

def premultiplied
  @premultiplied
end

#widthObject (readonly)

Returns the value of attribute width.



9
10
11
# File 'lib/texel/image.rb', line 9

def width
  @width
end

Instance Method Details

#bytesize_per_pixelObject



32
33
34
# File 'lib/texel/image.rb', line 32

def bytesize_per_pixel
  channels * DTYPE_SIZES.fetch(dtype)
end

#dup_with(data: nil, **tags) ⇒ Object

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/texel/image.rb', line 50

def dup_with(data: nil, **tags)
  unknown_tags = tags.keys - DUPLICABLE_TAGS
  raise ArgumentError, "unknown image tags: #{unknown_tags.join(", ")}" unless unknown_tags.empty?

  self.class.new(
    width: tags.fetch(:width, width),
    height: tags.fetch(:height, height),
    channels: tags.fetch(:channels, channels),
    dtype: tags.fetch(:dtype, dtype),
    color_space: tags.fetch(:color_space, color_space),
    data: data || self.data,
    premultiplied: tags.fetch(:premultiplied, premultiplied)
  )
end

#pixel(x, y) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/texel/image.rb', line 40

def pixel(x, y)
  unless x.is_a?(Integer) && y.is_a?(Integer) && x.between?(0, width - 1) && y.between?(0, height - 1)
    raise IndexError, "pixel coordinates (#{x}, #{y}) are outside #{width}x#{height} image"
  end

  byte_offset = ((y * width) + x) * bytesize_per_pixel
  bytes = data.byteslice(byte_offset, bytesize_per_pixel)
  PixelData.unpack(bytes, dtype)
end

#premultiplied?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/texel/image.rb', line 65

def premultiplied?
  premultiplied
end

#row_bytesObject



36
37
38
# File 'lib/texel/image.rb', line 36

def row_bytes
  width * bytesize_per_pixel
end