Class: RImageAnalysisTools::ImageWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/rimageanalysistools/image/io/image_writer.rb

Instance Method Summary collapse

Constructor Details

#initializeImageWriter

Returns a new instance of ImageWriter.



38
39
40
# File 'lib/rimageanalysistools/image/io/image_writer.rb', line 38

def initialize
  
end

Instance Method Details

#write(filename, image_to_write) ⇒ Object



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rimageanalysistools/image/io/image_writer.rb', line 42

def write(filename, image_to_write)

  wr = ImageWriter.new

  m = image_to_write.

  pixels = image_to_write.getPixelData

  pixel_array = pixels.toImagePlus.getProcessor.getPixels

  m.setPixelsDimensionOrder(DimensionOrder.fromString(pixels.getDimensionOrder.upcase), 0)

  m.setPixelsType(PixelType.fromString(FormatTools.getPixelTypeString(pixels.getDataType)), 0)

  wr.setMetadataRetrieve(m)

  File.unlink(filename) if File.exist?(filename)

  wr.setId(filename)

  pixels.getNumPlanes.times do |i|

    c = i % pixels::size_c
    z = ((i - c)/pixels::size_c) % pixels::size_z
    t = (i - c - pixels::size_c * z)/(pixels::size_z * pixels::size_c)

    conv_index = pixels.toImagePlus.getStackIndex(c+1, z+1, t+1)

    pixels.toImagePlus.setSliceWithoutUpdate(conv_index)
    pixelData = pixels.toImagePlus.getProcessor.getPixels

    size_to_allocate = pixels::size_x * pixels::size_y * FormatTools.getBytesPerPixel(pixels::dataType)

    out = ByteBuffer.allocate(size_to_allocate)

    out.asFloatBuffer.put(pixelData)

    wr.savePlane(i, out.array)
  
  end

  wr.close

end