#!/usr/bin/env ruby # # A ruby program to convert a file to images. # # author: Torsten Tränkner # license: GPL version 3 # class FileToImagesConverter require 'fileutils' require 'base64' require 'digest/sha1' VERSION="0.1" # returns the version def version return VERSION end # JPG header HEADER="FF D8 FF E0 00 10 4A 46 49 46 00 01 01 01 00 48 00 48 00 00" # JPG footer FOOTER="FF DB 00 43 00 03 02 02 03 02 02 03 03 03 03 04 03 03 04 05 08 05 05 04 04 05 0A 07 07 06 08 0C 0A 0C 0C 0B 0A 0B 0B 0D 0E 12 10 0D 0E 11 0E 0B 0B 10 16 10 11 13 14 15 15 15 0C 0F 17 18 16 14 18 12 14 15 14 FF DB 00 43 01 03 04 04 05 04 05 09 05 05 09 14 0D 0B 0D 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 FF C2 00 11 08 00 0A 00 0A 03 01 11 00 02 11 01 03 11 01 FF C4 00 16 00 01 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 08 FF C4 00 14 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF DA 00 0C 03 01 00 02 10 03 10 00 00 01 D4 85 40 3F FF C4 00 16 10 01 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 03 02 10 FF DA 00 08 01 01 00 01 05 02 22 80 2D FF C4 00 14 11 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20 FF DA 00 08 01 03 01 01 3F 01 1F FF C4 00 14 11 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20 FF DA 00 08 01 02 01 01 3F 01 1F FF C4 00 1A 10 00 03 00 03 01 00 00 00 00 00 00 00 00 00 00 00 01 02 03 11 12 20 51 FF DA 00 08 01 01 00 06 3F 02 49 CD 16 73 41 AA A2 8C 00 3C E3 FF C4 00 18 10 01 00 03 01 00 00 00 00 00 00 00 00 00 00 00 00 01 11 20 31 41 FF DA 00 08 01 01 00 01 3F 21 3C C5 14 31 00 0C 03 94 FF DA 00 0C 03 01 00 02 00 03 00 00 00 10 12 4F FF C4 00 14 11 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20 FF DA 00 08 01 03 01 01 3F 10 1F FF C4 00 14 11 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20 FF DA 00 08 01 02 01 01 3F 10 1F FF C4 00 15 10 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 20 FF DA 00 08 01 01 00 01 3F 10 07 EA CD 70 B0 00 00 00 00 23 FF D9" # maximum number of comments per image file MAXIMUM_COMMENTS = 30 # size of one image comment CHUNK_SIZE = 65520 # # converts a given file to (several) images # in the subdirectory "parts.filename" # def self.convert(filename) # puts "Creating images for: #{filename}" # create a directory for the images directory="parts.#{filename}" if File.directory?(directory) FileUtils.remove_dir(directory) end FileUtils.mkdir(directory) # get base64 encoded string of the file base64String="" File.open(filename, 'r') do |openedFile| base64String=Base64.strict_encode64(openedFile.read) end sha1sumOfBase64String = Digest::SHA1.hexdigest(base64String) imageIndex=0 numberOfChunks=(Float(base64String.length) / CHUNK_SIZE).ceil numberOfImages=(Float(numberOfChunks) / MAXIMUM_COMMENTS).ceil # puts "#{base64String.length}, numberOfChunks: #{numberOfChunks}, numberOfImages: #{numberOfImages}" numberOfImages.times do |imageIndex| imageFilename="image%03i.jpg"%imageIndex #puts "imageFilename: #{imageFilename}" File.open(File.join(directory, imageFilename), 'w') do |openedFile| openedFile.write([HEADER.delete(" ")].pack("H*")) MAXIMUM_COMMENTS.times do |partIndex| partStart=(MAXIMUM_COMMENTS * CHUNK_SIZE * imageIndex) + partIndex * CHUNK_SIZE if partStart >= base64String.length next end partEnd=(MAXIMUM_COMMENTS * CHUNK_SIZE * imageIndex) + (partIndex + 1) * CHUNK_SIZE if partEnd >= base64String.length partEnd=base64String.length end #puts "partStart: #{partStart}, partEnd: #{partEnd}" commentSize = partEnd - partStart + 6 # write the comment openedFile.write("\xff\xfe") # comment size as binary openedFile.write("%c"%(commentSize/256)+"%c"%(commentSize%256)) openedFile.write("%%") openedFile.write(base64String[partStart..partEnd-1]) openedFile.write("%%") end openedFile.write([FOOTER.delete(" ")].pack("H*")) end # prepare the link file File.open(File.join(directory, "linkfile.txt"), 'w') do |openedFile| openedFile.write("// put your upload links here:\n") numberOfImages.times do openedFile.write("l=\n") end openedFile.write("\nf=#{filename}\n\n") openedFile.write("// SHA1 checksum:\n") openedFile.write("c=#{sha1sumOfBase64String}\n") openedFile.write("a=\n") end end end end if __FILE__ == $0 if ARGV.length == 1 FileToImagesConverter.convert(ARGV[0]) else puts "A script to convert a file to images." puts "Version #{FileToImagesConverter.version}\n\n" puts "Usage:\nruby #{$0} " end end