Module: Pdfrb::ImageLoader

Defined in:
lib/pdfrb/image_loader.rb,
lib/pdfrb/image_loader/pdf.rb,
lib/pdfrb/image_loader/png.rb,
lib/pdfrb/image_loader/jpeg.rb

Overview

Image loaders: build a Type::XObjectImage (or Type::XObjectForm for PDF) from a JPEG, PNG, or PDF source. Each loader is a module under ImageLoader::* with a call(document, io, **opts) class method that returns an image object or nil if it can't handle the format.

Defined Under Namespace

Modules: JPEG, PDF, PNG

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loadersObject (readonly)

Returns the value of attribute loaders.



17
18
19
# File 'lib/pdfrb/image_loader.rb', line 17

def loaders
  @loaders
end

Class Method Details

.load(document, io, **opts) ⇒ Object

Build an image XObject from io (IO, file path, or bytes). Iterates registered loaders until one matches.

Raises:



27
28
29
30
31
32
33
34
35
# File 'lib/pdfrb/image_loader.rb', line 27

def load(document, io, **opts)
  bytes = read_bytes(io)
  @loaders.each do |loader|
    result = loader.call(document, bytes, **opts)
    return result if result
  end
  raise Pdfrb::Error,
        "no image loader matched (unknown or unrecognised format)"
end

.register(loader) ⇒ Object

Register a loader. Loaders are tried in registration order; first non-nil return wins.



21
22
23
# File 'lib/pdfrb/image_loader.rb', line 21

def register(loader)
  @loaders << loader
end

.reset!Object



37
38
39
# File 'lib/pdfrb/image_loader.rb', line 37

def reset!
  @loaders.clear
end