cimg.cxx 3.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,196 @@
1
+ /*
2
+ #
3
+ # File : tiff_stream.h
4
+ # ( C++ header file - CImg plug-in )
5
+ #
6
+ # Description : This CImg plug-in provide functions to load and save tiff images
7
+ # from std::istream/ to std::ostream
8
+ # This file is a part of the CImg Library project.
9
+ # ( http://cimg.eu )
10
+ #
11
+ # Copyright : Wolf Blecher
12
+ # ( Wolf.Blecher(at)sirona.com )
13
+ #
14
+ # License : CeCILL v2.0
15
+ # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
16
+ #
17
+ # This software is governed by the CeCILL license under French law and
18
+ # abiding by the rules of distribution of free software. You can use,
19
+ # modify and/ or redistribute the software under the terms of the CeCILL
20
+ # license as circulated by CEA, CNRS and INRIA at the following URL
21
+ # "http://www.cecill.info".
22
+ #
23
+ # As a counterpart to the access to the source code and rights to copy,
24
+ # modify and redistribute granted by the license, users are provided only
25
+ # with a limited warranty and the software's author, the holder of the
26
+ # economic rights, and the successive licensors have only limited
27
+ # liability.
28
+ #
29
+ # In this respect, the user's attention is drawn to the risks associated
30
+ # with loading, using, modifying and/or developing or reproducing the
31
+ # software by the user in light of its specific status of free software,
32
+ # that may mean that it is complicated to manipulate, and that also
33
+ # therefore means that it is reserved for developers and experienced
34
+ # professionals having in-depth computer knowledge. Users are therefore
35
+ # encouraged to load and test the software's suitability as regards their
36
+ # requirements in conditions enabling the security of their systems and/or
37
+ # data to be ensured and, more generally, to use and operate it in the
38
+ # same conditions as regards security.
39
+ #
40
+ # The fact that you are presently reading this means that you have had
41
+ # knowledge of the CeCILL license and that you accept its terms.
42
+ #
43
+ */
44
+
45
+ /*-----------------------------------------------------------------------------------
46
+
47
+ IMPORTANT NOTE :
48
+
49
+ You *need* to include the following lines in your own code to use this plugin:
50
+
51
+ #include "tiffio.h"
52
+ #include "tiffio.hxx"
53
+
54
+ You *need* to link your code with the libtiff and libtiffxx libraries as well.
55
+
56
+ ------------------------------------------------------------------------------------*/
57
+
58
+ #ifndef cimg_use_tiff
59
+ #error cimg_use_tiff not defined
60
+ #endif
61
+
62
+ #ifndef cimg_plugin_tiff_stream
63
+ #define cimg_plugin_tiff_stream
64
+
65
+ #include <ios>
66
+
67
+ /////////////////////////////////////////////////////////////////
68
+ //
69
+ // Define main CImg plugin functions.
70
+ // (you should use these functions only in your own code)
71
+ //
72
+ /////////////////////////////////////////////////////////////////
73
+
74
+ //! Save image as a TIFF file.
75
+ /**
76
+ \param tiffOutStream std::ostream, where to write the image to
77
+ \param compression_type Type of data compression.
78
+ Can be <tt>{ 1=None | 2=CCITTRLE | 3=CCITTFAX3 | 4=CCITTFAX4 | 5=LZW | 6=JPEG }</tt>.
79
+ \note
80
+ - libtiff support is enabled by defining the precompilation
81
+ directive \c cimg_use_tiff.
82
+ - When libtiff is enabled, 2D and 3D (multipage) several
83
+ channel per pixel are supported for
84
+ <tt>char,uchar,short,ushort,float</tt> and \c double pixel types.
85
+ **/
86
+ const CImg<T>& save_tiff(std::ostream *tiffOutStream, const unsigned int compression_type=0) const {
87
+ if (!tiffOutStream->good())
88
+ {
89
+ throw CImgArgumentException(_cimg_instance
90
+ "save_tiff(): tiffstream is not good!",
91
+ cimg_instance);
92
+ }
93
+
94
+ if (is_empty())
95
+ {
96
+ throw CImgArgumentException(_cimg_instance
97
+ "Not allowed to write empty images to stream",
98
+ cimg_instance
99
+ );
100
+ }
101
+
102
+ TIFF *tif = TIFFStreamOpen("MemTiff", tiffOutStream);
103
+ if (tif)
104
+ {
105
+ cimg_forZ(*this,z) {
106
+ CImg<T> slice = get_slice(z);
107
+ double val_min, val_max = (double)slice.max_min(val_min);
108
+ slice._save_tiff(tif,z,z,compression_type,0,0,val_min,val_max);
109
+ }
110
+ tiffOutStream->flush();
111
+ TIFFClose(tif);
112
+ }
113
+ else
114
+ {
115
+ throw CImgIOException(_cimg_instance
116
+ "save_tiff(): Failed to stream for writing.",
117
+ cimg_instance);
118
+ }
119
+
120
+ return *this;
121
+ }
122
+
123
+ //! Load images from a TIFF file.
124
+ /**
125
+ \param tiffInStream std::istream to read data from.
126
+ \param first_frame Index of first image frame to read.
127
+ \param last_frame Index of last image frame to read.
128
+ \param step_frame Step applied between each frame.
129
+ **/
130
+ CImg<T>& load_tiff(std::istream* tiffInStream,
131
+ const unsigned int first_frame=0, const unsigned int last_frame=~0U,
132
+ const unsigned int step_frame=1)
133
+ {
134
+ const unsigned int
135
+ nfirst_frame = first_frame<last_frame?first_frame:last_frame,
136
+ nstep_frame = step_frame?step_frame:1;
137
+ unsigned int nlast_frame = first_frame<last_frame?last_frame:first_frame;
138
+
139
+ TIFF *tif = TIFFStreamOpen("MemTiff", tiffInStream);
140
+ if (tif)
141
+ {
142
+ unsigned int nb_images = 0;
143
+ do {
144
+ ++nb_images;
145
+ } while (TIFFReadDirectory(tif));
146
+
147
+ if (nfirst_frame>=nb_images || (nlast_frame!=~0U && nlast_frame>=nb_images))
148
+ {
149
+ cimg::warn("load_tiff(): Invalid specified frame range is [%u,%u] (step %u) "
150
+ "since stream contains %u image(s).",
151
+ nfirst_frame,nlast_frame,nstep_frame,nb_images);
152
+ }
153
+
154
+ if (nfirst_frame>=nb_images)
155
+ {
156
+ return assign();
157
+ }
158
+
159
+ if (nlast_frame>=nb_images)
160
+ {
161
+ nlast_frame = nb_images - 1;
162
+ }
163
+ TIFFSetDirectory(tif,0);
164
+ CImg<T> frame;
165
+ for (unsigned int l = nfirst_frame; l<=nlast_frame; l+=nstep_frame) {
166
+ frame._load_tiff(tif,l,0,0,0);
167
+ if (l==nfirst_frame)
168
+ assign(frame._width,frame._height,1 + (nlast_frame - nfirst_frame)/nstep_frame,frame._spectrum);
169
+ if (frame._width>_width || frame._height>_height || frame._spectrum>_spectrum)
170
+ resize(std::max(frame._width,_width),std::max(frame._height,_height),
171
+ -100,std::max(frame._spectrum,_spectrum),0);
172
+ draw_image(0,0,(l - nfirst_frame)/nstep_frame,frame);
173
+ }
174
+ TIFFClose(tif);
175
+ }
176
+ else
177
+ {
178
+ throw CImgIOException(_cimg_instance
179
+ "load_tiff(): Failed to read data from stream",
180
+ cimg_instance);
181
+ }
182
+
183
+ return *this;
184
+ }
185
+
186
+ //! Load a multi-page TIFF file \newinstance.
187
+ static CImg<T> get_load_tiff(std::istream* tiffInStream,
188
+ const unsigned int first_frame=0, const unsigned int last_frame=~0U,
189
+ const unsigned int step_frame=1)
190
+ {
191
+ return CImg<T>().load_tiff(tiffInStream,first_frame,last_frame,step_frame);
192
+ }
193
+
194
+ // End of the plug-in
195
+ //-------------------
196
+ #endif /* cimg_plugin_tiff_stream */
@@ -0,0 +1,109 @@
1
+ /*
2
+ #
3
+ # File : tinymatwriter.h
4
+ # ( C++ header file - CImg plug-in )
5
+ #
6
+ # Description : This CImg plug-in provide functions to write image as
7
+ # Matlab MAT files
8
+ # ( http://cimg.eu )
9
+ #
10
+ # Copyright : Jan W. Krieger
11
+ # ( j.krieger(at)dkfz.de jan(at)jkrieger.de )
12
+ #
13
+ # License : CeCILL v2.0
14
+ # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
15
+ #
16
+ # This software is governed by the CeCILL license under French law and
17
+ # abiding by the rules of distribution of free software. You can use,
18
+ # modify and/ or redistribute the software under the terms of the CeCILL
19
+ # license as circulated by CEA, CNRS and INRIA at the following URL
20
+ # "http://www.cecill.info".
21
+ #
22
+ # As a counterpart to the access to the source code and rights to copy,
23
+ # modify and redistribute granted by the license, users are provided only
24
+ # with a limited warranty and the software's author, the holder of the
25
+ # economic rights, and the successive licensors have only limited
26
+ # liability.
27
+ #
28
+ # In this respect, the user's attention is drawn to the risks associated
29
+ # with loading, using, modifying and/or developing or reproducing the
30
+ # software by the user in light of its specific status of free software,
31
+ # that may mean that it is complicated to manipulate, and that also
32
+ # therefore means that it is reserved for developers and experienced
33
+ # professionals having in-depth computer knowledge. Users are therefore
34
+ # encouraged to load and test the software's suitability as regards their
35
+ # requirements in conditions enabling the security of their systems and/or
36
+ # data to be ensured and, more generally, to use and operate it in the
37
+ # same conditions as regards security.
38
+ #
39
+ # The fact that you are presently reading this means that you have had
40
+ # knowledge of the CeCILL license and that you accept its terms.
41
+ #
42
+ */
43
+
44
+ /*-----------------------------------------------------------------------------------
45
+
46
+ IMPORTANT NOTE :
47
+
48
+ You *need* to compile tinymatwriter.cpp and link the result to your project and
49
+ include "tinymatwriter.h" berfore inclusing CIMg or this plugin!
50
+
51
+ This library is available from:
52
+ https://github.com/jkriege2/TinyMAT
53
+ ------------------------------------------------------------------------------------*/
54
+
55
+
56
+
57
+ #ifndef cimg_plugin_tinymatwriter
58
+ #define cimg_plugin_tinymatwriter
59
+
60
+ #include<cstdint>
61
+
62
+ /////////////////////////////////////////////////////////////////
63
+ //
64
+ // Define main CImg plugin functions.
65
+ // (you should use these functions only in your own code)
66
+ //
67
+ /////////////////////////////////////////////////////////////////
68
+
69
+ //! Save image as a MAT file.
70
+ /**
71
+ \param filename filename of the output file
72
+ \note TinyMATWriter supports signed/unsigned int with 8/16/32/64 bits, double, float and bool as pixel types!
73
+ **/
74
+ const CImg& save_tinymat(const char *filename) const {
75
+
76
+ TinyMATWriterFile* mat=TinyMATWriter_open(filename);
77
+ if (mat) {
78
+ int32_t size_x=width();
79
+ int32_t size_y=height();
80
+ int32_t size_z=depth();
81
+ int32_t size_c=spectrum();
82
+
83
+ int32_t sizes[4]={size_x, size_y, size_z, size_c};
84
+ uint32_t dims=4;
85
+ if (size_c==1) {
86
+ dims=3;
87
+ if (size_z==1) {
88
+ dims=2;
89
+ if (size_y==1) {
90
+ dims=1;
91
+ }
92
+ }
93
+ }
94
+
95
+ TinyMATWriter_writeMatrixND_rowmajor(mat, "CImg_image", data(), sizes, dims);
96
+ TinyMATWriter_close(mat);
97
+ } else {
98
+ throw CImgIOException(_cimg_instance
99
+ "save_tinymat(): Failed to open file.",
100
+ cimg_instance);
101
+ }
102
+
103
+ return *this;
104
+ }
105
+
106
+
107
+ // End of the plug-in
108
+ //-------------------
109
+ #endif /* cimg_plugin_tinymatwriter */