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.

605 lines
20KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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 ARGB 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. /** Creates a pixel from a 32-bit argb value.
  47. */
  48. PixelARGB (const uint32 argbValue) noexcept
  49. : argb (argbValue)
  50. {
  51. }
  52. PixelARGB (const uint8 a, const uint8 r, const uint8 g, const uint8 b) noexcept
  53. {
  54. components.b = b;
  55. components.g = g;
  56. components.r = r;
  57. components.a = a;
  58. }
  59. forcedinline uint32 getARGB() const noexcept { return argb; }
  60. forcedinline uint32 getUnpremultipliedARGB() const noexcept { PixelARGB p (argb); p.unpremultiply(); return p.getARGB(); }
  61. forcedinline uint32 getRB() const noexcept { return 0x00ff00ff & argb; }
  62. forcedinline uint32 getAG() const noexcept { return 0x00ff00ff & (argb >> 8); }
  63. forcedinline uint8 getAlpha() const noexcept { return components.a; }
  64. forcedinline uint8 getRed() const noexcept { return components.r; }
  65. forcedinline uint8 getGreen() const noexcept { return components.g; }
  66. forcedinline uint8 getBlue() const noexcept { return components.b; }
  67. #if JUCE_GCC && ! JUCE_CLANG
  68. // NB these are here as a workaround because GCC refuses to bind to packed values.
  69. forcedinline uint8& getAlpha() noexcept { return comps [indexA]; }
  70. forcedinline uint8& getRed() noexcept { return comps [indexR]; }
  71. forcedinline uint8& getGreen() noexcept { return comps [indexG]; }
  72. forcedinline uint8& getBlue() noexcept { return comps [indexB]; }
  73. #else
  74. forcedinline uint8& getAlpha() noexcept { return components.a; }
  75. forcedinline uint8& getRed() noexcept { return components.r; }
  76. forcedinline uint8& getGreen() noexcept { return components.g; }
  77. forcedinline uint8& getBlue() noexcept { return components.b; }
  78. #endif
  79. /** Blends another pixel onto this one.
  80. This takes into account the opacity of the pixel being overlaid, and blends
  81. it accordingly.
  82. */
  83. template <class Pixel>
  84. forcedinline void blend (const Pixel& src) noexcept
  85. {
  86. const uint32 alpha = 0x100 - src.getAlpha();
  87. uint32 rb = src.getRB() + maskPixelComponents (getRB() * alpha);
  88. uint32 ag = src.getAG() + maskPixelComponents (getAG() * alpha);
  89. argb = clampPixelComponents (rb) + (clampPixelComponents (ag) << 8);
  90. }
  91. /** Blends another pixel onto this one.
  92. This takes into account the opacity of the pixel being overlaid, and blends
  93. it accordingly.
  94. */
  95. forcedinline void blend (const PixelRGB src) noexcept;
  96. /** Blends another pixel onto this one, applying an extra multiplier to its opacity.
  97. The opacity of the pixel being overlaid is scaled by the extraAlpha factor before
  98. being used, so this can blend semi-transparently from a PixelRGB argument.
  99. */
  100. template <class Pixel>
  101. forcedinline void blend (const Pixel& src, uint32 extraAlpha) noexcept
  102. {
  103. uint32 ag = maskPixelComponents (extraAlpha * src.getAG());
  104. const uint32 alpha = 0x100 - (ag >> 16);
  105. ag += maskPixelComponents (getAG() * alpha);
  106. uint32 rb = maskPixelComponents (extraAlpha * src.getRB())
  107. + maskPixelComponents (getRB() * alpha);
  108. argb = clampPixelComponents(rb) + (clampPixelComponents (ag) << 8);
  109. }
  110. /** Blends another pixel with this one, creating a colour that is somewhere
  111. between the two, as specified by the amount.
  112. */
  113. template <class Pixel>
  114. forcedinline void tween (const Pixel& src, const uint32 amount) noexcept
  115. {
  116. uint32 drb = getRB();
  117. drb += (((src.getRB() - drb) * amount) >> 8);
  118. drb &= 0x00ff00ff;
  119. uint32 dag = getAG();
  120. dag += (((src.getAG() - dag) * amount) >> 8);
  121. dag &= 0x00ff00ff;
  122. dag <<= 8;
  123. dag |= drb;
  124. argb = dag;
  125. }
  126. /** Copies another pixel colour over this one.
  127. This doesn't blend it - this colour is simply replaced by the other one.
  128. */
  129. template <class Pixel>
  130. forcedinline void set (const Pixel& src) noexcept
  131. {
  132. argb = src.getARGB();
  133. }
  134. /** Replaces the colour's alpha value with another one. */
  135. forcedinline void setAlpha (const uint8 newAlpha) noexcept
  136. {
  137. components.a = newAlpha;
  138. }
  139. /** Multiplies the colour's alpha value with another one. */
  140. forcedinline void multiplyAlpha (int multiplier) noexcept
  141. {
  142. ++multiplier;
  143. argb = ((((uint32) multiplier) * getAG()) & 0xff00ff00)
  144. | (((((uint32) multiplier) * getRB()) >> 8) & 0x00ff00ff);
  145. }
  146. forcedinline void multiplyAlpha (const float multiplier) noexcept
  147. {
  148. multiplyAlpha ((int) (multiplier * 255.0f));
  149. }
  150. /** Sets the pixel's colour from individual components. */
  151. void setARGB (const uint8 a, const uint8 r, const uint8 g, const uint8 b) noexcept
  152. {
  153. components.b = b;
  154. components.g = g;
  155. components.r = r;
  156. components.a = a;
  157. }
  158. /** Premultiplies the pixel's RGB values by its alpha. */
  159. forcedinline void premultiply() noexcept
  160. {
  161. const uint32 alpha = components.a;
  162. if (alpha < 0xff)
  163. {
  164. if (alpha == 0)
  165. {
  166. components.b = 0;
  167. components.g = 0;
  168. components.r = 0;
  169. }
  170. else
  171. {
  172. components.b = (uint8) ((components.b * alpha + 0x7f) >> 8);
  173. components.g = (uint8) ((components.g * alpha + 0x7f) >> 8);
  174. components.r = (uint8) ((components.r * alpha + 0x7f) >> 8);
  175. }
  176. }
  177. }
  178. /** Unpremultiplies the pixel's RGB values. */
  179. forcedinline void unpremultiply() noexcept
  180. {
  181. const uint32 alpha = components.a;
  182. if (alpha < 0xff)
  183. {
  184. if (alpha == 0)
  185. {
  186. components.b = 0;
  187. components.g = 0;
  188. components.r = 0;
  189. }
  190. else
  191. {
  192. components.b = (uint8) jmin ((uint32) 0xff, (components.b * 0xff) / alpha);
  193. components.g = (uint8) jmin ((uint32) 0xff, (components.g * 0xff) / alpha);
  194. components.r = (uint8) jmin ((uint32) 0xff, (components.r * 0xff) / alpha);
  195. }
  196. }
  197. }
  198. forcedinline void desaturate() noexcept
  199. {
  200. if (components.a < 0xff && components.a > 0)
  201. {
  202. const int newUnpremultipliedLevel = (0xff * ((int) components.r + (int) components.g + (int) components.b) / (3 * components.a));
  203. components.r = components.g = components.b
  204. = (uint8) ((newUnpremultipliedLevel * components.a + 0x7f) >> 8);
  205. }
  206. else
  207. {
  208. components.r = components.g = components.b
  209. = (uint8) (((int) components.r + (int) components.g + (int) components.b) / 3);
  210. }
  211. }
  212. /** Returns a uint32 which when written to memory, will be in the order r, g, b, a. */
  213. inline uint32 getInRGBAMemoryOrder() const noexcept
  214. {
  215. #if JUCE_BIG_ENDIAN
  216. return (((uint32) components.r) << 24) | (((uint32) components.g) << 16) | (((uint32) components.b) << 8) | components.a;
  217. #else
  218. return (((uint32) components.a) << 24) | (((uint32) components.b) << 16) | (((uint32) components.g) << 8) | components.r;
  219. #endif
  220. }
  221. //==============================================================================
  222. /** The indexes of the different components in the byte layout of this type of colour. */
  223. #if JUCE_BIG_ENDIAN
  224. enum { indexA = 0, indexR = 1, indexG = 2, indexB = 3 };
  225. #else
  226. enum { indexA = 3, indexR = 2, indexG = 1, indexB = 0 };
  227. #endif
  228. private:
  229. //==============================================================================
  230. struct Components
  231. {
  232. #if JUCE_BIG_ENDIAN
  233. uint8 a, r, g, b;
  234. #else
  235. uint8 b, g, r, a;
  236. #endif
  237. } JUCE_PACKED;
  238. union
  239. {
  240. uint32 argb;
  241. Components components;
  242. #if JUCE_GCC
  243. uint8 comps[4];
  244. #endif
  245. };
  246. }
  247. #ifndef DOXYGEN
  248. JUCE_PACKED
  249. #endif
  250. ;
  251. //==============================================================================
  252. /**
  253. Represents a 24-bit RGB pixel, and can perform compositing operations on it.
  254. This is used internally by the imaging classes.
  255. @see PixelARGB
  256. */
  257. class JUCE_API PixelRGB
  258. {
  259. public:
  260. /** Creates a pixel without defining its colour. */
  261. PixelRGB() noexcept {}
  262. ~PixelRGB() noexcept {}
  263. /** Creates a pixel from a 32-bit argb value.
  264. (The argb format is that used by PixelARGB)
  265. */
  266. PixelRGB (const uint32 argb) noexcept
  267. {
  268. r = (uint8) (argb >> 16);
  269. g = (uint8) (argb >> 8);
  270. b = (uint8) (argb);
  271. }
  272. forcedinline uint32 getARGB() const noexcept { return 0xff000000 | b | (((uint32) g) << 8) | (((uint32) r) << 16); }
  273. forcedinline uint32 getUnpremultipliedARGB() const noexcept { return getARGB(); }
  274. forcedinline uint32 getRB() const noexcept { return b | (uint32) (r << 16); }
  275. forcedinline uint32 getAG() const noexcept { return (uint32) (0xff0000 | g); }
  276. forcedinline uint8 getAlpha() const noexcept { return 0xff; }
  277. forcedinline uint8 getRed() const noexcept { return r; }
  278. forcedinline uint8 getGreen() const noexcept { return g; }
  279. forcedinline uint8 getBlue() const noexcept { return b; }
  280. forcedinline uint8& getRed() noexcept { return r; }
  281. forcedinline uint8& getGreen() noexcept { return g; }
  282. forcedinline uint8& getBlue() noexcept { return b; }
  283. /** Blends another pixel onto this one.
  284. This takes into account the opacity of the pixel being overlaid, and blends
  285. it accordingly.
  286. */
  287. template <class Pixel>
  288. forcedinline void blend (const Pixel& src) noexcept
  289. {
  290. const uint32 alpha = 0x100 - src.getAlpha();
  291. uint32 rb = clampPixelComponents (src.getRB() + maskPixelComponents (getRB() * alpha));
  292. uint32 ag = src.getAG() + (g * alpha >> 8);
  293. r = (uint8) (rb >> 16);
  294. g = (uint8) clampPixelComponents (ag);
  295. b = (uint8) rb;
  296. }
  297. forcedinline void blend (const PixelRGB src) noexcept
  298. {
  299. set (src);
  300. }
  301. /** Blends another pixel onto this one, applying an extra multiplier to its opacity.
  302. The opacity of the pixel being overlaid is scaled by the extraAlpha factor before
  303. being used, so this can blend semi-transparently from a PixelRGB argument.
  304. */
  305. template <class Pixel>
  306. forcedinline void blend (const Pixel& src, uint32 extraAlpha) noexcept
  307. {
  308. uint32 ag = maskPixelComponents (extraAlpha * src.getAG());
  309. const uint32 alpha = 0x100 - (ag >> 16);
  310. ag += g * alpha >> 8;
  311. uint32 rb = clampPixelComponents (maskPixelComponents (extraAlpha * src.getRB())
  312. + maskPixelComponents (getRB() * alpha));
  313. b = (uint8) rb;
  314. g = (uint8) clampPixelComponents (ag);
  315. r = (uint8) (rb >> 16);
  316. }
  317. /** Blends another pixel with this one, creating a colour that is somewhere
  318. between the two, as specified by the amount.
  319. */
  320. template <class Pixel>
  321. forcedinline void tween (const Pixel& src, const uint32 amount) noexcept
  322. {
  323. uint32 drb = getRB();
  324. drb += (((src.getRB() - drb) * amount) >> 8);
  325. uint32 dag = getAG();
  326. dag += (((src.getAG() - dag) * amount) >> 8);
  327. b = (uint8) drb;
  328. g = (uint8) dag;
  329. r = (uint8) (drb >> 16);
  330. }
  331. /** Copies another pixel colour over this one.
  332. This doesn't blend it - this colour is simply replaced by the other one.
  333. Because PixelRGB has no alpha channel, any alpha value in the source pixel
  334. is thrown away.
  335. */
  336. template <class Pixel>
  337. forcedinline void set (const Pixel& src) noexcept
  338. {
  339. b = src.getBlue();
  340. g = src.getGreen();
  341. r = src.getRed();
  342. }
  343. /** This method is included for compatibility with the PixelARGB class. */
  344. forcedinline void setAlpha (const uint8) noexcept {}
  345. /** Multiplies the colour's alpha value with another one. */
  346. forcedinline void multiplyAlpha (int) noexcept {}
  347. /** Multiplies the colour's alpha value with another one. */
  348. forcedinline void multiplyAlpha (float) noexcept {}
  349. /** Sets the pixel's colour from individual components. */
  350. void setARGB (const uint8, const uint8 red, const uint8 green, const uint8 blue) noexcept
  351. {
  352. r = red;
  353. g = green;
  354. b = blue;
  355. }
  356. /** Premultiplies the pixel's RGB values by its alpha. */
  357. forcedinline void premultiply() noexcept {}
  358. /** Unpremultiplies the pixel's RGB values. */
  359. forcedinline void unpremultiply() noexcept {}
  360. forcedinline void desaturate() noexcept
  361. {
  362. r = g = b = (uint8) (((int) r + (int) g + (int) b) / 3);
  363. }
  364. //==============================================================================
  365. /** The indexes of the different components in the byte layout of this type of colour. */
  366. #if JUCE_MAC
  367. enum { indexR = 0, indexG = 1, indexB = 2 };
  368. #else
  369. enum { indexR = 2, indexG = 1, indexB = 0 };
  370. #endif
  371. private:
  372. //==============================================================================
  373. #if JUCE_MAC
  374. uint8 r, g, b;
  375. #else
  376. uint8 b, g, r;
  377. #endif
  378. }
  379. #ifndef DOXYGEN
  380. JUCE_PACKED
  381. #endif
  382. ;
  383. forcedinline void PixelARGB::blend (const PixelRGB src) noexcept
  384. {
  385. set (src);
  386. }
  387. //==============================================================================
  388. /**
  389. Represents an 8-bit single-channel pixel, and can perform compositing operations on it.
  390. This is used internally by the imaging classes.
  391. @see PixelARGB, PixelRGB
  392. */
  393. class JUCE_API PixelAlpha
  394. {
  395. public:
  396. /** Creates a pixel without defining its colour. */
  397. PixelAlpha() noexcept {}
  398. ~PixelAlpha() noexcept {}
  399. /** Creates a pixel from a 32-bit argb value.
  400. (The argb format is that used by PixelARGB)
  401. */
  402. PixelAlpha (const uint32 argb) noexcept
  403. {
  404. a = (uint8) (argb >> 24);
  405. }
  406. forcedinline uint32 getARGB() const noexcept { return (((uint32) a) << 24) | (((uint32) a) << 16) | (((uint32) a) << 8) | a; }
  407. forcedinline uint32 getUnpremultipliedARGB() const noexcept { return (((uint32) a) << 24) | 0xffffff; }
  408. forcedinline uint32 getRB() const noexcept { return (((uint32) a) << 16) | a; }
  409. forcedinline uint32 getAG() const noexcept { return (((uint32) a) << 16) | a; }
  410. forcedinline uint8 getAlpha() const noexcept { return a; }
  411. forcedinline uint8& getAlpha() noexcept { return a; }
  412. forcedinline uint8 getRed() const noexcept { return 0; }
  413. forcedinline uint8 getGreen() const noexcept { return 0; }
  414. forcedinline uint8 getBlue() const noexcept { return 0; }
  415. /** Blends another pixel onto this one.
  416. This takes into account the opacity of the pixel being overlaid, and blends
  417. it accordingly.
  418. */
  419. template <class Pixel>
  420. forcedinline void blend (const Pixel& src) noexcept
  421. {
  422. const int srcA = src.getAlpha();
  423. a = (uint8) ((a * (0x100 - srcA) >> 8) + srcA);
  424. }
  425. /** Blends another pixel onto this one, applying an extra multiplier to its opacity.
  426. The opacity of the pixel being overlaid is scaled by the extraAlpha factor before
  427. being used, so this can blend semi-transparently from a PixelRGB argument.
  428. */
  429. template <class Pixel>
  430. forcedinline void blend (const Pixel& src, uint32 extraAlpha) noexcept
  431. {
  432. ++extraAlpha;
  433. const int srcAlpha = (int) ((extraAlpha * src.getAlpha()) >> 8);
  434. a = (uint8) ((a * (0x100 - srcAlpha) >> 8) + srcAlpha);
  435. }
  436. /** Blends another pixel with this one, creating a colour that is somewhere
  437. between the two, as specified by the amount.
  438. */
  439. template <class Pixel>
  440. forcedinline void tween (const Pixel& src, const uint32 amount) noexcept
  441. {
  442. a += ((src.getAlpha() - a) * amount) >> 8;
  443. }
  444. /** Copies another pixel colour over this one.
  445. This doesn't blend it - this colour is simply replaced by the other one.
  446. */
  447. template <class Pixel>
  448. forcedinline void set (const Pixel& src) noexcept
  449. {
  450. a = src.getAlpha();
  451. }
  452. /** Replaces the colour's alpha value with another one. */
  453. forcedinline void setAlpha (const uint8 newAlpha) noexcept
  454. {
  455. a = newAlpha;
  456. }
  457. /** Multiplies the colour's alpha value with another one. */
  458. forcedinline void multiplyAlpha (int multiplier) noexcept
  459. {
  460. ++multiplier;
  461. a = (uint8) ((a * multiplier) >> 8);
  462. }
  463. forcedinline void multiplyAlpha (const float multiplier) noexcept
  464. {
  465. a = (uint8) (a * multiplier);
  466. }
  467. /** Sets the pixel's colour from individual components. */
  468. forcedinline void setARGB (const uint8 a_, const uint8 /*r*/, const uint8 /*g*/, const uint8 /*b*/) noexcept
  469. {
  470. a = a_;
  471. }
  472. /** Premultiplies the pixel's RGB values by its alpha. */
  473. forcedinline void premultiply() noexcept {}
  474. /** Unpremultiplies the pixel's RGB values. */
  475. forcedinline void unpremultiply() noexcept {}
  476. forcedinline void desaturate() noexcept {}
  477. //==============================================================================
  478. /** The indexes of the different components in the byte layout of this type of colour. */
  479. enum { indexA = 0 };
  480. private:
  481. //==============================================================================
  482. uint8 a;
  483. }
  484. #ifndef DOXYGEN
  485. JUCE_PACKED
  486. #endif
  487. ;
  488. #if JUCE_MSVC
  489. #pragma pack (pop)
  490. #endif
  491. #endif // JUCE_PIXELFORMATS_H_INCLUDED