user55937
user55937

Reputation: 61

How to access pixel values using NITRO NITF library using C++ bindings

I have a task to write a module to unpack National Imagery Transmission Format (NITF) images and pass around the data in memory to various processing modules. I have chosen to use the NITRO library. I am trying to figure out how to read the image and access the pixel values, but I am having trouble. I am using the C++ bindings.

I successfully compiled the library. Now, I am trying to use the unit tests to understand how to use the library, namely read an image and access the pixel values. There are also some examples here. However, the unit tests and code snippets don't perform this task directly.

My toy example is below. I've tried variations of the code below, but I almost always get some error in image_reader.read(). The code below results in an error about too many bands, but if I limit the number of bands, then I don't get an error but the buffer doesn't seem to have any values in it.

I would be grateful to anyone who could give me some guidance or tips on how to use this library to access pixel values.

#include "stdafx.h"

#define IMPORT_NITRO_API

#include <import/nitf.hpp>

int _tmain(int argc, _TCHAR* argv[])
{
    const std::string filename = "my_image.NTF";
    nitf::Reader reader;
    nitf::IOHandle io(filename.c_str());
    nitf::Record record = reader.read(io);

    nitf::List images = record.getImages();
    nitf::ListIterator iter = images.begin(); // NITF can store more than one image - just try the first
    nitf::ImageSegment segment = *iter; 

    nitf::SubWindow window; // define a subwindow for reading - try to read the whole image although it might be slow
    unsigned int numRows = segment.getSubheader().getNumRows();
    unsigned int numCols = segment.getSubheader().getNumCols();
    const int band_count = segment.getSubheader().getBandCount();
    window.setNumRows(numRows);
    window.setNumCols(numCols);
    window.setNumBands(band_count);

    nitf::Uint32* band_list = new nitf::Uint32[band_count];
    for (nitf::Uint32 band_number = 0; band_number < band_count; band_number++)
        band_list[band_number] = band_number;

    window.setBandList(band_list);

    auto image_reader = reader.newImageReader(1); // 1 seems to be the image number: nitro-master\c\nitf\tests\test_create_xmltre.c
    std::vector< std::vector<nitf::Uint8> > buffer(band_count); // User-defined data buffers for read
    for (nitf::Uint32 band_number = 0; band_number < band_count; band_number++)
        buffer[band_number].resize(numRows * numCols);

    int padded = 0; // Returns TRUE if pad pixels may have been read
    image_reader.read(window, (nitf::Uint8**)&buffer[0], &padded);

    return 0;
}

Upvotes: 2

Views: 1082

Answers (0)

Related Questions