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.

676 lines
21KB

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