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.

611 lines
19KB

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