wcs2kml.cc
// The real main is defined here inside of the namespace to reduce the amount
// of typing.
The real main is defined here inside of the namespace to reduce the amount
of typing.
The real main is defined here inside of the namespace to reduce the amount of typing .
int Main(int argc, char **argv) {
  string usage = "Usage: ";
  usage += argv[0];
  usage += " --imagefile=<PNG image> --fitsfile=<FITS file with WCS>";
  google::SetUsageMessage(usage);
  google::ParseCommandLineFlags(&argc, &argv, true);
  if (FLAGS_imagefile.empty() || FLAGS_fitsfile.empty()) {
    fprintf(stderr, "%s\n", usage.c_str());
    fprintf(stderr, "Type '%s --help' for list of options\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  printf("Reading image %s...\n", FLAGS_imagefile.c_str());
  Image image;
  if (!image.Read(FLAGS_imagefile)) {
    fprintf(stderr, "Unable to read image file '%s'\n",
            FLAGS_imagefile.c_str());
    exit(EXIT_FAILURE);
  }
  printf("Input image is size %d x %d\n", image.width(), image.height());
  printf("Reading FITS file %s...\n", FLAGS_fitsfile.c_str());
  WcsProjection wcs(FLAGS_fitsfile, image.width(), image.height());
  printf("Computing bounding box in ra, dec...\n");
  Color bg_color(4);
  bg_color.SetAllChannels(0);
  SkyProjection projection(image, wcs);
  projection.SetBackgroundColor(bg_color);
  const BoundingBox &bounding_box = projection.bounding_box();
  printf("This image:\n");
  printf("Wraps around in ra? ");
  if (bounding_box.is_wrapped()) {
    printf("yes\n");
  } else {
    printf("no\n");
  }
  printf("Crosses the north pole? ");
  if (bounding_box.crosses_north_pole()) {
    printf("yes\n");
  } else {
    printf("no\n");
  }
  printf("Crosses the south pole? ");
  if (bounding_box.crosses_south_pole()) {
    printf("yes\n");
  } else {
    printf("no\n");
  }
  double ra_min, ra_max;
  double dec_min, dec_max;
  bounding_box.GetWrappedRaBounds(&ra_min, &ra_max);
  bounding_box.GetDecBounds(&dec_min, &dec_max);
  printf("Range in ra is %.8f to %.8f\n", ra_min, ra_max);
  printf("Range in dec is %.8f to %.8f\n", dec_min, dec_max);
  if (FLAGS_input_image_origin_is_upper_left) {
    projection.set_input_image_origin(SkyProjection::UPPER_LEFT);
  } else {
    projection.set_input_image_origin(SkyProjection::LOWER_LEFT);
  }
  if (FLAGS_output_width > 0 && FLAGS_output_height > 0) {
    projection.SetProjectedSize(FLAGS_output_width, FLAGS_output_height);
  }
  if (FLAGS_copy_input_size) {
    projection.SetProjectedSize(image.width(), image.height());
  }
  projection.SetMaxSideLength(FLAGS_max_side_length);
  printf("Projected image size will be %d x %d\n",
         projection.projected_width(), projection.projected_height());
  if (FLAGS_automask) {
    printf("Using automasking for color:\n");
    printf("Red: %d\n", FLAGS_automask_red);
    printf("Green: %d\n", FLAGS_automask_green);
    printf("Blue: %d\n", FLAGS_automask_blue);
    Color mask_out_color(4);
    mask_out_color.SetChannel(0, FLAGS_automask_red);
    mask_out_color.SetChannel(1, FLAGS_automask_green);
    mask_out_color.SetChannel(2, FLAGS_automask_blue);
    mask_out_color.SetChannel(3, 255);
    Image mask;
    Mask::CreateMask(image, mask_out_color, &mask);
    Mask::SetAlphaChannelFromMask(mask, &image);
    printf("Writing mask to file %s.png...\n", FLAGS_automaskfile.c_str());
    if (!mask.Write(FLAGS_automaskfile + ".png")) {
      fprintf(stderr, "Couldn't write mask to file\n");
      exit(EXIT_FAILURE);
    }
  } else if (!FLAGS_maskfile.empty()) {
    printf("Using masking from %s\n", FLAGS_maskfile.c_str());
    Image mask;
    if (!mask.Read(FLAGS_maskfile)) {
      fprintf(stderr, "Couldn't read mask file\n");
      exit(EXIT_FAILURE);
    }
    if (!mask.ConvertToGrayscale()) {
      fprintf(stderr, "Couldn't convert mask to grayscale\n");
      exit(EXIT_FAILURE);
    }
    Mask::SetAlphaChannelFromMask(mask, &image);
  }
  printf("Warping input image...\n");
  Image projected_image;
  projection.WarpImage(&projected_image);
  image.Clear();
  if (!FLAGS_regionate) {
    printf("Writing warped image to '%s'...\n", FLAGS_outfile.c_str());
    if (!projected_image.Write(FLAGS_outfile)) {
      fprintf(stderr, "Couldn't write image to file\n");
      exit(EXIT_FAILURE);
    }
    printf("Writing KML to '%s'...\n", FLAGS_kmlfile.c_str());
    WriteKmlBox(FLAGS_kmlfile, FLAGS_outfile, FLAGS_ground_overlay_name,
                projection);
  } else {
    printf("Root KML will be written to '%s'...\n",
           FLAGS_kmlfile.c_str());
    printf("Regionating warped image in directory '%s'...\n",
           FLAGS_regionate_dir.c_str());
    Regionator regionator(projected_image, bounding_box);
    regionator.SetMaxTileSideLength(FLAGS_regionate_tile_size);
    regionator.set_filename_prefix(FLAGS_regionate_prefix);
    regionator.set_output_directory(FLAGS_regionate_dir);
    regionator.set_root_kml(FLAGS_kmlfile);
    regionator.set_min_lod_pixels(FLAGS_regionate_min_lod_pixels);
    regionator.set_max_lod_pixels(FLAGS_regionate_max_lod_pixels);
    regionator.set_top_level_draw_order(FLAGS_regionate_top_level_draw_order);
    regionator.set_draw_tile_borders(FLAGS_regionate_draw_tile_borders);
    regionator.Regionate();
  }
  if (!FLAGS_wldfile.empty()) {
    printf("Writing world file to '%s'...\n", FLAGS_wldfile.c_str());
    WriteWorldFile(FLAGS_wldfile, projection);
  }
  printf("All done\n");
  return 0;
}
int Main ( int argc , char * * argv ) { string usage = " " ; usage += argv [ 0 ] ; usage += " " ; google :: SetUsageMessage ( usage ) ; google :: ParseCommandLineFlags ( & argc , & argv , true ) ; if ( FLAGS_imagefile . empty ( ) || FLAGS_fitsfile . empty ( ) ) { fprintf ( stderr , " \n " , usage . c_str ( ) ) ; fprintf ( stderr , " \n " , argv [ 0 ] ) ; exit ( EXIT_FAILURE ) ; } printf ( " \n " , FLAGS_imagefile . c_str ( ) ) ; Image image ; if ( ! image . Read ( FLAGS_imagefile ) ) { fprintf ( stderr , " \n " , FLAGS_imagefile . c_str ( ) ) ; exit ( EXIT_FAILURE ) ; } printf ( " \n " , image . width ( ) , image . height ( ) ) ; printf ( " \n " , FLAGS_fitsfile . c_str ( ) ) ; WcsProjection wcs ( FLAGS_fitsfile , image . width ( ) , image . height ( ) ) ; printf ( " \n " ) ; Color bg_color ( 4 ) ; bg_color . SetAllChannels ( 0 ) ; SkyProjection projection ( image , wcs ) ; projection . SetBackgroundColor ( bg_color ) ; const BoundingBox & bounding_box = projection . bounding_box ( ) ; printf ( " \n " ) ; printf ( " " ) ; if ( bounding_box . is_wrapped ( ) ) { printf ( " \n " ) ; } else { printf ( " \n " ) ; } printf ( " " ) ; if ( bounding_box . crosses_north_pole ( ) ) { printf ( " \n " ) ; } else { printf ( " \n " ) ; } printf ( " " ) ; if ( bounding_box . crosses_south_pole ( ) ) { printf ( " \n " ) ; } else { printf ( " \n " ) ; } double ra_min , ra_max ; double dec_min , dec_max ; bounding_box . GetWrappedRaBounds ( & ra_min , & ra_max ) ; bounding_box . GetDecBounds ( & dec_min , & dec_max ) ; printf ( " \n " , ra_min , ra_max ) ; printf ( " \n " , dec_min , dec_max ) ; if ( FLAGS_input_image_origin_is_upper_left ) { projection . set_input_image_origin ( SkyProjection :: UPPER_LEFT ) ; } else { projection . set_input_image_origin ( SkyProjection :: LOWER_LEFT ) ; } if ( FLAGS_output_width > 0 && FLAGS_output_height > 0 ) { projection . SetProjectedSize ( FLAGS_output_width , FLAGS_output_height ) ; } if ( FLAGS_copy_input_size ) { projection . SetProjectedSize ( image . width ( ) , image . height ( ) ) ; } projection . SetMaxSideLength ( FLAGS_max_side_length ) ; printf ( " \n " , projection . projected_width ( ) , projection . projected_height ( ) ) ; if ( FLAGS_automask ) { printf ( " \n " ) ; printf ( " \n " , FLAGS_automask_red ) ; printf ( " \n " , FLAGS_automask_green ) ; printf ( " \n " , FLAGS_automask_blue ) ; Color mask_out_color ( 4 ) ; mask_out_color . SetChannel ( 0 , FLAGS_automask_red ) ; mask_out_color . SetChannel ( 1 , FLAGS_automask_green ) ; mask_out_color . SetChannel ( 2 , FLAGS_automask_blue ) ; mask_out_color . SetChannel ( 3 , 255 ) ; Image mask ; Mask :: CreateMask ( image , mask_out_color , & mask ) ; Mask :: SetAlphaChannelFromMask ( mask , & image ) ; printf ( " \n " , FLAGS_automaskfile . c_str ( ) ) ; if ( ! mask . Write ( FLAGS_automaskfile + " " ) ) { fprintf ( stderr , " \n " ) ; exit ( EXIT_FAILURE ) ; } } else if ( ! FLAGS_maskfile . empty ( ) ) { printf ( " \n " , FLAGS_maskfile . c_str ( ) ) ; Image mask ; if ( ! mask . Read ( FLAGS_maskfile ) ) { fprintf ( stderr , " \n " ) ; exit ( EXIT_FAILURE ) ; } if ( ! mask . ConvertToGrayscale ( ) ) { fprintf ( stderr , " \n " ) ; exit ( EXIT_FAILURE ) ; } Mask :: SetAlphaChannelFromMask ( mask , & image ) ; } printf ( " \n " ) ; Image projected_image ; projection . WarpImage ( & projected_image ) ; image . Clear ( ) ; if ( ! FLAGS_regionate ) { printf ( " \n " , FLAGS_outfile . c_str ( ) ) ; if ( ! projected_image . Write ( FLAGS_outfile ) ) { fprintf ( stderr , " \n " ) ; exit ( EXIT_FAILURE ) ; } printf ( " \n " , FLAGS_kmlfile . c_str ( ) ) ; WriteKmlBox ( FLAGS_kmlfile , FLAGS_outfile , FLAGS_ground_overlay_name , projection ) ; } else { printf ( " \n " , FLAGS_kmlfile . c_str ( ) ) ; printf ( " \n " , FLAGS_regionate_dir . c_str ( ) ) ; Regionator regionator ( projected_image , bounding_box ) ; regionator . SetMaxTileSideLength ( FLAGS_regionate_tile_size ) ; regionator . set_filename_prefix ( FLAGS_regionate_prefix ) ; regionator . set_output_directory ( FLAGS_regionate_dir ) ; regionator . set_root_kml ( FLAGS_kmlfile ) ; regionator . set_min_lod_pixels ( FLAGS_regionate_min_lod_pixels ) ; regionator . set_max_lod_pixels ( FLAGS_regionate_max_lod_pixels ) ; regionator . set_top_level_draw_order ( FLAGS_regionate_top_level_draw_order ) ; regionator . set_draw_tile_borders ( FLAGS_regionate_draw_tile_borders ) ; regionator . Regionate ( ) ; } if ( ! FLAGS_wldfile . empty ( ) ) { printf ( " \n " , FLAGS_wldfile . c_str ( ) ) ; WriteWorldFile ( FLAGS_wldfile , projection ) ; } printf ( " \n " ) ; return 0 ; }
The real main is defined here inside of the namespace to reduce the amount
of typing.
The real main is defined here inside of the namespace to reduce the amount of typing .
// Read the image file into memory. // Read the WCS from the input FITS file. // The background color for the image is transparent so that we don't // black out imagery below the overlay. // Create a projection object.  This copies a pointer to image so modifying // image after this point will have changes to the copy SkyProjection keeps. // This also and computes the bounding box of the image. // Report information on the image's bounding box. // FITS files have their origin at (1, 1) in the lower left corner, while // most raster formats have (0, 0) in the upper left corner.  Depending on // how the FITS image was converted to PNG, you may have to correct for // this.  If the output image is flipped from how it should be, try varying // this option. // The algorithm for automatically determining the output dimensions has // difficulty with extreme warping and rotation angles near 0, 90, 180, and // 270.  In those cases setting the projected dimensions by hand is useful. // This ensures that the output image is not unreasonably huge. // Set up the masking. // Determine the color to search for when building the mask. // NB: We are modifying the original image, but projection keeps a pointer // to this image so it sees the changes too. // Use masking from file. // The image mask will be RGBA after being read. // NB: We are modifying the original image, but projection keeps a pointer // to this image so it sees the changes too. // Warp the image.  We can only warp once because the internal copy is // automatically cleaned up afterwards. // We no longer need the original image. // Write to file. // Write a single warped file and accompanying KML. // Regionate output warped image into a series of tiles and KML documents // that loads more effciently than a single image. // Write world file.