1 /*
2  * The GNU Octave dicom package is Copyright Andy Buckle 2010
3  * Contact: blondandy using the sf.net system,
4  * <https://sourceforge.net/sendmessage.php?touser=1760416>
5  *
6  * Changes (C) 2018 John Donoghue <john.donoghue.ieee.org>
7  *
8  * The GNU Octave dicom package is free software: you can redistribute
9  * it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, either
11  * version 3 of the License, or (at your option) any later version.
12  *
13  * The GNU Octave dicom package is distributed in the hope that it
14  * will be useful, but WITHOUT ANY WARRANTY; without even the
15  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16  * PURPOSE.  See the GNU General Public License for more details.
17  *
18  * Please see the file, "COPYING" for further details of GNU General
19  * Public License version 3.
20  *
21  */
22 
23 #include <octave/oct.h>
24 #include <octave/ov-struct.h>
25 #include <octave/file-stat.h>
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include "gdcmImageReader.h"
32 using namespace gdcm;
33 
34 static bool
isdicom(const std::string & filename)35 isdicom (const std::string& filename)
36 {
37   if (!filename.length ())
38     return false;
39 
40   gdcm::Reader reader;
41   reader.SetFileName (filename.c_str ());
42 
43   // gdcm::Reader.Read() will return false if the file does not exists but
44   // also prints to stderr so we check it first.
45   return OCTAVE__FILE_STAT (filename).exists () && reader.Read ();
46 }
47 
48 DEFUN_DLD (isdicom, args, ,
49 "-*- texinfo -*- \n\
50 @deftypefn {Loadable Function} {} isdicom (@var{filename}) \n\
51 Return true if @var{filename} is a valid DICOM file.\n\
52 \n\
53 @seealso{dicomdict, dicominfo, dicomread, dicomwrite} \n\
54 @end deftypefn")
55 {
56   const octave_idx_type nargin = args.length ();
57   if (nargin != 1)
58     {
59       print_usage ();
60       return octave_value_list ();
61     }
62   if (! args(0).is_string ())
63     {
64       error ("isdicom: FILENAME must be a string");
65       return octave_value_list ();
66     }
67   // Do NOT have this function accept a cell array of strings.
68   //
69   // This function should return true or false if its input is valid input
70   // for a function that reads a DICOM file.  While it is tempting, and very
71   // easy to implement, to have this function accept a cell array of strings
72   // and return a boolNDArray, a cell array is not valid and will cause any
73   // function that uses it to first check if it's a cell array.  It will also
74   // prevent it from being used by imformats and any other image IO function
75   // in Octave core
76   const std::string filename = args(0).string_value ();
77 
78   return octave_value (isdicom (filename));
79 }
80 
81 /*
82 %!test
83 %! fpath = tempname ();
84 %! urlwrite ("http://imagej.nih.gov/ij/images/CT%20Scan.dcm", fpath);
85 %! assert (isdicom (fpath), true);
86 
87 %!test
88 %! assert (isdicom (which ("gray")), false);
89 %! assert (isdicom (""), false);
90 
91 %!fail ("isdicom")
92 
93 %!fail ("isdicom(1)")
94 */
95