Created
November 8, 2013 23:14
-
-
Save jimfoltz/7379169 to your computer and use it in GitHub Desktop.
Ruby snippet to extract the thumbnail .png enbeded in a .skp (SketchUp) file.
This file contains 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
# with a lot of help from Adam on comp.lang.ruby | |
def extract_chunk(input, output) | |
lenword = input.read(4) | |
length = lenword.unpack('N')[0] | |
type = input.read(4) | |
data = length>0 ? input.read(length) : "" | |
crc = input.read(4) | |
return nil if length<0 || !(('A'..'z')===type[0,1]) | |
#return nil if validate_crc(type+data, crc) | |
output.write lenword | |
output.write type | |
output.write data | |
output.write crc | |
return type | |
end | |
def extract_png(input, output) | |
hdr = input.read(8) | |
raise "Not a PNG File" if hdr[0,4]!= "\211PNG" | |
raise "file not in binary mode" if hdr[4,4]!="\r\n\032\n" | |
output.write(hdr) | |
loop do | |
chunk_type = extract_chunk(input,output) | |
#p chunk_type | |
break if chunk_type.nil? || chunk_type == 'IEND' | |
end | |
end | |
files = Dir["*.skp"] | |
files.each do |f| | |
fp = File.new(f,"rb") | |
fn = File.basename(f, ".skp") | |
m = /\211PNG/.match(fp.read) | |
raise "no PNG" if !m | |
fp.seek(m.begin(0)) | |
ofp = File.new("#{fn}.png","wb") | |
extract_png(fp,ofp) | |
fp.close | |
ofp.close | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment