Audio plugin host https://kx.studio/carla
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.

681 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 (&Listener::imageDataBeingDeleted, this);
  30. }
  31. void ImagePixelData::sendDataChangeMessage()
  32. {
  33. listeners.call (&Listener::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() == (ScopedPointer<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 * 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 + x * pixelStride + y * 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 * 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 ScopedPointer<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. #if JUCE_ALLOW_STATIC_NULL_VARIABLES
  209. const Image Image::null;
  210. #endif
  211. int Image::getReferenceCount() const noexcept { return image == nullptr ? 0 : image->getSharedCount(); }
  212. int Image::getWidth() const noexcept { return image == nullptr ? 0 : image->width; }
  213. int Image::getHeight() const noexcept { return image == nullptr ? 0 : image->height; }
  214. Rectangle<int> Image::getBounds() const noexcept { return image == nullptr ? Rectangle<int>() : Rectangle<int> (image->width, image->height); }
  215. Image::PixelFormat Image::getFormat() const noexcept { return image == nullptr ? UnknownFormat : image->pixelFormat; }
  216. bool Image::isARGB() const noexcept { return getFormat() == ARGB; }
  217. bool Image::isRGB() const noexcept { return getFormat() == RGB; }
  218. bool Image::isSingleChannel() const noexcept { return getFormat() == SingleChannel; }
  219. bool Image::hasAlphaChannel() const noexcept { return getFormat() != RGB; }
  220. LowLevelGraphicsContext* Image::createLowLevelContext() const
  221. {
  222. return image == nullptr ? nullptr : image->createLowLevelContext();
  223. }
  224. void Image::duplicateIfShared()
  225. {
  226. if (getReferenceCount() > 1)
  227. image = image->clone();
  228. }
  229. Image Image::createCopy() const
  230. {
  231. if (image != nullptr)
  232. return Image (image->clone());
  233. return Image();
  234. }
  235. Image Image::rescaled (const int newWidth, const int newHeight, const Graphics::ResamplingQuality quality) const
  236. {
  237. if (image == nullptr || (image->width == newWidth && image->height == newHeight))
  238. return *this;
  239. const ScopedPointer<ImageType> type (image->createType());
  240. Image newImage (type->create (image->pixelFormat, newWidth, newHeight, hasAlphaChannel()));
  241. Graphics g (newImage);
  242. g.setImageResamplingQuality (quality);
  243. g.drawImageTransformed (*this, AffineTransform::scale (newWidth / (float) image->width,
  244. newHeight / (float) image->height), false);
  245. return newImage;
  246. }
  247. Image Image::convertedToFormat (PixelFormat newFormat) const
  248. {
  249. if (image == nullptr || newFormat == image->pixelFormat)
  250. return *this;
  251. const int w = image->width, h = image->height;
  252. const ScopedPointer<ImageType> type (image->createType());
  253. Image newImage (type->create (newFormat, w, h, false));
  254. if (newFormat == SingleChannel)
  255. {
  256. if (! hasAlphaChannel())
  257. {
  258. newImage.clear (getBounds(), Colours::black);
  259. }
  260. else
  261. {
  262. const BitmapData destData (newImage, 0, 0, w, h, BitmapData::writeOnly);
  263. const BitmapData srcData (*this, 0, 0, w, h);
  264. for (int y = 0; y < h; ++y)
  265. {
  266. const PixelARGB* const src = (const PixelARGB*) srcData.getLinePointer (y);
  267. uint8* const dst = destData.getLinePointer (y);
  268. for (int x = 0; x < w; ++x)
  269. dst[x] = src[x].getAlpha();
  270. }
  271. }
  272. }
  273. else if (image->pixelFormat == SingleChannel && newFormat == Image::ARGB)
  274. {
  275. const BitmapData destData (newImage, 0, 0, w, h, BitmapData::writeOnly);
  276. const BitmapData srcData (*this, 0, 0, w, h);
  277. for (int y = 0; y < h; ++y)
  278. {
  279. const PixelAlpha* const src = (const PixelAlpha*) srcData.getLinePointer (y);
  280. PixelARGB* const dst = (PixelARGB*) destData.getLinePointer (y);
  281. for (int x = 0; x < w; ++x)
  282. dst[x].set (src[x]);
  283. }
  284. }
  285. else
  286. {
  287. if (hasAlphaChannel())
  288. newImage.clear (getBounds());
  289. Graphics g (newImage);
  290. g.drawImageAt (*this, 0, 0);
  291. }
  292. return newImage;
  293. }
  294. NamedValueSet* Image::getProperties() const
  295. {
  296. return image == nullptr ? nullptr : &(image->userData);
  297. }
  298. //==============================================================================
  299. Image::BitmapData::BitmapData (Image& im, const int x, const int y, const int w, const int h, BitmapData::ReadWriteMode mode)
  300. : width (w), height (h)
  301. {
  302. // The BitmapData class must be given a valid image, and a valid rectangle within it!
  303. jassert (im.image != nullptr);
  304. jassert (x >= 0 && y >= 0 && w > 0 && h > 0 && x + w <= im.getWidth() && y + h <= im.getHeight());
  305. im.image->initialiseBitmapData (*this, x, y, mode);
  306. jassert (data != nullptr && pixelStride > 0 && lineStride != 0);
  307. }
  308. Image::BitmapData::BitmapData (const Image& im, const int x, const int y, const int w, const int h)
  309. : width (w), height (h)
  310. {
  311. // The BitmapData class must be given a valid image, and a valid rectangle within it!
  312. jassert (im.image != nullptr);
  313. jassert (x >= 0 && y >= 0 && w > 0 && h > 0 && x + w <= im.getWidth() && y + h <= im.getHeight());
  314. im.image->initialiseBitmapData (*this, x, y, readOnly);
  315. jassert (data != nullptr && pixelStride > 0 && lineStride != 0);
  316. }
  317. Image::BitmapData::BitmapData (const Image& im, BitmapData::ReadWriteMode mode)
  318. : width (im.getWidth()),
  319. height (im.getHeight())
  320. {
  321. // The BitmapData class must be given a valid image!
  322. jassert (im.image != nullptr);
  323. im.image->initialiseBitmapData (*this, 0, 0, mode);
  324. jassert (data != nullptr && pixelStride > 0 && lineStride != 0);
  325. }
  326. Image::BitmapData::~BitmapData()
  327. {
  328. }
  329. Colour Image::BitmapData::getPixelColour (const int x, const int y) const noexcept
  330. {
  331. jassert (isPositiveAndBelow (x, width) && isPositiveAndBelow (y, height));
  332. const uint8* const pixel = getPixelPointer (x, y);
  333. switch (pixelFormat)
  334. {
  335. case Image::ARGB: return Colour ( ((const PixelARGB*) pixel)->getUnpremultiplied());
  336. case Image::RGB: return Colour (*((const PixelRGB*) pixel));
  337. case Image::SingleChannel: return Colour (*((const PixelAlpha*) pixel));
  338. default: jassertfalse; break;
  339. }
  340. return Colour();
  341. }
  342. void Image::BitmapData::setPixelColour (const int x, const int y, Colour colour) const noexcept
  343. {
  344. jassert (isPositiveAndBelow (x, width) && isPositiveAndBelow (y, height));
  345. uint8* const pixel = getPixelPointer (x, y);
  346. const PixelARGB col (colour.getPixelARGB());
  347. switch (pixelFormat)
  348. {
  349. case Image::ARGB: ((PixelARGB*) pixel)->set (col); break;
  350. case Image::RGB: ((PixelRGB*) pixel)->set (col); break;
  351. case Image::SingleChannel: ((PixelAlpha*) pixel)->set (col); break;
  352. default: jassertfalse; break;
  353. }
  354. }
  355. //==============================================================================
  356. void Image::clear (const Rectangle<int>& area, Colour colourToClearTo)
  357. {
  358. if (image != nullptr)
  359. {
  360. const ScopedPointer<LowLevelGraphicsContext> g (image->createLowLevelContext());
  361. g->setFill (colourToClearTo);
  362. g->fillRect (area, true);
  363. }
  364. }
  365. //==============================================================================
  366. Colour Image::getPixelAt (const int x, const int y) const
  367. {
  368. if (isPositiveAndBelow (x, getWidth()) && isPositiveAndBelow (y, getHeight()))
  369. {
  370. const BitmapData srcData (*this, x, y, 1, 1);
  371. return srcData.getPixelColour (0, 0);
  372. }
  373. return Colour();
  374. }
  375. void Image::setPixelAt (const int x, const int y, Colour colour)
  376. {
  377. if (isPositiveAndBelow (x, getWidth()) && isPositiveAndBelow (y, getHeight()))
  378. {
  379. const BitmapData destData (*this, x, y, 1, 1, BitmapData::writeOnly);
  380. destData.setPixelColour (0, 0, colour);
  381. }
  382. }
  383. void Image::multiplyAlphaAt (const int x, const int y, const float multiplier)
  384. {
  385. if (isPositiveAndBelow (x, getWidth()) && isPositiveAndBelow (y, getHeight())
  386. && hasAlphaChannel())
  387. {
  388. const BitmapData destData (*this, x, y, 1, 1, BitmapData::readWrite);
  389. if (isARGB())
  390. ((PixelARGB*) destData.data)->multiplyAlpha (multiplier);
  391. else
  392. *(destData.data) = (uint8) (*(destData.data) * multiplier);
  393. }
  394. }
  395. template <class PixelType>
  396. struct PixelIterator
  397. {
  398. template <class PixelOperation>
  399. static void iterate (const Image::BitmapData& data, const PixelOperation& pixelOp)
  400. {
  401. for (int y = 0; y < data.height; ++y)
  402. {
  403. uint8* p = data.getLinePointer (y);
  404. for (int x = 0; x < data.width; ++x)
  405. {
  406. pixelOp (*(PixelType*) p);
  407. p += data.pixelStride;
  408. }
  409. }
  410. }
  411. };
  412. template <class PixelOperation>
  413. static void performPixelOp (const Image::BitmapData& data, const PixelOperation& pixelOp)
  414. {
  415. switch (data.pixelFormat)
  416. {
  417. case Image::ARGB: PixelIterator<PixelARGB> ::iterate (data, pixelOp); break;
  418. case Image::RGB: PixelIterator<PixelRGB> ::iterate (data, pixelOp); break;
  419. case Image::SingleChannel: PixelIterator<PixelAlpha>::iterate (data, pixelOp); break;
  420. default: jassertfalse; break;
  421. }
  422. }
  423. struct AlphaMultiplyOp
  424. {
  425. float alpha;
  426. template <class PixelType>
  427. void operator() (PixelType& pixel) const
  428. {
  429. pixel.multiplyAlpha (alpha);
  430. }
  431. };
  432. void Image::multiplyAllAlphas (const float amountToMultiplyBy)
  433. {
  434. jassert (hasAlphaChannel());
  435. const BitmapData destData (*this, 0, 0, getWidth(), getHeight(), BitmapData::readWrite);
  436. performPixelOp (destData, AlphaMultiplyOp { amountToMultiplyBy });
  437. }
  438. struct DesaturateOp
  439. {
  440. template <class PixelType>
  441. void operator() (PixelType& pixel) const
  442. {
  443. pixel.desaturate();
  444. }
  445. };
  446. void Image::desaturate()
  447. {
  448. if (isARGB() || isRGB())
  449. {
  450. const BitmapData destData (*this, 0, 0, getWidth(), getHeight(), BitmapData::readWrite);
  451. performPixelOp (destData, DesaturateOp());
  452. }
  453. }
  454. void Image::createSolidAreaMask (RectangleList<int>& result, const float alphaThreshold) const
  455. {
  456. if (hasAlphaChannel())
  457. {
  458. const uint8 threshold = (uint8) jlimit (0, 255, roundToInt (alphaThreshold * 255.0f));
  459. SparseSet<int> pixelsOnRow;
  460. const BitmapData srcData (*this, 0, 0, getWidth(), getHeight());
  461. for (int y = 0; y < srcData.height; ++y)
  462. {
  463. pixelsOnRow.clear();
  464. const uint8* lineData = srcData.getLinePointer (y);
  465. if (isARGB())
  466. {
  467. for (int x = 0; x < srcData.width; ++x)
  468. {
  469. if (((const PixelARGB*) lineData)->getAlpha() >= threshold)
  470. pixelsOnRow.addRange (Range<int> (x, x + 1));
  471. lineData += srcData.pixelStride;
  472. }
  473. }
  474. else
  475. {
  476. for (int x = 0; x < srcData.width; ++x)
  477. {
  478. if (*lineData >= threshold)
  479. pixelsOnRow.addRange (Range<int> (x, x + 1));
  480. lineData += srcData.pixelStride;
  481. }
  482. }
  483. for (int i = 0; i < pixelsOnRow.getNumRanges(); ++i)
  484. {
  485. const Range<int> range (pixelsOnRow.getRange (i));
  486. result.add (Rectangle<int> (range.getStart(), y, range.getLength(), 1));
  487. }
  488. result.consolidate();
  489. }
  490. }
  491. else
  492. {
  493. result.add (0, 0, getWidth(), getHeight());
  494. }
  495. }
  496. void Image::moveImageSection (int dx, int dy,
  497. int sx, int sy,
  498. int w, int h)
  499. {
  500. if (dx < 0)
  501. {
  502. w += dx;
  503. sx -= dx;
  504. dx = 0;
  505. }
  506. if (dy < 0)
  507. {
  508. h += dy;
  509. sy -= dy;
  510. dy = 0;
  511. }
  512. if (sx < 0)
  513. {
  514. w += sx;
  515. dx -= sx;
  516. sx = 0;
  517. }
  518. if (sy < 0)
  519. {
  520. h += sy;
  521. dy -= sy;
  522. sy = 0;
  523. }
  524. const int minX = jmin (dx, sx);
  525. const int minY = jmin (dy, sy);
  526. w = jmin (w, getWidth() - jmax (sx, dx));
  527. h = jmin (h, getHeight() - jmax (sy, dy));
  528. if (w > 0 && h > 0)
  529. {
  530. const int maxX = jmax (dx, sx) + w;
  531. const int maxY = jmax (dy, sy) + h;
  532. const BitmapData destData (*this, minX, minY, maxX - minX, maxY - minY, BitmapData::readWrite);
  533. uint8* dst = destData.getPixelPointer (dx - minX, dy - minY);
  534. const uint8* src = destData.getPixelPointer (sx - minX, sy - minY);
  535. const size_t lineSize = (size_t) (destData.pixelStride * w);
  536. if (dy > sy)
  537. {
  538. while (--h >= 0)
  539. {
  540. const int offset = h * destData.lineStride;
  541. memmove (dst + offset, src + offset, lineSize);
  542. }
  543. }
  544. else if (dst != src)
  545. {
  546. while (--h >= 0)
  547. {
  548. memmove (dst, src, lineSize);
  549. dst += destData.lineStride;
  550. src += destData.lineStride;
  551. }
  552. }
  553. }
  554. }
  555. } // namespace juce