bool BackgroundSuppressionShell::preprocess()
{
   if(mpRaster == NULL)
   {
      return false;
   }
   RasterDataDescriptor *pDesc = static_cast<RasterDataDescriptor*>(mpRaster->getDataDescriptor());
   if(mpTemporaryBuffer.get() == NULL)
   {
      mpTemporaryBuffer.reset(new char[pDesc->getRowCount() * pDesc->getColumnCount() * pDesc->getBytesPerElement()]);
      if(mpTemporaryBuffer.get() == NULL)
      {
         return false;
      }
   }
   DataAccessor frameAccessor = getCurrentFrameAccessor();
   size_t rowSize = pDesc->getColumnCount() * pDesc->getBytesPerElement();
   for(unsigned int row = 0; row < pDesc->getRowCount(); row++)
   {
      if(!frameAccessor.isValid())
      {
         return false;
      }
      memcpy(mpTemporaryBuffer.get() + row * rowSize, frameAccessor->getRow(), rowSize);
      frameAccessor->nextColumn();
   }
   return true;
}
Esempio n. 2
0
const double *SignatureLibraryImp::getOrdinateData(unsigned int index) const
{
   if (mNeedToResample || mResampledData.empty())
   {
      const_cast<SignatureLibraryImp*>(this)->resample(mAbscissa);
   }

   if (index < mSignatures.size() && (!mAbscissa.empty() || mpOdre.get() != NULL))
   {
      if (mAbscissa.empty())
      {
         const RasterDataDescriptor* pDesc =
            dynamic_cast<const RasterDataDescriptor*>(mpOdre.get()->getDataDescriptor());
         if (pDesc != NULL)
         {
            FactoryResource<DataRequest> pRequest;
            pRequest->setInterleaveFormat(BIP);
            pRequest->setBands(pDesc->getActiveBand(0),
               pDesc->getActiveBand(pDesc->getBandCount() - 1), pDesc->getBandCount());
            pRequest->setRows(pDesc->getActiveRow(index), pDesc->getActiveRow(index), 1);
            DataAccessor da = mpOdre->getDataAccessor(pRequest.release());
            if (da.isValid())
            {
               static vector<double> sOriginalOrdinateData;
               sOriginalOrdinateData.resize(mOriginalAbscissa.size());
               switchOnEncoding(pDesc->getDataType(), getOriginalAsDouble, da->getRow(), mOriginalAbscissa.size(),
                  sOriginalOrdinateData);
               return &sOriginalOrdinateData[0];
            }
         }

         return NULL;
      }

      return &mResampledData[index * getAbscissa().size()];
   }

   return NULL;
}
Esempio n. 3
0
bool ResultsExporter::runAllTests(Progress *pProgress, ostream& failure)
{
    bool success = true;

    mpProgress = pProgress;
    mpResults = RasterUtilities::createRasterElement("ResultsExporterTestMatrix", 2, 2, FLT4BYTES, true, NULL);
    if (mpResults == NULL)
    {
        failure << "Unable to create a Raster Element.";
        success = false;
    }
    else
    {
        ModelResource<RasterElement> pResultsResource(mpResults);
        RasterDataDescriptor* pDescriptor = dynamic_cast<RasterDataDescriptor*>(mpResults->getDataDescriptor());
        if (pDescriptor == NULL)
        {
            failure << "Unable to get the Raster Data Descriptor.";
            success = false;
        }
        else
        {
            // Bad values
            std::vector<int> badValues;
            badValues.push_back(2);
            badValues.push_back(4);
            pDescriptor->setBadValues(badValues);

            // Create the element
            FactoryResource<DataRequest> pRequest;
            pRequest->setWritable(true);
            DataAccessor da = mpResults->getDataAccessor(pRequest.release());
            if (da.isValid() == false)
            {
                failure << "Data Accessor is not valid.";
                success = false;
            }
            else
            {
                float* pData = reinterpret_cast<float*>(da->getRow());
                if (pData == NULL)
                {
                    failure << "Data is not accessible.";
                    success = false;
                }
                else
                {
                    pData[0] = 1.0;
                    pData[1] = 2.0;
                    pData[2] = 3.0;
                    pData[3] = 4.0;

                    mFirstThreshold = 1.5;
                    mSecondThreshold = 4.5;
                    mPassArea = MIDDLE;
                    mGeocoordType = GEOCOORD_LATLON;
                    mbMetadata = false;
                    mbAppendFile = false;

                    const Filename* pTempPath = ConfigurationSettings::getSettingTempPath();
                    if (pTempPath == NULL)
                    {
                        failure << "Unable to get the temporary path from ConfigurationSettings.";
                        success = false;
                    }
                    else
                    {
                        const string filename = pTempPath->getFullPathAndName() + "/ResultsExporterTest.rls";
                        mpFileDescriptor = dynamic_cast<RasterFileDescriptor*>
                                           (RasterUtilities::generateFileDescriptorForExport(pDescriptor, filename));
                        if (mpFileDescriptor == NULL)
                        {
                            failure << "Unable to generate a Raster File Descriptor for export.";
                            success = false;
                        }
                        else
                        {
                            FactoryResource<RasterFileDescriptor> pFileDescriptor(mpFileDescriptor);
                            stringstream stream;
                            writeOutput(stream);

                            if (stream.str() != "ResultsExporterTestMatrix    Pixel: (1, 2)    3.000000\n\n")
                            {
                                failure << "Invalid output stream.";
                                success = false;
                            }
                        }
                    }
                }
            }
        }
    }

    mpResults = NULL;
    mpProgress = NULL;
    mpFileDescriptor = NULL;
    return success;
}
Esempio n. 4
0
bool GeoTIFFExporter::writeCube(TIFF* pOut)
{
    if (pOut == NULL)
    {
        return false;
    }

    VERIFY(mpRaster != NULL);
    VERIFY(mpFileDescriptor != NULL);

    const RasterDataDescriptor* pDescriptor = dynamic_cast<const RasterDataDescriptor*>(mpRaster->getDataDescriptor());
    if (pDescriptor == NULL)
    {
        return false;
    }

    int size = 0;
    int row = 0;
    unsigned char* pTempPtr = NULL;
    unsigned char* pBuffer = NULL;
    unsigned char* pDataCubePtr = NULL;
    unsigned short numRows = pDescriptor->getRowCount();
    unsigned short numCols = pDescriptor->getColumnCount();
    unsigned short numBands = pDescriptor->getBandCount();
    unsigned short scols = mpFileDescriptor->getColumnCount();
    unsigned short srows = mpFileDescriptor->getRowCount();
    unsigned short sbands = mpFileDescriptor->getBandCount();

    FactoryResource<DataRequest> pRequest;
    pRequest->setInterleaveFormat(BIP);
    DataAccessor accessor = mpRaster->getDataAccessor(pRequest.release());
    if (!accessor.isValid())
    {
        mMessage = "Could not get a valid BIP accessor for this dataset.";
        if (mpProgress != NULL)
        {
            mpProgress->updateProgress(mMessage, 0, ERRORS);
        }

        return false;
    }

    InterleaveFormatType eInterleave = pDescriptor->getInterleaveFormat();
    if (eInterleave != BIP)
    {
        mMessage = "Data will be saved in BIP format.";
        if (mpProgress != NULL)
        {
            mpProgress->updateProgress(mMessage, 0, WARNING);
        }
    }

    unsigned int bytesPerElement(pDescriptor->getBytesPerElement());
    size = scols * sbands * bytesPerElement;

    TIFFSetField(pOut, TIFFTAG_IMAGEWIDTH, scols);
    TIFFSetField(pOut, TIFFTAG_IMAGELENGTH, srows);
    TIFFSetField(pOut, TIFFTAG_SAMPLESPERPIXEL, sbands);

    //for this tag, must multiply by # of bytes per data type
    TIFFSetField(pOut, TIFFTAG_BITSPERSAMPLE, static_cast<unsigned short>(bytesPerElement * 8));
    TIFFSetField(pOut, TIFFTAG_SAMPLEFORMAT, static_cast<unsigned short>(
                     getTiffSampleFormat(pDescriptor->getDataType())));

    bool packBits = OptionsTiffExporter::getSettingPackBitsCompression();
    if (mpOptionWidget.get() != NULL)
    {
        mpOptionWidget->applyChanges();
        packBits = mpOptionWidget->getPackBitsCompression();
    }
    ttag_t compOpt = (packBits ? COMPRESSION_PACKBITS : COMPRESSION_NONE);

    TIFFSetField(pOut, TIFFTAG_COMPRESSION, compOpt);
    TIFFSetField(pOut, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
    TIFFSetField(pOut, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
    TIFFSetField(pOut, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);      //????
    TIFFSetField(pOut, TIFFTAG_ROWSPERSTRIP, mRowsPerStrip);

    //ready to test write
    mMessage = "Writing out GeoTIFF file...";
    if (mpProgress)
    {
        mpProgress->updateProgress( mMessage, 0, NORMAL);
    }

    if ((numRows == srows) && (numCols == scols) && (numBands == sbands))   // full cube write from memory
    {
        for (row = 0; row < srows; row++)
        {
            if (mAbortFlag)
            {
                mMessage = "GeoTIFF export aborted!";
                if (mpProgress != NULL)
                {
                    mpProgress->updateProgress(mMessage, 0, ERRORS);
                }

                return false;
            }

            VERIFY(accessor.isValid());
            pBuffer = reinterpret_cast<unsigned char*>(accessor->getRow());
            if (pBuffer != NULL)
            {
                if (TIFFWriteScanline(pOut, pBuffer, row, size) < 0)
                {
                    mMessage = "Unable to save GeoTIFF file, check folder permissions.";
                    if (mpProgress)
                    {
                        mpProgress->updateProgress(mMessage, 0, ERRORS);
                    }

                    return false;
                }

                updateProgress(row, srows, mMessage, NORMAL);
            }
            accessor->nextRow();
        }
    }
    else // subcube write
    {
        const vector<DimensionDescriptor>& rows = mpFileDescriptor->getRows();
        const vector<DimensionDescriptor>& columns = mpFileDescriptor->getColumns();
        const vector<DimensionDescriptor>& bands = mpFileDescriptor->getBands();

        unsigned int activeRowNumber = 0;
        unsigned int rowSize(0);
        unsigned int outRow(0);
        for (unsigned int r = 0; r < srows; ++r)
        {
            if (mAbortFlag == true)
            {
                mMessage = "GeoTIFF export aborted!";
                if (mpProgress != NULL)
                {
                    mpProgress->updateProgress(mMessage, 0, ERRORS);
                }

                return false;
            }

            DimensionDescriptor rowDim = rows[r];
            if (rowDim.isActiveNumberValid())
            {
                // Skip to the next row
                for (; activeRowNumber < rowDim.getActiveNumber(); ++activeRowNumber)
                {
                    accessor->nextRow();
                }

                VERIFY(accessor.isValid());

                vector<char> rowData(size);
                if (rowData.empty())
                {
                    mMessage = "Error GeoTIFFExporter008: Unable to allocate row buffer.";
                    if (mpProgress)
                    {
                        mpProgress->updateProgress(mMessage, 0, ERRORS);
                    }

                    return false;
                }

                unsigned int activeColumnNumber = 0;

                char* pExportData = &(rowData.front());
                for (unsigned int c = 0; c < scols; ++c)
                {
                    DimensionDescriptor columnDim = columns[c];
                    if (columnDim.isActiveNumberValid())
                    {
                        // Skip to the next column
                        for (; activeColumnNumber < columnDim.getActiveNumber(); ++activeColumnNumber)
                        {
                            accessor->nextColumn();
                        }

                        char* pCurrentPixel = reinterpret_cast<char*>(accessor->getColumn());

                        unsigned int activeBandNumber = 0;
                        for (unsigned int b = 0; b < sbands; ++b)
                        {
                            DimensionDescriptor bandDim = bands[b];
                            if (bandDim.isActiveNumberValid())
                            {
                                // Skip to the next band
                                for (; activeBandNumber < bandDim.getActiveNumber(); ++activeBandNumber)
                                {
                                    pCurrentPixel += bytesPerElement;
                                }

                                memcpy(pExportData, pCurrentPixel, bytesPerElement);
                                pExportData += bytesPerElement;
                                rowSize += bytesPerElement;
                            }
                        }
                    }
                    else // this column is not included, go to next column
                    {
                        accessor->nextColumn();
                    }
                }

                if (rowSize > 0)
                {
                    // write here
                    if (TIFFWriteScanline(pOut, &rowData[0], outRow, size) < 0)
                    {
                        mMessage = "Error GeoTIFFExporter006: Unable to save GeoTIFF file, check folder permissions.";
                        if (mpProgress)
                        {
                            mpProgress->updateProgress(mMessage, 0, ERRORS);
                        }

                        return false;
                    }

                    updateProgress(outRow++, srows, mMessage, NORMAL);
                }
            }
            else // this row is not included, go to next row
            {
                accessor->nextRow();
            }
        }
    }

    //assumed everything has been done correctly up to now
    //copy over Geo ref info if there are any, else
    //try to look for world file in same directory and apply
    if (!(applyWorldFile(pOut)))
    {
        if (!(CreateGeoTIFF(pOut)))
        {
            //no geo info found, where is it located?
            mMessage = "Geo data is unavailable and will not be written to the output file!";
            updateProgress(srows, srows, mMessage, WARNING);
            if (mpStep != NULL)
            {
                mpStep->addMessage(mMessage, "app", "9C1E7ADE-ADC4-468c-B15E-FEB53D5FEF5B", true);
            }
        }
    }

    return true;
}
Esempio n. 5
0
bool SignatureLibraryImp::insertSignatures(const vector<Signature*>& signatures)
{
   if (find(signatures.begin(), signatures.end(), static_cast<Signature*>(NULL)) != signatures.end())
   {
      return false;
   }

   SignatureLibrary* pInterface = dynamic_cast<SignatureLibrary*>(this);
   if (!mOriginalAbscissa.empty())
   {
      return false;
   }

   const DynamicObject* pMetadata = getMetadata();
   VERIFY(pMetadata != NULL);
   const vector<double>* pWavelengths =
      dv_cast<vector<double> >(&pMetadata->getAttributeByPath(CENTER_WAVELENGTHS_METADATA_PATH));
   if (pWavelengths == NULL || pWavelengths->empty())
   {
      return false;
   }

   RasterDataDescriptor* pRasterDescriptor = RasterUtilities::generateRasterDataDescriptor(getName(), 
      pInterface, signatures.size(), pWavelengths->size(), 1, BIP, FLT8BYTES, ON_DISK);
   if (pRasterDescriptor == NULL)
   {
      return false;
   }

   ModelResource<RasterElement> pRasterElement (dynamic_cast<RasterElement*>(
      Service<ModelServices>()->createElement(pRasterDescriptor)));

   if (pRasterElement.get() == NULL)
   {
      return false;
   }

   PlugInResource resampler("Resampler");
   Resampler* pResampler = dynamic_cast<Resampler*>(resampler.get());
   if (pResampler == NULL)
   {
      return false;
   }

   DataAccessor accessor = pRasterElement->getDataAccessor();
   vector<Signature*>::const_iterator ppSignature;
   int i = 0;
   for (ppSignature = signatures.begin(); ppSignature != signatures.end(); ++ppSignature, ++i)
   {
      if (!accessor.isValid())
      {
         return false;
      }
      double* pRasterData = reinterpret_cast<double*>(accessor->getRow());
      const vector<double>* pFromData = dv_cast<vector<double> >(&(*ppSignature)->getData("Reflectance"));
      const vector<double>* pFromWavelengths = dv_cast<vector<double> >(&(*ppSignature)->getData("Wavelength"));
      if (pFromData == NULL || pFromWavelengths == NULL)
      {
         return false;
      }
      vector<double> toData;
      vector<int> toBands;
      vector<double> toFwhm;
      string errorMessage;
      bool success = pResampler->execute(*pFromData, toData, *pFromWavelengths, *pWavelengths, toFwhm,
         toBands, errorMessage);
      if (success == false || toBands.size() != pWavelengths->size())
      {
         return false;
      }
      std::copy(toData.begin(), toData.end(), pRasterData);
      accessor->nextRow();
      string name = (*ppSignature)->getName();
      DataDescriptor* pDataDesc = Service<ModelServices>()->createDataDescriptor(name, "DataElement", pInterface);
      DataDescriptorImp* pSigDesc = dynamic_cast<DataDescriptorImp*>(pDataDesc);
      mSignatures.push_back(new LibrarySignatureAdapter(*pSigDesc, SessionItemImp::generateUniqueId(), i, pInterface));
      mSignatureNames[name] = mSignatures.back();
   }

   mpOdre.reset(pRasterElement.release());

   mOriginalAbscissa = *pWavelengths;

   notify(SIGNAL_NAME(Subject, Modified));

   return true;
}
Esempio n. 6
0
bool SignatureLibraryImp::resample(const vector<double> &abscissa)
{
   if (mpOdre.get() == NULL)
   {
      return false;
   }

   if (abscissa == mAbscissa)
   {
      return true;
   }

   PlugInResource resampler("Resampler");
   Resampler* pResampler = dynamic_cast<Resampler*>(resampler.get());
   if (pResampler == NULL)
   {
      return false;
   }

   desample();

   if (abscissa.empty())
   {
      return true;
   }

   RasterDataDescriptor* pDesc = dynamic_cast<RasterDataDescriptor*>(mpOdre->getDataDescriptor());
   VERIFY(pDesc != NULL);

   unsigned int numSigs = pDesc->getRowCount();
   if (numSigs == 0)
   {
      return true;
   }

   mResampledData.resize(numSigs * abscissa.size());

   DataAccessor da = mpOdre->getDataAccessor();
   if (da.isValid() == false)
   {
      desample();
      return false;
   }

   vector<double> originalOrdinateData(mOriginalAbscissa.size());
   vector<double> toData;
   vector<double> toFwhm;
   vector<int> toBands;
   string errorMessage;
   for (unsigned int i = 0; i < mSignatures.size(); ++i)
   {
      switchOnEncoding(pDesc->getDataType(), getOriginalAsDouble, da->getRow(), mOriginalAbscissa.size(),
         originalOrdinateData);
      toData.clear();
      toData.reserve(mAbscissa.size());
      toBands.clear();
      toBands.reserve(mAbscissa.size());
      bool success = pResampler->execute(originalOrdinateData, toData, 
         mOriginalAbscissa, abscissa, toFwhm, toBands, errorMessage);
      if (!success || toData.size() != abscissa.size())
      {
         desample();
         return false;
      }
      std::copy(toData.begin(), toData.end(), &mResampledData[i*abscissa.size()]);
      da->nextRow();
   }

   mAbscissa = abscissa;

   mNeedToResample = false;

   notify(SIGNAL_NAME(Subject, Modified));

   return true;
}
Esempio n. 7
0
RasterPage* ConvertToBsqPager::getPage(DataRequest* pOriginalRequest, DimensionDescriptor startRow,
   DimensionDescriptor startColumn, DimensionDescriptor startBand)
{
   VERIFYRV(pOriginalRequest != NULL, NULL);
   if (pOriginalRequest->getWritable())
   {
      return NULL;
   }

   InterleaveFormatType requestedType = pOriginalRequest->getInterleaveFormat();
   DimensionDescriptor stopRow = pOriginalRequest->getStopRow();
   DimensionDescriptor stopColumn = pOriginalRequest->getStopColumn();
   DimensionDescriptor stopBand = pOriginalRequest->getStopBand();

   unsigned int concurrentRows = std::min(pOriginalRequest->getConcurrentRows(),
      stopRow.getActiveNumber() - startRow.getActiveNumber() + 1);
   unsigned int concurrentBands = pOriginalRequest->getConcurrentBands();

   VERIFY(requestedType == BSQ);
   VERIFY(startBand == stopBand && concurrentBands == 1);

   VERIFY(mpRaster != NULL);
   const RasterDataDescriptor* pDd = dynamic_cast<const RasterDataDescriptor*>(mpRaster->getDataDescriptor());
   VERIFY(pDd != NULL);
   InterleaveFormatType interleave = pDd->getInterleaveFormat();

   VERIFY(interleave == BIL || interleave == BIP);

   unsigned int numRows = pDd->getRowCount();
   unsigned int numCols = pDd->getColumnCount();
   unsigned int numBands = pDd->getBandCount();

   if (startRow.getActiveNumber() >= numRows || stopRow.getActiveNumber() >= numRows ||
      startColumn.getActiveNumber() >= numCols || stopColumn.getActiveNumber() >= numCols ||
      startBand.getActiveNumber() >= numBands || stopBand.getActiveNumber() >= numBands)
   {
      return NULL;
   }

   unsigned int cols = stopColumn.getActiveNumber() - startColumn.getActiveNumber() + 1;
   std::auto_ptr<ConvertToBsqPage> pPage(new ConvertToBsqPage(concurrentRows, cols, mBytesPerElement));
   unsigned char* pDst = reinterpret_cast<unsigned char*>(pPage->getRawData());
   if (pDst == NULL)
   {
      return NULL;
   }

   FactoryResource<DataRequest> pRequest;
   pRequest->setRows(startRow, stopRow);
   pRequest->setColumns(startColumn, stopColumn, cols);
   pRequest->setBands(startBand, startBand, 1);
   DataAccessor da = mpRaster->getDataAccessor(pRequest.release());

   if (interleave == BIP)
   {
      for (unsigned int row = 0; row < concurrentRows; ++row)
      {
         for (unsigned int col = 0; col < cols; ++col)
         {
            if (da.isValid() == false)
            {
               return NULL;
            }

            memcpy(pDst, da->getColumn(), mBytesPerElement);
            pDst += mBytesPerElement;
            da->nextColumn();
         }

         da->nextRow();
      }
   }
   else if (interleave == BIL)
   {
      for (unsigned int row = 0; row < concurrentRows; ++row)
      {
         if (da.isValid() == false)
         {
            return NULL;
         }

         memcpy(pDst, da->getRow(), mBytesPerElement * cols);
         pDst += mBytesPerElement * cols;
         da->nextRow();
      }
   }

   return pPage.release();
}