static R2Image *
ReadImage(const char *filename)
{
    // Allocate a image
    R2Image *image = new R2Image();
    if (!image) {
        fprintf(stderr, "Unable to allocate image");
        return NULL;
    }
    
    // Read image
    if (!image->Read(filename)) {
        fprintf(stderr, "Unable to read image file %s", filename);
        return NULL;
    }
    
    // Print message
    if (print_verbose) {
        printf("Read image from %s\n", filename);
        printf("  Resolution = %d %d\n", image->Width(), image->Height());
        printf("  L1Norm = %g\n", image->L1Norm());
        printf("  L2Norm = %g\n", image->L2Norm());
        fflush(stdout);
    }
    
    // Return image
    return image;
}
Beispiel #2
0
int R3Mesh::
ReadImage(const char *filename)
{
  // Create a mesh by reading an image file, 
  // constructing vertices at (x,y,luminance), 
  // and connecting adjacent pixels into faces. 
  // That is, the image is interpretted as a height field, 
  // where the luminance of each pixel provides its z-coordinate.

  // Read image
  R2Image *image = new R2Image();
  if (!image->Read(filename)) return 0;

  // Create vertices and store in arrays
  R3MeshVertex ***vertices = new R3MeshVertex **[image->Width() ];
  for (int i = 0; i < image->Width(); i++) {
    vertices[i] = new R3MeshVertex *[image->Height() ];
    for (int j = 0; j < image->Height(); j++) {
      double luminance = image->Pixel(i, j).Luminance();
      double z = luminance * image->Width();
      R3Point position((double) i, (double) j, z);
      R2Point texcoords((double) i, (double) j);
      vertices[i][j] = CreateVertex(position, R3zero_vector, texcoords);
    }
  }

  // Create faces
  vector<R3MeshVertex *> face_vertices;
  for (int i = 1; i < image->Width(); i++) {
    for (int j = 1; j < image->Height(); j++) {
      face_vertices.clear();
      face_vertices.push_back(vertices[i-1][j-1]);
      face_vertices.push_back(vertices[i][j-1]);
      face_vertices.push_back(vertices[i][j]);
      CreateFace(face_vertices);
      face_vertices.clear();
      face_vertices.push_back(vertices[i-1][j-1]);
      face_vertices.push_back(vertices[i][j]);
      face_vertices.push_back(vertices[i-1][j]);
      CreateFace(face_vertices);
    }
  }

  // Delete vertex arrays
  for (int i = 0; i < image->Width(); i++) delete [] vertices[i];
  delete [] vertices;

  // Delete image
  delete image;

  // Return success
  return 1;
}
Beispiel #3
0
int R3Model::
ReadObjMtlFile(const char *dirname, const char *mtlname)
{
  // Open file
  char filename[1024];
  sprintf(filename, "%s/%s", dirname, mtlname);
  FILE *fp = fopen(filename, "r");
  if (!fp) {
    RNFail("Unable to open file %s", filename);
    return 0;
  }

  // Parse file
  char buffer[1024];
  int line_count = 0;
  R3Brdf *brdf = NULL;
  R2Texture *texture = NULL;
  R3Material *material = NULL;
  while (fgets(buffer, 1023, fp)) {
    // Increment line counter
    line_count++;

    // Skip white space
    char *bufferp = buffer;
    while (isspace(*bufferp)) bufferp++;

    // Skip blank lines and comments
    if (*bufferp == '#') continue;
    if (*bufferp == '\0') continue;

    // Get keyword
    char keyword[80];
    if (sscanf(bufferp, "%s", keyword) != 1) {
      RNFail("Syntax error on line %d in file %s", line_count, filename);
      return 0;
    }

    // Check keyword
    if (!strcmp(keyword, "newmtl")) {
      // Parse line
      char name[1024];
      if (sscanf(bufferp, "%s%s", keyword, name) != (unsigned int) 2) {
        RNFail("Syntax error on line %d in file %s", line_count, filename);
        return 0;
      }

      // Create new material
      texture = NULL;
      brdf = new R3Brdf();
      material = new R3Material(brdf, texture, name);
      materials.Insert(material);
      RNArray<R3Triangle *> *mat_tris = new RNArray<R3Triangle *>();
      material_triangles.Insert(mat_tris);
    }
    else if (!strcmp(keyword, "Ka")) {
      // Parse line
      double r, g, b;
      if (sscanf(bufferp, "%s%lf%lf%lf", keyword, &r, &g, &b) != (unsigned int) 4) {
        RNFail("Syntax error on line %d in file %s", line_count, filename);
        return 0;
      }

      // Set ambient reflectance 
      if (material && brdf) {
        brdf->SetAmbient(RNRgb(r, g, b));
        material->Update();
      }
    }
    else if (!strcmp(keyword, "Kd")) {
      // Parse line
      double r, g, b;
      if (sscanf(bufferp, "%s%lf%lf%lf", keyword, &r, &g, &b) != (unsigned int) 4) {
        RNFail("Syntax error on line %d in file %s", line_count, filename);
        return 0;
      }

      // Set diffuse reflectance 
      if (material && brdf) {
        brdf->SetDiffuse(RNRgb(r, g, b));
        material->Update();
      }
    }
    else if (!strcmp(keyword, "Ks")) {
      // Parse line
      double r, g, b;
      if (sscanf(bufferp, "%s%lf%lf%lf", keyword, &r, &g, &b) != (unsigned int) 4) {
        RNFail("Syntax error on line %d in file %s", line_count, filename);
        return 0;
      }

      // Set specular reflectance 
      if (material && brdf) {
        brdf->SetSpecular(RNRgb(r, g, b));
        material->Update();
      }
    }
    else if (!strcmp(keyword, "Ns")) {
      // Parse line
      double ns;
      if (sscanf(bufferp, "%s%lf", keyword, &ns) != (unsigned int) 2) {
        RNFail("Syntax error on line %d in file %s", line_count, filename);
        return 0;
      }

      // Set shininess
      if (material && brdf) {
        brdf->SetShininess(ns);
        material->Update();
      }
    }
    else if (!strcmp(keyword, "Ni")) {
      // Parse line
      double index_of_refraction;
      if (sscanf(bufferp, "%s%lf", keyword, &index_of_refraction) != (unsigned int) 2) {
        RNFail("Syntax error on line %d in file %s", line_count, filename);
        return 0;
      }

      // Set index of refraction
      if (material && brdf) {
        brdf->SetIndexOfRefraction(index_of_refraction);
        material->Update();
      }
    }
    else if (!strcmp(keyword, "d")) {
      // Parse line
      double transparency;
      if (sscanf(bufferp, "%s%lf", keyword, &transparency) != (unsigned int) 2) {
        RNFail("Syntax error on line %d in file %s", line_count, filename);
        return 0;
      }

      // Set opacity
      if (material && brdf) {
        brdf->SetOpacity(1 - transparency);
        material->Update();
      }
    }
    else if (!strcmp(keyword, "map_Kd")) {
      // Parse line
      char texture_name[1024];
      if (sscanf(bufferp, "%s%s", keyword, texture_name) != (unsigned int) 2) {
        RNFail("Syntax error on line %d in file %s", line_count, filename);
        return 0;
      }

      // Set texture
      if (material) {
        char texture_filename[1024];
        sprintf(texture_filename, "%s/%s", dirname, texture_name);
        R2Image *image = new R2Image();
        if (!image->Read(texture_filename)) return 0;
        R2Texture *texture = new R2Texture(image);
        material->SetTexture(texture);
        material->Update();
      }
    }
  }

  // Close file
  fclose(fp);

  // Return success
  return 1;
}
Beispiel #4
0
int 
main(int argc, char **argv)
{
  // Look for help
  for (int i = 0; i < argc; i++) {
    if (!strcmp(argv[i], "-help")) {
      ShowUsage();
    }
	if (!strcmp(argv[i], "-svdTest")) {
      R2Image *image = new R2Image();
	  image->svdTest();
	  return 0;
    }
  }

  // Read input and output image filenames
  if (argc < 3)  ShowUsage();
  argv++, argc--; // First argument is program name
  char *input_image_name = *argv; argv++, argc--; 
  char *output_image_name = *argv; argv++, argc--; 

  // Allocate image
  R2Image *image = new R2Image();
  if (!image) {
    fprintf(stderr, "Unable to allocate image\n");
    exit(-1);
  }

  // Read input image
  if (!image->Read(input_image_name)) {
    fprintf(stderr, "Unable to read image from %s\n", input_image_name);
    exit(-1);
  }

  // Initialize sampling method
  int sampling_method = R2_IMAGE_POINT_SAMPLING;

  // Parse arguments and perform operations 
  while (argc > 0) {
    if (!strcmp(*argv, "-brightness")) {
      CheckOption(*argv, argc, 2);
      double factor = atof(argv[1]);
      argv += 2, argc -=2;
      image->Brighten(factor);
    }
	else if (!strcmp(*argv, "-sobelX")) {
      argv++, argc--;
      image->SobelX();
    }
	else if (!strcmp(*argv, "-sobelY")) {
      argv++, argc--;
      image->SobelY();
    }
	else if (!strcmp(*argv, "-log")) {
      argv++, argc--;
      image->LoG();
    }
    else if (!strcmp(*argv, "-saturation")) {
      CheckOption(*argv, argc, 2);
      double factor = atof(argv[1]);
      argv += 2, argc -= 2;
      image->ChangeSaturation(factor);
    }
	else if (!strcmp(*argv, "-harris")) {
      CheckOption(*argv, argc, 2);
      double sigma = atof(argv[1]);
      argv += 2, argc -= 2;
      image->Harris(sigma);
    }
    else if (!strcmp(*argv, "-blur")) {
      CheckOption(*argv, argc, 2);
      double sigma = atof(argv[1]);
      argv += 2, argc -= 2;
      image->Blur(sigma);
    }
    else if (!strcmp(*argv, "-sharpen")) {
      argv++, argc--;
      image->Sharpen();
    }
    else if (!strcmp(*argv, "-matchTranslation")) {
      CheckOption(*argv, argc, 2);
      R2Image *other_image = new R2Image(argv[1]);
      argv += 2, argc -= 2;
      image->blendOtherImageTranslated(other_image);
      delete other_image;
    }
    else if (!strcmp(*argv, "-matchHomography")) {
      CheckOption(*argv, argc, 2);
      R2Image *other_image = new R2Image(argv[1]);
      argv += 2, argc -= 2;
      image->blendOtherImageHomography(other_image);
      delete other_image;
    }
    else {
      // Unrecognized program argument
      fprintf(stderr, "image: invalid option: %s\n", *argv);
      ShowUsage();
    }
  }

  // Write output image
  if (!image->Write(output_image_name)) {
    fprintf(stderr, "Unable to read image from %s\n", output_image_name);
    exit(-1);
  }

  // Delete image
  delete image;

  // Return success
  return EXIT_SUCCESS;
}
Beispiel #5
0
int 
main(int argc, char **argv)
{
  // Look for help
  for (int i = 0; i < argc; i++) {
    if (!strcmp(argv[i], "-help")) {
      ShowUsage();
    }
  }

  // Read input and output image filenames
  if (argc < 3)  ShowUsage();
  argv++, argc--; // First argument is program name
  char *input_image_name = *argv; argv++, argc--; 
  char *output_image_name = *argv; argv++, argc--; 

  // Allocate image
  R2Image *image = new R2Image();
  if (!image) {
    fprintf(stderr, "Unable to allocate image\n");
    exit(-1);
  }

  // Read input image
  if (!image->Read(input_image_name)) {
    fprintf(stderr, "Unable to read image from %s\n", input_image_name);
    exit(-1);
  }

  // Initialize sampling method
  int sampling_method = R2_IMAGE_POINT_SAMPLING;

  // Parse arguments and perform operations 
  while (argc > 0) {
    if (!strcmp(*argv, "-bilateral")) {
      CheckOption(*argv, argc, 3);
      double sx = atof(argv[1]);
      double sy = atof(argv[2]);
      argv += 3, argc -= 3;
      image->BilateralFilter(sy, sx);
    } 
    else if (!strcmp(*argv, "-blackandwhite")) {
      argv++, argc--;
      image->BlackAndWhite();
    }
    else if (!strcmp(*argv, "-blur")) {
      CheckOption(*argv, argc, 2);
      double sigma = atof(argv[1]);
      argv += 2, argc -= 2;
      image->Blur(sigma);
    }
    else if (!strcmp(*argv, "-brightness")) {
      CheckOption(*argv, argc, 2);
      double factor = atof(argv[1]);
      argv += 2, argc -=2;
      image->Brighten(factor);
    }
    else if (!strcmp(*argv, "-composite")) {
      CheckOption(*argv, argc, 5);
      R2Image *top_image = new R2Image(argv[2]);
      R2Image *bottom_mask = new R2Image(argv[1]);
      R2Image *top_mask = new R2Image(argv[3]);
      int operation = atoi(argv[4]);
      argv += 5, argc -= 5;
      image->CopyChannel(*bottom_mask, R2_IMAGE_BLUE_CHANNEL, R2_IMAGE_ALPHA_CHANNEL);
      top_image->CopyChannel(*top_mask, R2_IMAGE_BLUE_CHANNEL, R2_IMAGE_ALPHA_CHANNEL);
      image->Composite(*top_image, operation);
      delete top_image;
      delete bottom_mask;
      delete top_mask;
    }
    else if (!strcmp(*argv, "-contrast")) {
      CheckOption(*argv, argc, 2);
      double factor = atof(argv[1]);
      argv += 2, argc -= 2;
      image->ChangeContrast(factor);
    }
    else if (!strcmp(*argv, "-crop")) {
      CheckOption(*argv, argc, 5);
      int x = atoi(argv[1]);
      int y = atoi(argv[2]);
      int w = atoi(argv[3]);
      int h = atoi(argv[4]);
      argv += 5, argc -= 5;
      image->Crop(x, y, w, h);
    }
    else if (!strcmp(*argv, "-dither")) {
      CheckOption(*argv, argc, 3);
      int dither_method = atoi(argv[1]);
      int nbits = atoi(argv[2]);
      argv += 3, argc -= 3;
      if (dither_method == 0) image->RandomDither(nbits);
      else if (dither_method == 1) image->OrderedDither(nbits);
      else if (dither_method == 2) image->FloydSteinbergDither(nbits);
      else { fprintf(stderr, "Invalid dither method: %d\n", dither_method); exit(-1); }
    }
    else if (!strcmp(*argv, "-edge")) {
      argv++, argc--;
      image->EdgeDetect();
    } 
    else if (!strcmp(*argv, "-extract")) {
      CheckOption(*argv, argc, 2);
      int channel = atoi(argv[1]);
      argv += 2, argc -= 2;
      image->ExtractChannel(channel);
    }
    else if (!strcmp(*argv, "-fun")) {
      image->Fun(sampling_method);
      argv++, argc--;
    }
    else if (!strcmp(*argv, "-gamma")) {
      CheckOption(*argv, argc, 2);
      double factor = atof(argv[1]);
      argv += 2, argc -= 2;
      image->ApplyGamma(factor);
    }
    else if (!strcmp(*argv, "-median")) {
      CheckOption(*argv, argc, 2);
      double sigma = atof(argv[1]);
      argv += 2, argc -= 2;
      image->MedianFilter(sigma);
    }
    else if (!strcmp(*argv, "-motionblur")) {
      CheckOption(*argv, argc, 1);
      int amount = atoi(argv[1]);
      argv += 2, argc -= 2;
      image->MotionBlur(amount);
    }
    else if (!strcmp(*argv, "-morph")) {
      int nsegments = 0;
      R2Segment *source_segments = NULL;
      R2Segment *target_segments = NULL;
      CheckOption(*argv, argc, 4);
      R2Image *target_image = new R2Image(argv[1]);
      ReadCorrespondences(argv[2], source_segments, target_segments, nsegments);
      double t = atof(argv[3]);
      argv += 4, argc -= 4;
      image->Morph(*target_image, source_segments, target_segments, nsegments, t, sampling_method);
      delete target_image;
    }
    else if (!strcmp(*argv, "-noise")) {
      CheckOption(*argv, argc, 2);
      double factor = atof(argv[1]);
      argv += 2, argc -= 2;
      image->AddNoise(factor);
    }
    else if (!strcmp(*argv, "-quantize")) {
      CheckOption(*argv, argc, 2);
      int nbits = atoi(argv[1]);
      argv += 2, argc -= 2;
      image->Quantize(nbits);
    }
    else if (!strcmp(*argv, "-rotate")) {
      CheckOption(*argv, argc, 2);
      double angle = atof(argv[1]);
      argv += 2, argc -= 2;
      image->Rotate(angle, sampling_method);
    }
    else if (!strcmp(*argv, "-sampling")) {
      CheckOption(*argv, argc, 2);
      sampling_method = atoi(argv[1]);
      argv += 2, argc -= 2;
    }
    else if (!strcmp(*argv, "-saturation")) {
      CheckOption(*argv, argc, 2);
      double factor = atof(argv[1]);
      argv += 2, argc -= 2;
      image->ChangeSaturation(factor);
    }
    else if (!strcmp(*argv, "-scale")) {
      CheckOption(*argv, argc, 3);
      double sx = atof(argv[1]);
      double sy = atof(argv[2]);
      argv += 3, argc -= 3;
      image->Scale(sx, sy, sampling_method);
    }
    else if (!strcmp(*argv, "-sharpen")) {
      argv++, argc--;
      image->Sharpen();
    }
    else {
      // Unrecognized program argument
      fprintf(stderr, "image: invalid option: %s\n", *argv);
      ShowUsage();
    }
  }

  // Write output image
  if (!image->Write(output_image_name)) {
    fprintf(stderr, "Unable to write image to %s\n", output_image_name);
    exit(-1);
  }

  // Delete image
  delete image;

  // Return success
  return EXIT_SUCCESS;
}