Created
April 1, 2012 06:13
-
-
Save koyachi/2271942 to your computer and use it in GitHub Desktop.
http://perfume-dev.github.com/ から得られるBVHの数値を適当に色にした
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
require "zlib" | |
require "pathname" | |
class PNG | |
def initialize(width, height, pixel_lines) | |
@width = width | |
@height = height | |
@depth = 8 | |
@color_type = 2 | |
@pixel_lines = pixel_lines | |
end | |
def lines | |
end | |
def save(path) | |
open(Pathname.new(path).expand_path, "w") do |f| | |
# ファイルシグニチャ | |
f.write "\x89PNG\r\n\x1a\n" | |
# ヘッダ | |
f.write chunk("IHDR", [@width, @height, 8, 2, 0, 0, 0].pack("NNCCCCC")) | |
# 画像データ | |
img_data = @pixel_lines.map {|line| ([0] + line.flatten).pack("C*")}.join | |
f.write chunk("IDAT", Zlib::Deflate.deflate(img_data)) | |
# 終端 | |
f.write chunk("IEND", "") | |
end | |
end | |
private | |
def chunk(type, data) | |
[data.bytesize, type, data, Zlib.crc32(type + data)].pack("NA4A*N") | |
end | |
end | |
tet = <<-TEST | |
width = 200 | |
height = 100 | |
pixel_lines = [] | |
0.upto(height -1) do |y| | |
line = [] | |
0.upto(width - 1) do |x| | |
line.push([(255 / width.to_f) * x, 0, 0]) | |
end | |
pixel_lines.push(line) | |
end | |
png = PNG.new(width, height, pixel_lines) | |
png.save("./test.png") | |
TEST | |
module KludgyBVHParser | |
class << self | |
def parse(path, proc) | |
pixel_lines = [] | |
open(Pathname.new(path).expand_path, "r") do |f| | |
skip = true | |
f.each do |line| | |
if line.match(/^Frame Time:.*/) | |
skip = false | |
next | |
end | |
unless skip | |
values = line.split(" ").map{|v| v.to_f} | |
pixel_lines.push(proc.call(values)) | |
end | |
end | |
end | |
pixel_lines | |
end | |
end | |
end | |
$last_values = [] | |
frame_proc = Proc.new do |values| | |
pixels = [] | |
if $last_values.length == 0 | |
values.each_with_index do |v,i| | |
rgb = [0, 0, 0] | |
pixels.push(rgb) | |
end | |
else | |
value_range = values.max - values.min | |
values.each_with_index do |v,i| | |
corrected_value = ((v + (-values.min)) / value_range * 255).to_i | |
rgb = [corrected_value, 0, 0] | |
pixels.push(rgb) | |
end | |
end | |
$last_values = values | |
pixels | |
end | |
path = "../data.org/bvhfiles/aachan.bvh" | |
pixel_lines = KludgyBVHParser.parse(path, frame_proc) | |
height = pixel_lines.length | |
width = pixel_lines[0].length | |
png = PNG.new(width, height, pixel_lines) | |
file_id = path.split("/")[-1] | |
png.save("./frame__#{file_id}.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment