The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

640 lines
20KB

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