CImage *ReadCImage(char *filename) { CImage *cimg=NULL; FILE *fp=NULL; char type[10]; int i,ncols,nrows,n; char z[256]; fp = fopen(filename,"rb"); if (fp == NULL){ fprintf(stderr,"Cannot open %s\n",filename); exit(-1); } fscanf(fp,"%s\n",type); if((strcmp(type,"P6")==0)){ NCFgets(z,255,fp); sscanf(z,"%d %d\n",&ncols,&nrows); n = ncols*nrows; NCFgets(z,255,fp); sscanf(z,"%d\n",&i); cimg = CreateCImage(ncols,nrows); for (i=0; i < n; i++){ cimg->C[0]->val[i] = fgetc(fp); cimg->C[1]->val[i] = fgetc(fp); cimg->C[2]->val[i] = fgetc(fp); } fclose(fp); }else{ fprintf(stderr,"Input image must be P6\n"); exit(-1); } return(cimg); }
CImage *DrawLabeledRegions(Image *img, Image *label){ CImage *border=CreateCImage(img->ncols,img->nrows); int x,y,k,p,q,u,v; AdjRel *A; A = Circular(1.0); for(y=0;y<img->nrows;y++){ for(x=0;x<img->ncols;x++){ p = x + img->tbrow[y]; border->C[0]->val[p]= border->C[1]->val[p]=border->C[2]->val[p]=img->val[p]; for(k=1;k<A->n;k++){ u = x+A->dx[k]; v = y+A->dy[k]; if(ValidPixel(img,u,v)){ q= u + img->tbrow[v]; if (label->val[p] != label->val[q]){ border->C[0]->val[p]=255; border->C[1]->val[p]=0; border->C[2]->val[p]=0; break; } } } } } DestroyAdjRel(&A); return(border); }
CImage *BlendImages(CImage *cimg1, CImage *cimg2, float alpha1, float alpha2) { CImage *cimg3=CreateCImage(cimg1->C[0]->ncols,cimg1->C[0]->nrows); int p,n=cimg1->C[0]->ncols*cimg1->C[0]->nrows; for (p=0; p < n; p++) { cimg3->C[0]->val[p] = alpha1*cimg1->C[0]->val[p] + alpha2*cimg2->C[0]->val[p]; cimg3->C[1]->val[p] = alpha1*cimg1->C[1]->val[p] + alpha2*cimg2->C[1]->val[p]; cimg3->C[2]->val[p] = alpha1*cimg1->C[2]->val[p] + alpha2*cimg2->C[2]->val[p]; } return(cimg3); }
CImage *CImageLABtoXYZ(CImage *cimg) { CImage *ncimg=NULL; int p,n,i; ncimg = CreateCImage(cimg->C[0]->ncols,cimg->C[0]->nrows); n = ncimg->C[0]->ncols*ncimg->C[0]->nrows; for (p=0; p < n; p++){ i = triplet(cimg->C[0]->val[p],cimg->C[1]->val[p],cimg->C[2]->val[p]); i = LAB2XYZ(i); ncimg->C[0]->val[p]=t0(i); ncimg->C[1]->val[p]=t1(i); ncimg->C[2]->val[p]=t2(i); } return(ncimg); }
CImage *ColorizeImage(Image *img, float R, float G, float B) { CImage *cimg=CreateCImage(img->ncols,img->nrows); float Cb,Cr,Y; int Imax,p,n=img->ncols*img->nrows; Imax = MAX(MaximumValue(img),1); Cb = -0.148*R-0.291*G+0.439*B+128.0/255.; Cr = 0.439*R-0.368*G-0.071*B+128.0/255.; for (p=0; p < n; p++) { Y = ((float)img->val[p]/Imax); R=296.82*(Y-0.062745098039)+ 406.98*(Cr-0.50196078431); G=296.82*(Y-0.062745098039)- 207.315*(Cr-0.50196078431)- 99.96*(Cb-0.50196078431); B=296.82*(Y-0.062745098039)+ 514.335*(Cb-0.50196078431); if (R<0.0) R=0.0; if (G<0.0) G=0.0; if (B<0.0) B=0.0; if (R>255.0) R=255.0; if (G>255.0) G=255.0; if (B>255.0) B=255.0; cimg->C[0]->val[p]=(int)(R); cimg->C[1]->val[p]=(int)(G); cimg->C[2]->val[p]=(int)(B); } return(cimg); }
CImage *CLinearStretch(CImage *cimg, int f1, int f2, int g1, int g2) { CImage *simg=NULL; int p,n; float a,R,G,B,Y,Cb,Cr,yf1,yf2,yg1,yg2; simg = CreateCImage(cimg->C[0]->ncols,cimg->C[0]->nrows); n = simg->C[0]->ncols*simg->C[0]->nrows; if (f1 != f2) { a = ((float)(g2-g1))/(f2-f1); } else { a = INT_MAX; } yf1 = (float)f1/255.; yf2 = (float)f2/255.; yg1 = (float)g1/255.; yg2 = (float)g2/255.; for (p=0; p < n; p++){ R = (float)cimg->C[0]->val[p]/255.; G = (float)cimg->C[1]->val[p]/255.; B = (float)cimg->C[2]->val[p]/255.; Y = 0.257*R+0.504*G+0.098*B+16.0/255.; Cb =-0.148*R-0.291*G+0.439*B+128.0/255.; Cr = 0.439*R-0.368*G-0.071*B+128.0/255.; if (Y < yf1) Y = yg1; else if (Y > yf2) Y = yg2; else { if (a != INT_MAX) Y = (a*(Y-yf1)+yg1); else{ Y = yg2; } } R= 296.82*(Y-0.062745098039)+\ 406.98*(Cr-0.50196078431); G =296.82*(Y-0.062745098039)-\ 207.315*(Cr-0.50196078431)-\ 99.96*(Cb-0.50196078431); B= 296.82*(Y-0.062745098039)+\ 514.335*(Cb-0.50196078431); if (R<0.0) R=0.0; if (G<0.0) G=0.0; if (B<0.0) B=0.0; if (R>255.0) R=255.0; if (G>255.0) G=255.0; if (B>255.0) B=255.0; simg->C[0]->val[p]=(int)(R); simg->C[1]->val[p]=(int)(G); simg->C[2]->val[p]=(int)(B); } return(simg); }