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.

643 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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. ImagePixelData::ImagePixelData (const Image::PixelFormat format, const int w, const int h)
  21. : pixelFormat (format), width (w), height (h)
  22. {
  23. jassert (format == Image::RGB || format == Image::ARGB || format == Image::SingleChannel);
  24. jassert (w > 0 && h > 0); // It's illegal to create a zero-sized image!
  25. }
  26. ImagePixelData::~ImagePixelData()
  27. {
  28. }
  29. //==============================================================================
  30. ImageType::ImageType() {}
  31. ImageType::~ImageType() {}
  32. Image ImageType::convert (const Image& source) const
  33. {
  34. if (source.isNull() || getTypeID() == (ScopedPointer<ImageType> (source.getPixelData()->createType())->getTypeID()))
  35. return source;
  36. const Image::BitmapData src (source, Image::BitmapData::readOnly);
  37. Image newImage (create (src.pixelFormat, src.width, src.height, false));
  38. Image::BitmapData dest (newImage, Image::BitmapData::writeOnly);
  39. jassert (src.pixelStride == dest.pixelStride && src.pixelFormat == dest.pixelFormat);
  40. for (int y = 0; y < dest.height; ++y)
  41. memcpy (dest.getLinePointer (y), src.getLinePointer (y), dest.lineStride);
  42. return newImage;
  43. }
  44. //==============================================================================
  45. NativeImageType::NativeImageType() {}
  46. NativeImageType::~NativeImageType() {}
  47. int NativeImageType::getTypeID() const
  48. {
  49. return 1;
  50. }
  51. //==============================================================================
  52. class SoftwarePixelData : public ImagePixelData
  53. {
  54. public:
  55. SoftwarePixelData (const Image::PixelFormat format_, const int w, const int h, const bool clearImage)
  56. : ImagePixelData (format_, w, h),
  57. pixelStride (format_ == Image::RGB ? 3 : ((format_ == Image::ARGB) ? 4 : 1)),
  58. lineStride ((pixelStride * jmax (1, w) + 3) & ~3)
  59. {
  60. imageData.allocate ((size_t) (lineStride * jmax (1, h)), clearImage);
  61. }
  62. LowLevelGraphicsContext* createLowLevelContext()
  63. {
  64. return new LowLevelGraphicsSoftwareRenderer (Image (this));
  65. }
  66. void initialiseBitmapData (Image::BitmapData& bitmap, int x, int y, Image::BitmapData::ReadWriteMode)
  67. {
  68. bitmap.data = imageData + x * pixelStride + y * lineStride;
  69. bitmap.pixelFormat = pixelFormat;
  70. bitmap.lineStride = lineStride;
  71. bitmap.pixelStride = pixelStride;
  72. }
  73. ImagePixelData* clone()
  74. {
  75. SoftwarePixelData* s = new SoftwarePixelData (pixelFormat, width, height, false);
  76. memcpy (s->imageData, imageData, (size_t) (lineStride * height));
  77. return s;
  78. }
  79. ImageType* createType() const { return new SoftwareImageType(); }
  80. private:
  81. HeapBlock<uint8> imageData;
  82. const int pixelStride, lineStride;
  83. JUCE_LEAK_DETECTOR (SoftwarePixelData);
  84. };
  85. SoftwareImageType::SoftwareImageType() {}
  86. SoftwareImageType::~SoftwareImageType() {}
  87. ImagePixelData* SoftwareImageType::create (Image::PixelFormat format, int width, int height, bool clearImage) const
  88. {
  89. return new SoftwarePixelData (format, width, height, clearImage);
  90. }
  91. int SoftwareImageType::getTypeID() const
  92. {
  93. return 2;
  94. }
  95. //==============================================================================
  96. class SubsectionPixelData : public ImagePixelData
  97. {
  98. public:
  99. SubsectionPixelData (ImagePixelData* const image_, const Rectangle<int>& area_)
  100. : ImagePixelData (image_->pixelFormat, area_.getWidth(), area_.getHeight()),
  101. image (image_), area (area_)
  102. {
  103. }
  104. LowLevelGraphicsContext* createLowLevelContext()
  105. {
  106. LowLevelGraphicsContext* g = image->createLowLevelContext();
  107. g->clipToRectangle (area);
  108. g->setOrigin (area.getX(), area.getY());
  109. return g;
  110. }
  111. void initialiseBitmapData (Image::BitmapData& bitmap, int x, int y, Image::BitmapData::ReadWriteMode mode)
  112. {
  113. image->initialiseBitmapData (bitmap, x + area.getX(), y + area.getY(), mode);
  114. }
  115. ImagePixelData* clone()
  116. {
  117. jassert (getReferenceCount() > 0); // (This method can't be used on an unowned pointer, as it will end up self-deleting)
  118. const ScopedPointer<ImageType> type (image->createType());
  119. Image newImage (type->create (pixelFormat, area.getWidth(), area.getHeight(), pixelFormat != Image::RGB));
  120. {
  121. Graphics g (newImage);
  122. g.drawImageAt (Image (this), -area.getX(), -area.getY());
  123. }
  124. newImage.getPixelData()->incReferenceCount();
  125. return newImage.getPixelData();
  126. }
  127. ImageType* createType() const { return image->createType(); }
  128. private:
  129. const ReferenceCountedObjectPtr<ImagePixelData> image;
  130. const Rectangle<int> area;
  131. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SubsectionPixelData);
  132. };
  133. Image Image::getClippedImage (const Rectangle<int>& area) const
  134. {
  135. if (area.contains (getBounds()))
  136. return *this;
  137. const Rectangle<int> validArea (area.getIntersection (getBounds()));
  138. if (validArea.isEmpty())
  139. return Image::null;
  140. return Image (new SubsectionPixelData (image, validArea));
  141. }
  142. //==============================================================================
  143. Image::Image()
  144. {
  145. }
  146. Image::Image (ImagePixelData* const instance)
  147. : image (instance)
  148. {
  149. }
  150. Image::Image (const PixelFormat format, int width, int height, bool clearImage)
  151. : image (NativeImageType().create (format, width, height, clearImage))
  152. {
  153. }
  154. Image::Image (const PixelFormat format, int width, int height, bool clearImage, const ImageType& type)
  155. : image (type.create (format, width, height, clearImage))
  156. {
  157. }
  158. Image::Image (const Image& other)
  159. : image (other.image)
  160. {
  161. }
  162. Image& Image::operator= (const Image& other)
  163. {
  164. image = other.image;
  165. return *this;
  166. }
  167. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  168. Image::Image (Image&& other) noexcept
  169. : image (static_cast <ReferenceCountedObjectPtr<ImagePixelData>&&> (other.image))
  170. {
  171. }
  172. Image& Image::operator= (Image&& other) noexcept
  173. {
  174. image = static_cast <ReferenceCountedObjectPtr<ImagePixelData>&&> (other.image);
  175. return *this;
  176. }
  177. #endif
  178. Image::~Image()
  179. {
  180. }
  181. const Image Image::null;
  182. int Image::getReferenceCount() const noexcept { return image == nullptr ? 0 : image->getReferenceCount(); }
  183. int Image::getWidth() const noexcept { return image == nullptr ? 0 : image->width; }
  184. int Image::getHeight() const noexcept { return image == nullptr ? 0 : image->height; }
  185. Rectangle<int> Image::getBounds() const noexcept { return image == nullptr ? Rectangle<int>() : Rectangle<int> (image->width, image->height); }
  186. Image::PixelFormat Image::getFormat() const noexcept { return image == nullptr ? UnknownFormat : image->pixelFormat; }
  187. bool Image::isARGB() const noexcept { return getFormat() == ARGB; }
  188. bool Image::isRGB() const noexcept { return getFormat() == RGB; }
  189. bool Image::isSingleChannel() const noexcept { return getFormat() == SingleChannel; }
  190. bool Image::hasAlphaChannel() const noexcept { return getFormat() != RGB; }
  191. LowLevelGraphicsContext* Image::createLowLevelContext() const
  192. {
  193. return image == nullptr ? nullptr : image->createLowLevelContext();
  194. }
  195. void Image::duplicateIfShared()
  196. {
  197. if (image != nullptr && image->getReferenceCount() > 1)
  198. image = image->clone();
  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 (image->pixelFormat, 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. const 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. }
  520. END_JUCE_NAMESPACE