PhoenixPNG  0.1.0
Set of tools to ease use of png file
Loading...
Searching...
No Matches
PImagePng Class Reference

Class which manipulate png image. More...

#include <PImagePng.h>

Public Types

enum  ColorType { RGB = PNG_COLOR_TYPE_RGB , RGBA = PNG_COLOR_TYPE_RGB_ALPHA }
 

Public Member Functions

void clear ()
 Clear the image and buffer.
 
bool createImage (size_t width, size_t height, PImagePng::ColorType colorType=PImagePng::RGB)
 Create an image.
 
void fill (color_t red, color_t green, color_t blue, color_t alpha=255)
 Fill the image with the given color.
 
void getColor (size_t idxWidth, size_t idxHeight, color_t &red, color_t &green, color_t &blue) const
 Get the color of the pixel at (idxWidth, idxHeight)
 
void getColor (size_t idxWidth, size_t idxHeight, color_t &red, color_t &green, color_t &blue, color_t &alpha) const
 Get the color of the pixel at (idxWidth, idxHeight)
 
png_byte * getData ()
 Get the buffer data of the current PImagePng.
 
const png_byte * getData () const
 Get the buffer data of the current PImagePng.
 
unsigned int getHeight () const
 Get the height of the image.
 
NbColorByte getNbBytePerPixel () const
 Get the number of bytes per pixel.
 
unsigned int getWidth () const
 Get the width of the image.
 
PImagePngoperator= (const PImagePng &other)
 Definition of equal operator of PImagePng.
 
 PImagePng ()
 Default constructor of PImagePng.
 
 PImagePng (const PImagePng &other)
 Copy constructor of PImagePng.
 
bool read (const PPath &fileName)
 Read the given png image.
 
template<typename T>
void setColor (const T *matValue, size_t nbRow, size_t nbCol, const PColorMap &colorMap)
 Set the color of the image by respect to a given matrix of values and the colorMap.
 
void setColor (size_t idxWidth, size_t idxHeight, color_t red, color_t green, color_t blue)
 Set the color of the pixel at (idxWidth, idxHeight)
 
void setColor (size_t idxWidth, size_t idxHeight, color_t red, color_t green, color_t blue, color_t alpha)
 Set the color of the pixel at (idxWidth, idxHeight)
 
bool write (const PPath &fileName)
 Write a png file.
 
virtual ~PImagePng ()
 Destructor of PImagePng.
 

Protected Member Functions

void copyPImagePng (const PImagePng &other)
 Copy function of PImagePng.
 

Private Member Functions

void initialisationPImagePng ()
 Initialisation function of the class PImagePng.
 

Private Attributes

png_byte * p_buffer
 Buffer use to store the image data.
 
PImagePng::ColorType p_colorType
 Type of the color of the current image.
 
png_image p_image
 Png image.
 
NbColorByte p_nbBytePerPixel
 Number of bytes per pixel (3 for RGB, 4 for RGBA)
 

Detailed Description

Class which manipulate png image.

Definition at line 19 of file PImagePng.h.

Member Enumeration Documentation

◆ ColorType

Enumerator
RGB 
RGBA 

Definition at line 21 of file PImagePng.h.

21 {
22 RGB = PNG_COLOR_TYPE_RGB,
23 RGBA = PNG_COLOR_TYPE_RGB_ALPHA
24 };

Constructor & Destructor Documentation

◆ PImagePng() [1/2]

PImagePng::PImagePng ( )

Default constructor of PImagePng.

Definition at line 12 of file PImagePng.cpp.

12 {
14}
void initialisationPImagePng()
Initialisation function of the class PImagePng.

References initialisationPImagePng().

Referenced by copyPImagePng(), operator=(), and PImagePng().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ PImagePng() [2/2]

PImagePng::PImagePng ( const PImagePng & other)

Copy constructor of PImagePng.

Parameters
other: class to copy

Definition at line 19 of file PImagePng.cpp.

19 {
21 copyPImagePng(other);
22}
void copyPImagePng(const PImagePng &other)
Copy function of PImagePng.

References copyPImagePng(), initialisationPImagePng(), and PImagePng().

+ Here is the call graph for this function:

◆ ~PImagePng()

PImagePng::~PImagePng ( )
virtual

Destructor of PImagePng.

Definition at line 25 of file PImagePng.cpp.

25 {
26 clear();
27}
void clear()
Clear the image and buffer.

References clear().

+ Here is the call graph for this function:

Member Function Documentation

◆ clear()

void PImagePng::clear ( )

Clear the image and buffer.

Definition at line 122 of file PImagePng.cpp.

