Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/core/src/convert.cpp
16337 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html
4
5
#include "precomp.hpp"
6
#include "opencl_kernels_core.hpp"
7
#include "convert.hpp"
8
9
namespace cv {
10
11
namespace hal {
12
13
void cvt16f32f( const float16_t* src, float* dst, int len )
14
{
15
int j = 0;
16
#if CV_SIMD
17
const int VECSZ = v_float32::nlanes;
18
for( ; j < len; j += VECSZ )
19
{
20
if( j > len - VECSZ )
21
{
22
if( j == 0 )
23
break;
24
j = len - VECSZ;
25
}
26
v_store(dst + j, vx_load_expand(src + j));
27
}
28
#endif
29
for( ; j < len; j++ )
30
dst[j] = (float)src[j];
31
}
32
33
void cvt32f16f( const float* src, float16_t* dst, int len )
34
{
35
int j = 0;
36
#if CV_SIMD
37
const int VECSZ = v_float32::nlanes;
38
for( ; j < len; j += VECSZ )
39
{
40
if( j > len - VECSZ )
41
{
42
if( j == 0 )
43
break;
44
j = len - VECSZ;
45
}
46
v_pack_store(dst + j, vx_load(src + j));
47
}
48
#endif
49
for( ; j < len; j++ )
50
dst[j] = float16_t(src[j]);
51
}
52
53
void addRNGBias32f( float* arr, const float* scaleBiasPairs, int len )
54
{
55
// the loop is simple enough, so we let the compiler to vectorize it
56
for( int i = 0; i < len; i++ )
57
arr[i] += scaleBiasPairs[i*2 + 1];
58
}
59
60
void addRNGBias64f( double* arr, const double* scaleBiasPairs, int len )
61
{
62
// the loop is simple enough, so we let the compiler to vectorize it
63
for( int i = 0; i < len; i++ )
64
arr[i] += scaleBiasPairs[i*2 + 1];
65
}
66
67
}
68
69
template<typename _Ts, typename _Td, typename _Twvec> inline void
70
cvt_( const _Ts* src, size_t sstep, _Td* dst, size_t dstep, Size size )
71
{
72
sstep /= sizeof(src[0]);
73
dstep /= sizeof(dst[0]);
74
75
for( int i = 0; i < size.height; i++, src += sstep, dst += dstep )
76
{
77
int j = 0;
78
#if CV_SIMD
79
const int VECSZ = _Twvec::nlanes*2;
80
for( ; j < size.width; j += VECSZ )
81
{
82
if( j > size.width - VECSZ )
83
{
84
if( j == 0 || src == (_Ts*)dst )
85
break;
86
j = size.width - VECSZ;
87
}
88
_Twvec v0, v1;
89
vx_load_pair_as(src + j, v0, v1);
90
v_store_pair_as(dst + j, v0, v1);
91
}
92
#endif
93
for( ; j < size.width; j++ )
94
dst[j] = saturate_cast<_Td>(src[j]);
95
}
96
}
97
98
// in order to reduce the code size, for (16f <-> ...) conversions
99
// we add a conversion function without loop unrolling
100
template<typename _Ts, typename _Td, typename _Twvec> inline void
101
cvt1_( const _Ts* src, size_t sstep, _Td* dst, size_t dstep, Size size )
102
{
103
sstep /= sizeof(src[0]);
104
dstep /= sizeof(dst[0]);
105
106
for( int i = 0; i < size.height; i++, src += sstep, dst += dstep )
107
{
108
int j = 0;
109
#if CV_SIMD
110
const int VECSZ = _Twvec::nlanes;
111
for( ; j < size.width; j += VECSZ )
112
{
113
if( j > size.width - VECSZ )
114
{
115
if( j == 0 || src == (_Ts*)dst )
116
break;
117
j = size.width - VECSZ;
118
}
119
_Twvec v;
120
vx_load_as(src + j, v);
121
v_store_as(dst + j, v);
122
}
123
vx_cleanup();
124
#endif
125
for( ; j < size.width; j++ )
126
dst[j] = saturate_cast<_Td>(src[j]);
127
}
128
}
129
130
static void cvtCopy( const uchar* src, size_t sstep,
131
uchar* dst, size_t dstep, Size size, size_t elemsize)
132
{
133
size_t len = size.width*elemsize;
134
for( int i = 0; i < size.height; i++, src += sstep, dst += dstep )
135
{
136
memcpy( dst, src, len );
137
}
138
}
139
140
#define DEF_CVT_FUNC(suffix, cvtfunc, _Ts, _Td, _Twvec) \
141
static void cvt##suffix(const _Ts* src, size_t sstep, uchar*, size_t, \
142
_Td* dst, size_t dstep, Size size, void*) \
143
{ cvtfunc<_Ts, _Td, _Twvec>(src, sstep, dst, dstep, size); }
144
145
////////////////////// 8u -> ... ////////////////////////
146
147
DEF_CVT_FUNC(8u8s, cvt_, uchar, schar, v_int16)
148
DEF_CVT_FUNC(8u16u, cvt_, uchar, ushort, v_uint16)
149
DEF_CVT_FUNC(8u16s, cvt_, uchar, short, v_int16)
150
DEF_CVT_FUNC(8u32s, cvt_, uchar, int, v_int32)
151
DEF_CVT_FUNC(8u32f, cvt_, uchar, float, v_float32)
152
DEF_CVT_FUNC(8u64f, cvt_, uchar, double, v_int32)
153
DEF_CVT_FUNC(8u16f, cvt1_, uchar, float16_t, v_float32)
154
155
////////////////////// 8s -> ... ////////////////////////
156
157
DEF_CVT_FUNC(8s8u, cvt_, schar, uchar, v_int16)
158
DEF_CVT_FUNC(8s16u, cvt_, schar, ushort, v_uint16)
159
DEF_CVT_FUNC(8s16s, cvt_, schar, short, v_int16)
160
DEF_CVT_FUNC(8s32s, cvt_, schar, int, v_int32)
161
DEF_CVT_FUNC(8s32f, cvt_, schar, float, v_float32)
162
DEF_CVT_FUNC(8s64f, cvt_, schar, double, v_int32)
163
DEF_CVT_FUNC(8s16f, cvt1_, schar, float16_t, v_float32)
164
165
////////////////////// 16u -> ... ////////////////////////
166
167
DEF_CVT_FUNC(16u8u, cvt_, ushort, uchar, v_uint16)
168
DEF_CVT_FUNC(16u8s, cvt_, ushort, schar, v_uint16)
169
DEF_CVT_FUNC(16u16s, cvt_, ushort, short, v_int32)
170
DEF_CVT_FUNC(16u32s, cvt_, ushort, int, v_int32)
171
DEF_CVT_FUNC(16u32f, cvt_, ushort, float, v_float32)
172
DEF_CVT_FUNC(16u64f, cvt_, ushort, double, v_int32)
173
DEF_CVT_FUNC(16u16f, cvt1_,ushort, float16_t, v_float32)
174
175
////////////////////// 16s -> ... ////////////////////////
176
177
DEF_CVT_FUNC(16s8u, cvt_, short, uchar, v_int16)
178
DEF_CVT_FUNC(16s8s, cvt_, short, schar, v_int16)
179
DEF_CVT_FUNC(16s16u, cvt_, short, ushort, v_int32)
180
DEF_CVT_FUNC(16s32s, cvt_, short, int, v_int32)
181
DEF_CVT_FUNC(16s32f, cvt_, short, float, v_float32)
182
DEF_CVT_FUNC(16s64f, cvt_, short, double, v_int32)
183
DEF_CVT_FUNC(16s16f, cvt1_,short, float16_t, v_float32)
184
185
////////////////////// 32s -> ... ////////////////////////
186
187
DEF_CVT_FUNC(32s8u, cvt_, int, uchar, v_int32)
188
DEF_CVT_FUNC(32s8s, cvt_, int, schar, v_int32)
189
DEF_CVT_FUNC(32s16u, cvt_, int, ushort, v_int32)
190
DEF_CVT_FUNC(32s16s, cvt_, int, short, v_int32)
191
DEF_CVT_FUNC(32s32f, cvt_, int, float, v_float32)
192
DEF_CVT_FUNC(32s64f, cvt_, int, double, v_int32)
193
DEF_CVT_FUNC(32s16f, cvt1_,int, float16_t, v_float32)
194
195
////////////////////// 32f -> ... ////////////////////////
196
197
DEF_CVT_FUNC(32f8u, cvt_, float, uchar, v_float32)
198
DEF_CVT_FUNC(32f8s, cvt_, float, schar, v_float32)
199
DEF_CVT_FUNC(32f16u, cvt_, float, ushort, v_float32)
200
DEF_CVT_FUNC(32f16s, cvt_, float, short, v_float32)
201
DEF_CVT_FUNC(32f32s, cvt_, float, int, v_float32)
202
DEF_CVT_FUNC(32f64f, cvt_, float, double, v_float32)
203
DEF_CVT_FUNC(32f16f, cvt1_,float, float16_t, v_float32)
204
205
////////////////////// 64f -> ... ////////////////////////
206
207
DEF_CVT_FUNC(64f8u, cvt_, double, uchar, v_int32)
208
DEF_CVT_FUNC(64f8s, cvt_, double, schar, v_int32)
209
DEF_CVT_FUNC(64f16u, cvt_, double, ushort, v_int32)
210
DEF_CVT_FUNC(64f16s, cvt_, double, short, v_int32)
211
DEF_CVT_FUNC(64f32s, cvt_, double, int, v_int32)
212
DEF_CVT_FUNC(64f32f, cvt_, double, float, v_float32)
213
DEF_CVT_FUNC(64f16f, cvt1_,double, float16_t, v_float32)
214
215
////////////////////// 16f -> ... ////////////////////////
216
217
DEF_CVT_FUNC(16f8u, cvt_, float16_t, uchar, v_float32)
218
DEF_CVT_FUNC(16f8s, cvt_, float16_t, schar, v_float32)
219
DEF_CVT_FUNC(16f16u, cvt1_, float16_t, ushort, v_float32)
220
DEF_CVT_FUNC(16f16s, cvt1_, float16_t, short, v_float32)
221
DEF_CVT_FUNC(16f32s, cvt1_, float16_t, int, v_float32)
222
DEF_CVT_FUNC(16f32f, cvt1_, float16_t, float, v_float32)
223
DEF_CVT_FUNC(16f64f, cvt1_, float16_t, double, v_float32)
224
225
///////////// "conversion" w/o conversion ///////////////
226
227
static void cvt8u(const uchar* src, size_t sstep, uchar*, size_t, uchar* dst, size_t dstep, Size size, void*)
228
{ cvtCopy(src, sstep, dst, dstep, size, 1); }
229
230
static void cvt16u(const ushort* src, size_t sstep, uchar*, size_t, ushort* dst, size_t dstep, Size size, void*)
231
{ cvtCopy((const uchar*)src, sstep, (uchar*)dst, dstep, size, 2); }
232
233
static void cvt32s(const int* src, size_t sstep, uchar*, size_t, int* dst, size_t dstep, Size size, void*)
234
{ cvtCopy((const uchar*)src, sstep, (uchar*)dst, dstep, size, 4); }
235
236
static void cvt64s(const int64* src, size_t sstep, uchar*, size_t, int64* dst, size_t dstep, Size size, void*)
237
{ cvtCopy((const uchar*)src, sstep, (uchar*)dst, dstep, size, 8); }
238
239
240
/* [TODO] Recover IPP calls
241
#if defined(HAVE_IPP)
242
#define DEF_CVT_FUNC_F(suffix, stype, dtype, ippFavor) \
243
static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
244
dtype* dst, size_t dstep, Size size, double*) \
245
{ \
246
CV_IPP_RUN(src && dst, CV_INSTRUMENT_FUN_IPP(ippiConvert_##ippFavor, src, (int)sstep, dst, (int)dstep, ippiSize(size.width, size.height)) >= 0) \
247
cvt_(src, sstep, dst, dstep, size); \
248
}
249
250
#define DEF_CVT_FUNC_F2(suffix, stype, dtype, ippFavor) \
251
static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
252
dtype* dst, size_t dstep, Size size, double*) \
253
{ \
254
CV_IPP_RUN(src && dst, CV_INSTRUMENT_FUN_IPP(ippiConvert_##ippFavor, src, (int)sstep, dst, (int)dstep, ippiSize(size.width, size.height), ippRndFinancial, 0) >= 0) \
255
cvt_(src, sstep, dst, dstep, size); \
256
}
257
#else
258
#define DEF_CVT_FUNC_F(suffix, stype, dtype, ippFavor) \
259
static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
260
dtype* dst, size_t dstep, Size size, double*) \
261
{ \
262
cvt_(src, sstep, dst, dstep, size); \
263
}
264
#define DEF_CVT_FUNC_F2 DEF_CVT_FUNC_F
265
#endif
266
267
#define DEF_CVT_FUNC(suffix, stype, dtype) \
268
static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
269
dtype* dst, size_t dstep, Size size, double*) \
270
{ \
271
cvt_(src, sstep, dst, dstep, size); \
272
}
273
274
#define DEF_CPY_FUNC(suffix, stype) \
275
static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
276
stype* dst, size_t dstep, Size size, double*) \
277
{ \
278
cpy_(src, sstep, dst, dstep, size); \
279
}
280
281
DEF_CPY_FUNC(8u, uchar)
282
DEF_CVT_FUNC_F(8s8u, schar, uchar, 8s8u_C1Rs)
283
DEF_CVT_FUNC_F(16u8u, ushort, uchar, 16u8u_C1R)
284
DEF_CVT_FUNC_F(16s8u, short, uchar, 16s8u_C1R)
285
DEF_CVT_FUNC_F(32s8u, int, uchar, 32s8u_C1R)
286
DEF_CVT_FUNC_F2(32f8u, float, uchar, 32f8u_C1RSfs)
287
DEF_CVT_FUNC(64f8u, double, uchar)
288
289
DEF_CVT_FUNC_F2(8u8s, uchar, schar, 8u8s_C1RSfs)
290
DEF_CVT_FUNC_F2(16u8s, ushort, schar, 16u8s_C1RSfs)
291
DEF_CVT_FUNC_F2(16s8s, short, schar, 16s8s_C1RSfs)
292
DEF_CVT_FUNC_F(32s8s, int, schar, 32s8s_C1R)
293
DEF_CVT_FUNC_F2(32f8s, float, schar, 32f8s_C1RSfs)
294
DEF_CVT_FUNC(64f8s, double, schar)
295
296
DEF_CVT_FUNC_F(8u16u, uchar, ushort, 8u16u_C1R)
297
DEF_CVT_FUNC_F(8s16u, schar, ushort, 8s16u_C1Rs)
298
DEF_CPY_FUNC(16u, ushort)
299
DEF_CVT_FUNC_F(16s16u, short, ushort, 16s16u_C1Rs)
300
DEF_CVT_FUNC_F2(32s16u, int, ushort, 32s16u_C1RSfs)
301
DEF_CVT_FUNC_F2(32f16u, float, ushort, 32f16u_C1RSfs)
302
DEF_CVT_FUNC(64f16u, double, ushort)
303
304
DEF_CVT_FUNC_F(8u16s, uchar, short, 8u16s_C1R)
305
DEF_CVT_FUNC_F(8s16s, schar, short, 8s16s_C1R)
306
DEF_CVT_FUNC_F2(16u16s, ushort, short, 16u16s_C1RSfs)
307
DEF_CVT_FUNC_F2(32s16s, int, short, 32s16s_C1RSfs)
308
DEF_CVT_FUNC(32f16s, float, short)
309
DEF_CVT_FUNC(64f16s, double, short)
310
311
DEF_CVT_FUNC_F(8u32s, uchar, int, 8u32s_C1R)
312
DEF_CVT_FUNC_F(8s32s, schar, int, 8s32s_C1R)
313
DEF_CVT_FUNC_F(16u32s, ushort, int, 16u32s_C1R)
314
DEF_CVT_FUNC_F(16s32s, short, int, 16s32s_C1R)
315
DEF_CPY_FUNC(32s, int)
316
DEF_CVT_FUNC_F2(32f32s, float, int, 32f32s_C1RSfs)
317
DEF_CVT_FUNC(64f32s, double, int)
318
319
DEF_CVT_FUNC_F(8u32f, uchar, float, 8u32f_C1R)
320
DEF_CVT_FUNC_F(8s32f, schar, float, 8s32f_C1R)
321
DEF_CVT_FUNC_F(16u32f, ushort, float, 16u32f_C1R)
322
DEF_CVT_FUNC_F(16s32f, short, float, 16s32f_C1R)
323
DEF_CVT_FUNC_F(32s32f, int, float, 32s32f_C1R)
324
DEF_CVT_FUNC(64f32f, double, float)
325
326
DEF_CVT_FUNC(8u64f, uchar, double)
327
DEF_CVT_FUNC(8s64f, schar, double)
328
DEF_CVT_FUNC(16u64f, ushort, double)
329
DEF_CVT_FUNC(16s64f, short, double)
330
DEF_CVT_FUNC(32s64f, int, double)
331
DEF_CVT_FUNC(32f64f, float, double)
332
DEF_CPY_FUNC(64s, int64)
333
*/
334
335
BinaryFunc getConvertFunc(int sdepth, int ddepth)
336
{
337
static BinaryFunc cvtTab[][8] =
338
{
339
{
340
(BinaryFunc)(cvt8u), (BinaryFunc)GET_OPTIMIZED(cvt8s8u), (BinaryFunc)GET_OPTIMIZED(cvt16u8u),
341
(BinaryFunc)GET_OPTIMIZED(cvt16s8u), (BinaryFunc)GET_OPTIMIZED(cvt32s8u), (BinaryFunc)GET_OPTIMIZED(cvt32f8u),
342
(BinaryFunc)GET_OPTIMIZED(cvt64f8u), (BinaryFunc)(cvt16f8u)
343
},
344
{
345
(BinaryFunc)GET_OPTIMIZED(cvt8u8s), (BinaryFunc)cvt8u, (BinaryFunc)GET_OPTIMIZED(cvt16u8s),
346
(BinaryFunc)GET_OPTIMIZED(cvt16s8s), (BinaryFunc)GET_OPTIMIZED(cvt32s8s), (BinaryFunc)GET_OPTIMIZED(cvt32f8s),
347
(BinaryFunc)GET_OPTIMIZED(cvt64f8s), (BinaryFunc)(cvt16f8s)
348
},
349
{
350
(BinaryFunc)GET_OPTIMIZED(cvt8u16u), (BinaryFunc)GET_OPTIMIZED(cvt8s16u), (BinaryFunc)cvt16u,
351
(BinaryFunc)GET_OPTIMIZED(cvt16s16u), (BinaryFunc)GET_OPTIMIZED(cvt32s16u), (BinaryFunc)GET_OPTIMIZED(cvt32f16u),
352
(BinaryFunc)GET_OPTIMIZED(cvt64f16u), (BinaryFunc)(cvt16f16u)
353
},
354
{
355
(BinaryFunc)GET_OPTIMIZED(cvt8u16s), (BinaryFunc)GET_OPTIMIZED(cvt8s16s), (BinaryFunc)GET_OPTIMIZED(cvt16u16s),
356
(BinaryFunc)cvt16u, (BinaryFunc)GET_OPTIMIZED(cvt32s16s), (BinaryFunc)GET_OPTIMIZED(cvt32f16s),
357
(BinaryFunc)GET_OPTIMIZED(cvt64f16s), (BinaryFunc)(cvt16f16s)
358
},
359
{
360
(BinaryFunc)GET_OPTIMIZED(cvt8u32s), (BinaryFunc)GET_OPTIMIZED(cvt8s32s), (BinaryFunc)GET_OPTIMIZED(cvt16u32s),
361
(BinaryFunc)GET_OPTIMIZED(cvt16s32s), (BinaryFunc)cvt32s, (BinaryFunc)GET_OPTIMIZED(cvt32f32s),
362
(BinaryFunc)GET_OPTIMIZED(cvt64f32s), (BinaryFunc)(cvt16f32s)
363
},
364
{
365
(BinaryFunc)GET_OPTIMIZED(cvt8u32f), (BinaryFunc)GET_OPTIMIZED(cvt8s32f), (BinaryFunc)GET_OPTIMIZED(cvt16u32f),
366
(BinaryFunc)GET_OPTIMIZED(cvt16s32f), (BinaryFunc)GET_OPTIMIZED(cvt32s32f), (BinaryFunc)cvt32s,
367
(BinaryFunc)GET_OPTIMIZED(cvt64f32f), (BinaryFunc)(cvt16f32f)
368
},
369
{
370
(BinaryFunc)GET_OPTIMIZED(cvt8u64f), (BinaryFunc)GET_OPTIMIZED(cvt8s64f), (BinaryFunc)GET_OPTIMIZED(cvt16u64f),
371
(BinaryFunc)GET_OPTIMIZED(cvt16s64f), (BinaryFunc)GET_OPTIMIZED(cvt32s64f), (BinaryFunc)GET_OPTIMIZED(cvt32f64f),
372
(BinaryFunc)(cvt64s), (BinaryFunc)(cvt16f64f)
373
},
374
{
375
(BinaryFunc)(cvt8u16f), (BinaryFunc)(cvt8s16f), (BinaryFunc)(cvt16u16f), (BinaryFunc)(cvt16s16f),
376
(BinaryFunc)(cvt32s16f), (BinaryFunc)(cvt32f16f), (BinaryFunc)(cvt64f16f), (BinaryFunc)(cvt16u)
377
}
378
};
379
return cvtTab[CV_MAT_DEPTH(ddepth)][CV_MAT_DEPTH(sdepth)];
380
}
381
382
#ifdef HAVE_OPENCL
383
static bool ocl_convertFp16( InputArray _src, OutputArray _dst, int sdepth, int ddepth )
384
{
385
int type = _src.type(), cn = CV_MAT_CN(type);
386
387
_dst.createSameSize( _src, CV_MAKETYPE(ddepth, cn) );
388
int kercn = 1;
389
int rowsPerWI = 1;
390
String build_opt = format("-D HALF_SUPPORT -D srcT=%s -D dstT=%s -D rowsPerWI=%d%s",
391
sdepth == CV_32F ? "float" : "half",
392
sdepth == CV_32F ? "half" : "float",
393
rowsPerWI,
394
sdepth == CV_32F ? " -D FLOAT_TO_HALF " : "");
395
ocl::Kernel k("convertFp16", ocl::core::halfconvert_oclsrc, build_opt);
396
if (k.empty())
397
return false;
398
399
UMat src = _src.getUMat();
400
UMat dst = _dst.getUMat();
401
402
ocl::KernelArg srcarg = ocl::KernelArg::ReadOnlyNoSize(src),
403
dstarg = ocl::KernelArg::WriteOnly(dst, cn, kercn);
404
405
k.args(srcarg, dstarg);
406
407
size_t globalsize[2] = { (size_t)src.cols * cn / kercn, ((size_t)src.rows + rowsPerWI - 1) / rowsPerWI };
408
return k.run(2, globalsize, NULL, false);
409
}
410
#endif
411
412
} // cv::
413
414
void cv::Mat::convertTo(OutputArray _dst, int _type, double alpha, double beta) const
415
{
416
CV_INSTRUMENT_REGION();
417
418
if( empty() )
419
{
420
_dst.release();
421
return;
422
}
423
424
bool noScale = fabs(alpha-1) < DBL_EPSILON && fabs(beta) < DBL_EPSILON;
425
426
if( _type < 0 )
427
_type = _dst.fixedType() ? _dst.type() : type();
428
else
429
_type = CV_MAKETYPE(CV_MAT_DEPTH(_type), channels());
430
431
int sdepth = depth(), ddepth = CV_MAT_DEPTH(_type);
432
if( sdepth == ddepth && noScale )
433
{
434
copyTo(_dst);
435
return;
436
}
437
438
Mat src = *this;
439
if( dims <= 2 )
440
_dst.create( size(), _type );
441
else
442
_dst.create( dims, size, _type );
443
Mat dst = _dst.getMat();
444
445
446
BinaryFunc func = noScale ? getConvertFunc(sdepth, ddepth) : getConvertScaleFunc(sdepth, ddepth);
447
double scale[] = {alpha, beta};
448
int cn = channels();
449
CV_Assert( func != 0 );
450
451
if( dims <= 2 )
452
{
453
Size sz = getContinuousSize(src, dst, cn);
454
func( src.data, src.step, 0, 0, dst.data, dst.step, sz, scale );
455
}
456
else
457
{
458
const Mat* arrays[] = {&src, &dst, 0};
459
uchar* ptrs[2] = {};
460
NAryMatIterator it(arrays, ptrs);
461
Size sz((int)(it.size*cn), 1);
462
463
for( size_t i = 0; i < it.nplanes; i++, ++it )
464
func(ptrs[0], 1, 0, 0, ptrs[1], 1, sz, scale);
465
}
466
}
467
468
//==================================================================================================
469
470
void cv::convertFp16( InputArray _src, OutputArray _dst )
471
{
472
CV_INSTRUMENT_REGION();
473
474
int sdepth = _src.depth(), ddepth = 0;
475
BinaryFunc func = 0;
476
477
switch( sdepth )
478
{
479
case CV_32F:
480
if(_dst.fixedType())
481
{
482
ddepth = _dst.depth();
483
CV_Assert(ddepth == CV_16S || ddepth == CV_16F);
484
CV_Assert(_dst.channels() == _src.channels());
485
}
486
else
487
ddepth = CV_16S;
488
func = (BinaryFunc)cvt32f16f;
489
break;
490
case CV_16S:
491
case CV_16F:
492
ddepth = CV_32F;
493
func = (BinaryFunc)cvt16f32f;
494
break;
495
default:
496
CV_Error(Error::StsUnsupportedFormat, "Unsupported input depth");
497
return;
498
}
499
500
CV_OCL_RUN(_src.dims() <= 2 && _dst.isUMat(),
501
ocl_convertFp16(_src, _dst, sdepth, ddepth))
502
503
Mat src = _src.getMat();
504
505
int type = CV_MAKETYPE(ddepth, src.channels());
506
_dst.create( src.dims, src.size, type );
507
Mat dst = _dst.getMat();
508
int cn = src.channels();
509
510
CV_Assert( func != 0 );
511
512
if( src.dims <= 2 )
513
{
514
Size sz = getContinuousSize(src, dst, cn);
515
func( src.data, src.step, 0, 0, dst.data, dst.step, sz, 0);
516
}
517
else
518
{
519
const Mat* arrays[] = {&src, &dst, 0};
520
uchar* ptrs[2] = {};
521
NAryMatIterator it(arrays, ptrs);
522
Size sz((int)(it.size*cn), 1);
523
524
for( size_t i = 0; i < it.nplanes; i++, ++it )
525
func(ptrs[0], 0, 0, 0, ptrs[1], 0, sz, 0);
526
}
527
}
528
529