<commit_before>// __BEGIN_LICENSE__
// Copyright (C) 2006-2010 United States Government as represented by
// the Administrator of the National Aeronautics and Space Administration.
// All Rights Reserved.
// __END_LICENSE__


/// \file image2plate.cc
///
/// This utility adds a georefernced image to a plate file.
///
/// By default, this tool will add an image to the mosaic, along with
/// mipmapped tiles for that image, to its own layer.  Layers are
/// indexed by transaction IDs.
///

#include <vw/Image.h>
#include <vw/FileIO.h>
#include <vw/Cartography.h>
#include <vw/Plate/PlateFile.h>
#include <vw/Plate/ToastPlateManager.h>
#include <vw/Plate/PlateCarreePlateManager.h>

using namespace vw;
using namespace vw::platefile;
using namespace vw::mosaic;
using namespace vw::cartography;

#include <boost/program_options.hpp>
namespace po = boost::program_options;

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/convenience.hpp>
namespace fs = boost::filesystem;

// Global variables
bool g_debug;

// Erases a file suffix if one exists and returns the base string
static std::string prefix_from_filename(std::string const& filename) {
  std::string result = filename;
  int index = result.rfind(".");
  if (index != -1) 
    result.erase(index, result.size());
  return result;
}

// --------------------------------------------------------------------------
//                                DO_MOSAIC
// --------------------------------------------------------------------------

template <class ViewT>
void do_mosaic(boost::shared_ptr<PlateFile> platefile, 
               ImageViewBase<ViewT> const& view,
               std::string filename, int transaction_id_override, 
               GeoReference const& georef, std::string output_mode, 
               bool tweak_settings_for_terrain) {

  std::ostringstream status_str;
  status_str << "\t    " << filename << " : ";

  if (output_mode == "toast") {

    boost::shared_ptr<ToastPlateManager<typename ViewT::pixel_type> > pm( 
      new ToastPlateManager<typename ViewT::pixel_type> (platefile) );

    pm->insert(view.impl(), filename, transaction_id_override, georef, 
               tweak_settings_for_terrain, g_debug,
               TerminalProgressCallback( "plate.tools.image2plate", "\t    Processing") );

  }  else if (output_mode == "equi")  {

    boost::shared_ptr<PlateCarreePlateManager<typename ViewT::pixel_type> > pm(
      new PlateCarreePlateManager<typename ViewT::pixel_type> (platefile) );

    pm->insert(view.impl(), filename, transaction_id_override, georef, 
               tweak_settings_for_terrain, g_debug,
               TerminalProgressCallback( "plate.tools.image2plate", "\t    Processing") );
    
  }

}

// --------------------------------------------------------------------------
//                                    MAIN
// --------------------------------------------------------------------------

