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.

644 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. ImagePixelData::ImagePixelData (const Image::PixelFormat format, const int w, const int h)
  18. : pixelFormat (format), width (w), height (h)
  19. {
  20. jassert (format == Image::RGB || format == Image::ARGB || format == Image::SingleChannel);
  21. jassert (w > 0 && h > 0); // It's illegal to create a zero-sized image!
  22. }
  23. ImagePixelData::~ImagePixelData()
  24. {
  25. }
  26. //==============================================================================
  27. ImageType::ImageType() {}
  28. ImageType::~ImageType() {}
  29. Image ImageType::convert (const Image& source) const
  30. {
  31. if (source.isNull() || getTypeID() == (ScopedPointer<ImageType> (source.getPixelData()->createType())->getTypeID()))
  32. return source;
  33. const Image::BitmapData src (source, Image::BitmapData::readOnly);
  34. Image newImage (create (src.pixelFormat, src.width, src.height, false));
  35. Image::BitmapData dest (newImage, Image::BitmapData::writeOnly);
  36. jassert (src.pixelStride == dest.pixelStride && src.pixelFormat == dest.pixelFormat);
  37. for (int y = 0; y < dest.height; ++y)
  38. memcpy (dest.getLinePointer (y), src.getLinePointer (y), (size_t) dest.lineStride);
  39. return newImage;
  40. }
  41. //==============================================================================
  42. class SoftwarePixelData : public ImagePixelData
  43. {
  44. public:
  45. SoftwarePixelData (const Image::PixelFormat format_, const int w, const int h, const bool clearImage)
  46. : ImagePixelData (format_, w, h),
  47. pixelStride (format_ == Image::RGB ? 3 : ((format_ == Image::ARGB) ? 4 : 1)),
  48. lineStride ((pixelStride * jmax (1, w) + 3) & ~3)
  49. {
  50. imageData.allocate ((size_t) (lineStride * jmax (1, h)), clearImage);
  51. }
  52. LowLevelGraphicsContext* createLowLevelContext() override
  53. {
  54. return new LowLevelGraphicsSoftwareRenderer (Image (this));
  55. }
  56. void initialiseBitmapData (Image::BitmapData& bitmap, int x, int y, Image::BitmapData::ReadWriteMode) override
  57. {
  58. bitmap.data = imageData + x * pixelStride + y * lineStride;
  59. bitmap.pixelFormat = pixelFormat;
  60. bitmap.lineStride = lineStride;
  61. bitmap.pixelStride = pixelStride;
  62. }
  63. ImagePixelData* clone() override
  64. {
  65. SoftwarePixelData* s = new SoftwarePixelData (pixelFormat, width, height, false);
  66. memcpy (s->imageData, imageData, (size_t) (lineStride * height));
  67. return s;
  68. }
  69. ImageType* createType() const override { return new SoftwareImageType(); }
  70. private:
  71. HeapBlock<uint8> imageData;
  72. const int pixelStride, lineStride;
  73. JUCE_LEAK_DETECTOR (SoftwarePixelData)
  74. };
  75. SoftwareImageType::SoftwareImageType() {}
  76. SoftwareImageType::~SoftwareImageType() {}
  77. ImagePixelData::Ptr SoftwareImageType::create (Image::PixelFormat format, int width, int height, bool clearImage) const
  78. {
  79. return new SoftwarePixelData (format, width, height, clearImage);
  80. }
  81. int SoftwareImageType::getTypeID() const
  82. {
  83. return 2;
  84. }
  85. //==============================================================================
  86. NativeImageType::NativeImageType() {}
  87. NativeImageType::~NativeImageType() {}
  88. int NativeImageType::getTypeID() const
  89. {
  90. return 1;
  91. }
  92. #if JUCE_WINDOWS || JUCE_LINUX
  93. ImagePixelData::Ptr NativeImageType::create (Image::PixelFormat format, int width, int height, bool clearImage) const
  94. {
  95. return new SoftwarePixelData (format, width, height, clearImage);
  96. }
  97. #endif
  98. //==============================================================================
  99. class SubsectionPixelData : public ImagePixelData
  100. {
  101. public:
  102. SubsectionPixelData (ImagePixelData* const im, const Rectangle<int>& r)
  103. : ImagePixelData (im->pixelFormat, r.getWidth(), r.getHeight()),
  104. image (im), area (r)
  105. {
  106. }
  107. LowLevelGraphicsContext* createLowLevelContext() override
  108. {
  109. LowLevelGraphicsContext* g = image->createLowLevelContext();
  110. g->clipToRectangle (area);
  111. g->setOrigin (area.getX(), area.getY());
  112. return g;
  113. }
  114. void initialiseBitmapData (Image::BitmapData& bitmap, int x, int y, Image::BitmapData::ReadWriteMode mode) override
  115. {
  116. image->initialiseBitmapData (bitmap, x + area.getX(), y + area.getY(), mode);
  117. }
  118. ImagePixelData* clone() override
  119. {
  120. jassert (getReferenceCount() > 0); // (This method can't be used on an unowned pointer, as it will end up self-deleting)
  121. const ScopedPointer<ImageType> type (image->createType());
  122. Image newImage (type->create (pixelFormat, area.getWidth(), area.getHeight(), pixelFormat != Image::RGB));
  123. {
  124. Graphics g (newImage);
  125. g.drawImageAt (Image (this), 0, 0);
  126. }
  127. newImage.getPixelData()->incReferenceCount();
  128. return newImage.getPixelData();
  129. }
  130. ImageType* createType() const override { return image->createType(); }
  131. private:
  132. const ImagePixelData::Ptr image;
  133. const Rectangle<int> area;
  134. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SubsectionPixelData)
  135. };
  136. Image Image::getClippedImage (const Rectangle<int>& area) const
  137. {
  138. if (area.contains (getBounds()))
  139. return *this;
  140. const Rectangle<int> validArea (area.getIntersection (getBounds()));
  141. return Image (validArea.isEmpty() ? nullptr : new SubsectionPixelData (image, validArea));
  142. }
  143. //==============================================================================
  144. Image::Image()
  145. {
  146. }
  147. Image::Image (ImagePixelData* const instance)
  148. : image (instance)
  149. {
  150. }
  151. Image::Image (const PixelFormat format, int width, int height, bool clearImage)
  152. : image (NativeImageType().create (format, width, height, clearImage))
  153. {
  154. }
  155. Image::Image (const PixelFormat format, int width, int height, bool clearImage, const ImageType& type)
  156. : image (type.create (format, width, height, clearImage))
  157. {
  158. }
  159. Image::Image (const Image& other)
  160. : image (other.image)
  161. {
  162. }
  163. Image& Image::operator= (const Image& other)
  164. {
  165. image = other.image;
  166. return *this;
  167. }
  168. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  169. Image::Image (Image&& other) noexcept
  170. : image (static_cast <ImagePixelData::Ptr&&> (other.image))
  171. {
  172. }
  173. Image& Image::operator= (Image&& other) noexcept
  174. {
  175. image = static_cast <ImagePixelData::Ptr&&> (other.image);
  176. return *this;
  177. }
  178. #endif
  179. Image::~Image()
  180. {
  181. }
  182. const Image Image::null;
  183. int Image::getReferenceCount() const noexcept { return image == nullptr ? 0 : image->getReferenceCount(); }
  184. int Image::getWidth() const noexcept { return image == nullptr ? 0 : image->width; }
  185. int Image::getHeight() const noexcept { return image == nullptr ? 0 : image->height; }
  186. Rectangle<int> Image::getBounds() const noexcept { return image == nullptr ? Rectangle<int>() : Rectangle<int> (image->width, image->height); }
  187. Image::PixelFormat Image::getFormat() const noexcept { return image == nullptr ? UnknownFormat : image->pixelFormat; }
  188. bool Image::isARGB() const noexcept { return getFormat() == ARGB; }
  189. bool Image::isRGB() const noexcept { return getFormat() == RGB; }
  190. bool Image::isSingleChannel() const noexcept { return getFormat() == SingleChannel; }
  191. bool Image::hasAlphaChannel() const noexcept { return getFormat() != RGB; }
  192. LowLevelGraphicsContext* Image::createLowLevelContext() const
  193. {
  194. return image == nullptr ? nullptr : image->createLowLevelContext();
  195. }
  196. void Image::duplicateIfShared()
  197. {
  198. if (image != nullptr && image->getReferenceCount() > 1)
  199. image = image->clone();
  200. }
  201. Image Image::createCopy() const
  202. {
  203. return Image (image != nullptr ? image->clone() : nullptr);
  204. }
  205. Image Image::rescaled (const int newWidth, const int newHeight, const Graphics::ResamplingQuality quality) const
  206. {
  207. if (image == nullptr || (image->width == newWidth && image->height == newHeight))
  208. return *this;
  209. const ScopedPointer<ImageType> type (image->createType());
  210. Image newImage (type->create (image->pixelFormat, newWidth, newHeight, hasAlphaChannel()));
  211. Graphics g (newImage);
  212. g.setImageResamplingQuality (quality);
  213. g.drawImage (*this, 0, 0, newWidth, newHeight, 0, 0, image->width, image->height, false);
  214. return newImage;
  215. }
  216. Image Image::convertedToFormat (PixelFormat newFormat) const
  217. {
  218. if (image == nullptr || newFormat == image->pixelFormat)
  219. return *this;
  220. const int w = image->width, h = image->height;
  221. const ScopedPointer<ImageType> type (image->createType());
  222. Image newImage (type->create (newFormat, w, h, false));
  223. if (newFormat == SingleChannel)
  224. {
  225. if (! hasAlphaChannel())
  226. {
  227. newImage.clear (getBounds(), Colours::black);
  228. }
  229. else
  230. {
  231. const BitmapData destData (newImage, 0, 0, w, h, BitmapData::writeOnly);
  232. const BitmapData srcData (*this, 0, 0, w, h);
  233. for (int y = 0; y < h; ++y)
  234. {
  235. const PixelARGB* const src = (const PixelARGB*) srcData.getLinePointer (y);
  236. uint8* const dst = destData.getLinePointer (y);
  237. for (int x = 0; x < w; ++x)
  238. dst[x] = src[x].getAlpha();
  239. }
  240. }
  241. }
  242. else if (image->pixelFormat == SingleChannel && newFormat == Image::ARGB)
  243. {
  244. const BitmapData destData (newImage, 0, 0, w, h, BitmapData::writeOnly);
  245. const BitmapData srcData (*this, 0, 0, w, h);
  246. for (int y = 0; y < h; ++y)
  247. {
  248. const PixelAlpha* const src = (const PixelAlpha*) srcData.getLinePointer (y);
  249. PixelARGB* const dst = (PixelARGB*) destData.getLinePointer (y);
  250. for (int x = 0; x < w; ++x)
  251. dst[x].set (src[x]);
  252. }
  253. }
  254. else
  255. {
  256. if (hasAlphaChannel())
  257. newImage.clear (getBounds());
  258. Graphics g (newImage);
  259. g.drawImageAt (*this, 0, 0);
  260. }
  261. return newImage;
  262. }
  263. NamedValueSet* Image::getProperties() const
  264. {
  265. return image == nullptr ? nullptr : &(image->userData);
  266. }
  267. //==============================================================================
  268. Image::BitmapData::BitmapData (Image& im, const int x, const int y, const int w, const int h, BitmapData::ReadWriteMode mode)
  269. : width (w), height (h)
  270. {
  271. // The BitmapData class must be given a valid image, and a valid rectangle within it!
  272. jassert (im.image != nullptr);
  273. jassert (x >= 0 && y >= 0 && w > 0 && h > 0 && x + w <= im.getWidth() && y + h <= im.getHeight());
  274. im.image->initialiseBitmapData (*this, x, y, mode);
  275. jassert (data != nullptr && pixelStride > 0 && lineStride != 0);
  276. }
  277. Image::BitmapData::BitmapData (const Image& im, const int x, const int y, const int w, const int h)
  278. : width (w), height (h)
  279. {
  280. // The BitmapData class must be given a valid image, and a valid rectangle within it!
  281. jassert (im.image != nullptr);
  282. jassert (x >= 0 && y >= 0 && w > 0 && h > 0 && x + w <= im.getWidth() && y + h <= im.getHeight());
  283. im.image->initialiseBitmapData (*this, x, y, readOnly);
  284. jassert (data != nullptr && pixelStride > 0 && lineStride != 0);
  285. }
  286. Image::BitmapData::BitmapData (const Image& im, BitmapData::ReadWriteMode mode)
  287. : width (im.getWidth()),
  288. height (im.getHeight())
  289. {
  290. // The BitmapData class must be given a valid image!
  291. jassert (im.image != nullptr);
  292. im.image->initialiseBitmapData (*this, 0, 0, mode);
  293. jassert (data != nullptr && pixelStride > 0 && lineStride != 0);
  294. }
  295. Image::BitmapData::~BitmapData()
  296. {
  297. }
  298. Colour Image::BitmapData::getPixelColour (const int x, const int y) const noexcept
  299. {
  300. jassert (isPositiveAndBelow (x, width) && isPositiveAndBelow (y, height));
  301. const uint8* const pixel = getPixelPointer (x, y);
  302. switch (pixelFormat)
  303. {
  304. case Image::ARGB: return Colour (((const PixelARGB*) pixel)->getUnpremultipliedARGB());
  305. case Image::RGB: return Colour (((const PixelRGB*) pixel)->getUnpremultipliedARGB());
  306. case Image::SingleChannel: return Colour (((const PixelAlpha*) pixel)->getUnpremultipliedARGB());
  307. default: jassertfalse; break;
  308. }
  309. return Colour();
  310. }
  311. void Image::BitmapData::setPixelColour (const int x, const int y, Colour colour) const noexcept
  312. {
  313. jassert (isPositiveAndBelow (x, width) && isPositiveAndBelow (y, height));
  314. uint8* const pixel = getPixelPointer (x, y);
  315. const PixelARGB col (colour.getPixelARGB());
  316. switch (pixelFormat)
  317. {
  318. case Image::ARGB: ((PixelARGB*) pixel)->set (col); break;
  319. case Image::RGB: ((PixelRGB*) pixel)->set (col); break;
  320. case Image::SingleChannel: ((PixelAlpha*) pixel)->set (col); break;
  321. default: jassertfalse; break;
  322. }
  323. }
  324. //==============================================================================
  325. void Image::clear (const Rectangle<int>& area, Colour colourToClearTo)
  326. {
  327. const ScopedPointer<LowLevelGraphicsContext> g (image->createLowLevelContext());
  328. g->setFill (colourToClearTo);
  329. g->fillRect (area, true);
  330. }
  331. //==============================================================================
  332. Colour Image::getPixelAt (const int x, const int y) const
  333. {
  334. if (isPositiveAndBelow (x, getWidth()) && isPositiveAndBelow (y, getHeight()))
  335. {
  336. const BitmapData srcData (*this, x, y, 1, 1);
  337. return srcData.getPixelColour (0, 0);
  338. }
  339. return Colour();
  340. }
  341. void Image::setPixelAt (const int x, const int y, Colour colour)
  342. {
  343. if (isPositiveAndBelow (x, getWidth()) && isPositiveAndBelow (y, getHeight()))
  344. {
  345. const BitmapData destData (*this, x, y, 1, 1, BitmapData::writeOnly);
  346. destData.setPixelColour (0, 0, colour);
  347. }
  348. }
  349. void Image::multiplyAlphaAt (const int x, const int y, const float multiplier)
  350. {
  351. if (isPositiveAndBelow (x, getWidth()) && isPositiveAndBelow (y, getHeight())
  352. && hasAlphaChannel())
  353. {
  354. const BitmapData destData (*this, x, y, 1, 1, BitmapData::readWrite);
  355. if (isARGB())
  356. ((PixelARGB*) destData.data)->multiplyAlpha (multiplier);
  357. else
  358. *(destData.data) = (uint8) (*(destData.data) * multiplier);
  359. }
  360. }
  361. template <class PixelType>
  362. struct PixelIterator
  363. {
  364. template <class PixelOperation>
  365. static void iterate (const Image::BitmapData& data, const PixelOperation& pixelOp)
  366. {
  367. for (int y = 0; y < data.height; ++y)
  368. {
  369. uint8* p = data.getLinePointer (y);
  370. for (int x = 0; x < data.width; ++x)
  371. {
  372. pixelOp (*(PixelType*) p);
  373. p += data.pixelStride;
  374. }
  375. }
  376. }
  377. };
  378. template <class PixelOperation>
  379. static void performPixelOp (const Image::BitmapData& data, const PixelOperation& pixelOp)
  380. {
  381. switch (data.pixelFormat)
  382. {
  383. case Image::ARGB: PixelIterator<PixelARGB> ::iterate (data, pixelOp); break;
  384. case Image::RGB: PixelIterator<PixelRGB> ::iterate (data, pixelOp); break;
  385. case Image::SingleChannel: PixelIterator<PixelAlpha>::iterate (data, pixelOp); break;
  386. default: jassertfalse; break;
  387. }
  388. }
  389. struct AlphaMultiplyOp
  390. {
  391. AlphaMultiplyOp (float alpha_) noexcept : alpha (alpha_) {}
  392. const float alpha;
  393. template <class PixelType>
  394. void operator() (PixelType& pixel) const
  395. {
  396. pixel.multiplyAlpha (alpha);
  397. }
  398. JUCE_DECLARE_NON_COPYABLE (AlphaMultiplyOp)
  399. };
  400. void Image::multiplyAllAlphas (const float amountToMultiplyBy)
  401. {
  402. jassert (hasAlphaChannel());
  403. const BitmapData destData (*this, 0, 0, getWidth(), getHeight(), BitmapData::readWrite);
  404. performPixelOp (destData, AlphaMultiplyOp (amountToMultiplyBy));
  405. }
  406. struct DesaturateOp
  407. {
  408. template <class PixelType>
  409. void operator() (PixelType& pixel) const
  410. {
  411. pixel.desaturate();
  412. }
  413. };
  414. void Image::desaturate()
  415. {
  416. if (isARGB() || isRGB())
  417. {
  418. const BitmapData destData (*this, 0, 0, getWidth(), getHeight(), BitmapData::readWrite);
  419. performPixelOp (destData, DesaturateOp());
  420. }
  421. }
  422. void Image::createSolidAreaMask (RectangleList<int>& result, const float alphaThreshold) const
  423. {
  424. if (hasAlphaChannel())
  425. {
  426. const uint8 threshold = (uint8) jlimit (0, 255, roundToInt (alphaThreshold * 255.0f));
  427. SparseSet<int> pixelsOnRow;
  428. const BitmapData srcData (*this, 0, 0, getWidth(), getHeight());
  429. for (int y = 0; y < srcData.height; ++y)
  430. {
  431. pixelsOnRow.clear();
  432. const uint8* lineData = srcData.getLinePointer (y);
  433. if (isARGB())
  434. {
  435. for (int x = 0; x < srcData.width; ++x)
  436. {
  437. if (((const PixelARGB*) lineData)->getAlpha() >= threshold)
  438. pixelsOnRow.addRange (Range<int> (x, x + 1));
  439. lineData += srcData.pixelStride;
  440. }
  441. }
  442. else
  443. {
  444. for (int x = 0; x < srcData.width; ++x)
  445. {
  446. if (*lineData >= threshold)
  447. pixelsOnRow.addRange (Range<int> (x, x + 1));
  448. lineData += srcData.pixelStride;
  449. }
  450. }
  451. for (int i = 0; i < pixelsOnRow.getNumRanges(); ++i)
  452. {
  453. const Range<int> range (pixelsOnRow.getRange (i));
  454. result.add (Rectangle<int> (range.getStart(), y, range.getLength(), 1));
  455. }
  456. result.consolidate();
  457. }
  458. }
  459. else
  460. {
  461. result.add (0, 0, getWidth(), getHeight());
  462. }
  463. }
  464. void Image::moveImageSection (int dx, int dy,
  465. int sx, int sy,
  466. int w, int h)
  467. {
  468. if (dx < 0)
  469. {
  470. w += dx;
  471. sx -= dx;
  472. dx = 0;
  473. }
  474. if (dy < 0)
  475. {
  476. h += dy;
  477. sy -= dy;
  478. dy = 0;
  479. }
  480. if (sx < 0)
  481. {
  482. w += sx;
  483. dx -= sx;
  484. sx = 0;
  485. }
  486. if (sy < 0)
  487. {
  488. h += sy;
  489. dy -= sy;
  490. sy = 0;
  491. }
  492. const int minX = jmin (dx, sx);
  493. const int minY = jmin (dy, sy);
  494. w = jmin (w, getWidth() - jmax (sx, dx));
  495. h = jmin (h, getHeight() - jmax (sy, dy));
  496. if (w > 0 && h > 0)
  497. {
  498. const int maxX = jmax (dx, sx) + w;
  499. const int maxY = jmax (dy, sy) + h;
  500. const BitmapData destData (*this, minX, minY, maxX - minX, maxY - minY, BitmapData::readWrite);
  501. uint8* dst = destData.getPixelPointer (dx - minX, dy - minY);
  502. const uint8* src = destData.getPixelPointer (sx - minX, sy - minY);
  503. const size_t lineSize = (size_t) (destData.pixelStride * w);
  504. if (dy > sy)
  505. {
  506. while (--h >= 0)
  507. {
  508. const int offset = h * destData.lineStride;
  509. memmove (dst + offset, src + offset, lineSize);
  510. }
  511. }
  512. else if (dst != src)
  513. {
  514. while (--h >= 0)
  515. {
  516. memmove (dst, src, lineSize);
  517. dst += destData.lineStride;
  518. src += destData.lineStride;
  519. }
  520. }
  521. }
  522. }