122 {
123 if(p_buffer == NULL){
124 png_image_free(&p_image);
125 }else{
126 free(p_buffer);
127 }
128}
png_image p_image
Png image.
Definition PImagePng.h:63
png_byte * p_buffer
Buffer use to store the image data.
Definition PImagePng.h:65

References p_buffer, and p_image.

Referenced by ~PImagePng().

+ Here is the caller graph for this function:

◆ copyPImagePng()

void PImagePng::copyPImagePng ( const PImagePng & other)
protected

Copy function of PImagePng.

Parameters
other: class to copy

Definition at line 232 of file PImagePng.cpp.

232 {
233 createImage(other.getWidth(), other.getHeight(), other.p_colorType);
234 memcpy(p_buffer, other.p_buffer, sizeof(char)*other.getWidth()*other.getHeight()*p_nbBytePerPixel);
235}
bool createImage(size_t width, size_t height, PImagePng::ColorType colorType=PImagePng::RGB)
Create an image.
Definition PImagePng.cpp:44
PImagePng::ColorType p_colorType
Type of the color of the current image.
Definition PImagePng.h:69
NbColorByte p_nbBytePerPixel
Number of bytes per pixel (3 for RGB, 4 for RGBA)
Definition PImagePng.h:67
unsigned int getHeight() const
Get the height of the image.
unsigned int getWidth() const
Get the width of the image.

References createImage(), getHeight(), getWidth(), p_buffer, p_colorType, p_nbBytePerPixel, and PImagePng().

Referenced by operator=(), and PImagePng().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createImage()

bool PImagePng::createImage ( size_t width,
size_t height,
PImagePng::ColorType colorType = PImagePng::RGB )

Create an image.

Parameters
width: width of the image
height: height of the image
colorType: color type of the image (ex RGB)
Returns
true on success, false otherwise

Definition at line 44 of file PImagePng.cpp.

44 {
45 p_image.width = width;
46 p_image.height = height;
47 p_colorType = colorType;
48 if(colorType == PImagePng::RGB){
49 p_image.format = PNG_FORMAT_RGB;
51 }else if(colorType == PImagePng::RGBA){
52 p_image.format = PNG_FORMAT_RGBA;
54 }
55 if(p_buffer != NULL){
56 free(p_buffer);
57 }
58 p_buffer = (png_byte*)malloc(PNG_IMAGE_SIZE(p_image));
59
60// png_structp pngStruct = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
61// if(pngStruct == NULL){return false;}
62// png_infop infoPtr = png_create_info_struct(pngStruct);
63// if(infoPtr == NULL){return false;}
64// png_set_IHDR(pngStruct, infoPtr, width, height,
65// 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
66// PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
67 return true;
68}

References p_buffer, p_colorType, p_image, p_nbBytePerPixel, RGB, and RGBA.

Referenced by copyPImagePng().

+ Here is the caller graph for this function:

◆ fill()

void PImagePng::fill ( color_t red,
color_t green,
color_t blue,
color_t alpha = 255 )

Fill the image with the given color.

Parameters
red: red proportion
green: green proportion
blue: blue proportion
alpha: transparence proportion

Definition at line 136 of file PImagePng.cpp.

136 {
137 for(png_uint_32 j(0u); j < p_image.height; ++j){
138 for(png_uint_32 i(0u); i < p_image.width; ++i){
139 setColor(i, j, red, green, blue);
140 }
141 }
142}
void setColor(size_t idxWidth, size_t idxHeight, color_t red, color_t green, color_t blue)
Set the color of the pixel at (idxWidth, idxHeight)

References p_image, and setColor().

+ Here is the call graph for this function:

◆ getColor() [1/2]

void PImagePng::getColor ( size_t idxWidth,
size_t idxHeight,
color_t & red,
color_t & green,
color_t & blue ) const

Get the color of the pixel at (idxWidth, idxHeight)

Parameters
idxWidth: index of the pixel on the image width
idxHeight: index of the pixel on the image height
[out]red: red proportion
[out]green: green proportion
[out]blue: blue proportion

Definition at line 181 of file PImagePng.cpp.

181 {
182 color_t alpha(255);
183 getColor(idxWidth, idxHeight, red, green , blue, alpha);
184}
unsigned char color_t
Type des of a color.
Definition PColorMap.h:14
void getColor(size_t idxWidth, size_t idxHeight, color_t &red, color_t &green, color_t &blue) const
Get the color of the pixel at (idxWidth, idxHeight)

References getColor().

Referenced by getColor().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getColor() [2/2]

void PImagePng::getColor ( size_t idxWidth,
size_t idxHeight,
color_t & red,
color_t & green,
color_t & blue,
color_t & alpha ) const