int main( int argc, char *argv[] ) {
  std::string url;
  std::string tile_filetype;
  std::string output_mode;
  int tile_size;
  int transaction_id_override = -1;
  float jpeg_quality;
  int png_compression;
  unsigned cache_size;
  double nodata_value = 0;
  std::vector<std::string> image_files;
  double user_spherical_datum;

  po::options_description general_options("Turns georeferenced image(s) into a TOAST quadtree.\n\nGeneral Options");
  general_options.add_options()
    ("output-name,o", po::value<std::string>(&url), "Specify the URL of the platefile.")
    ("transaction-id,t", po::value<int>(&transaction_id_override), "Specify the transaction_id to use for this transaction. If you don't specify one, one will be automatically assigned.\n")
    ("file-type", po::value<std::string>(&tile_filetype)->default_value("png"), "Output file type")
    ("nodata-value", po::value<double>(&nodata_value), "Explicitly set the value to treat as na data (i.e. transparent) in the input file.")
    ("mode,m", po::value<std::string>(&output_mode)->default_value("toast"), "Output mode [toast, equi]")
    ("tile-size", po::value<int>(&tile_size)->default_value(256), "Tile size, in pixels")
    ("jpeg-quality", po::value<float>(&jpeg_quality)->default_value(0.95), "JPEG quality factor (0.0 to 1.0)")
    ("png-compression", po::value<int>(&png_compression)->default_value(3), "PNG compression level (0 to 9)")
    ("cache", po::value<unsigned>(&cache_size)->default_value(512), "Source data cache size, in megabytes")
    ("terrain", "Tweak a few settings that are best for terrain platefiles. Turns on nearest neighbor sampling in mipmapping and zero out semi-transparent pixels.")
    ("force-lunar-datum", "Use the lunar spherical datum for the input images' geographic coordinate systems, even if they are not encoded to do so.")
    ("force-mars-datum", "Use the Mars spherical datum for the input images' geographic coordinate systems, even if they are not encoded to do so.")
    ("force-spherical-datum", po::value<double>(&user_spherical_datum), "Choose an arbitrary input spherical datum to use for input images', overriding the existing datum.")
    ("force-float", "Force the platefile to use a channel type of float.")
    ("debug", "Display helpful debugging messages.")
    ("help", "Display this help message");

  po::options_description hidden_options("");
  hidden_options.add_options()
    ("input-file", po::value<std::vector<std::string> >(&image_files));

  po::options_description options("Allowed Options");
  options.add(general_options).add(hidden_options);

  po::positional_options_description p;
  p.add("input-file", -1);

  std::ostringstream usage;
  usage << "Usage: " << argv[0] << " [options] <filename>..." <<std::endl << std::endl;
  usage << general_options << std::endl;

  po::variables_map vm;
  try { 
    po::store( po::command_line_parser( argc, argv ).options(options).positional(p).run(), vm );
    po::notify( vm );
  } catch (po::error &e) {
    std::cout << "An error occured while parsing command line arguments.\n\n";
    std::cout << usage.str();
    return 0;    
  }

  if( vm.count("help") ) {
    std::cout << usage.str();
    return 0;
  }

  if( vm.count("input-file") < 1 ) {
    std::cerr << "Error: must specify at least one input file!" << std::endl << std::endl;
    std::cout << usage.str();
    return 1;
  }

  //------------------------- SET DEFAULT OPTIONS -----------------------------

  if( url == "" )
    url = prefix_from_filename(image_files[0]) + "_" + output_mode + ".plate";

  if( tile_size <= 0 || tile_size != pow(2,int(log(tile_size)/log(2))) ) {
    std::cerr << "Error: The tile size must be a power of two!  (You specified: " << tile_size << ")" << std::endl << std::endl;
    std::cout << usage.str();
    return 1;
  }
<commit_msg>Removed vestigial check for power of two tile size.  This is no longer necessary.<commit_after>// __BEGIN_LICENSE__
// Copyright (C) 2006-2010 United States Government as represented by
// the Administrator of the National Aeronautics and Space Administration.
// All Rights Reserved.
// __END_LICENSE__


/// \file image2plate.cc
///
/// This utility adds a georefernced image to a plate file.
///
/// By default, this tool will add an image to the mosaic, along with
/// mipmapped tiles for that image, to its own layer.  Layers are
/// indexed by transaction IDs.
///

#include <vw/Image.h>
#include <vw/FileIO.h>
#include <vw/Cartography.h>
#include <vw/Plate/PlateFile.h>
#include <vw/Plate/ToastPlateManager.h>
#include <vw/Plate/PlateCarreePlateManager.h>

using namespace vw;
using namespace vw::platefile;
using namespace vw::mosaic;
using namespace vw::cartography;

#include <boost/program_options.hpp>
namespace po = boost::program_options;

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/convenience.hpp>
namespace fs = boost::filesystem;

// Global variables
bool g_debug;

// Erases a file suffix if one exists and returns the base string
static std::string prefix_from_filename(std::string const& filename) {
  std::string result = filename;
  int index = result.rfind(".");
  if (index != -1) 
    result.erase(index, result.size());
  return result;
}

// --------------------------------------------------------------------------
//                                DO_MOSAIC
// --------------------------------------------------------------------------

template <class ViewT>
void do_mosaic(boost::shared_ptr<PlateFile> platefile, 
               ImageViewBase<ViewT> const& view,
               std::string filename, int transaction_id_override, 
               GeoReference const& georef, std::string output_mode, 
               bool tweak_settings_for_terrain) {

  std::ostringstream status_str;
  status_str << "\t    " << filename << " : ";

  if (output_mode == "toast") {

    boost::shared_ptr<ToastPlateManager<typename ViewT::pixel_type> > pm( 
      new ToastPlateManager<typename ViewT::pixel_type> (platefile) );

    pm->insert(view.impl(), filename, transaction_id_override, georef, 
               tweak_settings_for_terrain, g_debug,
               TerminalProgressCallback( "plate.tools.image2plate", "\t    Processing") );

  }  else if (output_mode == "equi")  {

    boost::shared_ptr<PlateCarreePlateManager<typename ViewT::pixel_type> > pm(
      new PlateCarreePlateManager<typename ViewT::pixel_type> (platefile) );

    pm->insert(view.impl(), filename, transaction_id_override, georef, 
               tweak_settings_for_terrain, g_debug,
               TerminalProgressCallback( "plate.tools.image2plate", "\t    Processing") );
    
  }

}

// --------------------------------------------------------------------------
//                                    MAIN
// --------------------------------------------------------------------------

int main( int argc, char *argv[] ) {
  std::string url;
  std::string tile_filetype;
  std::string output_mode;
  int tile_size;
  int transaction_id_override = -1;
  float jpeg_quality;
  int png_compression;
  unsigned cache_size;
  double nodata_value = 0;
  std::vector<std::string> image_files;
  double user_spherical_datum;

  po::options_description general_options("Turns georeferenced image(s) into a TOAST quadtree.\n\nGeneral Options");
  general_options.add_options()
    ("output-name,o", po::value<std::string>(&url), "Specify the URL of the platefile.")
    ("transaction-id,t", po::value<int>(&transaction_id_override), "Specify the transaction_id to use for this transaction. If you don't specify one, one will be automatically assigned.\n")
    ("file-type", po::value<std::string>(&tile_filetype)->default_value("png"), "Output file type")
    ("nodata-value", po::value<double>(&nodata_value), "Explicitly set the value to treat as na data (i.e. transparent) in the input file.")
    ("mode,m", po::value<std::string>(&output_mode)->default_value("toast"), "Output mode [toast, equi]")
    ("tile-size", po::value<int>(&tile_size)->default_value(256), "Tile size, in pixels")
    ("jpeg-quality", po::value<float>(&jpeg_quality)->default_value(0.95), "JPEG quality factor (0.0 to 1.0)")
    ("png-compression", po::value<int>(&png_compression)->default_value(3), "PNG compression level (0 to 9)")
    ("cache", po::value<unsigned>(&cache_size)->default_value(512), "Source data cache size, in megabytes")
    ("terrain", "Tweak a few settings that are best for terrain platefiles. Turns on nearest neighbor sampling in mipmapping and zero out semi-transparent pixels.")
    ("force-lunar-datum", "Use the lunar spherical datum for the input images' geographic coordinate systems, even if they are not encoded to do so.")
    ("force-mars-datum", "Use the Mars spherical datum for the input images' geographic coordinate systems, even if they are not encoded to do so.")
    ("force-spherical-datum", po::value<double>(&user_spherical_datum), "Choose an arbitrary input spherical datum to use for input images', overriding the existing datum.")
    ("force-float", "Force the platefile to use a channel type of float.")
    ("debug", "Display helpful debugging messages.")
    ("help", "Display this help message");

  po::options_description hidden_options("");
  hidden_options.add_options()
    ("input-file", po::value<std::vector<std::string> >(&image_files));

  po::options_description options("Allowed Options");
  options.add(general_options).add(hidden_options);

  po::positional_options_description p;
  p.add("input-file", -1);

  std::ostringstream usage;
  usage << "Usage: " << argv[0] << " [options] <filename>..." <<std::endl << std::endl;
  usage << general_options << std::endl;

  po::variables_map vm;
  try { 
    po::store( po::command_line_parser( argc, argv ).options(options).positional(p).run(), vm );
    po::notify( vm );
  } catch (po::error &e) {
    std::cout << "An error occured while parsing command line arguments.\n\n";
    std::cout << usage.str();
    return 0;    
  }

  if( vm.count("help") ) {
    std::cout << usage.str();
    return 0;
  }

  if( vm.count("input-file") < 1 ) {
    std::cerr << "Error: must specify at least one input file!" << std::endl << std::endl;
    std::cout << usage.str();
    return 1;
  }

  //------------------------- SET DEFAULT OPTIONS -----------------------------

  if( url == "" )
    url = prefix_from_filename(image_files[0]) + "_" + output_mode + ".plate";

{'fraction_non_alphanumeric': 0.18664165103189492, 'fraction_numerical': 0.005853658536585366, 'mean_word_length': 3.070250458155162, 'pattern_counts': {'":': 0, '<': 150, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7458433', 'n_tokens_mistral': 4098, 'n_tokens_neox': 3910, 'n_words': 1076}