Easy3D 2.5.3
ImageIO Class Reference

Implementation of file input/output operations for images. More...

#include <easy3d/fileio/image_io.h>

Static Public Member Functions

static bool load (const std::string &file_name, std::vector< unsigned char > &data, int &width, int &height, int &channels, int requested_channels=0, bool flip_vertically=true)
 Load image data from a file. The following formats are supported JPG/JPEG, PNG, BMP, PSD, TGA, GIF, HDR, PNM (.ppm and .pgm). File format is determined by the file extension given in the file name. More...
 
static bool save (const std::string &file_name, const std::vector< unsigned char > &data, int width, int height, int channels, bool flip_vertically=false)
 Write image data into a file. The following formats are supported JPG/JPEG, PNG, BMP, and TGA. File format is determined by the file extension given in the file name. More...
 

Detailed Description

Implementation of file input/output operations for images.

Member Function Documentation

◆ load()

bool load ( const std::string &  file_name,
std::vector< unsigned char > &  data,
int &  width,
int &  height,
int &  channels,
int  requested_channels = 0,
bool  flip_vertically = true 
)
static

Load image data from a file. The following formats are supported JPG/JPEG, PNG, BMP, PSD, TGA, GIF, HDR, PNM (.ppm and .pgm). File format is determined by the file extension given in the file name.

Parameters
file_nameThe image file to load.
dataOutputs the image data. Data will be empty if the specified image file doesn't exist, allocation failed, or the image is corrupt or invalid. The image data returned is a 1D array of 'unsigned char' which points to the pixel data, or empty when failed. The pixel data consists of 'height' scanlines of 'width' pixels, with each pixel consisting of N interleaved 8-bit channels/components; the first pixel pointed to is top-left-most in the image. There is no padding between image scanlines or between pixels, regardless of format. The number of channels N is 'requested_channels' if it is non-zero, or 'channels' otherwise. An output image with N components has the following components interleaved in this order in each pixel: N=#channels components 1 grey 2 grey, alpha 3 red, green, blue 4 red, green, blue, alpha
widthOutputs image width in pixels. Unchanged if failed.
heightOutputs image height in pixels. Unchanged if failed.
channelsOutputs the number of 8-bit image channels per pixel. Unchanged if failed.
requested_channelsUser requested image channels. If non-zero, force to treat the image as if has this number of components, e.g. if you set it 4, you will always get RGBA output, but you can check channels to get the actual number of channels.
flip_verticallyFlip the image data vertically if it is true (default value). This is convenient for OpenGL applications where the first pixel in the output array is expected to be the bottom left corner of the image.
Returns
true on success or false if failed.

Example usage:

int width, height, channels;
std::vector<unsigned char> data;
bool success = ImageIO::load(data, file_name, &width, &height, &channels, 0);
// ... process data if true returned ...
static bool load(const std::string &file_name, std::vector< unsigned char > &data, int &width, int &height, int &channels, int requested_channels=0, bool flip_vertically=true)
Load image data from a file. The following formats are supported JPG/JPEG, PNG, BMP,...
Definition: image_io.cpp:41

◆ save()

bool save ( const std::string &  file_name,
const std::vector< unsigned char > &  data,
int  width,
int  height,
int  channels,
bool  flip_vertically = false 
)
static

Write image data into a file. The following formats are supported JPG/JPEG, PNG, BMP, and TGA. File format is determined by the file extension given in the file name.

Parameters
file_nameThe file to which the image data will be save.
dataThe image data. The image data storage must follow N=#channels components 1 grey 2 grey, alpha 3 red, green, blue 4 red, green, blue, alpha
widthThe width of the image, in pixels.
heightThe height of the image, in pixels.
channelsThe number of 8-bit image channels per pixel.
flip_verticallyFlip the image data vertically before writing.
Returns
Return true on success or false if failed.

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