#include #include #include #include #include #include /* for jmpbuf declaration in writepng.h */ #include "readpng.h" #include "writepng.h" #define ARGS "f:o:t:" char *Usage = "rw_png -f filename\n\ -o output file\n\ -t PNM type for output (6 is 24bit, 8 is 32bit)\n"; char Fname[255]; char Oname[255]; int PNMtype; mainprog_info Wpng_info; /* global in Greg's code */ int main(int argc, char *argv[]) { int i,j; unsigned char *image; FILE *fd; FILE *od; unsigned long width; unsigned long height; unsigned long rowbytes; int c; int rc; int wc; double display_exp = 2.0; /* check this */ int channels; unsigned char *curr; unsigned char *new_curr; unsigned char red, green, blue, alpha; unsigned char *new_image; unsigned long new_rowbytes; PNMtype = 6; while((c = getopt(argc,argv,ARGS)) != EOF) { switch(c) { case 'f': strncpy(Fname,optarg,sizeof(Fname)); break; case 'o': strncpy(Oname,optarg,sizeof(Oname)); break; case 't': PNMtype = atoi(optarg); break; default: fprintf(stderr,"unrecognized command %c\n", c); fprintf(stderr,"%s",Usage); exit(1); } } if(Fname[0] == 0) { fprintf(stderr,"must specify file name\n"); fprintf(stderr,"%s",Usage); exit(1); } fd = fopen(Fname,"rb"); if(fd == NULL) { fprintf(stderr,"couldn't open file %s\n",Fname); exit(1); } if((PNMtype != 6) && (PNMtype != 8)) { fprintf(stderr,"bad PNM type: %d\n",PNMtype); exit(1); } rc = readpng_init(fd,&width,&height); if(rc != 0) { fprintf(stderr,"readpng_init error %d\n", rc); exit(1); } image = readpng_get_image(display_exp, &channels, &rowbytes); if(image == NULL) { fprintf(stderr,"readpng_get_image fails\n"); fclose(fd); readpng_cleanup(FALSE); exit(1); } if(Oname[0] == 0) { for(i=0; i < height; i++) { curr = &(image[i*rowbytes+0]); for(j=0; j < width; j++) { red = curr[0]; green = curr[1]; blue = curr[2]; if(channels == 4) { alpha = curr[3]; curr += 4; printf("r: %d, g: %d, b: %d, alpha: %d\n", red,green,blue,alpha); } else { printf("r: %d, g: %d, b: %d\n", red,green,blue); curr += 3; } } } fclose(fd); readpng_cleanup(FALSE); exit(0); } Wpng_info.infile = NULL; Wpng_info.outfile = NULL; Wpng_info.image_data = NULL; Wpng_info.row_pointers = NULL; Wpng_info.filter = FALSE; Wpng_info.interlaced = FALSE; Wpng_info.have_bg = FALSE; Wpng_info.have_time = FALSE; Wpng_info.have_text = 0; Wpng_info.gamma = 0.0; od = fopen(Oname,"wb"); if(od == NULL) { fprintf(stderr,"couldn't output file %s\n", Oname); fclose(fd); readpng_cleanup(FALSE); exit(1); } Wpng_info.outfile = od; /* * figure out the image match */ /* * if we want a 4 channel output and we started with a 3 channel * image */ if((PNMtype == 8) && (channels == 3)) { printf("converting 3 channel to 4 channel\n"); new_image = (unsigned char *)malloc(height*width*4); new_rowbytes = width*4; if(new_image == NULL) { exit(1); } memset(new_image,0,height*width*4); for(i=0; i < height; i++) { curr = &(image[i*rowbytes+0]); new_curr = &(new_image[i*new_rowbytes+0]); for(j=0; j < width; j++) { new_curr[0] = curr[0]; new_curr[1] = curr[1]; new_curr[2] = curr[2]; new_curr[3] = 255; curr += 3; new_curr += 4; } } free(image); image = new_image; rowbytes = new_rowbytes; } else if((PNMtype == 6) && (channels == 4)) { printf("converting 4 channel image to 3\n"); } Wpng_info.pnmtype = PNMtype; Wpng_info.height = height; Wpng_info.width = width; Wpng_info.sample_depth = 8; wc = writepng_init(&Wpng_info); if(wc != 0) { fprintf(stderr,"writepng_init: error %d\n", wc); fclose(fd); fclose(od); readpng_cleanup(FALSE); exit(1); } Wpng_info.image_data = image; Wpng_info.row_pointers = (unsigned char **)malloc(height* sizeof(unsigned char *)); if(Wpng_info.row_pointers == NULL) { fclose(fd); fclose(od); writepng_cleanup(&Wpng_info); exit(1); } for(i=0; i < height; i++) { Wpng_info.row_pointers[i] = &(image[i*rowbytes+0]); } wc = writepng_encode_image(&Wpng_info); if(wc != 0) { fprintf(stderr,"writepng_encode_image error %d\n", wc); fclose(fd); fclose(od); writepng_cleanup(&Wpng_info); exit(1); } free(Wpng_info.row_pointers); free(image); writepng_cleanup(&Wpng_info); exit(0); }