iam able to run the same program with differnet sub-image sizes..but not with sizes less than 10...i will include my whole code in here...the program reads the path of bunch of images from a file(like 10,20 etc) at a time..
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <wand/magick-wand.h>
#include<time.h>
#define img_wdt 1000
#define img_hgt 1000
#define cnt_img 50
#define wdt_sbimg 50
#define hgt_sbimg 50
struct var
{
unsigned int n_sbimg, // No. of Sub-images divided accrding to sb_image width and height
sbimg_pxl, // No of pixels in a Sub-Image
treshold,
tot_pxl,
img_cnt,
tot_sbimg;
size_t sbimg_wdt,sbimg_hgt; // Sub-Image Width and Height
size_t *row,*col; // col & row are used to store starting positions of each sb_img
size_t width,height; // Original Image width and height
};
#define loc(x,y,sbimg_wdt) (((x*sbimg_wdt)+y)*3) // Macro used to calculate the location in a 1-D array
#define round(a,b) ((int)(a/b)*b) //Macro used to calculate the floor of a decimal value
void different_pxls(struct var v,unsigned char *pxl_data,unsigned char *&new_data); //Function to Calculate Diffetent Pixels In CPU
void subimages_pxls(unsigned char *pxl_data,struct var &v,char **argv,int grp_cnt,char **final_str);
int main(int argc,char **argv)
{
if (argc != 6)
{
(void) fprintf(stdout,"Usage: %s <CPU> <img_cnt> <Sbimg_wdt> <sbimg_hgt> <Treshold> \n",argv[0]);
exit(0);
}
struct var v;
MagickBooleanType status;
MagickWand *magick_wand;
FILE *fp;
fp = fopen("./filenames.txt", "r");
if(!fp)
{
printf("Error in opening File");
}
char str[200],**final_str;
final_str=(char **) malloc(400*sizeof(char));
v.img_cnt=atoi(argv[2]);
v.sbimg_wdt=atoi(argv[3]);
v.sbimg_hgt=atoi(argv[4]);
v.treshold=atoi(argv[5]);
int cnt=0;
unsigned long x,y;
unsigned char *pxl_data;
unsigned char *sbimg_pxl_data;
int i,j,k=0,grp_cnt=0;
while(!feof(fp))
{
sbimg_pxl_data =(unsigned char *)malloc((wdt_sbimg*hgt_sbimg*3) * sizeof(unsigned char));
pxl_data=(unsigned char*)malloc((img_wdt*img_hgt*3*cnt_img)*sizeof(unsigned char));
v.col=(size_t*)malloc(((img_wdt/wdt_sbimg)*(img_hgt/hgt_sbimg))*sizeof(size_t)); // v.col & v.row are used to store starting positions of each sb_img
v.row=(size_t*)malloc(((img_wdt/wdt_sbimg)*(img_hgt/hgt_sbimg))*sizeof(size_t));
cnt=0;
time_t birth,death;
time(&birth);
unsigned char *start=pxl_data;
while(cnt<v.img_cnt&&(fgets(str,sizeof(str),fp)!=NULL))
{
int len = strlen(str)-1;
if(str[len] == '\n')
{
str[len] = 0;
}
printf("Grabbing R,G,B values For Group : %d Image : %d %s\n",grp_cnt,cnt,str);
MagickWandGenesis(); // to initialize the magick-wand Environment
magick_wand=NewMagickWand(); // To create the wand to store image
status=MagickReadImage(magick_wand,str); // Function to read image into wand
if (status == MagickFalse)
{
printf ("\n\nProblem in Reading Image\n\n");
}
size_t wdt=MagickGetImageWidth(magick_wand); // Function returns the width of image in the wand
size_t hgt=MagickGetImageHeight(magick_wand); // Function returns the height of image in the wand
v.width=round(wdt,v.sbimg_wdt);
v.height=round(hgt,v.sbimg_hgt);
v.n_sbimg=(v.width/v.sbimg_wdt)*(v.height/v.sbimg_hgt); // To get The Number of Sub-images
k=0;
//Loop to store the starting positions in v.row and v.col
for(i=0;i<v.height;i=i+v.sbimg_hgt)
{
for(j=0;j<v.width;j=j+v.sbimg_wdt)
{
v.row[k]=i;
v.col[k]=j;
k++;
}
}
v.sbimg_pxl=(v.sbimg_wdt*v.sbimg_hgt)*3;
v.tot_pxl=(v.width*v.height)*3*v.img_cnt;
v.tot_sbimg=v.n_sbimg*v.img_cnt;
PixelIterator * rg_itr;
PixelWand ** pixels;
int a=0;
//loop to store the r,g,b values in dev_pxl array
while(a<v.n_sbimg)
{
i=0;
rg_itr=NewPixelRegionIterator(magick_wand,v.col[a],v.row[a],v.sbimg_wdt,v.sbimg_hgt);//this function grabs the sb_img based on starting positionsof sb_img
for (y=0; y<v.sbimg_hgt; y++)
{
pixels = PixelGetNextIteratorRow(rg_itr, &v.sbimg_wdt);//this function iterates row by row in sb_img
for (x=0;x<v.sbimg_wdt; x++)
{
sbimg_pxl_data[i] = (unsigned char) (255*PixelGetRed(pixels[x]));
sbimg_pxl_data[i+1] = (unsigned char) (255*PixelGetGreen(pixels[x]));
sbimg_pxl_data[i+2] = (unsigned char) (255*PixelGetBlue(pixels[x]));
i=i+3;
}
}
memcpy(start,sbimg_pxl_data,v.sbimg_pxl*sizeof(unsigned char));
start=start+v.sbimg_pxl;
a++;
}
final_str[cnt]=(char*) malloc (250*sizeof(char));
strcpy(final_str[cnt],str);
rg_itr=DestroyPixelIterator(rg_itr);
magick_wand=DestroyMagickWand(magick_wand); // To Destroy the image in the wand
MagickWandTerminus();// To terminate the magick-wand Environment
cnt++;
}
grp_cnt++;
time(&death);
printf("Time taken to get R,G,B Values are %.2f\n",difftime(death,birth));
subimages_pxls(pxl_data,v,argv,grp_cnt,final_str);
free(sbimg_pxl_data);
}
return(0);
}
void subimages_pxls(unsigned char *pxl_data,struct var &v,char **argv, int grp_cnt,char **final_str)
{
printf("\nNo of Sub Images in each Image Partition: %d\n",v.n_sbimg);
unsigned char *dev_pxl,*dev_new_data,*new_data;
new_data=(unsigned char*)malloc(v.tot_pxl*sizeof(unsigned char));
char *ch;
ch=argv[1];
if(strcmp(ch,"GPU")==0||strcmp(ch,"gpu")==0)
{
}
else
{
memcpy(new_data,pxl_data,v.tot_pxl*sizeof(unsigned char));
cudaEvent_t start, stop;
float elapsedTime;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start, 0);
different_pxls(v,pxl_data,new_data);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsedTime, start, stop);
cudaEventDestroy(start); cudaEventDestroy(stop);
printf("Time taken To calculate average of Sub-Image Pixels in CPU %3.1f \n", elapsedTime);
}
time_t bg,nd;
time(&bg);
unsigned int a,x,y;
for(int ip=0;ip<v.img_cnt;ip++)
{
MagickWand *image;
MagickBooleanType status;
MagickWandGenesis(); // to initialize the magick-wand Environment
image=NewMagickWand(); // To create the wand to store image
MagickSetSize(image,v.width,v.height);
status = MagickReadImage(image, "xc:none" );
if (status == MagickFalse)
printf ("status problem");
PixelIterator *new_iterator;
PixelWand **new_pixels;
a=0;
while(a<v.n_sbimg)
{
new_iterator=NewPixelRegionIterator(image,v.col[a],v.row[a],v.sbimg_wdt,v.sbimg_hgt);
for(y=0;y<v.sbimg_hgt;y++)
{
new_pixels = PixelGetNextIteratorRow(new_iterator, &v.sbimg_wdt);
for(x=0;x<v.sbimg_wdt;x++)
{
int t=loc(y,x,v.sbimg_wdt)+ a*v.sbimg_pxl+ip*(v.width*v.height*3);
unsigned char r=new_data[t],g=new_data[t+1],b=new_data[t+2];
char color[64];
sprintf(color,"rgb(%d,%d,%d)",r,g,b);
PixelSetColor(new_pixels[x],color); // this function sets the color of pixel.
PixelSyncIterator(new_iterator);
}
}
a++;
}
int len = strlen(final_str[ip]);
if(final_str[ip][len] == '\0')
{
final_str[ip][len-4] = 0;
}
final_str[ip]=strcat(final_str[ip],"_marked.png");
printf("Writing %d Image : %s\n",ip,final_str[ip]);
status=MagickWriteImages(image,final_str[ip],MagickTrue);
if (status == MagickFalse)
{
printf ("status problem");
}
DestroyPixelIterator(new_iterator);
image=DestroyMagickWand(image); // To Destroy the image in the wand
MagickWandTerminus();
}
time(&nd);
printf("Time Taken to Create %d images is %.2f\n",v.img_cnt,difftime(nd,bg));
free(pxl_data);
for(int i=0;i<v.img_cnt;i++)
{
free(final_str[i]);
}
free(final_str);
}
void different_pxls(struct var v,unsigned char *pxl_data,unsigned char *&new_data)
{
printf("Identifying Different Pixels in CPU\n");
unsigned int x,y,rsum,gsum,bsum,tot_sum,count,sbimg_no;
int nx,ny,gny,gnx;
unsigned int local_img_id,n_nx=0,n_ny=0,startx,starty,actual_img_id;
for(sbimg_no=0;sbimg_no<v.tot_sbimg;sbimg_no++)
{
int lsbimg=sbimg_no%v.n_sbimg;
for(y=0;y<v.sbimg_hgt;y++)
{
for(x=0;x<v.sbimg_wdt;x++)
{
gsum=rsum=bsum=count=0;
startx=(lsbimg%(v.width/v.sbimg_wdt))*v.sbimg_wdt;
starty=round(lsbimg/(v.width/v.sbimg_wdt)*v.sbimg_hgt,v.sbimg_hgt);
unsigned int t1=(loc(y,x,v.sbimg_wdt)+ sbimg_no*v.sbimg_pxl);
for(nx=-1;nx<=1;nx++)
{
for(ny=-1;ny<=1;ny++)
{
gnx=x+nx+startx;
gny=y+ny+starty;
/*if(sbimg_no==289&&x==0&&y==0)
{
printf("sb-img :%d,local sbimg no :%d :: startx:%d :: starty :%d :: Location :%d \n",sbimg_no,lsbimg,startx,starty,t1);
//printf("gnx : %d , gny : %d\n",gnx,gny);
printf("Curr pxl -- R :%d,G :%d,B :%d \n",*(pxl_data+t1),pxl_data[t1+1],pxl_data[t1+2]);
getchar();
}*/
if((gnx<v.width)&&(gnx>=0)&&(gny<v.height)&&(gny>=0))
{
local_img_id=round(gnx/v.sbimg_wdt,1)+round(gny/v.sbimg_hgt,1)*(v.width/v.sbimg_wdt);
actual_img_id=local_img_id+round(sbimg_no/v.n_sbimg,1)*v.n_sbimg;
n_nx=gnx%v.sbimg_wdt;
n_ny=gny%v.sbimg_hgt;
unsigned int t2=(loc(n_ny,n_nx,v.sbimg_wdt)+actual_img_id*v.sbimg_pxl);
rsum=rsum+abs(pxl_data[t1]-pxl_data[t2]);
gsum=gsum+abs(pxl_data[t1+1]-pxl_data[t2+1]);
bsum=bsum+abs(pxl_data[t1+2]-pxl_data[t2+2]);
count++;
/*if(sbimg_no==289&&x==0&&y==0)
{
printf("Local img :%d, Actual img :%d\n",local_img_id,actual_img_id);
printf("n_nx : %d , n_ny :%d ,loc t2 = %d\n",n_nx,n_ny,t2);
printf("Neigh -- R :%d,G :%d,B :%d \n",*(pxl_data+t2),pxl_data[t2+1],pxl_data[t2+2]);
printf("rsum :%d,gsum :%d,bsum :%d\n",rsum,gsum,bsum);
printf("cnt : %d\n",count);
getchar();
}*/
}
}
}
tot_sum=(rsum+gsum+bsum)/3;
/*if(sbimg_no==289&&x==0&&y==0)
{
printf("tot_sum :%d,nxt_sum :%d\n",tot_sum,(tot_sum/(count-1)));
}*/
if((tot_sum/(count-1))>v.treshold)
{
new_data[t1]=255;
new_data[t1+1]=0;
new_data[t1+2]=0;
}
}
}
}
}