Arm-2D  
2D Image Processing Library for Cortex-M Processors
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: 29. April 2024
26 * $Revision: V.1.0.6
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
57/*============================ MACROS ========================================*/
58
59/*!
60 * \addtogroup Deprecated
61 * @{
62 */
63#define arm_2dp_convert_colour_to_rgb888 arm_2dp_convert_colour_to_cccn888
64#define arm_2d_convert_colour_to_rgb888 arm_2d_convert_colour_to_cccn888
65/*! @} */
66
67
68/*!
69 * \addtogroup gConversion 6 Conversion Operations
70 * @{
71 */
72
73/*============================ MACROFIED FUNCTIONS ===========================*/
74
75#define arm_2d_convert_colour_to_gray8( __SRC_ADDR, /* source tile address */ \
76 __DES_ADDR /* target tile address */) \
77 arm_2dp_convert_colour_to_gray8(NULL, \
78 (__SRC_ADDR), \
79 (__DES_ADDR))
80
81#define arm_2d_tile_copy_to_gray8( __SRC_ADDR, /* source tile address */ \
82 __DES_ADDR, /* target tile address */ \
83 __DES_REGION) /* target region address */ \
84 arm_2dp_tile_copy_to_gray8( NULL, \
85 (__SRC_ADDR), \
86 (__DES_ADDR), \
87 (__DES_REGION))
88
89#define arm_2d_convert_colour_to_rgb565(__SRC_ADDR, /* source tile address */ \
90 __DES_ADDR /* target tile address */) \
91 arm_2dp_convert_colour_to_rgb565( NULL, \
92 (__SRC_ADDR), \
93 (__DES_ADDR))
94
95#define arm_2d_tile_copy_to_rgb565( __SRC_ADDR, /* source tile address */ \
96 __DES_ADDR, /* target tile address */ \
97 __DES_REGION) /* target region address */ \
98 arm_2dp_tile_copy_to_rgb565(NULL, \
99 (__SRC_ADDR), \
100 (__DES_ADDR), \
101 (__DES_REGION))
102
103#define arm_2d_convert_colour_to_cccn888(__SRC_ADDR, /* source tile address */ \
104 __DES_ADDR /* target tile address */) \
105 arm_2dp_convert_colour_to_cccn888( NULL, \
106 (__SRC_ADDR), \
107 (__DES_ADDR))
108
109#define arm_2d_tile_copy_to_cccn888(__SRC_ADDR, /* source tile address */ \
110 __DES_ADDR, /* target tile address */ \
111 __DES_REGION) /* target region address */ \
112 arm_2dp_tile_copy_to_cccn888( NULL, \
113 (__SRC_ADDR), \
114 (__DES_ADDR), \
115 (__DES_REGION))
116
117#define arm_2d_pixel_ccca8888_to_rgb565(__COLOUR) \
118 ({__arm_2d_color_fast_rgb_t ARM_2D_SAFE_NAME(tChannels); \
119 __arm_2d_ccca8888_unpack((__COLOUR), &ARM_2D_SAFE_NAME(tChannels)); \
120 __arm_2d_rgb565_pack(&ARM_2D_SAFE_NAME(tChannels));})
121
122#define arm_2d_pixel_ccca8888_to_gray8(__COLOUR) \
123 ({__arm_2d_color_fast_rgb_t ARM_2D_SAFE_NAME(tChannels); \
124 __arm_2d_ccca8888_unpack((__COLOUR), &ARM_2D_SAFE_NAME(tChannels)); \
125 __arm_2d_gray8_pack(&ARM_2D_SAFE_NAME(tChannels));})
126
127#define arm_2d_pixel_brga8888_to_rgb565 arm_2d_pixel_ccca8888_to_rgb565
128#define arm_2d_pixel_brga8888_to_gray8 arm_2d_pixel_ccca8888_to_gray8
129
130/*============================ TYPES =========================================*/
131
133
134/*! \brief 3x16-bit packed RGB color
135 * autovectorizer friendly format
136 */
137typedef union {
138 uint16_t BGRA[4];
139 struct {
140 uint16_t B;
141 uint16_t G;
142 uint16_t R;
143 uint16_t A;
144 };
146
147/*============================ GLOBAL VARIABLES ==============================*/
148/*============================ PROTOTYPES ====================================*/
149
150/*----------------------------------------------------------------------------*
151 * RGB565 channels extraction/packing *
152 *----------------------------------------------------------------------------*/
153
154/*!
155 * \brief unpack a rgb565 colour into a given __arm_2d_color_fast_rgb_t object
156 * \param[in] hwColour the target rgb565 colour
157 * \param[in] ptRGB a __arm_2d_color_fast_rgb_t object
158 */
159ARM_NONNULL(2)
160__STATIC_INLINE void __arm_2d_rgb565_unpack(uint16_t hwColor,
162{
163 assert(NULL != ptRGB);
164
165 /* uses explicit extraction, leading to a more efficient autovectorized code */
166 uint16_t maskRunpk = 0x001f, maskGunpk = 0x003f;
167
168 ptRGB->B = (uint16_t) ((hwColor & maskRunpk) << 3);
169 ptRGB->R = (uint16_t) ((hwColor >> 11) << 3);
170 ptRGB->G = (uint16_t) (((hwColor >> 5) & maskGunpk) << 2);
171
172 ptRGB->A = 0xFF;
173}
174
175/*!
176 * \brief unpack a 32bit colour into a given __arm_2d_color_fast_rgb_t object
177 * \param[in] wColour the target brga888 colour
178 * \param[in] ptRGB a __arm_2d_color_fast_rgb_t object
179 */
180ARM_NONNULL(2)
181__STATIC_INLINE void __arm_2d_ccca8888_unpack( uint32_t wColor,
183{
184 assert(NULL != ptRGB);
185
186 uint8_t *pchChannel = (uint8_t *)&wColor;
187
188 ptRGB->B = (uint16_t) pchChannel[0];
189 ptRGB->G = (uint16_t) pchChannel[1];
190 ptRGB->R = (uint16_t) pchChannel[2];
191 ptRGB->A = (uint16_t) pchChannel[3];
192}
193
194
195/*!
196 * \brief generate a gray8 colour from a __arm_2d_color_fast_rgb_t object
197 * \param[in] ptRGB the target __arm_2d_color_fast_rgb_t object
198 * \return uint8_t a gray8 colour
199 */
200ARM_NONNULL(1)
201__STATIC_INLINE uint8_t __arm_2d_gray8_pack(__arm_2d_color_fast_rgb_t * ptRGB)
202{
203 assert(NULL != ptRGB);
204
205 uint16_t tGrayScale = (ptRGB->R + ptRGB->G + ptRGB->B) / 3;
206
207 return (uint8_t)( (tGrayScale <= 255) * tGrayScale
208 + (tGrayScale > 255) * 255);
209}
210
211
212/*!
213 * \brief generate a rgb565 colour from a __arm_2d_color_fast_rgb_t object
214 * \param[in] ptRGB the target __arm_2d_color_fast_rgb_t object
215 * \return uint16_t a rgb565 colour
216 */
217ARM_NONNULL(1)
218__STATIC_INLINE uint16_t __arm_2d_rgb565_pack(__arm_2d_color_fast_rgb_t * ptRGB)
219{
220 assert(NULL != ptRGB);
221
222 arm_2d_color_rgb565_t tOutput = {
223 .u5B = (uint16_t) (ptRGB->B >> 3),
224 .u6G = (uint16_t) (ptRGB->G >> 2),
225 .u5R = (uint16_t) (ptRGB->R >> 3),
226 };
227 return tOutput.tValue;
228}
229
230/*!
231 * \brief generate a cccn888 colour from a __arm_2d_color_fast_rgb_t object
232 * \param[in] ptRGB the target __arm_2d_color_fast_rgb_t object
233 * \return uint32_t a cccn888 colour
234 * \note the alpha channel will be kept in the output value
235 */
236ARM_NONNULL(1)
237__STATIC_INLINE uint32_t __arm_2d_ccca888_pack(__arm_2d_color_fast_rgb_t * ptRGB)
238{
239 assert(NULL != ptRGB);
240
241 arm_2d_color_bgra8888_t tOutput = {
242 .u8B = (uint16_t) ptRGB->B,
243 .u8G = (uint16_t) ptRGB->G,
244 .u8R = (uint16_t) ptRGB->R,
245 .u8A = (uint16_t) ptRGB->A,
246 };
247 return tOutput.tValue;
248}
249
250
251/*----------------------------------------------------------------------------*
252 * Colour Conversion *
253 *----------------------------------------------------------------------------*/
254
255/*!
256 * \brief convert the colour format of a given tile to gray8
257 * \param[in] ptOP the control block, NULL means using the default control block
258 * \param[in] ptSource the source tile
259 * \param[in] ptTarget the output tile (holding a buffer)
260 * \return arm_fsm_rt_t the operation result
261 */
262extern
263ARM_NONNULL(2,3)
265 const arm_2d_tile_t *ptSource,
266 const arm_2d_tile_t *ptTarget);
267
268/*!
269 * \brief copy a given tile to a gray8 target tile
270 * \param[in] ptOP the control block, NULL means using the default control block
271 * \param[in] ptSource the source tile
272 * \param[in] ptTarget the output tile (holding a buffer)
273 * \param[in] ptRegion the target region, NULL means using the region of the
274 * target tile.
275 * \return arm_fsm_rt_t the operation result
276 */
277extern
278ARM_NONNULL(2,3)
280 const arm_2d_tile_t *ptSource,
281 const arm_2d_tile_t *ptTarget,
282 const arm_2d_region_t *ptRegion);
283
284/*!
285 * \brief convert the colour format of a given tile to rgb565
286 * \param[in] ptOP the control block, NULL means using the default control block
287 * \param[in] ptSource the source tile
288 * \param[in] ptTarget the output tile (holding a buffer)
289 * \return arm_fsm_rt_t the operation result
290 */
291extern
292ARM_NONNULL(2,3)
294 const arm_2d_tile_t *ptSource,
295 const arm_2d_tile_t *ptTarget);
296
297/*!
298 * \brief copy a given tile to a rgb565 target tile
299 * \param[in] ptOP the control block, NULL means using the default control block
300 * \param[in] ptSource the source tile
301 * \param[in] ptTarget the output tile (holding a buffer)
302 * \param[in] ptRegion the target region, NULL means using the region of the
303 * target tile.
304 * \return arm_fsm_rt_t the operation result
305 */
306extern
307ARM_NONNULL(2,3)
309 const arm_2d_tile_t *ptSource,
310 const arm_2d_tile_t *ptTarget,
311 const arm_2d_region_t *ptRegion);
312
313/*!
314 * \brief convert the colour format of a given tile to cccn888
315 * \param[in] ptOP the control block, NULL means using the default control block
316 * \param[in] ptSource the source tile
317 * \param[in] ptTarget the output tile (holding a buffer)
318 * \return arm_fsm_rt_t the operation result
319 */
320extern
321ARM_NONNULL(2,3)
323 const arm_2d_tile_t *ptSource,
324 const arm_2d_tile_t *ptTarget);
325
326/*!
327 * \brief copy a given tile to a cccn888 target tile
328 * \param[in] ptOP the control block, NULL means using the default control block
329 * \param[in] ptSource the source tile
330 * \param[in] ptTarget the output tile (holding a buffer)
331 * \param[in] ptRegion the target region, NULL means using the region of the
332 * target tile.
333 * \return arm_fsm_rt_t the operation result
334 */
335extern
336ARM_NONNULL(2,3)
338 const arm_2d_tile_t *ptSource,
339 const arm_2d_tile_t *ptTarget,
340 const arm_2d_region_t *ptRegion);
341
342/*! @} */
343
344#if defined(__clang__)
345# pragma clang diagnostic pop
346#elif defined(__IS_COMPILER_IAR__)
347# pragma diag_warning=Go029
348#endif
349
350#ifdef __cplusplus
351}
352#endif
353
354#endif