gem5  v20.1.0.0
pixel.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #ifndef __BASE_PIXEL_HH__
39 #define __BASE_PIXEL_HH__
40 
41 #include <cmath>
42 #include <cstdint>
43 #include <string>
44 #include <vector>
45 
46 #include "base/compiler.hh"
47 #include "base/cprintf.hh"
48 #include "base/str.hh"
49 #include "base/types.hh"
50 
54 struct Pixel
55 {
57  : red(0), green(0), blue(0), padding(0) {}
58 
59  Pixel(uint8_t _red, uint8_t _green, uint8_t _blue)
60  : red(_red), green(_green), blue(_blue), padding(0) {}
61 
62  uint8_t red;
63  uint8_t green;
64  uint8_t blue;
65  uint8_t padding;
66 };
67 
68 inline bool
69 operator==(const Pixel &lhs, const Pixel &rhs)
70 {
71  return lhs.red == rhs.red &&
72  lhs.green == rhs.green &&
73  lhs.blue == rhs.blue &&
74  lhs.padding == rhs.padding;
75 }
76 
88 {
89  public:
93  struct Channel {
98  Channel(unsigned offset, unsigned width);
99 
104  uint8_t toPixel(uint32_t word) const {
105  return round(((word >> offset) & mask) * factor);
106  }
107 
112  uint32_t fromPixel(uint8_t ch) const {
113  return (static_cast<uint8_t>(round(ch / factor)) & mask) << offset;
114  }
115 
117  unsigned offset;
119  unsigned mask;
124  float factor;
125  };
126 
127  PixelConverter(unsigned length,
128  unsigned ro, unsigned go, unsigned bo,
129  unsigned rw, unsigned gw, unsigned bw,
130  ByteOrder byte_order = ByteOrder::little);
131 
133  Pixel toPixel(uint32_t word) const {
134  return Pixel(ch_r.toPixel(word),
135  ch_g.toPixel(word),
136  ch_b.toPixel(word));
137  }
138 
140  Pixel toPixel(const uint8_t *rfb) const {
141  return toPixel(readWord(rfb));
142  }
143 
145  uint32_t fromPixel(const Pixel &pixel) const {
146  return ch_r.fromPixel(pixel.red) |
147  ch_g.fromPixel(pixel.green) |
148  ch_b.fromPixel(pixel.blue);
149  }
150 
155  void fromPixel(uint8_t *rfb, const Pixel &pixel) const {
156  writeWord(rfb, fromPixel(pixel));
157  }
158 
169  uint32_t readWord(const uint8_t *p) const;
176  void writeWord(uint8_t *p, uint32_t word) const;
177 
179  unsigned length;
185  unsigned depth;
187  ByteOrder byte_order;
188 
195 
201  static const PixelConverter rgb565_le;
202 
208  static const PixelConverter rgb565_be;
209 };
210 
211 inline bool
212 to_number(const std::string &value, Pixel &retval)
213 {
214  uint32_t num;
215  if (!to_number(value, num))
216  return false;
217 
218  retval = PixelConverter::rgba8888_le.toPixel(num);
219  return true;
220 }
221 
222 inline std::ostream &
223 operator<<(std::ostream &os, const Pixel &pxl)
224 {
225  os << csprintf("%#.08x", PixelConverter::rgba8888_le.fromPixel(pxl));
226  return os;
227 }
228 
229 #endif // __BASE_PIXEL_HH__
PixelConverter::Channel::fromPixel
uint32_t fromPixel(uint8_t ch) const
Convert an 8-bit representation of a color into an external format.
Definition: pixel.hh:112
PixelConverter::Channel::mask
unsigned mask
Bit mask (after shifting)
Definition: pixel.hh:119
X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:803
sc_dt::word
unsigned int word
Definition: scfx_mant.hh:96
Pixel::blue
uint8_t blue
Definition: pixel.hh:64
PixelConverter::Channel::factor
float factor
Scaling factor when converting to the full range of an 8-bit color channel.
Definition: pixel.hh:124
to_number
bool to_number(const std::string &value, Pixel &retval)
Definition: pixel.hh:212
PixelConverter::fromPixel
void fromPixel(uint8_t *rfb, const Pixel &pixel) const
Convert a pixel into a color word and store the resulting word in memory.
Definition: pixel.hh:155
PixelConverter
Configurable RGB pixel converter.
Definition: pixel.hh:87
PixelConverter::rgb565_be
static const PixelConverter rgb565_be
Predefined 16-bit RGB565 (red in least significant bits, big endian) conversion helper.
Definition: pixel.hh:208
ArmISA::width
Bitfield< 4 > width
Definition: miscregs_types.hh:68
Pixel::red
uint8_t red
Definition: pixel.hh:62
PixelConverter::Channel::Channel
Channel(unsigned offset, unsigned width)
Definition: pixel.cc:65
PixelConverter::Channel::toPixel
uint8_t toPixel(uint32_t word) const
Get the value of a single color channel represented as an 8-bit number.
Definition: pixel.hh:104
PowerISA::bo
Bitfield< 25, 21 > bo
Definition: types.hh:62
PixelConverter::PixelConverter
PixelConverter(unsigned length, unsigned ro, unsigned go, unsigned bo, unsigned rw, unsigned gw, unsigned bw, ByteOrder byte_order=ByteOrder::little)
Definition: pixel.cc:51
PixelConverter::rgba8888_le
static const PixelConverter rgba8888_le
Predefined 32-bit RGB (red in least significant bits, 8 bits/channel, little endian) conversion helpe...
Definition: pixel.hh:198
PixelConverter::ch_g
Channel ch_g
Green channel conversion helper.
Definition: pixel.hh:192
PixelConverter::byte_order
ByteOrder byte_order
Byte order when stored to memory.
Definition: pixel.hh:187
PixelConverter::Channel
Color channel conversion and scaling helper class.
Definition: pixel.hh:93
PixelConverter::toPixel
Pixel toPixel(uint32_t word) const
Get the Pixel representation of a color word.
Definition: pixel.hh:133
str.hh
Pixel::Pixel
Pixel(uint8_t _red, uint8_t _green, uint8_t _blue)
Definition: pixel.hh:59
PixelConverter::writeWord
void writeWord(uint8_t *p, uint32_t word) const
Write a word of a given length and endianness to memory.
Definition: pixel.cc:89
PixelConverter::length
unsigned length
Bytes per pixel when stored in memory (including padding)
Definition: pixel.hh:179
PixelConverter::depth
unsigned depth
Number of bits used to represent one pixel value (excluding padding).
Definition: pixel.hh:185
PixelConverter::rgba8888_be
static const PixelConverter rgba8888_be
Predefined 32-bit RGB (red in least significant bits, 8 bits/channel, big endian) conversion helper.
Definition: pixel.hh:205
cprintf.hh
compiler.hh
Pixel
Internal gem5 representation of a Pixel.
Definition: pixel.hh:54
PixelConverter::ch_b
Channel ch_b
Blue channel conversion helper.
Definition: pixel.hh:194
operator==
bool operator==(const Pixel &lhs, const Pixel &rhs)
Definition: pixel.hh:69
PixelConverter::rgb565_le
static const PixelConverter rgb565_le
Predefined 16-bit RGB565 (red in least significant bits, little endian) conversion helper.
Definition: pixel.hh:201
operator<<
std::ostream & operator<<(std::ostream &os, const Pixel &pxl)
Definition: pixel.hh:223
PixelConverter::Channel::offset
unsigned offset
Offset in bits.
Definition: pixel.hh:117
PixelConverter::ch_r
Channel ch_r
Red channel conversion helper.
Definition: pixel.hh:190
Pixel::Pixel
Pixel()
Definition: pixel.hh:56
PixelConverter::readWord
uint32_t readWord(const uint8_t *p) const
Read a word of a given length and endianness from memory.
Definition: pixel.cc:73
types.hh
Pixel::padding
uint8_t padding
Definition: pixel.hh:65
ArmISA::rw
Bitfield< 31 > rw
Definition: miscregs_types.hh:249
PixelConverter::toPixel
Pixel toPixel(const uint8_t *rfb) const
Get a Pixel representation by reading a word from memory.
Definition: pixel.hh:140
MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
Pixel::green
uint8_t green
Definition: pixel.hh:63
csprintf
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:158
PixelConverter::fromPixel
uint32_t fromPixel(const Pixel &pixel) const
Convert a Pixel into a color word.
Definition: pixel.hh:145

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17