Module: Retouch::ImageIO

Defined in:
lib/retouch/io.rb

Class Method Summary collapse

Class Method Details

.read(path) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/retouch/io.rb', line 7

def read(path)
  signature = File.binread(path, 8)
  format = if signature.start_with?(Tessel::SIGNATURE)
             "PNG"
           elsif signature.start_with?("P3", "P6")
             "PPM"
           elsif signature.start_with?("BM")
             "BMP"
           elsif signature.start_with?("GIF87a", "GIF89a")
             "GIF"
           end
  return with_format(Operations.open_gif(path, frame: 0), format) if format == "GIF"

  with_format(Tessel.read(path), format || "unknown")
rescue Tessel::UnsupportedError => e
  raise Error, "unsupported image format for #{path}: #{e.message}", cause: e
end

.with_format(image, format) ⇒ Object



45
46
47
48
# File 'lib/retouch/io.rb', line 45

def with_format(image, format)
   = image..merge("format" => format)
  Tessel::Image.from_rgba(image.width, image.height, image.bytes, metadata:)
end

.write(image, path, level: 6, strip: false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/retouch/io.rb', line 25

def write(image, path, level: 6, strip: false)
  raise TypeError, "image must be a Tessel::Image" unless image.is_a?(Tessel::Image)

  image = Tessel::Image.from_rgba(image.width, image.height, image.bytes) if strip
  extension = File.extname(path).downcase
  case extension
  when ".png"
    image.write(path, level: Integer(level))
  when ".ppm", ".pnm"
    image.write(path, binary: true)
  when ".bmp"
    image.write(path)
  else
    raise Error, "unsupported output format: #{extension.empty? ? "(no extension)" : extension}"
  end
  path
rescue ArgumentError, Tessel::Error => e
  raise Error, e.message, cause: e
end