forked from hunter-packages/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.cpp
More file actions
284 lines (240 loc) · 7.17 KB
/
image.cpp
File metadata and controls
284 lines (240 loc) · 7.17 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <af/graphics.h>
#include <af/image.h>
#include <af/index.h>
#include <af/data.h>
#include <ArrayInfo.hpp>
#include <graphics_common.hpp>
#include <err_common.hpp>
#include <backend.hpp>
#include <image.hpp>
#include <handle.hpp>
#include <reorder.hpp>
#include <tile.hpp>
#include <join.hpp>
#include <cast.hpp>
#include <arith.hpp>
#include <iostream>
#include <limits>
using af::dim4;
using namespace detail;
#if defined(WITH_GRAPHICS)
using namespace graphics;
template<typename T>
Array<T> normalizePerType(const Array<T>& in)
{
Array<float> inFloat = cast<float, T>(in);
Array<float> cnst = createValueArray<float>(in.dims(), 1.0 - 1.0e-6f);
Array<float> scaled = arithOp<float, af_mul_t>(inFloat, cnst, in.dims());
return cast<T, float>(scaled);
}
template<>
Array<float> normalizePerType<float>(const Array<float>& in)
{
return in;
}
template<typename T>
static fg::Image* convert_and_copy_image(const af_array in)
{
const Array<T> _in = getArray<T>(in);
dim4 inDims = _in.dims();
dim4 rdims = (inDims[2]>1 ? dim4(2, 1, 0, 3) : dim4(1, 0, 2, 3));
Array<T> imgData = reorder(_in, rdims);
ForgeManager& fgMngr = ForgeManager::getInstance();
// The inDims[2] * 100 is a hack to convert to fg::ChannelFormat
// TODO Write a proper conversion function
fg::Image* ret_val = fgMngr.getImage(inDims[1], inDims[0], (fg::ChannelFormat)(inDims[2] * 100), getGLType<T>());
copy_image<T>(normalizePerType<T>(imgData), ret_val);
return ret_val;
}
#endif
af_err af_draw_image(const af_window wind, const af_array in, const af_cell* const props)
{
#if defined(WITH_GRAPHICS)
if(wind==0) {
std::cerr<<"Not a valid window"<<std::endl;
return AF_SUCCESS;
}
try {
ArrayInfo info = getInfo(in);
af::dim4 in_dims = info.dims();
af_dtype type = info.getType();
DIM_ASSERT(0, in_dims[2] == 1 || in_dims[2] == 3 || in_dims[2] == 4);
DIM_ASSERT(0, in_dims[3] == 1);
fg::Window* window = reinterpret_cast<fg::Window*>(wind);
window->makeCurrent();
fg::Image* image = NULL;
switch(type) {
case f32: image = convert_and_copy_image<float >(in); break;
case b8 : image = convert_and_copy_image<char >(in); break;
case s32: image = convert_and_copy_image<int >(in); break;
case u32: image = convert_and_copy_image<uint >(in); break;
case s16: image = convert_and_copy_image<short >(in); break;
case u16: image = convert_and_copy_image<ushort>(in); break;
case u8 : image = convert_and_copy_image<uchar >(in); break;
default: TYPE_ERROR(1, type);
}
window->setColorMap((fg::ColorMap)props->cmap);
if (props->col>-1 && props->row>-1)
window->draw(props->col, props->row, *image, props->title);
else
window->draw(*image);
}
CATCHALL;
return AF_SUCCESS;
#else
AF_RETURN_ERROR("ArrayFire compiled without graphics support", AF_ERR_NO_GFX);
#endif
}
af_err af_create_window(af_window *out, const int width, const int height, const char* const title)
{
#if defined(WITH_GRAPHICS)
fg::Window* wnd;
try {
graphics::ForgeManager& fgMngr = graphics::ForgeManager::getInstance();
fg::Window* mainWnd = NULL;
try {
mainWnd = fgMngr.getMainWindow();
} catch(...) {
std::cerr<<"OpenGL context creation failed"<<std::endl;
}
if(mainWnd==0) {
std::cerr<<"Not a valid window"<<std::endl;
return AF_SUCCESS;
}
wnd = new fg::Window(width, height, title, mainWnd);
wnd->setFont(fgMngr.getFont());
}
CATCHALL;
*out = reinterpret_cast<af_window>(wnd);
return AF_SUCCESS;
#else
AF_RETURN_ERROR("ArrayFire compiled without graphics support", AF_ERR_NO_GFX);
#endif
}
af_err af_set_position(const af_window wind, const unsigned x, const unsigned y)
{
#if defined(WITH_GRAPHICS)
if(wind==0) {
std::cerr<<"Not a valid window"<<std::endl;
return AF_SUCCESS;
}
try {
fg::Window* wnd = reinterpret_cast<fg::Window*>(wind);
wnd->setPos(x, y);
}
CATCHALL;
return AF_SUCCESS;
#else
AF_RETURN_ERROR("ArrayFire compiled without graphics support", AF_ERR_NO_GFX);
#endif
}
af_err af_set_title(const af_window wind, const char* const title)
{
#if defined(WITH_GRAPHICS)
if(wind==0) {
std::cerr<<"Not a valid window"<<std::endl;
return AF_SUCCESS;
}
try {
fg::Window* wnd = reinterpret_cast<fg::Window*>(wind);
wnd->setTitle(title);
}
CATCHALL;
return AF_SUCCESS;
#else
AF_RETURN_ERROR("ArrayFire compiled without graphics support", AF_ERR_NO_GFX);
#endif
}
af_err af_set_size(const af_window wind, const unsigned w, const unsigned h)
{
#if defined(WITH_GRAPHICS)
if(wind==0) {
std::cerr<<"Not a valid window"<<std::endl;
return AF_SUCCESS;
}
try {
fg::Window* wnd = reinterpret_cast<fg::Window*>(wind);
wnd->setSize(w, h);
}
CATCHALL;
return AF_SUCCESS;
#else
AF_RETURN_ERROR("ArrayFire compiled without graphics support", AF_ERR_NO_GFX);
#endif
}
af_err af_grid(const af_window wind, const int rows, const int cols)
{
#if defined(WITH_GRAPHICS)
if(wind==0) {
std::cerr<<"Not a valid window"<<std::endl;
return AF_SUCCESS;
}
try {
fg::Window* wnd = reinterpret_cast<fg::Window*>(wind);
wnd->grid(rows, cols);
}
CATCHALL;
return AF_SUCCESS;
#else
AF_RETURN_ERROR("ArrayFire compiled without graphics support", AF_ERR_NO_GFX);
#endif
}
af_err af_show(const af_window wind)
{
#if defined(WITH_GRAPHICS)
if(wind==0) {
std::cerr<<"Not a valid window"<<std::endl;
return AF_SUCCESS;
}
try {
fg::Window* wnd = reinterpret_cast<fg::Window*>(wind);
wnd->swapBuffers();
}
CATCHALL;
return AF_SUCCESS;
#else
AF_RETURN_ERROR("ArrayFire compiled without graphics support", AF_ERR_NO_GFX);
#endif
}
af_err af_is_window_closed(bool *out, const af_window wind)
{
#if defined(WITH_GRAPHICS)
if(wind==0) {
std::cerr<<"Not a valid window"<<std::endl;
return AF_SUCCESS;
}
try {
fg::Window* wnd = reinterpret_cast<fg::Window*>(wind);
*out = wnd->close();
}
CATCHALL;
return AF_SUCCESS;
#else
AF_RETURN_ERROR("ArrayFire compiled without graphics support", AF_ERR_NO_GFX);
#endif
}
af_err af_destroy_window(const af_window wind)
{
#if defined(WITH_GRAPHICS)
if(wind==0) {
std::cerr<<"Not a valid window"<<std::endl;
return AF_SUCCESS;
}
try {
fg::Window* wnd = reinterpret_cast<fg::Window*>(wind);
delete wnd;
}
CATCHALL;
return AF_SUCCESS;
#else
AF_RETURN_ERROR("ArrayFire compiled without graphics support", AF_ERR_NO_GFX);
#endif
}