README.txt 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. Latest Version
  2. --------------
  3. 1.1 - Amazonic Bagua (Stable)
  4. 1.2 - Imperial Cusco (Stable)
  5. 1.3 - Ancient Cajamarca (Stable)
  6. Download
  7. --------
  8. https://github.com/nanodocumet/Nanodicom
  9. Explanation
  10. -----------
  11. There seems to be only one available PHP DICOM parser: php-dicom. However, after
  12. using it for a while (I took the lead on that project and added some new
  13. features), I felt that it needed a big overhaul.
  14. The rationale for this new toolkit is to provide the following features:
  15. - Robustness. Should not abruptly die. Exceptions are now triggered properly.
  16. - No dependencies. Self-contained at its core, period.
  17. - Performance improvements.
  18. - Memory is kept as low as possible, expect to have at least 10-30% extra
  19. of the size of the DICOM object to be read.
  20. - Speed. Dictionaries are avoided at parsing time. Only a small subset is
  21. loaded. If only certain tags are requested, the parsing will stop there.
  22. - Handling exceptions have been added.
  23. - Up to date. DICOM dictionary has been updated to 2009. Except for few cases
  24. (multiplicity and conditional VR) almost 99% of tags are present. The new way
  25. to load dictionary allows for easy extensibility to include own private
  26. dictionaries.
  27. - Documentation. Great tools should have great documentation. Users should solve
  28. most of their inquiries by reading documentation. More to come. For now, inline
  29. documentation is extensively provided.
  30. - Community-based. Using github to host the source code, hopefully others can
  31. contribute.
  32. - Extensibility. The core code should be kept intact. The toolkit allows the easy
  33. extension of the main core class. It also allows the dynamic extension to include
  34. other tools at runtime. Please be aware that this feature is still experimental.
  35. Read Additional notes at the end of the file for more information.
  36. Requirements
  37. ------------
  38. - PHP 5.2.6+ (tested, possibly older versions)
  39. License
  40. -------
  41. The MIT License
  42. Copyright (c) 2010-2011 Jorge "Nano" Documet
  43. Permission is hereby granted, free of charge, to any person obtaining a copy
  44. of this software and associated documentation files (the "Software"), to deal
  45. in the Software without restriction, including without limitation the rights
  46. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  47. copies of the Software, and to permit persons to whom the Software is
  48. furnished to do so, subject to the following conditions:
  49. The above copyright notice and this permission notice shall be included in
  50. all copies or substantial portions of the Software.
  51. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  52. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  53. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  54. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  55. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  56. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  57. THE SOFTWARE.
  58. Tested environments
  59. -------------------
  60. 1)
  61. 32-bits Windows7
  62. PHP 5.3.0 (cli) (built: Jun 29 2009 21:25:23)
  63. Copyright (c) 1997-2009 The PHP Group
  64. Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies
  65. with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
  66. 2)
  67. 64-bits "Linux 2.6.32-4-pve #1 SMP Mon Sep 20 11:36:51 CEST 2010 x86_64
  68. GNU/Linux" - Debian Lenny
  69. PHP 5.2.6-1+lenny9 with Suhosin-Patch 0.9.6.2 (cli) (built: Aug 4 2010
  70. 06:06:53)
  71. Copyright (c) 1997-2008 The PHP Group
  72. Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
  73. 3)
  74. 32-bits "Linux 2.6.32-24-generic-pae #42-Ubuntu SMP Fri Aug 20 15:37:22 UTC
  75. 2010 i686 GNU/Linux" - Ubuntu 10.04 (Virtual Machine)
  76. PHP 5.3.2-1ubuntu4.2 with Suhosin-Patch (cli) (built: May 13 2010 20:01:00)
  77. Copyright (c) 1997-2009 The PHP Group
  78. Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
  79. 4)
  80. 64 bits "Linux 2.6.33.7-vs2.3.0.36.30.4 #23 SMP Tue Sep 28 05:47:35 PDT 2010
  81. x86_64 GNU/Linux" - Debian Lenny (Virtual Machine)
  82. PHP 5.2.14 (cli) (built: Oct 4 2010 16:17:01)
  83. Copyright (c) 1997-2010 The PHP Group
  84. Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
  85. with Zend Extension Manager v1.2.2, Copyright (c) 2003-2007, by Zend
  86. Technologies
  87. with XCache v1.2.2, Copyright (c) 2005-2007, by mOo
  88. with Zend Optimizer v3.3.9, Copyright (c) 1998-2009, by Zend Technologies
  89. 5)
  90. 32 bits "Linux 2.6.32-24-generic-pae #39-Ubuntu SMP Wed Jul 28 07:39:26 UTC
  91. 2010 i686 GNU/Linux" - Ubuntu 10.04 (Virtual Machine)
  92. PHP 5.3.2-1ubuntu4.5 with Suhosin-Patch (cli) (built: Sep 17 2010 13:41:55)
  93. Copyright (c) 1997-2009 The PHP Group
  94. Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
  95. 6)
  96. 64 bits Sun Solaris Ultra-Sparc
  97. More details to come
  98. Steps to use the module
  99. -----------------------
  100. 1) Place the nanodicom and dicom folders anywhere (but they have to be in the
  101. same parent folder)
  102. 2) Add the following statement anywhere in your code
  103. require_once PATH_TO_NANODICOM.'nanodicom.php';
  104. 3) Load files the following way:
  105. $dicom = Nanodicom::factory(LOCATION_OF_FILE);
  106. Supported official extensions (tools)
  107. -------------------------------------
  108. 'simple' => Simple and dummy wrapper to Core. Nothing extra
  109. 'dumper' => To dump the contents of a DICOM file. Very helpful for dumping
  110. to prompt or creating html output. xml output should be straightforward.
  111. 'anonymizer' => To anonymize DICOM files. It keeps track of repeated values
  112. among files for consistency.
  113. New v1.1!: Searches and replaces inside Sequences. Great for DICOMDIR
  114. New v1.2!: Allows passing an array to replace values if found. Great
  115. when loading anonymized values from DB or other sources.
  116. 'pixeler' => New v1.2!: Reads pixel data from images and returns image objects.
  117. Uncompressed images only for now.
  118. New v1.2!: Reads RLE, jpeg 8-bit lossy and uncompressed data.
  119. New v1.2!: Reads Monochrome1 and Monochrome2 or Paletter color.
  120. New v1.2!: Reads Planar Configuration of 0 (Color-by-pixel RGBRGB...)
  121. and 1 (Color-by-plane RRRR...GGGG...BBBB...)
  122. 'getter' => New from v1.3: Parses the file but forces the loading of the dictionaries.
  123. Miscelaneous
  124. ------------
  125. - Coding conventions from Kohana:
  126. http://kohanaframework.org/guide/about.conventions
  127. - DICOM standard:
  128. ftp://medical.nema.org/medical/dicom/2009/
  129. - DICOM Dictionary up to 2009 version (self-extracted)
  130. ftp://medical.nema.org/medical/dicom/2009/09_06pu3.pdf
  131. - Special thanks to the hundreds of people that uploaded their DICOM files to the
  132. php-DICOM test page. I have used that repository to test this new toolkit extensively.
  133. - DICOM sample files have been obtained from python package "pydicom" (thanks Darcy)
  134. http://code.google.com/p/pydicom/
  135. http://code.google.com/p/pydicom/source/browse/source/dicom/testfiles/README.txt
  136. - Inspiration for nanodcm command line helper
  137. https://github.com/goyote/kohana-installer
  138. Supported (Tested) Transfer Syntaxes
  139. ---------------------------
  140. - For reading (actually, just for parsing, no pixel data is read)
  141. NONE
  142. 1.2.840.10008.1.2 Implicit VR Little Endian: Default Transfer Syntax for DICOM
  143. 1.2.840.10008.1.2.1 Explicit VR Little Endian
  144. 1.2.840.10008.1.2.1.99 Deflated Explicit VR Little Endian
  145. 1.2.840.10008.1.2.2 Explicit VR Big Endian
  146. 1.2.840.10008.1.2.4.50 JPEG Baseline (Process 1): Default Transfer Syntax
  147. for Lossy JPEG 8 Bit Image Compression
  148. 1.2.840.10008.1.2.4.51 JPEG Extended (Process 2 & 4): Default Transfer Syntax for
  149. Lossy JPEG 12 Bit Image Compression (Process 4 only)
  150. 1.2.840.10008.1.2.4.57 JPEG Lossless, Non-Hierarchical (Process 14)
  151. 1.2.840.10008.1.2.4.70 JPEG Lossless, Non-Hierarchical, First-Order Prediction
  152. (Process 14 [Selection Value 1]): Default Transfer Syntax
  153. for Lossless JPEG Image Compression
  154. 1.2.840.10008.1.2.4.90 JPEG 2000 Image Compression (Lossless Only)
  155. 1.2.840.10008.1.2.4.91 JPEG 2000 Image Compression
  156. 1.2.840.10008.1.2.5 RLE Lossless
  157. 1.2.840.10008.1.2.5.1.1 UNKNOWN
  158. - For writing (please see Additional Notes)
  159. 1.2.840.10008.1.2 Implicit VR Little Endian: Default Transfer Syntax for DICOM
  160. 1.2.840.10008.1.2.1 Explicit VR Little Endian
  161. 1.2.840.10008.1.2.1.99 Deflated Explicit VR Little Endian
  162. 1.2.840.10008.1.2.2 Explicit VR Big Endian
  163. Roadmap (as of November 17th, 2011)
  164. -------
  165. Version 1.4
  166. 1) Improve Pixeler performance
  167. Version 2.0
  168. 1) DICOM Network tools (low priority)
  169. Examples
  170. --------
  171. 1) Most basic example. Fast!
  172. $dicom = Nanodicom::factory($filename);
  173. // Only a small subset of the dictionary entries were loaded
  174. echo $dicom->parse()->profiler_diff('parse')."\n";
  175. 2) Load only given tags. It will stop once all given tags are found. Fastest!
  176. $dicom = Nanodicom::factory($filename, 'simple');
  177. $dicom->parse(array(array(0x0010, 0x0010)));
  178. // Only a small subset of the dictionary entries were loaded
  179. echo $dicom->profiler_diff('parse')."\n";
  180. echo 'Patient name if exists: '.$dicom->value(0x0010, 0x0010)."\n"; // Patient Name if exists
  181. // This will return nothing because dictionaries were not loaded
  182. echo 'Patient name should be empty here: '.$dicom->PatientName."\n";
  183. 3) Load only given tags by name. Stops once all tags are found. Not so fast.
  184. $dicom = Nanodicom::factory($filename, 'simple');
  185. $dicom->parse(array('PatientName'));
  186. echo $dicom->profiler_diff('parse')."\n";
  187. echo 'Patient name if exists: '.$dicom->value(0x0010, 0x0010)."\n"; // Patient Name if exists
  188. // Or
  189. echo 'Patient name if exists: '.$dicom->PatientName."\n"; // Patient Name if exists
  190. 4) Load only given tags. Dump it and print certain tags. Load 'dumper' directly.
  191. $dicom = Nanodicom::factory($filename, 'dumper');
  192. $dicom->parse(array(array(0x0010, 0x0010)));
  193. echo $dicom->dump();
  194. echo $dicom->profiler_diff('parse')."\n";
  195. // Patient Name if exists
  196. echo 'Something should show if element exists.'.$dicom->value(0x0010, 0x0010)."\n";
  197. // This will return the value because 'dumper' was used and loaded the dictionaries
  198. echo 'This should be empty, no dictionaries loaded.'.$dicom->PatientName."\n";
  199. 5) Load simple and print certain value
  200. $dicom = Nanodicom::factory($filename);
  201. $dicom->parse();
  202. echo $dicom->profiler_diff('parse')."\n";
  203. echo 'Patient Name: '.$dicom->value(0x0010, 0x0010)."\n"; // Patient Name if exists
  204. 6) Load simple and extend it to 'dumper'
  205. $dicom = Nanodicom::factory($filename);
  206. echo $dicom->parse()->extend('dumper')->dump();
  207. echo $dicom->profiler_diff('parse').' '.$dicom->profiler_diff('dump')."\n";
  208. 7) Load simple and extend it to 'dumper'. No need to parse, dump() does it. Parsing is done only once.
  209. $dicom = Nanodicom::factory($filename);
  210. echo $dicom->extend('dumper')->dump();
  211. echo $dicom->profiler_diff('parse').' '.$dicom->profiler_diff('dump')."\n";
  212. 8) Load 'dumper' directly. Dump output is in html format.
  213. $dicom = Nanodicom::factory($filename, 'dumper');
  214. echo $dicom->parse()->dump('html');
  215. echo $dicom->profiler_diff('parse').' '.$dicom->profiler_diff('dump')."\n";
  216. 9) Load 'anonymizer' directly.
  217. $dicom = Nanodicom::factory($filename, 'anonymizer');
  218. file_put_contents($filename.'.ex9', $dicom->anonymize());
  219. 10) Extend 'anonymizer'. No need to call parse(), anonymize() will do it.
  220. $dicom = Nanodicom::factory($filename);
  221. file_put_contents($filename.'.ex10', $dicom->extend('anonymizer')->anonymize());
  222. 11) Double extension (and probably you can go on and on)
  223. $dicom = Nanodicom::factory($filename);
  224. echo $dicom->extend('dumper')->dump();
  225. file_put_contents($filename.'.ex11', $dicom->extend('anonymizer')->anonymize());
  226. 12) Save file as Explicit VR Little Endian. Read Additional notes below!
  227. $dicom = Nanodicom::factory($filename);
  228. echo $dicom->parse()->profiler_diff('parse')."\n";
  229. // Setting values takes care of even length
  230. // If set to '1.2.840.10008.1.2.1.99' it will use deflate
  231. $dicom->value(0x0002, 0x0010, $dicom::EXPLICIT_VR_LITTLE_ENDIAN);
  232. echo $dicom->write_file($filename.'.ex12')->profiler_diff('write')."\n";
  233. 13) Pass file contents instead of filename.
  234. $contents = file_get_contents(FILE);
  235. $dicom = Nanodicom::factory($contents, 'dumper', 'blob');
  236. echo $dicom->dump();
  237. 14) Check if file has preamble and DICM.
  238. $dicom = Nanodicom::factory($filename);
  239. echo 'Is DICOM? '.$dicom->is_dicom()."\n";
  240. 15) Anonymize and save file as Explicit VR Little Endian. Read Additional notes below!
  241. $dicom = Nanodicom::factory($filename);
  242. echo $dicom->parse()->profiler_diff('parse')."\n";
  243. // Setting values takes care of even length
  244. // If set to '1.2.840.10008.1.2.1.99' it will use deflate
  245. $dicom->value(0x0002, 0x0010, $dicom::EXPLICIT_VR_LITTLE_ENDIAN);
  246. file_put_contents($filename.'.ex15', $dicom->extend('anonymizer')->anonymize());
  247. 16) Provide your own dumper formatting
  248. $formatting = array(
  249. 'dataset_begin' => '',
  250. 'dataset_end' => "\n",
  251. 'item_begin' => '',
  252. 'item_end' => '',
  253. 'spacer' => ' ',
  254. 'text_begin' => '',
  255. 'text_end' => '',
  256. 'columns' => array(
  257. 'off' => array('%04X', ' '),
  258. 'g' => array('%04X', ':'),
  259. 'e' => array('%04X', ' '),
  260. 'name' => array('%-30.30s', ' '),
  261. 'vr' => array('%2s', ' '),
  262. 'len' => array('%-3d', ' '),
  263. 'val' => array('[%s]', ''),
  264. ),
  265. );
  266. $dicom = Nanodicom::factory($filename, 'dumper');
  267. echo $dicom->dump($formatting);
  268. 17) Anonymize on the fly and dump the contents, don't save to a file
  269. $dicom = Nanodicom::factory($filename, 'anonymizer');
  270. $dicom1 = Nanodicom::factory($dicom->anonymize(), 'dumper', 'blob');
  271. echo $dicom1->dump();
  272. unset($dicom);
  273. unset($dicom1);
  274. 18) Pass your own list of elements to anonymizer
  275. // Own tag elements for anonymizing
  276. $tags = array(
  277. array(0x0008, 0x0020, '{date|Ymd}'), // Study Date
  278. array(0x0008, 0x0021, '{date|Ymd}'), // Series Date
  279. array(0x0008, 0x0090, 'physician{random}'), // Referring Physician
  280. array(0x0010, 0x0010, 'patient{consecutive}'), // Patient Name
  281. array(0x0010, 0x0020, 'id{consecutive}'), // Patient ID
  282. array(0x0010, 0x0030, '{date|Ymd}'), // Patient Date of Birth
  283. );
  284. $dicom = Nanodicom::factory($filename, 'anonymizer');
  285. $dicom1 = Nanodicom::factory($dicom->anonymize($tags), 'dumper', 'blob');
  286. echo $dicom1->dump();
  287. unset($dicom);
  288. unset($dicom1);
  289. 19) Pass your own list of mappings to anonymizer. Patient Name should be replace to
  290. 'Mapped' if 'Anonymized' is found. Case sensitive
  291. // Own tag elements for anonymizing
  292. $tags = array(
  293. array(0x0008, 0x0020, '{date|Ymd}'), // Study Date
  294. array(0x0008, 0x0021, '{date|Ymd}'), // Series Date
  295. array(0x0008, 0x0090, 'physician{random}'), // Referring Physician
  296. array(0x0010, 0x0010, 'patient{consecutive}'), // Patient Name
  297. array(0x0010, 0x0020, 'id{consecutive}'), // Patient ID
  298. array(0x0010, 0x0030, '{date|Ymd}'), // Patient Date of Birth
  299. );
  300. $replacements = array(
  301. array(0x0010, 0x0010, 'anonymized', 'Mapped'),
  302. );
  303. $dicom = Nanodicom::factory($filename, 'anonymizer');
  304. $dicom1 = Nanodicom::factory($dicom->anonymize($tags, $replacements), 'dumper', 'blob');
  305. echo $dicom1->dump();
  306. file_put_contents($filename.'.ex19', $dicom1->write());
  307. unset($dicom);
  308. unset($dicom1);
  309. 20) Gets the images from the dicom object if they exist. This example is for gd
  310. $dicom = Nanodicom::factory($filename, 'pixeler');
  311. if ( ! file_exists($filename.'.0.jpg'))
  312. {
  313. $images = $dicom->get_images();
  314. // If using another library, for example, imagemagick, the following should be done:
  315. // $images = $dicom->set_driver('imagick')->get_images();
  316. if ($images !== FALSE)
  317. {
  318. foreach ($images as $index => $image)
  319. {
  320. // Defaults to jpg
  321. $test->write_image($image, $dir.$file.'.'.$index);
  322. // To write another format, pass the format in second parameter.
  323. // This will write a png image instead
  324. // $test->write_image($image, $dir.$file.'.'.$index, 'png');
  325. }
  326. }
  327. else
  328. {
  329. echo "There are no DICOM images or transfer syntax not supported yet.\n";
  330. }
  331. $images = NULL;
  332. }
  333. else
  334. {
  335. echo "Image already exists\n";
  336. }
  337. unset($dicom);
  338. 21) Prints summary report
  339. $dicom = Nanodicom::factory($filename);
  340. echo $dicom->summary();
  341. unset($dicom);
  342. 22) Proper way of handling exceptions
  343. try
  344. {
  345. // All other examples should be placed here
  346. $dicom = Nanodicom::factory('simple', $filename);
  347. echo $dicom->parse()->profiler_diff('parse')."\n";
  348. }
  349. catch (Nanodicom_Exception $e)
  350. {
  351. echo 'File failed. '.$e->getMessage()."\n";
  352. }
  353. Tests
  354. ---------------
  355. Basic tests have been added. They are run using phpunit (you need to have phpunit installed)
  356. To run (at the base directory of nanodicom)
  357. $ phpunit NanodicomCoreTest.php
  358. PHPUnit 3.4.5 by Sebastian Bergmann.
  359. ............
  360. Time: 2 seconds, Memory: 7.00Mb
  361. OK (12 tests, 12 assertions)
  362. Additional notes
  363. ---------------
  364. 1) Attention! No pixel data conversion is provided yet to change transfer syntaxes at will. Even though
  365. the toolkit DOES change the transfer syntax and the related encoding is verified (Implicit vs Explicit and
  366. Big vs Little Endian), encapsulated pixel data IS NOT CONVERTED! Thus, encapsulated Dicom files with a not
  367. encapsulated Transfer Syntax can lead to problems.
  368. 2) Extended tools get a copy of the current Nanodicom object at the moment of extension. Whatever happens in the
  369. extended tool is not kept in sync with the main (initially loaded) tool. This could lead to unexpected
  370. results. Thus, please treat the 'extend' feature still as experimental. Loading the right tool from the
  371. beginning (ie dumper or anonymizer) and performing any operations directly there is recommended.
  372. 3) In order to have the nanodcm tool running, you need to change the mode to 755. Run `chmod 0755 nanodcm` to
  373. achieve that.