[49] | 1 | #include "image.hpp" |
---|
| 2 | #include "palette.hpp" |
---|
| 3 | #include <stdio.h> |
---|
| 4 | #include "macs.hpp" |
---|
| 5 | #include "ppmread.hpp" |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | int read_ppm_header(FILE *fp, int *parm) |
---|
| 9 | { |
---|
| 10 | int nr; |
---|
| 11 | char st[100],sig[50]; |
---|
| 12 | nr=0; |
---|
| 13 | fscanf(fp,"%s",sig); |
---|
| 14 | if (!strcmp(sig,"P6")) |
---|
| 15 | { |
---|
| 16 | do |
---|
| 17 | { |
---|
| 18 | fscanf(fp,"%s",sig); |
---|
| 19 | if (sig[0]=='#') |
---|
| 20 | fgets(st,100,fp); |
---|
| 21 | else |
---|
| 22 | { |
---|
| 23 | if (sscanf(sig,"%d",parm)) |
---|
| 24 | { nr++; parm++; } |
---|
| 25 | else return 0; |
---|
| 26 | } |
---|
| 27 | } while (nr<3 && !feof(fp)); |
---|
| 28 | } |
---|
| 29 | else return 0; |
---|
| 30 | // fgets(st,100,fp); |
---|
| 31 | return 1; |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | void write_ppm(image *im,palette *pal,char *fn) |
---|
| 35 | { |
---|
| 36 | FILE *fp; |
---|
| 37 | CHECK(im && pal && fn); |
---|
| 38 | unsigned char r[3],*c; |
---|
| 39 | int x,y; |
---|
| 40 | clear_errors(); |
---|
| 41 | fp=fopen(fn,"wb"); |
---|
| 42 | if (!fp) set_error(imWRITE_ERROR); |
---|
| 43 | else |
---|
| 44 | { |
---|
| 45 | fprintf(fp,"%s %d %d %d\n","P6",im->width(),im->height(),(int)256); |
---|
| 46 | for (y=0;y<im->height();y++) |
---|
| 47 | { c=(unsigned char *)im->scan_line(y); |
---|
| 48 | for (x=0;x<im->width();x++) |
---|
| 49 | { r[0]=pal->red(c[x]); |
---|
| 50 | r[1]=pal->green(c[x]); |
---|
| 51 | r[2]=pal->blue(c[x]); |
---|
| 52 | fwrite(&r[0],1,1,fp); |
---|
| 53 | fwrite(&r[1],1,1,fp); |
---|
| 54 | fwrite(&r[2],1,1,fp); |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | fclose(fp); |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | #define TSIZE 1001 |
---|
| 62 | |
---|
| 63 | image *read_ppm(char *fn,palette *&pal, int pal_type) |
---|
| 64 | { |
---|
| 65 | FILE *fp; |
---|
| 66 | image *im; |
---|
| 67 | unsigned char *c,col[3]; |
---|
| 68 | |
---|
| 69 | char buf[30]; |
---|
| 70 | int l,h,maxc,i,j,parm[3],find_color; |
---|
| 71 | CONDITION(fn,"Null filename"); |
---|
| 72 | clear_errors(); |
---|
| 73 | fp=fopen(fn,"rb"); |
---|
| 74 | im=NULL; |
---|
| 75 | CONDITION(fp,"Filename not found"); |
---|
| 76 | if (!fp) { set_error(imFILE_NOT_FOUND); return NULL; } |
---|
| 77 | |
---|
| 78 | if (read_ppm_header(fp, parm)==0) set_error(imFILE_CORRUPTED); |
---|
| 79 | else |
---|
| 80 | { |
---|
| 81 | l=parm[0]; h=parm[1]; maxc=parm[2]; |
---|
| 82 | |
---|
| 83 | if (!pal) |
---|
| 84 | pal=new palette; |
---|
| 85 | fgets(buf,30,fp); |
---|
| 86 | im=new image(l,h); |
---|
| 87 | printf("Created image %d,%d\n",l,h); |
---|
| 88 | for (i=0;i<h;i++) |
---|
| 89 | { c=(unsigned char *)im->scan_line(i); |
---|
| 90 | for (j=0;j<l;j++) |
---|
| 91 | { |
---|
| 92 | if (fread(col,1,3,fp)!=3) set_error(imFILE_CORRUPTED); |
---|
| 93 | if (pal_type==PPM_R3G3B2) |
---|
| 94 | c[j]=(col[0]*7/255)|((col[1]*7/255)<<3)|((col[2]*3/255)<<6); |
---|
| 95 | else if (pal_type==PPM_BW) |
---|
| 96 | c[j]=col[0]*255/parm[2]; |
---|
| 97 | else |
---|
| 98 | { |
---|
| 99 | find_color=pal->find_color(col[0],col[1],col[2]); |
---|
| 100 | if (find_color>=0) c[j]=find_color; |
---|
| 101 | else c[j]=(unsigned char) pal->add_color(col[0],col[1],col[2]); |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | fclose(fp); |
---|
| 107 | return im; |
---|
| 108 | } |
---|