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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
|
/* (c) 2001 Karel 'Clock' Kulhavy
* Serves the purpose to manipulate grayscale PNG images according to a
* linear command list.
* commandline format: improcess input_filename command_filename output_filename
* Internal format: a 2D field of unsigned's
* 0x000000 black
* 0xffffff white
* Commandfile format:
* command argument1 argument2...
* command argument1 argument2...
* .
* .
* .
*/
#include <stdio.h>
#include <stdlib.h>
#include <png.h>
int *image;
int xs,ys;
int force_gamma_1;
void read_png(unsigned char *filename)
{
unsigned char *temporary_array;
png_structp png_ptr;
png_infop info_ptr;
double gamma;
int y1,number_of_passes;
unsigned char **ptrs;
FILE *f;
f=fopen(filename,"r");
png_ptr=png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
info_ptr=png_create_info_struct(png_ptr);
png_init_io(png_ptr,f);
png_read_info(png_ptr, info_ptr);
xs=png_get_image_width(png_ptr,info_ptr);
ys=png_get_image_height(png_ptr,info_ptr);
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);
}
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);
}
}
/* 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);
image=malloc(xs*ys*sizeof(image));
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);
png_destroy_read_struct(&png_ptr, &info_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);
fclose(f);
}
void write_png(unsigned char *filename)
{
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 clip(void)
{
int a;
int *iptr;
int val;
iptr=image;
for (a=xs*ys;a;a--){
val=*iptr;
if (val<0) val=0;
if (val>0xffffff) val=0xffffff;
*iptr++=val;
}
}
void threshold(int param)
{
int a;
int *iptr;
int val;
iptr=image;
for (a=xs*ys;a;a--){
val=*iptr;
*iptr++=(val>=param)?0xffffff:0;
}
}
void mul(int param)
{
int a;
int *iptr;
int val;
iptr=image;
for (a=xs*ys;a;a--){
val=*iptr;
val*=param;
*iptr++=val;
}
}
void add(int param)
{
int a;
int *iptr;
int val;
iptr=image;
for (a=xs*ys;a;a--){
val=*iptr;
val+=param;
*iptr++=val;
}
}
void divide(int param)
{
int a;
int *iptr;
int val;
iptr=image;
for (a=xs*ys;a;a--){
val=*iptr;
val/=param;
*iptr++=val;
}
}
void right_shift(int param)
{
int a;
int *iptr;
int val;
iptr=image;
for (a=xs*ys;a;a--){
val=*iptr;
val>>=param;
*iptr++=val;
}
}
void left_shift(int param)
{
int a;
int *iptr;
int val;
iptr=image;
for (a=xs*ys;a;a--){
val=*iptr;
val<<=param;
*iptr++=val;
}
}
void flip()
{
int *new;
int x,y;
int *iptr,*nptr;
new=malloc(xs*ys*sizeof(*new));
if (!new){
fprintf(stderr,"Out of memory when flipping.\n");
exit(1);
}
iptr=image;
nptr=new;
for (y=ys;y;y--){
for(x=xs;x;x--){
*nptr=*iptr;
iptr++;
nptr+=ys;
}
nptr-=xs*ys;
nptr++;
}
free(image);
image=new;
x=xs;
xs=ys;
ys=x;
}
void mirror(void){
int y;
int *fptr, *rptr, *lptr;
int xchg;
lptr=image;
for (y=ys;y;y--){
fptr=lptr;
rptr=lptr+xs-1;
while(rptr>fptr){
xchg=*rptr;
*rptr=*fptr;
*fptr=xchg;
fptr++;
rptr--;
}
lptr+=xs;
}
}
void append(int lines, int value){
int *ptr;
if (lines<=0) return;
image=realloc(image,xs*(ys+lines)*sizeof(*image));
if (!image){
fprintf(stderr,"Out of memory when appending lines to the image.\n");
exit(1);
}
ptr=image+xs*ys;
ys+=lines;
for (lines*=xs;lines;lines--){
*ptr++=value;
}
}
void detract(int lines){
if (lines>=ys) return; /* Invalid */
ys-=lines;
image=realloc(image,xs*ys*sizeof(*image));
if (!image){
fprintf(stderr,"Out of memory at detract.\n");
exit(1);
}
}
/* Blurs so that each pixel is replaced by sum of its value, value of "pixels" pixels
* left and "pixels" pixels right, divided by 2*pixels+1.
*/
void blurbox(int pixels)
{
int *templine, *iptr;
int *tptr;
int *addptr,*subptr;
int divisor=pixels*2+1;
int y,x;
int val;
templine=malloc((2*pixels+xs)*sizeof(*templine));
if (!templine){
fprintf(stderr,"Out of memory when box blurring.\n");
exit(1);
}
iptr=image;
for (y=ys;y;y--){
tptr=templine;
val=*iptr/divisor;
for (x=pixels;x;x--){
*tptr++=val;
}
for (x=xs;x;x--){
*tptr++=(*iptr++)/divisor;
}
val=iptr[-1]/divisor;
iptr-=xs;
for (x=pixels;x;x--){
*tptr++=val;
}
val=0;
tptr=templine;
for (x=divisor-1;x;x--){
val+=*tptr++;
}
addptr=tptr;
subptr=templine;
for (x=xs;x;x--){
val+=*addptr++;
*iptr++=val;
val-=*subptr++;
}
}
free(templine);
}
void perform_command_line(unsigned char *command)
{
char *ptr;
int param1, param2;
/* Find the first space, newline, tab or null */
ptr=command;
while(!(*ptr==0||*ptr==10||*ptr==32||*ptr==9)) ptr++;
if (!*ptr) return; /* Invalid */
*ptr=0;
ptr++;
if (!strcmp(command,"clip")){
clip();
}else if (!strcmp(command,"threshold")){
param1=strtol(ptr,NULL,0);
threshold(param1);
}else if (!strcmp(command,"flip")){
flip();
}else if (!strcmp(command,"append")){
param1=strtol(ptr,&ptr,0);
param2=strtol(ptr,&ptr,0);
append(param1,param2);
}else if (!strcmp (command,"detract")){
param1=strtol(ptr,&ptr,0);
detract(param1);
}else if (!strcmp(command,"mirror")){
mirror();
}else if (!strcmp(command,"blurbox")){
param1=strtol(ptr,&ptr,0);
blurbox(param1);
}else if (!strcmp(command,"gaussian")){
param1=strtol(ptr,&ptr,0);
param2=strtol(ptr,&ptr,0);
if (param1>0){
for (;param1;param1--){
blurbox(param2);
}
}
}else if (!strcmp(command,"*")){
param1=strtol(ptr,&ptr,0);
mul(param1);
}else if (!strcmp(command,"+")){
param1=strtol(ptr,&ptr,0);
add(param1);
}else if (!strcmp(command,"/")){
param1=strtol(ptr,&ptr,0);
if (param1) divide(param1);
}else if (!strcmp(command,">>")){
param1=strtol(ptr,&ptr,0);
right_shift(param1);
}else if (!strcmp(command,"<<")){
param1=strtol(ptr,&ptr,0);
left_shift(param1);
}else{
fprintf(stderr,"Invalid command %s encountered.\n",command);
}
}
void process_commands(unsigned char *filename)
{
FILE *f;
unsigned char string[1024];
if (!strlen(filename)) return;
/* "" as command filename */
f=fopen(filename,"r");
if (!f){
fprintf(stderr,"Can't open command file %s.\n",filename);
exit(1);
}
while(fgets(string, sizeof(string),f)){
perform_command_line(string);
}
fclose(f);
}
int main(int argc, char** argv)
{
if (argc<4){
usage:
fprintf(stderr,
"Usage: improcess [-f] input_filename "
"command_filename output_filename\nIf command_filename\
is \"\", then no operations are performed.\n");
return 0;
}
if (argv[1][0]=='-'&&argv[1][1]=='f'&&!argv[1][2])
{
/* -f flag */
if (argc<5) goto usage;
force_gamma_1=1;
read_png(argv[2]);
process_commands(argv[3]);
write_png(argv[4]);
}else{
read_png(argv[1]);
process_commands(argv[2]);
write_png(argv[3]);
}
return 0;
}
|