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.

647 lines
20KB

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