1 /*
2   Teem: Tools to process and visualize scientific data and images             .
3   Copyright (C) 2012, 2011, 2010, 2009  University of Chicago
4   Copyright (C) 2008, 2007, 2006, 2005  Gordon Kindlmann
5   Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998  University of Utah
6 
7   This library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Lesser General Public License
9   (LGPL) as published by the Free Software Foundation; either
10   version 2.1 of the License, or (at your option) any later version.
11   The terms of redistributing and/or modifying this software also
12   include exceptions to the LGPL that facilitate static linking.
13 
14   This library is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   Lesser General Public License for more details.
18 
19   You should have received a copy of the GNU Lesser General Public License
20   along with this library; if not, write to Free Software Foundation, Inc.,
21   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 */
23 
24 #include "unrrdu.h"
25 #include "privateUnrrdu.h"
26 
27 #define INFO "Write nrrd with specific format, encoding, or endianness"
28 static const char *_unrrdu_saveInfoL =
29 (INFO
30  ". Use \"unu\tsave\t-f\tpnm\t|\txv\t-\" to view PPM- or "
31  "PGM-compatible nrrds on unix.  EPS output is a EPSF-3.0 file with "
32  "BoundingBox and HiResBoundingBox DSC comments, and is suitable for "
33  "inclusion into other PostScript documents.  As a stand-alone file, the "
34  "image is conveniently centered on an 8.5x11 inch page, with 0.5 "
35  "inch margins.\n "
36  "* Uses various fields in the NrrdIOState passed to nrrdSave");
37 
38 int
unrrdu_saveMain(int argc,const char ** argv,const char * me,hestParm * hparm)39 unrrdu_saveMain(int argc, const char **argv, const char *me,
40                 hestParm *hparm) {
41   hestOpt *opt = NULL;
42   char *out, *err, *outData,
43     encInfo[AIR_STRLEN_HUGE], fmtInfo[AIR_STRLEN_HUGE];
44   Nrrd *nin, *nout;
45   airArray *mop;
46   NrrdIoState *nio;
47   int pret, enc[3], formatType;
48 
49   mop = airMopNew();
50   nio = nrrdIoStateNew();
51   airMopAdd(mop, nio, (airMopper)nrrdIoStateNix, airMopAlways);
52 
53   strcpy(fmtInfo,
54          "output file format. Possibilities include:\n "
55          "\b\bo \"nrrd\": standard nrrd format\n "
56          "\b\bo \"pnm\": PNM image; PPM for color, PGM for grayscale\n "
57          "\b\bo \"text\": plain ASCII text for 1-D and 2-D data\n "
58          "\b\bo \"vtk\": VTK \"STRUCTURED_POINTS\" dataset");
59   if (nrrdFormatPNG->available()) {
60     strcat(fmtInfo,
61            "\n \b\bo \"png\": PNG image");
62   }
63   strcat(fmtInfo,
64          "\n \b\bo \"eps\": EPS file");
65   hestOptAdd(&opt, "f,format", "form", airTypeEnum, 1, 1, &formatType, NULL,
66              fmtInfo, NULL, nrrdFormatType);
67   strcpy(encInfo,
68          "encoding of data in file.  Not all encodings are supported in "
69          "a given format. Possibilities include:"
70          "\n \b\bo \"raw\": raw encoding"
71          "\n \b\bo \"ascii\": print data in ascii"
72          "\n \b\bo \"hex\": two hex digits per byte");
73   if (nrrdEncodingGzip->available()) {
74     strcat(encInfo,
75            "\n \b\bo \"gzip\", \"gz\": gzip compressed raw data");
76   }
77   if (nrrdEncodingBzip2->available()) {
78     strcat(encInfo,
79            "\n \b\bo \"bzip2\", \"bz2\": bzip2 compressed raw data");
80   }
81   if (nrrdEncodingGzip->available() || nrrdEncodingBzip2->available()) {
82     strcat(encInfo,
83            "\n The specifiers for compressions may be followed by a colon "
84            "\":\", followed by an optional digit giving compression \"level\" "
85            "(for gzip) or \"block size\" (for bzip2).  For gzip, this can be "
86            "followed by an optional character for a compression strategy:\n "
87            "\b\bo \"d\": default, Huffman with string match\n "
88            "\b\bo \"h\": Huffman alone\n "
89            "\b\bo \"f\": specialized for filtered data\n "
90            "For example, \"gz\", \"gz:9\", \"gz:9f\" are all valid");
91   }
92   hestOptAdd(&opt, "e,encoding", "enc", airTypeOther, 1, 1, enc, "raw",
93              encInfo, NULL, NULL, &unrrduHestEncodingCB);
94   hestOptAdd(&opt, "en,endian", "end", airTypeEnum, 1, 1, &(nio->endian),
95              airEnumStr(airEndian, airMyEndian()),
96              "Endianness to save data out as; \"little\" for Intel and "
97              "friends; \"big\" for everyone else. "
98              "Defaults to endianness of this machine",
99              NULL, airEndian);
100   OPT_ADD_NIN(nin, "input nrrd");
101   OPT_ADD_NOUT(out, "output nrrd");
102   hestOptAdd(&opt, "od,ouputdata", "name", airTypeString, 1, 1, &outData, "",
103              "when saving to a \".nhdr\" file, "
104              "this option allows you to explicitly name the data file, "
105              "instead of (by default, not using this option) having it be "
106              "the same filename base as the header file.");
107 
108   airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways);
109 
110   USAGE(_unrrdu_saveInfoL);
111   PARSE();
112   airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways);
113   nout = nrrdNew();
114   airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
115 
116   nrrdCopy(nout, nin);
117 
118   nio->encoding = nrrdEncodingArray[enc[0]];
119   nio->format = nrrdFormatArray[formatType];
120   if (nrrdEncodingTypeGzip == enc[0]) {
121     nio->zlibLevel = enc[1];
122     nio->zlibStrategy = enc[2];
123   } else if (nrrdEncodingTypeBzip2 == enc[0]) {
124     nio->bzip2BlockSize = enc[1];
125   }
126   if (airMyEndian() != nio->endian) {
127     nrrdSwapEndian(nout);
128   }
129 
130   if (airEndsWith(out, NRRD_EXT_NHDR)) {
131     if (nio->format != nrrdFormatNRRD) {
132       fprintf(stderr, "%s: WARNING: will use %s format\n", me,
133               nrrdFormatNRRD->name);
134       nio->format = nrrdFormatNRRD;
135     }
136     if (strlen(outData)) {
137       airArrayLenSet(nio->dataFNArr, 1);
138       nio->dataFN[0] = airStrdup(outData);
139     }
140   }
141 
142   SAVE(out, nout, nio);
143 
144   airMopOkay(mop);
145   return 0;
146 }
147 
148 UNRRDU_CMD(save, INFO);
149