Back to index

libsfml  1.6+dfsg2
Public Member Functions | Static Public Member Functions | Private Member Functions
sf::priv::ImageLoader Class Reference

ImageLoader load and save images from files ; Supported formats are : bmp, dds, jpg, png, tga, psd /////////////////////////////////////////////////////////. More...

#include <ImageLoader.hpp>

List of all members.

Public Member Functions

bool LoadImageFromFile (const std::string &Filename, std::vector< Color > &Pixels, unsigned int &Width, unsigned int &Height)
 Load pixels from an image file.
bool LoadImageFromMemory (const char *Data, std::size_t SizeInBytes, std::vector< Color > &Pixels, unsigned int &Width, unsigned int &Height)
 Load pixels from an image file in memory.
bool SaveImageToFile (const std::string &Filename, const std::vector< Color > &Pixels, unsigned int Width, unsigned int Height)
 Save pixels to an image file.

Static Public Member Functions

static ImageLoaderGetInstance ()
 Get the unique instance of the class.

Private Member Functions

 ImageLoader ()
 Default constructor.
 ~ImageLoader ()
 Destructor.
bool WriteJpg (const std::string &Filename, const std::vector< Color > &Pixels, unsigned int Width, unsigned int Height)
 Save a JPG image file.
bool WritePng (const std::string &Filename, const std::vector< Color > &Pixels, unsigned int Width, unsigned int Height)
 Save a PNG image file.

Detailed Description

ImageLoader load and save images from files ; Supported formats are : bmp, dds, jpg, png, tga, psd /////////////////////////////////////////////////////////.

Definition at line 45 of file ImageLoader.hpp.


Constructor & Destructor Documentation

Default constructor.

Default constructor /////////////////////////////////////////////////////////.

/////////////////////////////////////////////////////////

Definition at line 70 of file ImageLoader.cpp.

{
    // Nothing to do
}

Destructor.

Destructor /////////////////////////////////////////////////////////.

/////////////////////////////////////////////////////////

Definition at line 79 of file ImageLoader.cpp.

{
    // Nothing to do
}

Member Function Documentation

Get the unique instance of the class.

Get the unique instance of the class /////////////////////////////////////////////////////////.

Returns:
Reference to the ImageLoader instance

/////////////////////////////////////////////////////////

Definition at line 59 of file ImageLoader.cpp.

{
    static ImageLoader Instance;

    return Instance;
}
bool sf::priv::ImageLoader::LoadImageFromFile ( const std::string &  Filename,
std::vector< Color > &  Pixels,
unsigned int &  Width,
unsigned int &  Height 
)

Load pixels from an image file.

Load pixels from an image file /////////////////////////////////////////////////////////.

Parameters:
Filename: Path of image file to load
Pixels: Array of pixels to fill with loaded image
Width: Width of loaded image, in pixels
Height: Height of loaded image, in pixels
Returns:
True if loading was successful

/////////////////////////////////////////////////////////

Definition at line 88 of file ImageLoader.cpp.

{
    // Clear the array (just in case)
    Pixels.clear();

    // Load the image and get a pointer to the pixels in memory
    int ImgWidth, ImgHeight, ImgChannels;
    unsigned char* PixelsPtr = SOIL_load_image(Filename.c_str(), &ImgWidth, &ImgHeight, &ImgChannels, SOIL_LOAD_RGBA);

    if (PixelsPtr)
    {
        // Assign the image properties
        Width  = ImgWidth;
        Height = ImgHeight;

        // Copy the loaded pixels to the pixel buffer
        Pixels.resize(Width * Height);
        memcpy(&Pixels[0], PixelsPtr, Width * Height * 4);

        // Free the loaded pixels (they are now in our own pixel buffer)
        SOIL_free_image_data(PixelsPtr);

        return true;
    }
    else
    {
        // Error, failed to load the image
        std::cerr << "Failed to load image \"" << Filename << "\". Reason : " << SOIL_last_result() << std::endl;

        return false;
    }
}
bool sf::priv::ImageLoader::LoadImageFromMemory ( const char *  Data,
std::size_t  SizeInBytes,
std::vector< Color > &  Pixels,
unsigned int &  Width,
unsigned int &  Height 
)

Load pixels from an image file in memory.

Load pixels from an image file in memory /////////////////////////////////////////////////////////.

