LMMS
Loading...
Searching...
No Matches
juce_PixelFormats.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29//==============================================================================
30#if JUCE_MSVC
31 #pragma pack (push, 1)
32#endif
33
34class PixelRGB;
35class PixelAlpha;
36
38{
39 return (x >> 8) & 0x00ff00ff;
40}
41
43{
44 return (x | (0x01000100 - maskPixelComponents (x))) & 0x00ff00ff;
45}
46
47//==============================================================================
59{
60public:
62 PixelARGB() noexcept = default;
63
65 {
66 components.b = b;
67 components.g = g;
68 components.r = r;
69 components.a = a;
70 }
71
72 //==============================================================================
75
79 {
80 #if JUCE_ANDROID
81 return (uint32) ((components.a << 24) | (components.r << 16) | (components.g << 8) | (components.b << 0));
82 #else
83 return getNativeARGB();
84 #endif
85 }
86
90 {
91 #if JUCE_BIG_ENDIAN
92 return getInARGBMaskOrder();
93 #else
94 return (uint32) ((components.b << 24) | (components.g << 16) | (components.r << 8) | components.a);
95 #endif
96 }
97
101
104 forcedinline uint32 getOddBytes() const noexcept { return 0x00ff00ff & (internal >> 8); }
105
106 //==============================================================================
111
112 //==============================================================================
117 template <class Pixel>
118 forcedinline void set (const Pixel& src) noexcept
119 {
120 internal = src.getNativeARGB();
121 }
122
123 //==============================================================================
125 void setARGB (uint8 a, uint8 r, uint8 g, uint8 b) noexcept
126 {
127 components.b = b;
128 components.g = g;
129 components.r = r;
130 components.a = a;
131 }
132
133 //==============================================================================
139 template <class Pixel>
140 forcedinline void blend (const Pixel& src) noexcept
141 {
142 auto rb = src.getEvenBytes();
143 auto ag = src.getOddBytes();
144
145 const auto alpha = 0x100 - (ag >> 16);
146
147 rb += maskPixelComponents (getEvenBytes() * alpha);
148 ag += maskPixelComponents (getOddBytes() * alpha);
149
151 }
152
158 forcedinline void blend (PixelRGB src) noexcept;
159
160
166 template <class Pixel>
167 forcedinline void blend (const Pixel& src, uint32 extraAlpha) noexcept
168 {
169 auto rb = maskPixelComponents (extraAlpha * src.getEvenBytes());
170 auto ag = maskPixelComponents (extraAlpha * src.getOddBytes());
171
172 const auto alpha = 0x100 - (ag >> 16);
173
174 rb += maskPixelComponents (getEvenBytes() * alpha);
175 ag += maskPixelComponents (getOddBytes() * alpha);
176
178 }
179
183 template <class Pixel>
184 forcedinline void tween (const Pixel& src, uint32 amount) noexcept
185 {
186 auto dEvenBytes = getEvenBytes();
187 dEvenBytes += (((src.getEvenBytes() - dEvenBytes) * amount) >> 8);
188 dEvenBytes &= 0x00ff00ff;
189
190 auto dOddBytes = getOddBytes();
191 dOddBytes += (((src.getOddBytes() - dOddBytes) * amount) >> 8);
192 dOddBytes &= 0x00ff00ff;
193 dOddBytes <<= 8;
194
195 dOddBytes |= dEvenBytes;
196 internal = dOddBytes;
197 }
198
199 //==============================================================================
201 forcedinline void setAlpha (uint8 newAlpha) noexcept
202 {
203 components.a = newAlpha;
204 }
205
207 forcedinline void multiplyAlpha (int multiplier) noexcept
208 {
209 // increment alpha by 1, so that if multiplier == 255 (full alpha),
210 // this function will not change the values.
211 ++multiplier;
212
213 internal = ((((uint32) multiplier) * getOddBytes()) & 0xff00ff00)
214 | (((((uint32) multiplier) * getEvenBytes()) >> 8) & 0x00ff00ff);
215 }
216
217 forcedinline void multiplyAlpha (float multiplier) noexcept
218 {
219 multiplyAlpha ((int) (multiplier * 255.0f));
220 }
221
222
224 {
226 p.unpremultiply();
227 return p;
228 }
229
232 {
233 const auto alpha = components.a;
234
235 if (alpha < 0xff)
236 {
237 if (alpha == 0)
238 {
239 components.b = 0;
240 components.g = 0;
241 components.r = 0;
242 }
243 else
244 {
245 components.b = (uint8) ((components.b * alpha + 0x7f) >> 8);
246 components.g = (uint8) ((components.g * alpha + 0x7f) >> 8);
247 components.r = (uint8) ((components.r * alpha + 0x7f) >> 8);
248 }
249 }
250 }
251
254 {
255 const auto alpha = components.a;
256
257 if (alpha < 0xff)
258 {
259 if (alpha == 0)
260 {
261 components.b = 0;
262 components.g = 0;
263 components.r = 0;
264 }
265 else
266 {
267 components.b = (uint8) jmin ((uint32) 0xffu, (components.b * 0xffu) / alpha);
268 components.g = (uint8) jmin ((uint32) 0xffu, (components.g * 0xffu) / alpha);
269 components.r = (uint8) jmin ((uint32) 0xffu, (components.r * 0xffu) / alpha);
270 }
271 }
272 }
273
275 {
276 if (components.a < 0xff && components.a > 0)
277 {
278 const auto newUnpremultipliedLevel = (0xff * ((int) components.r + (int) components.g + (int) components.b) / (3 * components.a));
279
281 = (uint8) ((newUnpremultipliedLevel * components.a + 0x7f) >> 8);
282 }
283 else
284 {
286 = (uint8) (((int) components.r + (int) components.g + (int) components.b) / 3);
287 }
288 }
289
290 //==============================================================================
292 #if JUCE_ANDROID
293 #if JUCE_BIG_ENDIAN
294 enum { indexA = 0, indexR = 3, indexG = 2, indexB = 1 };
295 #else
296 enum { indexA = 3, indexR = 0, indexG = 1, indexB = 2 };
297 #endif
298 #else
299 #if JUCE_BIG_ENDIAN
300 enum { indexA = 0, indexR = 1, indexG = 2, indexB = 3 };
301 #else
302 enum { indexA = 3, indexR = 2, indexG = 1, indexB = 0 };
303 #endif
304 #endif
305
306private:
307 //==============================================================================
308 PixelARGB (uint32 internalValue) noexcept
309 : internal (internalValue)
310 {
311 }
312
313 //==============================================================================
315 {
316 #if JUCE_ANDROID
317 #if JUCE_BIG_ENDIAN
318 uint8 a, b, g, r;
319 #else
320 uint8 r, g, b, a;
321 #endif
322 #else
323 #if JUCE_BIG_ENDIAN
324 uint8 a, r, g, b;
325 #else
326 uint8 b, g, r, a;
327 #endif
328 #endif
330
331 union
332 {
335 };
336}
337#ifndef DOXYGEN
339#endif
341
342
343//==============================================================================
354{
355public:
357 PixelRGB() noexcept = default;
358
359 //==============================================================================
365 {
366 #if JUCE_ANDROID
367 return (uint32) ((0xffu << 24) | r | ((uint32) g << 8) | ((uint32) b << 16));
368 #else
369 return (uint32) ((0xffu << 24) | b | ((uint32) g << 8) | ((uint32) r << 16));
370 #endif
371 }
372
376 {
377 #if JUCE_ANDROID
378 return (uint32) ((0xffu << 24) | b | ((uint32) g << 8) | ((uint32) r << 16));
379 #else
380 return getNativeARGB();
381 #endif
382 }
383
387 {
388 #if JUCE_BIG_ENDIAN
389 return getInARGBMaskOrder();
390 #else
391 return (uint32) ((b << 24) | (g << 16) | (r << 8) | 0xff);
392 #endif
393 }
394
401 {
402 #if JUCE_ANDROID
403 return (uint32) (r | (b << 16));
404 #else
405 return (uint32) (b | (r << 16));
406 #endif
407 }
408
415
416 //==============================================================================
421
422 //==============================================================================
429 template <class Pixel>
430 forcedinline void set (const Pixel& src) noexcept
431 {
432 b = src.getBlue();
433 g = src.getGreen();
434 r = src.getRed();
435 }
436
438 void setARGB (uint8, uint8 red, uint8 green, uint8 blue) noexcept
439 {
440 r = red;
441 g = green;
442 b = blue;
443 }
444
445 //==============================================================================
451 template <class Pixel>
452 forcedinline void blend (const Pixel& src) noexcept
453 {
454 const auto alpha = (uint32) (0x100 - src.getAlpha());
455
456 // getEvenBytes returns 0x00rr00bb on non-android
457 const auto rb = clampPixelComponents (src.getEvenBytes() + maskPixelComponents (getEvenBytes() * alpha));
458 // getOddBytes returns 0x00aa00gg on non-android
459 const auto ag = clampPixelComponents (src.getOddBytes() + ((g * alpha) >> 8));
460
461 g = (uint8) (ag & 0xff);
462
463 #if JUCE_ANDROID
464 b = (uint8) (rb >> 16);
465 r = (uint8) (rb & 0xff);
466 #else
467 r = (uint8) (rb >> 16);
468 b = (uint8) (rb & 0xff);
469 #endif
470 }
471
472 forcedinline void blend (PixelRGB src) noexcept
473 {
474 set (src);
475 }
476
482 template <class Pixel>
483 forcedinline void blend (const Pixel& src, uint32 extraAlpha) noexcept
484 {
485 auto ag = maskPixelComponents (extraAlpha * src.getOddBytes());
486 auto rb = maskPixelComponents (extraAlpha * src.getEvenBytes());
487
488 const auto alpha = 0x100 - (ag >> 16);
489
490 ag = clampPixelComponents (ag + (g * alpha >> 8));
492
493 g = (uint8) (ag & 0xff);
494
495 #if JUCE_ANDROID
496 b = (uint8) (rb >> 16);
497 r = (uint8) (rb & 0xff);
498 #else
499 r = (uint8) (rb >> 16);
500 b = (uint8) (rb & 0xff);
501 #endif
502 }
503
507 template <class Pixel>
508 forcedinline void tween (const Pixel& src, uint32 amount) noexcept
509 {
510 auto dEvenBytes = getEvenBytes();
511 dEvenBytes += (((src.getEvenBytes() - dEvenBytes) * amount) >> 8);
512
513 auto dOddBytes = getOddBytes();
514 dOddBytes += (((src.getOddBytes() - dOddBytes) * amount) >> 8);
515
516 g = (uint8) (dOddBytes & 0xff); // dOddBytes = 0x00aa00gg
517
518 #if JUCE_ANDROID
519 r = (uint8) (dEvenBytes & 0xff); // dEvenBytes = 0x00bb00rr
520 b = (uint8) (dEvenBytes >> 16);
521 #else
522 b = (uint8) (dEvenBytes & 0xff); // dEvenBytes = 0x00rr00bb
523 r = (uint8) (dEvenBytes >> 16);
524 #endif
525 }
526
527 //==============================================================================
529 forcedinline void setAlpha (uint8) noexcept {}
530
532 forcedinline void multiplyAlpha (int) noexcept {}
533
535 forcedinline void multiplyAlpha (float) noexcept {}
536
539
542
544 {
545 r = g = b = (uint8) (((int) r + (int) g + (int) b) / 3);
546 }
547
548 //==============================================================================
550 #if JUCE_MAC
551 enum { indexR = 0, indexG = 1, indexB = 2 };
552 #else
553 enum { indexR = 2, indexG = 1, indexB = 0 };
554 #endif
555
556private:
557 //==============================================================================
559 {
560 #if JUCE_ANDROID
561 b = (uint8) (internal >> 16);
562 g = (uint8) (internal >> 8);
563 r = (uint8) (internal);
564 #else
565 r = (uint8) (internal >> 16);
566 g = (uint8) (internal >> 8);
567 b = (uint8) (internal);
568 #endif
569 }
570
571 //==============================================================================
572 #if JUCE_MAC
573 uint8 r, g, b;
574 #else
576 #endif
577
578}
579#ifndef DOXYGEN
581#endif
582;
583
585{
586 set (src);
587}
588
589//==============================================================================
600{
601public:
603 PixelAlpha() noexcept = default;
604
605 //==============================================================================
610 forcedinline uint32 getNativeARGB() const noexcept { return (uint32) ((a << 24) | (a << 16) | (a << 8) | a); }
611
615
619
626
632 forcedinline uint32 getOddBytes() const noexcept { return (uint32) ((a << 16) | a); }
633
634 //==============================================================================
639
640 //==============================================================================
645 template <class Pixel>
646 forcedinline void set (const Pixel& src) noexcept
647 {
648 a = src.getAlpha();
649 }
650
653 {
654 a = a_;
655 }
656
657 //==============================================================================
663 template <class Pixel>
664 forcedinline void blend (const Pixel& src) noexcept
665 {
666 const auto srcA = src.getAlpha();
667 a = (uint8) ((a * (0x100 - srcA) >> 8) + srcA);
668 }
669
675 template <class Pixel>
676 forcedinline void blend (const Pixel& src, uint32 extraAlpha) noexcept
677 {
678 ++extraAlpha;
679 const auto srcAlpha = (int) ((extraAlpha * src.getAlpha()) >> 8);
680 a = (uint8) ((a * (0x100 - srcAlpha) >> 8) + srcAlpha);
681 }
682
686 template <class Pixel>
687 forcedinline void tween (const Pixel& src, uint32 amount) noexcept
688 {
689 a += ((src.getAlpha() - a) * amount) >> 8;
690 }
691
692 //==============================================================================
694 forcedinline void setAlpha (uint8 newAlpha) noexcept
695 {
696 a = newAlpha;
697 }
698
700 forcedinline void multiplyAlpha (int multiplier) noexcept
701 {
702 ++multiplier;
703 a = (uint8) ((a * multiplier) >> 8);
704 }
705
706 forcedinline void multiplyAlpha (float multiplier) noexcept
707 {
708 a = (uint8) (a * multiplier);
709 }
710
713
716
718
719 //==============================================================================
721 enum { indexA = 0 };
722
723private:
724 //==============================================================================
727
728 //==============================================================================
730}
731#ifndef DOXYGEN
733#endif
734;
735
736#if JUCE_MSVC
737 #pragma pack (pop)
738#endif
739
740} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
uint8_t a
Definition Spc_Cpu.h:141
forcedinline void tween(const Pixel &src, uint32 amount) noexcept
Definition juce_PixelFormats.h:184
forcedinline void set(const Pixel &src) noexcept
Definition juce_PixelFormats.h:118
PixelARGB() noexcept=default
uint32 getInARGBMemoryOrder() const noexcept
Definition juce_PixelFormats.h:89
PixelARGB(uint32 internalValue) noexcept
Definition juce_PixelFormats.h:308
forcedinline uint8 getGreen() const noexcept
Definition juce_PixelFormats.h:109
forcedinline uint32 getNativeARGB() const noexcept
Definition juce_PixelFormats.h:74
forcedinline void setAlpha(uint8 newAlpha) noexcept
Definition juce_PixelFormats.h:201
forcedinline void blend(const Pixel &src) noexcept
Definition juce_PixelFormats.h:140
forcedinline void desaturate() noexcept
Definition juce_PixelFormats.h:274
forcedinline void unpremultiply() noexcept
Definition juce_PixelFormats.h:253
forcedinline void multiplyAlpha(float multiplier) noexcept
Definition juce_PixelFormats.h:217
forcedinline uint32 getInARGBMaskOrder() const noexcept
Definition juce_PixelFormats.h:78
forcedinline uint8 getAlpha() const noexcept
Definition juce_PixelFormats.h:107
uint32 internal
Definition juce_PixelFormats.h:333
forcedinline void blend(const Pixel &src, uint32 extraAlpha) noexcept
Definition juce_PixelFormats.h:167
Components components
Definition juce_PixelFormats.h:334
void setARGB(uint8 a, uint8 r, uint8 g, uint8 b) noexcept
Definition juce_PixelFormats.h:125
forcedinline void premultiply() noexcept
Definition juce_PixelFormats.h:231
forcedinline uint32 getEvenBytes() const noexcept
Definition juce_PixelFormats.h:100
@ indexG
Definition juce_PixelFormats.h:302
@ indexA
Definition juce_PixelFormats.h:302
@ indexR
Definition juce_PixelFormats.h:302
@ indexB
Definition juce_PixelFormats.h:302
PixelARGB getUnpremultiplied() const noexcept
Definition juce_PixelFormats.h:223
forcedinline uint8 getRed() const noexcept
Definition juce_PixelFormats.h:108
forcedinline void multiplyAlpha(int multiplier) noexcept
Definition juce_PixelFormats.h:207
forcedinline uint8 getBlue() const noexcept
Definition juce_PixelFormats.h:110
forcedinline uint32 getOddBytes() const noexcept
Definition juce_PixelFormats.h:104
uint8 a
Definition juce_PixelFormats.h:729
PixelAlpha(uint32 internal) noexcept
Definition juce_PixelFormats.h:725
forcedinline void setARGB(uint8 a_, uint8, uint8, uint8) noexcept
Definition juce_PixelFormats.h:652
uint32 getInARGBMemoryOrder() const noexcept
Definition juce_PixelFormats.h:618
forcedinline void desaturate() noexcept
Definition juce_PixelFormats.h:717
forcedinline void tween(const Pixel &src, uint32 amount) noexcept
Definition juce_PixelFormats.h:687
forcedinline void multiplyAlpha(int multiplier) noexcept
Definition juce_PixelFormats.h:700
forcedinline uint32 getEvenBytes() const noexcept
Definition juce_PixelFormats.h:625
forcedinline void setAlpha(uint8 newAlpha) noexcept
Definition juce_PixelFormats.h:694
forcedinline uint8 getBlue() const noexcept
Definition juce_PixelFormats.h:638
forcedinline void premultiply() noexcept
Definition juce_PixelFormats.h:712
@ indexA
Definition juce_PixelFormats.h:721
forcedinline uint8 getRed() const noexcept
Definition juce_PixelFormats.h:636
forcedinline void blend(const Pixel &src) noexcept
Definition juce_PixelFormats.h:664
forcedinline uint8 getAlpha() const noexcept
Definition juce_PixelFormats.h:635
forcedinline uint32 getInARGBMaskOrder() const noexcept
Definition juce_PixelFormats.h:614
forcedinline void multiplyAlpha(float multiplier) noexcept
Definition juce_PixelFormats.h:706
forcedinline void set(const Pixel &src) noexcept
Definition juce_PixelFormats.h:646
forcedinline uint32 getOddBytes() const noexcept
Definition juce_PixelFormats.h:632
forcedinline void unpremultiply() noexcept
Definition juce_PixelFormats.h:715
PixelAlpha() noexcept=default
forcedinline void blend(const Pixel &src, uint32 extraAlpha) noexcept
Definition juce_PixelFormats.h:676
forcedinline uint8 getGreen() const noexcept
Definition juce_PixelFormats.h:637
forcedinline uint32 getNativeARGB() const noexcept
Definition juce_PixelFormats.h:610
Definition juce_PixelFormats.h:354
PixelRGB(uint32 internal) noexcept
Definition juce_PixelFormats.h:558
forcedinline uint8 getBlue() const noexcept
Definition juce_PixelFormats.h:420
forcedinline void premultiply() noexcept
Definition juce_PixelFormats.h:538
forcedinline void setAlpha(uint8) noexcept
Definition juce_PixelFormats.h:529
forcedinline uint32 getInARGBMaskOrder() const noexcept
Definition juce_PixelFormats.h:375
forcedinline uint8 getGreen() const noexcept
Definition juce_PixelFormats.h:419
forcedinline uint32 getOddBytes() const noexcept
Definition juce_PixelFormats.h:414
void setARGB(uint8, uint8 red, uint8 green, uint8 blue) noexcept
Definition juce_PixelFormats.h:438
uint8 g
Definition juce_PixelFormats.h:575
forcedinline void set(const Pixel &src) noexcept
Definition juce_PixelFormats.h:430
forcedinline void blend(const Pixel &src, uint32 extraAlpha) noexcept
Definition juce_PixelFormats.h:483
uint8 r
Definition juce_PixelFormats.h:575
forcedinline void blend(const Pixel &src) noexcept
Definition juce_PixelFormats.h:452
forcedinline void blend(PixelRGB src) noexcept
Definition juce_PixelFormats.h:472
forcedinline void desaturate() noexcept
Definition juce_PixelFormats.h:543
forcedinline void multiplyAlpha(int) noexcept
Definition juce_PixelFormats.h:532
uint32 getInARGBMemoryOrder() const noexcept
Definition juce_PixelFormats.h:386
@ indexG
Definition juce_PixelFormats.h:553
@ indexR
Definition juce_PixelFormats.h:553
@ indexB
Definition juce_PixelFormats.h:553
uint8 b
Definition juce_PixelFormats.h:575
forcedinline uint8 getRed() const noexcept
Definition juce_PixelFormats.h:418
forcedinline uint8 getAlpha() const noexcept
Definition juce_PixelFormats.h:417
forcedinline uint32 getEvenBytes() const noexcept
Definition juce_PixelFormats.h:400
forcedinline void tween(const Pixel &src, uint32 amount) noexcept
Definition juce_PixelFormats.h:508
PixelRGB() noexcept=default
forcedinline void unpremultiply() noexcept
Definition juce_PixelFormats.h:541
forcedinline void multiplyAlpha(float) noexcept
Definition juce_PixelFormats.h:535
forcedinline uint32 getNativeARGB() const noexcept
Definition juce_PixelFormats.h:364
int g
Definition inflate.c:1573
unsigned x[BMAX+1]
Definition inflate.c:1586
#define forcedinline
#define JUCE_PACKED
#define JUCE_API
Definition juce_StandardHeader.h:152
Definition juce_CharacterFunctions.h:68
Definition carla_juce.cpp:31
constexpr Type jmin(Type a, Type b)
Definition juce_MathsFunctions.h:106
class JUCE_API juce::PixelARGB JUCE_PACKED
unsigned int uint32
Definition juce_MathsFunctions.h:45
uint32 clampPixelComponents(uint32 x) noexcept
Definition juce_PixelFormats.h:42
uint32 maskPixelComponents(uint32 x) noexcept
Definition juce_PixelFormats.h:37
unsigned char uint8
Definition juce_MathsFunctions.h:37
png_structrp int png_fixed_point red
Definition png.h:1083
Definition juce_PixelFormats.h:315
uint8 r
Definition juce_PixelFormats.h:326
uint8 b
Definition juce_PixelFormats.h:326
uint8 g
Definition juce_PixelFormats.h:326
uint8 a
Definition juce_PixelFormats.h:326
uch * p
Definition crypt.c:594
int r
Definition crypt.c:458
b
Definition crypt.c:628
typedef int(UZ_EXP MsgFn)()
#define const
Definition zconf.h:137