Gamp v0.0.7-54-gccdc599
Gamp: Graphics, Audio, Multimedia and Processing
Loading...
Searching...
No Matches
PixelUtil.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright Gothel Software e.K.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * This Source Code Form is subject to the terms of the MIT License
8 * If a copy of the MIT was not distributed with this file,
9 * you can obtain one at https://opensource.org/license/mit/.
10 */
11#ifndef GAMP_RENDER_PIXEL_PIXELUTIL_HPP_
12#define GAMP_RENDER_PIXEL_PIXELUTIL_HPP_
13
14#include <climits>
15#include <cstdint>
16#include <limits>
17
18#include <jau/basic_types.hpp>
19#include <jau/int_math.hpp>
20#include <jau/int_types.hpp>
21#include <jau/math/recti.hpp>
22#include <jau/math/vec2i.hpp>
23#include <jau/string_util.hpp>
24#include <jau/enum_util.hpp>
25
26#include <gamp/GampTypes.hpp>
28
29namespace gamp::render::pixel {
30
31 /** @addtogroup Gamp_Pixel
32 *
33 * @{
34 */
35
36 /** Component types */
37 enum class CType : uint8_t {
38 /** undefined */
39 none = 0,
40
41 /** Red component */
42 R = 1,
43 /** Green component */
44 G = 2,
45 /** Blue component */
46 B = 3,
47 /** Alpha component */
48 A = 4,
49 /** Luminance component, e.g. grayscale or Y of YUV */
50 Y = 10,
51 /** U component of YUV */
52 U = 11,
53 /** V component of YUV */
54 V = 12
55 };
57
58 typedef std::array<uint32_t, 4> u32array4_t;
59 typedef std::array<CType, 4> comparray4_t;
60
61 /** maximum index, denoting `no position` or `not found`. */
62 inline constexpr uint32_t npos = std::numeric_limits<uint32_t>::max();
63
64 /** Returns first index of given {@link CType} within `pool` , npos if not exists. */
65 inline uint32_t find(CType s, jau::nsize_t poolCount, const comparray4_t& pool, bool mapRGB2Y) noexcept {
66 uint32_t i=0;
67 while( i < poolCount && pool[i] != s) { ++i; }
68
69 if( poolCount == i ) {
70 // Not found
71 if( mapRGB2Y && 1 == poolCount && pool[0] == CType::Y &&
72 ( CType::R == s ||
73 CType::G == s ||
74 CType::B == s ) )
75 {
76 // Special not-found case: Mapping LUMINANCE/Y -> RGB
77 return 0;
78 } else {
79 return npos;
80 }
81 } else {
82 return i;
83 }
84 }
85
87 private:
88 jau::nsize_t m_dstCount, m_srcCount;
89
90 /**
91 * Contains the source index for each destination index,
92 * length is {@link Composition#componentCount()} of destination.
93 */
94 u32array4_t dst2src;
95 /**
96 * Contains the destination index for each source index,
97 * length is {@link Composition#componentCount()} of source.
98 */
99 u32array4_t src2dst;
100
101 /**
102 * Contains the source index of RGBA components.
103 */
104 u32array4_t srcRGBA;
105 bool hasSrcRGB;
106
107 public:
108 constexpr ComponentMap(jau::nsize_t srcCompCount, const comparray4_t& srcCompOrder,
109 jau::nsize_t dstCompCount, const comparray4_t& dstCompOrder) noexcept
110 : m_dstCount(dstCompCount), m_srcCount(srcCompCount), dst2src(), src2dst()
111 {
112 for(jau::nsize_t i=0; i<m_dstCount; ++i) {
113 dst2src[i] = find(dstCompOrder[i], srcCompCount, srcCompOrder, true);
114 }
115 for(jau::nsize_t i=0; i<m_srcCount; ++i) {
116 src2dst[i] = find(srcCompOrder[i], dstCompCount, dstCompOrder, true);
117 }
118 srcRGBA[0] = find(CType::R, srcCompCount, srcCompOrder, false);
119 srcRGBA[0] = find(CType::G, srcCompCount, srcCompOrder, false);
120 srcRGBA[0] = find(CType::B, srcCompCount, srcCompOrder, false);
121 srcRGBA[0] = find(CType::A, srcCompCount, srcCompOrder, false);
122 hasSrcRGB = npos != srcRGBA[0] && npos != srcRGBA[1] && npos != srcRGBA[2];
123 }
124 };
125
126 /**
127 * Returns shifted bytes from the given {@code data} at given {@code offset}
128 * of maximal 4 {@code bytesPerPixel}.
129 * @param bytesPerPixel number of bytes per pixel to fetch, a maximum of 4 are allowed
130 * @param data byte buffer covering at least one complete pixel, i.e. bytesPerPixel bytes
131 * @return the shifted 32bit integer value of the pixel
132 */
133 inline uint32_t getShifted32(uint32_t bytesPerPixel, const uint8_t* data) {
134 if( bytesPerPixel > 4 ) {
135 throw jau::IllegalArgumentError(std::to_string(bytesPerPixel)+" bytesPerPixel too big, i.e. > 4", E_FILE_LINE);
136 }
137 uint32_t res = 0;
138 for(uint32_t i=0; i<bytesPerPixel; ++i) {
139 res |= ( 0xffU & data[i] ) << CHAR_BIT*i;
140 }
141 return res;
142 }
143 /**
144 * Returns shifted bytes from the given {@code data} at given {@code offset}
145 * of maximal 8 {@code bytesPerPixel}.
146 * @param bytesPerPixel number of bytes per pixel to fetch, a maximum of 8 are allowed
147 * @param data byte buffer covering at least one complete pixel, i.e. bytesPerPixel bytes
148 * @return the shifted 64bit integer value of the pixel
149 */
150 inline uint64_t getShifted64(uint32_t bytesPerPixel, const uint8_t* data) {
151 if( bytesPerPixel > 8 ) {
152 throw jau::IllegalArgumentError(std::to_string(bytesPerPixel)+" bytesPerPixel too big, i.e. > 8", E_FILE_LINE);
153 }
154 uint64_t res = 0;
155 for(uint32_t i=0; i<bytesPerPixel; ++i) {
156 res |= ( 0xffU & data[i] ) << CHAR_BIT*i;
157 }
158 return res;
159 }
160
161
162} // namespace gamp::render::pixel
163
164#endif /* GAMP_RENDER_PIXEL_PIXELUTIL_HPP_ */
#define E_FILE_LINE
#define JAU_MAKE_ENUM_STRING(type,...)
std::array< uint32_t, 4 > u32array4_t
Definition PixelUtil.hpp:58
CType
Component types.
Definition PixelUtil.hpp:37
uint32_t find(CType s, jau::nsize_t poolCount, const comparray4_t &pool, bool mapRGB2Y) noexcept
Returns first index of given CType within pool , npos if not exists.
Definition PixelUtil.hpp:65
uint64_t getShifted64(uint32_t bytesPerPixel, const uint8_t *data)
Returns shifted bytes from the given data at given offset of maximal 8 bytesPerPixel.
constexpr uint32_t npos
maximum index, denoting no position or not found.
Definition PixelUtil.hpp:62
std::array< CType, 4 > comparray4_t
Definition PixelUtil.hpp:59
uint32_t getShifted32(uint32_t bytesPerPixel, const uint8_t *data)
Returns shifted bytes from the given data at given offset of maximal 4 bytesPerPixel.
constexpr ComponentMap(jau::nsize_t srcCompCount, const comparray4_t &srcCompOrder, jau::nsize_t dstCompCount, const comparray4_t &dstCompOrder) noexcept
@ U
U component of YUV.
Definition PixelUtil.hpp:52
@ V
V component of YUV.
Definition PixelUtil.hpp:54
@ Y
Luminance component, e.g.
Definition PixelUtil.hpp:50
@ A
Alpha component.
Definition PixelUtil.hpp:48
@ G
Green component.
Definition PixelUtil.hpp:44
uint_bytes_t< sizeof(unsigned long int)> nsize_t
Natural 'size_t' alternative using uint<XX>_t with xx = sizeof(unsigned long int)*8 as its natural si...
Definition int_types.hpp:85