<commit_before>#include <ossim/support_data/TiffHandlerState.h>
#include <ossim/base/ossimStreamFactoryRegistry.h>
#include <ossim/support_data/ossimGeoTiff.h>
#include <ossim/base/ossimTieGptSet.h>
#include <ossim/base/ossimTrace.h>
#include <ossim/projection/ossimUtmpt.h>
#include <ossim/projection/ossimMapProjection.h>
#include <ossim/projection/ossimBilinearProjection.h>
#include <ossim/projection/ossimEpsgProjectionFactory.h>
#include <ossim/projection/ossimProjectionFactoryRegistry.h>
#include <ossim/base/ossimGeoTiffCoordTransformsLut.h>
#include <ossim/base/ossimGeoTiffDatumLut.h>
#include <ossim/base/ossimStreamFactoryRegistry.h>
#include <ossim/support_data/TiffStreamAdaptor.h>
#include <ossim/support_data/ossimTiffInfo.h>
#include <xtiffio.h>
#include <geo_normalize.h>
#include <geotiff.h>
#include <geo_tiffp.h>
#include <cpl_serv.h>
#include <geovalues.h>
#include <cstddef> //nullptr
#include <sstream>
#include <ossim/projection/ossimEquDistCylProjection.h>

static ossimTrace traceDebug("ossim::TiffHandlerState");

static const ossimGeoTiffCoordTransformsLut COORD_TRANS_LUT;
static const ossimGeoTiffDatumLut DATUM_LUT;

const ossimString ossim::TiffHandlerState::m_typeName = "ossim::TiffHandlerState";

ossim::TiffHandlerState::TiffHandlerState()
{

}
      
ossim::TiffHandlerState::~TiffHandlerState()
{

}

const ossimString& ossim::TiffHandlerState::getTypeName()const
{
   return m_typeName;
}

const ossimString& ossim::TiffHandlerState::getStaticTypeName()
{
   return m_typeName;
}

void ossim::TiffHandlerState::addValue(const ossimString& key, 
                                       const ossimString& value)
{
  m_tags.add(key.c_str(), value.c_str());
}

bool ossim::TiffHandlerState::getValue(ossimString& value,
                                       const ossim_uint32 directory, 
                                        const ossimString& key)const
{
  return getValue(value, 
                  "tiff.image"+ossimString::toString(directory)+"."+key);
}

bool ossim::TiffHandlerState::getValue(ossimString& value,
                                       const ossimString& key)const
{
  bool result = false;

  const char* v = m_tags.find(key.c_str());
  if(v)
  {
    result = true;
    value  = v;
  }

  return result;
}

bool ossim::TiffHandlerState::exists(ossim_uint32 directory, const ossimString& key)const
{
  return exists("tiff.image"+
                ossimString::toString(directory)+
                "."+key);

}
bool ossim::TiffHandlerState::exists(const ossimString& key)const
{
  return (m_tags.find(key)!= 0);

}

bool ossim::TiffHandlerState::checkBool(ossim_uint32 directory, const ossimString& key)const
{
  return checkBool("tiff.image"+ossimString::toString(directory)+"."+key);
}

bool ossim::TiffHandlerState::checkBool(const ossimString& key)const
{
  bool result = false;
  const char* value = m_tags.find(key);
  if(value)
  {
    result = ossimString(value).toBool();
  }
  return result;
}

bool ossim::TiffHandlerState::loadDefaults(const ossimFilename& file, 
                                           ossim_uint32 entry)
{
  bool result = ImageHandlerState::loadDefaults(file, entry);
  
  if(result)
  {
    std::shared_ptr<std::istream> tiffStream = ossim::StreamFactoryRegistry::instance()->createIstream(file);
    if(tiffStream)
    {
       std::shared_ptr<ossim::TiffIStreamAdaptor> streamAdaptor = std::make_shared<ossim::TiffIStreamAdaptor>(tiffStream,
                                                                   file.c_str());
       TIFF* tiffPtr = XTIFFClientOpen(file.c_str(), "rm", 
                                    (thandle_t)streamAdaptor.get(),
                                    ossim::TiffIStreamAdaptor::tiffRead, 
                                    ossim::TiffIStreamAdaptor::tiffWrite, 
                                    ossim::TiffIStreamAdaptor::tiffSeek, 
                                    ossim::TiffIStreamAdaptor::tiffClose, 
                                    ossim::TiffIStreamAdaptor::tiffSize,
                                    ossim::TiffIStreamAdaptor::tiffMap, 
                                    ossim::TiffIStreamAdaptor::tiffUnmap);

       if(tiffPtr)
       {
         loadDefaults(tiffPtr);
         result = true;
         XTIFFClose(tiffPtr);
         tiffPtr = 0;
       }
    }    
  }

  return result;
}

