Is there an efficient way to create an RMagick image from the data in a 2D NArray (a Ruby class that's supposed to be more efficient than regular arrays), or are the two libraries just incompatible in their data types?
The following code works, but it does it the hard way: converting data types pixel by pixel with nested do-loops. As far as I can tell, this gives me a lot of extra work and none of the advantages of NArray. It runs slower than molasses in January:
def LineaProcessor.createImage(dataArray, width, height, filename)
image = Magick::Image.new width, height
# scale pixel values from 0..256*255 (the RMagick pixel uses 16 bits)
scaling = 65280/dataArray.max
# set each pixel...I couldn't find any easy way to convert array types
width.times do |x|
height.times do |y|
g = dataArray[x+y*width]*scaling
pixel = Magick::Pixel.new(g, g, g,0)
image.pixel_color x, y, pixel
end
end
image
end
You can convert a greyscale 8-bit NArray to an RMagick image like this