#!/usr/bin/env ruby -wKU require "pp" require 'fileutils' require "gruff" require 'rmagick' def read_cal(file) data_segment = false correction_r = Array.new() correction_g = Array.new() correction_b = Array.new() File.readlines(file).each do |line| break if line == "END_DATA\n" if data_segment irgb = line.split(" ").map { |e| (Float(e)*4095).to_i } correction_r.push irgb[1] correction_g.push irgb[2] correction_b.push irgb[3] end data_segment = true if line == "BEGIN_DATA\n" end return correction_r, correction_g, correction_b end def normalize(i) return 0xFF * i / 0xFFF end def correct(x, cx) return cx.each_with_index.map { |value, i| normalised_value = normalize(value) puts "Mapping value #{i} -> #{normalised_value} -> #{x[normalised_value]}" x[normalised_value] } end r,g,b = read_cal(ARGV[0]) (1..(ARGV.size-1)).each { |fi| puts "Correction pass #{fi}" cr,cg,cb = read_cal(ARGV[fi]) r = correct(r, cr) g = correct(g, cg) b = correct(b, cb) } def encode(measures) # puts "Input is #{measures.size} long" out = Array.new() # p measures m2_lb = 0 m2_hb = 0 (0..127).each do |i| x = 0xFF * measures[i * 2] / 0x0FFF y = 0xFF * measures[i * 2 + 1] / 0x0FFF # puts sprintf("Encoding 0x%02X and 0x%02X", x, y) m1_lb = x & 0x0F m1_hb = x & 0xFF0 >> 4 m2_lb = y & 0x0F m2_hb = y & 0xFF0 >> 4 a = ((m2_lb << 4) + (m1_lb)) b = m1_hb c = m2_hb out.push a out.push b out.push c # puts sprintf("Result: 0x%02X, 0x%02X, 0x%02X", a, b, c) end out[384] = 0x0 out[385] = 0xFF return out end [r, g, b].each { |c| encoded = encode(c) (0..41).each { |x| (0..8).each { |y| print "0x" + sprintf("%02x,", encoded[x*9 + y]).upcase } puts " \\" } (0..7).each { |y| print "0x" + sprintf("%02x,", encoded[378 + y]).upcase } puts " \\" }