void ossim::TiffHandlerState::loadDefaults(TIFF* tiffPtr)
{
  ossim_int64   currentDirectory = TIFFCurrentDirectory(tiffPtr);
  TIFFSetDirectory(tiffPtr, 0);
  ossim_int32   numberOfDirectories = TIFFNumberOfDirectories(tiffPtr);

  addValue("number_of_directories", ossimString::toString(numberOfDirectories));

  ossim_int32 idx=0;
  for(;idx < numberOfDirectories;++idx)
  {
    if (!TIFFSetDirectory(tiffPtr, idx))
    {
       break;
    }
    else
    {
      loadDirectory(tiffPtr, idx);
    }
  }
  TIFFSetDirectory(tiffPtr, currentDirectory);
}

void ossim::TiffHandlerState::loadDefaults(std::shared_ptr<ossim::istream> &str,
                                         const std::string &connectionString)
{
  setConnectionString(connectionString);
  ossim_int64 offset = str->tellg();
  ossimTiffInfo info;
  std::ostringstream out;
  str->seekg(0);
  if(info.open(str, connectionString))
  {
    std::vector<ossimString> prefixValues;
    ossim_int32 nValues=0;
    info.print(out);
    m_tags.parseString(out.str());
    m_tags.getSortedList(prefixValues, "tiff.image");
    nValues = prefixValues.size();
    addValue("tiff.number_of_directories", ossimString::toString(nValues));
    ossim_uint32 idx = 0;
    ossim_int64 h = getImageLength(0);
    ossim_int64 w = getImageWidth(0);
    ossim_int64 tw = getTileWidth(0);
    ossim_int64 th = getTileLength(0);
    bool isTiled = (th&&tw&& ((tw < w) && (th < h)));
    for (auto key : prefixValues)
    {
      if(isTiled)
      {
        addValue(key + ".is_tiled", "true");
      }
      else
      {
        addValue(key+".is_tiled", "false");
      }
    }
  }
  // std::cout << "______________________________________________\n"
  //           << m_tags
  //           << "_______________________________________________\n";
  str->clear();
  str->seekg(offset);
}

void ossim::TiffHandlerState::loadCurrentDirectory(TIFF* tiffPtr)
{
  if(tiffPtr) 
  {
    loadDirectory(tiffPtr, TIFFCurrentDirectory(tiffPtr));
  }
}
<commit_msg>Fixed TiffHandlerState to allow for single-tiled small images.<commit_after>#include <ossim/support_data/TiffHandlerState.h>
#include <ossim/base/ossimStreamFactoryRegistry.h>
#include <ossim/support_data/ossimGeoTiff.h>
#include <ossim/base/ossimTieGptSet.h>
#include <ossim/base/ossimTrace.h>
#include <ossim/projection/ossimUtmpt.h>
#include <ossim/projection/ossimMapProjection.h>
#include <ossim/projection/ossimBilinearProjection.h>
#include <ossim/projection/ossimEpsgProjectionFactory.h>
#include <ossim/projection/ossimProjectionFactoryRegistry.h>
#include <ossim/base/ossimGeoTiffCoordTransformsLut.h>
#include <ossim/base/ossimGeoTiffDatumLut.h>
#include <ossim/base/ossimStreamFactoryRegistry.h>
#include <ossim/support_data/TiffStreamAdaptor.h>
#include <ossim/support_data/ossimTiffInfo.h>
#include <xtiffio.h>
#include <geo_normalize.h>
#include <geotiff.h>
#include <geo_tiffp.h>
#include <cpl_serv.h>
#include <geovalues.h>
#include <cstddef> //nullptr
#include <sstream>
#include <ossim/projection/ossimEquDistCylProjection.h>

static ossimTrace traceDebug("ossim::TiffHandlerState");

static const ossimGeoTiffCoordTransformsLut COORD_TRANS_LUT;
static const ossimGeoTiffDatumLut DATUM_LUT;

const ossimString ossim::TiffHandlerState::m_typeName = "ossim::TiffHandlerState";

ossim::TiffHandlerState::TiffHandlerState()
{

}
      
ossim::TiffHandlerState::~TiffHandlerState()
{

}

const ossimString& ossim::TiffHandlerState::getTypeName()const
{
   return m_typeName;
}

const ossimString& ossim::TiffHandlerState::getStaticTypeName()
{
   return m_typeName;
}

void ossim::TiffHandlerState::addValue(const ossimString& key, 
                                       const ossimString& value)
{
  m_tags.add(key.c_str(), value.c_str());
}

bool ossim::TiffHandlerState::getValue(ossimString& value,
                                       const ossim_uint32 directory, 
                                        const ossimString& key)const
{
  return getValue(value, 
                  "tiff.image"+ossimString::toString(directory)+"."+key);
}

bool ossim::TiffHandlerState::getValue(ossimString& value,
                                       const ossimString& key)const
{
  bool result = false;

  const char* v = m_tags.find(key.c_str());
  if(v)
  {
    result = true;
    value  = v;
  }

  return result;
}

