Arm-2D  
2D Image Processing Library for Cortex-M Processors
 
Loading...
Searching...
No Matches
arm_2d_conversion.h
1/*
2 * Copyright (C) 2022 Arm Limited or its affiliates. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19/* ----------------------------------------------------------------------
20 * Project: Arm-2D Library
21 * Title: #include "arm_2d.h"
22 * Description: Public header file to contain the APIs for colour space
23 * conversions
24 *
25 * $Date: 09. Aug 2022
26 * $Revision: V.1.0.3
27 *
28 * Target Processor: Cortex-M cores
29 * -------------------------------------------------------------------- */
30
31#ifndef __ARM_2D_CONVERSION_H__
32#define __ARM_2D_CONVERSION_H__
33
34/*============================ INCLUDES ======================================*/
35
36#include "arm_2d_types.h"
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42#if defined(__clang__)
43# pragma clang diagnostic push
44# pragma clang diagnostic ignored "-Wunknown-warning-option"
45# pragma clang diagnostic ignored "-Wreserved-identifier"
46# pragma clang diagnostic ignored "-Wdeclaration-after-statement"
47# pragma clang diagnostic ignored "-Wunknown-warning-option"
48# pragma clang diagnostic ignored "-Wreserved-identifier"
49# pragma clang diagnostic ignored "-Wsign-conversion"
50# pragma clang diagnostic ignored "-Wnarrowing"
51#elif defined(__IS_COMPILER_IAR__)
52# pragma diag_suppress=Go029
53#endif
54
55/*!
56 * \addtogroup gConversion 6 Conversion Operations
57 * @{
58 */
59
60/*============================ MACROS ========================================*/
61/*============================ MACROFIED FUNCTIONS ===========================*/
62
63#define arm_2d_convert_colour_to_rgb888(__SRC_ADDR, /* source tile address */ \
64 __DES_ADDR /* target tile address */) \
65 arm_2dp_convert_colour_to_rgb888( NULL, \
66 (__SRC_ADDR), \
67 (__DES_ADDR))
68
69#define arm_2d_convert_colour_to_rgb565(__SRC_ADDR, /* source tile address */ \
70 __DES_ADDR /* target tile address */) \
71 arm_2dp_convert_colour_to_rgb565( NULL, \
72 (__SRC_ADDR), \
73 (__DES_ADDR))
74
75#define arm_2d_pixel_brga8888_to_rgb565(__COLOUR) \
76 ({__arm_2d_color_fast_rgb_t ARM_2D_SAFE_NAME(tChannels); \
77 __arm_2d_brga8888_unpack((__COLOUR), &ARM_2D_SAFE_NAME(tChannels)); \
78 __arm_2d_rgb565_pack(&ARM_2D_SAFE_NAME(tChannels));})
79
80#define arm_2d_pixel_brga8888_to_gray8(__COLOUR) \
81 ({__arm_2d_color_fast_rgb_t ARM_2D_SAFE_NAME(tChannels); \
82 __arm_2d_brga8888_unpack((__COLOUR), &ARM_2D_SAFE_NAME(tChannels)); \
83 __arm_2d_gray8_pack(&ARM_2D_SAFE_NAME(tChannels));})
84
85/*============================ TYPES =========================================*/
86
88
89/*! \brief 3x16-bit packed RGB color
90 * autovectorizer friendly format
91 */
92typedef union {
93 uint16_t BGRA[4];
94 struct {
95 uint16_t B;
96 uint16_t G;
97 uint16_t R;
98 uint16_t A;
99 };
101
102/*============================ GLOBAL VARIABLES ==============================*/
103/*============================ PROTOTYPES ====================================*/
104
105/*----------------------------------------------------------------------------*
106 * RGB565 channels extraction/packing *
107 *----------------------------------------------------------------------------*/
108
109/*!
110 * \brief unpack a rgb565 colour into a given __arm_2d_color_fast_rgb_t object
111 * \param[in] hwColour the target rgb565 colour
112 * \param[out] ptRGB a __arm_2d_color_fast_rgb_t object
113 */
114ARM_NONNULL(2)
115__STATIC_INLINE void __arm_2d_rgb565_unpack(uint16_t hwColor,
117{
118 assert(NULL != ptRGB);
119
120 /* uses explicit extraction, leading to a more efficient autovectorized code */
121 uint16_t maskRunpk = 0x001f, maskGunpk = 0x003f;
122
123 ptRGB->B = (uint16_t) ((hwColor & maskRunpk) << 3);
124 ptRGB->R = (uint16_t) ((hwColor >> 11) << 3);
125 ptRGB->G = (uint16_t) (((hwColor >> 5) & maskGunpk) << 2);
126
127 ptRGB->A = 0xFF;
128}
129
130/*!
131 * \brief unpack a 32bit colour into a given __arm_2d_color_fast_rgb_t object
132 * \param[in] wColour the target brga888 colour
133 * \param[out] ptRGB a __arm_2d_color_fast_rgb_t object
134 */
135ARM_NONNULL(2)
136__STATIC_INLINE void __arm_2d_brga8888_unpack(uint32_t wColor,
138{
139 assert(NULL != ptRGB);
140
141 uint8_t *pchChannel = (uint8_t *)&wColor;
142
143 ptRGB->B = (uint16_t) pchChannel[0];
144 ptRGB->G = (uint16_t) pchChannel[1];
145 ptRGB->R = (uint16_t) pchChannel[2];
146 ptRGB->A = (uint16_t) pchChannel[3];
147}
148
149
150/*!
151 * \brief generate a gray8 colour from a __arm_2d_color_fast_rgb_t object
152 * \param[in] ptRGB the target __arm_2d_color_fast_rgb_t object
153 * \return uint8_t a gray8 colour
154 */
155ARM_NONNULL(1)
156__STATIC_INLINE uint8_t __arm_2d_gray8_pack(__arm_2d_color_fast_rgb_t * ptRGB)
157{
158 assert(NULL != ptRGB);
159
160 uint16_t tGrayScale = (ptRGB->R + ptRGB->G + ptRGB->B) / 3;
161
162 return (uint8_t)( (tGrayScale <= 255) * tGrayScale
163 + (tGrayScale > 255) * 255);
164}
165
166
167/*!
168 * \brief generate a rgb565 colour from a __arm_2d_color_fast_rgb_t object
169 * \param[in] ptRGB the target __arm_2d_color_fast_rgb_t object
170 * \return uint16_t a rgb565 colour
171 */
172ARM_NONNULL(1)
173__STATIC_INLINE uint16_t __arm_2d_rgb565_pack(__arm_2d_color_fast_rgb_t * ptRGB)
174{
175 assert(NULL != ptRGB);
176
177 arm_2d_color_rgb565_t tOutput = {
178 .u5R = (uint16_t) ptRGB->R >> 3,
179 .u6G = (uint16_t) ptRGB->G >> 2,
180 .u5B = (uint16_t) ptRGB->B >> 3,
181 };
182 return tOutput.tValue;
183}
184
185/*!
186 * \brief generate a cccn888 colour from a __arm_2d_color_fast_rgb_t object
187 * \param[in] ptRGB the target __arm_2d_color_fast_rgb_t object
188 * \return uint32_t a cccn888 colour
189 * \note the alpha channel will be kept in the output value
190 */
191ARM_NONNULL(1)
192__STATIC_INLINE uint32_t __arm_2d_cccn888_pack(__arm_2d_color_fast_rgb_t * ptRGB)
193{
194 assert(NULL != ptRGB);
195
196 arm_2d_color_bgra8888_t tOutput = {
197 .u8R = (uint16_t) ptRGB->R,
198 .u8G = (uint16_t) ptRGB->G,
199 .u8B = (uint16_t) ptRGB->B,
200 .u8A = (uint16_t) ptRGB->A,
201 };
202 return tOutput.tValue;
203}
204
205
206/*----------------------------------------------------------------------------*
207 * Colour Conversion *
208 *----------------------------------------------------------------------------*/
209
210/*!
211 * \brief convert the colour format of a given tile to gray8
212 * \param[in] ptOP the control block, NULL means using the default control block
213 * \param[in] ptSource the source tile
214 * \param[out] ptTarget the output tile (holding a buffer)
215 * \return arm_fsm_rt_t the operation result
216 */
217extern
218ARM_NONNULL(2,3)
220 const arm_2d_tile_t *ptSource,
221 const arm_2d_tile_t *ptTarget);
222
223/*!
224 * \brief convert the colour format of a given tile to rgb888
225 * \param[in] ptOP the control block, NULL means using the default control block
226 * \param[in] ptSource the source tile
227 * \param[out] ptTarget the output tile (holding a buffer)
228 * \return arm_fsm_rt_t the operation result
229 */
230extern
231ARM_NONNULL(2,3)
233 const arm_2d_tile_t *ptSource,
234 const arm_2d_tile_t *ptTarget);
235/*!
236 * \brief convert the colour format of a given tile to rgb565
237 * \param[in] ptOP the control block, NULL means using the default control block
238 * \param[in] ptSource the source tile
239 * \param[out] ptTarget the output tile (holding a buffer)
240 * \return arm_fsm_rt_t the operation result
241 */
242extern
243ARM_NONNULL(2,3)
245 const arm_2d_tile_t *ptSource,
246 const arm_2d_tile_t *ptTarget);
247
248/*! @} */
249
250#if defined(__clang__)
251# pragma clang diagnostic pop
252#elif defined(__IS_COMPILER_IAR__)
253# pragma diag_warning=Go029
254#endif
255
256#ifdef __cplusplus
257}
258#endif
259
260#endif