The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

606 lines
20KB

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