bool ossim::TiffHandlerState::exists(ossim_uint32 directory, const ossimString& key)const
{
  return exists("tiff.image"+
                ossimString::toString(directory)+
                "."+key);

}
bool ossim::TiffHandlerState::exists(const ossimString& key)const
{
  return (m_tags.find(key)!= 0);

}

bool ossim::TiffHandlerState::checkBool(ossim_uint32 directory, const ossimString& key)const
{
  return checkBool("tiff.image"+ossimString::toString(directory)+"."+key);
}

bool ossim::TiffHandlerState::checkBool(const ossimString& key)const
{
  bool result = false;
  const char* value = m_tags.find(key);
  if(value)
  {
    result = ossimString(value).toBool();
  }
  return result;
}

bool ossim::TiffHandlerState::loadDefaults(const ossimFilename& file, 
                                           ossim_uint32 entry)
{
  bool result = ImageHandlerState::loadDefaults(file, entry);
  
  if(result)
  {
    std::shared_ptr<std::istream> tiffStream = ossim::StreamFactoryRegistry::instance()->createIstream(file);
    if(tiffStream)
    {
       std::shared_ptr<ossim::TiffIStreamAdaptor> streamAdaptor = std::make_shared<ossim::TiffIStreamAdaptor>(tiffStream,
                                                                   file.c_str());
       TIFF* tiffPtr = XTIFFClientOpen(file.c_str(), "rm", 
                                    (thandle_t)streamAdaptor.get(),
                                    ossim::TiffIStreamAdaptor::tiffRead, 
                                    ossim::TiffIStreamAdaptor::tiffWrite, 
                                    ossim::TiffIStreamAdaptor::tiffSeek, 
                                    ossim::TiffIStreamAdaptor::tiffClose, 
                                    ossim::TiffIStreamAdaptor::tiffSize,
                                    ossim::TiffIStreamAdaptor::tiffMap, 
                                    ossim::TiffIStreamAdaptor::tiffUnmap);

       if(tiffPtr)
       {
         loadDefaults(tiffPtr);
         result = true;
         XTIFFClose(tiffPtr);
         tiffPtr = 0;
       }
    }    
  }

  return result;
}

void ossim::TiffHandlerState::loadDefaults(TIFF* tiffPtr)
{
  ossim_int64   currentDirectory = TIFFCurrentDirectory(tiffPtr);
  TIFFSetDirectory(tiffPtr, 0);
  ossim_int32   numberOfDirectories = TIFFNumberOfDirectories(tiffPtr);

  addValue("number_of_directories", ossimString::toString(numberOfDirectories));

  ossim_int32 idx=0;
  for(;idx < numberOfDirectories;++idx)
  {
    if (!TIFFSetDirectory(tiffPtr, idx))
    {
       break;
    }
    else
    {
      loadDirectory(tiffPtr, idx);
    }
  }
  TIFFSetDirectory(tiffPtr, currentDirectory);
}

void ossim::TiffHandlerState::loadDefaults(std::shared_ptr<ossim::istream> &str,
                                         const std::string &connectionString)
{
  setConnectionString(connectionString);
  ossim_int64 offset = str->tellg();
  ossimTiffInfo info;
  std::ostringstream out;
  str->seekg(0);
  if(info.open(str, connectionString))
  {
    std::vector<ossimString> prefixValues;
    ossim_int32 nValues=0;
    info.print(out);
    m_tags.parseString(out.str());
    m_tags.getSortedList(prefixValues, "tiff.image");
    nValues = prefixValues.size();
    addValue("tiff.number_of_directories", ossimString::toString(nValues));
    ossim_uint32 idx = 0;
    //ossim_int64 h = getImageLength(0);
    //ossim_int64 w = getImageWidth(0);
    ossim_int64 tw = getTileWidth(0);
    ossim_int64 th = getTileLength(0);
    //bool isTiled = (th&&tw&& ((tw < w) && (th < h)));
    bool isTiled = (tw>0 && th>0); // Can be a single tile (no strip info)
    for (auto key : prefixValues)
    {
      if(isTiled)
      {
        addValue(key + ".is_tiled", "true");
      }
      else
      {
        addValue(key+".is_tiled", "false");
      }
    }
  }
  // std::cout << "______________________________________________\n"
  //           << m_tags
  //           << "_______________________________________________\n";
  str->clear();
  str->seekg(offset);
}

void ossim::TiffHandlerState::loadCurrentDirectory(TIFF* tiffPtr)
{
  if(tiffPtr) 
  {
    loadDirectory(tiffPtr, TIFFCurrentDirectory(tiffPtr));
  }
}

{'fraction_non_alphanumeric': 0.12039925140361822, 'fraction_numerical': 0.006238303181534623, 'mean_word_length': 3.052132701421801, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6799675', 'n_tokens_mistral': 4468, 'n_tokens_neox': 4233, 'n_words': 615}