c++ - How i can extract information from DICOM file ? -
i want write script extract header information of dicom file using c or c ++, don't want use external libraries dicomsdl... when open file bloc-notes see special characters , character string patient name .. if can me read file.
yes, open file in binary though might contain sequences of characters. out deep it, consider writing following record of out file (i'm showing record c-struct):
struct rec_tag { int id; char name[50]; };
now, suppose use structure create file, shown in following code:
file1.c:
/* compile as: gcc -ansi -pedantic -wall file.c -o file_test */ #include <stdio.h> #include <stdlib.h> #include <string.h> struct rec_tag { int id; char name[50]; }; int main(int argc, char** argv) { file* fp = null; struct rec_tag rec1; struct rec_tag rec2; rec1.id = 20; strcpy(rec1.name, "thurizas"); rec2.id = 345689; strcpy(rec2.name, "marouane"); if(null != (fp = fopen("./short.dat", "ab"))) { fwrite(&rec1, sizeof(struct rec_tag), 1, fp); fwrite(&rec2, sizeof(struct rec_tag), 1, fp); fclose(fp); } return 0; }
now, suppose open file in emacs, lots of special symbols (such ^t , ^@) strings interspersed strings. can instructive open file in hex editor (say okteta) , see:
14 00 00 00 74 68 75 72 69 7a 61 73 00 00 00 00 01 00 00 00 00 00 00 00 ed 06 40 00 00 00 00 00 c2 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 a0 06 40 00 00 00 00 00 59 46 05 00 4d 61 72 6f 75 61 6e 65 00 7f 00 00 2e 4e 3d f6 00 00 00 00 67 03 40 00 00 00 00 00 ff ff ff ff 00 00 00 00 c0 b5 b3 c5 ff 7f 00 00 38 f1 ca 31 7f 00 00
now, sequence of hex digits 74 68 75 72 69 7a 61 73
ascii codes "thurizas" (which editors display). first 4 bytes in file id number. present (potential) issue, created file on computer x86_64 process, , integer stored in memory in little-endian form, sequence 14 00 00 00
needs read ... backwards (for lack of better term) 00 00 00 14
32-bit hexadecimal representation of 20.
also, notice because not particularly careful on how treated character arrays there extraneous garbage bytes in file.
now, out knowing format of file (i.e. how data written file), have hard time figure out how read in. however, because know format can write simple program read it:
file1.c:
/* compile as: gcc -ansi -pedantic -wall file1.c -o read_test */ #include <stdio.h> #include <stdlib.h> #include <string.h> struct rec_tag { int id; char name[50]; }; int main(int argc, char** argv) { file* fp = null; struct rec_tag rec1; struct rec_tag rec2; if(null != (fp = fopen("./short.dat", "rb"))) { fread(&rec1, sizeof(struct rec_tag), 1, fp); fread(&rec2, sizeof(struct rec_tag), 1, fp); printf("id: %d, name: %s\n", rec1.id, rec1.name); printf("id: %d, name: %s\n", rec2.id, rec2.name); fclose(fp); } return 0; }
and when run, produces result:
[******@broadsword junk]$ ./read_test id: 20, name: thurizas id: 345689, name: marouane
hopefully, helps on how interpret file , shows 1 way of reading in. in situation, following steps
- get , read formal specification dicom file.
- try "hand" read of file. open file in hex editor, , using specification see if can step through file , figure out how data stored.
- write program read in data.
finally, disclaimers:
- all code compiled using gcc version 4.8.2 , run on centos 7 system.
- i know
b
flag fopen , fread ignored on posix compliant systems (including linux), put there in case code run on non-posix system, , explicit doing binary i/o - error checking , handling kept minimum prevent post becoming wall-of-text (which did).
hope helps, t.
Comments
Post a Comment