MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / rgb2gray

Function rgb2gray

src/api/c/rgb_gray.cpp:39–72  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

37
38template<typename T, typename cType>
39static af_array rgb2gray(const af_array& in, const float r, const float g,
40 const float b) {
41 Array<cType> input = cast<cType>(getArray<T>(in));
42 dim4 inputDims = input.dims();
43 dim4 matDims(inputDims[0], inputDims[1], 1, inputDims[3]);
44
45 Array<cType> rCnst = createValueArray<cType>(matDims, scalar<cType>(r));
46 Array<cType> gCnst = createValueArray<cType>(matDims, scalar<cType>(g));
47 Array<cType> bCnst = createValueArray<cType>(matDims, scalar<cType>(b));
48
49 std::vector<af_seq> slice1(4, af_span), slice2(4, af_span),
50 slice3(4, af_span);
51 // extract three channels as three slices
52 slice1[2] = {0, 0, 1};
53 slice2[2] = {1, 1, 1};
54 slice3[2] = {2, 2, 1};
55
56 Array<cType> ch1Temp = createSubArray(input, slice1);
57 Array<cType> ch2Temp = createSubArray(input, slice2);
58 Array<cType> ch3Temp = createSubArray(input, slice3);
59
60 // r*Slice0
61 Array<cType> expr1 = arithOp<cType, af_mul_t>(ch1Temp, rCnst, matDims);
62 // g*Slice1
63 Array<cType> expr2 = arithOp<cType, af_mul_t>(ch2Temp, gCnst, matDims);
64 // b*Slice2
65 Array<cType> expr3 = arithOp<cType, af_mul_t>(ch3Temp, bCnst, matDims);
66 // r*Slice0 + g*Slice1
67 Array<cType> expr4 = arithOp<cType, af_add_t>(expr1, expr2, matDims);
68 // r*Slice0 + g*Slice1 + b*Slice2
69 Array<cType> result = arithOp<cType, af_add_t>(expr3, expr4, matDims);
70
71 return getHandle<cType>(result);
72}
73
74template<typename T, typename cType>
75static af_array gray2rgb(const af_array& in, const float r, const float g,

Callers 2

gray_rgb.cppFile · 0.50
TESTFunction · 0.50

Calls 2

createSubArrayFunction · 0.50
dimsMethod · 0.45

Tested by

no test coverage detected