def image(file, options={})
Prawn.verify_options [:at, :position, :vposition, :height,
:width, :scale, :fit], options
if file.respond_to?(:read)
image_content = file.read
else
raise ArgumentError, "#{file} not found" unless File.file?(file)
image_content = File.binread(file)
end
image_sha1 = Digest::SHA1.hexdigest(image_content)
proc_set :ImageC
if image_registry[image_sha1]
info = image_registry[image_sha1][:info]
image_obj = image_registry[image_sha1][:obj]
else
image_obj = case detect_image_format(image_content)
when :jpg then
info = Prawn::Images::JPG.new(image_content)
build_jpg_object(image_content, info)
when :png then
info = Prawn::Images::PNG.new(image_content)
build_png_object(image_content, info)
end
image_registry[image_sha1] = {:obj => image_obj, :info => info}
end
w,h = calc_image_dimensions(info, options)
if options[:at]
x,y = translate(options[:at])
else
x,y = image_position(w,h,options)
move_text_position h
end
label = "I#{next_image_id}"
page_xobjects.merge!( label => image_obj )
instruct = "\nq\n%.3f 0 0 %.3f %.3f %.3f cm\n/%s Do\nQ"
add_content instruct % [ w, h, x, y - h, label ]
return info
end