1// [Blend2D]
2// 2D Vector Graphics Powered by a JIT Compiler.
3//
4// [License]
5// Zlib - See LICENSE.md file in the package.
6
7#ifndef BLEND2D_BLPIXELCONVERTER_H
8#define BLEND2D_BLPIXELCONVERTER_H
9
10#include "./blapi.h"
11#include "./blformat.h"
12#include "./blgeometry.h"
13
14//! \addtogroup blend2d_api_imaging
15//! \{
16
17// ============================================================================
18// [Typedefs]
19// ============================================================================
20
21//! \cond INTERNAL
22//! \ingroup blend2d_internal
23//! Pixel converter function.
24typedef BLResult (BL_CDECL* BLPixelConverterFunc)(
25 const BLPixelConverterCore* self,
26 uint8_t* dstData, intptr_t dstStride,
27 const uint8_t* srcData, intptr_t srcStride,
28 uint32_t w, uint32_t h, const BLPixelConverterOptions* options) BL_NOEXCEPT;
29//! \endcond
30
31// ============================================================================
32// [BLPixelConverter - Options]
33// ============================================================================
34
35//! Pixel conversion options.
36struct BLPixelConverterOptions {
37 BLPointI origin;
38 size_t gap;
39};
40
41// ============================================================================
42// [BLPixelConverter - Core]
43// ============================================================================
44
45//! Pixel converter [C Interface - Core].
46struct BLPixelConverterCore {
47 //! Converter function.
48 BLPixelConverterFunc convertFunc;
49
50 union {
51 uint8_t strategy;
52 //! Internal data used by the pixel converter not exposed to users.
53 uint8_t data[64];
54 };
55};
56
57// ============================================================================
58// [BLPixelConverter - C++]
59// ============================================================================
60
61#ifdef __cplusplus
62//! Pixel converter [C++ API].
63//!
64//! Provides interface to convert pixels between various pixel formats. The
65//! primary purpose of this class is to allow efficient conversion between
66//! pixel formats used natively by Blend2D and pixel formats required by I/O.
67class BLPixelConverter : public BLPixelConverterCore {
68public:
69 BL_INLINE BLPixelConverter() noexcept { blPixelConverterInit(this); }
70 BL_INLINE BLPixelConverter(const BLPixelConverter& other) noexcept { blPixelConverterInitWeak(this, &other); }
71 BL_INLINE ~BLPixelConverter() noexcept { blPixelConverterReset(this); }
72
73 BL_INLINE BLPixelConverter& operator=(const BLPixelConverter& other) noexcept {
74 blPixelConverterAssign(this, &other);
75 return *this;
76 }
77
78 //! Returns `true` if the converter is initialized.
79 BL_INLINE bool isInitialized() const noexcept { return strategy != 0; }
80
81 //! Reset the pixel converter.
82 BL_INLINE BLResult reset() noexcept {
83 return blPixelConverterReset(this);
84 }
85
86 BL_INLINE BLResult assign(const BLPixelConverter& other) noexcept {
87 return blPixelConverterAssign(this, &other);
88 }
89
90 BL_INLINE BLResult create(const BLFormatInfo& dstInfo, const BLFormatInfo& srcInfo) noexcept {
91 return blPixelConverterCreate(this, &dstInfo, &srcInfo);
92 }
93
94 //! Convert a single span of pixels of `w` width.
95 BL_INLINE BLResult convertSpan(void* dstData, const void* srcData, uint32_t w,
96 const BLPixelConverterOptions* options = nullptr) const noexcept {
97 return convertFunc(this, static_cast<uint8_t*>(dstData), 0, static_cast<const uint8_t*>(srcData), 0, w, 1, options);
98 }
99
100 //! Convert a rectangular area of pixels from source format to destination.
101 BL_INLINE BLResult convertRect(void* dstData, intptr_t dstStride,
102 const void* srcData, intptr_t srcStride,
103 uint32_t w, uint32_t h, const BLPixelConverterOptions* options = nullptr) const noexcept {
104 return convertFunc(this, static_cast<uint8_t*>(dstData), dstStride, static_cast<const uint8_t*>(srcData), srcStride, w, h, options);
105 }
106};
107#endif
108
109//! \}
110
111#endif // BLEND2D_BLPIXELCONVERTER_H
112