Class: RubySpriter::GimpProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_spriter/gimp_processor.rb

Overview

Processes images with GIMP

Constant Summary collapse

DEFAULT_BG_THRESHOLD =

Default background color tolerance for selection operations (0-100 scale)

15.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gimp_path, options = {}) ⇒ GimpProcessor

Returns a new instance of GimpProcessor.



14
15
16
17
18
# File 'lib/ruby_spriter/gimp_processor.rb', line 14

def initialize(gimp_path, options = {})
  @gimp_path = gimp_path
  @options = options
  @gimp_version = options[:gimp_version] || { major: 3, minor: 0 }  # Default to GIMP 3
end

Instance Attribute Details

#gimp_pathObject (readonly)

Returns the value of attribute gimp_path.



9
10
11
# File 'lib/ruby_spriter/gimp_processor.rb', line 9

def gimp_path
  @gimp_path
end

#gimp_versionObject (readonly)

Returns the value of attribute gimp_version.



9
10
11
# File 'lib/ruby_spriter/gimp_processor.rb', line 9

def gimp_version
  @gimp_version
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/ruby_spriter/gimp_processor.rb', line 9

def options
  @options
end

Instance Method Details

#execute_python_script(script, output_file) ⇒ Boolean

Execute a Python script with GIMP (used by ThresholdStepper)

Parameters:

  • script (String)

    The Python script content

  • output_file (String)

    Expected output file path

Returns:

  • (Boolean)

    True if successful, false otherwise



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ruby_spriter/gimp_processor.rb', line 59

def execute_python_script(script, output_file)
  script_file = File.join(Dir.tmpdir, "gimp_threshold_#{Time.now.to_i}_#{rand(10_000)}.py")
  log_file = File.join(Dir.tmpdir, "gimp_threshold_log_#{Time.now.to_i}_#{rand(10_000)}.txt")

  begin
    File.write(script_file, script)

    if options[:debug]
      Utils::OutputFormatter.indent("DEBUG: Threshold script: #{script_file}")
      Utils::OutputFormatter.indent("DEBUG: Expected output: #{output_file}")
    end

    # Execute using existing platform-specific methods
    if Platform.windows?
      execute_gimp_windows(script_file, log_file)
    else
      execute_gimp_unix(script_file, log_file)
    end

    # Check if output was created
    if File.exist?(output_file) && File.size(output_file).positive?
      true
    else
      if options[:debug]
        Utils::OutputFormatter.indent("WARNING: Threshold script did not produce output")
      end
      false
    end
  rescue StandardError => e
    if options[:debug]
      Utils::OutputFormatter.indent("ERROR in threshold script: #{e.message}")
    end
    false
  ensure
    cleanup_temp_files(script_file, log_file) unless options[:keep_temp]
  end
end

#process(input_file) ⇒ String

Process image with GIMP operations

Parameters:

  • input_file (String)

    Path to input image

Returns:

  • (String)

    Path to processed output file



23
24
25
26
27
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
# File 'lib/ruby_spriter/gimp_processor.rb', line 23

def process(input_file)
  Utils::FileHelper.validate_readable!(input_file)

  Utils::OutputFormatter.header("GIMP Processing")

  # Inform about Xvfb usage on Linux
  if Platform.linux?
    Utils::OutputFormatter.note("Using GIMP via Xvfb (virtual display - no GUI windows)")
  end

  # Inform user if automatic operation order optimization is applied
  if options[:scale_percent] && options[:remove_bg] &&
     options[:operation_order] == :scale_then_remove_bg
    Utils::OutputFormatter.note("Auto-optimized: Removing background before scaling for better quality")
  end

  working_file = input_file
  operations = determine_operations

  # Execute operations in configured order
  operations.each do |operation|
    working_file = send(operation, working_file)
  end

  # Apply sharpening at the very end, after all GIMP operations
  if options[:sharpen]
    working_file = apply_sharpen_imagemagick(working_file)
  end

  working_file
end