Class: RubySpriter::ThresholdStepper

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

Overview

ThresholdStepper applies multiple threshold-based background removal passes using GIMP Python-fu with edge-sampled background colors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_file, output_file, background_palette, gimp_processor, options = {}) ⇒ ThresholdStepper

Returns a new instance of ThresholdStepper.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby_spriter/threshold_stepper.rb', line 13

def initialize(input_file, output_file, background_palette, gimp_processor, options = {})
  @input_file = input_file
  @output_file = output_file
  @background_palette = background_palette
  @gimp_processor = gimp_processor
  @options = options
  @threshold_values = parse_threshold_values(options[:threshold_values])
  @threshold_timeout = options[:threshold_timeout] || 60
  @total_timeout = options[:total_threshold_timeout] || 300
  @thresholds_processed = 0
  @skipped_thresholds = 0
  @start_time = nil
  @end_time = nil
end

Instance Attribute Details

#background_paletteObject (readonly)

Returns the value of attribute background_palette.



11
12
13
# File 'lib/ruby_spriter/threshold_stepper.rb', line 11

def background_palette
  @background_palette
end

#gimp_processorObject (readonly)

Returns the value of attribute gimp_processor.



11
12
13
# File 'lib/ruby_spriter/threshold_stepper.rb', line 11

def gimp_processor
  @gimp_processor
end

#input_fileObject (readonly)

Returns the value of attribute input_file.



11
12
13
# File 'lib/ruby_spriter/threshold_stepper.rb', line 11

def input_file
  @input_file
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/ruby_spriter/threshold_stepper.rb', line 11

def options
  @options
end

#output_fileObject (readonly)

Returns the value of attribute output_file.



11
12
13
# File 'lib/ruby_spriter/threshold_stepper.rb', line 11

def output_file
  @output_file
end

Instance Method Details

#processObject



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruby_spriter/threshold_stepper.rb', line 28

def process
  @start_time = Time.now
  temp_results = []

  begin
    Timeout.timeout(@total_timeout) do
      @threshold_values.each_with_index do |threshold, index|
        temp_output = File.join(Dir.tmpdir, "threshold_#{threshold}_#{Time.now.to_i}_#{index}.png")

        begin
          Timeout.timeout(@threshold_timeout) do
            script = generate_gimp_script(threshold, temp_output)

            if @gimp_processor.execute_python_script(script, temp_output)
              temp_results << temp_output
              @thresholds_processed += 1
            else
              @skipped_thresholds += 1
              log_debug "Threshold #{threshold} failed to process"
            end
          end
        rescue Timeout::Error
          @skipped_thresholds += 1
          log_debug "Threshold #{threshold} timed out after #{@threshold_timeout}s"
        rescue StandardError => e
          @skipped_thresholds += 1
          log_debug "Threshold #{threshold} error: #{e.message}"
        end
      end
    end
  rescue Timeout::Error
    log_debug "Total threshold stepping timed out after #{@total_timeout}s"
  end

  @end_time = Time.now

  # Composite all threshold results
  if temp_results.any?
    composite_results(temp_results)
  else
    # Fallback: copy input to output if no thresholds succeeded
    FileUtils.cp(@input_file, @output_file)
  end

  # Cleanup temp files
  temp_results.each { |f| File.delete(f) if File.exist?(f) }
end

#reportObject



76
77
78
79
80
81
82
# File 'lib/ruby_spriter/threshold_stepper.rb', line 76

def report
  {
    thresholds_processed: @thresholds_processed,
    skipped_thresholds: @skipped_thresholds,
    total_time: @end_time && @start_time ? (@end_time - @start_time).round(2) : 0
  }
end