Parameters:
Data: Pointer to the file data in memory
SizeInBytes: Size of the data to load, in bytes
Pixels: Array of pixels to fill with loaded image
Width: Width of loaded image, in pixels
Height: Height of loaded image, in pixels
Returns:
True if loading was successful

/////////////////////////////////////////////////////////

Definition at line 125 of file ImageLoader.cpp.

{
    // Clear the array (just in case)
    Pixels.clear();

    // Load the image and get a pointer to the pixels in memory
    const unsigned char* Buffer = reinterpret_cast<const unsigned char*>(Data);
    int Size = static_cast<int>(SizeInBytes);
    int ImgWidth, ImgHeight, ImgChannels;
    unsigned char* PixelsPtr = SOIL_load_image_from_memory(Buffer, Size, &ImgWidth, &ImgHeight, &ImgChannels, SOIL_LOAD_RGBA);

    if (PixelsPtr)
    {
        // Assign the image properties
        Width  = ImgWidth;
        Height = ImgHeight;

        // Copy the loaded pixels to the pixel buffer
        Pixels.resize(Width * Height);
        memcpy(&Pixels[0], PixelsPtr, Width * Height * 4);

        // Free the loaded pixels (they are now in our own pixel buffer)
        SOIL_free_image_data(PixelsPtr);

        return true;
    }
    else
    {
        // Error, failed to load the image
        std::cerr << "Failed to load image from memory. Reason : " << SOIL_last_result() << std::endl;

        return false;
    }
}
bool sf::priv::ImageLoader::SaveImageToFile ( const std::string &  Filename,
const std::vector< Color > &  Pixels,
unsigned int  Width,
unsigned int  Height 
)

Save pixels to an image file.

Save pixels to an image file /////////////////////////////////////////////////////////.

Parameters:
Filename: Path of image file to save
Pixels: Array of pixels to save to image
Width: Width of image to save, in pixels
Height: Height of image to save, in pixels
Returns:
True if saving was successful

/////////////////////////////////////////////////////////

Definition at line 164 of file ImageLoader.cpp.

{
    // Deduce the image type from its extension
    int Type = -1;
    if (Filename.size() > 3)
    {
        std::string Extension = Filename.substr(Filename.size() - 3);
        if      (Extension == "bmp" || Extension == "BMP") Type = SOIL_SAVE_TYPE_BMP;
        else if (Extension == "tga" || Extension == "TGA") Type = SOIL_SAVE_TYPE_TGA;
        else if (Extension == "dds" || Extension == "DDS") Type = SOIL_SAVE_TYPE_DDS;

        // Special handling for PNG and JPG -- not handled by SOIL
        else if (Extension == "png" || Extension == "PNG") return WritePng(Filename, Pixels, Width, Height);
        else if (Extension == "jpg" || Extension == "JPG") return WriteJpg(Filename, Pixels, Width, Height);
    }

    if (Type == -1)
    {
        // Error, incompatible type
        std::cerr << "Failed to save image \"" << Filename << "\". Reason : this image format is not supported" << std::endl;
        return false;
    }

    // Finally save the image
    const unsigned char* PixelsPtr = reinterpret_cast<const unsigned char*>(&Pixels[0]);
    if (!SOIL_save_image(Filename.c_str(), Type, static_cast<int>(Width), static_cast<int>(Height), 4, PixelsPtr))
    {
        // Error, failed to save the image
        std::cerr << "Failed to save image \"" << Filename << "\". Reason : " << SOIL_last_result() << std::endl;
        return false;
    }

    return true;
}

Here is the call graph for this function:

bool sf::priv::ImageLoader::WriteJpg ( const std::string &  Filename,
const std::vector< Color > &  Pixels,
unsigned int  Width,
unsigned int  Height 
) [private]

Save a JPG image file.

Save a JPG image file /////////////////////////////////////////////////////////.

Parameters:
Filename: Path of image file to save
Pixels: Array of pixels to save to image
Width: Width of image to save, in pixels
Height: Height of image to save, in pixels
Returns:
True if saving was successful

/////////////////////////////////////////////////////////

Definition at line 203 of file ImageLoader.cpp.

