2015-02-23 22:29:18 +01:00
|
|
|
require "rubella/output/base"
|
2015-10-22 12:28:12 +02:00
|
|
|
require 'rmagick'
|
2015-02-18 23:45:53 +01:00
|
|
|
|
2015-02-17 22:28:31 +01:00
|
|
|
module Rubella
|
|
|
|
module Output
|
|
|
|
|
2015-02-24 20:57:56 +01:00
|
|
|
# Creates a pixel based graphic as representation of the given storage
|
|
|
|
# data using ImageMagick.
|
|
|
|
# By the RMagick extention. @data will contain a Magick image for futher
|
|
|
|
# handling the data please read the RMagick library documentation:
|
|
|
|
# http://www.imagemagick.org/RMagick/doc/
|
|
|
|
#
|
2015-02-23 22:29:18 +01:00
|
|
|
class Image < Base
|
2015-02-18 23:45:53 +01:00
|
|
|
|
2015-02-24 20:57:56 +01:00
|
|
|
# Constructor
|
|
|
|
# Has a default field_size of 15 pixel.
|
|
|
|
#
|
2015-04-25 17:02:39 +02:00
|
|
|
# @param data Rubella::Storage
|
2015-02-24 20:57:56 +01:00
|
|
|
# @param field_size int How many pixel one value has
|
|
|
|
# @return Rubella::Output::Image
|
|
|
|
#
|
2015-04-25 17:02:39 +02:00
|
|
|
def initialize data, field_size = 15
|
|
|
|
super data, field_size
|
2015-02-24 20:57:56 +01:00
|
|
|
end
|
|
|
|
|
2015-04-25 17:02:39 +02:00
|
|
|
# Creates a pixel based graphic.
|
2015-02-24 20:57:56 +01:00
|
|
|
#
|
2015-04-25 17:02:39 +02:00
|
|
|
# @return RMagick::Image
|
2015-02-24 20:57:56 +01:00
|
|
|
#
|
2015-04-25 17:02:39 +02:00
|
|
|
def render
|
2015-02-18 23:45:53 +01:00
|
|
|
# image size
|
2015-04-26 07:30:25 +02:00
|
|
|
x = @data.length*@field_size # columns x field_size
|
|
|
|
y = @data.dataset_length*@field_size # buckets x field_size
|
2015-02-18 23:45:53 +01:00
|
|
|
|
|
|
|
# start drawing the damn thing
|
2015-10-22 12:28:12 +02:00
|
|
|
image = Magick::Image.new(x, y) { self.background_color = "lime" }
|
2015-02-18 23:45:53 +01:00
|
|
|
|
|
|
|
i = 0
|
2015-04-26 07:16:48 +02:00
|
|
|
@data.each do |point|
|
2015-02-18 23:45:53 +01:00
|
|
|
j = 0
|
|
|
|
point.reverse.each do |part|
|
|
|
|
# draw a red rectangle on the white background
|
|
|
|
core = Magick::Draw.new
|
|
|
|
|
2015-10-22 12:18:34 +02:00
|
|
|
intensity = 127.5 * part
|
2015-02-18 23:45:53 +01:00
|
|
|
# Fix for float stupidity
|
2015-10-22 12:18:34 +02:00
|
|
|
intensity = 127.5 if intensity > 127.5
|
2015-02-18 23:45:53 +01:00
|
|
|
|
|
|
|
# Get the correct value
|
2015-10-22 12:18:34 +02:00
|
|
|
l = (255-intensity).round
|
2015-02-18 23:45:53 +01:00
|
|
|
|
|
|
|
# Draw
|
|
|
|
core.fill(Magick::Pixel.from_hsla(0, 255, l, 1).to_color)
|
|
|
|
core.rectangle((i*@field_size), (j*@field_size), ((i+1)*@field_size), ((j+1)*@field_size))
|
2015-04-25 17:02:39 +02:00
|
|
|
core.draw image
|
2015-02-18 23:45:53 +01:00
|
|
|
j = j + 1
|
|
|
|
end
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
|
2015-04-25 17:02:39 +02:00
|
|
|
image
|
2015-02-18 23:45:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-02-17 22:28:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|