PhoenixPNG  0.1.0
Set of tools to ease use of png file
Loading...
Searching...
No Matches
PImagePng.cpp
Go to the documentation of this file.
1/***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5****************************************/
6
7#include <string.h>
8
9#include "PImagePng.h"
10
15
17
23
28
30
34 copyPImagePng(other);
35 return *this;
36}
37
39
44bool PImagePng::createImage(size_t width, size_t height, PImagePng::ColorType colorType){
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}
69
71
74bool PImagePng::read(const PPath & fileName){
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}
107
109
112bool PImagePng::write(const PPath & fileName){
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}
120
123 if(p_buffer == NULL){
124 png_image_free(&p_image);
125 }else{
126 free(p_buffer);
127 }
128}
129
131
136void PImagePng::fill(color_t red, color_t green, color_t blue, color_t alpha){
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}
143
145
151void PImagePng::setColor(size_t idxWidth, size_t idxHeight, color_t red, color_t green, color_t blue){
152 color_t alpha(255);
153 setColor(idxWidth, idxHeight, red, green , blue, alpha);
154}
155
157
164void PImagePng::setColor(size_t idxWidth, size_t idxHeight, color_t red, color_t green, color_t blue, color_t alpha){
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}
173
175
181void PImagePng::getColor(size_t idxWidth, size_t idxHeight, color_t & red, color_t & green, color_t & blue) const{
182 color_t alpha(255);
183 getColor(idxWidth, idxHeight, red, green , blue, alpha);
184}
185
187
194void PImagePng::getColor(size_t idxWidth, size_t idxHeight, color_t & red, color_t & green, color_t & blue, color_t & alpha) const{
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}
203
205
207unsigned int PImagePng::getWidth() const{return p_image.width;}
208
210
212unsigned int PImagePng::getHeight() const{return p_image.height;}
213
215
217const png_byte* PImagePng::getData() const{return p_buffer;}
218
220
222png_byte* PImagePng::getData(){return p_buffer;}
223
225
228
230
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}
236
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}
245
246
247
248
249
unsigned char color_t
Type des of a color.
Definition PColorMap.h:14
unsigned int NbColorByte
Number of bytes use in the image color (3 for RGB, 4 for RGBA)
Definition PImagePng.h:16
bool check_is_png(const PPath &fileName)
Check if the given filename is a png file.
Definition check_png.cpp:16
void initialisationPImagePng()
Initialisation function of the class PImagePng.
bool createImage(size_t width, size_t height, PImagePng::ColorType colorType=PImagePng::RGB)
Create an image.
Definition PImagePng.cpp:44
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 fill(color_t red, color_t green, color_t blue, color_t alpha=255)
Fill the image with the given color.
bool read(const PPath &fileName)
Read the given png image.
Definition PImagePng.cpp:74
NbColorByte getNbBytePerPixel() const
Get the number of bytes per pixel.
png_image p_image
Png image.
Definition PImagePng.h:63
void clear()
Clear the image and buffer.
PImagePng()
Default constructor of PImagePng.
Definition PImagePng.cpp:12
void copyPImagePng(const PImagePng &other)
Copy function of PImagePng.
png_byte * p_buffer
Buffer use to store the image data.
Definition PImagePng.h:65
PImagePng & operator=(const PImagePng &other)
Definition of equal operator of PImagePng.
Definition PImagePng.cpp:33
const png_byte * getData() const
Get the buffer data of the current PImagePng.
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
virtual ~PImagePng()
Destructor of PImagePng.
Definition PImagePng.cpp:25
unsigned int getHeight() const
Get the height of the image.
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)
bool write(const PPath &fileName)
Write a png file.
unsigned int getWidth() const
Get the width of the image.