-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathimage.cpp
More file actions
126 lines (104 loc) · 3.93 KB
/
image.cpp
File metadata and controls
126 lines (104 loc) · 3.93 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
/*******************************************************
* 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/data.h>
#include <af/graphics.h>
#include <af/image.h>
#include <af/index.h>
#include <arith.hpp>
#include <backend.hpp>
#include <common/ArrayInfo.hpp>
#include <common/cast.hpp>
#include <common/err_common.hpp>
#include <common/graphics_common.hpp>
#include <handle.hpp>
#include <image.hpp>
#include <join.hpp>
#include <platform.hpp>
#include <reorder.hpp>
#include <tile.hpp>
#include <limits>
using af::dim4;
using arrayfire::common::cast;
using arrayfire::common::ForgeManager;
using arrayfire::common::ForgeModule;
using arrayfire::common::forgePlugin;
using arrayfire::common::getGLType;
using arrayfire::common::makeContextCurrent;
using detail::arithOp;
using detail::Array;
using detail::copy_image;
using detail::createValueArray;
using detail::forgeManager;
using detail::schar;
using detail::uchar;
using detail::uint;
using detail::ushort;
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();
// The inDims[2] * 100 is a hack to convert to fg_channel_format
// TODO(pradeep): Write a proper conversion function
fg_image ret_val = fgMngr.getImage(
inDims[1], inDims[0], static_cast<fg_channel_format>(inDims[2] * 100),
getGLType<T>());
copy_image<T>(normalizePerType<T>(imgData), ret_val);
return ret_val;
}
af_err af_draw_image(const af_window window, const af_array in,
const af_cell* const props) {
try {
if (window == 0) { AF_ERROR("Not a valid window", AF_ERR_INTERNAL); }
const 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);
makeContextCurrent(window);
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 s8: image = convert_and_copy_image<schar>(in); break;
case u8: image = convert_and_copy_image<uchar>(in); break;
default: TYPE_ERROR(1, type);
}
ForgeModule& _ = forgePlugin();
auto gridDims = forgeManager().getWindowGrid(window);
FG_CHECK(_.fg_set_window_colormap(window, (fg_color_map)props->cmap));
if (props->col > -1 && props->row > -1) {
FG_CHECK(_.fg_draw_image_to_cell(
window, gridDims.first, gridDims.second,
props->row * gridDims.second + props->col, image, props->title,
true));
} else {
FG_CHECK(_.fg_draw_image(window, image, true));
}
}
CATCHALL;
return AF_SUCCESS;
}