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.

669 lines
21KB

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