Get the color of the pixel at (idxWidth, idxHeight)

Parameters
idxWidth: index of the pixel on the image width
idxHeight: index of the pixel on the image height
[out]red: red proportion
[out]green: green proportion
[out]blue: blue proportion
[out]alpha: alpha proportion

Definition at line 194 of file PImagePng.cpp.

194 {
195 size_t index((idxHeight*p_image.width + idxWidth)*p_nbBytePerPixel);
196 red = p_buffer[index];
197 green = p_buffer[index + 1lu];
198 blue = p_buffer[index + 2lu];
199 if(p_nbBytePerPixel >= 4lu){
200 alpha = p_buffer[index + 3lu];
201 }
202}

References p_buffer, p_image, and p_nbBytePerPixel.

◆ getData() [1/2]

png_byte * PImagePng::getData ( )

Get the buffer data of the current PImagePng.

Returns
buffer data of the current PImagePng

Definition at line 222 of file PImagePng.cpp.

222{return p_buffer;}

References p_buffer.

◆ getData() [2/2]

const png_byte * PImagePng::getData ( ) const

Get the buffer data of the current PImagePng.

Returns
buffer data of the current PImagePng

Definition at line 217 of file PImagePng.cpp.

217{return p_buffer;}

References p_buffer.

◆ getHeight()

unsigned int PImagePng::getHeight ( ) const

Get the height of the image.

Returns
height of the image

Definition at line 212 of file PImagePng.cpp.

212{return p_image.height;}

References p_image.

Referenced by copyPImagePng().

+ Here is the caller graph for this function:

◆ getNbBytePerPixel()

NbColorByte PImagePng::getNbBytePerPixel ( ) const

Get the number of bytes per pixel.

Returns
number of bytes per pixel

Definition at line 227 of file PImagePng.cpp.

227{return p_nbBytePerPixel;}

References p_nbBytePerPixel.

◆ getWidth()

unsigned int PImagePng::getWidth ( ) const

Get the width of the image.

Returns
width of the image

Definition at line 207 of file PImagePng.cpp.

207{return p_image.width;}

References p_image.

Referenced by copyPImagePng().

+ Here is the caller graph for this function:

◆ initialisationPImagePng()

void PImagePng::initialisationPImagePng ( )
private

Initialisation function of the class PImagePng.

Definition at line 238 of file PImagePng.cpp.

238 {
239 memset(&p_image, 0, (sizeof p_image));
240 p_image.version = PNG_IMAGE_VERSION; //We have to set the PNG version for this image
241
242 p_buffer = NULL;
244}

References p_buffer, p_image, and p_nbBytePerPixel.

Referenced by PImagePng(), and PImagePng().

+ Here is the caller graph for this function:

◆ operator=()

PImagePng & PImagePng::operator= ( const PImagePng & other)

Definition of equal operator of PImagePng.

Parameters
other: class to copy
Returns
copied class

Definition at line 33 of file PImagePng.cpp.

33 {
34 copyPImagePng(other);
35 return *this;
36}

References copyPImagePng(), and PImagePng().

+ Here is the call graph for this function:

◆ read()

bool PImagePng::read ( const PPath & fileName)

Read the given png image.

Parameters
fileName: name of the file to be read
Returns
true on success, false otherwise

Definition at line 74 of file PImagePng.cpp.

74 {
75 if(fileName == ""){return false;}
76 if(!check_is_png(fileName)){
77 std::cerr << "PImagePng::read : file is not a png '"<<fileName<<"'" << std::endl;
78 return false;
79 }
80 if(png_image_begin_read_from_file(&p_image, fileName.c_str()) == 0){
81 std::cerr << "PImagePng::read : cannot read png image '"<<fileName<<"'" << std::endl;
82 return false;
83 }
84 /* Set the format in which to read the PNG file; this code chooses a
85 * simple sRGB format with a non-associated alpha channel, adequate to
86 * store most images.
87 */
88 if(p_image.format == PNG_FORMAT_RGB){
91 }else if(p_image.format == PNG_FORMAT_RGBA){
94 }
95
96 /* Now allocate enough memory to hold the image in this format; the
97 * PNG_IMAGE_SIZE macro uses the information about the image (width,
98 * height and format) stored in 'image'.
99 */
100 p_buffer = (png_byte*)malloc(PNG_IMAGE_SIZE(p_image));
101 if(p_buffer != NULL &&
102 png_image_finish_read(&p_image, NULL/*background*/, p_buffer, 0/*row_stride*/, NULL/*colormap*/) != 0){
103 return true;
104 }
105 return false;
106}
bool check_is_png(const PPath &fileName)
Check if the given filename is a png file.
Definition check_png.cpp:16

