class Jpegsan::Jpeg

Constants

JPEG_MARKERS

Table B.1 in www.w3.org/Graphics/JPEG/itu-t81.pdf

JpegMarker
MARKER_BY_SUFFIX

Attributes

data[RW]

Public Class Methods

new(file) click to toggle source
# File lib/jpegsan/jpeg.rb, line 95
def initialize(file)
  @data = []

  items = []
  marker = false
  current_marker = nil

  file.read.chars.each do |e|
    if marker
      marker = false
      # "X'FF00' is NOT a marker but X'FF' value"
      if e == ?\x00
        items << ?\xff
        items << ?\x00
        next
      end

      # "X'FFFF' is treated as is
      if e == ?\xff
        items << ?\xff
        items << ?\xff
        next
      end

      if !items.empty?
        current_marker.data_sequence = items.join.b
        items.clear
      end

      current_marker = MARKER_BY_SUFFIX[e].dup
      if current_marker.nil?
        current_marker = JpegMarker.new(e, 'UNK', 'Unknown marker')
      end

      @data << current_marker
      next
    end

    if e == ?\xff
      marker = true
      next
    end
    items << e
  end

  if !items.empty?
    @data << items.join
  end
end

Public Instance Methods

open_file() click to toggle source
# File lib/jpegsan/jpeg.rb, line 150
def open_file
  if @last_filename.nil?
    STDERR.puts('WARN: No file to open')
    return
  end

  system('open', @last_filename)
end
save(dest = 'out.jpg') click to toggle source
# File lib/jpegsan/jpeg.rb, line 145
def save(dest = 'out.jpg')
  @last_filename = dest
  open(dest, 'w').write(@data.map(&:to_bytes).join)
end
show_list() click to toggle source
# File lib/jpegsan/jpeg.rb, line 159
def show_list
 @data.each do |e|
   STDERR.puts "= MARKER #{e.code_suffix.b.inspect}: #{e&.symbol} (#{e&.description}) ="
   if e.sequence_in_hex
     STDERR.puts e.sequence_in_hex
   end
 end

 nil
end