Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/dicom/ruby_extensions.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

#__original_pack__Object

Renames the original pack method.



168
# File 'lib/dicom/ruby_extensions.rb', line 168

alias __original_pack__ pack

#pack(format) ⇒ String

Redefines the old pack method, adding the ability to encode signed integers in big endian (which surprisingly has not been supported out of the box in Ruby until version 1.9.3).

Parameters:

  • format (String)

    a format string which decides the encoding scheme to use

Returns:

  • (String)

    the encoded binary string



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/dicom/ruby_extensions.rb', line 176

def pack(format)
  # FIXME: At some time in the future, when Ruby 1.9.3 can be set as required ruby version,
  # this custom pack (as well as unpack) method can be discarded, and the desired endian
  # encodings can probably be achieved with the new template strings introduced in 1.9.3.
  #
  # Check for some custom pack strings that we've invented:
  case format
    when 'k*' # SS
      # Pack LE SS, re-unpack as LE US, then finally pack BE US:
      wrongly_packed = self.__original_pack__('s*')
      reunpacked = wrongly_packed.__original_unpack__('S*')
      correct = reunpacked.__original_pack__('n*')
    when 'r*' # SL
      # Pack LE SL, re-unpack as LE UL, then finally pack BE UL:
      wrongly_packed = self.__original_pack__('l*')
      reunpacked = wrongly_packed.__original_unpack__('I*')
      correct = reunpacked.__original_pack__('N*')
    else
      # Call the original method for all other (normal) cases:
      self.__original_pack__(format)
  end
end

#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)


204
205
206
207
208
209
210
211
212
213
# File 'lib/dicom/ruby_extensions.rb', line 204

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, native 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

Raises:

  • (ArgumentError)


221
222
223
224
225
226
227
228
229
230
# File 'lib/dicom/ruby_extensions.rb', line 221

def to_signed(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.collect {|i| i - 128}
  when 16
    return self.collect {|i| i - 32768}
  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

Raises:

  • (ArgumentError)


238
239
240
241
242
243
244
245
246
247
# File 'lib/dicom/ruby_extensions.rb', line 238

def to_unsigned(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.collect {|i| i + 128}
  when 16
    return self.collect {|i| i + 32768}
  end
end