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.

679 lines
21KB

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