#! /usr/local/bin/ruby -w
require 'RMagick'

# Demonstrate the Image#stegano method.

# Create a small watermark from the Snake image by
# shrinking it and converting it to B&W.
watermark = Magick::Image.read('images/Snake.wmf').first
watermark = watermark.scale(64.0/watermark.rows)
watermark = watermark.quantize(256, Magick::GRAYColorspace)
wmrows = watermark.rows
wmcols = watermark.columns

# Read the image in which we'll hide the watermark.
jj = Magick::Image.read('images/Jean_Jacket.jpg').first
jj = jj.scale(250.0/jj.rows)

# Embed the watermark starting at offset 91.
puts 'Embedding watermark...'
stegano = jj.stegano(watermark, 91)

# Write the watermarked image in PNG format. Note that
# the format must be lossless - Image#stegano doesn't work
# with lossy formats like JPEG.
stegano.write('jj.png')

# Read the image and retrieve the watermark. The size
# attribute describes the size and offset of the watermark.

puts 'Retrieving watermark. This may take a few seconds...'
stegano = Magick::Image.read('stegano:jj.png') {
    self.size = "#{wmcols}x#{wmrows}+91"
}

# We don't need this any more.
File.delete('jj.png')

#stegano[0].display
stegano[0].write('stegano.gif')
exit