Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

757 lines
27KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_PIXELFORMATS_H_INCLUDED
  18. #define JUCE_PIXELFORMATS_H_INCLUDED
  19. //==============================================================================
  20. #if JUCE_MSVC
  21. #pragma pack (push, 1)
  22. #endif
  23. class PixelRGB;
  24. class PixelAlpha;
  25. inline uint32 maskPixelComponents (uint32 x) noexcept
  26. {
  27. return (x >> 8) & 0x00ff00ff;
  28. }
  29. inline uint32 clampPixelComponents (uint32 x) noexcept
  30. {
  31. return (x | (0x01000100 - maskPixelComponents (x))) & 0x00ff00ff;
  32. }
  33. //==============================================================================
  34. /**
  35. Represents a 32-bit INTERNAL pixel with premultiplied alpha, and can perform compositing
  36. operations with it.
  37. This is used internally by the imaging classes.
  38. @see PixelRGB
  39. */
  40. class JUCE_API PixelARGB
  41. {
  42. public:
  43. /** Creates a pixel without defining its colour. */
  44. PixelARGB() noexcept {}
  45. ~PixelARGB() noexcept {}
  46. PixelARGB (const uint8 a, const uint8 r, const uint8 g, const uint8 b) noexcept
  47. {
  48. components.b = b;
  49. components.g = g;
  50. components.r = r;
  51. components.a = a;
  52. }
  53. //==============================================================================
  54. /** Returns a uint32 which represents the pixel in a platform dependent format. */
  55. forcedinline uint32 getNativeARGB() const noexcept { return internal; }
  56. /** Returns a uint32 which will be in argb order as if constructed with the following mask operation
  57. ((alpha << 24) | (red << 16) | (green << 8) | blue). */
  58. forcedinline uint32 getInARGBMaskOrder() const noexcept
  59. {
  60. #if JUCE_ANDROID
  61. return (uint32) ((components.a << 24) | (components.r << 16) | (components.g << 8) | (components.b << 0));
  62. #else
  63. return getNativeARGB();
  64. #endif
  65. }
  66. /** Returns a uint32 which when written to memory, will be in the order a, r, g, b. In other words,
  67. if the return-value is read as a uint8 array then the elements will be in the order of a, r, g, b*/
  68. inline uint32 getInARGBMemoryOrder() const noexcept
  69. {
  70. #if JUCE_BIG_ENDIAN
  71. return getInARGBMaskOrder();
  72. #else
  73. return (uint32) ((components.b << 24) | (components.g << 16) | (components.r << 8) | components.a);
  74. #endif
  75. }
  76. /** Return channels with an even index and insert zero bytes between them. This is useful for blending
  77. operations. The exact channels which are returned is platform dependent. */
  78. forcedinline uint32 getEvenBytes() const noexcept { return 0x00ff00ff & internal; }
  79. /** Return channels with an odd index and insert zero bytes between them. This is useful for blending
  80. operations. The exact channels which are returned is platform dependent. */
  81. forcedinline uint32 getOddBytes() const noexcept { return 0x00ff00ff & (internal >> 8); }
  82. //==============================================================================
  83. forcedinline uint8 getAlpha() const noexcept { return components.a; }
  84. forcedinline uint8 getRed() const noexcept { return components.r; }
  85. forcedinline uint8 getGreen() const noexcept { return components.g; }
  86. forcedinline uint8 getBlue() const noexcept { return components.b; }
  87. #if JUCE_GCC && ! JUCE_CLANG
  88. // NB these are here as a workaround because GCC refuses to bind to packed values.
  89. forcedinline uint8& getAlpha() noexcept { return comps [indexA]; }
  90. forcedinline uint8& getRed() noexcept { return comps [indexR]; }
  91. forcedinline uint8& getGreen() noexcept { return comps [indexG]; }
  92. forcedinline uint8& getBlue() noexcept { return comps [indexB]; }
  93. #else
  94. forcedinline uint8& getAlpha() noexcept { return components.a; }
  95. forcedinline uint8& getRed() noexcept { return components.r; }
  96. forcedinline uint8& getGreen() noexcept { return components.g; }
  97. forcedinline uint8& getBlue() noexcept { return components.b; }
  98. #endif
  99. //==============================================================================
  100. /** Copies another pixel colour over this one.
  101. This doesn't blend it - this colour is simply replaced by the other one.
  102. */
  103. template <class Pixel>
  104. forcedinline void set (const Pixel& src) noexcept
  105. {
  106. internal = src.getNativeARGB();
  107. }
  108. //==============================================================================
  109. /** Sets the pixel's colour from individual components. */
  110. void setARGB (const uint8 a, const uint8 r, const uint8 g, const uint8 b) noexcept
  111. {
  112. components.b = b;
  113. components.g = g;
  114. components.r = r;
  115. components.a = a;
  116. }
  117. //==============================================================================
  118. /** Blends another pixel onto this one.
  119. This takes into account the opacity of the pixel being overlaid, and blends
  120. it accordingly.
  121. */
  122. template <class Pixel>
  123. forcedinline void blend (const Pixel& src) noexcept
  124. {
  125. uint32 rb = src.getEvenBytes();
  126. uint32 ag = src.getOddBytes();
  127. const uint32 alpha = 0x100 - (ag >> 16);
  128. rb += maskPixelComponents (getEvenBytes() * alpha);
  129. ag += maskPixelComponents (getOddBytes() * alpha);
  130. internal = clampPixelComponents (rb) | (clampPixelComponents (ag) << 8);
  131. }
  132. /** Blends another pixel onto this one.
  133. This takes into account the opacity of the pixel being overlaid, and blends
  134. it accordingly.
  135. */
  136. forcedinline void blend (const PixelRGB src) noexcept;
  137. /** Blends another pixel onto this one, applying an extra multiplier to its opacity.
  138. The opacity of the pixel being overlaid is scaled by the extraAlpha factor before
  139. being used, so this can blend semi-transparently from a PixelRGB argument.
  140. */
  141. template <class Pixel>
  142. forcedinline void blend (const Pixel& src, uint32 extraAlpha) noexcept
  143. {
  144. uint32 rb = maskPixelComponents (extraAlpha * src.getEvenBytes());
  145. uint32 ag = maskPixelComponents (extraAlpha * src.getOddBytes());
  146. const uint32 alpha = 0x100 - (ag >> 16);
  147. rb += maskPixelComponents (getEvenBytes() * alpha);
  148. ag += maskPixelComponents (getOddBytes() * alpha);
  149. internal = clampPixelComponents (rb) | (clampPixelComponents (ag) << 8);
  150. }
  151. /** Blends another pixel with this one, creating a colour that is somewhere
  152. between the two, as specified by the amount.
  153. */
  154. template <class Pixel>
  155. forcedinline void tween (const Pixel& src, const uint32 amount) noexcept
  156. {
  157. uint32 dEvenBytes = getEvenBytes();
  158. dEvenBytes += (((src.getEvenBytes() - dEvenBytes) * amount) >> 8);
  159. dEvenBytes &= 0x00ff00ff;
  160. uint32 dOddBytes = getOddBytes();
  161. dOddBytes += (((src.getOddBytes() - dOddBytes) * amount) >> 8);
  162. dOddBytes &= 0x00ff00ff;
  163. dOddBytes <<= 8;
  164. dOddBytes |= dEvenBytes;
  165. internal = dOddBytes;
  166. }
  167. //==============================================================================
  168. /** Replaces the colour's alpha value with another one. */
  169. forcedinline void setAlpha (const uint8 newAlpha) noexcept
  170. {
  171. components.a = newAlpha;
  172. }
  173. /** Multiplies the colour's alpha value with another one. */
  174. forcedinline void multiplyAlpha (int multiplier) noexcept
  175. {
  176. // increment alpha by 1, so that if multiplier == 255 (full alpha),
  177. // this function will not change the values.
  178. ++multiplier;
  179. internal = ((((uint32) multiplier) * getOddBytes()) & 0xff00ff00)
  180. | (((((uint32) multiplier) * getEvenBytes()) >> 8) & 0x00ff00ff);
  181. }
  182. forcedinline void multiplyAlpha (const float multiplier) noexcept
  183. {
  184. multiplyAlpha ((int) (multiplier * 255.0f));
  185. }
  186. inline PixelARGB getUnpremultiplied() const noexcept { PixelARGB p (internal); p.unpremultiply(); return p; }
  187. /** Premultiplies the pixel's RGB values by its alpha. */
  188. forcedinline void premultiply() noexcept
  189. {
  190. const uint32 alpha = components.a;
  191. if (alpha < 0xff)
  192. {
  193. if (alpha == 0)
  194. {
  195. components.b = 0;
  196. components.g = 0;
  197. components.r = 0;
  198. }
  199. else
  200. {
  201. components.b = (uint8) ((components.b * alpha + 0x7f) >> 8);
  202. components.g = (uint8) ((components.g * alpha + 0x7f) >> 8);
  203. components.r = (uint8) ((components.r * alpha + 0x7f) >> 8);
  204. }
  205. }
  206. }
  207. /** Unpremultiplies the pixel's RGB values. */
  208. forcedinline void unpremultiply() noexcept
  209. {
  210. const uint32 alpha = components.a;
  211. if (alpha < 0xff)
  212. {
  213. if (alpha == 0)
  214. {
  215. components.b = 0;
  216. components.g = 0;
  217. components.r = 0;
  218. }
  219. else
  220. {
  221. components.b = (uint8) jmin ((uint32) 0xffu, (components.b * 0xffu) / alpha);
  222. components.g = (uint8) jmin ((uint32) 0xffu, (components.g * 0xffu) / alpha);
  223. components.r = (uint8) jmin ((uint32) 0xffu, (components.r * 0xffu) / alpha);
  224. }
  225. }
  226. }
  227. forcedinline void desaturate() noexcept
  228. {
  229. if (components.a < 0xff && components.a > 0)
  230. {
  231. const int newUnpremultipliedLevel = (0xff * ((int) components.r + (int) components.g + (int) components.b) / (3 * components.a));
  232. components.r = components.g = components.b
  233. = (uint8) ((newUnpremultipliedLevel * components.a + 0x7f) >> 8);
  234. }
  235. else
  236. {
  237. components.r = components.g = components.b
  238. = (uint8) (((int) components.r + (int) components.g + (int) components.b) / 3);
  239. }
  240. }
  241. //==============================================================================
  242. /** The indexes of the different components in the byte layout of this type of colour. */
  243. #if JUCE_ANDROID
  244. #if JUCE_BIG_ENDIAN
  245. enum { indexA = 0, indexR = 3, indexG = 2, indexB = 1 };
  246. #else
  247. enum { indexA = 3, indexR = 0, indexG = 1, indexB = 2 };
  248. #endif
  249. #else
  250. #if JUCE_BIG_ENDIAN
  251. enum { indexA = 0, indexR = 1, indexG = 2, indexB = 3 };
  252. #else
  253. enum { indexA = 3, indexR = 2, indexG = 1, indexB = 0 };
  254. #endif
  255. #endif
  256. private:
  257. //==============================================================================
  258. PixelARGB (const uint32 internalValue) noexcept
  259. : internal (internalValue)
  260. {
  261. }
  262. //==============================================================================
  263. struct Components
  264. {
  265. #if JUCE_ANDROID
  266. #if JUCE_BIG_ENDIAN
  267. uint8 a, b, g, r;
  268. #else
  269. uint8 r, g, b, a;
  270. #endif
  271. #else
  272. #if JUCE_BIG_ENDIAN
  273. uint8 a, r, g, b;
  274. #else
  275. uint8 b, g, r, a;
  276. #endif
  277. #endif
  278. } JUCE_PACKED;
  279. union
  280. {
  281. uint32 internal;
  282. Components components;
  283. #if JUCE_GCC
  284. uint8 comps[4]; // helper struct needed because gcc does not allow references to packed union members
  285. #endif
  286. };
  287. }
  288. #ifndef DOXYGEN
  289. JUCE_PACKED
  290. #endif
  291. ;
  292. //==============================================================================
  293. /**
  294. Represents a 24-bit RGB pixel, and can perform compositing operations on it.
  295. This is used internally by the imaging classes.
  296. @see PixelARGB
  297. */
  298. class JUCE_API PixelRGB
  299. {
  300. public:
  301. /** Creates a pixel without defining its colour. */
  302. PixelRGB() noexcept {}
  303. ~PixelRGB() noexcept {}
  304. //==============================================================================
  305. /** Returns a uint32 which represents the pixel in a platform dependent format which is compatible
  306. with the native format of a PixelARGB.
  307. @see PixelARGB::getNativeARGB */
  308. forcedinline uint32 getNativeARGB() const noexcept
  309. {
  310. #if JUCE_ANDROID
  311. return (uint32) ((0xff << 24) | r | (g << 8) | (b << 16));
  312. #else
  313. return (uint32) ((0xff << 24) | b | (g << 8) | (r << 16));
  314. #endif
  315. }
  316. /** Returns a uint32 which will be in argb order as if constructed with the following mask operation
  317. ((alpha << 24) | (red << 16) | (green << 8) | blue). */
  318. forcedinline uint32 getInARGBMaskOrder() const noexcept
  319. {
  320. #if JUCE_ANDROID
  321. return (uint32) ((0xff << 24) | (r << 16) | (g << 8) | (b << 0));
  322. #else
  323. return getNativeARGB();
  324. #endif
  325. }
  326. /** Returns a uint32 which when written to memory, will be in the order a, r, g, b. In other words,
  327. if the return-value is read as a uint8 array then the elements will be in the order of a, r, g, b*/
  328. inline uint32 getInARGBMemoryOrder() const noexcept
  329. {
  330. #if JUCE_BIG_ENDIAN
  331. return getInARGBMaskOrder();
  332. #else
  333. return (uint32) ((b << 24) | (g << 16) | (r << 8) | 0xff);
  334. #endif
  335. }
  336. /** Return channels with an even index and insert zero bytes between them. This is useful for blending
  337. operations. The exact channels which are returned is platform dependent but compatible with the
  338. return value of getEvenBytes of the PixelARGB class.
  339. @see PixelARGB::getEvenBytes */
  340. forcedinline uint32 getEvenBytes() const noexcept
  341. {
  342. #if JUCE_ANDROID
  343. return (uint32) (r | (b << 16));
  344. #else
  345. return (uint32) (b | (r << 16));
  346. #endif
  347. }
  348. /** Return channels with an odd index and insert zero bytes between them. This is useful for blending
  349. operations. The exact channels which are returned is platform dependent but compatible with the
  350. return value of getOddBytes of the PixelARGB class.
  351. @see PixelARGB::getOddBytes */
  352. forcedinline uint32 getOddBytes() const noexcept { return (uint32)0xff0000 | g; }
  353. //==============================================================================
  354. forcedinline uint8 getAlpha() const noexcept { return 0xff; }
  355. forcedinline uint8 getRed() const noexcept { return r; }
  356. forcedinline uint8 getGreen() const noexcept { return g; }
  357. forcedinline uint8 getBlue() const noexcept { return b; }
  358. forcedinline uint8& getRed() noexcept { return r; }
  359. forcedinline uint8& getGreen() noexcept { return g; }
  360. forcedinline uint8& getBlue() noexcept { return b; }
  361. //==============================================================================
  362. /** Copies another pixel colour over this one.
  363. This doesn't blend it - this colour is simply replaced by the other one.
  364. Because PixelRGB has no alpha channel, any alpha value in the source pixel
  365. is thrown away.
  366. */
  367. template <class Pixel>
  368. forcedinline void set (const Pixel& src) noexcept
  369. {
  370. b = src.getBlue();
  371. g = src.getGreen();
  372. r = src.getRed();
  373. }
  374. /** Sets the pixel's colour from individual components. */
  375. void setARGB (const uint8, const uint8 red, const uint8 green, const uint8 blue) noexcept
  376. {
  377. r = red;
  378. g = green;
  379. b = blue;
  380. }
  381. //==============================================================================
  382. /** Blends another pixel onto this one.
  383. This takes into account the opacity of the pixel being overlaid, and blends
  384. it accordingly.
  385. */
  386. template <class Pixel>
  387. forcedinline void blend (const Pixel& src) noexcept
  388. {
  389. const uint32 alpha = (uint32) (0x100 - src.getAlpha());
  390. // getEvenBytes returns 0x00rr00bb on non-android
  391. uint32 rb = clampPixelComponents (src.getEvenBytes() + maskPixelComponents (getEvenBytes() * alpha));
  392. // getOddBytes returns 0x00aa00gg on non-android
  393. uint32 ag = clampPixelComponents (src.getOddBytes() + ((g * alpha) >> 8));
  394. g = (uint8) (ag & 0xff);
  395. #if JUCE_ANDROID
  396. b = (uint8) (rb >> 16);
  397. r = (uint8) (rb & 0xff);
  398. #else
  399. r = (uint8) (rb >> 16);
  400. b = (uint8) (rb & 0xff);
  401. #endif
  402. }
  403. forcedinline void blend (const PixelRGB src) noexcept
  404. {
  405. set (src);
  406. }
  407. /** Blends another pixel onto this one, applying an extra multiplier to its opacity.
  408. The opacity of the pixel being overlaid is scaled by the extraAlpha factor before
  409. being used, so this can blend semi-transparently from a PixelRGB argument.
  410. */
  411. template <class Pixel>
  412. forcedinline void blend (const Pixel& src, uint32 extraAlpha) noexcept
  413. {
  414. uint32 ag = maskPixelComponents (extraAlpha * src.getOddBytes());
  415. uint32 rb = maskPixelComponents (extraAlpha * src.getEvenBytes());
  416. const uint32 alpha = 0x100 - (ag >> 16);
  417. ag = clampPixelComponents (ag + (g * alpha >> 8));
  418. rb = clampPixelComponents (rb + maskPixelComponents (getEvenBytes() * alpha));
  419. g = (uint8) (ag & 0xff);
  420. #if JUCE_ANDROID
  421. b = (uint8) (rb >> 16);
  422. r = (uint8) (rb & 0xff);
  423. #else
  424. r = (uint8) (rb >> 16);
  425. b = (uint8) (rb & 0xff);
  426. #endif
  427. }
  428. /** Blends another pixel with this one, creating a colour that is somewhere
  429. between the two, as specified by the amount.
  430. */
  431. template <class Pixel>
  432. forcedinline void tween (const Pixel& src, const uint32 amount) noexcept
  433. {
  434. uint32 dEvenBytes = getEvenBytes();
  435. dEvenBytes += (((src.getEvenBytes() - dEvenBytes) * amount) >> 8);
  436. uint32 dOddBytes = getOddBytes();
  437. dOddBytes += (((src.getOddBytes() - dOddBytes) * amount) >> 8);
  438. g = (uint8) (dOddBytes & 0xff); // dOddBytes = 0x00aa00gg
  439. #if JUCE_ANDROID
  440. r = (uint8) (dEvenBytes & 0xff); // dEvenBytes = 0x00bb00rr
  441. b = (uint8) (dEvenBytes >> 16);
  442. #else
  443. b = (uint8) (dEvenBytes & 0xff); // dEvenBytes = 0x00rr00bb
  444. r = (uint8) (dEvenBytes >> 16);
  445. #endif
  446. }
  447. //==============================================================================
  448. /** This method is included for compatibility with the PixelARGB class. */
  449. forcedinline void setAlpha (const uint8) noexcept {}
  450. /** Multiplies the colour's alpha value with another one. */
  451. forcedinline void multiplyAlpha (int) noexcept {}
  452. /** Multiplies the colour's alpha value with another one. */
  453. forcedinline void multiplyAlpha (float) noexcept {}
  454. /** Premultiplies the pixel's RGB values by its alpha. */
  455. forcedinline void premultiply() noexcept {}
  456. /** Unpremultiplies the pixel's RGB values. */
  457. forcedinline void unpremultiply() noexcept {}
  458. forcedinline void desaturate() noexcept
  459. {
  460. r = g = b = (uint8) (((int) r + (int) g + (int) b) / 3);
  461. }
  462. //==============================================================================
  463. /** The indexes of the different components in the byte layout of this type of colour. */
  464. #if JUCE_MAC
  465. enum { indexR = 0, indexG = 1, indexB = 2 };
  466. #else
  467. enum { indexR = 2, indexG = 1, indexB = 0 };
  468. #endif
  469. private:
  470. //==============================================================================
  471. PixelRGB (const uint32 internal) noexcept
  472. {
  473. #if JUCE_ANDROID
  474. b = (uint8) (internal >> 16);
  475. g = (uint8) (internal >> 8);
  476. r = (uint8) (internal);
  477. #else
  478. r = (uint8) (internal >> 16);
  479. g = (uint8) (internal >> 8);
  480. b = (uint8) (internal);
  481. #endif
  482. }
  483. //==============================================================================
  484. #if JUCE_MAC
  485. uint8 r, g, b;
  486. #else
  487. uint8 b, g, r;
  488. #endif
  489. }
  490. #ifndef DOXYGEN
  491. JUCE_PACKED
  492. #endif
  493. ;
  494. forcedinline void PixelARGB::blend (const PixelRGB src) noexcept
  495. {
  496. set (src);
  497. }
  498. //==============================================================================
  499. /**
  500. Represents an 8-bit single-channel pixel, and can perform compositing operations on it.
  501. This is used internally by the imaging classes.
  502. @see PixelARGB, PixelRGB
  503. */
  504. class JUCE_API PixelAlpha
  505. {
  506. public:
  507. /** Creates a pixel without defining its colour. */
  508. PixelAlpha() noexcept {}
  509. ~PixelAlpha() noexcept {}
  510. //==============================================================================
  511. /** Returns a uint32 which represents the pixel in a platform dependent format which is compatible
  512. with the native format of a PixelARGB.
  513. @see PixelARGB::getNativeARGB */
  514. forcedinline uint32 getNativeARGB() const noexcept { return (uint32) ((a << 24) | (a << 16) | (a << 8) | a); }
  515. /** Returns a uint32 which will be in argb order as if constructed with the following mask operation
  516. ((alpha << 24) | (red << 16) | (green << 8) | blue). */
  517. forcedinline uint32 getInARGBMaskOrder() const noexcept { return getNativeARGB(); }
  518. /** Returns a uint32 which when written to memory, will be in the order a, r, g, b. In other words,
  519. if the return-value is read as a uint8 array then the elements will be in the order of a, r, g, b*/
  520. inline uint32 getInARGBMemoryOrder() const noexcept { return getNativeARGB(); }
  521. /** Return channels with an even index and insert zero bytes between them. This is useful for blending
  522. operations. The exact channels which are returned is platform dependent but compatible with the
  523. return value of getEvenBytes of the PixelARGB class.
  524. @see PixelARGB::getEvenBytes */
  525. forcedinline uint32 getEvenBytes() const noexcept { return (uint32) ((a << 16) | a); }
  526. /** Return channels with an odd index and insert zero bytes between them. This is useful for blending
  527. operations. The exact channels which are returned is platform dependent but compatible with the
  528. return value of getOddBytes of the PixelARGB class.
  529. @see PixelARGB::getOddBytes */
  530. forcedinline uint32 getOddBytes() const noexcept { return (uint32) ((a << 16) | a); }
  531. //==============================================================================
  532. forcedinline uint8 getAlpha() const noexcept { return a; }
  533. forcedinline uint8& getAlpha() noexcept { return a; }
  534. forcedinline uint8 getRed() const noexcept { return 0; }
  535. forcedinline uint8 getGreen() const noexcept { return 0; }
  536. forcedinline uint8 getBlue() const noexcept { return 0; }
  537. //==============================================================================
  538. /** Copies another pixel colour over this one.
  539. This doesn't blend it - this colour is simply replaced by the other one.
  540. */
  541. template <class Pixel>
  542. forcedinline void set (const Pixel& src) noexcept
  543. {
  544. a = src.getAlpha();
  545. }
  546. /** Sets the pixel's colour from individual components. */
  547. forcedinline void setARGB (const uint8 a_, const uint8 /*r*/, const uint8 /*g*/, const uint8 /*b*/) noexcept
  548. {
  549. a = a_;
  550. }
  551. //==============================================================================
  552. /** Blends another pixel onto this one.
  553. This takes into account the opacity of the pixel being overlaid, and blends
  554. it accordingly.
  555. */
  556. template <class Pixel>
  557. forcedinline void blend (const Pixel& src) noexcept
  558. {
  559. const int srcA = src.getAlpha();
  560. a = (uint8) ((a * (0x100 - srcA) >> 8) + srcA);
  561. }
  562. /** Blends another pixel onto this one, applying an extra multiplier to its opacity.
  563. The opacity of the pixel being overlaid is scaled by the extraAlpha factor before
  564. being used, so this can blend semi-transparently from a PixelRGB argument.
  565. */
  566. template <class Pixel>
  567. forcedinline void blend (const Pixel& src, uint32 extraAlpha) noexcept
  568. {
  569. ++extraAlpha;
  570. const int srcAlpha = (int) ((extraAlpha * src.getAlpha()) >> 8);
  571. a = (uint8) ((a * (0x100 - srcAlpha) >> 8) + srcAlpha);
  572. }
  573. /** Blends another pixel with this one, creating a colour that is somewhere
  574. between the two, as specified by the amount.
  575. */
  576. template <class Pixel>
  577. forcedinline void tween (const Pixel& src, const uint32 amount) noexcept
  578. {
  579. a += ((src.getAlpha() - a) * amount) >> 8;
  580. }
  581. //==============================================================================
  582. /** Replaces the colour's alpha value with another one. */
  583. forcedinline void setAlpha (const uint8 newAlpha) noexcept
  584. {
  585. a = newAlpha;
  586. }
  587. /** Multiplies the colour's alpha value with another one. */
  588. forcedinline void multiplyAlpha (int multiplier) noexcept
  589. {
  590. ++multiplier;
  591. a = (uint8) ((a * multiplier) >> 8);
  592. }
  593. forcedinline void multiplyAlpha (const float multiplier) noexcept
  594. {
  595. a = (uint8) (a * multiplier);
  596. }
  597. /** Premultiplies the pixel's RGB values by its alpha. */
  598. forcedinline void premultiply() noexcept {}
  599. /** Unpremultiplies the pixel's RGB values. */
  600. forcedinline void unpremultiply() noexcept {}
  601. forcedinline void desaturate() noexcept {}
  602. //==============================================================================
  603. /** The indexes of the different components in the byte layout of this type of colour. */
  604. enum { indexA = 0 };
  605. private:
  606. //==============================================================================
  607. PixelAlpha (const uint32 internal) noexcept
  608. {
  609. a = (uint8) (internal >> 24);
  610. }
  611. //==============================================================================
  612. uint8 a;
  613. }
  614. #ifndef DOXYGEN
  615. JUCE_PACKED
  616. #endif
  617. ;
  618. #if JUCE_MSVC
  619. #pragma pack (pop)
  620. #endif
  621. #endif // JUCE_PIXELFORMATS_H_INCLUDED