Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/dicom/extensions/array.rb

Overview

Extensions to the Array class. These mainly deal with encoding integer arrays as well as conversion between signed and unsigned integers.

Instance Method Summary collapse

Instance Method Details

#to_blob(depth) ⇒ String

Packs an array of (unsigned) integers to a binary string (blob).

Parameters:

  • depth (Integer)

    the bit depth to be used when encoding the unsigned integers

Returns:

  • (String)

    an encoded binary string

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
# File 'lib/dicom/extensions/array.rb', line 12

def to_blob(depth)
  raise ArgumentError, "Expected Integer, got #{depth.class}" unless depth.is_a?(Integer)
  raise ArgumentError, "Unsupported bit depth #{depth}." unless [8,16].include?(depth)
  case depth
  when 8
    return self.pack('C*') # Unsigned char
  when 16
    return self.pack('S<*') # Unsigned short, little endian byte order
  end
end

#to_signed(depth) ⇒ Array<Integer>

Shifts the integer values of the array to make a signed data set. The size of the shift is determined by the given bit depth.

Parameters:

  • depth (Integer)

    the bit depth of the integers

Returns:

  • (Array<Integer>)

    an array of signed integers



29
30
31
32
33
34
35
36
37
38
# File 'lib/dicom/extensions/array.rb', line 29

def to_signed(depth)
  case depth
  when 8
    self.collect {|i| i - 128}
  when 16
    self.collect {|i| i - 32768}
  else
    raise ArgumentError, "Unknown or unsupported bit depth: #{depth}"
  end
end

#to_unsigned(depth) ⇒ Array<Integer>

Shifts the integer values of the array to make an unsigned data set. The size of the shift is determined by the given bit depth.

Parameters:

  • depth (Integer)

    the bit depth of the integers

Returns:

  • (Array<Integer>)

    an array of unsigned integers



46
47
48
49
50
51
52
53
54
55
# File 'lib/dicom/extensions/array.rb', line 46

def to_unsigned(depth)
  case depth
  when 8
    self.collect {|i| i + 128}
  when 16
    self.collect {|i| i + 32768}
  else
    raise ArgumentError, "Unknown or unsupported bit depth: #{depth}"
  end
end