forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgray_rgb.cpp
More file actions
130 lines (100 loc) · 3.17 KB
/
gray_rgb.cpp
File metadata and controls
130 lines (100 loc) · 3.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
/*******************************************************
* 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 <arrayfire.h>
#include <gtest/gtest.h>
#include <testHelpers.hpp>
#include <af/dim4.hpp>
#include <string>
#include <vector>
using af::array;
using af::randu;
using std::vector;
TEST(rgb_gray, 32bit) {
array rgb = randu(10, 10, 3);
array gray = rgb2gray(rgb);
vector<float> h_rgb(rgb.elements());
vector<float> h_gray(gray.elements());
rgb.host(&h_rgb[0]);
gray.host(&h_gray[0]);
int num = gray.elements();
int roff = 0;
int goff = num;
int boff = 2 * num;
const float rPercent = 0.2126f;
const float gPercent = 0.7152f;
const float bPercent = 0.0722f;
for (int i = 0; i < num; i++) {
float res = rPercent * h_rgb[i + roff] + gPercent * h_rgb[i + goff] +
bPercent * h_rgb[i + boff];
ASSERT_FLOAT_EQ(res, h_gray[i]);
}
}
TEST(rgb_gray, 8bit) {
array rgb = randu(10, 10, 3, u8);
array gray = rgb2gray(rgb);
vector<uchar> h_rgb(rgb.elements());
vector<float> h_gray(gray.elements());
rgb.host(&h_rgb[0]);
gray.host(&h_gray[0]);
int num = gray.elements();
int roff = 0;
int goff = num;
int boff = 2 * num;
const float rPercent = 0.2126f;
const float gPercent = 0.7152f;
const float bPercent = 0.0722f;
for (int i = 0; i < num; i++) {
float res = rPercent * h_rgb[i + roff] + gPercent * h_rgb[i + goff] +
bPercent * h_rgb[i + boff];
ASSERT_FLOAT_EQ(res, h_gray[i]);
}
}
TEST(gray_rgb, 32bit) {
array gray = randu(10, 10);
const float rPercent = 0.33f;
const float gPercent = 0.34f;
const float bPercent = 0.33f;
array rgb = gray2rgb(gray, rPercent, gPercent, bPercent);
vector<float> h_rgb(rgb.elements());
vector<float> h_gray(gray.elements());
int num = gray.elements();
int roff = 0;
int goff = num;
int boff = 2 * num;
for (int i = 0; i < num; i++) {
float gray = h_gray[i];
float r = rPercent * gray;
float g = gPercent * gray;
float b = bPercent * gray;
ASSERT_FLOAT_EQ(r, h_rgb[i + roff]);
ASSERT_FLOAT_EQ(g, h_rgb[i + goff]);
ASSERT_FLOAT_EQ(b, h_rgb[i + boff]);
}
}
TEST(rgb_gray, MaxDim) {
size_t largeDim = 65535 * 32 + 1;
array rgb = randu(1, largeDim, 3, u8);
array gray = rgb2gray(rgb);
vector<uchar> h_rgb(rgb.elements());
vector<float> h_gray(gray.elements());
rgb.host(&h_rgb[0]);
gray.host(&h_gray[0]);
int num = gray.elements();
int roff = 0;
int goff = num;
int boff = 2 * num;
const float rPercent = 0.2126f;
const float gPercent = 0.7152f;
const float bPercent = 0.0722f;
for (int i = 0; i < num; i++) {
float res = rPercent * h_rgb[i + roff] + gPercent * h_rgb[i + goff] +
bPercent * h_rgb[i + boff];
ASSERT_FLOAT_EQ(res, h_gray[i]);
}
}