/
ff_write.c
153 lines (133 loc) · 4 KB
/
ff_write.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "dvio.h"
#include "io_loadmod.h"
#include "parser.h"
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
/* FIX: put these in a header */
#ifdef HAVE_LIBPNG
static char const* iom_filetypes[] = {"gif", "jpg", "jpeg", "tif", "tiff", "png", "bmp", 0};
#else
static char const* iom_filetypes[] = {"gif", "jpg", "jpeg", "tif", "tiff", "bmp", 0};
#endif
Var* ff_write(vfuncptr func, Var* arg)
{
Var* ob = NULL;
char* filename = NULL;
char* title = NULL;
char* type = NULL;
char* separator = NULL; /* for csv */
int header = 0; /* for csv */
int force = 0; /* Force file overwrite */
int hdf_old = 0; // write hdf file backward like davinci used to
unsigned short iom_type_idx, iom_type_found;
Alist alist[9];
alist[0] = make_alist("object", ID_UNK, NULL, &ob);
alist[1] = make_alist("filename", ID_STRING, NULL, &filename);
alist[2] = make_alist("type", ID_ENUM, NULL, &type);
alist[3] = make_alist("title", ID_STRING, NULL, &title);
alist[4] = make_alist("force", DV_INT32, NULL, &force);
alist[5] = make_alist("separator", ID_STRING, NULL, &separator);
alist[6] = make_alist("header", DV_INT32, NULL, &header);
alist[7] = make_alist("hdf_old", DV_INT32, NULL, &hdf_old);
alist[8].name = NULL;
if (parse_args(func, arg, alist) == 0) return (NULL);
/**
** Make sure user specified an object
**/
if (ob == NULL) {
parse_error("%s: No object specified.", func->name);
return (NULL);
}
/**
** get filename. Verify type
**/
if (filename == NULL) {
parse_error("No filename specified.");
return (NULL);
}
filename = dv_locate_file(filename);
if (type == NULL) {
parse_error("No type specified.");
return (NULL);
}
/*
** Get title string
*/
if (title == NULL) {
title = (char*)"DV data product";
}
/* Check type against list of types supported by iomedley. */
iom_type_idx = iom_type_found = 0;
while (iom_filetypes[iom_type_idx]) {
if (!strcasecmp(type, iom_filetypes[iom_type_idx])) {
iom_type_found = 1;
break;
}
iom_type_idx++;
}
if (iom_type_found)
dv_WriteIOM(ob, filename, type, force);
else if (!strcasecmp(type, "raw"))
dv_WriteRaw(ob, filename, force);
else if (!strcasecmp(type, "vicar"))
dv_WriteVicar(ob, filename, force);
else if (!strcasecmp(type, "grd"))
dv_WriteGRD(ob, filename, force, title, (char*)"davinci");
/* else if (!strcasecmp(type, "pnm")) dv_WritePNM(ob, filename, force); */
else if (!strcasecmp(type, "pgm"))
dv_WritePGM(ob, filename, force);
else if (!strcasecmp(type, "ppm"))
dv_WritePPM(ob, filename, force);
else if (!strcasecmp(type, "ascii"))
WriteAscii(ob, filename, force);
else if (!strcasecmp(type, "csv"))
dv_WriteCSV(ob, filename, separator, header, force);
else if (!strcasecmp(type, "ers"))
dv_WriteERS(ob, filename, force);
else if (!strcasecmp(type, "imath"))
dv_WriteIMath(ob, filename, force);
else if (!strcasecmp(type, "isis"))
dv_WriteISIS(ob, filename, force, title);
else if (!strcasecmp(type, "envi"))
dv_WriteENVI(ob, filename, force);
else if (!strcasecmp(type, "specpr")) {
if (!force && file_exists(filename)) {
parse_error("File %s already exists.\n", filename);
return NULL;
}
WriteSpecpr(ob, filename, title);
}
/*
** Below here are optional packages
*/
#ifdef HAVE_LIBHDF5
else if (!strcasecmp(type, "hdf")) {
struct stat statbuf;
if (!force && !stat(filename, &statbuf)) {
parse_error("File %s already exists.\n", filename);
return NULL;
}
/* force ? */
WriteHDF5(-1, filename, ob, hdf_old);
}
#endif
#ifdef BUILD_MODULE_SUPPORT
else if (iomod_handler_for_type(type))
write_to_io_module(ob, filename, type, force);
#endif
#if 0
#ifdef HAVE_LIBMAGICK
// else if (dvio_ValidGfx(type, GFX_type)) dv_WriteGFX_Image(ob, filename, force, GFX_type);
else if (1) paramdvWriteImage(ob, filename, type, force);
#endif
#endif
else {
sprintf(error_buf, "Unrecognized type: %s", type);
parse_error(NULL);
return (NULL);
}
free(filename);
return (NULL);
}