{
    // Open the file to write in
    FILE* File = fopen(Filename.c_str(), "wb");
    if (!File)
    {
        std::cerr << "Failed to save image file \"" << Filename << "\". Reason : cannot open file" << std::endl;
        return false;
    }

    // Initialize the error handler
    jpeg_compress_struct CompressInfo;
    jpeg_error_mgr ErrorManager;
    CompressInfo.err = jpeg_std_error(&ErrorManager);

    // Initialize all the writing and compression infos
    jpeg_create_compress(&CompressInfo);
    CompressInfo.image_width      = Width;
    CompressInfo.image_height     = Height;
    CompressInfo.input_components = 3;
    CompressInfo.in_color_space   = JCS_RGB;
    jpeg_stdio_dest(&CompressInfo, File);
    jpeg_set_defaults(&CompressInfo);
    jpeg_set_quality(&CompressInfo, 90, TRUE);

    // Get rid of the aplha channel
    std::vector<Uint8> PixelsBuffer(Width * Height * 3);
    for (std::size_t i = 0; i < Pixels.size(); ++i)
    {
        PixelsBuffer[i * 3 + 0] = Pixels[i].r;
        PixelsBuffer[i * 3 + 1] = Pixels[i].g;
        PixelsBuffer[i * 3 + 2] = Pixels[i].b;
    }
    Uint8* PixelsPtr = &PixelsBuffer[0];

    // Start compression
    jpeg_start_compress(&CompressInfo, TRUE);

    // Write each row of the image
    while (CompressInfo.next_scanline < CompressInfo.image_height)
    {
        JSAMPROW RowPointer = PixelsPtr + (CompressInfo.next_scanline * Width * 3);
        jpeg_write_scanlines(&CompressInfo, &RowPointer, 1);
    }

    // Finish compression
    jpeg_finish_compress(&CompressInfo);
    jpeg_destroy_compress(&CompressInfo);

    // Close the file
    fclose(File);

    return true;
}

Here is the caller graph for this function:

bool sf::priv::ImageLoader::WritePng ( const std::string &  Filename,
const std::vector< Color > &  Pixels,
unsigned int  Width,
unsigned int  Height 
) [private]

Save a PNG image file.

Save a PNG image file /////////////////////////////////////////////////////////.

Parameters:
Filename: Path of image file to save
Pixels: Array of pixels to save to image
Width: Width of image to save, in pixels
Height: Height of image to save, in pixels
Returns:
True if saving was successful

/////////////////////////////////////////////////////////

Definition at line 262 of file ImageLoader.cpp.

{
    // Open the file to write in
    FILE* File = fopen(Filename.c_str(), "wb");
    if (!File)
    {
        std::cerr << "Failed to save image file \"" << Filename << "\". Reason : cannot open file" << std::endl;
        return false;
    }

    // Create the main PNG structure
    png_structp Png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, &PngErrorHandler, NULL);
    if (!Png)
    {
        fclose(File);
        std::cerr << "Failed to save image file \"" << Filename << "\". Reason : cannot allocate PNG write structure" << std::endl;
        return false;
    }

    // Initialize the image informations
    png_infop PngInfo = png_create_info_struct(Png);
    if (!PngInfo)
    {
        fclose(File);
        png_destroy_write_struct(&Png, NULL);
        std::cerr << "Failed to save image file \"" << Filename << "\". Reason : cannot allocate PNG info structure" << std::endl;
        return false;
    }

    // For proper error handling...
    if (setjmp(Png->jmpbuf))
    {
        png_destroy_write_struct(&Png, &PngInfo);
        return false;
    }

    // Link the file to the PNG structure
    png_init_io(Png, File);

    // Set the image informations
    png_set_IHDR(Png, PngInfo, Width, Height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);

    // Write the header
    png_write_info(Png, PngInfo);

    // Get the pointers to the pixels rows into an array
    png_byte* PixelsPtr = (png_byte*)&Pixels[0];
    std::vector<png_byte*> RowPointers(Height);
    for (unsigned int i = 0; i < Height; ++i)
    {
        RowPointers[i] = PixelsPtr;
        PixelsPtr += Width * 4;
    }

    // Write pixels row by row
    png_set_rows(Png, PngInfo, &RowPointers[0]);
    png_write_png(Png, PngInfo, PNG_TRANSFORM_IDENTITY, NULL);

    // Finish writing the file
    png_write_end(Png, PngInfo);

    // Cleanup resources
    png_destroy_write_struct(&Png, &PngInfo);
    fclose(File);

    return true;
}

Here is the caller graph for this function:


The documentation for this class was generated from the following files: