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.

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