132 {
133 short *image = NULL;
134 short *rotimage = NULL;
135 char infile[255];
136 char outfile[255];
137 char xDimStr[10];
138 char yDimStr[10];
139 int32_t dim[2];
140 FILE *infilefp;
141 int x, y, i, n, j;
143 double xInterval = 0.02;
144 double yInterval = 0.02;
145 double *indexes = NULL;
146 char **columnNames = NULL;
147 int32_t *data = NULL;
148
149 process_cmdline_args(argc, argv, infile, outfile);
150
151
152 if (!xDim) {
153 xDim = XDIMENSION;
154 }
155 if (!yDim) {
156 yDim = YDIMENSION;
157 }
158
159 image = (short *)malloc(sizeof(short) * xDim * yDim);
160 if (!image) {
161 fprintf(stderr, "%s: Error allocating memory for image.\n", argv[0]);
162 exit(EXIT_FAILURE);
163 }
164
165 rotimage = (short *)malloc(sizeof(short) * xDim * yDim);
166 if (!rotimage) {
167 fprintf(stderr, "%s: Error allocating memory for rotated image.\n", argv[0]);
168 exit(EXIT_FAILURE);
169 }
170
171
172 if ((infilefp = fopen(infile, "rb")) == NULL) {
173 fprintf(stderr, "%s: Error: unable to open input file: %s\n", argv[0], infile);
174 exit(EXIT_FAILURE);
175 }
176
177
178 for (x = 0; x < xDim * yDim; x++) {
179 int byte = getc(infilefp);
180 if (byte == EOF) {
181 fprintf(stderr, "%s: Unexpected EOF at index %d\n", argv[0], x);
182 exit(EXIT_FAILURE);
183 }
184 image[x] = (short)byte;
185 }
186 fclose(infilefp);
187
188
189 i = 0;
190 for (x = 0; x < xDim; x++) {
191 for (y = 0; y < yDim; y++) {
192 rotimage[y * xDim + x] = image[i];
193 i++;
194 }
195 }
196
197
198
199 if (transpose) {
200 sprintf(yDimStr, "%d", xDim);
201 sprintf(xDimStr, "%d", yDim);
202 } else {
203 sprintf(xDimStr, "%d", xDim);
204 sprintf(yDimStr, "%d", yDim);
205 }
206
207 if (asciiOutput) {
209 fprintf(stderr, "%s: SDDS error: unable to initialize output file %s\n", argv[0], outfile);
211 exit(EXIT_FAILURE);
212 }
213 } else {
215 fprintf(stderr, "%s: SDDS error: unable to initialize output file %s\n", argv[0], outfile);
217 exit(EXIT_FAILURE);
218 }
219 }
220
221 if (transpose) {
222 if (useXmax)
223 xInterval = (xMax - xMin) / (yDim - 1);
224 if (useYmax)
225 yInterval = (yMax - yMin) / (xDim - 1);
226 } else {
227 if (useXmax)
228 xInterval = (xMax - xMin) / (xDim - 1);
229 if (useYmax)
230 yInterval = (yMax - yMin) / (yDim - 1);
231 }
232
233 if (contour) {
234 if (!multiColumnMode) {
235
237 fprintf(stderr, "%s: SDDS error: unable to define parameter 1: Variable1Name\n", argv[0]);
239 exit(EXIT_FAILURE);
240 }
241
243 fprintf(stderr, "%s: SDDS error: unable to define parameter 2: Variable2Name\n", argv[0]);
245 exit(EXIT_FAILURE);
246 }
247
249 fprintf(stderr, "%s: SDDS error: unable to define parameter 3: xInterval\n", argv[0]);
251 exit(EXIT_FAILURE);
252 }
253
255 fprintf(stderr, "%s: SDDS error: unable to define parameter 4: xMinimum\n", argv[0]);
257 exit(EXIT_FAILURE);
258 }
259
261 fprintf(stderr, "%s: SDDS error: unable to define parameter 5: xDimension\n", argv[0]);
263 exit(EXIT_FAILURE);
264 }
265
267 fprintf(stderr, "%s: SDDS error: unable to define parameter 6: yInterval\n", argv[0]);
269 exit(EXIT_FAILURE);
270 }
271
273 fprintf(stderr, "%s: SDDS error: unable to define parameter 7: yMinimum\n", argv[0]);
275 exit(EXIT_FAILURE);
276 }
277
279 fprintf(stderr, "%s: SDDS error: unable to define parameter 8: yDimension\n", argv[0]);
281 exit(EXIT_FAILURE);
282 }
283 }
284 }
285
286
287 if (array) {
289 fprintf(stderr, "%s: SDDS error: unable to define array %s\n", argv[0], "ImageArray");
291 exit(EXIT_FAILURE);
292 }
293 } else {
294 if (!multiColumnMode) {
296 fprintf(stderr, "%s: SDDS error: unable to define column %s\n", argv[0], "Image");
298 exit(EXIT_FAILURE);
299 }
300 } else {
302 fprintf(stderr, "%s: SDDS error: problem defining column Index.\n", argv[0]);
304 exit(EXIT_FAILURE);
305 }
306 if (transpose) {
307 dim[0] = xDim;
308 dim[1] = yDim;
309 } else {
310 dim[0] = yDim;
311 dim[1] = xDim;
312 }
313 indexes = malloc(sizeof(double) * dim[1]);
314 if (!indexes) {
315 fprintf(stderr, "%s: Error allocating memory for indexes.\n", argv[0]);
316 exit(EXIT_FAILURE);
317 }
318 columnNames = malloc(sizeof(char *) * dim[0]);
319 if (!columnNames) {
320 fprintf(stderr, "%s: Error allocating memory for column names.\n", argv[0]);
321 exit(EXIT_FAILURE);
322 }
323 data = malloc(sizeof(int32_t) * dim[1]);
324 if (!data) {
325 fprintf(stderr, "%s: Error allocating memory for data.\n", argv[0]);
326 exit(EXIT_FAILURE);
327 }
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);
332 exit(EXIT_FAILURE);
333 }
334 sprintf(columnNames[i], "Line%" PRId32, i);
336 fprintf(stderr, "%s: SDDS error: problem defining column %s.\n", argv[0], columnNames[i]);
338 exit(EXIT_FAILURE);
339 }
340 }
341 }
342 }
343
345 fprintf(stderr, "%s: SDDS error: unable to write layout\n", argv[0]);
347 exit(EXIT_FAILURE);
348 }
349
350 if (!multiColumnMode) {
351 if (!SDDS_StartTable(&table, xDim * yDim)) {
352 fprintf(stderr, "%s: SDDS error: unable to start table\n", argv[0]);
354 exit(EXIT_FAILURE);
355 }
356 } else {
357 if (!SDDS_StartTable(&table, dim[1])) {
358 fprintf(stderr, "%s: SDDS error: unable to start table\n", argv[0]);
360 exit(EXIT_FAILURE);
361 }
362 }
363
364 if (!array) {
365 if (!multiColumnMode) {
366 if (transpose) {
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);
371 exit(EXIT_FAILURE);
372 }
373 }
374 } else {
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);
379 exit(EXIT_FAILURE);
380 }
381 }
382 }
383 } else {
384 for (i = 0; i < dim[1]; i++)
385 indexes[i] = xMin + xInterval * i;
388 exit(EXIT_FAILURE);
389 }
390 n = 0;
391
392 for (i = 0; i < dim[0]; i++) {
393 for (j = 0; j < dim[1]; j++) {
394 if (transpose) {
395 data[j] = image[n];
396 } else {
397 data[j] = rotimage[n];
398 }
399 n++;
400 }
401
404 exit(EXIT_FAILURE);
405 }
406 }
407 }
408 } else {
409 if (transpose) {
410 dim[0] = xDim;
411 dim[1] = yDim;
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]);
415 exit(EXIT_FAILURE);
416 }
417 } else {
418 dim[0] = yDim;
419 dim[1] = xDim;
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]);
423 exit(EXIT_FAILURE);
424 }
425 }
426 }
427
428 if (!SDDS_WriteTable(&table)) {
429 fprintf(stderr, "%s: SDDS error: unable to write table\n", argv[0]);
431 exit(EXIT_FAILURE);
432 }
433
435 fprintf(stderr, "%s: SDDS error: unable to terminate SDDS\n", argv[0]);
437 exit(EXIT_FAILURE);
438 }
439
440
441 if (image)
442 free(image);
443 if (rotimage)
444 free(rotimage);
445 if (indexes)
446 free(indexes);
447 if (data)
448 free(data);
449 if (columnNames) {
450 free(columnNames);
451 }
452
453 return EXIT_SUCCESS;
454}
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.