Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added functions for creating sRGB enabled textures from file / memory, etc. #177

Merged
merged 3 commits into from
Dec 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 57 additions & 25 deletions include/SFML/Graphics/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,29 @@ CSFML_GRAPHICS_API sfTexture* sfTexture_create(unsigned int width, unsigned int
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API sfTexture* sfTexture_createFromFile(const char* filename, const sfIntRect* area);

////////////////////////////////////////////////////////////
/// \brief Create a new sRGB-enabled texture from a file
///
/// When providing texture data from an image file or memory, it can
eXpl0it3r marked this conversation as resolved.
Show resolved Hide resolved
/// either be stored in a linear color space or an sRGB color space.
/// Most digital images account for gamma correction already, so they
/// would need to be "uncorrected" back to linear color space before
/// being processed by the hardware. The hardware can automatically
/// convert it from the sRGB color space to a linear color space when
/// it gets sampled. When the rendered image gets output to the final
/// framebuffer, it gets converted back to sRGB.
///
/// This load option is only useful in conjunction with an sRGB capable
/// framebuffer. This can be requested during window creation.
///
/// \param filename Path of the image file to load
/// \param area Area of the source image to load (NULL to load the entire image)
///
/// \return A new sfTexture object, or NULL if it failed
///
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API sfTexture* sfTexture_createSrgbFromFile(const char* filename, const sfIntRect* area);

////////////////////////////////////////////////////////////
/// \brief Create a new texture from a file in memory
///
Expand All @@ -80,6 +103,18 @@ CSFML_GRAPHICS_API sfTexture* sfTexture_createFromFile(const char* filename, con
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API sfTexture* sfTexture_createFromMemory(const void* data, size_t sizeInBytes, const sfIntRect* area);

////////////////////////////////////////////////////////////
/// \brief Create a new sRGB-enabled texture from a file in memory
///
/// \param data Pointer to the file data in memory
/// \param sizeInBytes Size of the data to load, in bytes
/// \param area Area of the source image to load (NULL to load the entire image)
///
/// \return A new sfTexture object, or NULL if it failed
///
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API sfTexture* sfTexture_createSrgbFromMemory(const void* data, size_t sizeInBytes, const sfIntRect* area);

////////////////////////////////////////////////////////////
/// \brief Create a new texture from a custom stream
///
Expand All @@ -91,6 +126,17 @@ CSFML_GRAPHICS_API sfTexture* sfTexture_createFromMemory(const void* data, size_
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API sfTexture* sfTexture_createFromStream(sfInputStream* stream, const sfIntRect* area);

////////////////////////////////////////////////////////////
/// \brief Create a new sRGB-enabled texture from a custom stream
///
/// \param stream Source stream to read from
/// \param area Area of the source image to load (NULL to load the entire image)
///
/// \return A new sfTexture object, or NULL if it failed
///
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API sfTexture* sfTexture_createSrgbFromStream(sfInputStream* stream, const sfIntRect* area);

////////////////////////////////////////////////////////////
/// \brief Create a new texture from an image
///
Expand All @@ -102,6 +148,17 @@ CSFML_GRAPHICS_API sfTexture* sfTexture_createFromStream(sfInputStream* stream,
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API sfTexture* sfTexture_createFromImage(const sfImage* image, const sfIntRect* area);

////////////////////////////////////////////////////////////
/// \brief Create a new sRGB-enabled texture from an image
///
/// \param image Image to upload to the texture
/// \param area Area of the source image to load (NULL to load the entire image)
///
/// \return A new sfTexture object, or NULL if it failed
///
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API sfTexture* sfTexture_createSrgbFromImage(const sfImage* image, const sfIntRect* area);

////////////////////////////////////////////////////////////
/// \brief Copy an existing texture
///
Expand Down Expand Up @@ -223,31 +280,6 @@ CSFML_GRAPHICS_API void sfTexture_setSmooth(sfTexture* texture, sfBool smooth);
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API sfBool sfTexture_isSmooth(const sfTexture* texture);

////////////////////////////////////////////////////////////
/// \brief Enable or disable conversion from sRGB
///
/// When providing texture data from an image file or memory, it can
/// either be stored in a linear color space or an sRGB color space.
/// Most digital images account for gamma correction already, so they
/// would need to be "uncorrected" back to linear color space before
/// being processed by the hardware. The hardware can automatically
/// convert it from the sRGB color space to a linear color space when
/// it gets sampled. When the rendered image gets output to the final
/// framebuffer, it gets converted back to sRGB.
///
/// After enabling or disabling sRGB conversion, make sure to reload
/// the texture data in order for the setting to take effect.
///
/// This option is only useful in conjunction with an sRGB capable
/// framebuffer. This can be requested during window creation.
///
/// \param sRgb True to enable sRGB conversion, false to disable it
///
/// \see sfTexture_isSrgb
///
////////////////////////////////////////////////////////////
CSFML_GRAPHICS_API void sfTexture_setSrgb(sfTexture* texture, sfBool sRgb);

////////////////////////////////////////////////////////////
/// \brief Tell whether the texture source is converted from sRGB or not
///
Expand Down
91 changes: 83 additions & 8 deletions src/SFML/Graphics/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ sfTexture* sfTexture_createFromFile(const char* filename, const sfIntRect* area)
return texture;
}

////////////////////////////////////////////////////////////
sfTexture* sfTexture_createSrgbFromFile(const char* filename, const sfIntRect* area)
{
sfTexture* texture = new sfTexture;

sf::IntRect rect;
if (area)
rect = sf::IntRect(area->left, area->top, area->width, area->height);

texture->This->setSrgb(true);

if (!texture->This->loadFromFile(filename, rect))
{
delete texture;
texture = NULL;
}

return texture;
}

////////////////////////////////////////////////////////////
sfTexture* sfTexture_createFromMemory(const void* data, size_t sizeInBytes, const sfIntRect* area)
Expand All @@ -86,6 +105,26 @@ sfTexture* sfTexture_createFromMemory(const void* data, size_t sizeInBytes, cons
return texture;
}

////////////////////////////////////////////////////////////
sfTexture* sfTexture_createSrgbFromMemory(const void* data, size_t sizeInBytes, const sfIntRect* area)
{
sfTexture* texture = new sfTexture;

sf::IntRect rect;
if (area)
rect = sf::IntRect(area->left, area->top, area->width, area->height);

texture->This->setSrgb(true);

if (!texture->This->loadFromMemory(data, sizeInBytes, rect))
{
delete texture;
texture = NULL;
}

return texture;
}


////////////////////////////////////////////////////////////
sfTexture* sfTexture_createFromStream(sfInputStream* stream, const sfIntRect* area)
Expand All @@ -108,6 +147,29 @@ sfTexture* sfTexture_createFromStream(sfInputStream* stream, const sfIntRect* ar
return texture;
}

////////////////////////////////////////////////////////////
sfTexture* sfTexture_createSrgbFromStream(sfInputStream* stream, const sfIntRect* area)
{
CSFML_CHECK_RETURN(stream, NULL);

sfTexture* texture = new sfTexture;

sf::IntRect rect;
if (area)
rect = sf::IntRect(area->left, area->top, area->width, area->height);

texture->This->setSrgb(true);

CallbackStream sfmlStream(stream);
if (!texture->This->loadFromStream(sfmlStream, rect))
{
delete texture;
texture = NULL;
}

return texture;
}


////////////////////////////////////////////////////////////
sfTexture* sfTexture_createFromImage(const sfImage* image, const sfIntRect* area)
Expand All @@ -129,6 +191,27 @@ sfTexture* sfTexture_createFromImage(const sfImage* image, const sfIntRect* area
return texture;
}

////////////////////////////////////////////////////////////
sfTexture* sfTexture_createSrgbFromImage(const sfImage* image, const sfIntRect* area)
{
CSFML_CHECK_RETURN(image, NULL);

sfTexture* texture = new sfTexture;

sf::IntRect rect;
if (area)
rect = sf::IntRect(area->left, area->top, area->width, area->height);

texture->This->setSrgb(true);

if (!texture->This->loadFromImage(image->This, rect))
{
delete texture;
texture = NULL;
}

return texture;
}

////////////////////////////////////////////////////////////
sfTexture* sfTexture_copy(const sfTexture* texture)
Expand Down Expand Up @@ -234,14 +317,6 @@ sfBool sfTexture_isSmooth(const sfTexture* texture)
return texture->This->isSmooth();
}


////////////////////////////////////////////////////////////
void sfTexture_setSrgb(sfTexture* texture, sfBool sRgb)
{
CSFML_CALL_PTR(texture, setSrgb(sRgb == sfTrue));
}


////////////////////////////////////////////////////////////
sfBool sfTexture_isSrgb(const sfTexture* texture)
{
Expand Down