Class: Morandi::ProfiledPixbuf

Inherits:
Gdk::Pixbuf
  • Object
show all
Defined in:
lib/morandi/profiled_pixbuf.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ProfiledPixbuf

Returns a new instance of ProfiledPixbuf.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/morandi/profiled_pixbuf.rb', line 28

def initialize(*args)
  @local_options = args.last.is_a?(Hash) && args.pop || {}

  if args[0].is_a?(String)
    @file = args[0]

    if suitable_for_jpegicc?
      icc_file = icc_cache_path

      args[0] = icc_file if valid_jpeg?(icc_file) || system("jpgicc", "-q97", @file, icc_file)
    end
  end

  super(*args)
rescue Gdk::PixbufError::CorruptImage => e
  if args[0].is_a?(String) && defined? Tempfile
    temp =  Tempfile.new
    pixbuf = self.class.from_string(File.read(args[0]))
    pixbuf.save(temp.path, 'jpeg')
    args[0] = temp.path
    super(*args)
    temp.close
    temp.unlink
  else
    throw e
  end
end

Class Method Details

.default_icc_path(path) ⇒ Object



24
25
26
# File 'lib/morandi/profiled_pixbuf.rb', line 24

def self.default_icc_path(path)
  "#{path}.icc.jpg"
end

.from_string(string, loader: nil, chunk_size: 4096) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/morandi/profiled_pixbuf.rb', line 15

def self.from_string(string, loader: nil, chunk_size: 4096)
  loader ||= Gdk::PixbufLoader.new
  ((string.bytesize + chunk_size - 1) / chunk_size).times do |i|
    loader.write(string.byteslice(i * chunk_size, chunk_size))
  end
  loader.close
  loader.pixbuf
end

Instance Method Details

#valid_jpeg?(filename) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
11
12
13
# File 'lib/morandi/profiled_pixbuf.rb', line 4

def valid_jpeg?(filename)
  return false unless File.exist?(filename)
  return false unless File.size(filename) > 0

  type, _, _ = Gdk::Pixbuf.get_file_info(filename)

  type && type.name.eql?('jpeg')
rescue
  false
end