lib/morandi/profiled_pixbuf.rb in morandi-0.12.1 vs lib/morandi/profiled_pixbuf.rb in morandi-0.99.03

- old
+ new

@@ -1,82 +1,61 @@ +# frozen_string_literal: true + require 'gdk_pixbuf2' -class Morandi::ProfiledPixbuf < GdkPixbuf::Pixbuf - def valid_jpeg?(filename) - return false unless File.exist?(filename) - return false unless File.size(filename) > 0 +module Morandi + # ProfiledPixbuf is a descendent of GdkPixbuf::Pixbuf with ICC support. + # It attempts to load an image using jpegicc/littlecms to ensure that it is sRGB. + class ProfiledPixbuf < GdkPixbuf::Pixbuf + def valid_jpeg?(filename) + return false unless File.exist?(filename) + return false unless File.size(filename).positive? - type, _, _ = GdkPixbuf::Pixbuf.get_file_info(filename) + type, = GdkPixbuf::Pixbuf.get_file_info(filename) - type && type.name.eql?('jpeg') - rescue - false - end + type && type.name.eql?('jpeg') + rescue StandardError + false + end - def self.from_string(string, loader: nil, chunk_size: 4096) - loader ||= GdkPixbuf::PixbufLoader.new - ((string.bytesize + chunk_size - 1) / chunk_size).times do |i| - loader.write(string.byteslice(i * chunk_size, chunk_size)) + # TODO: this doesn't use lcms + def self.from_string(string, loader: nil, chunk_size: 4096) + loader ||= GdkPixbuf::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 - loader.close - loader.pixbuf - end - def self.default_icc_path(path) - "#{path}.icc.jpg" - end + def self.default_icc_path(path) + "#{path}.icc.jpg" + end - def initialize(*args) - @local_options = args.last.is_a?(Hash) && args.pop || {} + def initialize(file, local_options, scale_to = nil) + @local_options = local_options + @file = file - 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) + valid_jpeg?(icc_file) || system('jpgicc', '-q97', @file, icc_file) + file = icc_file if valid_jpeg?(icc_file) end - end - # TODO: This is to fix some deprecation warnings. This needs refactoring. - # All can be implemented without having to hack on the PixBuff gem. - case args.size - when 1 - super(file: args.first) - when 3 - super(path: args[0], width: args[1], height: args[2]) - else - super(*args) - end - rescue GdkPixbuf::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 - - if args.size == 1 - super file: args.first + if scale_to + super(file: file, width: scale_to, height: scale_to) else - super(*args) + super(file: file) end - - temp.close - temp.unlink - else - throw e end - end + private - protected - def suitable_for_jpegicc? - type, _, _ = GdkPixbuf::Pixbuf.get_file_info(@file) + def suitable_for_jpegicc? + valid_jpeg?(@file) + end - type && type.name.eql?('jpeg') - end - - def icc_cache_path - @local_options['path.icc'] || Morandi::ProfiledPixbuf.default_icc_path(@file) + def icc_cache_path + @local_options['path.icc'] || Morandi::ProfiledPixbuf.default_icc_path(@file) + end end end