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.

292 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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_Graphics.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. enum PixelFormat
  43. {
  44. RGB, /**<< each pixel is a 3-byte packed RGB colour value. For byte order, see the PixelRGB class. */
  45. ARGB, /**<< each pixel is a 4-byte ARGB premultiplied colour value. For byte order, see the PixelARGB class. */
  46. SingleChannel /**<< each pixel is a 1-byte alpha channel value. */
  47. };
  48. //==============================================================================
  49. /** Creates an in-memory image with a specified size and format.
  50. @param format the number of colour channels in the image
  51. @param imageWidth the desired width of the image, in pixels - this value must be
  52. greater than zero (otherwise a width of 1 will be used)
  53. @param imageHeight the desired width of the image, in pixels - this value must be
  54. greater than zero (otherwise a height of 1 will be used)
  55. @param clearImage if true, the image will initially be cleared to black or transparent
  56. black. If false, the image may contain random data, and the
  57. user will have to deal with this
  58. */
  59. Image (const PixelFormat format,
  60. const int imageWidth,
  61. const int imageHeight,
  62. const bool clearImage);
  63. /** Creates a copy of another image.
  64. @see createCopy
  65. */
  66. Image (const Image& other);
  67. /** Destructor. */
  68. virtual ~Image();
  69. //==============================================================================
  70. /** Returns the image's width (in pixels). */
  71. int getWidth() const throw() { return imageWidth; }
  72. /** Returns the image's height (in pixels). */
  73. int getHeight() const throw() { return imageHeight; }
  74. /** Returns the image's pixel format. */
  75. PixelFormat getFormat() const throw() { return format; }
  76. /** True if the image's format is ARGB. */
  77. bool isARGB() const throw() { return format == ARGB; }
  78. /** True if the image's format is RGB. */
  79. bool isRGB() const throw() { return format == RGB; }
  80. /** True if the image contains an alpha-channel. */
  81. bool hasAlphaChannel() const throw() { return format != RGB; }
  82. //==============================================================================
  83. /** Clears a section of the image with a given colour.
  84. This won't do any alpha-blending - it just sets all pixels in the image to
  85. the given colour (which may be non-opaque if the image has an alpha channel).
  86. */
  87. virtual void clear (int x, int y, int w, int h,
  88. const Colour& colourToClearTo = Colour (0x00000000));
  89. /** Returns a new image that's a copy of this one.
  90. A new size for the copied image can be specified, or values less than
  91. zero can be passed-in to use the image's existing dimensions.
  92. It's up to the caller to delete the image when no longer needed.
  93. */
  94. virtual Image* createCopy (int newWidth = -1,
  95. int newHeight = -1,
  96. const Graphics::ResamplingQuality quality = Graphics::mediumResamplingQuality) const;
  97. /** Returns a new single-channel image which is a copy of the alpha-channel of this image.
  98. */
  99. virtual Image* createCopyOfAlphaChannel() const;
  100. //==============================================================================
  101. /** Returns the colour of one of the pixels in the image.
  102. If the co-ordinates given are beyond the image's boundaries, this will
  103. return Colours::transparentBlack.
  104. (0, 0) is the image's top-left corner.
  105. @see getAlphaAt, setPixelAt, blendPixelAt
  106. */
  107. virtual const Colour getPixelAt (const int x, const int y) const;
  108. /** Sets the colour of one of the image's pixels.
  109. If the co-ordinates are beyond the image's boundaries, then nothing will
  110. happen.
  111. Note that unlike blendPixelAt(), this won't do any alpha-blending, it'll
  112. just replace the existing pixel with the given one. The colour's opacity
  113. will be ignored if this image doesn't have an alpha-channel.
  114. (0, 0) is the image's top-left corner.
  115. @see blendPixelAt
  116. */
  117. virtual void setPixelAt (const int x, const int y, const Colour& colour);
  118. /** Changes the opacity of a pixel.
  119. This only has an effect if the image has an alpha channel and if the
  120. given co-ordinates are inside the image's boundary.
  121. The multiplier must be in the range 0 to 1.0, and the current alpha
  122. at the given co-ordinates will be multiplied by this value.
  123. @see getAlphaAt, setPixelAt
  124. */
  125. virtual void multiplyAlphaAt (const int x, const int y, const float multiplier);
  126. /** Changes the overall opacity of the image.
  127. This will multiply the alpha value of each pixel in the image by the given
  128. amount (limiting the resulting alpha values between 0 and 255). This allows
  129. you to make an image more or less transparent.
  130. If the image doesn't have an alpha channel, this won't have any effect.
  131. */
  132. virtual void multiplyAllAlphas (const float amountToMultiplyBy);
  133. /** Changes all the colours to be shades of grey, based on their current luminosity.
  134. */
  135. virtual void desaturate();
  136. //==============================================================================
  137. /** Locks some of the pixels in the image so they can be read and written to.
  138. This returns a pointer to some memory containing the pixels in the given
  139. rectangle. It also returns values for the line and pixel stride used within
  140. the data. The format of the pixel data is the same as that of this image.
  141. When you've finished reading and changing the data, you must call
  142. releasePixelDataReadWrite() to give the pixels back to the image.
  143. For images that are stored in memory, this method may just return a direct
  144. pointer to the image's data, but other types of image may be stored elsewhere,
  145. e.g. in video memory, and if so, this lockPixelDataReadWrite() and
  146. releasePixelDataReadWrite() may need to create a temporary copy in main memory.
  147. If you only need read-access to the pixel data, use lockPixelDataReadOnly()
  148. instead.
  149. @see releasePixelDataReadWrite, lockPixelDataReadOnly
  150. */
  151. virtual uint8* lockPixelDataReadWrite (int x, int y, int w, int h, int& lineStride, int& pixelStride);
  152. /** Releases a block of memory that was locked with lockPixelDataReadWrite().
  153. */
  154. virtual void releasePixelDataReadWrite (void* sourceData);
  155. /** Locks some of the pixels in the image so they can be read.
  156. This returns a pointer to some memory containing the pixels in the given
  157. rectangle. It also returns values for the line and pixel stride used within
  158. the data. The format of the pixel data is the same as that of this image.
  159. When you've finished reading the data, you must call releasePixelDataReadOnly()
  160. to let the image free the memory if necessary.
  161. For images that are stored in memory, this method may just return a direct
  162. pointer to the image's data, but other types of image may be stored elsewhere,
  163. e.g. in video memory, and if so, this lockPixelDataReadWrite() and
  164. releasePixelDataReadWrite() may need to create a temporary copy in main memory.
  165. If you only need to read and write the pixel data, use lockPixelDataReadWrite()
  166. instead.
  167. @see releasePixelDataReadOnly, lockPixelDataReadWrite
  168. */
  169. virtual const uint8* lockPixelDataReadOnly (int x, int y, int w, int h, int& lineStride, int& pixelStride) const;
  170. /** Releases a block of memory that was locked with lockPixelDataReadOnly().
  171. */
  172. virtual void releasePixelDataReadOnly (const void* sourceData) const;
  173. /** Copies some pixel values to a rectangle of the image.
  174. The format of the pixel data must match that of the image itself, and the
  175. rectangle supplied must be within the image's bounds.
  176. */
  177. virtual void setPixelData (int destX, int destY, int destW, int destH,
  178. const uint8* sourcePixelData, int sourceLineStride);
  179. /** Copies a section of the image to somewhere else within itself.
  180. */
  181. virtual void moveImageSection (int destX, int destY,
  182. int sourceX, int sourceY,
  183. int width, int height);
  184. /** Creates a RectangleList containing rectangles for all non-transparent pixels
  185. of the image.
  186. @param result the list that will have the area added to it
  187. @param alphaThreshold for a semi-transparent image, any pixels whose alpha is
  188. above this level will be considered opaque
  189. */
  190. void createSolidAreaMask (RectangleList& result,
  191. const float alphaThreshold = 0.5f) const;
  192. //==============================================================================
  193. juce_UseDebuggingNewOperator
  194. /** Creates a context suitable for drawing onto this image.
  195. Don't call this method directly! It's used internally by the Graphics class.
  196. */
  197. virtual LowLevelGraphicsContext* createLowLevelContext();
  198. protected:
  199. const PixelFormat format;
  200. const int imageWidth, imageHeight;
  201. /** Used internally so that subclasses can call a constructor that doesn't allocate memory */
  202. Image (const PixelFormat format,
  203. const int imageWidth,
  204. const int imageHeight);
  205. int pixelStride, lineStride;
  206. uint8* imageData;
  207. private:
  208. //==============================================================================
  209. const Image& operator= (const Image&);
  210. };
  211. #endif // __JUCE_IMAGE_JUCEHEADER__