58static char *option[N_OPTIONS] = {
59 "maxcontrast",
"frompage",
"topage",
"columnPrefix",
"pipe",
"16bit"};
63 "sdds2tiff [-pipe[=input]] <input> <output> \n"
64 " [-fromPage=<pageNumber>] [-toPage=<pageNumber>]\n"
65 " [-columnPrefix=<Line>]\n"
66 " [-maxContrast] [-16bit]\n\n"
67 " Two styles of input files are accepted:\n"
68 " 1. A single column SDDS file with Variable1Name and Variable2Name parameters,\n"
69 " as well as <Variable1Name>Dimension and <Variable2Name>Dimension parameters.\n"
70 " 2. A file containing multiple columns called Line*.\n"
72 " Each page in the input file will be converted to a separate TIFF image.\n"
73 " The output files will be named <output>.%04ld\n\n"
74 " Program by Robert Soliday. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
77int main(
int argc,
char *argv[]) {
81 char **columnNames = NULL;
82 char *input = NULL, *output = NULL;
83 char *columnPrefix = NULL;
84 char *buffer = NULL, *outputName = NULL;
85 uint16_t *buffer16 = NULL;
86 int32_t **data = NULL;
100 char *xVar = NULL, *yVar = NULL;
101 char zColumnName[40];
102 char xDimName[40], yDimName[40];
106 unsigned long pipeFlags = 0;
107 long maxPossibleLong = 255;
108 double maxPossible = maxPossibleLong;
114 argc =
scanargs(&s_arg, argc, argv);
116 fprintf(stderr,
"%s", USAGE);
121 for (i = 1; i < argc; i++) {
122 if (s_arg[i].arg_type == OPTION) {
123 switch (
match_string(s_arg[i].list[0], option, N_OPTIONS, 0)) {
124 case OPT_MAXCONTRAST:
128 if (s_arg[i].n_items < 2) {
131 if (sscanf(s_arg[i].list[1],
"%ld", &fromPage) != 1 || fromPage <= 0) {
132 SDDS_Bomb(
"invalid -fromPage syntax or value");
136 if (s_arg[i].n_items < 2) {
139 if (sscanf(s_arg[i].list[1],
"%ld", &toPage) != 1 || toPage <= 0) {
140 SDDS_Bomb(
"invalid -toPage syntax or value");
143 case OPT_COLUMNPREFIX:
144 if (s_arg[i].n_items < 2) {
145 SDDS_Bomb(
"invalid -columnPrefix syntax");
151 fprintf(stderr,
"invalid -pipe syntax\n");
157 maxPossible = maxPossibleLong = 65535;
160 fprintf(stderr,
"sdds2tiff: invalid option seen\n%s", USAGE);
166 }
else if (!output) {
169 fprintf(stderr,
"sdds2tiff: too many filenames\n%s", USAGE);
175 if (fromPage && toPage && fromPage > toPage) {
176 SDDS_Bomb(
"invalid -fromPage and -toPage");
180 columnPrefix = malloc(5 *
sizeof(
char));
181 sprintf(columnPrefix,
"Line");
184 if (pipeFlags & USE_STDIN) {
191 outputName = malloc((strlen(output) + 10) *
sizeof(
char));
209 fprintf(stderr,
"sdds2tiff: Expected one column but found more than one\n");
212 sprintf(zColumnName,
"%s", columnNames[0]);
213 }
else if (style == 2) {
214 for (i = 0; i < nColumns; i++) {
215 if (strncmp(columnPrefix, columnNames[i], strlen(columnPrefix)) == 0) {
219 if (linesFound == 0) {
220 fprintf(stderr,
"sdds2tiff: No columns found named %s*\n", columnPrefix);
223 data = malloc(linesFound *
sizeof(*data));
227 while (SDDS_ReadTable(&SDDS_dataset) > 0) {
228 if ((fromPage > 0 && fromPage > page) || (toPage > 0 && toPage < page)) {
232 rows = SDDS_RowCount(&SDDS_dataset);
236 fprintf(stderr,
"sdds2tiff: problem getting parameter Variable1Name\n");
240 fprintf(stderr,
"sdds2tiff: problem getting parameter Variable2Name\n");
244 sprintf(xDimName,
"%sDimension", xVar);
245 sprintf(yDimName,
"%sDimension", yVar);
247 fprintf(stderr,
"sdds2tiff: problem getting parameter %s\n", xDimName);
251 fprintf(stderr,
"sdds2tiff: problem getting parameter %s\n", yDimName);
255 fprintf(stderr,
"%s %s\n", xVar, yVar);
256 fprintf(stderr,
"%" PRId64
" %" PRId32
"\n", xDim, yDim);
260 if (xDim * yDim != rows) {
261 fprintf(stderr,
"sdds2tiff: %s * %s does not equal the number of rows in the page\n", xDimName, yDimName);
265 data = malloc(
sizeof(*data));
267 for (i = 0; i < rows; i++) {
268 if (data[0][i] > maxvalue) {
269 maxvalue = data[0][i];
274 buffer = malloc(rows *
sizeof(
char));
276 buffer16 = malloc(rows *
sizeof(int16_t));
278 }
else if (style == 2) {
280 for (i = 0; i < nColumns; i++) {
281 if (strncmp(columnPrefix, columnNames[i], strlen(columnPrefix)) == 0) {
283 for (k = 0; k < rows; k++) {
284 if (data[j][k] > maxvalue) {
285 maxvalue = data[j][k];
293 buffer = malloc(rows * linesFound *
sizeof(
char));
295 buffer16 = malloc(rows * linesFound *
sizeof(uint16_t));
303 div = maxvalue / maxPossible;
304 }
else if (maxvalue <= maxPossibleLong) {
306 }
else if (maxvalue <= 3 * maxPossibleLong) {
309 div = maxvalue / maxPossible;
312 sprintf(outputName,
"%s.%04ld", output, index);
313 tif = TIFFOpen(outputName,
"w");
315 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, xDim);
316 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, yDim);
317 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bit16 ? 16 : 8);
318 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
319 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, yDim);
320 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
321 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
322 TIFFSetField(tif, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
323 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
324 TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
328 for (i = 0; i < xDim; i++) {
329 for (j = 0; j < yDim; j++) {
331 buffer[xDim * (yDim - (j + 1)) + i] = (
unsigned int)(round(data[0][k] / div));
333 buffer16[xDim * (yDim - (j + 1)) + i] = (uint16_t)(round(data[0][k] / div));
338 TIFFWriteEncodedStrip(tif, 0, bit16 ? (
char *)buffer16 : buffer, rows * (bit16 ? 2 : 1));
339 }
else if (style == 2) {
340 for (j = 0; j < yDim; j++) {
341 for (i = 0; i < xDim; i++) {
343 buffer[j * xDim + i] = (
unsigned int)(round(data[(yDim - j) - 1][i] / div));
345 buffer16[j * xDim + i] = (uint16_t)(round(data[(yDim - j) - 1][i] / div));
349 TIFFWriteEncodedStrip(tif, 0, bit16 ? (
char *)buffer16 : buffer, xDim * yDim * (bit16 ? 2 : 1));
363 }
else if (style == 2) {
364 for (j = 0; j < yDim; j++) {
386 for (i = 0; i < nColumns; i++) {
387 if (columnNames[i]) {
388 free(columnNames[i]);
396 if (style == 2 && data) {
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
char ** SDDS_GetColumnNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all columns in the SDDS dataset.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
int32_t SDDS_CheckParameter(SDDS_DATASET *SDDS_dataset, char *name, char *units, int32_t type, FILE *fp_message)
Checks if a parameter exists in the SDDS dataset with the specified name, units, and type.
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
#define SDDS_STRING
Identifier for the string data type.
long match_string(char *string, char **option, long n_options, long mode)
Matches a given string against an array of option strings based on specified modes.
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
long processPipeOption(char **item, long items, unsigned long *flags)
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
void free_scanargs(SCANNED_ARG **scanned, int argc)