30 #ifndef PT_GFX_ARGB32IMAGE_H
31 #define PT_GFX_ARGB32IMAGE_H
33 #include <Pt/Gfx/Api.h>
34 #include <Pt/Gfx/Color.h>
35 #include <Pt/Gfx/CompositionMode.h>
36 #include <Pt/Gfx/BasicImage.h>
52 static std::size_t imageSize(
const Size& size, Pt::ssize_t padding)
54 std::size_t l = (size.
width() * 4) + padding;
55 std::size_t n = l * size.
height();
59 static Pt::ssize_t pixelStride()
79 return Color(a, r, g, b);
82 static void fromColor(
Pt::uint8_t* p,
const Color& c)
97 to[0] = (
Pt::uint8_t) ( (alphaSrc * from[0] + alphaInv * to[0]) >> 8 );
98 to[1] = (
Pt::uint8_t) ( (alphaSrc * from[1] + alphaInv * to[1]) >> 8 );
99 to[2] = (
Pt::uint8_t) ( (alphaSrc * from[2] + alphaInv * to[2]) >> 8 );
100 to[3] = (
Pt::uint8_t) ( (alphaSrc * alphaSrc + alphaInv * to[3]) >> 8 );
103 static void sourceOver(
Pt::uint8_t* to,
const Pt::Gfx::Color& from)
109 to[0] = (
Pt::uint8_t) ( (alphaSrc * (from.blue () >> 8) + alphaInv * to[0]) >> 8 );
110 to[1] = (
Pt::uint8_t) ( (alphaSrc * (from.green() >> 8) + alphaInv * to[1]) >> 8 );
111 to[2] = (
Pt::uint8_t) ( (alphaSrc * (from.red () >> 8) + alphaInv * to[2]) >> 8 );
112 to[3] = (
Pt::uint8_t) ( (alphaSrc * alpha + alphaInv * to[3]) >> 8 );
115 static void assign(
Pt::uint8_t* to,
const Color& c,
116 CompositionMode mode)
120 case CompositionMode::SourceCopy:
121 Argb32Model::fromColor(to, c);
124 case CompositionMode::SourceOver:
125 Argb32Model::sourceOver(to, c);
131 CompositionMode mode)
135 case CompositionMode::SourceCopy:
139 case CompositionMode::SourceOver:
140 Argb32Model::sourceOver(to, from);
145 static void assign(
Pt::uint8_t* to,
const Color& c,
150 case CompositionMode::SourceCopy:
154 to[0] = (blendAlphaSrc * (c.blue () >> 8) + blendAlphaInv * to[0]) >> 8;
155 to[1] = (blendAlphaSrc * (c.green() >> 8) + blendAlphaInv * to[1]) >> 8;
156 to[2] = (blendAlphaSrc * (c.red () >> 8) + blendAlphaInv * to[2]) >> 8;
157 to[3] = (blendAlphaSrc * (c.alpha() >> 8) + blendAlphaInv * to[3]) >> 8;
161 case CompositionMode::SourceOver:
164 const Pt::uint32_t blendAlphaSrc = colorAlpha * blendingAlpha / 255;
166 to[0] = (blendAlphaSrc * (c.blue () >> 8) + blendAlphaInv * to[0]) >> 8;
167 to[1] = (blendAlphaSrc * (c.green() >> 8) + blendAlphaInv * to[1]) >> 8;
168 to[2] = (blendAlphaSrc * (c.red () >> 8) + blendAlphaInv * to[2]) >> 8;
169 to[3] = (blendAlphaSrc * colorAlpha + blendAlphaInv * to[3]) >> 8;
180 case CompositionMode::SourceCopy:
184 to[0] = (blendAlphaSrc * from[0] + blendAlphaInv * to[0]) >> 8;
185 to[1] = (blendAlphaSrc * from[1] + blendAlphaInv * to[1]) >> 8;
186 to[2] = (blendAlphaSrc * from[2] + blendAlphaInv * to[2]) >> 8;
187 to[3] = (blendAlphaSrc * from[3] + blendAlphaInv * to[3]) >> 8;
191 case CompositionMode::SourceOver:
194 const Pt::uint32_t blendAlphaSrc = colorAlpha * blendingAlpha / 255;
196 to[0] = (blendAlphaSrc * from[0] + blendAlphaInv * to[0]) >> 8;
197 to[1] = (blendAlphaSrc * from[1] + blendAlphaInv * to[1]) >> 8;
198 to[2] = (blendAlphaSrc * from[2] + blendAlphaInv * to[2]) >> 8;
199 to[3] = (blendAlphaSrc * colorAlpha + blendAlphaInv * to[3]) >> 8;
205 static void assign(
Pt::uint8_t* to,
const Color& c,
size_t length,
206 CompositionMode mode);
209 CompositionMode mode);
211 template <
typename T>
212 static void advance(T*& p, Pt::ssize_t& xpos, Pt::ssize_t& ypos,
213 const BasicView<Argb32Model>& view)
215 if( ++xpos >= view.width() )
226 template <
typename T>
227 static void advance(T*& p, Pt::ssize_t n, Pt::ssize_t& xpos, Pt::ssize_t& ypos,
228 const BasicView<Argb32Model>& view, T* data)
230 Pt::ssize_t off = xpos + n;
231 ypos += off / view.width();
232 xpos = off % view.width();
234 p = data + view.stride() * ypos + xpos * 4;
246 ConstPixel(
const BasicView<Argb32Model>& view, Pt::ssize_t xpos, Pt::ssize_t ypos)
252 reset(view, xpos, ypos);
262 void reset(
const BasicView<Argb32Model>& view, Pt::ssize_t xpos, Pt::ssize_t ypos)
268 Pt::ssize_t off = view.stride() * ypos + (xpos * 4);
269 _p = view.data() + off;
283 Argb32Model::advance(_p, _xpos, _ypos, *_view);
286 void advance( Pt::ssize_t n )
288 Argb32Model::advance(_p, n, _xpos, _ypos, *_view, _view->data());
291 Color toColor()
const
293 return Argb32Model::toColor(_p);
305 return (*val & 0x00FF0000) >> 16;
311 return (*val & 0x0000FF00) >> 8;
317 return *val & 0x000000FF;
334 const BasicView<Argb32Model>* _view;
346 Pixel(BasicView<Argb32Model>& view, Pt::ssize_t xpos, Pt::ssize_t ypos)
352 reset(view, xpos, ypos);
364 assign(p, CompositionMode::SourceCopy);
370 assign(p, CompositionMode::SourceCopy);
374 Pixel& operator=(
const Color& color)
376 assign(color, CompositionMode::SourceCopy);
380 void reset(BasicView<Argb32Model>& view, Pt::ssize_t xpos, Pt::ssize_t ypos)
386 Pt::ssize_t off = view.stride() * ypos + (xpos * 4);
387 _p = view.data() + off;
390 void reset(
const Pixel& p)
401 Argb32Model::advance(_p, _xpos, _ypos, *_view);
404 void advance( Pt::ssize_t n )
406 Argb32Model::advance(_p, n, _xpos, _ypos, *_view, _view->data());
409 void assign(
const Color& c, CompositionMode mode)
411 Argb32Model::assign(_p, c, mode);
414 void assign(
const Pixel& p, CompositionMode mode)
416 Argb32Model::assign(_p, p._p, mode);
419 void assign(
const ConstPixel& p, CompositionMode mode)
421 Argb32Model::assign(_p, p._p, mode);
424 Color toColor()
const
426 return Argb32Model::toColor(_p);
438 return (*val & 0x00FF0000) >> 16;
444 return (*val & 0x0000FF00) >> 8;
450 return *val & 0x000000FF;
456 *val = (*val & 0x00FFFFFF) | (
uint32_t(a) << 24);
462 *val = (*val & 0xFF00FFFF) | (
uint32_t(r) << 16);
468 *val = (*val & 0xFFFF00FF) | (
uint32_t(g) << 8);
474 *val = (*val & 0xFFFFFF00) |
uint32_t(b);
477 bool operator!=(
const Pixel& p)
const
478 {
return _p != p._p; }
480 bool operator==(
const Pixel& p)
const
481 {
return _p == p._p; }
484 BasicView<Argb32Model>* _view;
499 : BasicImage(size, padding)
505 : BasicImage(data, size, padding)
uint_type uint16_t
Unsigned 16-bit integer type.
Definition: Types.h:30
T width() const
Returns the width.
Definition: Size.h:65
ARGB-32 image model.
Definition: Argb32Image.h:46
Const pixel in a ARGB-32 Image.
Definition: Argb32Image.h:241
T height() const
Returns the height.
Definition: Size.h:69
Const pixel in a ARGB-32 Image.
Definition: Argb32Image.h:343
Argb32Image(const Size &size, size_t padding=0)
Constructor.
Definition: Argb32Image.h:498
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Types.h:18
ARGB-32 image.
Definition: Argb32Image.h:493
Argb32Image(Pt::uint8_t *data, const Size &size, size_t padding=0)
Construct from external buffer.
Definition: Argb32Image.h:504