1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "modules/desktop_capture/rgba_color.h"
12
13 #include "rtc_base/system/arch.h"
14
15 namespace webrtc {
16
17 namespace {
18
AlphaEquals(uint8_t i,uint8_t j)19 bool AlphaEquals(uint8_t i, uint8_t j) {
20 // On Linux and Windows 8 or early version, '0' was returned for alpha channel
21 // from capturer APIs, on Windows 10, '255' was returned. So a workaround is
22 // to treat 0 as 255.
23 return i == j || ((i == 0 || i == 255) && (j == 0 || j == 255));
24 }
25
26 } // namespace
27
RgbaColor(uint8_t blue,uint8_t green,uint8_t red,uint8_t alpha)28 RgbaColor::RgbaColor(uint8_t blue, uint8_t green, uint8_t red, uint8_t alpha) {
29 this->blue = blue;
30 this->green = green;
31 this->red = red;
32 this->alpha = alpha;
33 }
34
RgbaColor(uint8_t blue,uint8_t green,uint8_t red)35 RgbaColor::RgbaColor(uint8_t blue, uint8_t green, uint8_t red)
36 : RgbaColor(blue, green, red, 0xff) {}
37
RgbaColor(const uint8_t * bgra)38 RgbaColor::RgbaColor(const uint8_t* bgra)
39 : RgbaColor(bgra[0], bgra[1], bgra[2], bgra[3]) {}
40
RgbaColor(uint32_t bgra)41 RgbaColor::RgbaColor(uint32_t bgra)
42 : RgbaColor(reinterpret_cast<uint8_t*>(&bgra)) {}
43
operator ==(const RgbaColor & right) const44 bool RgbaColor::operator==(const RgbaColor& right) const {
45 return blue == right.blue && green == right.green && red == right.red &&
46 AlphaEquals(alpha, right.alpha);
47 }
48
operator !=(const RgbaColor & right) const49 bool RgbaColor::operator!=(const RgbaColor& right) const {
50 return !(*this == right);
51 }
52
ToUInt32() const53 uint32_t RgbaColor::ToUInt32() const {
54 #if defined(WEBRTC_ARCH_LITTLE_ENDIAN)
55 return blue | (green << 8) | (red << 16) | (alpha << 24);
56 #else
57 return (blue << 24) | (green << 16) | (red << 8) | alpha;
58 #endif
59 }
60
61 } // namespace webrtc
62