Implementation of file input/output operations for images.
More...
#include <easy3d/fileio/image_io.h>
|
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.
|
|
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.
|
|
Implementation of file input/output operations for images.
◆ 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). The file format is determined by the file extension given in the file name.
- Parameters
-
file_name | The image file to load. |
data | Outputs 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 points to the 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 channels 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 |
|
width | Outputs image width in pixels. Unchanged if failed. |
height | Outputs image height in pixels. Unchanged if failed. |
channels | Outputs the number of 8-bit image channels per pixel. Unchanged if failed. |
requested_channels | User requested image channels. If non-zero, force to treat the image as if it has this number of components. For example, if you set it 4, you will always get RGBA output, but you can check channels to get the actual number of channels. |
flip_vertically | Flip 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);
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.
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. The file format is determined by the file extension given in the file name.
- Parameters
-
file_name | The file to which the image data will be saved. |
data | The 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 |
|
width | The width of the image, in pixels. |
height | The height of the image, in pixels. |
channels | The number of 8-bit image channels per pixel. |
flip_vertically | Flip 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:
- G:/3_code/Easy3D/easy3d/fileio/image_io.h
- G:/3_code/Easy3D/easy3d/fileio/image_io.cpp