-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftcl_import.cpp
More file actions
185 lines (155 loc) · 5.23 KB
/
Copy pathftcl_import.cpp
File metadata and controls
185 lines (155 loc) · 5.23 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (c) 2010 Stephen Williams (steve@icarus.com)
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU
* General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
# include "FitsbenchMain.h"
# include "FitsbenchItem.h"
# include <tiffio.h>
# include "qassert.h"
using namespace std;
static int import_tiff(WorkFolder*work, const QString&name, const char*path);
static int import_pnm(WorkFolder*work, const QString&name, const char*path);
int FitsbenchMain::ftcl_import_thunk_(ClientData raw, Tcl_Interp*interp,
int objc, Tcl_Obj*CONST objv[])
{
FitsbenchMain*eng = reinterpret_cast<FitsbenchMain*> (raw);
qassert(eng->tcl_engine_ == interp);
return eng->ftcl_import_(objc, objv);
}
/*
* import <work/name> <path>
*/
int FitsbenchMain::ftcl_import_(int objc, Tcl_Obj*const objv[])
{
if (objc < 3) {
Tcl_AppendResult(tcl_engine_, "usage: import <work/name> <path>", 0);
return TCL_ERROR;
}
QString name;
WorkFolder*work = workfolder_from_name_(objv[1], name);
if (work == 0) {
Tcl_AppendResult(tcl_engine_, "Unable to find work folder", 0);
return TCL_ERROR;
}
const char*path = Tcl_GetString(objv[2]);
const char*suff = strrchr(path, '.');
if (suff == 0) {
Tcl_AppendResult(tcl_engine_, "I don't know what ", path, " is.", 0);
return TCL_ERROR;
}
if (strcasecmp(suff, ".tif") == 0) {
return import_tiff(work, name, path);
} else if (strcasecmp(suff, ".tiff") == 0) {
return import_tiff(work, name, path);
} else if (strcasecmp(suff, ".pgm") == 0) {
return import_pnm(work, name, path);
} else if (strcasecmp(suff, ".ppm") == 0) {
return import_pnm(work, name, path);
} else {
Tcl_AppendResult(tcl_engine_, "I don't know what ", path, " is.", 0);
return TCL_ERROR;
}
}
template <class data_t> static void import_tiff_gray(WorkFolder::Image*img, TIFF*fd, data_t*buf)
{
vector<long> axes = img->get_axes();
qassert(axes.size() == 2);
vector<long> dst (2);
dst[0] = 0;
for (dst[1] = 0 ; dst[1] < axes[1] ; dst[1] += 1) {
TIFFReadScanline(fd, buf, dst[1], 0);
img->set_line(dst, axes[0], buf);
}
}
template <class data_t> static void import_tiff_color(WorkFolder::Image*img, TIFF*fd, data_t*buf)
{
vector<long> axes = img->get_axes();
qassert(axes.size() == 3);
vector<long> dst (3);
dst[0] = 0;
vector<data_t*> planes ( axes[2] );
for (int clr = 0 ; clr < axes[2] ; clr += 1)
planes[clr] = new data_t [axes[0]];
for (dst[1] = 0 ; dst[1] < axes[1] ; dst[1] += 1) {
TIFFReadScanline(fd, buf, dst[1], 0);
for (int xdx = 0 ; xdx < axes[0] ; xdx += 1) {
for (int clr = 0 ; clr < axes[2] ; clr += 1)
planes[clr][xdx] = buf[xdx*axes[2] + clr];
}
for (int clr = 0 ; clr < axes[2] ; clr += 1) {
dst[2] = clr;
img->set_line(dst, axes[0], planes[clr]);
}
}
for (int clr = 0 ; clr < axes[2] ; clr += 1)
delete[] planes[clr];
}
static int import_tiff(WorkFolder*work, const QString&name, const char*path)
{
TIFF*fd = TIFFOpen(path, "r");
uint16 planes;
TIFFGetFieldDefaulted(fd, TIFFTAG_SAMPLESPERPIXEL, &planes);
uint32 wid, hei;
TIFFGetFieldDefaulted(fd, TIFFTAG_IMAGEWIDTH, &wid);
TIFFGetFieldDefaulted(fd, TIFFTAG_IMAGELENGTH, &hei);
vector<long> axes ( planes > 1? 3 : 2 );
axes[0] = wid;
axes[1] = hei;
if (planes > 1)
axes[2] = planes;
uint16 bits_per_sample;
TIFFGetFieldDefaulted(fd, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
DataArray::type_t type;
if (bits_per_sample <= 8)
type = DataArray::DT_UINT8;
else if (bits_per_sample <= 16)
type = DataArray::DT_UINT16;
else if (bits_per_sample <= 32)
type = DataArray::DT_UINT32;
else
type = DataArray::DT_UINT64;
WorkFolder::Image*img = new WorkFolder::Image(work, name);
img->reconfig(axes, type);
switch (type) {
case DataArray::DT_UINT8: {
uint8_t*buf = new uint8_t [axes[0] * planes];
if (planes==1)
import_tiff_gray(img, fd, buf);
else
import_tiff_color(img, fd, buf);
delete[]buf;
break;
}
case DataArray::DT_UINT16: {
uint16_t*buf = new uint16_t [axes[0] * planes];
if (planes==1)
import_tiff_gray(img, fd, buf);
else
import_tiff_color(img, fd, buf);
delete[]buf;
break;
}
default:
break;
}
TIFFClose(fd);
return TCL_OK;
}
static int import_pnm(WorkFolder*work, const QString&name, const char*path)
{
return TCL_OK;
}