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.

593 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. template <class Pixel>
  72. forcedinline void blend (const Pixel& src) noexcept
  73. {
  74. uint32 sargb = src.getARGB();
  75. const uint32 alpha = 0x100 - (sargb >> 24);
  76. sargb += 0x00ff00ff & ((getRB() * alpha) >> 8);
  77. sargb += 0xff00ff00 & (getAG() * alpha);
  78. argb = sargb;
  79. }
  80. /** Blends another pixel onto this one.
  81. This takes into account the opacity of the pixel being overlaid, and blends
  82. it accordingly.
  83. */
  84. forcedinline void blend (const PixelRGB& src) noexcept;
  85. /** Blends another pixel onto this one, applying an extra multiplier to its opacity.
  86. The opacity of the pixel being overlaid is scaled by the extraAlpha factor before
  87. being used, so this can blend semi-transparently from a PixelRGB argument.
  88. */
  89. template <class Pixel>
  90. forcedinline void blend (const Pixel& src, uint32 extraAlpha) noexcept
  91. {
  92. ++extraAlpha;
  93. uint32 sargb = ((extraAlpha * src.getAG()) & 0xff00ff00)
  94. | (((extraAlpha * src.getRB()) >> 8) & 0x00ff00ff);
  95. const uint32 alpha = 0x100 - (sargb >> 24);
  96. sargb += 0x00ff00ff & ((getRB() * alpha) >> 8);
  97. sargb += 0xff00ff00 & (getAG() * alpha);
  98. argb = sargb;
  99. }
  100. /** Blends another pixel with this one, creating a colour that is somewhere
  101. between the two, as specified by the amount.
  102. */
  103. template <class Pixel>
  104. forcedinline void tween (const Pixel& src, const uint32 amount) noexcept
  105. {
  106. uint32 drb = getRB();
  107. drb += (((src.getRB() - drb) * amount) >> 8);
  108. drb &= 0x00ff00ff;
  109. uint32 dag = getAG();
  110. dag += (((src.getAG() - dag) * amount) >> 8);
  111. dag &= 0x00ff00ff;
  112. dag <<= 8;
  113. dag |= drb;
  114. argb = dag;
  115. }
  116. /** Copies another pixel colour over this one.
  117. This doesn't blend it - this colour is simply replaced by the other one.
  118. */
  119. template <class Pixel>
  120. forcedinline void set (const Pixel& src) noexcept
  121. {
  122. argb = src.getARGB();
  123. }
  124. /** Replaces the colour's alpha value with another one. */
  125. forcedinline void setAlpha (const uint8 newAlpha) noexcept
  126. {
  127. components.a = newAlpha;
  128. }
  129. /** Multiplies the colour's alpha value with another one. */
  130. forcedinline void multiplyAlpha (int multiplier) noexcept
  131. {
  132. ++multiplier;
  133. argb = ((((uint32) multiplier) * getAG()) & 0xff00ff00)
  134. | (((((uint32) multiplier) * getRB()) >> 8) & 0x00ff00ff);
  135. }
  136. forcedinline void multiplyAlpha (const float multiplier) noexcept
  137. {
  138. multiplyAlpha ((int) (multiplier * 255.0f));
  139. }
  140. /** Sets the pixel's colour from individual components. */
  141. void setARGB (const uint8 a, const uint8 r, const uint8 g, const uint8 b) noexcept
  142. {
  143. components.b = b;
  144. components.g = g;
  145. components.r = r;
  146. components.a = a;
  147. }
  148. /** Premultiplies the pixel's RGB values by its alpha. */
  149. forcedinline void premultiply() noexcept
  150. {
  151. const uint32 alpha = components.a;
  152. if (alpha < 0xff)
  153. {
  154. if (alpha == 0)
  155. {
  156. components.b = 0;
  157. components.g = 0;
  158. components.r = 0;
  159. }
  160. else
  161. {
  162. components.b = (uint8) ((components.b * alpha + 0x7f) >> 8);
  163. components.g = (uint8) ((components.g * alpha + 0x7f) >> 8);
  164. components.r = (uint8) ((components.r * alpha + 0x7f) >> 8);
  165. }
  166. }
  167. }
  168. /** Unpremultiplies the pixel's RGB values. */
  169. forcedinline void unpremultiply() noexcept
  170. {
  171. const uint32 alpha = components.a;
  172. if (alpha < 0xff)
  173. {
  174. if (alpha == 0)
  175. {
  176. components.b = 0;
  177. components.g = 0;
  178. components.r = 0;
  179. }
  180. else
  181. {
  182. components.b = (uint8) jmin ((uint32) 0xff, (components.b * 0xff) / alpha);
  183. components.g = (uint8) jmin ((uint32) 0xff, (components.g * 0xff) / alpha);
  184. components.r = (uint8) jmin ((uint32) 0xff, (components.r * 0xff) / alpha);
  185. }
  186. }
  187. }
  188. forcedinline void desaturate() noexcept
  189. {
  190. if (components.a < 0xff && components.a > 0)
  191. {
  192. const int newUnpremultipliedLevel = (0xff * ((int) components.r + (int) components.g + (int) components.b) / (3 * components.a));
  193. components.r = components.g = components.b
  194. = (uint8) ((newUnpremultipliedLevel * components.a + 0x7f) >> 8);
  195. }
  196. else
  197. {
  198. components.r = components.g = components.b
  199. = (uint8) (((int) components.r + (int) components.g + (int) components.b) / 3);
  200. }
  201. }
  202. /** Returns a uint32 which when written to memory, will be in the order r, g, b, a. */
  203. inline uint32 getInRGBAMemoryOrder() const noexcept
  204. {
  205. #if JUCE_BIG_ENDIAN
  206. return (((uint32) components.r) << 24) | (((uint32) components.g) << 16) | (((uint32) components.b) << 8) | components.a;
  207. #else
  208. return (((uint32) components.a) << 24) | (((uint32) components.b) << 16) | (((uint32) components.g) << 8) | components.r;
  209. #endif
  210. }
  211. //==============================================================================
  212. /** The indexes of the different components in the byte layout of this type of colour. */
  213. #if JUCE_BIG_ENDIAN
  214. enum { indexA = 0, indexR = 1, indexG = 2, indexB = 3 };
  215. #else
  216. enum { indexA = 3, indexR = 2, indexG = 1, indexB = 0 };
  217. #endif
  218. private:
  219. //==============================================================================
  220. struct Components
  221. {
  222. #if JUCE_BIG_ENDIAN
  223. uint8 a : 8, r : 8, g : 8, b : 8;
  224. #else
  225. uint8 b, g, r, a;
  226. #endif
  227. } PACKED;
  228. union
  229. {
  230. uint32 argb;
  231. Components components;
  232. };
  233. }
  234. #ifndef DOXYGEN
  235. PACKED
  236. #endif
  237. ;
  238. //==============================================================================
  239. /**
  240. Represents a 24-bit RGB pixel, and can perform compositing operations on it.
  241. This is used internally by the imaging classes.
  242. @see PixelARGB
  243. */
  244. class JUCE_API PixelRGB
  245. {
  246. public:
  247. /** Creates a pixel without defining its colour. */
  248. PixelRGB() noexcept {}
  249. ~PixelRGB() noexcept {}
  250. /** Creates a pixel from a 32-bit argb value.
  251. (The argb format is that used by PixelARGB)
  252. */
  253. PixelRGB (const uint32 argb) noexcept
  254. {
  255. r = (uint8) (argb >> 16);
  256. g = (uint8) (argb >> 8);
  257. b = (uint8) (argb);
  258. }
  259. forcedinline uint32 getARGB() const noexcept { return 0xff000000 | b | (((uint32) g) << 8) | (((uint32) r) << 16); }
  260. forcedinline uint32 getUnpremultipliedARGB() const noexcept { return getARGB(); }
  261. forcedinline uint32 getRB() const noexcept { return b | (uint32) (r << 16); }
  262. forcedinline uint32 getAG() const noexcept { return (uint32) (0xff0000 | g); }
  263. forcedinline uint8 getAlpha() const noexcept { return 0xff; }
  264. forcedinline uint8 getRed() const noexcept { return r; }
  265. forcedinline uint8 getGreen() const noexcept { return g; }
  266. forcedinline uint8 getBlue() const 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. 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 getRed() const noexcept { return 0; }
  399. forcedinline uint8 getGreen() const noexcept { return 0; }
  400. forcedinline uint8 getBlue() const noexcept { return 0; }
  401. /** Blends another pixel onto this one.
  402. This takes into account the opacity of the pixel being overlaid, and blends
  403. it accordingly.
  404. */
  405. template <class Pixel>
  406. forcedinline void blend (const Pixel& src) noexcept
  407. {
  408. const int srcA = src.getAlpha();
  409. a = (uint8) ((a * (0x100 - srcA) >> 8) + srcA);
  410. }
  411. /** Blends another pixel onto this one, applying an extra multiplier to its opacity.
  412. The opacity of the pixel being overlaid is scaled by the extraAlpha factor before
  413. being used, so this can blend semi-transparently from a PixelRGB argument.
  414. */
  415. template <class Pixel>
  416. forcedinline void blend (const Pixel& src, uint32 extraAlpha) noexcept
  417. {
  418. ++extraAlpha;
  419. const int srcAlpha = (int) ((extraAlpha * src.getAlpha()) >> 8);
  420. a = (uint8) ((a * (0x100 - srcAlpha) >> 8) + srcAlpha);
  421. }
  422. /** Blends another pixel with this one, creating a colour that is somewhere
  423. between the two, as specified by the amount.
  424. */
  425. template <class Pixel>
  426. forcedinline void tween (const Pixel& src, const uint32 amount) noexcept
  427. {
  428. a += ((src.getAlpha() - a) * amount) >> 8;
  429. }
  430. /** Copies another pixel colour over this one.
  431. This doesn't blend it - this colour is simply replaced by the other one.
  432. */
  433. template <class Pixel>
  434. forcedinline void set (const Pixel& src) noexcept
  435. {
  436. a = src.getAlpha();
  437. }
  438. /** Replaces the colour's alpha value with another one. */
  439. forcedinline void setAlpha (const uint8 newAlpha) noexcept
  440. {
  441. a = newAlpha;
  442. }
  443. /** Multiplies the colour's alpha value with another one. */
  444. forcedinline void multiplyAlpha (int multiplier) noexcept
  445. {
  446. ++multiplier;
  447. a = (uint8) ((a * multiplier) >> 8);
  448. }
  449. forcedinline void multiplyAlpha (const float multiplier) noexcept
  450. {
  451. a = (uint8) (a * multiplier);
  452. }
  453. /** Sets the pixel's colour from individual components. */
  454. forcedinline void setARGB (const uint8 a_, const uint8 /*r*/, const uint8 /*g*/, const uint8 /*b*/) noexcept
  455. {
  456. a = a_;
  457. }
  458. /** Premultiplies the pixel's RGB values by its alpha. */
  459. forcedinline void premultiply() noexcept {}
  460. /** Unpremultiplies the pixel's RGB values. */
  461. forcedinline void unpremultiply() noexcept {}
  462. forcedinline void desaturate() noexcept {}
  463. //==============================================================================
  464. /** The indexes of the different components in the byte layout of this type of colour. */
  465. enum { indexA = 0 };
  466. private:
  467. //==============================================================================
  468. uint8 a : 8;
  469. }
  470. #ifndef DOXYGEN
  471. PACKED
  472. #endif
  473. ;
  474. #if JUCE_MSVC
  475. #pragma pack (pop)
  476. #endif
  477. #undef PACKED
  478. #endif // __JUCE_PIXELFORMATS_JUCEHEADER__