void MetadataReader::doBands(const std::string& key,
                             ::nitf::ImageSubheader& header)
{
    const int nbands = (int)header.getNumImageBands();
    for (int i=0; i<nbands; i++)
    {
        ::nitf::BandInfo bandinfo = header.getBandInfo(i);
        std::string subkey = key + ".BAND:" + std::to_string(i);
        doBand(subkey, bandinfo);
    }
}
void MetadataReader::doImageSubheader(const std::string& key,
                                      ::nitf::ImageSubheader& subheader)
{
    writeField(key, "IID1", subheader.getImageId());
    writeField(key, "IDATIM", subheader.getImageDateAndTime());
    writeField(key, "TGTID", subheader.getTargetId());
    writeField(key, "IID2", subheader.getImageTitle());
    writeField(key, "ISCLAS", subheader.getImageSecurityClass());
    
    ::nitf::FileSecurity security = subheader.getSecurityGroup();
    doSecurity(key, "I", security);

    writeField(key, "ENCRYP", subheader.getEncrypted());
    writeField(key, "ISORCE", subheader.getImageSource());
    writeField(key, "NROWS", subheader.getNumRows());
    writeField(key, "NCOLS", subheader.getNumCols());
    writeField(key, "PVTYPE", subheader.getPixelValueType());
    writeField(key, "IREP", subheader.getImageRepresentation());
    writeField(key, "ICAT", subheader.getImageCategory());
    writeField(key, "ABPP", subheader.getActualBitsPerPixel());
    writeField(key, "PJUST", subheader.getPixelJustification());
    writeField(key, "ICORDS", subheader.getImageCoordinateSystem());
    writeField(key, "IGEOLO", subheader.getCornerCoordinates());
    writeField(key, "NICOM", subheader.getNumImageComments());

    ::nitf::List list = subheader.getImageComments();
    doComments(key, list);
    
    writeField(key, "IC", subheader.getImageCompression());
    writeField(key, "COMRAT", subheader.getCompressionRate());
    writeField(key, "NBANDS", subheader.getNumImageBands());
    writeField(key, "XBANDS", subheader.getNumMultispectralImageBands());

    doBands(key, subheader);

    writeField(key, "ISYNC", subheader.getImageSyncCode());
    writeField(key, "IMODE", subheader.getImageMode());
    writeField(key, "NBPR", subheader.getNumBlocksPerRow());
    writeField(key, "NBPC", subheader.getNumBlocksPerCol());
    writeField(key, "NPPBH", subheader.getNumPixelsPerHorizBlock());
    writeField(key, "NPPVB", subheader.getNumPixelsPerVertBlock());
    writeField(key, "NBPP", subheader.getNumBitsPerPixel());
    writeField(key, "IDLVL", subheader.getImageDisplayLevel());
    writeField(key, "IALVL", subheader.getImageAttachmentLevel());
    writeField(key, "ILOC", subheader.getImageLocation());
    writeField(key, "IMAG", subheader.getImageMagnification());
}
void NITFReadControl::validateSegment(nitf::ImageSubheader subheader,
                                      const NITFImageInfo* info)
{
    const size_t numBandsSeg =
            static_cast<nitf::Uint32>(subheader.getNumImageBands());

    const std::string pjust = subheader.getPixelJustification().toString();
    // TODO: More validation in here!
    if (pjust != "R")
    {
        throw except::Exception(Ctxt("Expected right pixel justification"));
    }

    // The number of bytes per pixel, which we count to be 3 in the
    // case of 24-bit true color and 8 in the case of complex float
    // and 1 in the case of most SIDD data
    const size_t numBitsPerPixel =
            static_cast<nitf::Uint32>(subheader.getNumBitsPerPixel());
    const size_t numBytesPerPixel = (numBitsPerPixel + 7) / 8;
    const size_t numBytesSeg = numBytesPerPixel * numBandsSeg;

    const size_t nbpp = info->getData()->getNumBytesPerPixel();
    if (numBytesSeg != nbpp)
    {
        std::ostringstream ostr;
        ostr << "Expected [" << nbpp << "] bytes per pixel, found ["
             << numBytesSeg << "]";
        throw except::Exception(Ctxt(ostr.str()));
    }

    const size_t numCols = info->getData()->getNumCols();
    const size_t numColsSubheader =
            static_cast<nitf::Uint32>(subheader.getNumCols());

    if (numColsSubheader != numCols)
    {
        std::ostringstream ostr;
        ostr << "Invalid column width: was expecting [" << numCols
             << "], got [" << numColsSubheader << "]";
        throw except::Exception(Ctxt(ostr.str()));
    }

}