Skip to content

Instantly share code, notes, and snippets.

@Malinskiy
Last active May 11, 2020 04:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Malinskiy/8ef0ec543ba7859eb8e254c2117198da to your computer and use it in GitHub Desktop.
Save Malinskiy/8ef0ec543ba7859eb8e254c2117198da to your computer and use it in GitHub Desktop.
#!/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 " \\"
}
#!/usr/bin/env ruby -wKU
require "pp"
require 'fileutils'
require "gruff"
require 'rmagick'
data_segment = false
r = Array.new()
g = Array.new()
b = Array.new()
STDIN.read.split("\n").each do |line|
break if line == "END_DATA"
if data_segment
irgb = line.split(" ").map { |e| (Float(e)*4095).to_i }
r.push irgb[1]
g.push irgb[2]
b.push irgb[3]
end
data_segment = true if line == "BEGIN_DATA"
end
# gr = Gruff::Line.new
# gr.theme = Gruff::Themes::GREYSCALE
# gr.dot_style = :square
# gr.data("r",r)
# gr.y_axis_increment = 1
# gr.write("r.png")
#
# gr = Gruff::Line.new
# gr.theme = Gruff::Themes::GREYSCALE
# gr.dot_style = :square
# gr.data("g",g)
# gr.y_axis_increment = 1
# gr.write("g.png")
#
# gr = Gruff::Line.new
# gr.theme = Gruff::Themes::GREYSCALE
# gr.dot_style = :square
# gr.data("b",b)
# gr.y_axis_increment = 1
# gr.write("b.png")
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 " \\"
}
#!/usr/bin/env ruby -wKU
require "pp"
require 'fileutils'
require "gruff"
require 'rmagick'
gamma_table = Hash.new()
current_idx = nil
current_parameter = nil
def convert(measures)
real_measures = Array.new()
(0..127).each do |i|
x = measures[i * 3]
y = measures[i * 3 + 1]
z = measures[i * 3 + 2]
m1_lb = x & 0x0F
m2_lb = x & 0xF0 >> 4
m1_hb = y << 4
m2_hb = z << 4
m1 = m1_lb + m1_hb
m2 = m2_lb + m2_hb
real_measures.push m1
real_measures.push m2
end
m_last_lb = measures[384] & 0x0F
m_last_hb = measures[385] << 4
m_last = m_last_lb + m_last_hb
real_measures.push m_last
return real_measures
end
STDIN.read.split("\n").each do |line|
if line =~ /\[gamma_table_(\d+)\]/
current_idx = $1
gamma_table[current_idx] = Hash.new()
gamma_table[current_idx]["r"] = []
gamma_table[current_idx]["g"] = []
gamma_table[current_idx]["b"] = []
end
current_parameter = "r" if line.include? "parameter_r"
current_parameter = "g" if line.include? "parameter_g"
current_parameter = "b" if line.include? "parameter_b"
if line.include? "0x"
line.gsub!(" ", "")
line.gsub!("\\", "")
line.gsub!("\r", "")
measures = line.split(",")
gamma_table[current_idx][current_parameter] += measures.map { |e| Integer(e) }.reject{|i| i.nil? }
end
end
panel = ARGV[0]
FileUtils.rm_rf("#{panel}")
Dir.mkdir "#{panel}"
gamma_table.each do |k,v|
image_list = []
["r","g","b"].each do |channel|
g = Gruff::Scatter.new
g.theme = Gruff::Themes::GREYSCALE
g.data("#{panel}/#{k}-#{channel}",(1..convert(v[channel]).size).to_a,convert(v[channel]))
g.write("#{panel}/#{k}-#{channel}.png")
image_list.push "#{panel}/#{k}-#{channel}.png"
end
Magick::ImageList.new(*image_list).append(false).write "#{panel}/#{k}.png"
puts "Writing to #{panel}/#{k}.png "
image_list.each { |img| FileUtils.rm img }
end
#!/usr/bin/env ruby -wKU
require "pp"
require 'fileutils'
require "gruff"
require 'rmagick'
data_segment = false
r = Array.new()
g = Array.new()
b = Array.new()
STDIN.read.split("\n").each do |line|
break if line == "END_DATA"
if data_segment
irgb = line.split(" ").map { |e| (Float(e)*4095).to_i }
r.push irgb[1]
g.push irgb[2]
b.push irgb[3]
end
data_segment = true if line == "BEGIN_DATA"
end
# gr = Gruff::Line.new
# gr.theme = Gruff::Themes::GREYSCALE
# gr.dot_style = :square
# gr.data("r",r)
# gr.y_axis_increment = 1
# gr.write("r.png")
#
# gr = Gruff::Line.new
# gr.theme = Gruff::Themes::GREYSCALE
# gr.dot_style = :square
# gr.data("g",g)
# gr.y_axis_increment = 1
# gr.write("g.png")
#
# gr = Gruff::Line.new
# gr.theme = Gruff::Themes::GREYSCALE
# gr.dot_style = :square
# gr.data("b",b)
# gr.y_axis_increment = 1
# gr.write("b.png")
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 " \\"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment