DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Use Base64 For Embedding Raster Data In Source File

02.08.2012
| 3320 views |
  • submit to reddit
        When we create a simple script with gui, it should be nice to puts the icons in the source script,
so whe can diffuse the code without any packaging.

This code create a ruby source which declare some file-content coded in ASCII/.base64
In the source, he put get_icon_filename(name) which put content to a temporary file and return
the filename created :

# usage: ruby gene.rb ../angel.png  face_crying.png face_smile_big.png > icons64.rb
require 'base64'
require 'tmpdir'

str=<<EEND
require 'base64'
require 'tmpdir'
$icons={}
EEND

lvar=[]
ARGV.each { |fn|
 varname=File.basename(fn).split('.')[0].gsub(/[^a-zA-Z0-9]+/,"_").downcase
 File.open(fn,"rb") { |f|   
     str+= "\n$icons['#{varname}']=<<EEND\n"+Base64.encode64(f.read)+"EEND\n" }
     lvar << varname
}
str+=<<'EEND'

def get_icon_filename(name)
  raise("icon '#{name}' unknown in #{$icons.keys}") unless $icons[name]
  fname=File.join(Dir.tmpdir,name+".png")
  puts "#{name} ==> #{fname} / #{$icons[name].size}" if $DEBUG
  File.open(fname,"wb") { |f| f.write($icons[name].unpack('m')) } unless File.exists?(fname)
  fname
end
EEND

puts str

STDERR.puts "#{lvar.join(", ")} done size=#{str.size}."

This generate a code like :
require 'base64'
require 'tmpdir'
$icons={}

$icons['angel']=<<EEND
iVBORw0KGgoAAAANSUhEUgAAAFAAAAB9CAYAAAA1I+RFAAAABHNCSVQICAgI
....
Hc7GYuTuE23+EVbG/wE7dk77N6cYpQAAAABJRU5ErkJggg==
EEND

$icons['face_crying']=<<EEND
iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgI
fAhkiAAAAAlwSFlzAAAN1wAADdcBQiibeAAAABl0RVh0U29mdHdhcmUAd3d3
...
31Ubdjyxtik75Th+55rrWLbzbO1ULTcJrJnruL1TMiGArs18rf0vPLcG1l08
LtAAAAAASUVORK5CYII=
EEND

$icons['face_smile_big']=<<EEND
iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgI
.....
IQlXZwKuzrjtNLc0aDs1ABcd0k6ypVidf+6Owx6Eu22ZAAAAAElFTkSuQmCC
EEND

def get_icon_filename(name)
  raise("icon '#{name}' unknown in #{$icons.keys}") unless $icons[name]
  fname=File.join(Dir.tmpdir,name+".png")
  puts "#{name} ==> #{fname} / #{$icons[name].size}" if $DEBUG
  File.open(fname,"wb") { |f| f.write($icons[name].unpack('m')) } unless File.exists?(fname)
  fname
end

Here a little Shoes app for use raster content
require 'green_shoes'

<< "contents of 'icons64.rb' here" >>

Shoes.app do
  stack {
	background "#A0A0B0".."#A0A0FF"
	$icons.keys.each { |name|
			para name
			image get_icon_filename(name)
	}
  }
end


Or a Ruiby app (see https://github.com/raubarede/Ruiby):

require_relative 'Ruiby/lib/ruiby.rb'
<< "contents of 'icons64.rb' here" >>

class X < Ruiby_gtk
	def component()
	  stack do
		$icons.keys.each do |n| 
			flow { 
                           slot(label(n))
                           slot(label("#"+get_icon_filename(n) )) 
			}
		end
	  end
	end
end
Ruiby.start { X.new("test",100,100) }