Class: RubySpriter::SmokeDetector

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

Overview

SmokeDetector identifies and optionally removes smoke-like transparency gradients Detects alpha values between 20-80% in contiguous regions

Constant Summary collapse

MIN_ALPHA =

Smoke detection thresholds

0.2
MAX_ALPHA =

20% - minimum alpha for smoke

0.8
MIN_AREA =

80% - maximum alpha for smoke

50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_image, output_image, config) ⇒ SmokeDetector

Minimum contiguous area in pixels



18
19
20
21
22
23
24
# File 'lib/ruby_spriter/smoke_detector.rb', line 18

def initialize(input_image, output_image, config)
  @input_image = input_image
  @output_image = output_image
  @config = config
  @smoke_regions = []
  @processing_time = 0
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/ruby_spriter/smoke_detector.rb', line 10

def config
  @config
end

#input_imageObject (readonly)

Returns the value of attribute input_image.



10
11
12
# File 'lib/ruby_spriter/smoke_detector.rb', line 10

def input_image
  @input_image
end

#output_imageObject (readonly)

Returns the value of attribute output_image.



10
11
12
# File 'lib/ruby_spriter/smoke_detector.rb', line 10

def output_image
  @output_image
end

#processing_timeObject (readonly)

Returns the value of attribute processing_time.



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

def processing_time
  @processing_time
end

#smoke_regionsObject (readonly)

Returns the value of attribute smoke_regions.



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

def smoke_regions
  @smoke_regions
end

Instance Method Details

#detectObject

Detect smoke-like transparency gradients



55
56
57
58
59
60
61
62
63
64
# File 'lib/ruby_spriter/smoke_detector.rb', line 55

def detect
  regions = []

  # Use ImageMagick to analyze alpha channel

  # Find regions with alpha in the smoke range (20-80%)

  regions = find_smoke_regions

  # Filter by minimum area

  regions.select { |r| r[:area] >= MIN_AREA }
end

#is_smoke_like?(alpha) ⇒ Boolean

Check if alpha value is smoke-like

Returns:

  • (Boolean)


78
79
80
# File 'lib/ruby_spriter/smoke_detector.rb', line 78

def is_smoke_like?(alpha)
  alpha >= MIN_ALPHA && alpha <= MAX_ALPHA
end

#processObject

Main processing method



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
# File 'lib/ruby_spriter/smoke_detector.rb', line 27

def process
  start_time = Time.now

  # Validate input file exists

  unless File.exist?(@input_image)
    warn "SmokeDetector: Input image does not exist: #{@input_image}"
    @processing_time = Time.now - start_time
    return false
  end

  # Always detect smoke (for reporting)

  @smoke_regions = detect

  # Remove smoke if configured

  if @config.remove_smoke
    FileUtils.cp(@input_image, @output_image)
    remove_smoke_regions(@smoke_regions)
  else
    # Just copy input to output

    FileUtils.cp(@input_image, @output_image)
  end

  @processing_time = Time.now - start_time

  true
end

#remove_smoke_regions(regions) ⇒ Object

Remove detected smoke regions



67
68
69
70
71
72
73
74
75
# File 'lib/ruby_spriter/smoke_detector.rb', line 67

def remove_smoke_regions(regions)
  return true if regions.empty?

  # Apply smoke removal to the entire image

  # Remove all pixels with alpha in the smoke range

  remove_smoke_pixels

  true
end

#reportObject

Generate detection report



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ruby_spriter/smoke_detector.rb', line 83

def report
  {
    smoke_detected: @smoke_regions.length,
    smoke_removed: @config.remove_smoke,
    smoke_regions: @smoke_regions.map do |r|
      {
        x: r[:x],
        y: r[:y],
        area: r[:area],
        alpha_range: r[:alpha_range]
      }
    end,
    processing_time: @processing_time.round(3),
    min_alpha_threshold: MIN_ALPHA,
    max_alpha_threshold: MAX_ALPHA,
    min_area_threshold: MIN_AREA
  }
end