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.

442 lines
19KB

  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. #ifndef __JUCE_IMAGE_JUCEHEADER__
  19. #define __JUCE_IMAGE_JUCEHEADER__
  20. #include "../colour/juce_Colour.h"
  21. #include "../contexts/juce_GraphicsContext.h"
  22. //==============================================================================
  23. /**
  24. Holds a fixed-size bitmap.
  25. The image is stored in either 24-bit RGB or 32-bit premultiplied-ARGB format.
  26. To draw into an image, create a Graphics object for it.
  27. e.g. @code
  28. // create a transparent 500x500 image..
  29. Image myImage (Image::RGB, 500, 500, true);
  30. Graphics g (myImage);
  31. g.setColour (Colours::red);
  32. g.fillEllipse (20, 20, 300, 200); // draws a red ellipse in our image.
  33. @endcode
  34. Other useful ways to create an image are with the ImageCache class, or the
  35. ImageFileFormat, which provides a way to load common image files.
  36. @see Graphics, ImageFileFormat, ImageCache, ImageConvolutionKernel
  37. */
  38. class JUCE_API Image
  39. {
  40. public:
  41. //==============================================================================
  42. /**
  43. */
  44. enum PixelFormat
  45. {
  46. UnknownFormat,
  47. RGB, /**<< each pixel is a 3-byte packed RGB colour value. For byte order, see the PixelRGB class. */
  48. ARGB, /**<< each pixel is a 4-byte ARGB premultiplied colour value. For byte order, see the PixelARGB class. */
  49. SingleChannel /**<< each pixel is a 1-byte alpha channel value. */
  50. };
  51. /**
  52. */
  53. enum ImageType
  54. {
  55. SoftwareImage = 0,
  56. NativeImage
  57. };
  58. //==============================================================================
  59. /** Creates a null image. */
  60. Image();
  61. /** Creates an image with a specified size and format.
  62. @param format the number of colour channels in the image
  63. @param imageWidth the desired width of the image, in pixels - this value must be
  64. greater than zero (otherwise a width of 1 will be used)
  65. @param imageHeight the desired width of the image, in pixels - this value must be
  66. greater than zero (otherwise a height of 1 will be used)
  67. @param clearImage if true, the image will initially be cleared to black (if it's RGB)
  68. or transparent black (if it's ARGB). If false, the image may contain
  69. junk initially, so you need to make sure you overwrite it thoroughly.
  70. @param type the type of image - this lets you specify whether you want a purely
  71. memory-based image, or one that may be managed by the OS if possible.
  72. */
  73. Image (PixelFormat format,
  74. int imageWidth,
  75. int imageHeight,
  76. bool clearImage,
  77. ImageType type = NativeImage);
  78. /** Creates a shared reference to another image.
  79. This won't create a duplicate of the image - when Image objects are copied, they simply
  80. point to the same shared image data. To make sure that an Image object has its own unique,
  81. unshared internal data, call duplicateIfShared().
  82. */
  83. Image (const Image& other);
  84. /** Makes this image refer to the same underlying image as another object.
  85. This won't create a duplicate of the image - when Image objects are copied, they simply
  86. point to the same shared image data. To make sure that an Image object has its own unique,
  87. unshared internal data, call duplicateIfShared().
  88. */
  89. Image& operator= (const Image&);
  90. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  91. Image (Image&& other) noexcept;
  92. Image& operator= (Image&&) noexcept;
  93. #endif
  94. /** Destructor. */
  95. ~Image();
  96. /** Returns true if the two images are referring to the same internal, shared image. */
  97. bool operator== (const Image& other) const noexcept { return image == other.image; }
  98. /** Returns true if the two images are not referring to the same internal, shared image. */
  99. bool operator!= (const Image& other) const noexcept { return image != other.image; }
  100. /** Returns true if this image isn't null.
  101. If you create an Image with the default constructor, it has no size or content, and is null
  102. until you reassign it to an Image which contains some actual data.
  103. The isNull() method is the opposite of isValid().
  104. @see isNull
  105. */
  106. inline bool isValid() const noexcept { return image != nullptr; }
  107. /** Returns true if this image is not valid.
  108. If you create an Image with the default constructor, it has no size or content, and is null
  109. until you reassign it to an Image which contains some actual data.
  110. The isNull() method is the opposite of isValid().
  111. @see isValid
  112. */
  113. inline bool isNull() const noexcept { return image == nullptr; }
  114. /** A null Image object that can be used when you need to return an invalid image.
  115. This object is the equivalient to an Image created with the default constructor.
  116. */
  117. static const Image null;
  118. //==============================================================================
  119. /** Returns the image's width (in pixels). */
  120. int getWidth() const noexcept { return image == nullptr ? 0 : image->width; }
  121. /** Returns the image's height (in pixels). */
  122. int getHeight() const noexcept { return image == nullptr ? 0 : image->height; }
  123. /** Returns a rectangle with the same size as this image.
  124. The rectangle's origin is always (0, 0).
  125. */
  126. const Rectangle<int> getBounds() const noexcept { return image == nullptr ? Rectangle<int>() : Rectangle<int> (image->width, image->height); }
  127. /** Returns the image's pixel format. */
  128. PixelFormat getFormat() const noexcept { return image == nullptr ? UnknownFormat : image->format; }
  129. /** True if the image's format is ARGB. */
  130. bool isARGB() const noexcept { return getFormat() == ARGB; }
  131. /** True if the image's format is RGB. */
  132. bool isRGB() const noexcept { return getFormat() == RGB; }
  133. /** True if the image's format is a single-channel alpha map. */
  134. bool isSingleChannel() const noexcept { return getFormat() == SingleChannel; }
  135. /** True if the image contains an alpha-channel. */
  136. bool hasAlphaChannel() const noexcept { return getFormat() != RGB; }
  137. //==============================================================================
  138. /** Clears a section of the image with a given colour.
  139. This won't do any alpha-blending - it just sets all pixels in the image to
  140. the given colour (which may be non-opaque if the image has an alpha channel).
  141. */
  142. void clear (const Rectangle<int>& area, const Colour& colourToClearTo = Colour (0x00000000));
  143. /** Returns a rescaled version of this image.
  144. A new image is returned which is a copy of this one, rescaled to the given size.
  145. Note that if the new size is identical to the existing image, this will just return
  146. a reference to the original image, and won't actually create a duplicate.
  147. */
  148. Image rescaled (int newWidth, int newHeight,
  149. Graphics::ResamplingQuality quality = Graphics::mediumResamplingQuality) const;
  150. /** Returns a version of this image with a different image format.
  151. A new image is returned which has been converted to the specified format.
  152. Note that if the new format is no different to the current one, this will just return
  153. a reference to the original image, and won't actually create a copy.
  154. */
  155. Image convertedToFormat (PixelFormat newFormat) const;
  156. /** Makes sure that no other Image objects share the same underlying data as this one.
  157. If no other Image objects refer to the same shared data as this one, this method has no
  158. effect. But if there are other references to the data, this will create a new copy of
  159. the data internally.
  160. Call this if you want to draw onto the image, but want to make sure that this doesn't
  161. affect any other code that may be sharing the same data.
  162. @see getReferenceCount
  163. */
  164. void duplicateIfShared();
  165. /** Returns an image which refers to a subsection of this image.
  166. This will not make a copy of the original - the new image will keep a reference to it, so that
  167. if the original image is changed, the contents of the subsection will also change. Likewise if you
  168. draw into the subimage, you'll also be drawing onto that area of the original image. Note that if
  169. you use operator= to make the original Image object refer to something else, the subsection image
  170. won't pick up this change, it'll remain pointing at the original.
  171. The area passed-in will be clipped to the bounds of this image, so this may return a smaller
  172. image than the area you asked for, or even a null image if the area was out-of-bounds.
  173. */
  174. Image getClippedImage (const Rectangle<int>& area) const;
  175. //==============================================================================
  176. /** Returns the colour of one of the pixels in the image.
  177. If the co-ordinates given are beyond the image's boundaries, this will
  178. return Colours::transparentBlack.
  179. @see setPixelAt, Image::BitmapData::getPixelColour
  180. */
  181. const Colour getPixelAt (int x, int y) const;
  182. /** Sets the colour of one of the image's pixels.
  183. If the co-ordinates are beyond the image's boundaries, then nothing will happen.
  184. Note that this won't do any alpha-blending, it'll just replace the existing pixel
  185. with the given one. The colour's opacity will be ignored if this image doesn't have
  186. an alpha-channel.
  187. @see getPixelAt, Image::BitmapData::setPixelColour
  188. */
  189. void setPixelAt (int x, int y, const Colour& colour);
  190. /** Changes the opacity of a pixel.
  191. This only has an effect if the image has an alpha channel and if the
  192. given co-ordinates are inside the image's boundary.
  193. The multiplier must be in the range 0 to 1.0, and the current alpha
  194. at the given co-ordinates will be multiplied by this value.
  195. @see setPixelAt
  196. */
  197. void multiplyAlphaAt (int x, int y, float multiplier);
  198. /** Changes the overall opacity of the image.
  199. This will multiply the alpha value of each pixel in the image by the given
  200. amount (limiting the resulting alpha values between 0 and 255). This allows
  201. you to make an image more or less transparent.
  202. If the image doesn't have an alpha channel, this won't have any effect.
  203. */
  204. void multiplyAllAlphas (float amountToMultiplyBy);
  205. /** Changes all the colours to be shades of grey, based on their current luminosity.
  206. */
  207. void desaturate();
  208. //==============================================================================
  209. /** Retrieves a section of an image as raw pixel data, so it can be read or written to.
  210. You should only use this class as a last resort - messing about with the internals of
  211. an image is only recommended for people who really know what they're doing!
  212. A BitmapData object should be used as a temporary, stack-based object. Don't keep one
  213. hanging around while the image is being used elsewhere.
  214. Depending on the way the image class is implemented, this may create a temporary buffer
  215. which is copied back to the image when the object is deleted, or it may just get a pointer
  216. directly into the image's raw data.
  217. You can use the stride and data values in this class directly, but don't alter them!
  218. The actual format of the pixel data depends on the image's format - see Image::getFormat(),
  219. and the PixelRGB, PixelARGB and PixelAlpha classes for more info.
  220. */
  221. class BitmapData
  222. {
  223. public:
  224. enum ReadWriteMode
  225. {
  226. readOnly,
  227. writeOnly,
  228. readWrite
  229. };
  230. BitmapData (Image& image, int x, int y, int w, int h, ReadWriteMode mode);
  231. BitmapData (const Image& image, int x, int y, int w, int h);
  232. BitmapData (const Image& image, ReadWriteMode mode);
  233. ~BitmapData();
  234. /** Returns a pointer to the start of a line in the image.
  235. The co-ordinate you provide here isn't checked, so it's the caller's responsibility to make
  236. sure it's not out-of-range.
  237. */
  238. inline uint8* getLinePointer (int y) const noexcept { return data + y * lineStride; }
  239. /** Returns a pointer to a pixel in the image.
  240. The co-ordinates you give here are not checked, so it's the caller's responsibility to make sure they're
  241. not out-of-range.
  242. */
  243. inline uint8* getPixelPointer (int x, int y) const noexcept { return data + y * lineStride + x * pixelStride; }
  244. /** Returns the colour of a given pixel.
  245. For performance reasons, this won't do any bounds-checking on the coordinates, so it's the caller's
  246. repsonsibility to make sure they're within the image's size.
  247. */
  248. const Colour getPixelColour (int x, int y) const noexcept;
  249. /** Sets the colour of a given pixel.
  250. For performance reasons, this won't do any bounds-checking on the coordinates, so it's the caller's
  251. repsonsibility to make sure they're within the image's size.
  252. */
  253. void setPixelColour (int x, int y, const Colour& colour) const noexcept;
  254. uint8* data;
  255. PixelFormat pixelFormat;
  256. int lineStride, pixelStride, width, height;
  257. //==============================================================================
  258. /** Used internally by custom image types to manage pixel data lifetime. */
  259. class BitmapDataReleaser
  260. {
  261. protected:
  262. BitmapDataReleaser() {}
  263. public:
  264. virtual ~BitmapDataReleaser() {}
  265. };
  266. ScopedPointer<BitmapDataReleaser> dataReleaser;
  267. private:
  268. JUCE_DECLARE_NON_COPYABLE (BitmapData);
  269. };
  270. //==============================================================================
  271. /** Copies a section of the image to somewhere else within itself. */
  272. void moveImageSection (int destX, int destY,
  273. int sourceX, int sourceY,
  274. int width, int height);
  275. /** Creates a RectangleList containing rectangles for all non-transparent pixels
  276. of the image.
  277. @param result the list that will have the area added to it
  278. @param alphaThreshold for a semi-transparent image, any pixels whose alpha is
  279. above this level will be considered opaque
  280. */
  281. void createSolidAreaMask (RectangleList& result,
  282. float alphaThreshold = 0.5f) const;
  283. //==============================================================================
  284. /** Returns a NamedValueSet that is attached to the image and which can be used for
  285. associating custom values with it.
  286. If this is a null image, this will return a null pointer.
  287. */
  288. NamedValueSet* getProperties() const;
  289. //==============================================================================
  290. /** Creates a context suitable for drawing onto this image.
  291. Don't call this method directly! It's used internally by the Graphics class.
  292. */
  293. LowLevelGraphicsContext* createLowLevelContext() const;
  294. /** Returns the number of Image objects which are currently referring to the same internal
  295. shared image data.
  296. @see duplicateIfShared
  297. */
  298. int getReferenceCount() const noexcept { return image == nullptr ? 0 : image->getReferenceCount(); }
  299. //==============================================================================
  300. /** This is a base class for task-specific types of image.
  301. Don't use this class directly! It's used internally by the Image class.
  302. */
  303. class SharedImage : public ReferenceCountedObject
  304. {
  305. public:
  306. SharedImage (PixelFormat format, int width, int height);
  307. ~SharedImage();
  308. virtual LowLevelGraphicsContext* createLowLevelContext() = 0;
  309. virtual SharedImage* clone() = 0;
  310. virtual ImageType getType() const = 0;
  311. virtual void initialiseBitmapData (BitmapData& bitmapData, int x, int y, BitmapData::ReadWriteMode mode) = 0;
  312. static SharedImage* createNativeImage (PixelFormat format, int width, int height, bool clearImage);
  313. static SharedImage* createSoftwareImage (PixelFormat format, int width, int height, bool clearImage);
  314. PixelFormat getPixelFormat() const noexcept { return format; }
  315. int getWidth() const noexcept { return width; }
  316. int getHeight() const noexcept { return height; }
  317. protected:
  318. friend class Image;
  319. friend class BitmapData;
  320. const PixelFormat format;
  321. const int width, height;
  322. NamedValueSet userData;
  323. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SharedImage);
  324. };
  325. /** @internal */
  326. SharedImage* getSharedImage() const noexcept { return image; }
  327. /** @internal */
  328. explicit Image (SharedImage* instance);
  329. private:
  330. //==============================================================================
  331. friend class SharedImage;
  332. friend class BitmapData;
  333. ReferenceCountedObjectPtr<SharedImage> image;
  334. JUCE_LEAK_DETECTOR (Image);
  335. };
  336. #endif // __JUCE_IMAGE_JUCEHEADER__