Class: RubySpriter::CompressionManager

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

Overview

Manages PNG compression with metadata preservation

Class Method Summary collapse

Class Method Details

.compress(input_file, output_file, debug: false) ⇒ Object

Compress PNG file using ImageMagick with maximum compression

Parameters:

  • input_file (String)

    Source PNG file

  • output_file (String)

    Destination PNG file

  • debug (Boolean) (defaults to: false)

    Enable debug output



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

def self.compress(input_file, output_file, debug: false)
  Utils::FileHelper.validate_readable!(input_file)

  cmd = build_compression_command(input_file, output_file)

  if debug
    Utils::OutputFormatter.indent("DEBUG: Compression command: #{cmd}")
  end

  stdout, stderr, status = Open3.capture3(cmd)

  unless status.success?
    raise ProcessingError, "Failed to compress PNG: #{stderr}"
  end

  Utils::FileHelper.validate_exists!(output_file)
end

.compress_with_metadata(input_file, output_file, debug: false) ⇒ Object

Compress PNG file while preserving embedded metadata

Parameters:

  • input_file (String)

    Source PNG file

  • output_file (String)

    Destination PNG file

  • debug (Boolean) (defaults to: false)

    Enable debug output



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

def self.(input_file, output_file, debug: false)
  # Read metadata before compression
   = MetadataManager.read(input_file)

  # Compress the file
  temp_file = output_file.gsub('.png', '_compress_temp.png')
  compress(input_file, temp_file, debug: debug)

  # Re-embed metadata if it existed
  if 
    MetadataManager.embed(
      temp_file,
      output_file,
      columns: [:columns],
      rows: [:rows],
      frames: [:frames],
      debug: debug
    )

    # Clean up temp file
    FileUtils.rm_f(temp_file) if File.exist?(temp_file)
  else
    # No metadata, just move temp to output
    FileUtils.mv(temp_file, output_file)
  end
end

.compression_stats(original_file, compressed_file) ⇒ Hash

Get compression statistics

Parameters:

  • original_file (String)

    Original file path

  • compressed_file (String)

    Compressed file path

Returns:

  • (Hash)

    Statistics including sizes and reduction percentage



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruby_spriter/compression_manager.rb', line 66

def self.compression_stats(original_file, compressed_file)
  original_size = File.size(original_file)
  compressed_size = File.size(compressed_file)
  saved_bytes = original_size - compressed_size
  reduction_percent = (saved_bytes.to_f / original_size * 100.0)

  {
    original_size: original_size,
    compressed_size: compressed_size,
    saved_bytes: saved_bytes,
    reduction_percent: reduction_percent
  }
end