Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:58:13

0001 ////////////////////////////////////////////////////////////
0002 //
0003 // SFML - Simple and Fast Multimedia Library
0004 // Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
0005 //
0006 // This software is provided 'as-is', without any express or implied warranty.
0007 // In no event will the authors be held liable for any damages arising from the use of this software.
0008 //
0009 // Permission is granted to anyone to use this software for any purpose,
0010 // including commercial applications, and to alter it and redistribute it freely,
0011 // subject to the following restrictions:
0012 //
0013 // 1. The origin of this software must not be misrepresented;
0014 //    you must not claim that you wrote the original software.
0015 //    If you use this software in a product, an acknowledgment
0016 //    in the product documentation would be appreciated but is not required.
0017 //
0018 // 2. Altered source versions must be plainly marked as such,
0019 //    and must not be misrepresented as being the original software.
0020 //
0021 // 3. This notice may not be removed or altered from any source distribution.
0022 //
0023 ////////////////////////////////////////////////////////////
0024 
0025 #ifndef SFML_IMAGE_HPP
0026 #define SFML_IMAGE_HPP
0027 
0028 ////////////////////////////////////////////////////////////
0029 // Headers
0030 ////////////////////////////////////////////////////////////
0031 #include <SFML/Graphics/Export.hpp>
0032 #include <SFML/Graphics/Color.hpp>
0033 #include <SFML/Graphics/Rect.hpp>
0034 #include <string>
0035 #include <vector>
0036 
0037 
0038 namespace sf
0039 {
0040 class InputStream;
0041 
0042 ////////////////////////////////////////////////////////////
0043 /// \brief Class for loading, manipulating and saving images
0044 ///
0045 ////////////////////////////////////////////////////////////
0046 class SFML_GRAPHICS_API Image
0047 {
0048 public:
0049 
0050     ////////////////////////////////////////////////////////////
0051     /// \brief Default constructor
0052     ///
0053     /// Creates an empty image.
0054     ///
0055     ////////////////////////////////////////////////////////////
0056     Image();
0057 
0058     ////////////////////////////////////////////////////////////
0059     /// \brief Destructor
0060     ///
0061     ////////////////////////////////////////////////////////////
0062     ~Image();
0063 
0064     ////////////////////////////////////////////////////////////
0065     /// \brief Create the image and fill it with a unique color
0066     ///
0067     /// \param width  Width of the image
0068     /// \param height Height of the image
0069     /// \param color  Fill color
0070     ///
0071     ////////////////////////////////////////////////////////////
0072     void create(unsigned int width, unsigned int height, const Color& color = Color(0, 0, 0));
0073 
0074     ////////////////////////////////////////////////////////////
0075     /// \brief Create the image from an array of pixels
0076     ///
0077     /// The \a pixel array is assumed to contain 32-bits RGBA pixels,
0078     /// and have the given \a width and \a height. If not, this is
0079     /// an undefined behavior.
0080     /// If \a pixels is null, an empty image is created.
0081     ///
0082     /// \param width  Width of the image
0083     /// \param height Height of the image
0084     /// \param pixels Array of pixels to copy to the image
0085     ///
0086     ////////////////////////////////////////////////////////////
0087     void create(unsigned int width, unsigned int height, const Uint8* pixels);
0088 
0089     ////////////////////////////////////////////////////////////
0090     /// \brief Load the image from a file on disk
0091     ///
0092     /// The supported image formats are bmp, png, tga, jpg, gif,
0093     /// psd, hdr, pic and pnm. Some format options are not supported,
0094     /// like jpeg with arithmetic coding or ASCII pnm.
0095     /// If this function fails, the image is left unchanged.
0096     ///
0097     /// \param filename Path of the image file to load
0098     ///
0099     /// \return True if loading was successful
0100     ///
0101     /// \see loadFromMemory, loadFromStream, saveToFile
0102     ///
0103     ////////////////////////////////////////////////////////////
0104     bool loadFromFile(const std::string& filename);
0105 
0106     ////////////////////////////////////////////////////////////
0107     /// \brief Load the image from a file in memory
0108     ///
0109     /// The supported image formats are bmp, png, tga, jpg, gif,
0110     /// psd, hdr, pic and pnm. Some format options are not supported,
0111     /// like jpeg with arithmetic coding or ASCII pnm.
0112     /// If this function fails, the image is left unchanged.
0113     ///
0114     /// \param data Pointer to the file data in memory
0115     /// \param size Size of the data to load, in bytes
0116     ///
0117     /// \return True if loading was successful
0118     ///
0119     /// \see loadFromFile, loadFromStream
0120     ///
0121     ////////////////////////////////////////////////////////////
0122     bool loadFromMemory(const void* data, std::size_t size);
0123 
0124     ////////////////////////////////////////////////////////////
0125     /// \brief Load the image from a custom stream
0126     ///
0127     /// The supported image formats are bmp, png, tga, jpg, gif,
0128     /// psd, hdr, pic and pnm. Some format options are not supported,
0129     /// like jpeg with arithmetic coding or ASCII pnm.
0130     /// If this function fails, the image is left unchanged.
0131     ///
0132     /// \param stream Source stream to read from
0133     ///
0134     /// \return True if loading was successful
0135     ///
0136     /// \see loadFromFile, loadFromMemory
0137     ///
0138     ////////////////////////////////////////////////////////////
0139     bool loadFromStream(InputStream& stream);
0140 
0141     ////////////////////////////////////////////////////////////
0142     /// \brief Save the image to a file on disk
0143     ///
0144     /// The format of the image is automatically deduced from
0145     /// the extension. The supported image formats are bmp, png,
0146     /// tga and jpg. The destination file is overwritten
0147     /// if it already exists. This function fails if the image is empty.
0148     ///
0149     /// \param filename Path of the file to save
0150     ///
0151     /// \return True if saving was successful
0152     ///
0153     /// \see create, loadFromFile, loadFromMemory
0154     ///
0155     ////////////////////////////////////////////////////////////
0156     bool saveToFile(const std::string& filename) const;
0157 
0158     ////////////////////////////////////////////////////////////
0159     /// \brief Save the image to a buffer in memory
0160     ///
0161     /// The format of the image must be specified.
0162     /// The supported image formats are bmp, png, tga and jpg.
0163     /// This function fails if the image is empty, or if
0164     /// the format was invalid.
0165     ///
0166     /// \param output Buffer to fill with encoded data
0167     /// \param format Encoding format to use
0168     ///
0169     /// \return True if saving was successful
0170     ///
0171     /// \see create, loadFromFile, loadFromMemory, saveToFile
0172     ///
0173     ////////////////////////////////////////////////////////////
0174     bool saveToMemory(std::vector<sf::Uint8>& output, const std::string& format) const;
0175 
0176     ////////////////////////////////////////////////////////////
0177     /// \brief Return the size (width and height) of the image
0178     ///
0179     /// \return Size of the image, in pixels
0180     ///
0181     ////////////////////////////////////////////////////////////
0182     Vector2u getSize() const;
0183 
0184     ////////////////////////////////////////////////////////////
0185     /// \brief Create a transparency mask from a specified color-key
0186     ///
0187     /// This function sets the alpha value of every pixel matching
0188     /// the given color to \a alpha (0 by default), so that they
0189     /// become transparent.
0190     ///
0191     /// \param color Color to make transparent
0192     /// \param alpha Alpha value to assign to transparent pixels
0193     ///
0194     ////////////////////////////////////////////////////////////
0195     void createMaskFromColor(const Color& color, Uint8 alpha = 0);
0196 
0197     ////////////////////////////////////////////////////////////
0198     /// \brief Copy pixels from another image onto this one
0199     ///
0200     /// This function does a slow pixel copy and should not be
0201     /// used intensively. It can be used to prepare a complex
0202     /// static image from several others, but if you need this
0203     /// kind of feature in real-time you'd better use sf::RenderTexture.
0204     ///
0205     /// If \a sourceRect is empty, the whole image is copied.
0206     /// If \a applyAlpha is set to true, alpha blending is
0207     /// applied from the source pixels to the destination pixels
0208     /// using the \b over operator. If it is false, the source
0209     /// pixels are copied unchanged with their alpha value.
0210     ///
0211     /// See https://en.wikipedia.org/wiki/Alpha_compositing for
0212     /// details on the \b over operator.
0213     ///
0214     /// \param source     Source image to copy
0215     /// \param destX      X coordinate of the destination position
0216     /// \param destY      Y coordinate of the destination position
0217     /// \param sourceRect Sub-rectangle of the source image to copy
0218     /// \param applyAlpha Should the copy take into account the source transparency?
0219     ///
0220     ////////////////////////////////////////////////////////////
0221     void copy(const Image& source, unsigned int destX, unsigned int destY, const IntRect& sourceRect = IntRect(0, 0, 0, 0), bool applyAlpha = false);
0222 
0223     ////////////////////////////////////////////////////////////
0224     /// \brief Change the color of a pixel
0225     ///
0226     /// This function doesn't check the validity of the pixel
0227     /// coordinates, using out-of-range values will result in
0228     /// an undefined behavior.
0229     ///
0230     /// \param x     X coordinate of pixel to change
0231     /// \param y     Y coordinate of pixel to change
0232     /// \param color New color of the pixel
0233     ///
0234     /// \see getPixel
0235     ///
0236     ////////////////////////////////////////////////////////////
0237     void setPixel(unsigned int x, unsigned int y, const Color& color);
0238 
0239     ////////////////////////////////////////////////////////////
0240     /// \brief Get the color of a pixel
0241     ///
0242     /// This function doesn't check the validity of the pixel
0243     /// coordinates, using out-of-range values will result in
0244     /// an undefined behavior.
0245     ///
0246     /// \param x X coordinate of pixel to get
0247     /// \param y Y coordinate of pixel to get
0248     ///
0249     /// \return Color of the pixel at coordinates (x, y)
0250     ///
0251     /// \see setPixel
0252     ///
0253     ////////////////////////////////////////////////////////////
0254     Color getPixel(unsigned int x, unsigned int y) const;
0255 
0256     ////////////////////////////////////////////////////////////
0257     /// \brief Get a read-only pointer to the array of pixels
0258     ///
0259     /// The returned value points to an array of RGBA pixels made of
0260     /// 8 bits integers components. The size of the array is
0261     /// width * height * 4 (getSize().x * getSize().y * 4).
0262     /// Warning: the returned pointer may become invalid if you
0263     /// modify the image, so you should never store it for too long.
0264     /// If the image is empty, a null pointer is returned.
0265     ///
0266     /// \return Read-only pointer to the array of pixels
0267     ///
0268     ////////////////////////////////////////////////////////////
0269     const Uint8* getPixelsPtr() const;
0270 
0271     ////////////////////////////////////////////////////////////
0272     /// \brief Flip the image horizontally (left <-> right)
0273     ///
0274     ////////////////////////////////////////////////////////////
0275     void flipHorizontally();
0276 
0277     ////////////////////////////////////////////////////////////
0278     /// \brief Flip the image vertically (top <-> bottom)
0279     ///
0280     ////////////////////////////////////////////////////////////
0281     void flipVertically();
0282 
0283 private:
0284 
0285     ////////////////////////////////////////////////////////////
0286     // Member data
0287     ////////////////////////////////////////////////////////////
0288     Vector2u           m_size;   //!< Image size
0289     std::vector<Uint8> m_pixels; //!< Pixels of the image
0290 };
0291 
0292 } // namespace sf
0293 
0294 
0295 #endif // SFML_IMAGE_HPP
0296 
0297 
0298 ////////////////////////////////////////////////////////////
0299 /// \class sf::Image
0300 /// \ingroup graphics
0301 ///
0302 /// sf::Image is an abstraction to manipulate images
0303 /// as bidimensional arrays of pixels. The class provides
0304 /// functions to load, read, write and save pixels, as well
0305 /// as many other useful functions.
0306 ///
0307 /// sf::Image can handle a unique internal representation of
0308 /// pixels, which is RGBA 32 bits. This means that a pixel
0309 /// must be composed of 8 bits red, green, blue and alpha
0310 /// channels -- just like a sf::Color.
0311 /// All the functions that return an array of pixels follow
0312 /// this rule, and all parameters that you pass to sf::Image
0313 /// functions (such as loadFromMemory) must use this
0314 /// representation as well.
0315 ///
0316 /// A sf::Image can be copied, but it is a heavy resource and
0317 /// if possible you should always use [const] references to
0318 /// pass or return them to avoid useless copies.
0319 ///
0320 /// Usage example:
0321 /// \code
0322 /// // Load an image file from a file
0323 /// sf::Image background;
0324 /// if (!background.loadFromFile("background.jpg"))
0325 ///     return -1;
0326 ///
0327 /// // Create a 20x20 image filled with black color
0328 /// sf::Image image;
0329 /// image.create(20, 20, sf::Color::Black);
0330 ///
0331 /// // Copy image1 on image2 at position (10, 10)
0332 /// image.copy(background, 10, 10);
0333 ///
0334 /// // Make the top-left pixel transparent
0335 /// sf::Color color = image.getPixel(0, 0);
0336 /// color.a = 0;
0337 /// image.setPixel(0, 0, color);
0338 ///
0339 /// // Save the image to a file
0340 /// if (!image.saveToFile("result.png"))
0341 ///     return -1;
0342 /// \endcode
0343 ///
0344 /// \see sf::Texture
0345 ///
0346 ////////////////////////////////////////////////////////////