Ejemplo n.º 1
0
//============================================================================
//                                  Main
//============================================================================
int main(int argc, char* argv[])
{
  //--------------------------------------------------------------------------
  // Read file "gargouille.ppm" into image (width and height)
  //--------------------------------------------------------------------------
  u_char* image = NULL;
  int width;
  int height;
  FILE* ppm_input;
 
  ppm_read_from_file(&width, &height, &image, "gargouille.ppm");
  
  // Create image "newimage" and initialise its parameters
  img* newimage = (img*)malloc(sizeof(img));
  newimage->data = image;
  newimage->width = width;
  newimage->height = height;

  //--------------------------------------------------------------------------
  // Create a desaturated (B&W) copy of the image we've just read and
  // write it into "gargouille_BW.ppm"
  //--------------------------------------------------------------------------

  // Copy image into image_bw
  int width_bw;
  int height_bw;
  char*image_bw = (u_char*) malloc(3 * width * height * sizeof(*image_bw));
  memcpy(image_bw, image, 3 * width * height * sizeof(*image_bw));

  // Desaturate image_bw
  ppm_desaturate(image_bw, width, height);

  // Write the desaturated image into "gargouille_BW.ppm"
  ppm_write_to_file(width, height, image_bw, "gargouille_BW.ppm");

  // Free the desaturated image
  free(image_bw);

  // Create image "bw" and initialise its parameters
  img* bw = (img*)malloc(sizeof(img));
  bw->width = width_bw;
  bw->height = height_bw;
  bw->data = image_bw;

  //--------------------------------------------------------------------------
  // Create a resized copy of the image and
  // write it into "gargouille_small.ppm"
  //--------------------------------------------------------------------------

  // Copy image into image_small
  int width_small; 
  int height_small;
  u_char* image_small = (u_char*) malloc(3 * width_small * height_small * sizeof(*image_small));
  memcpy(image_small, image, 3 * width_small * height_small * sizeof(*image_small));

  // Shrink image_small size 2-fold
  ppm_shrink(&image_small, &width_small, &height_small, 2);

  // Write the desaturated image into "gargouille_small.ppm"
  ppm_write_to_file(width_small, height_small, image_small, "gargouille_small.ppm");

  // Create image "small" and initialise its parameters
  img* small = (img*)malloc(sizeof(img));
  small->width = width_small;
  small->height = height_small;
  small->data = image_small;

  // Free the not yet freed images
  free(image);
  free(image_small);

  return 0;
}
Ejemplo n.º 2
0
Archivo: ppm2.c Proyecto: Xius/tpcaucpp
//============================================================================
//                                  Main
//============================================================================
int main(int argc, char* argv[])
{
  //--------------------------------------------------------------------------
  // Read file "gargouille.ppm" into image (width and height)
  //--------------------------------------------------------------------------

  screen pixel;
  char* filename;
  filename = "gargouille.ppm";
  ppm_read_from_file(filename, &pixel.width, &pixel.height, &pixel.image);


  //--------------------------------------------------------------------------
  // Create a desaturated (B&W) copy of the image we've just read and
  // write it into "gargouille_BW.ppm"
  //--------------------------------------------------------------------------
  // Copy image into image_bw
  screen pixel_bw;
  pixel_bw.width  = pixel.width;
  pixel_bw.height = pixel.height;
  pixel_bw.image = (unsigned char*) malloc(3 * pixel.width * pixel.height * sizeof(*pixel_bw.image));
  memcpy(pixel_bw.image, pixel.image, 3 * pixel.width * pixel.height * sizeof(*pixel_bw.image));

  // Desaturate image_bw
  ppm_desaturate(pixel_bw.image, pixel.width, pixel.height);

  // Write the desaturated image into "gargouille_BW.ppm"
  filename = "gargouille_BW.ppm";
  FILE* ppm_output = fopen("gargouille_BW.ppm", "wb");
  ppm_write_to_file(filename,pixel.width, pixel.height, pixel_bw.image);
  fclose(ppm_output);

  // Free the desaturated image
  free(pixel_bw.image);


  //--------------------------------------------------------------------------
  // Create a resized copy of the image and
  // write it into "gargouille_small.ppm"
  //--------------------------------------------------------------------------
  // Copy image into image_small
  screen pixelsmall;
  pixelsmall.width  = pixel.width;
  pixelsmall.height = pixel.height;
  pixelsmall.image = (unsigned char*) malloc(3 * pixel.width * pixel.height * sizeof(*pixelsmall.image));
  memcpy(pixelsmall.image, pixel.image, 3 * pixel.width * pixelsmall.height * sizeof(pixelsmall.image));

  // Shrink image_small size 2-fold
  ppm_shrink(&pixelsmall.image, &pixelsmall.width, &pixelsmall.height, 2);

  // Write the desaturated image into "gargouille_small.ppm"
  filename = "gargouille_small.ppm";
  ppm_write_to_file(filename ,pixelsmall.width, pixelsmall.height, pixelsmall.image);


  // Free the not yet freed images
  free(pixel.image);
  free(pixelsmall.image);

  return 0;
}