Exemplo n.º 1
0
void manuallyWriteImageBands(nitf::ImageSegment & segment,
                             const std::string& imageName,
                             nitf::ImageReader& deserializer,
                             int imageNumber)
{
    int padded;

    nitf::ImageSubheader subheader = segment.getSubheader();


    nitf::Uint32 nBits = subheader.getNumBitsPerPixel();
    nitf::Uint32 nBands = subheader.getNumImageBands();
    nitf::Uint32 xBands = subheader.getNumMultispectralImageBands();
    nBands += xBands;

    nitf::Uint32 nRows = subheader.getNumRows();
    nitf::Uint32 nColumns = subheader.getNumCols();

    //one row at a time
    size_t subWindowSize = nColumns * NITF_NBPP_TO_BYTES(nBits);

    std::cout << "NBANDS -> " << nBands << std::endl
        << "XBANDS -> " << xBands << std::endl
        << "NROWS -> " << nRows << std::endl
        << "NCOLS -> " << nColumns << std::endl
        << "PVTYPE -> " << subheader.getPixelValueType().toString() << std::endl
        << "NBPP -> " << subheader.getNumBitsPerPixel().toString() << std::endl
        << "ABPP -> " << subheader.getActualBitsPerPixel().toString() << std::endl
        << "PJUST -> " << subheader.getPixelJustification().toString() << std::endl
        << "IMODE -> " << subheader.getImageMode().toString() << std::endl
        << "NBPR -> " << subheader.getNumBlocksPerRow().toString() << std::endl
        << "NBPC -> " << subheader.getNumBlocksPerCol().toString() << std::endl
        << "NPPBH -> " << (int)subheader.getNumPixelsPerHorizBlock() << std::endl
        << "NPPBV -> " << (int)subheader.getNumPixelsPerVertBlock() << std::endl
        << "IC -> " << subheader.getImageCompression().toString() << std::endl
        << "COMRAT -> " << subheader.getCompressionRate().toString() << std::endl;

    nitf::Uint8** buffer = new nitf::Uint8*[nBands];
    nitf::Uint32* bandList = new nitf::Uint32[nBands];

    for (nitf::Uint32 band = 0; band < nBands; band++)
        bandList[band] = band;

    nitf::SubWindow subWindow;
    subWindow.setStartCol(0);
    subWindow.setNumRows(1);
    subWindow.setNumCols(nColumns);

    // necessary ?
    nitf::DownSampler* pixelSkip = new nitf::PixelSkip(1, 1);
    subWindow.setDownSampler(pixelSkip);
    subWindow.setBandList(bandList);
    subWindow.setNumBands(nBands);

    assert(buffer);
    for (nitf::Uint32 i = 0; i < nBands; i++)
    {
        buffer[i] = new nitf::Uint8[subWindowSize];
        assert(buffer[i]);
    }

    std::vector<nitf::IOHandle> handles;
    //make the files
    for (nitf::Uint32 i = 0; i < nBands; i++)
    {
        std::string name = makeBandName(imageName, imageNumber, i);
        nitf::IOHandle toFile(name, NITF_ACCESS_WRITEONLY, NITF_CREATE);
        handles.push_back(toFile);
    }

    //read all row blocks and write to disk
    for (nitf::Uint32 i = 0; i < nRows; ++i)
    {
        subWindow.setStartRow(i);
        deserializer.read(subWindow, buffer, &padded);
        for (nitf::Uint32 j = 0; j < nBands; j++)
        {
            handles[j].write((const char*)buffer[j], subWindowSize);
        }
    }

    //close output handles
    for (nitf::Uint32 i = 0; i < nBands; i++)
        handles[i].close();

    /* free buffers */
    for (nitf::Uint32 i = 0; i < nBands; i++)
        delete [] buffer[i];
    delete [] buffer;
    delete [] bandList;
    delete pixelSkip;
}
Exemplo n.º 2
0
void writeImage(nitf::ImageSegment &segment,
                nitf::Reader &reader,
                const int imageNumber,
                const char *imageName,
                nitf_Uint32 rowSkipFactor,
                nitf_Uint32 columnSkipFactor, bool optz)
{
    size_t subWindowSize;
    nitf::SubWindow subWindow;
    unsigned int i;
    int padded;
    nitf::Uint8** buffer = NULL;
    nitf::Uint32 band;
    nitf::Uint32 * bandList;

    nitf::ImageReader deserializer = reader.newImageReader(imageNumber);

    // missing skip factor
    nitf::ImageSubheader subheader = segment.getSubheader();

    nitf::Uint32 nBits   = subheader.getNumBitsPerPixel();

    nitf::Uint32 nBands  = subheader.getNumImageBands();
    nitf::Uint32 xBands  = subheader.getNumMultispectralImageBands();
    nBands += xBands;


    nitf::Uint32 nRows   = subheader.getNumRows();
    nitf::Uint32 nCols   = subheader.getNumCols();
    subWindowSize = (size_t)(nRows / rowSkipFactor) *
        (size_t)(nCols / columnSkipFactor) *
        (size_t)NITF_NBPP_TO_BYTES(nBits);

    if (optz)
    {
        std::string irep =
                segment.getSubheader().getImageRepresentation().toString();
        std::string ic =
                segment.getSubheader().getImageCompression().toString();

        if (nBands == 3 && segment.getSubheader().getImageMode().toString()
                == "P" && str::startsWith(irep, "RGB")
                && NITF_NBPP_TO_BYTES(nBits) == 1 && str::startsWith(ic, "N"))
        {
            subWindowSize *= nBands;
            nBands = 1;
            std::cout << "Using accelerated 3-band RGB mode pix-interleaved image" << std::endl;
        }
        if (nBands == 2 && segment.getSubheader().getImageMode().toString()
                == "P" && str::startsWith(ic, "N"))
        {

            subWindowSize *= nBands;
            nBands = 1;
            std::cout << "Using accelerated 2-band IQ mode pix-interleaved image" << std::endl;
        }
    }


    std::cout << "NBANDS -> " << nBands << std::endl;
    std::cout << "XBANDS -> " << xBands << std::endl;
    std::cout << "NROWS -> "  << nRows  << std::endl;
    std::cout << "NCOLS -> "  << nCols  << std::endl;
    std::cout << "PVTYPE -> " << subheader.getPixelValueType().toString() << std::endl;
    std::cout << "NBPP -> " << subheader.getNumBitsPerPixel() .toString() << std::endl;
    std::cout << "ABPP -> " << subheader.getActualBitsPerPixel().toString() << std::endl;
    std::cout << "PJUST -> " << subheader.getPixelJustification().toString() << std::endl;
    std::cout << "IMODE -> " << subheader.getImageMode().toString() << std::endl;
    std::cout << "NBPR -> " << subheader.getNumBlocksPerRow().toString() << std::endl;
    std::cout << "NBPC -> " << subheader.getNumBlocksPerCol().toString() << std::endl;
    std::cout << "NPPBH -> " << (int)subheader.getNumPixelsPerHorizBlock() << std::endl;
    std::cout << "NPPBV -> " << (int)subheader.getNumPixelsPerVertBlock() << std::endl;
    std::cout << "IC -> " << subheader.getImageCompression().toString() << std::endl;
    std::cout << "COMRAT -> " << subheader.getCompressionRate().toString() << std::endl;

    std::cout << "Allocating work buffer..." << std::endl;

    buffer = new nitf::Uint8*[nBands];
    assert(buffer);

    band = 0;
    bandList = new nitf::Uint32[nBands];

    subWindow.setStartCol(0);
    subWindow.setStartRow(0);

    subWindow.setNumRows(nRows / rowSkipFactor);
    subWindow.setNumCols(nCols / columnSkipFactor);

    nitf::PixelSkip pixelSkip(rowSkipFactor, columnSkipFactor);

    subWindow.setDownSampler(&pixelSkip);

    for (band = 0; band < nBands; band++)
    {
        bandList[band] = band;
        buffer[band] = new nitf::Uint8[subWindowSize];
        assert(buffer[band]);
    }
    subWindow.setBandList(bandList);
    subWindow.setNumBands(nBands);

    std::cout << "Reading image..." << std::endl;
    deserializer.read(subWindow, buffer, &padded);

    std::cout << "Call completed!" << std::endl;

    std::cout << "Writing bands..." << std::endl;
    for (i = 0; i < nBands; i++)
    {
        std::cout << "Writing band # " << i << std::endl;
        std::string base = sys::Path::basename(imageName);

        size_t where = 0;
        while ((where = base.find(".")) != (size_t)std::string::npos)
            base.replace(where, 1, "_");

        std::ostringstream file;
        file << base << "__" << imageNumber << "__"
             << nRows / rowSkipFactor << '_'
             << nCols / columnSkipFactor << '_'
             << nBits << "_band_" << i << ".out";

        nitf::IOHandle toFile(file.str(), NITF_ACCESS_WRITEONLY, NITF_CREATE);
        toFile.write((const char *) buffer[i], subWindowSize);
        toFile.close();
        std::cout << "Finished # " << i << std::endl;
    }

    for (band = 0; band < nBands; band++)
        delete [] buffer[band];
    delete [] buffer;
    delete [] bandList;
}