dicom 0.9.3 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +312 -290
- data/COPYING +674 -674
- data/Gemfile +3 -0
- data/dicom.gemspec +31 -0
- data/lib/dicom.rb +53 -51
- data/lib/dicom/anonymizer.rb +98 -123
- data/lib/dicom/audit_trail.rb +104 -116
- data/lib/dicom/constants.rb +219 -170
- data/lib/dicom/d_client.rb +122 -150
- data/lib/dicom/d_library.rb +219 -287
- data/lib/dicom/d_object.rb +451 -539
- data/lib/dicom/d_read.rb +151 -245
- data/lib/dicom/d_server.rb +329 -359
- data/lib/dicom/d_write.rb +327 -395
- data/lib/dicom/deprecated.rb +1 -72
- data/lib/dicom/dictionary/elements.txt +3646 -0
- data/lib/dicom/dictionary/uids.txt +334 -0
- data/lib/dicom/dictionary_element.rb +61 -0
- data/lib/dicom/element.rb +278 -218
- data/lib/dicom/elemental.rb +21 -27
- data/lib/dicom/file_handler.rb +121 -121
- data/lib/dicom/image_item.rb +819 -861
- data/lib/dicom/image_processor.rb +24 -15
- data/lib/dicom/image_processor_mini_magick.rb +21 -23
- data/lib/dicom/image_processor_r_magick.rb +39 -34
- data/lib/dicom/item.rb +133 -120
- data/lib/dicom/link.rb +1531 -1532
- data/lib/dicom/logging.rb +155 -158
- data/lib/dicom/parent.rb +782 -847
- data/lib/dicom/ruby_extensions.rb +248 -229
- data/lib/dicom/sequence.rb +109 -92
- data/lib/dicom/stream.rb +480 -511
- data/lib/dicom/uid.rb +82 -0
- data/lib/dicom/variables.rb +9 -9
- data/lib/dicom/version.rb +5 -5
- data/rakefile.rb +29 -0
- metadata +130 -76
- data/lib/dicom/dictionary.rb +0 -3280
data/lib/dicom/d_object.rb
CHANGED
@@ -1,539 +1,451 @@
|
|
1
|
-
# === TODO:
|
2
|
-
#
|
3
|
-
# * The retrieve file network functionality (#get_image in DClient class) has not been tested.
|
4
|
-
# * Make the networking code more intelligent in its handling of unexpected network communication.
|
5
|
-
# * Full support for compressed image data.
|
6
|
-
# * Read/Write 12 bit image data.
|
7
|
-
# * Full color support (RGB and PALETTE COLOR with #image already implemented).
|
8
|
-
# * Support for extraction of multiple encapsulated pixel data frames in #pixels and #narray.
|
9
|
-
# * Image handling currently ignores DICOM tags like Pixel Aspect Ratio, Image Orientation and (to some degree) Photometric Interpretation.
|
10
|
-
# * More robust and flexible options for reorienting extracted pixel arrays?
|
11
|
-
# * A curious observation: Creating a DLibrary instance is exceptionally slow on Ruby 1.9.1: 0.4 seconds versus ~0.01 seconds on Ruby 1.8.7!
|
12
|
-
# * Add these as github issues and remove this list!
|
13
|
-
|
14
|
-
|
15
|
-
# Copyright 2008-2012 Christoffer Lervag
|
16
|
-
#
|
17
|
-
# This program is free software: you can redistribute it and/or modify
|
18
|
-
# it under the terms of the GNU General Public License as published by
|
19
|
-
# the Free Software Foundation, either version 3 of the License, or
|
20
|
-
# (at your option) any later version.
|
21
|
-
#
|
22
|
-
# This program is distributed in the hope that it will be useful,
|
23
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
24
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
25
|
-
# GNU General Public License for more details.
|
26
|
-
#
|
27
|
-
# You should have received a copy of the GNU General Public License
|
28
|
-
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
29
|
-
#
|
30
|
-
module DICOM
|
31
|
-
|
32
|
-
# The DObject class is the main class for interacting with the DICOM object.
|
33
|
-
# Reading from and writing to files is executed from instances of this class.
|
34
|
-
#
|
35
|
-
# === Inheritance
|
36
|
-
#
|
37
|
-
# As the DObject class inherits from the ImageItem class, which itself inherits from the Parent class,
|
38
|
-
# all ImageItem and Parent methods are also available to instances of DObject.
|
39
|
-
#
|
40
|
-
class DObject < ImageItem
|
41
|
-
include Logging
|
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
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
#
|
98
|
-
#
|
99
|
-
#
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
#
|
172
|
-
#
|
173
|
-
#
|
174
|
-
#
|
175
|
-
#
|
176
|
-
#
|
177
|
-
#
|
178
|
-
#
|
179
|
-
#
|
180
|
-
#
|
181
|
-
#
|
182
|
-
#
|
183
|
-
#
|
184
|
-
#
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
#
|
194
|
-
|
195
|
-
#
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
# Returns
|
218
|
-
#
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
#
|
233
|
-
#
|
234
|
-
#
|
235
|
-
#
|
236
|
-
#
|
237
|
-
#
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
end
|
250
|
-
|
251
|
-
#
|
252
|
-
#
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
#
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
#
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
#
|
422
|
-
#
|
423
|
-
#
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
# Update our Stream instance with the new encoding:
|
453
|
-
@stream.endian = new_endian
|
454
|
-
# If endianness is changed, re-encode elements (only elements depending on endianness will actually be re-encoded):
|
455
|
-
encode_children(old_endian) if old_endian != new_endian
|
456
|
-
end
|
457
|
-
|
458
|
-
# Passes the DObject to the DWrite class, which traverses the data element
|
459
|
-
# structure and encodes a proper DICOM binary string, which is finally written to the specified file.
|
460
|
-
#
|
461
|
-
# === Parameters
|
462
|
-
#
|
463
|
-
# * <tt>file_name</tt> -- A string which identifies the path & name of the DICOM file which is to be written to disk.
|
464
|
-
# * <tt>options</tt> -- A hash of parameters.
|
465
|
-
#
|
466
|
-
# === Options
|
467
|
-
#
|
468
|
-
# * <tt>:add_meta</tt> -- Boolean. If set to false, no manipulation of the DICOM object's meta group will be performed before the DObject is written to file.
|
469
|
-
#
|
470
|
-
# === Examples
|
471
|
-
#
|
472
|
-
# dcm.write(path + "test.dcm")
|
473
|
-
#
|
474
|
-
def write(file_name, options={})
|
475
|
-
raise ArgumentError, "Invalid file_name. Expected String, got #{file_name.class}." unless file_name.is_a?(String)
|
476
|
-
insert_missing_meta unless options[:add_meta] == false
|
477
|
-
w = DWrite.new(self, transfer_syntax, file_name, options)
|
478
|
-
w.write
|
479
|
-
# Write process succesful?
|
480
|
-
@write_success = w.success
|
481
|
-
end
|
482
|
-
|
483
|
-
|
484
|
-
# Following methods are private:
|
485
|
-
private
|
486
|
-
|
487
|
-
|
488
|
-
# Adds any missing meta group (0002,xxxx) data elements to the DICOM object,
|
489
|
-
# to ensure that a valid DICOM object will be written to file.
|
490
|
-
#
|
491
|
-
def insert_missing_meta
|
492
|
-
# File Meta Information Version:
|
493
|
-
Element.new("0002,0001", [0,1], :parent => self) unless exists?("0002,0001")
|
494
|
-
# Media Storage SOP Class UID:
|
495
|
-
Element.new("0002,0002", value("0008,0016"), :parent => self) unless exists?("0002,0002")
|
496
|
-
# Media Storage SOP Instance UID:
|
497
|
-
Element.new("0002,0003", value("0008,0018"), :parent => self) unless exists?("0002,0003")
|
498
|
-
# Transfer Syntax UID:
|
499
|
-
Element.new("0002,0010", transfer_syntax, :parent => self) unless exists?("0002,0010")
|
500
|
-
if !exists?("0002,0012") and !exists?("0002,0013")
|
501
|
-
# Implementation Class UID:
|
502
|
-
Element.new("0002,0012", UID, :parent => self)
|
503
|
-
# Implementation Version Name:
|
504
|
-
Element.new("0002,0013", NAME, :parent => self)
|
505
|
-
end
|
506
|
-
# Source Application Entity Title:
|
507
|
-
Element.new("0002,0016", DICOM.source_app_title, :parent => self) unless exists?("0002,0016")
|
508
|
-
# Group Length: Delete the old one (if it exists) before creating a new one.
|
509
|
-
delete("0002,0000")
|
510
|
-
Element.new("0002,0000", meta_group_length, :parent => self)
|
511
|
-
end
|
512
|
-
|
513
|
-
# Determines and returns the length of the meta group in the DObject instance.
|
514
|
-
#
|
515
|
-
def meta_group_length
|
516
|
-
group_length = 0
|
517
|
-
meta_elements = group(META_GROUP)
|
518
|
-
tag = 4
|
519
|
-
vr = 2
|
520
|
-
meta_elements.each do |element|
|
521
|
-
case element.vr
|
522
|
-
when "OB","OW","OF","SQ","UN","UT"
|
523
|
-
length = 6
|
524
|
-
else
|
525
|
-
length = 2
|
526
|
-
end
|
527
|
-
group_length += tag + vr + length + element.bin.length
|
528
|
-
end
|
529
|
-
return group_length
|
530
|
-
end
|
531
|
-
|
532
|
-
# Returns the attributes (children) of this instance (for comparison purposes).
|
533
|
-
#
|
534
|
-
def state
|
535
|
-
@tags
|
536
|
-
end
|
537
|
-
|
538
|
-
end
|
539
|
-
end
|
1
|
+
# === TODO:
|
2
|
+
#
|
3
|
+
# * The retrieve file network functionality (#get_image in DClient class) has not been tested.
|
4
|
+
# * Make the networking code more intelligent in its handling of unexpected network communication.
|
5
|
+
# * Full support for compressed image data.
|
6
|
+
# * Read/Write 12 bit image data.
|
7
|
+
# * Full color support (RGB and PALETTE COLOR with #image already implemented).
|
8
|
+
# * Support for extraction of multiple encapsulated pixel data frames in #pixels and #narray.
|
9
|
+
# * Image handling currently ignores DICOM tags like Pixel Aspect Ratio, Image Orientation and (to some degree) Photometric Interpretation.
|
10
|
+
# * More robust and flexible options for reorienting extracted pixel arrays?
|
11
|
+
# * A curious observation: Creating a DLibrary instance is exceptionally slow on Ruby 1.9.1: 0.4 seconds versus ~0.01 seconds on Ruby 1.8.7!
|
12
|
+
# * Add these as github issues and remove this list!
|
13
|
+
|
14
|
+
|
15
|
+
# Copyright 2008-2012 Christoffer Lervag
|
16
|
+
#
|
17
|
+
# This program is free software: you can redistribute it and/or modify
|
18
|
+
# it under the terms of the GNU General Public License as published by
|
19
|
+
# the Free Software Foundation, either version 3 of the License, or
|
20
|
+
# (at your option) any later version.
|
21
|
+
#
|
22
|
+
# This program is distributed in the hope that it will be useful,
|
23
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
24
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
25
|
+
# GNU General Public License for more details.
|
26
|
+
#
|
27
|
+
# You should have received a copy of the GNU General Public License
|
28
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
29
|
+
#
|
30
|
+
module DICOM
|
31
|
+
|
32
|
+
# The DObject class is the main class for interacting with the DICOM object.
|
33
|
+
# Reading from and writing to files is executed from instances of this class.
|
34
|
+
#
|
35
|
+
# === Inheritance
|
36
|
+
#
|
37
|
+
# As the DObject class inherits from the ImageItem class, which itself inherits from the Parent class,
|
38
|
+
# all ImageItem and Parent methods are also available to instances of DObject.
|
39
|
+
#
|
40
|
+
class DObject < ImageItem
|
41
|
+
include Logging
|
42
|
+
|
43
|
+
# An attribute set as nil. This attribute is included to provide consistency with the other element types which usually have a parent defined.
|
44
|
+
attr_reader :parent
|
45
|
+
# A boolean which is set as true if a DICOM file has been successfully read & parsed from a file (or binary string).
|
46
|
+
attr_accessor :read_success
|
47
|
+
# The Stream instance associated with this DObject instance (this attribute is mostly used internally).
|
48
|
+
attr_reader :stream
|
49
|
+
# A boolean which is set as true if a DObject instance has been successfully written to file (or successfully encoded).
|
50
|
+
attr_reader :write_success
|
51
|
+
|
52
|
+
alias_method :read?, :read_success
|
53
|
+
alias_method :written?, :write_success
|
54
|
+
|
55
|
+
# Creates a DObject instance by downloading a DICOM file
|
56
|
+
# specified by a hyperlink, and parsing the retrieved file.
|
57
|
+
#
|
58
|
+
# @note Highly experimental and un-tested!
|
59
|
+
# @note Designed for the HTTP protocol only.
|
60
|
+
# @note Whether this method should be included or removed from ruby-dicom is up for debate.
|
61
|
+
#
|
62
|
+
# @param [String] link a hyperlink string which specifies remote location of the DICOM file to be loaded.
|
63
|
+
# @return [DObject] the created DObject instance
|
64
|
+
#
|
65
|
+
def self.get(link)
|
66
|
+
raise ArgumentError, "Invalid argument 'link'. Expected String, got #{link.class}." unless link.is_a?(String)
|
67
|
+
raise ArgumentError, "Invalid argument 'link'. Expected a string starting with 'http', got #{link}." unless link.index('http') == 0
|
68
|
+
require 'open-uri'
|
69
|
+
bin = nil
|
70
|
+
file = nil
|
71
|
+
# Try to open the remote file using open-uri:
|
72
|
+
retrials = 0
|
73
|
+
begin
|
74
|
+
file = open(link, 'rb') # binary encoding (ASCII-8BIT)
|
75
|
+
rescue Exception => e
|
76
|
+
if retrials > 3
|
77
|
+
retrials = 0
|
78
|
+
raise "Unable to retrieve the file. File does not exist?"
|
79
|
+
else
|
80
|
+
logger.warn("Exception in ruby-dicom when loading a dicom file from: #{file}")
|
81
|
+
logger.debug("Retrying... #{retrials}")
|
82
|
+
retrials += 1
|
83
|
+
retry
|
84
|
+
end
|
85
|
+
end
|
86
|
+
bin = File.open(file, "rb") { |f| f.read }
|
87
|
+
# Parse the file contents and create the DICOM object:
|
88
|
+
if bin
|
89
|
+
dcm = self.parse(bin)
|
90
|
+
else
|
91
|
+
dcm = self.new
|
92
|
+
dcm.read_success = false
|
93
|
+
end
|
94
|
+
return dcm
|
95
|
+
end
|
96
|
+
|
97
|
+
# Creates a DObject instance by parsing an encoded binary DICOM string.
|
98
|
+
#
|
99
|
+
# @param [String] string an encoded binary string containing DICOM information
|
100
|
+
# @param [Hash] options the options to use for parsing the DICOM string
|
101
|
+
# @option options [Boolean] :signature if set as false, the parsing algorithm will not be looking for the DICOM header signature (defaults to true)
|
102
|
+
# @option options [String] :syntax if a syntax string is specified, the parsing algorithm will be forced to use this transfer syntax when decoding the binary string
|
103
|
+
# @example Parse a DICOM file that has already been loaded to a binary string
|
104
|
+
# require 'dicom'
|
105
|
+
# dcm = DICOM::DObject.parse(str)
|
106
|
+
# @example Parse a header-less DICOM string with explicit little endian transfer syntax
|
107
|
+
# dcm = DICOM::DObject.parse(str, :syntax => '1.2.840.10008.1.2.1')
|
108
|
+
#
|
109
|
+
def self.parse(string, options={})
|
110
|
+
raise ArgumentError, "Invalid argument 'string'. Expected String, got #{string.class}." unless string.is_a?(String)
|
111
|
+
raise ArgumentError, "Invalid option :syntax. Expected String, got #{options[:syntax].class}." if options[:syntax] && !options[:syntax].is_a?(String)
|
112
|
+
signature = options[:signature].nil? ? true : options[:signature]
|
113
|
+
dcm = self.new
|
114
|
+
dcm.send(:read, string, signature, :syntax => options[:syntax])
|
115
|
+
if dcm.read?
|
116
|
+
logger.debug("DICOM string successfully parsed.")
|
117
|
+
else
|
118
|
+
logger.warn("Failed to parse this string as DICOM.")
|
119
|
+
end
|
120
|
+
return dcm
|
121
|
+
end
|
122
|
+
|
123
|
+
# Creates a DObject instance by reading and parsing a DICOM file.
|
124
|
+
#
|
125
|
+
# @param [String] file a string which specifies the path of the DICOM file to be loaded
|
126
|
+
# @example Load a DICOM file
|
127
|
+
# require 'dicom'
|
128
|
+
# dcm = DICOM::DObject.read('test.dcm')
|
129
|
+
#
|
130
|
+
def self.read(file)
|
131
|
+
raise ArgumentError, "Invalid argument 'file'. Expected String, got #{file.class}." unless file.is_a?(String)
|
132
|
+
# Read the file content:
|
133
|
+
bin = nil
|
134
|
+
unless File.exist?(file)
|
135
|
+
logger.error("Invalid (non-existing) file: #{file}")
|
136
|
+
else
|
137
|
+
unless File.readable?(file)
|
138
|
+
logger.error("File exists but I don't have permission to read it: #{file}")
|
139
|
+
else
|
140
|
+
if File.directory?(file)
|
141
|
+
logger.error("Expected a file, got a directory: #{file}")
|
142
|
+
else
|
143
|
+
if File.size(file) < 8
|
144
|
+
logger.error("This file is too small to contain any DICOM information: #{file}.")
|
145
|
+
else
|
146
|
+
bin = File.open(file, "rb") { |f| f.read }
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
# Parse the file contents and create the DICOM object:
|
152
|
+
if bin
|
153
|
+
dcm = self.parse(bin)
|
154
|
+
# If reading failed, and no transfer syntax was detected, we will make another attempt at reading the file while forcing explicit (little endian) decoding.
|
155
|
+
# This will help for some rare cases where the DICOM file is saved (erroneously, Im sure) with explicit encoding without specifying the transfer syntax tag.
|
156
|
+
if !dcm.read? and !dcm.exists?("0002,0010")
|
157
|
+
logger.info("Attempting a second decode pass (assuming Explicit Little Endian transfer syntax).")
|
158
|
+
dcm = self.parse(bin, :syntax => EXPLICIT_LITTLE_ENDIAN)
|
159
|
+
end
|
160
|
+
else
|
161
|
+
dcm = self.new
|
162
|
+
end
|
163
|
+
if dcm.read?
|
164
|
+
logger.info("DICOM file successfully read: #{file}")
|
165
|
+
else
|
166
|
+
logger.warn("Reading DICOM file failed: #{file}")
|
167
|
+
end
|
168
|
+
return dcm
|
169
|
+
end
|
170
|
+
|
171
|
+
# Creates a DObject instance (DObject is an abbreviation for "DICOM object").
|
172
|
+
#
|
173
|
+
# The DObject instance holds references to the different types of objects (Element, Item, Sequence)
|
174
|
+
# that makes up a DICOM object. A DObject is typically buildt by reading and parsing a file or a binary
|
175
|
+
# string (with DObject::read or ::parse), but can also be buildt from an empty state by this method.
|
176
|
+
#
|
177
|
+
# To customize logging behaviour, refer to the Logging module documentation.
|
178
|
+
#
|
179
|
+
# @example Create an empty DICOM object
|
180
|
+
# require 'dicom'
|
181
|
+
# dcm = DICOM::DObject.new
|
182
|
+
# @example Increasing the log message threshold (default level is INFO)
|
183
|
+
# DICOM.logger.level = Logger::ERROR
|
184
|
+
#
|
185
|
+
def initialize
|
186
|
+
# Initialization of variables that DObject share with other parent elements:
|
187
|
+
initialize_parent
|
188
|
+
# Structural information (default values):
|
189
|
+
@explicit = true
|
190
|
+
@str_endian = false
|
191
|
+
# Control variables:
|
192
|
+
@read_success = nil
|
193
|
+
# Initialize a Stream instance which is used for encoding/decoding:
|
194
|
+
@stream = Stream.new(nil, @str_endian)
|
195
|
+
# The DObject instance is the top of the hierarchy and unlike other elements it has no parent:
|
196
|
+
@parent = nil
|
197
|
+
end
|
198
|
+
|
199
|
+
# Checks for equality.
|
200
|
+
#
|
201
|
+
# Other and self are considered equivalent if they are
|
202
|
+
# of compatible types and their attributes are equivalent.
|
203
|
+
#
|
204
|
+
# @param other an object to be compared with self.
|
205
|
+
# @return [Boolean] true if self and other are considered equivalent
|
206
|
+
#
|
207
|
+
def ==(other)
|
208
|
+
if other.respond_to?(:to_dcm)
|
209
|
+
other.send(:state) == state
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
alias_method :eql?, :==
|
214
|
+
|
215
|
+
# Encodes the DICOM object into a series of binary string segments with a specified maximum length.
|
216
|
+
#
|
217
|
+
# Returns the encoded binary strings in an array.
|
218
|
+
#
|
219
|
+
# @param [Integer] max_size the maximum allowed size of the binary data strings to be encoded
|
220
|
+
# @param [String] transfer_syntax the transfer syntax string to be used when encoding the DICOM object to string segments. When this method is used for making network packets, the transfer_syntax is not part of the object, and thus needs to be specified.
|
221
|
+
# @return [Array<String>] the encoded DICOM strings
|
222
|
+
# @example Encode the DObject to strings of max length 2^14 bytes
|
223
|
+
# encoded_strings = dcm.encode_segments(16384)
|
224
|
+
#
|
225
|
+
def encode_segments(max_size, transfer_syntax=IMPLICIT_LITTLE_ENDIAN)
|
226
|
+
raise ArgumentError, "Invalid argument. Expected an Integer, got #{max_size.class}." unless max_size.is_a?(Integer)
|
227
|
+
raise ArgumentError, "Argument too low (#{max_size}), please specify a bigger Integer." unless max_size > 16
|
228
|
+
raise "Can not encode binary segments for an empty DICOM object." if children.length == 0
|
229
|
+
encode_in_segments(max_size, :syntax => transfer_syntax)
|
230
|
+
end
|
231
|
+
|
232
|
+
# Computes a hash code for this object.
|
233
|
+
#
|
234
|
+
# @note Two objects with the same attributes will have the same hash code.
|
235
|
+
#
|
236
|
+
# @return [Fixnum] the object's hash code
|
237
|
+
#
|
238
|
+
def hash
|
239
|
+
state.hash
|
240
|
+
end
|
241
|
+
|
242
|
+
# Prints information of interest related to the DICOM object.
|
243
|
+
# Calls the Parent#print method as well as DObject#summary.
|
244
|
+
#
|
245
|
+
def print_all
|
246
|
+
puts ""
|
247
|
+
print(:value_max => 30)
|
248
|
+
summary
|
249
|
+
end
|
250
|
+
|
251
|
+
# Gathers key information about the DObject as well as some system data, and prints this information to the screen.
|
252
|
+
# This information includes properties like encoding, byte order, modality and various image properties.
|
253
|
+
#
|
254
|
+
# @return [Array<String>] strings describing the properties of the DICOM object
|
255
|
+
#
|
256
|
+
def summary
|
257
|
+
# FIXME: Perhaps this method should be split up in one or two separate methods
|
258
|
+
# which just builds the information arrays, and a third method for printing this to the screen.
|
259
|
+
sys_info = Array.new
|
260
|
+
info = Array.new
|
261
|
+
# Version of Ruby DICOM used:
|
262
|
+
sys_info << "Ruby DICOM version: #{VERSION}"
|
263
|
+
# System endian:
|
264
|
+
cpu = (CPU_ENDIAN ? "Big Endian" : "Little Endian")
|
265
|
+
sys_info << "Byte Order (CPU): #{cpu}"
|
266
|
+
# File path/name:
|
267
|
+
info << "File: #{@file}"
|
268
|
+
# Modality:
|
269
|
+
modality = (LIBRARY.uid(value('0008,0016')) ? LIBRARY.uid(value('0008,0016')).name : "SOP Class unknown or not specified!")
|
270
|
+
info << "Modality: #{modality}"
|
271
|
+
# Meta header presence (Simply check for the presence of the transfer syntax data element), VR and byte order:
|
272
|
+
ts_status = self['0002,0010'] ? '' : ' (Assumed)'
|
273
|
+
ts = LIBRARY.uid(transfer_syntax)
|
274
|
+
explicit = ts ? ts.explicit? : true
|
275
|
+
endian = ts ? ts.big_endian? : false
|
276
|
+
meta_comment = ts ? "" : " (But unknown/invalid transfer syntax: #{transfer_syntax})"
|
277
|
+
info << "Meta Header: #{self['0002,0010'] ? 'Yes' : 'No'}#{meta_comment}"
|
278
|
+
info << "Value Representation: #{explicit ? 'Explicit' : 'Implicit'}#{ts_status}"
|
279
|
+
info << "Byte Order (File): #{endian ? 'Big Endian' : 'Little Endian'}#{ts_status}"
|
280
|
+
# Pixel data:
|
281
|
+
pixels = self[PIXEL_TAG]
|
282
|
+
unless pixels
|
283
|
+
info << "Pixel Data: No"
|
284
|
+
else
|
285
|
+
info << "Pixel Data: Yes"
|
286
|
+
# Image size:
|
287
|
+
cols = (exists?("0028,0011") ? self["0028,0011"].value : "Columns missing")
|
288
|
+
rows = (exists?("0028,0010") ? self["0028,0010"].value : "Rows missing")
|
289
|
+
info << "Image Size: #{cols}*#{rows}"
|
290
|
+
# Frames:
|
291
|
+
frames = value("0028,0008") || "1"
|
292
|
+
unless frames == "1" or frames == 1
|
293
|
+
# Encapsulated or 3D pixel data:
|
294
|
+
if pixels.is_a?(Element)
|
295
|
+
frames = frames.to_s + " (3D Pixel Data)"
|
296
|
+
else
|
297
|
+
frames = frames.to_s + " (Encapsulated Multiframe Image)"
|
298
|
+
end
|
299
|
+
end
|
300
|
+
info << "Number of frames: #{frames}"
|
301
|
+
# Color:
|
302
|
+
colors = (exists?("0028,0004") ? self["0028,0004"].value : "Not specified")
|
303
|
+
info << "Photometry: #{colors}"
|
304
|
+
# Compression:
|
305
|
+
compression = (ts ? (ts.compressed_pixels? ? ts.name : 'No') : 'No' )
|
306
|
+
info << "Compression: #{compression}#{ts_status}"
|
307
|
+
# Pixel bits (allocated):
|
308
|
+
bits = (exists?("0028,0100") ? self["0028,0100"].value : "Not specified")
|
309
|
+
info << "Bits per Pixel: #{bits}"
|
310
|
+
end
|
311
|
+
# Print the DICOM object's key properties:
|
312
|
+
separator = "-------------------------------------------"
|
313
|
+
puts "System Properties:"
|
314
|
+
puts separator + "\n"
|
315
|
+
puts sys_info
|
316
|
+
puts "\n"
|
317
|
+
puts "DICOM Object Properties:"
|
318
|
+
puts separator
|
319
|
+
puts info
|
320
|
+
puts separator
|
321
|
+
return info
|
322
|
+
end
|
323
|
+
|
324
|
+
# Returns self.
|
325
|
+
#
|
326
|
+
# @return [DObject] self
|
327
|
+
#
|
328
|
+
def to_dcm
|
329
|
+
self
|
330
|
+
end
|
331
|
+
|
332
|
+
# Gives the transfer syntax string of the DObject.
|
333
|
+
#
|
334
|
+
# If a transfer syntax has not been defined in the DObject, a default tansfer syntax is assumed and returned.
|
335
|
+
#
|
336
|
+
# @return [String] the DObject's transfer syntax
|
337
|
+
#
|
338
|
+
def transfer_syntax
|
339
|
+
return value("0002,0010") || IMPLICIT_LITTLE_ENDIAN
|
340
|
+
end
|
341
|
+
|
342
|
+
# Changes the transfer syntax Element of the DObject instance, and performs re-encoding of all
|
343
|
+
# numerical values if a switch of endianness is implied.
|
344
|
+
#
|
345
|
+
# @note This method does not change the compressed state of the pixel data element. Changing
|
346
|
+
# the transfer syntax between an uncompressed and compressed state will NOT change the pixel
|
347
|
+
# data accordingly (this must be taken care of manually).
|
348
|
+
#
|
349
|
+
# @param [String] new_syntax the new transfer syntax string to be applied to the DObject
|
350
|
+
#
|
351
|
+
def transfer_syntax=(new_syntax)
|
352
|
+
# Verify old and new transfer syntax:
|
353
|
+
new_uid = LIBRARY.uid(new_syntax)
|
354
|
+
old_uid = LIBRARY.uid(transfer_syntax)
|
355
|
+
raise ArgumentError, "Invalid/unknown transfer syntax specified: #{new_syntax}" unless new_uid && new_uid.transfer_syntax?
|
356
|
+
raise ArgumentError, "Invalid/unknown existing transfer syntax: #{new_syntax} Unable to reliably handle byte order encoding. Modify the transfer syntax element directly instead." unless old_uid && old_uid.transfer_syntax?
|
357
|
+
# Set the new transfer syntax:
|
358
|
+
if exists?("0002,0010")
|
359
|
+
self["0002,0010"].value = new_syntax
|
360
|
+
else
|
361
|
+
add(Element.new("0002,0010", new_syntax))
|
362
|
+
end
|
363
|
+
# Update our Stream instance with the new encoding:
|
364
|
+
@stream.endian = new_uid.big_endian?
|
365
|
+
# If endianness is changed, re-encode elements (only elements depending on endianness will actually be re-encoded):
|
366
|
+
encode_children(old_uid.big_endian?) if old_uid.big_endian? != new_uid.big_endian?
|
367
|
+
end
|
368
|
+
|
369
|
+
# Writes the DICOM object to file.
|
370
|
+
#
|
371
|
+
# @note The goal of the Ruby DICOM library is to yield maximum conformance with the DICOM
|
372
|
+
# standard when outputting DICOM files. Therefore, when encoding the DICOM file, manipulation
|
373
|
+
# of items such as the meta group, group lengths and header signature may occur. Therefore,
|
374
|
+
# the file that is written may not be an exact bitwise copy of the file that was read, even if no
|
375
|
+
# DObject manipulation has been done by the user.
|
376
|
+
#
|
377
|
+
# @param [String] file_name the path of the DICOM file which is to be written to disk
|
378
|
+
# @param [Hash] options the options to use for writing the DICOM file
|
379
|
+
# @option options [Boolean] :add_meta <DEPRECATED> if set to false, no manipulation of the DICOM object's meta group will be performed before the DObject is written to file
|
380
|
+
# @option options [Boolean] :ignore_meta if true, no manipulation of the DICOM object's meta group will be performed before the DObject is written to file
|
381
|
+
# @example Encode a DICOM file from a DObject
|
382
|
+
# dcm.write('C:/dicom/test.dcm')
|
383
|
+
#
|
384
|
+
def write(file_name, options={})
|
385
|
+
logger.warn("Option :add_meta => false is deprecated. Use option :ignore_meta => true instead.") if options[:add_meta] == false
|
386
|
+
raise ArgumentError, "Invalid file_name. Expected String, got #{file_name.class}." unless file_name.is_a?(String)
|
387
|
+
insert_missing_meta unless options[:add_meta] == false or options[:ignore_meta]
|
388
|
+
write_elements(:file_name => file_name, :signature => true, :syntax => transfer_syntax)
|
389
|
+
end
|
390
|
+
|
391
|
+
|
392
|
+
private
|
393
|
+
|
394
|
+
|
395
|
+
# Adds any missing meta group (0002,xxxx) data elements to the DICOM object,
|
396
|
+
# to ensure that a valid DICOM object is encoded.
|
397
|
+
#
|
398
|
+
def insert_missing_meta
|
399
|
+
# File Meta Information Version:
|
400
|
+
Element.new("0002,0001", [0,1], :parent => self) unless exists?("0002,0001")
|
401
|
+
# Media Storage SOP Class UID:
|
402
|
+
Element.new("0002,0002", value("0008,0016"), :parent => self) unless exists?("0002,0002")
|
403
|
+
# Media Storage SOP Instance UID:
|
404
|
+
Element.new("0002,0003", value("0008,0018"), :parent => self) unless exists?("0002,0003")
|
405
|
+
# Transfer Syntax UID:
|
406
|
+
Element.new("0002,0010", transfer_syntax, :parent => self) unless exists?("0002,0010")
|
407
|
+
if !exists?("0002,0012") and !exists?("0002,0013")
|
408
|
+
# Implementation Class UID:
|
409
|
+
Element.new("0002,0012", UID_ROOT, :parent => self)
|
410
|
+
# Implementation Version Name:
|
411
|
+
Element.new("0002,0013", NAME, :parent => self)
|
412
|
+
end
|
413
|
+
# Source Application Entity Title:
|
414
|
+
Element.new("0002,0016", DICOM.source_app_title, :parent => self) unless exists?("0002,0016")
|
415
|
+
# Group Length: Delete the old one (if it exists) before creating a new one.
|
416
|
+
delete("0002,0000")
|
417
|
+
Element.new("0002,0000", meta_group_length, :parent => self)
|
418
|
+
end
|
419
|
+
|
420
|
+
# Determines the length of the meta group in the DObject instance.
|
421
|
+
#
|
422
|
+
# @return [Integer] the length of the file meta group string
|
423
|
+
#
|
424
|
+
def meta_group_length
|
425
|
+
group_length = 0
|
426
|
+
meta_elements = group(META_GROUP)
|
427
|
+
tag = 4
|
428
|
+
vr = 2
|
429
|
+
meta_elements.each do |element|
|
430
|
+
case element.vr
|
431
|
+
when "OB","OW","OF","SQ","UN","UT"
|
432
|
+
length = 6
|
433
|
+
else
|
434
|
+
length = 2
|
435
|
+
end
|
436
|
+
group_length += tag + vr + length + element.bin.length
|
437
|
+
end
|
438
|
+
return group_length
|
439
|
+
end
|
440
|
+
|
441
|
+
# Collects the attributes of this instance.
|
442
|
+
#
|
443
|
+
# @return [Array<Element, Sequence>] an array of elements and sequences
|
444
|
+
#
|
445
|
+
def state
|
446
|
+
@tags
|
447
|
+
end
|
448
|
+
|
449
|
+
end
|
450
|
+
|
451
|
+
end
|