References check_is_png(), p_buffer, p_colorType, p_image, p_nbBytePerPixel, RGB, and RGBA.

+ Here is the call graph for this function:

◆ setColor() [1/3]

template<typename T>
void PImagePng::setColor ( const T * matValue,
size_t nbRow,
size_t nbCol,
const PColorMap & colorMap )

Set the color of the image by respect to a given matrix of values and the colorMap.

Parameters
matValue: matrix of value to be used to colorize the PImagePng
nbRow: number of rows of the matrix
nbCol: number of columns of the matrix
colorMap: PColorMap to be used to convert values in color

Definition at line 20 of file PImagePng_impl.h.

20 {
21 size_t nbValueRow(std::min(nbRow, (size_t)p_image.height));
22 size_t nbValueCol(std::min(nbCol, (size_t)p_image.width));
23
24 for(size_t i(0lu); i < nbValueRow; ++i){
25 for(size_t j(0lu); j < nbValueCol; ++j){
26 float value(matValue[i*nbCol + j]);
27 color_t red(0), green(0), blue(0), alpha(0);
28 colorMap.interpolate(red, green, blue, alpha, value);
29 setColor(j, i, red, green, blue, alpha);
30 }
31 }
32
33}
void interpolate(color_t &red, color_t &green, color_t &blue, float value) const
Interpolate color by respect to the given value.

References PColorMap::interpolate(), p_image, and setColor().

+ Here is the call graph for this function:

◆ setColor() [2/3]

void PImagePng::setColor ( size_t idxWidth,
size_t idxHeight,
color_t red,
color_t green,
color_t blue )

Set the color of the pixel at (idxWidth, idxHeight)

Parameters
idxWidth: index of the pixel on the image width
idxHeight: index of the pixel on the image height
red: red proportion
green: green proportion
blue: blue proportion

Definition at line 151 of file PImagePng.cpp.

151 {
152 color_t alpha(255);
153 setColor(idxWidth, idxHeight, red, green , blue, alpha);
154}

References setColor().

Referenced by fill(), setColor(), and setColor().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setColor() [3/3]

void PImagePng::setColor ( size_t idxWidth,
size_t idxHeight,
color_t red,
color_t green,
color_t blue,
color_t alpha )

Set the color of the pixel at (idxWidth, idxHeight)

Parameters
idxWidth: index of the pixel on the image width
idxHeight: index of the pixel on the image height
red: red proportion
green: green proportion
blue: blue proportion
alpha: alpha proportion

Definition at line 164 of file PImagePng.cpp.

164 {
165 size_t index((idxHeight*p_image.width + idxWidth)*p_nbBytePerPixel);
166 p_buffer[index] = red;
167 p_buffer[index + 1lu] = green;
168 p_buffer[index + 2lu] = blue;
169 if(p_nbBytePerPixel >= 4lu){
170 p_buffer[index + 3lu] = alpha;
171 }
172}

References p_buffer, p_image, and p_nbBytePerPixel.

◆ write()

bool PImagePng::write ( const PPath & fileName)

Write a png file.

Parameters
fileName: name fo the png file to be written
Returns
true on success, false otherwise

Definition at line 112 of file PImagePng.cpp.

112 {
113 if(fileName == "" || p_buffer == NULL){return false;}
114 if(png_image_write_to_file(&p_image, fileName.c_str(), 0/*convert_to_8bit*/, p_buffer, 0/*row_stride*/, NULL/*colormap*/) != 0){
115 /* The image has been written successfully. */
116 return true;
117 }
118 return false;
119}

References p_buffer, and p_image.

Member Data Documentation

◆ p_buffer

png_byte* PImagePng::p_buffer
private

Buffer use to store the image data.

Definition at line 65 of file PImagePng.h.

Referenced by clear(), copyPImagePng(), createImage(), getColor(), getData(), getData(), initialisationPImagePng(), read(), setColor(), and write().

◆ p_colorType

PImagePng::ColorType PImagePng::p_colorType
private

Type of the color of the current image.

Definition at line 69 of file PImagePng.h.

Referenced by copyPImagePng(), createImage(), and read().

◆ p_image

png_image PImagePng::p_image
private

◆ p_nbBytePerPixel

NbColorByte PImagePng::p_nbBytePerPixel
private

Number of bytes per pixel (3 for RGB, 4 for RGBA)

Definition at line 67 of file PImagePng.h.

Referenced by copyPImagePng(), createImage(), getColor(), getNbBytePerPixel(), initialisationPImagePng(), read(), and setColor().


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