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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <stdarg.h>
#include <png.h>
int *image1, *image2;
int xs,ys;
int force_gamma_1;
/*****************************************************************************/
void
add_png(int param, int* image1, int* image2) {
int a;
int *iptr1, *iptr2;
int val;
iptr1=image1;
iptr2=image2;
for(a=xs*ys; a; a--) {
val = *iptr1;
if(val<=param) {
*iptr2++=0;
} else {
*iptr2++;
}
iptr1++;
}
}
/***************************************************************************/
void
threshold(int param, int * image) {
int a;
int *iptr;
int val;
iptr=image;
for(a=0;a<xs*ys;a++) {
val=*iptr;
*iptr++=(val>=param)?0xffffff:0;
}
}
/*****************************************************************************/
void
write_png(unsigned char *filename, int *image) {
unsigned char *temporary;
unsigned char *tptr;
int *iptr,a;
FILE *f;
png_structp png_ptr;
png_infop info_ptr;
f = fopen(filename,"w");
if(!f) {
fprintf(stderr,"Unable to open file %s.\n",filename);
exit(1);
}
temporary=malloc(xs*ys);
if(!temporary){
fprintf(stderr,"Out of memory.\n");
exit(1);
}
iptr=image;
tptr=temporary;
for(a=xs*ys; a; a--) {
*tptr++=(*iptr++)>>16;
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
info_ptr = png_create_info_struct(png_ptr);
png_init_io(png_ptr,f);
png_set_filter(png_ptr,0,PNG_FILTER_NONE|PNG_FILTER_SUB|PNG_FILTER_UP
|PNG_FILTER_AVG|PNG_FILTER_PAETH);
png_set_compression_level(png_ptr,Z_BEST_COMPRESSION);
png_set_IHDR(png_ptr,info_ptr,xs,ys,8,PNG_COLOR_TYPE_GRAY,PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
png_set_gAMA(png_ptr,info_ptr,1.0);
png_write_info(png_ptr,info_ptr);
tptr=temporary;
for(a=ys; a; a--) {
png_write_row(png_ptr,tptr);
tptr+=xs;
}
png_write_end(png_ptr,info_ptr);
png_destroy_write_struct(&png_ptr,&info_ptr);
free(temporary);
fclose(f);
}
/*****************************************************************************/
void
read_png(FILE *f, png_structp png_ptr, png_infop info_ptr,
int *image, int invertMode) {
unsigned char *temporary_array;
png_color_16p* background;
double gamma;
int y1,number_of_passes;
unsigned char **ptrs;
if (png_get_gAMA(png_ptr,info_ptr, &gamma)) {
if (force_gamma_1) {
png_set_gamma(png_ptr, 1.0, 1.0);
/* Forcing gamma is here for repairing files after processing
* with GIMP, which deliberately writes gamma=00 00 b1 8f
* even when input gamma was 00 01 86 a0 and no gamma setting
* change is performed by user (simply saind, GIMP is
* braindead)
*/
} else {
png_set_gamma(png_ptr, 1.0, gamma);
}
} else {
png_set_gamma(png_ptr, 1.0, 0.454545);
}
{
int bit_depth;
int color_type;
color_type = png_get_color_type(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
if (color_type==PNG_COLOR_TYPE_GRAY) {
if (bit_depth<8) {
png_set_expand(png_ptr);
}
if (bit_depth==16) {
png_set_strip_16(png_ptr);
}
}
if (color_type==PNG_COLOR_TYPE_PALETTE){
png_set_expand(png_ptr);
/*png_set_rgb_to_gray(png_ptr,1,54.0/256,183.0/256);*/
png_set_rgb_to_gray(png_ptr, 1, 0, 100000);
}
if (color_type & PNG_COLOR_MASK_ALPHA){
png_set_strip_alpha(png_ptr);
}
if (color_type==PNG_COLOR_TYPE_RGB ||
color_type==PNG_COLOR_TYPE_RGB_ALPHA){
png_set_rgb_to_gray(png_ptr, 1, 54.0/256, 183.0/256);
/*png_set_rgb_to_gray(png_ptr, 1, 0, 100000);*/
}
}
if(invertMode == 1) {
png_set_invert_mono(png_ptr);
}
/* If the depth is different from 8 bits/gray, make the libpng expand
* it to 8 bit gray.
*/
number_of_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr,info_ptr);
temporary_array = malloc(xs*ys);
ptrs = malloc(ys*sizeof(*ptrs));
for(y1=0;y1<ys;y1++) {
ptrs[y1]=temporary_array+xs*y1;
}
for(;number_of_passes;number_of_passes--) {
png_read_rows(png_ptr, ptrs, NULL, ys);
}
png_read_end(png_ptr, NULL);
{
int *imptr=image;
unsigned char *tptr=temporary_array;
int a;
for (y1=ys*xs;y1;y1--) {
a=*tptr++;
a=a|(a<<8)|(a<<16);
*imptr++=a;
}
}
free(ptrs);
free(temporary_array);
}
/*****************************************************************************/
int
main (int argc, char *argv[]) {
FILE *f1, *f2;
png_structp png_p1, png_p2;
png_infop info_p1, info_p2;
if(argc < 2) {
fprintf (stderr, "usage: %s filename.png fileout.png\n", argv[0]);
return -1;
}
/* 1ère lecture de l'image :
* on traite l'image pour récupérer le texte en noir */
f1 = fopen(argv[1],"r");
if(f1 == NULL) {
fprintf(stderr,"impossible d'ouvrir %s\n",argv[1]);
exit(1);
}
png_p1 = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
info_p1 = png_create_info_struct(png_p1);
png_init_io(png_p1,f1);
png_read_info(png_p1, info_p1);
xs = png_get_image_width(png_p1,info_p1);
ys = png_get_image_height(png_p1,info_p1);
image1 = malloc(xs*ys*sizeof(image1));
/* on lit l'image, les pixels sont stockées dans image1 */
/* revertMode = 0 : l'image n'est pas inversée
*/
read_png(f1, png_p1, info_p1, image1, 0);
png_destroy_read_struct(&png_p1, &info_p1, NULL);
fclose(f1);
/* 2eme lecture de l'image originale */
/* on va inverser l'image pour pouvoir recuperer le texte
* qui est en blanc */
f2 = fopen(argv[1],"r");
if(f2 == NULL) {
fprintf(stderr,"impossible d'ouvrir %s\n",argv[1]);
if(image1 != NULL) free(image1);
exit(1);
}
png_p2 = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
info_p2 = png_create_info_struct(png_p2);
png_init_io(png_p2,f2);
png_read_info(png_p2, info_p2);
image2 = malloc(xs*ys*sizeof(image2));
/* on lit l'image, les pixels sont stockées dans image2 */
/* revertMode = 1 : l'image est inversée
* inversée = pixel noir ==> blanc && pixel blanc ==>noir
*/
read_png(f2, png_p2, info_p2, image2, 1);
png_destroy_read_struct(&png_p2, &info_p2, NULL);
fclose(f2);
write_png("img1.png",image1);
write_png("img2.png",image2);
/* traitement parallèle des 2 images
* on lit image2 et on modifie image1 quand on a des pixels noirs
* pour les reporter dans image1
*/
add_png(20, image1, image2);
/* threshold pour avoir image ne noir et blanc */
threshold(20, image2);
/* ecriture du fichier final */
write_png(argv[2], image2);
if(image1 != NULL) free(image1);
if(image2 != NULL) free(image2);
return 0;
} |
Partager