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 * Colour channels extraction/packing *
152 *----------------------------------------------------------------------------*/
153
154/*!
155 * \brief unpack a 8bit colour into a given __arm_2d_color_fast_rgb_t object
156 * \param[in] wColour the target brga888 colour
157 * \param[in] ptRGB a __arm_2d_color_fast_rgb_t object
158 */
159ARM_NONNULL(2)
160__STATIC_INLINE void __arm_2d_gray8_unpack( uint8_t chColor,
162{
163 assert(NULL != ptRGB);
164
165 ptRGB->B = (uint16_t) chColor;
166 ptRGB->G = (uint16_t) chColor;
167 ptRGB->R = (uint16_t) chColor;
168 ptRGB->A = (uint16_t) 0xFF;
169}
170
171/*!
172 * \brief unpack a rgb565 colour into a given __arm_2d_color_fast_rgb_t object
173 * \param[in] hwColour the target rgb565 colour
174 * \param[in] ptRGB a __arm_2d_color_fast_rgb_t object
175 */
176ARM_NONNULL(2)
177__STATIC_INLINE void __arm_2d_rgb565_unpack(uint16_t hwColor,
179{
180 assert(NULL != ptRGB);
181
182 /* uses explicit extraction, leading to a more efficient autovectorized code */
183 uint16_t maskRunpk = 0x001f, maskGunpk = 0x003f;
184
185 ptRGB->B = (uint16_t) ((hwColor & maskRunpk) << 3);
186 ptRGB->R = (uint16_t) ((hwColor >> 11) << 3);
187 ptRGB->G = (uint16_t) (((hwColor >> 5) & maskGunpk) << 2);
188
189 ptRGB->A = 0xFF;
190}
191
192/*!
193 * \brief unpack a 32bit colour into a given __arm_2d_color_fast_rgb_t object
194 * \param[in] wColour the target brga888 colour
195 * \param[in] ptRGB a __arm_2d_color_fast_rgb_t object
196 */
197ARM_NONNULL(2)
198__STATIC_INLINE void __arm_2d_ccca8888_unpack( uint32_t wColor,
200{
201 assert(NULL != ptRGB);
202
203 uint8_t *pchChannel = (uint8_t *)&wColor;
204
205 ptRGB->B = (uint16_t) pchChannel[0];
206 ptRGB->G = (uint16_t) pchChannel[1];
207 ptRGB->R = (uint16_t) pchChannel[2];
208 ptRGB->A = (uint16_t) pchChannel[3];
209}
210
211
212/*!
213 * \brief generate a gray8 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 uint8_t a gray8 colour
216 */
217ARM_NONNULL(1)
218__STATIC_INLINE uint8_t __arm_2d_gray8_pack(__arm_2d_color_fast_rgb_t * ptRGB)
219{
220 assert(NULL != ptRGB);
221
222 uint16_t tGrayScale = (ptRGB->R * 77 + ptRGB->G * 151 + ptRGB->B * 28) >> 8;
223
224 return (uint8_t)( (tGrayScale <= 255) * tGrayScale
225 + (tGrayScale > 255) * 255);
226}
227
228
229/*!
230 * \brief generate a rgb565 colour from a __arm_2d_color_fast_rgb_t object
231 * \param[in] ptRGB the target __arm_2d_color_fast_rgb_t object
232 * \return uint16_t a rgb565 colour
233 */
234ARM_NONNULL(1)
235__STATIC_INLINE uint16_t __arm_2d_rgb565_pack(__arm_2d_color_fast_rgb_t * ptRGB)
236{
237 assert(NULL != ptRGB);
238
239 arm_2d_color_rgb565_t tOutput = {
240 .u5B = (uint16_t) (ptRGB->B >> 3),
241 .u6G = (uint16_t) (ptRGB->G >> 2),
242 .u5R = (uint16_t) (ptRGB->R >> 3),
243 };
244 return tOutput.tValue;
245}
246
247/*!
248 * \brief generate a cccn888 colour from a __arm_2d_color_fast_rgb_t object
249 * \param[in] ptRGB the target __arm_2d_color_fast_rgb_t object
250 * \return uint32_t a cccn888 colour
251 * \note the alpha channel will be kept in the output value
252 */
253ARM_NONNULL(1)
254__STATIC_INLINE uint32_t __arm_2d_ccca888_pack(__arm_2d_color_fast_rgb_t * ptRGB)
255{
256 assert(NULL != ptRGB);
257
258 arm_2d_color_bgra8888_t tOutput = {
259 .u8B = (uint16_t) ptRGB->B,
260 .u8G = (uint16_t) ptRGB->G,
261 .u8R = (uint16_t) ptRGB->R,
262 .u8A = (uint16_t) ptRGB->A,
263 };
264 return tOutput.tValue;
265}
266
267
268/*----------------------------------------------------------------------------*
269 * Colour Conversion *
270 *----------------------------------------------------------------------------*/
271
272/*!
273 * \brief convert the colour format of a given tile to gray8
274 * \param[in] ptOP the control block, NULL means using the default control block
275 * \param[in] ptSource the source tile
276 * \param[in] ptTarget the output tile (holding a buffer)
277 * \return arm_fsm_rt_t the operation result
278 */
279extern
280ARM_NONNULL(2,3)
282 const arm_2d_tile_t *ptSource,
283 const arm_2d_tile_t *ptTarget);
284
285/*!
286 * \brief copy a given tile to a gray8 target tile
287 * \param[in] ptOP the control block, NULL means using the default control block
288 * \param[in] ptSource the source tile
289 * \param[in] ptTarget the output tile (holding a buffer)
290 * \param[in] ptRegion the target region, NULL means using the region of the
291 * target tile.
292 * \return arm_fsm_rt_t the operation result
293 */
294extern
295ARM_NONNULL(2,3)
297 const arm_2d_tile_t *ptSource,
298 const arm_2d_tile_t *ptTarget,
299 const arm_2d_region_t *ptRegion);
300
301/*!
302 * \brief convert the colour format of a given tile to rgb565
303 * \param[in] ptOP the control block, NULL means using the default control block
304 * \param[in] ptSource the source tile
305 * \param[in] ptTarget the output tile (holding a buffer)
306 * \return arm_fsm_rt_t the operation result
307 */
308extern
309ARM_NONNULL(2,3)
311 const arm_2d_tile_t *ptSource,
312 const arm_2d_tile_t *ptTarget);
313
314/*!
315 * \brief copy a given tile to a rgb565 target tile
316 * \param[in] ptOP the control block, NULL means using the default control block
317 * \param[in] ptSource the source tile
318 * \param[in] ptTarget the output tile (holding a buffer)
319 * \param[in] ptRegion the target region, NULL means using the region of the
320 * target tile.
321 * \return arm_fsm_rt_t the operation result
322 */
323extern
324ARM_NONNULL(2,3)
326 const arm_2d_tile_t *ptSource,
327 const arm_2d_tile_t *ptTarget,
328 const arm_2d_region_t *ptRegion);
329
330/*!
331 * \brief convert the colour format of a given tile to cccn888
332 * \param[in] ptOP the control block, NULL means using the default control block
333 * \param[in] ptSource the source tile
334 * \param[in] ptTarget the output tile (holding a buffer)
335 * \return arm_fsm_rt_t the operation result
336 */
337extern
338ARM_NONNULL(2,3)
340 const arm_2d_tile_t *ptSource,
341 const arm_2d_tile_t *ptTarget);
342
343/*!
344 * \brief copy a given tile to a cccn888 target tile
345 * \param[in] ptOP the control block, NULL means using the default control block
346 * \param[in] ptSource the source tile
347 * \param[in] ptTarget the output tile (holding a buffer)
348 * \param[in] ptRegion the target region, NULL means using the region of the
349 * target tile.
350 * \return arm_fsm_rt_t the operation result
351 */
352extern
353ARM_NONNULL(2,3)
355 const arm_2d_tile_t *ptSource,
356 const arm_2d_tile_t *ptTarget,
357 const arm_2d_region_t *ptRegion);
358
359/*! @} */
360
361#if defined(__clang__)
362# pragma clang diagnostic pop
363#elif defined(__IS_COMPILER_IAR__)
364# pragma diag_warning=Go029
365#endif
366
367#ifdef __cplusplus
368}
369#endif
370
371#endif