File: gray_rgb.cpp

package info (click to toggle)
arrayfire 3.3.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 109,016 kB
  • sloc: cpp: 127,909; lisp: 6,878; python: 3,923; ansic: 1,051; sh: 347; makefile: 338; xml: 175
file content (105 lines) | stat: -rw-r--r-- 2,572 bytes parent folder | download
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
/*******************************************************
 * 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 <gtest/gtest.h>
#include <arrayfire.h>
#include <af/dim4.hpp>
#include <string>
#include <vector>
#include <testHelpers.hpp>

TEST(rgb_gray, 32bit)
{
    af::array rgb = af::randu(10, 10, 3);
    af::array gray = af::rgb2gray(rgb);

    std::vector<float> h_rgb(rgb.elements());
    std::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)
{
    af::array rgb = af::randu(10, 10, 3, u8);
    af::array gray = af::rgb2gray(rgb);

    std::vector<uchar> h_rgb(rgb.elements());
    std::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)
{
    af::array gray = af::randu(10, 10);

    const float rPercent=0.33f;
    const float gPercent=0.34f;
    const float bPercent=0.33f;

    af::array rgb = af::gray2rgb(gray, rPercent, gPercent, bPercent);
    std::vector<float> h_rgb(rgb.elements());
    std::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]);
    }
}