@@ -3,45 +3,152 @@ Status](https://github.com/ImagingDataCommons/libdicom/actions/workflows/run_uni
33
44# libdicom
55
6- C library and executable tools for reading DICOM files.
6+ libdicom is a C library and a set of command-line tools
7+ for reading DICOM WSI files. It is free (MIT licensed),
8+ fast, cross-platform, uses little memory, has no dependencies, [ includes API
9+ documentation] ( http://www.rollthepotato.net/~john/libdicom/index.html ) ,
10+ and is [ easy to use from languages like
11+ Python] ( https://github.com/jcupitt/pylibdicom ) .
712
8- ## Getting started
13+ ![ A DICOM WSI being viewed via OpenSlide 4.0 ] ( data/vipsdisp.png )
914
10- ### Building from source
15+ libdicom returns compressed frame data, not RGB pixel arrays. OpenSlide 4.0
16+ uses libdicom to implement DICOM support and is a better choice if you want
17+ to process image files.
18+
19+ libdicom aims to support most popular DICOM WSI variants. If you have a
20+ sample file which does not work well, please [ open an issue and we'll try
21+ to add support] ( https://github.com/ImagingDataCommons/libdicom/issues ) .
22+
23+ ## Building from source
1124
1225``` shell
13- meson setup --buildtype release builddir
26+ cd libdicom-1.0.0
27+ meson setup builddir --buildtype release
1428meson compile -C builddir
1529meson install -C builddir
1630```
1731See [ the installation
18- documentation] ( https://libdicom.readthedocs.io/en/latest /installation.html )
32+ documentation] ( https:/http://www.rollthepotato.net/~john/libdicom /installation.html )
1933for build dependencies and installation options.
2034
21- ### Printing the metadata of a DICOM file
22-
23- ``` shell
24- dcm-dump data/test_files/sm_image.dcm
35+ ## Sample code
36+
37+ See [ the
38+ documentation] ( http://www.rollthepotato.net/~john/libdicom/index.html )
39+ for full details.
40+
41+ ``` c
42+ #include < stdlib.h>
43+ #include < dicom/dicom.h>
44+
45+ int main () {
46+ const char *file_path = "/path/to/file.dcm";
47+ DcmError *error = NULL;
48+
49+ DcmFilehandle *filehandle = dcm_filehandle_create_from_file(&error, file_path);
50+ if (filehandle == NULL) {
51+ dcm_error_log (error);
52+ dcm_error_clear(&error);
53+ return 1;
54+ }
55+
56+ const DcmDataSet *metadata =
57+ dcm_filehandle_get_metadata_subset (&error, filehandle);
58+ if (metadata == NULL) {
59+ dcm_error_log(error);
60+ dcm_error_clear(&error);
61+ dcm_filehandle_destroy(filehandle);
62+ return 1;
63+ }
64+
65+ const char *num_frames;
66+ uint32_t tag = dcm_dict_tag_from_keyword("NumberOfFrames");
67+ DcmElement *element = dcm_dataset_get(&error, metadata, tag);
68+ if (element == NULL ||
69+ !dcm_element_get_value_string(&error, element, 0, &num_frames)) {
70+ dcm_error_log (error);
71+ dcm_error_clear(&error);
72+ dcm_filehandle_destroy(filehandle);
73+ return 1;
74+ }
75+
76+ printf ("NumerOfFrames == %s\n", num_frames);
77+
78+ dcm_filehandle_destroy (filehandle);
79+
80+ return 0;
81+ }
2582```
2683
27- ### Fetching a frame from a file
84+ Or in Python:
85+
86+ ``` python
87+ import sys
88+ import pylibdicom
89+
90+ file = pylibdicom.Filehandle.create_from_file(sys.argv[1 ])
91+ metadata = file .get_metadata_subset()
92+ num_frames_tag = pylibdicom.Tag.create_from_keyword(" NumberOfFrames" )
93+ num_frames = int (metadata.get(num_frames_tag).get_value()[0 ])
94+ for frame_number in range (1 , num_frames + 1 ):
95+ frame = file .read_frame(frame_number)
96+ value = frame.get_value()
97+ print (f " frame { frame_number} -> { frame} { len (value)} bytes " )
98+
99+ # you can also read frames by (x, y) tile position ... this works for
100+ # TILED_FULL and for sparse images
101+ frame = file .read_frame_position(2 , 7 )
102+ value = frame.get_value()
103+ print (f " frame { 2 , 7 } -> { frame} { len (value)} bytes " )
104+ ```
28105
29- ``` shell
30- dcm-getframe -o tile.raw data/test_files/sm_image.dcm 12
106+ This will print:
107+
108+ ``` python
109+ $ ./ read- frames.py sm_image.dcm
110+ opening libdicom ...
111+ init for libdicom ...
112+ libdicom version: 1.0 .0
113+ frame 1 -> < 10x10 pixels, 8 bits, 3 bands, RGB > 300 bytes
114+ frame 2 -> < 10x10 pixels, 8 bits, 3 bands, RGB > 300 bytes
115+ frame 3 -> < 10x10 pixels, 8 bits, 3 bands, RGB > 300 bytes
116+ ...
31117```
32118
33- ### From Python
119+ # # Command-line tools
120+
121+ libdicom comes with two small command- line tools which can be useful for
122+ testing.
34123
35- There's a sample Python binding here:
124+ `dcm- dump` will print all metadata from a DICOM file . It' s fast, and can
125+ dump DICOM files of any size while using only a small amount of memory.
36126
37- https://github.com/ImagingDataCommons/pylibdicom
127+ For example:
38128
39- ## Documentation
129+ ```!
130+ $ dcm- dump sm_image.dcm
131+ == =File Meta Information== =
132+ (0002 ,0001 ) FileMetaInformationVersion | OB | 2 | 1 | 00 01
133+ (0002 ,0002 ) MediaStorageSOPClassUID | UI | 30 | 1 | 1.2 .840.10008.5.1.4.1.1.77.1.6
134+ (0002 ,0003 ) MediaStorageSOPInstanceUID | UI | 64 | 1 | 1.2 .826.0.1.3680043.9.7433.3.12857516184849951143044513877282227
135+ (0002 ,0010 ) TransferSyntaxUID | UI | 20 | 1 | 1.2 .840.10008.1.2.1
136+ (0002 ,0012 ) ImplementationClassUID | UI | 28 | 1 | 1.2 .826.0.1.3680043.9.7433.1
137+ (0002 ,0013 ) ImplementationVersionName | SH | 14 | 1 | wsiget v0.0.1
138+ ...
139+ ```
140+
141+ `dcm- getframe` will read a single frame from a DICOM file .
142+
143+ For example:
144+
145+ ```shell
146+ dcm- getframe - o tile.raw data/ test_files/ sm_image.dcm 12
147+ ```
40148
41- User and developer guides as well as API documentation can be found at
42- [ libdicom.readthedocs.io] ( https://libdicom.readthedocs.io/en/latest/ ) .
149+ To read frame 12 .
43150
44- # Thanks
151+ # # Thanks
45152
46153Development of this library was supported by [NCI Imaging Data
47154Commons](https:// imaging.datacommons.cancer.gov/ ), and has been funded in
0 commit comments