64static char *option[N_OPTIONS] = {
65 "maxcontrast",
"frompage",
"topage",
"columnPrefix",
"pipe",
"16bit"};
69 "sdds2tiff [<input>] [<output>] \n"
71 " [-fromPage=<pageNumber>] \n"
72 " [-toPage=<pageNumber>]\n"
73 " [-columnPrefix=<Line>]\n"
76 " Two styles of input files are accepted:\n"
77 " 1. A single column SDDS file with Variable1Name and Variable2Name parameters,\n"
78 " as well as <Variable1Name>Dimension and <Variable2Name>Dimension parameters.\n"
79 " 2. A file containing multiple columns called Line*.\n"
81 " Each page in the input file will be converted to a separate TIFF image.\n"
82 " The output files will be named <output>.%04ld\n\n"
83 " Program by Robert Soliday. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
86int main(
int argc,
char *argv[]) {
90 char **columnNames = NULL;
91 char *input = NULL, *output = NULL;
92 char *columnPrefix = NULL;
93 char *buffer = NULL, *outputName = NULL;
94 uint16_t *buffer16 = NULL;
95 int32_t **data = NULL;
101 int32_t nColumns = 0;
109 char *xVar = NULL, *yVar = NULL;
110 char zColumnName[40];
111 char xDimName[40], yDimName[40];
115 unsigned long pipeFlags = 0;
116 long maxPossibleLong = 255;
117 double maxPossible = maxPossibleLong;
123 argc =
scanargs(&s_arg, argc, argv);
125 fprintf(stderr,
"%s", USAGE);
130 for (i = 1; i < argc; i++) {
131 if (s_arg[i].arg_type == OPTION) {
132 switch (
match_string(s_arg[i].list[0], option, N_OPTIONS, 0)) {
133 case OPT_MAXCONTRAST:
137 if (s_arg[i].n_items < 2) {
140 if (sscanf(s_arg[i].list[1],
"%ld", &fromPage) != 1 || fromPage <= 0) {
141 SDDS_Bomb(
"invalid -fromPage syntax or value");
145 if (s_arg[i].n_items < 2) {
148 if (sscanf(s_arg[i].list[1],
"%ld", &toPage) != 1 || toPage <= 0) {
149 SDDS_Bomb(
"invalid -toPage syntax or value");
152 case OPT_COLUMNPREFIX:
153 if (s_arg[i].n_items < 2) {
154 SDDS_Bomb(
"invalid -columnPrefix syntax");
160 fprintf(stderr,
"invalid -pipe syntax\n");
166 maxPossible = maxPossibleLong = 65535;
169 fprintf(stderr,
"sdds2tiff: invalid option seen\n%s", USAGE);
175 }
else if (!output) {
178 fprintf(stderr,
"sdds2tiff: too many filenames\n%s", USAGE);
184 if (fromPage && toPage && fromPage > toPage) {
185 SDDS_Bomb(
"invalid -fromPage and -toPage");
189 columnPrefix = malloc(5 *
sizeof(
char));
190 sprintf(columnPrefix,
"Line");
193 if (pipeFlags & USE_STDIN) {
200 outputName = malloc((strlen(output) + 10) *
sizeof(
char));
218 fprintf(stderr,
"sdds2tiff: Expected one column but found more than one\n");
221 sprintf(zColumnName,
"%s", columnNames[0]);
222 }
else if (style == 2) {
223 for (i = 0; i < nColumns; i++) {
224 if (strncmp(columnPrefix, columnNames[i], strlen(columnPrefix)) == 0) {
228 if (linesFound == 0) {
229 fprintf(stderr,
"sdds2tiff: No columns found named %s*\n", columnPrefix);
232 data = malloc(linesFound *
sizeof(*data));
236 while (SDDS_ReadTable(&SDDS_dataset) > 0) {
237 if ((fromPage > 0 && fromPage > page) || (toPage > 0 && toPage < page)) {
241 rows = SDDS_RowCount(&SDDS_dataset);
245 fprintf(stderr,
"sdds2tiff: problem getting parameter Variable1Name\n");
249 fprintf(stderr,
"sdds2tiff: problem getting parameter Variable2Name\n");
253 sprintf(xDimName,
"%sDimension", xVar);
254 sprintf(yDimName,
"%sDimension", yVar);
256 fprintf(stderr,
"sdds2tiff: problem getting parameter %s\n", xDimName);
260 fprintf(stderr,
"sdds2tiff: problem getting parameter %s\n", yDimName);
264 fprintf(stderr,
"%s %s\n", xVar, yVar);
265 fprintf(stderr,
"%" PRId64
" %" PRId32
"\n", xDim, yDim);
269 if (xDim * yDim != rows) {
270 fprintf(stderr,
"sdds2tiff: %s * %s does not equal the number of rows in the page\n", xDimName, yDimName);
274 data = malloc(
sizeof(*data));
276 for (i = 0; i < rows; i++) {
277 if (data[0][i] > maxvalue) {
278 maxvalue = data[0][i];
283 buffer = malloc(rows *
sizeof(
char));
285 buffer16 = malloc(rows *
sizeof(int16_t));
287 }
else if (style == 2) {
289 for (i = 0; i < nColumns; i++) {
290 if (strncmp(columnPrefix, columnNames[i], strlen(columnPrefix)) == 0) {
292 for (k = 0; k < rows; k++) {
293 if (data[j][k] > maxvalue) {
294 maxvalue = data[j][k];
302 buffer = malloc(rows * linesFound *
sizeof(
char));
304 buffer16 = malloc(rows * linesFound *
sizeof(uint16_t));
312 div = maxvalue / maxPossible;
313 }
else if (maxvalue <= maxPossibleLong) {
315 }
else if (maxvalue <= 3 * maxPossibleLong) {
318 div = maxvalue / maxPossible;
321 sprintf(outputName,
"%s.%04ld", output, index);
322 tif = TIFFOpen(outputName,
"w");
324 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, xDim);
325 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, yDim);
326 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bit16 ? 16 : 8);
327 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
328 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, yDim);
329 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
330 TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
331 TIFFSetField(tif, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
332 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
333 TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
337 for (i = 0; i < xDim; i++) {
338 for (j = 0; j < yDim; j++) {
340 buffer[xDim * (yDim - (j + 1)) + i] = (
unsigned int)(round(data[0][k] / div));
342 buffer16[xDim * (yDim - (j + 1)) + i] = (uint16_t)(round(data[0][k] / div));
347 TIFFWriteEncodedStrip(tif, 0, bit16 ? (
char *)buffer16 : buffer, rows * (bit16 ? 2 : 1));
348 }
else if (style == 2) {
349 for (j = 0; j < yDim; j++) {
350 for (i = 0; i < xDim; i++) {
352 buffer[j * xDim + i] = (
unsigned int)(round(data[(yDim - j) - 1][i] / div));
354 buffer16[j * xDim + i] = (uint16_t)(round(data[(yDim - j) - 1][i] / div));
358 TIFFWriteEncodedStrip(tif, 0, bit16 ? (
char *)buffer16 : buffer, xDim * yDim * (bit16 ? 2 : 1));
372 }
else if (style == 2) {
373 for (j = 0; j < yDim; j++) {
395 for (i = 0; i < nColumns; i++) {
396 if (columnNames[i]) {
397 free(columnNames[i]);
405 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)