Class: Rgltf::Image

Inherits:
Properties::Base show all
Defined in:
lib/rgltf/properties/image.rb

Instance Attribute Summary collapse

Attributes inherited from Properties::Base

#extensions, #extras, #index, #name, #owner_type, #source_json

Instance Method Summary collapse

Methods inherited from Properties::Base

#extension, #nested_properties, #parse_extensions!

Constructor Details

#initialize(json, document:, index:) ⇒ Image

Returns a new instance of Image.



7
8
9
10
11
12
13
14
15
16
# File 'lib/rgltf/properties/image.rb', line 7

def initialize(json, document:, index:)
  super(json, document:, index:, owner_type: :image)
  @source_uri = json['uri']
  @mime_type = json['mimeType']
  @buffer_view = if json['bufferView'].nil?
                   nil
                 else
                   document.fetch_reference(:buffer_views, json['bufferView'], "images/#{index}/bufferView")
                 end
end

Instance Attribute Details

#buffer_viewObject (readonly)

Returns the value of attribute buffer_view.



5
6
7
# File 'lib/rgltf/properties/image.rb', line 5

def buffer_view
  @buffer_view
end

#mime_typeObject (readonly)

Returns the value of attribute mime_type.



5
6
7
# File 'lib/rgltf/properties/image.rb', line 5

def mime_type
  @mime_type
end

Instance Method Details

#bytesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rgltf/properties/image.rb', line 26

def bytes
  @bytes ||= begin
    data = if buffer_view
             buffer_view.buffer.bytes.byteslice(buffer_view.byte_offset, buffer_view.byte_length)
           elsif @source_uri
             @document.buffer_resolver.resolve_resource(@source_uri)
           else
             raise MissingResourceError, "image #{index} has neither uri nor bufferView"
           end
    raise FormatError, "image #{index} could not be read" unless data
    if buffer_view && data.bytesize != buffer_view.byte_length
      raise FormatError, "image #{index} exceeds its buffer"
    end

    data.b.freeze
  end
end

#release!Object



44
45
46
# File 'lib/rgltf/properties/image.rb', line 44

def release!
  @bytes = nil
end

#source?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/rgltf/properties/image.rb', line 22

def source?
  buffer_view || @source_uri
end

#uriObject



18
19
20
# File 'lib/rgltf/properties/image.rb', line 18

def uri
  @source_uri unless @source_uri&.start_with?('data:')
end