2009-01-29 00:18:34 +08:00
|
|
|
/*
|
2010-01-07 04:37:29 +08:00
|
|
|
* DSFML - SFML Library wrapper for the D programming language.
|
|
|
|
* Copyright (C) 2008 Julien Dagorn (sirjulio13@gmail.com)
|
|
|
|
* Copyright (C) 2010 Andreas Hollandt
|
2009-01-29 00:18:34 +08:00
|
|
|
*
|
2010-01-07 04:37:29 +08:00
|
|
|
* This software is provided 'as-is', without any express or
|
|
|
|
* implied warranty. In no event will the authors be held
|
|
|
|
* liable for any damages arising from the use of this software.
|
2009-01-29 00:18:34 +08:00
|
|
|
*
|
2010-01-07 04:37:29 +08:00
|
|
|
* Permission is granted to anyone to use this software for any purpose,
|
|
|
|
* including commercial applications, and to alter it and redistribute
|
|
|
|
* it freely, subject to the following restrictions:
|
2009-01-29 00:18:34 +08:00
|
|
|
*
|
2010-01-07 04:37:29 +08:00
|
|
|
* 1. The origin of this software must not be misrepresented;
|
|
|
|
* you must not claim that you wrote the original software.
|
|
|
|
* If you use this software in a product, an acknowledgment
|
|
|
|
* in the product documentation would be appreciated but
|
|
|
|
* is not required.
|
2009-01-29 00:18:34 +08:00
|
|
|
*
|
2010-01-07 04:37:29 +08:00
|
|
|
* 2. Altered source versions must be plainly marked as such,
|
|
|
|
* and must not be misrepresented as being the original software.
|
2009-01-29 00:18:34 +08:00
|
|
|
*
|
2010-01-07 04:37:29 +08:00
|
|
|
* 3. This notice may not be removed or altered from any
|
|
|
|
* source distribution.
|
2009-01-29 00:18:34 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
module dsfml.graphics.image;
|
|
|
|
|
2010-01-08 06:00:45 +08:00
|
|
|
import dsfml.graphics.color,
|
2010-01-07 04:25:45 +08:00
|
|
|
dsfml.graphics.rect;
|
2010-03-16 10:04:52 +08:00
|
|
|
// dsfml.graphics.renderwindow;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-01-07 04:25:45 +08:00
|
|
|
import dsfml.system.common,
|
|
|
|
dsfml.system.exception,
|
|
|
|
dsfml.system.stringutil;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Image is the low-level class for loading and
|
|
|
|
* manipulating images
|
|
|
|
*/
|
2009-01-29 00:18:34 +08:00
|
|
|
class Image : DSFMLObject
|
|
|
|
{
|
2010-01-08 02:07:22 +08:00
|
|
|
package:
|
2010-03-16 10:04:52 +08:00
|
|
|
this(SFMLClass ptr)
|
2010-01-08 02:07:22 +08:00
|
|
|
{
|
|
|
|
super(ptr, true);
|
|
|
|
}
|
2010-03-03 23:29:16 +08:00
|
|
|
|
2010-01-08 02:07:22 +08:00
|
|
|
public:
|
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Default constructor
|
|
|
|
*/
|
2010-01-07 04:37:29 +08:00
|
|
|
this()
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
super(sfImage_Create());
|
|
|
|
}
|
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Construct an empty image
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* width = Image width
|
|
|
|
* height = Image height
|
|
|
|
* col = Image color (black by default)
|
|
|
|
*/
|
2010-01-07 04:37:29 +08:00
|
|
|
this(uint width, uint height, Color col = Color.BLACK)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
super(sfImage_CreateFromColor(width, height, col));
|
|
|
|
}
|
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Construct the image from a file
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* filename = Path of the image file to load
|
|
|
|
*
|
|
|
|
* Throws:
|
|
|
|
* LoadingException if filename is empty or null.
|
|
|
|
*/
|
2010-01-07 04:37:29 +08:00
|
|
|
this(string filename)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
2010-01-07 04:37:29 +08:00
|
|
|
if (filename is null || filename.length == 0)
|
|
|
|
throw new LoadingException("LoadingException : Filename is invalid.");
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
super(sfImage_CreateFromFile(toStringz(filename)));
|
|
|
|
}
|
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Construct the image from a file in memory
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* data = array of data in memory
|
|
|
|
* Throws:
|
|
|
|
* LoadingException if data is empty or null.
|
|
|
|
*/
|
2010-01-07 04:37:29 +08:00
|
|
|
this(ubyte[] data)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
2010-01-07 04:37:29 +08:00
|
|
|
if (data is null || data.length == 0)
|
2010-03-10 02:58:41 +08:00
|
|
|
throw new LoadingException("Memory stream is invalid.");
|
2010-01-07 04:37:29 +08:00
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
super(sfImage_CreateFromMemory(data.ptr, data.length));
|
|
|
|
}
|
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Construct the image directly from an array of pixels
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* width = Image width
|
|
|
|
* height = Image height
|
|
|
|
* data = array of pixels in memory (assumed format is RGBA)
|
|
|
|
*
|
|
|
|
* Throws:
|
|
|
|
* LoadingException if data length doesn't match Width * Height * 4
|
|
|
|
*/
|
2010-01-07 04:37:29 +08:00
|
|
|
this(uint width, uint height, ubyte[] data)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
2010-01-07 04:37:29 +08:00
|
|
|
if (width * height * 4 != data.length)
|
2010-03-10 02:58:41 +08:00
|
|
|
throw new LoadingException("Pixels array length doesn't match the specified size.");
|
2010-01-07 04:37:29 +08:00
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
super(sfImage_CreateFromPixels(width, height, data.ptr));
|
|
|
|
}
|
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
override void dispose()
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
sfImage_Destroy(m_ptr);
|
|
|
|
}
|
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Save the content of the image to a file
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* filename = Path of the file to save (overwritten if already exist)
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* True if saving was successful
|
|
|
|
*/
|
2010-01-07 04:37:29 +08:00
|
|
|
bool saveToFile(string filename)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
return cast(bool)sfImage_SaveToFile(m_ptr, toStringz(filename));
|
|
|
|
}
|
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Create an empty image
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* width = Image width
|
|
|
|
* height = Image height
|
|
|
|
* col = Image color (black by default)
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* True if creation was successful
|
|
|
|
*/
|
2010-01-07 04:37:29 +08:00
|
|
|
bool create(uint width, uint height, Color col = Color.BLACK)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
m_ptr = sfImage_CreateFromColor(width, height, col);
|
|
|
|
return (m_ptr !is null);
|
|
|
|
}
|
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Create transparency mask from a specified colorkey
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* colorKey = Color to become transparent
|
|
|
|
* alpha = Alpha value to use for transparent pixels (0 by default)
|
|
|
|
*/
|
2010-01-07 04:37:29 +08:00
|
|
|
void createMaskFromColor(Color colorKey, ubyte alpha = 0)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
sfImage_CreateMaskFromColor(m_ptr, colorKey, alpha);
|
|
|
|
}
|
2010-03-16 10:04:52 +08:00
|
|
|
|
|
|
|
/+
|
|
|
|
/**
|
|
|
|
* Create the image from the current contents of the
|
|
|
|
* given window
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* window = Window to capture
|
|
|
|
* sourceRect = Sub-rectangle of the screen to copy (empty by default - entire image)
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* True if copy was successful
|
|
|
|
*/
|
|
|
|
bool copyScreen(RenderWindow window, IntRect sourceRect = IntRect())
|
|
|
|
{
|
|
|
|
return cast(bool)sfImage_CopyScreen(m_ptr, window.nativePointer, sourceRect);
|
|
|
|
}
|
|
|
|
+/
|
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Copy pixels from another image onto this one.
|
|
|
|
* This function does a slow pixel copy and should only
|
|
|
|
* be used at initialization time
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* source = Source image to copy
|
|
|
|
* destX = X coordinate of the destination position
|
|
|
|
* destY = Y coordinate of the destination position
|
|
|
|
* sourceRect = Sub-rectangle of the source image to copy
|
|
|
|
*/
|
2010-03-16 10:04:52 +08:00
|
|
|
void copyImage(Image source, uint destX, uint destY, IntRect sourceRect = IntRect())
|
2010-01-07 04:37:29 +08:00
|
|
|
{
|
2010-03-16 10:04:52 +08:00
|
|
|
sfImage_CopyImage(m_ptr, source.nativePointer, destX, destY, sourceRect);
|
2010-01-07 04:37:29 +08:00
|
|
|
}
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Change the color of a pixel
|
|
|
|
* Don't forget to call Update when you end modifying pixels
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* x = X coordinate of pixel in the image
|
|
|
|
* y = Y coordinate of pixel in the image
|
|
|
|
* col = New color for pixel (X, Y)
|
|
|
|
*/
|
2010-01-07 04:37:29 +08:00
|
|
|
void setPixel(uint x, uint y, Color col)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
sfImage_SetPixel(m_ptr, x, y, col);
|
|
|
|
}
|
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Get a pixel from the image
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* x = X coordinate of pixel in the image
|
|
|
|
* y = Y coordinate of pixel in the image
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Color of pixel (x, y)
|
|
|
|
*/
|
2010-01-07 04:37:29 +08:00
|
|
|
Color getPixel(uint x, uint y)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
return sfImage_GetPixel(m_ptr, x, y);
|
|
|
|
}
|
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Get an array of pixels (8 bits integers RGBA)
|
|
|
|
* Array size is GetWidth() x GetHeight() x 4
|
|
|
|
* This array becomes invalid if you reload or resize the image
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* array of pixels
|
|
|
|
*/
|
2010-01-07 04:37:29 +08:00
|
|
|
ubyte[] getPixelsArray()
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
2010-03-16 10:04:52 +08:00
|
|
|
return sfImage_GetPixelsPtr(m_ptr)[0..width() * height() * 4];
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Bind the image for rendering
|
|
|
|
*/
|
2010-01-07 04:37:29 +08:00
|
|
|
void bind()
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
sfImage_Bind(m_ptr);
|
|
|
|
}
|
|
|
|
|
2010-03-16 10:04:52 +08:00
|
|
|
/**
|
|
|
|
* Update a sub-rectangle of the image from an array of pixels
|
|
|
|
*
|
|
|
|
* Warning: for performances reasons, this function doesn't
|
|
|
|
* perform any check; thus you're responsible of ensuring that
|
|
|
|
* rectangle does not exceed the image size, and that
|
|
|
|
* pixels contains enough elements.
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* rectangle = sub rectangle of the image to update
|
|
|
|
* pixels = array of pixels to write to the image
|
|
|
|
*/
|
|
|
|
void updatePixels(ubyte[] pixels, IntRect rectangle)
|
|
|
|
{
|
|
|
|
sfImage_UpdatePixels(m_ptr, pixels.ptr, rectangle);
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
{
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Enable or disable image smooth filter.
|
|
|
|
* This parameter is enabled by default
|
|
|
|
*
|
|
|
|
* Params:
|
2010-03-16 10:04:52 +08:00
|
|
|
* s = True to enable smoothing filter, false to disable it
|
2010-03-03 23:29:16 +08:00
|
|
|
*/
|
2010-03-16 10:04:52 +08:00
|
|
|
void smooth(bool s)
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
2010-03-16 10:04:52 +08:00
|
|
|
sfImage_SetSmooth(m_ptr, s);
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Return the width of the image
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Width in pixels
|
|
|
|
*/
|
2010-03-16 10:04:52 +08:00
|
|
|
uint width()
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
return sfImage_GetWidth(m_ptr);
|
|
|
|
}
|
|
|
|
|
2010-01-07 04:37:29 +08:00
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Return the height of the image
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Height in pixels
|
|
|
|
*/
|
2010-03-16 10:04:52 +08:00
|
|
|
uint height()
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
return sfImage_GetHeight(m_ptr);
|
|
|
|
}
|
2010-01-07 04:37:29 +08:00
|
|
|
|
|
|
|
/**
|
2010-03-03 23:29:16 +08:00
|
|
|
* Tells whether the smooth filtering is enabled or not
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* True if image smoothing is enabled
|
|
|
|
*/
|
2010-03-16 10:04:52 +08:00
|
|
|
bool smooth()
|
2010-01-07 04:37:29 +08:00
|
|
|
{
|
|
|
|
return cast(bool)sfImage_IsSmooth(m_ptr);
|
|
|
|
}
|
2010-03-16 10:04:52 +08:00
|
|
|
}
|
2010-03-03 23:29:16 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
static extern (C)
|
2010-01-08 02:07:22 +08:00
|
|
|
{
|
2010-03-16 10:04:52 +08:00
|
|
|
SFMLClass function() sfImage_Create;
|
|
|
|
SFMLClass function(uint, uint, Color) sfImage_CreateFromColor;
|
|
|
|
SFMLClass function(uint, uint, ubyte*) sfImage_CreateFromPixels;
|
|
|
|
SFMLClass function(cchar*) sfImage_CreateFromFile;
|
|
|
|
SFMLClass function(ubyte* ,size_t) sfImage_CreateFromMemory;
|
|
|
|
void function(SFMLClass) sfImage_Destroy;
|
|
|
|
int function(SFMLClass, cchar*) sfImage_SaveToFile;
|
|
|
|
void function(SFMLClass, Color, ubyte) sfImage_CreateMaskFromColor;
|
|
|
|
SFMLClass function(SFMLClass) sfImage_Copy;
|
|
|
|
int function(SFMLClass, SFMLClass, IntRect) sfImage_CopyScreen;
|
|
|
|
void function(SFMLClass, SFMLClass, uint, uint, IntRect) sfImage_CopyImage;
|
|
|
|
void function(SFMLClass, uint, uint, Color) sfImage_SetPixel;
|
|
|
|
Color function(SFMLClass, uint, uint) sfImage_GetPixel;
|
|
|
|
ubyte* function(SFMLClass) sfImage_GetPixelsPtr;
|
|
|
|
void function(SFMLClass) sfImage_Bind;
|
|
|
|
void function(SFMLClass, int) sfImage_SetSmooth;
|
|
|
|
uint function(SFMLClass) sfImage_GetWidth;
|
|
|
|
uint function(SFMLClass) sfImage_GetHeight;
|
|
|
|
int function(SFMLClass) sfImage_IsSmooth;
|
|
|
|
void function(SFMLClass, ubyte*, IntRect) sfImage_UpdatePixels;
|
2010-01-07 04:37:29 +08:00
|
|
|
}
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2010-03-03 23:29:16 +08:00
|
|
|
mixin(loadFromSharedLib2("csfml-graphics", "sfImage",
|
2010-03-16 10:04:52 +08:00
|
|
|
"Create", "CreateFromColor", "CreateFromPixels", "CreateFromFile", "CreateFromMemory", "Destroy", "SaveToFile",
|
|
|
|
"CreateMaskFromColor", "Copy", "CopyScreen", "CopyImage", "SetPixel", "GetPixel", "GetPixelsPtr", "Bind", "SetSmooth", "GetWidth",
|
|
|
|
"GetHeight", "IsSmooth", "UpdatePixels"));
|
2010-01-08 02:07:22 +08:00
|
|
|
}
|