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.

677 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::Ptr 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. sourceImage (im), area (r)
  125. {
  126. }
  127. LowLevelGraphicsContext* createLowLevelContext() override
  128. {
  129. LowLevelGraphicsContext* g = sourceImage->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. sourceImage->initialiseBitmapData (bitmap, x + area.getX(), y + area.getY(), mode);
  137. if (mode != Image::BitmapData::readOnly)
  138. sendDataChangeMessage();
  139. }
  140. ImagePixelData::Ptr 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 (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. return newImage.getPixelData();
  150. }
  151. ImageType* createType() const override { return sourceImage->createType(); }
  152. /* as we always hold a reference to image, don't double count */
  153. int getSharedCount() const noexcept override { return getReferenceCount() + sourceImage->getSharedCount() - 1; }
  154. private:
  155. friend class Image;
  156. const ImagePixelData::Ptr sourceImage;
  157. const Rectangle<int> area;
  158. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SubsectionPixelData)
  159. };
  160. Image Image::getClippedImage (const Rectangle<int>& area) const
  161. {
  162. if (area.contains (getBounds()))
  163. return *this;
  164. const Rectangle<int> validArea (area.getIntersection (getBounds()));
  165. return Image (validArea.isEmpty() ? nullptr : new SubsectionPixelData (image, validArea));
  166. }
  167. //==============================================================================
  168. Image::Image() noexcept
  169. {
  170. }
  171. Image::Image (ImagePixelData* const instance) noexcept
  172. : image (instance)
  173. {
  174. }
  175. Image::Image (const PixelFormat format, int width, int height, bool clearImage)
  176. : image (NativeImageType().create (format, width, height, clearImage))
  177. {
  178. }
  179. Image::Image (const PixelFormat format, int width, int height, bool clearImage, const ImageType& type)
  180. : image (type.create (format, width, height, clearImage))
  181. {
  182. }
  183. Image::Image (const Image& other) noexcept
  184. : image (other.image)
  185. {
  186. }
  187. Image& Image::operator= (const Image& other)
  188. {
  189. image = other.image;
  190. return *this;
  191. }
  192. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  193. Image::Image (Image&& other) noexcept
  194. : image (static_cast<ImagePixelData::Ptr&&> (other.image))
  195. {
  196. }
  197. Image& Image::operator= (Image&& other) noexcept
  198. {
  199. image = static_cast<ImagePixelData::Ptr&&> (other.image);
  200. return *this;
  201. }
  202. #endif
  203. Image::~Image()
  204. {
  205. }
  206. #if JUCE_ALLOW_STATIC_NULL_VARIABLES
  207. const Image Image::null;
  208. #endif
  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 ScopedPointer<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 ScopedPointer<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. const ScopedPointer<LowLevelGraphicsContext> g (image->createLowLevelContext());
  357. g->setFill (colourToClearTo);
  358. g->fillRect (area, true);
  359. }
  360. //==============================================================================
  361. Colour Image::getPixelAt (const int x, const int y) const
  362. {
  363. if (isPositiveAndBelow (x, getWidth()) && isPositiveAndBelow (y, getHeight()))
  364. {
  365. const BitmapData srcData (*this, x, y, 1, 1);
  366. return srcData.getPixelColour (0, 0);
  367. }
  368. return Colour();
  369. }
  370. void Image::setPixelAt (const int x, const int y, Colour colour)
  371. {
  372. if (isPositiveAndBelow (x, getWidth()) && isPositiveAndBelow (y, getHeight()))
  373. {
  374. const BitmapData destData (*this, x, y, 1, 1, BitmapData::writeOnly);
  375. destData.setPixelColour (0, 0, colour);
  376. }
  377. }
  378. void Image::multiplyAlphaAt (const int x, const int y, const float multiplier)
  379. {
  380. if (isPositiveAndBelow (x, getWidth()) && isPositiveAndBelow (y, getHeight())
  381. && hasAlphaChannel())
  382. {
  383. const BitmapData destData (*this, x, y, 1, 1, BitmapData::readWrite);
  384. if (isARGB())
  385. ((PixelARGB*) destData.data)->multiplyAlpha (multiplier);
  386. else
  387. *(destData.data) = (uint8) (*(destData.data) * multiplier);
  388. }
  389. }
  390. template <class PixelType>
  391. struct PixelIterator
  392. {
  393. template <class PixelOperation>
  394. static void iterate (const Image::BitmapData& data, const PixelOperation& pixelOp)
  395. {
  396. for (int y = 0; y < data.height; ++y)
  397. {
  398. uint8* p = data.getLinePointer (y);
  399. for (int x = 0; x < data.width; ++x)
  400. {
  401. pixelOp (*(PixelType*) p);
  402. p += data.pixelStride;
  403. }
  404. }
  405. }
  406. };
  407. template <class PixelOperation>
  408. static void performPixelOp (const Image::BitmapData& data, const PixelOperation& pixelOp)
  409. {
  410. switch (data.pixelFormat)
  411. {
  412. case Image::ARGB: PixelIterator<PixelARGB> ::iterate (data, pixelOp); break;
  413. case Image::RGB: PixelIterator<PixelRGB> ::iterate (data, pixelOp); break;
  414. case Image::SingleChannel: PixelIterator<PixelAlpha>::iterate (data, pixelOp); break;
  415. default: jassertfalse; break;
  416. }
  417. }
  418. struct AlphaMultiplyOp
  419. {
  420. AlphaMultiplyOp (float alpha_) noexcept : alpha (alpha_) {}
  421. const float alpha;
  422. template <class PixelType>
  423. void operator() (PixelType& pixel) const
  424. {
  425. pixel.multiplyAlpha (alpha);
  426. }
  427. JUCE_DECLARE_NON_COPYABLE (AlphaMultiplyOp)
  428. };
  429. void Image::multiplyAllAlphas (const float amountToMultiplyBy)
  430. {
  431. jassert (hasAlphaChannel());
  432. const BitmapData destData (*this, 0, 0, getWidth(), getHeight(), BitmapData::readWrite);
  433. performPixelOp (destData, AlphaMultiplyOp (amountToMultiplyBy));
  434. }
  435. struct DesaturateOp
  436. {
  437. template <class PixelType>
  438. void operator() (PixelType& pixel) const
  439. {
  440. pixel.desaturate();
  441. }
  442. };
  443. void Image::desaturate()
  444. {
  445. if (isARGB() || isRGB())
  446. {
  447. const BitmapData destData (*this, 0, 0, getWidth(), getHeight(), BitmapData::readWrite);
  448. performPixelOp (destData, DesaturateOp());
  449. }
  450. }
  451. void Image::createSolidAreaMask (RectangleList<int>& result, const float alphaThreshold) const
  452. {
  453. if (hasAlphaChannel())
  454. {
  455. const uint8 threshold = (uint8) jlimit (0, 255, roundToInt (alphaThreshold * 255.0f));
  456. SparseSet<int> pixelsOnRow;
  457. const BitmapData srcData (*this, 0, 0, getWidth(), getHeight());
  458. for (int y = 0; y < srcData.height; ++y)
  459. {
  460. pixelsOnRow.clear();
  461. const uint8* lineData = srcData.getLinePointer (y);
  462. if (isARGB())
  463. {
  464. for (int x = 0; x < srcData.width; ++x)
  465. {
  466. if (((const PixelARGB*) lineData)->getAlpha() >= threshold)
  467. pixelsOnRow.addRange (Range<int> (x, x + 1));
  468. lineData += srcData.pixelStride;
  469. }
  470. }
  471. else
  472. {
  473. for (int x = 0; x < srcData.width; ++x)
  474. {
  475. if (*lineData >= threshold)
  476. pixelsOnRow.addRange (Range<int> (x, x + 1));
  477. lineData += srcData.pixelStride;
  478. }
  479. }
  480. for (int i = 0; i < pixelsOnRow.getNumRanges(); ++i)
  481. {
  482. const Range<int> range (pixelsOnRow.getRange (i));
  483. result.add (Rectangle<int> (range.getStart(), y, range.getLength(), 1));
  484. }
  485. result.consolidate();
  486. }
  487. }
  488. else
  489. {
  490. result.add (0, 0, getWidth(), getHeight());
  491. }
  492. }
  493. void Image::moveImageSection (int dx, int dy,
  494. int sx, int sy,
  495. int w, int h)
  496. {
  497. if (dx < 0)
  498. {
  499. w += dx;
  500. sx -= dx;
  501. dx = 0;
  502. }
  503. if (dy < 0)
  504. {
  505. h += dy;
  506. sy -= dy;
  507. dy = 0;
  508. }
  509. if (sx < 0)
  510. {
  511. w += sx;
  512. dx -= sx;
  513. sx = 0;
  514. }
  515. if (sy < 0)
  516. {
  517. h += sy;
  518. dy -= sy;
  519. sy = 0;
  520. }
  521. const int minX = jmin (dx, sx);
  522. const int minY = jmin (dy, sy);
  523. w = jmin (w, getWidth() - jmax (sx, dx));
  524. h = jmin (h, getHeight() - jmax (sy, dy));
  525. if (w > 0 && h > 0)
  526. {
  527. const int maxX = jmax (dx, sx) + w;
  528. const int maxY = jmax (dy, sy) + h;
  529. const BitmapData destData (*this, minX, minY, maxX - minX, maxY - minY, BitmapData::readWrite);
  530. uint8* dst = destData.getPixelPointer (dx - minX, dy - minY);
  531. const uint8* src = destData.getPixelPointer (sx - minX, sy - minY);
  532. const size_t lineSize = (size_t) (destData.pixelStride * w);
  533. if (dy > sy)
  534. {
  535. while (--h >= 0)
  536. {
  537. const int offset = h * destData.lineStride;
  538. memmove (dst + offset, src + offset, lineSize);
  539. }
  540. }
  541. else if (dst != src)
  542. {
  543. while (--h >= 0)
  544. {
  545. memmove (dst, src, lineSize);
  546. dst += destData.lineStride;
  547. src += destData.lineStride;
  548. }
  549. }
  550. }
  551. }