Class: Quirc::ImageProcessor::Vips

Inherits:
Quirc::ImageProcessor show all
Defined in:
lib/quirc/image_processor/vips.rb

Instance Attribute Summary

Attributes inherited from Quirc::ImageProcessor

#pathname

Instance Method Summary collapse

Methods inherited from Quirc::ImageProcessor

#initialize

Constructor Details

This class inherits a constructor from Quirc::ImageProcessor

Instance Method Details

#to_grayscale(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/quirc/image_processor/vips.rb', line 5

def to_grayscale(*args)
  read_image do |image|
    with_tempfile("-grayscale-8bit.tiff") do |tempfile|
      options = args.last.is_a?(Hash) ? args.pop : {}
      width, height = args
      width  ||= options.delete(:width) || image.width
      height ||= options.delete(:height) || image.height
      pipeline = ImageProcessing::Vips.source(image)
      if width != image.width || height != image.height
        pipeline = pipeline.resize_to_fit(width, height, **options)
      end
      pipeline = pipeline.colourspace("b_w")
      pipeline = pipeline.flatten
      pipeline = pipeline.convert("tiff")
      image    = pipeline.call(save: false)
      pipeline.call(destination: tempfile.path)
      tempfile.rewind
      yield tempfile.read, image.width, image.height
    end
  end
end