Module: DoubleVision
- Defined in:
- lib/doubleVision.rb,
lib/doubleVision/version.rb
Overview
Manipulates PNG gamma metadata to create images that display differently when rendered in different clients.
Can be used to create images that display differently as thumbnails than when they are clicked on sites like facebook.
Constant Summary collapse
- VERSION =
"0.0.1"
Class Method Summary collapse
-
.createImage(img1, img2, options = {}) ⇒ Object
Takes to ChunkyPNG::Image and returns the chunkypng datastream of an image that displays img1 when rendered with gamma support and img2 without it.
-
.createImageFiles(file1, file2, outFile, options = {}) ⇒ Object
Creates a magic image from two png files of the same size.
-
.trans(num, options) ⇒ Object
turns a color component from 0-255 into a new color for the firefox image.
Class Method Details
.createImage(img1, img2, options = {}) ⇒ Object
Takes to ChunkyPNG::Image and returns the chunkypng datastream of an image that displays img1 when rendered with gamma support and img2 without it.
Will throw an ArgumentError if the images do not have the same dimensions.
Options is a hash containing various parameters
- :gamma
-
is the gamma value to use. This should be a very low value.
- :fade1
-
is the amount to fade the first image. Default is 220/255.0
- :fade2
-
is the amount to fade the second image. Default is 210/255.0
- :shift
-
is the amount to shift up the colours of the second image. Default is 10.
The fade and shift parameters are required to properly segment the colors so the algorithm works.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/doubleVision.rb', line 29 def DoubleVision.createImage(img1,img2, = {}) [:gamma] ||= 0.023 [:fade1] ||= 220/255.0 [:fade2] ||= 210/255.0 [:shift] ||= 10 if img1.dimension != img2.dimension raise ArgumentError, "Image sizes must have the same dimensions." end out = ChunkyPNG::Image.new(img1.dimension.width*2, img1.dimension.height*2,ChunkyPNG::Color::BLACK) out.dimension.width.times do |x| out.dimension.height.times do |y| if x % 2 == 0 && y % 2 == 0 col = img1[x/2,y/2] r = trans(ChunkyPNG::Color.r(col),) g = trans(ChunkyPNG::Color.g(col),) b = trans(ChunkyPNG::Color.b(col),) out[x,y] = ChunkyPNG::Color.rgb(r,g,b) else col = img2[x/2,y/2] r = (ChunkyPNG::Color.r(col) * [:fade2]).round g = (ChunkyPNG::Color.g(col) * [:fade2]).round b = (ChunkyPNG::Color.b(col) * [:fade2]).round out[x,y] = ChunkyPNG::Color.rgb(r,g,b) end end end pngGamma = ([:gamma]*100000).to_i # turn the integer into a 4 byte big-endian unsigned int byte string bytestr = [pngGamma].pack("L>") # The chunk is named gAMA because the PNG spec is weird chunk = ChunkyPNG::Chunk::Generic.new("gAMA",bytestr) ds = out.to_datastream ds.other_chunks << chunk ds end |
.createImageFiles(file1, file2, outFile, options = {}) ⇒ Object
Creates a magic image from two png files of the same size. Takes two filenames and saves the result in outFile.
Will throw an ArgumentError if the images do not have the same dimensions.
73 74 75 76 77 78 |
# File 'lib/doubleVision.rb', line 73 def DoubleVision.createImageFiles(file1,file2,outFile, = {}) img1 = ChunkyPNG::Image.from_file(file1) img2 = ChunkyPNG::Image.from_file(file2) out = createImage(img1,img2,) out.save(outFile) end |
.trans(num, options) ⇒ Object
turns a color component from 0-255 into a new color for the firefox image
12 13 14 15 |
# File 'lib/doubleVision.rb', line 12 def DoubleVision.trans(num,) scaled = num*[:fade1] + [:shift] (((scaled/255.0)**([:gamma]))*255.0).floor end |