1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
| //Functions for overriding reading/writing png using iostreams
void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
FILE * file = ( FILE *)(png_get_io_ptr(png_ptr));
fread((char *)data, 1, length, file);
}
void user_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
FILE * file = ( FILE *)(png_get_io_ptr(png_ptr));
fwrite((char *)data, 1, length, file);
}
void user_flush_data(png_structp png_ptr)
{
FILE * file = ( FILE *)(png_get_io_ptr(png_ptr));
//flush(0);
}
// extraction des canaux R,G,B
float** load(char *file_name ,int bande)
{
int width, height,x,y;
png_byte color_type;
png_byte bit_depth;
png_structp png_ptr;
png_infop info_ptr,info_ptr2;
int number_of_passes;
png_bytep * row_pointers;
float** T;FILE *fp;
char header[8]; // 8 is the maximum size that can be checked
fp = fopen(file_name, "rb");
if (!fp)
printf("[read_png_file] File %s could not be opened for reading", file_name);
fread(header, 1, 8, fp);
if (!png_check_sig((unsigned char *) header, 8)) {
printf("[read_png_file] File %s erreur", file_name);
fclose(file_name);
return 0;
}
// initialize stuff
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
cout << "[read_png_file] png_create_read_struct failed" << endl;
return NULL;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
cout << "[read_png_file] png_create_info_struct failed" << endl;
png_destroy_read_struct(&png_ptr, NULL, NULL);
return NULL;
}
if (png_sig_cmp((png_bytep)header, 0, 8))
printf("[read_png_file] File %s is not recognized as a PNG file", file_name);
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
cout << "[read_png_file] probleme d'initialisation Error during init_io" << endl;
return NULL;
}
png_ptr->io_ptr = (png_voidp)file_name;
png_set_sig_bytes(png_ptr, 8);
png_set_read_fn(png_ptr, &fp, user_read_data);
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
width = info_ptr->width;
height = info_ptr->height;
number_of_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
for (y=0; y<height; y++)
row_pointers[y] = (png_byte*) malloc(info_ptr->rowbytes);
png_read_image(png_ptr, row_pointers);
T=allocate_2d_float(height,width,0);
for (y=0; y<height; y++)
{
png_byte* row = row_pointers[y];
for (x=0; x<width; x++)
{
png_byte* ptr = &(row[x*3]);
//printf("Pixel at position [ %d - %d ] has the following RGBA values: %d - %d - %d - %d\n",
// x, y, ptr[0], ptr[1], ptr[2], ptr[3]);
if(bande == 1) T[y][x] =(float)static_cast<float>(ptr[0]);
if(bande == 2) T[y][x] =(float)static_cast<float>(ptr[1]);
if(bande == 3) T[y][x] =(float)static_cast<float>(ptr[2]);
}
}
fclose(fp);
return T;
}
void save_image_resultat(char *name,int Ni,int Nj,float **denoised_R,float **denoised_G,float **denoised_B,char * type)
{
int width, height,x,y;
png_structp png_ptr;
png_infop info_ptr;
png_bytep * row_pointers;
FILE *fp = fopen(name, "wb");
if (!fp)
printf("[write_png_file] File %s could not be opened for writing", nameDenoisedChar);
// initialize stuff
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
{
cout << "[write_png_file] png_create_write_struct failed" << endl;
return;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
cout << "[write_png_file] png_create_info_struct failed" << endl;
png_destroy_write_struct(&png_ptr, NULL);
return;
}
png_init_io(png_ptr, fp);
if (setjmp(png_jmpbuf(png_ptr)))
{
cout << "[write_png_file] Error during init_io" << endl;
png_destroy_write_struct(&png_ptr, &info_ptr);
return;
}
png_set_write_fn(png_ptr, &fp, user_write_data, user_flush_data);
//write header
if (setjmp(png_jmpbuf(png_ptr)))
{
cout << "[write_png_file] Error during writing header" << endl;
png_destroy_write_struct(&png_ptr, &info_ptr);
return;
}
png_set_IHDR(png_ptr, info_ptr, width, height,
8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
// write bytes
if (setjmp(png_jmpbuf(png_ptr)))
{
cout << "[write_png_file] Error during writing bytes" << endl;
png_destroy_write_struct(&png_ptr, &info_ptr);
return;
}
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * Ni);
for (y=0; y<Ni; y++)
row_pointers[y] = (png_byte*) malloc(sizeof(png_bytep) * Nj);
for (y=0; y<Ni; y++)
{
png_byte* row = row_pointers[y];
for (x=0; x<Nj; x++)
{
png_byte* ptr = &(row[x*3]);
if(denoised_R[y][x]<=0) ptr[0] = 0;
else if(denoised_R[y][x]>=255) ptr[0] = 255;
else ptr[0] = denoised_R[y][x];
if(denoised_G[y][x]<=0) ptr[1] = 0;
else if(denoised_G[y][x]>=255) ptr[1] = 255;
else ptr[1] = denoised_G[y][x];
if(denoised_B[y][x]<=0) ptr[2] = 0;
else if(denoised_B[y][x]>=255) ptr[2] = 255;
else ptr[2] = denoised_B[y][x];
}
}
png_write_image(png_ptr, row_pointers);
png_write_end(png_ptr, NULL);
for ( y=0; y<height; y++)
free(row_pointers[y]);
free(row_pointers);
png_destroy_write_struct(&png_ptr, &info_ptr);
} |
Partager