Class: Pura::Ico::Encoder
- Inherits:
-
Object
- Object
- Pura::Ico::Encoder
- Defined in:
- lib/pura/ico/encoder.rb
Class Method Summary collapse
Instance Method Summary collapse
- #encode ⇒ Object
-
#initialize(images) ⇒ Encoder
constructor
A new instance of Encoder.
Constructor Details
#initialize(images) ⇒ Encoder
Returns a new instance of Encoder.
14 15 16 |
# File 'lib/pura/ico/encoder.rb', line 14 def initialize(images) @images = images end |
Class Method Details
.encode(images, output_path) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/pura/ico/encoder.rb', line 6 def self.encode(images, output_path) images = [images] unless images.is_a?(Array) encoder = new(images) data = encoder.encode File.binwrite(output_path, data) data.bytesize end |
Instance Method Details
#encode ⇒ Object
18 19 20 21 22 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 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/pura/ico/encoder.rb', line 18 def encode require "pura-png" count = @images.size # Encode each image as PNG data png_blobs = @images.map { |img| encode_png_blob(img) } # Calculate offsets # Header: 6 bytes # Directory entries: 16 bytes each header_size = 6 + (16 * count) offsets = [] current_offset = header_size png_blobs.each do |blob| offsets << current_offset current_offset += blob.bytesize end out = String.new(encoding: Encoding::BINARY, capacity: current_offset) # ICO header out << [0, 1, count].pack("v3") # reserved=0, type=1 (ICO), count # Directory entries @images.each_with_index do |img, i| w = img.width >= 256 ? 0 : img.width h = img.height >= 256 ? 0 : img.height out << [ w, # width (0 = 256) h, # height (0 = 256) 0, # color count (0 for >= 256 colors) 0, # reserved 1, # color planes 32 # bits per pixel ].pack("C4v2") out << [ png_blobs[i].bytesize, # data size offsets[i] # data offset ].pack("V2") end # Image data (PNG blobs) png_blobs.each { |blob| out << blob } out end |