//Add file names to the corresponding arrays.
void addToArray(char* file_name,int i )
{
  
      
      if(is_jpg(file_name))
	{
	  
	  jpg_array[jpg_count] = file_name;
	  jpg_count++;
	}
      
      if(is_png(file_name))
	{
	  
	  png_array[png_count] = file_name;
	  png_count++;
	}

      if(is_gif(file_name))
	{
	  
	  gif_array[gif_count] = file_name;
	  gif_count++;
	}
      
      if(is_tiff(file_name))
	{
	  
	  tiff_array[tiff_count] = file_name;
	  tiff_count++;
	}
}
static void
save_file_create_ready_cb (GObject *source,
                           GAsyncResult *res,
                           gpointer user_data)
{
  ScreenshotApplication *self = user_data;
  GFileOutputStream *os;
  GError *error = NULL;
  gchar *basename = g_file_get_basename (G_FILE (source));
  gchar *extension = g_strrstr (basename, ".");
  gchar *format = NULL;
  GSList *formats = NULL;

  if (extension == NULL)
    extension = "png";
  else
    extension++;

  format = extension;

  formats = gdk_pixbuf_get_formats();
  g_slist_foreach (formats,
                   find_out_writable_format_by_extension,
                   (gpointer) &format);
  g_slist_free (formats);
  g_free (basename);

  if (self->priv->should_overwrite)
    os = g_file_replace_finish (G_FILE (source), res, &error);
  else
    os = g_file_create_finish (G_FILE (source), res, &error);

  if (error != NULL)
    {
      save_pixbuf_handle_error (self, error);
      g_error_free (error);
      return;
    }

  if (is_png (format))
    {
      if (has_profile (self))
        save_with_description_and_profile (self, os, format);
      else
        save_with_description (self, os, format);
    }
  else
    {
      save_with_no_profile_or_description (self, os, format);
    }

  g_object_unref (os);
  g_free (format);
}
Beispiel #3
0
inline boost::optional<std::string> type_from_filename(std::string const& filename)

