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.

juce_Image.h 22KB

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