75#define PARM_TRANSPOSE 4
85#define PARM_MULTICOLUMNMODE 14
100int multiColumnMode = 0;
112 {
"-2d", 3, PARM_2D,
"Output SDDS file as 2D array"},
113 {
"-debug", 6, PARM_DEBUG,
"Enable debug mode"},
114 {
"-xdim", 5, PARM_XDIM,
"Set X dimension of the image"},
115 {
"-ydim", 5, PARM_YDIM,
"Set Y dimension of the image"},
116 {
"-transpose", 10, PARM_TRANSPOSE,
"Transpose the image"},
117 {
"-ascii", 6, PARM_ASCII,
"Output SDDS file as ASCII"},
118 {
"-contour", 8, PARM_CONTOUR,
"Generate SDDS headers for sddscontour tool"},
119 {
"-help", 5, PARM_HELP,
"Display usage information"},
120 {
"-?", 2, PARM_QMARK,
"Display usage information"},
121 {
"-xmin", 5, PARM_XMIN,
"Set minimum X value"},
122 {
"-ymin", 5, PARM_YMIN,
"Set minimum Y value"},
123 {
"-xmax", 5, PARM_XMAX,
"Set maximum X value"},
124 {
"-ymax", 5, PARM_YMAX,
"Set maximum Y value"},
125 {
"-multicolumnmode", 16, PARM_MULTICOLUMNMODE,
"Enable multi-column mode"},
126 {NULL, -1, -1, NULL}};
129int process_cmdline_args(
int argc,
char *argv[],
char *infile,
char *outfile);
130void usage(
char *name);
132int main(
int argc,
char *argv[]) {
134 short *rotimage = NULL;
143 double xInterval = 0.02;
144 double yInterval = 0.02;
145 double *indexes = NULL;
146 char **columnNames = NULL;
147 int32_t *data = NULL;
149 process_cmdline_args(argc, argv, infile, outfile);
159 image = (
short *)malloc(
sizeof(
short) * xDim * yDim);
161 fprintf(stderr,
"%s: Error allocating memory for image.\n", argv[0]);
165 rotimage = (
short *)malloc(
sizeof(
short) * xDim * yDim);
167 fprintf(stderr,
"%s: Error allocating memory for rotated image.\n", argv[0]);
172 if ((infilefp = fopen(infile,
"rb")) == NULL) {
173 fprintf(stderr,
"%s: Error: unable to open input file: %s\n", argv[0], infile);
178 for (x = 0; x < xDim * yDim; x++) {
179 int byte = getc(infilefp);
181 fprintf(stderr,
"%s: Unexpected EOF at index %d\n", argv[0], x);
184 image[x] = (short)
byte;
190 for (x = 0; x < xDim; x++) {
191 for (y = 0; y < yDim; y++) {
192 rotimage[y * xDim + x] = image[i];
200 sprintf(yDimStr,
"%d", xDim);
201 sprintf(xDimStr,
"%d", yDim);
203 sprintf(xDimStr,
"%d", xDim);
204 sprintf(yDimStr,
"%d", yDim);
209 fprintf(stderr,
"%s: SDDS error: unable to initialize output file %s\n", argv[0], outfile);
215 fprintf(stderr,
"%s: SDDS error: unable to initialize output file %s\n", argv[0], outfile);
223 xInterval = (xMax - xMin) / (yDim - 1);
225 yInterval = (yMax - yMin) / (xDim - 1);
228 xInterval = (xMax - xMin) / (xDim - 1);
230 yInterval = (yMax - yMin) / (yDim - 1);
234 if (!multiColumnMode) {
237 fprintf(stderr,
"%s: SDDS error: unable to define parameter 1: Variable1Name\n", argv[0]);
243 fprintf(stderr,
"%s: SDDS error: unable to define parameter 2: Variable2Name\n", argv[0]);
249 fprintf(stderr,
"%s: SDDS error: unable to define parameter 3: xInterval\n", argv[0]);
255 fprintf(stderr,
"%s: SDDS error: unable to define parameter 4: xMinimum\n", argv[0]);
261 fprintf(stderr,
"%s: SDDS error: unable to define parameter 5: xDimension\n", argv[0]);
267 fprintf(stderr,
"%s: SDDS error: unable to define parameter 6: yInterval\n", argv[0]);
273 fprintf(stderr,
"%s: SDDS error: unable to define parameter 7: yMinimum\n", argv[0]);
279 fprintf(stderr,
"%s: SDDS error: unable to define parameter 8: yDimension\n", argv[0]);
289 fprintf(stderr,
"%s: SDDS error: unable to define array %s\n", argv[0],
"ImageArray");
294 if (!multiColumnMode) {
296 fprintf(stderr,
"%s: SDDS error: unable to define column %s\n", argv[0],
"Image");
302 fprintf(stderr,
"%s: SDDS error: problem defining column Index.\n", argv[0]);
313 indexes = malloc(
sizeof(
double) * dim[1]);
315 fprintf(stderr,
"%s: Error allocating memory for indexes.\n", argv[0]);
318 columnNames = malloc(
sizeof(
char *) * dim[0]);
320 fprintf(stderr,
"%s: Error allocating memory for column names.\n", argv[0]);
323 data = malloc(
sizeof(int32_t) * dim[1]);
325 fprintf(stderr,
"%s: Error allocating memory for data.\n", argv[0]);
328 for (i = 0; i < dim[0]; i++) {
329 columnNames[i] = malloc(
sizeof(
char) * 20);
330 if (!columnNames[i]) {
331 fprintf(stderr,
"%s: Error allocating memory for column name %d.\n", argv[0], i);
334 sprintf(columnNames[i],
"Line%" PRId32, i);
336 fprintf(stderr,
"%s: SDDS error: problem defining column %s.\n", argv[0], columnNames[i]);
345 fprintf(stderr,
"%s: SDDS error: unable to write layout\n", argv[0]);
350 if (!multiColumnMode) {
351 if (!SDDS_StartTable(&table, xDim * yDim)) {
352 fprintf(stderr,
"%s: SDDS error: unable to start table\n", argv[0]);
357 if (!SDDS_StartTable(&table, dim[1])) {
358 fprintf(stderr,
"%s: SDDS error: unable to start table\n", argv[0]);
365 if (!multiColumnMode) {
367 for (x = 0; x < xDim * yDim; x++) {
368 if (!
SDDS_SetRowValues(&table, (SDDS_SET_BY_NAME | SDDS_PASS_BY_VALUE), x,
"Image", rotimage[x], NULL)) {
369 fprintf(stderr,
"%s: SDDS error: unable to write row %d\n", argv[0], x);
375 for (x = 0; x < xDim * yDim; x++) {
376 if (!
SDDS_SetRowValues(&table, (SDDS_SET_BY_NAME | SDDS_PASS_BY_VALUE), x,
"Image", image[x], NULL)) {
377 fprintf(stderr,
"%s: SDDS error: unable to write row %d\n", argv[0], x);
384 for (i = 0; i < dim[1]; i++)
385 indexes[i] = xMin + xInterval * i;
392 for (i = 0; i < dim[0]; i++) {
393 for (j = 0; j < dim[1]; j++) {
397 data[j] = rotimage[n];
412 if (
SDDS_SetArray(&table,
"ImageArray", SDDS_CONTIGUOUS_DATA, rotimage, dim) == 0) {
413 fprintf(stderr,
"%s: SDDS Error - unable to set array\n", argv[0]);
420 if (
SDDS_SetArray(&table,
"ImageArray", SDDS_CONTIGUOUS_DATA, image, dim) == 0) {
421 fprintf(stderr,
"%s: SDDS Error - unable to set array\n", argv[0]);
428 if (!SDDS_WriteTable(&table)) {
429 fprintf(stderr,
"%s: SDDS error: unable to write table\n", argv[0]);
435 fprintf(stderr,
"%s: SDDS error: unable to terminate SDDS\n", argv[0]);
464int process_cmdline_args(
int argc,
char *argv[],
char *infile,
char *outfile) {
476 strcpy(infile, argv[1]);
477 strcpy(outfile, argv[2]);
480 for (i = 3; i < argc && !cmderror; i++) {
481 for (j = 0; !scan_done && !cmderror && ptable[j].parm; j++) {
482 if (strncmp(ptable[j].parm, argv[i], ptable[j].len) == 0) {
483 switch (ptable[j].
id) {
489 if (argv[i][0] ==
'-')
492 if (sscanf(argv[i],
"%d", &debug) < 1)
504 if (argv[i][0] ==
'-')
507 if (sscanf(argv[i],
"%d", &xDim) < 1)
519 if (argv[i][0] ==
'-')
522 if (sscanf(argv[i],
"%d", &yDim) < 1)
534 if (argv[i][0] ==
'-')
537 if (sscanf(argv[i],
"%lf", &xMin) < 1)
548 if (argv[i][0] ==
'-')
551 if (sscanf(argv[i],
"%lf", &xMax) < 1)
564 if (argv[i][0] ==
'-')
567 if (sscanf(argv[i],
"%lf", &yMin) < 1)
578 if (argv[i][0] ==
'-')
581 if (sscanf(argv[i],
"%lf", &yMax) < 1)
595 case PARM_MULTICOLUMNMODE:
648void usage(
char *name) {
649 pr(stderr,
"Image2SDDS Utility %s\n", VERSION);
650 pr(stderr,
"=============================\n");
651 pr(stderr,
"Usage:\n");
652 pr(stderr,
" image2sdds <IMAGE infile> <SDDS outfile>\n");
653 pr(stderr,
" [-2d]\n");
654 pr(stderr,
" [-ascii]\n");
655 pr(stderr,
" [-contour]\n");
656 pr(stderr,
" [-multicolumnmode]\n");
657 pr(stderr,
" [-transpose]\n");
658 pr(stderr,
" [-xdim <value>]\n");
659 pr(stderr,
" [-ydim <value>]\n");
660 pr(stderr,
" [-xmin <value>]\n");
661 pr(stderr,
" [-xmax <value>]\n");
662 pr(stderr,
" [-ymin <value>]\n");
663 pr(stderr,
" [-ymax <value>]\n");
664 pr(stderr,
" [-debug]\n");
665 pr(stderr,
" [-help]\n");
666 pr(stderr,
"Options:\n");
667 pr(stderr,
" -2d Output SDDS file as a 2D array.\n");
668 pr(stderr,
" -ascii Write SDDS file as ASCII (default is binary).\n");
669 pr(stderr,
" -contour Generate SDDS headers for the sddscontour tool.\n");
670 pr(stderr,
" -multicolumnmode Enable multi-column mode.\n");
671 pr(stderr,
" -transpose Transpose the image about the diagonal.\n");
672 pr(stderr,
" -xdim <value> Set X dimension of the image (default = %d).\n", XDIMENSION);
673 pr(stderr,
" -ydim <value> Set Y dimension of the image (default = %d).\n", YDIMENSION);
674 pr(stderr,
" -xmin <value> Set minimum X value.\n");
675 pr(stderr,
" -xmax <value> Set maximum X value.\n");
676 pr(stderr,
" -ymin <value> Set minimum Y value.\n");
677 pr(stderr,
" -ymax <value> Set maximum Y value.\n");
678 pr(stderr,
" -debug <level> Enable debug mode with the specified level.\n");
679 pr(stderr,
" -? or -help Display this usage message.\n\n");
680 pr(stderr,
"Purpose:\n");
681 pr(stderr,
" Reads image data from <infile> and writes SDDS data to <outfile>.\n");
682 pr(stderr,
" Supports various output formats and options for data manipulation.\n");
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_SetRowValues(SDDS_DATASET *SDDS_dataset, int32_t mode, int64_t row,...)
int32_t SDDS_SetColumnFromDoubles(SDDS_DATASET *SDDS_dataset, int32_t mode, double *data, int64_t rows,...)
Sets the values for a single data column using double-precision floating-point numbers.
int32_t SDDS_SetArray(SDDS_DATASET *SDDS_dataset, char *array_name, int32_t mode, void *data_pointer, int32_t *dimension)
Sets the values of an array variable in the SDDS dataset using specified dimensions.
int32_t SDDS_SetColumnFromLongs(SDDS_DATASET *SDDS_dataset, int32_t mode, int32_t *data, int64_t rows,...)
Sets the values for a single data column using long integer numbers.
int32_t SDDS_DefineParameter1(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, void *fixed_value)
Defines a data parameter with a fixed numerical value.
int32_t SDDS_InitializeOutput(SDDS_DATASET *SDDS_dataset, int32_t data_mode, int32_t lines_per_row, const char *description, const char *contents, const char *filename)
Initializes the SDDS output dataset.
int32_t SDDS_DefineSimpleColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *unit, int32_t type)
Defines a simple data column within the SDDS dataset.
int32_t SDDS_DefineArray(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, int32_t field_length, int32_t dimensions, const char *group_name)
Defines a data array within the SDDS dataset.
int32_t SDDS_DefineColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, int32_t field_length)
Defines a data column within the SDDS dataset.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
int32_t SDDS_DefineParameter(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, char *fixed_value)
Defines a data parameter with a fixed string value.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
#define SDDS_SHORT
Identifier for the signed short integer data type.
#define SDDS_CHARACTER
Identifier for the character data type.
#define SDDS_DOUBLE
Identifier for the double data type.