{
    typedef boost::optional<std::string> result_type;
    if (is_png(filename)) return result_type("png");
    if (is_jpeg(filename)) return result_type("jpeg");
    if (is_tiff(filename)) return result_type("tiff");
    if (is_pdf(filename)) return result_type("pdf");
    if (is_svg(filename)) return result_type("svg");
    if (is_ps(filename)) return result_type("ps");
    return result_type();
}
  int main(int argc, char* argv[])
{

  char* recvBuff = malloc(512);

  if(argc<3)
    {
      perror("Incorrect number of arguments\n");
      exit(0);
    }



  struct stat s;

  int err = stat("images",&s);

  if(-1==err)
    {
      if(ENOENT == errno)
	{
	  printf("Directory for images does not exist, creating it\n");
	  mkdir("images",0777);
	  printf("Directory created\n");
	}
    }
  else
    {
      if(S_ISDIR(s.st_mode))
	{
	  printf("Directory for images exists\n");
	}
    }

  struct stat st = {0};
  
  if(stat("images/catalog.csv",&st)!=-1)
    {
      perror("Catalog exists. Deleting it..\n");
      remove("images/catalog.csv");
    }
 


  htmlpath = malloc(200);

  strcpy(htmlpath,"images/");
  strcat(htmlpath,"download.html");

    
  

  int sockfd;
  
  struct sockaddr_in serv_addr;
  
  if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
    {
      perror("Could not create socket connection\n");
      exit(0);
    }

  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(atoi(argv[2]));
  serv_addr.sin_addr.s_addr = inet_addr(argv[1]);

  if(connect(sockfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr))<0)
    {
      perror("Connection failed\n");
      exit(0);
    }

  char* logpath = malloc(200);

  strcpy(logpath,"images/catalog.csv");

  
  write_to_image(logpath,sockfd);


  FILE* fp;

  fp = fopen(logpath,"r");
  
  char ch;

  int lines=0;
  while(!feof(fp))
    {
      ch = fgetc(fp);
      if(ch == '\n')    //Get the number of lines in log file.
	{
	  lines++;
	}
    }
  
  fclose(fp);

  char* line = NULL;
  
  size_t len = 0;

  fp = fopen(logpath,"r");
  getline(&line,&len,fp);             //Read log file line by line.
  

  printf("Dumping catalog.csv content\n");

  int i = 1;
  int j;
  int k;
  int m;
  int n;
  for(k = 0;k<lines-1;k++)
    {
      strcpy(file_array[file_count],"images/");
      printf("[%d] ",i);
      getline(&line,&len,fp);

      for(j=0;line[j]!=',';j++)
	{
	  
	  file_array[file_count][j+7] = line[j];
	  printf("%c",line[j]);
	}

      char* name = strtok(line,",");
      char* size = strtok(NULL,",");
      char* sum = strtok(NULL,",");
      strcpy(checksums[file_count],sum);
      //printf("%s\n",checksums[file_count]);
     
    
	  

      
      
      file_count++;
      i++;
      printf(" \n");
    }
      
  fclose(fp);
  

  for(i=0;i<file_count;i++)
    {
      
      addToArray(file_array[i],i);
    }





  if(argv[3]!=NULL)                  //Arg[3] present. Passive mode.
     {
      printf("Passive mode\n");
      
    

      write(sockfd,argv[3],11);

      write(sockfd,"ready",5);
  
      if(is_jpg(argv[3]))                 
	{                               //Write jpg images
	  for(i=0;i<jpg_count;i++)
	    write_to_image(jpg_array[i],sockfd);
	}

      if(is_png(argv[3]))
	{
	  for(i=0;i<png_count;i++)          //Write png images.
	    write_to_image(png_array[i],sockfd);
	}
      
       if(is_gif(argv[3]))
	 {                                //Write gif files.
	  for(i=0;i<gif_count;i++)
	    write_to_image(gif_array[i],sockfd);
	}

       if(is_tiff(argv[3]))
	 {                                 //Write tiff files.
	   for(i=0;i<tiff_count;i++)
	     write_to_image(tiff_array[i],sockfd);
	 }
	  
    }

   else
     {                              //argv[3] not present. interactive mode.
      inter = 1;
      printf("Interactive mode\n");
      write(sockfd,"interactive",11);

      char input[1];
      while(1)
	{

	  printf("Enter the file number to download\n");
	  scanf("%s",input);
	  
	  if(atoi(input)==0)
	    {
	      write(sockfd,input,2);
	      printf("Entered 0. Exiting...\n");
	      close(sockfd);
	      break;
	    }
	  printf("File number entered:%d\n",atoi(input));

	  write(sockfd,input,2);
	  write_to_image(file_array[atoi(input)-1],sockfd);
	}
      
    }

    printf("Done\n");


    //Create html file.
    FILE* html;
    
    html = fopen("./images/download.html","w+");
    
    fprintf(html,"<html><head>Downloaded Images</head>");
    fprintf(html,"<title>Downloaded Images</title>\n");
    fprintf(html,"<p>");


    /*
      For each file in the downloaded(images) folder, look for the
      file in the file_array,get the index, see if checksums 
      are equal. Accordingly write to html.
    */

    DIR* dir;
    struct dirent* file;

    dir = opendir("./images/");
    
    while((file = readdir(dir))!=NULL)
      {
	int i;
	int j;
	if(file->d_name[0] == '.') continue;
	if(strcmp(file->d_name,"catalog.csv")==0) continue;
	char* path = malloc(200);
	strcpy(path,"images/");
	strcat(path,file->d_name);
	unsigned char sum[MD5_DIGEST_LENGTH];
	char convertedsum[(MD5_DIGEST_LENGTH*2)+1];
	md5sum(path,sum);

	/**stackoverflow.com/questions/7627723/how-to-create-a-md5-hash-of-a-string-in-c**/
	for(j=0;j<MD5_DIGEST_LENGTH;j++)
	  {
	    sprintf(convertedsum+(j*2),"%02x",sum[j]);
	  }
	convertedsum[2*MD5_DIGEST_LENGTH]='\0';
	char* other_name = malloc(200);

	for(i=0;i<file_count;i++)
	  {
	    
	    if(strstr(path,file_array[i]))
	      {
		
		if(strncmp(convertedsum,checksums[i],MD5_DIGEST_LENGTH)==0)
		  {
		    
		    fprintf(html,"Checksum match!\n");
		    fprintf(html, "      ");
		    fprintf(html, "<a href = \"");
		    
		    fprintf(html,"%s\">",file->d_name);
		    fprintf(html,"%s",file->d_name);
		    fprintf(html,"</a></p>\n");

		  }
		else
		  {
		    
		    fprintf(html,"Checksum mismatch");
		    fprintf(html,"       ");
		    fprintf(html,"%s",file->d_name);
		    fprintf(html,"</p>");
		  }
	      }
	  }
      }

     
	    


	fprintf(html,"</body></html>");
	fclose(html);


  return 0;
    
     

}
Beispiel #5
0
int main(void) {
    struct dirent **namelist;
    int i, n, N;
    unsigned char *image = NULL;
    float *image_bw_f;
    unsigned char *image_bw_u8;
    float *image_out_old;
    float *image_out_new;
    char fullpath[255];
    char outpath_old[255];
    char outpath_new[255];
    int imW, imH;

    float sigma;
    float err;

    sigma = 1.0;

    N = scandir("demo_simplexy_images", &namelist, is_input_image, alphasort);
    if (N < 0) {
        perror("scandir");
        return 1;
    }

    for (n = 0; n < N; n++) {

        strcpy(fullpath, "demo_simplexy_images/");
        strcat(fullpath, namelist[n]->d_name);

        strcpy(outpath_old, "demo_simplexy_images/out_");
        strcat(outpath_old, namelist[n]->d_name);
        outpath_old[strlen(outpath_old)-4] = '\0';
        strcat(outpath_old, "_old");
        strcat(outpath_old, ".png");

        strcpy(outpath_new, "demo_simplexy_images/out_");
        strcat(outpath_new, namelist[n]->d_name);
        outpath_new[strlen(outpath_new)-4] = '\0';
        strcat(outpath_new, "_new");
        strcat(outpath_new, ".png");

        fprintf(stderr,"demo_dsmooth: loading %s ", fullpath);

        if (is_png(namelist[n])) {
            fprintf(stderr, "as a PNG\n");
            image = cairoutils_read_png(fullpath, &imW, &imH);
        }

        if (is_jpeg(namelist[n])) {
            fprintf(stderr, "as a JPEG\n");
            image = cairoutils_read_jpeg(fullpath, &imW, &imH);
        }

        image_bw_u8 = to_bw_u8(image, imW, imH);
        image_bw_f = malloc(sizeof(float) * imW * imH);
        for (i = 0; i < imW*imH; i++) {
            image_bw_f[i] = (float)image_bw_u8[i];
        }

        image_out_old = malloc(sizeof(float)*imW*imH);
        image_out_new = malloc(sizeof(float)*imW*imH);

        fprintf(stderr,"demo_dsmooth: running %s through dsmooth\n", fullpath);
        dsmooth(image_bw_f, imW, imH, sigma, image_out_old);

        fprintf(stderr,"demo_dsmooth: running %s through dsmooth2\n", fullpath);
        dsmooth2(image_bw_f, imW, imH, sigma, image_out_new);

        err = 0.0;
        for (i = 0; i < imW*imH; i++) {
            err += fabs(image_out_old[i]-image_out_new[i]);
        }
        err = err / (imW*imH);

        fprintf(stderr, "demo_dsmooth: error between smooths: %f per pixel\n", err);

        //		fprintf(stderr, "demo_dsmooth: writing old dsmoothed image to %s\n", outpath_old);
        //		cairoutils_write_png(outpath_old, to_cairo_bw(image_out_old, imW, imH), imW, imH);

        //		fprintf(stderr, "demo_dsmooth: writing new dsmoothed image to %s\n", outpath_new);
        //		cairoutils_write_png(outpath_new, to_cairo_bw(image_out_new, imW, imH), imW, imH);

        free(namelist[n]);
        free(image);
        free(image_bw_f);
        free(image_bw_u8);
        free(image_out_old);
        free(image_out_new);

    }
    free(namelist);

    return 0;
}
Beispiel #6
0
int is_image(const struct dirent *de) {
    return is_jpeg(de) || is_png(de);
}