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 **/
save_tinymat(const char * filename)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 */
110