Class: Texel::Image
- Inherits:
-
Object
- Object
- Texel::Image
- 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
-
#channels ⇒ Object
readonly
Returns the value of attribute channels.
-
#color_space ⇒ Object
readonly
Returns the value of attribute color_space.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#dtype ⇒ Object
readonly
Returns the value of attribute dtype.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#premultiplied ⇒ Object
Returns the value of attribute premultiplied.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #bytesize_per_pixel ⇒ Object
- #dup_with(data: nil, **tags) ⇒ Object
-
#initialize(width:, height:, channels:, dtype:, color_space:, data:, premultiplied: false) ⇒ Image
constructor
A new instance of Image.
- #pixel(x, y) ⇒ Object
- #premultiplied? ⇒ Boolean
- #row_bytes ⇒ Object
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
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
9 10 11 |
# File 'lib/texel/image.rb', line 9 def channels @channels end |
#color_space ⇒ Object (readonly)
Returns the value of attribute color_space.
9 10 11 |
# File 'lib/texel/image.rb', line 9 def color_space @color_space end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
9 10 11 |
# File 'lib/texel/image.rb', line 9 def data @data end |
#dtype ⇒ Object (readonly)
Returns the value of attribute dtype.
9 10 11 |
# File 'lib/texel/image.rb', line 9 def dtype @dtype end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
9 10 11 |
# File 'lib/texel/image.rb', line 9 def height @height end |
#premultiplied ⇒ Object
Returns the value of attribute premultiplied.
9 10 11 |
# File 'lib/texel/image.rb', line 9 def premultiplied @premultiplied end |
#width ⇒ Object (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_pixel ⇒ Object
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
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, **) = .keys - DUPLICABLE_TAGS raise ArgumentError, "unknown image tags: #{.join(", ")}" unless .empty? self.class.new( width: .fetch(:width, width), height: .fetch(:height, height), channels: .fetch(:channels, channels), dtype: .fetch(:dtype, dtype), color_space: .fetch(:color_space, color_space), data: data || self.data, premultiplied: .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
65 66 67 |
# File 'lib/texel/image.rb', line 65 def premultiplied? premultiplied end |
#row_bytes ⇒ Object
36 37 38 |
# File 'lib/texel/image.rb', line 36 def row_bytes width * bytesize_per_pixel end |