NITF_BOOL readImageSegment(imgInfo *img, nitf_Reader *reader, nitf_Error *error) { nitf_Uint32 nBits; /* Bits/pixel */ nitf_Uint32 nBands; /* Number of bands */ nitf_Uint32 xBands; /* Number of extended bands */ nitf_Uint32 nRows; /* Number of rows */ nitf_Uint32 nColumns; /* Number of columns */ size_t subimageSize; /* Image band size in bytes */ nitf_SubWindow *subimage; /* Sub-image object specifying full image */ nitf_DownSampler *pixelSkip; /* Downsample for sub-window */ nitf_Uint32 *bandList; /* List of bands for read */ nitf_Uint32 band; /* Current band */ nitf_Uint8 **buffers; /* Read buffer one/band */ /* Image reader */ nitf_ImageReader *deserializer; int padded; /* Argument for read */ int i; /* Get dimension and band info */ nitf_Field_get(img->subhdr->numBitsPerPixel, &nBits, NITF_CONV_UINT, NITF_INT32_SZ, error); nitf_Field_get(img->subhdr->numImageBands, &nBands, NITF_CONV_UINT, NITF_INT32_SZ, error); nitf_Field_get(img->subhdr->numMultispectralImageBands, &xBands, NITF_CONV_UINT, NITF_INT32_SZ, error); nBands += xBands; nitf_Field_get(img->subhdr->numRows, &nRows, NITF_CONV_UINT, NITF_INT32_SZ, error); nitf_Field_get(img->subhdr->numCols, &nColumns, NITF_CONV_UINT, NITF_INT32_SZ, error); img->bytes = NITF_NBPP_TO_BYTES(nBits); subimageSize = nRows * nColumns * img->bytes; img->imgSize = subimageSize; /* Setup sub-window */ subimage = nitf_SubWindow_construct(error); if (subimage == NULL) return(NITF_FAILURE); bandList = (nitf_Uint32 *) NITF_MALLOC(sizeof(nitf_Uint32 *) * nBands); if (bandList == NULL) return(NITF_FAILURE); subimage->startCol = 0; subimage->startRow = 0; subimage->numRows = nRows; subimage->numCols = nColumns; for (band = 0;band < nBands;band++) bandList[band] = band; subimage->bandList = bandList; subimage->numBands = nBands; pixelSkip = nitf_PixelSkip_construct(1, 1, error); if (pixelSkip == NULL) return(NITF_FAILURE); if (!nitf_SubWindow_setDownSampler(subimage, pixelSkip, error)) return(NITF_FAILURE); /* Set-up buffers (one/band) */ buffers = (nitf_Uint8 **) NITF_MALLOC(nBands * sizeof(nitf_Uint8*)); if (buffers == NULL) return(NITF_FAILURE); for (i = 0;i < nBands;i++) { buffers[i] = (nitf_Uint8 *) malloc(subimageSize); if (buffers[i] == NULL) return(NITF_FAILURE); } deserializer = nitf_Reader_newImageReader(reader, img->index, error); if (deserializer == NULL) return(NITF_FAILURE); if (!nitf_ImageReader_read(deserializer, subimage, buffers, &padded, error)) return(NITF_FAILURE); img->nBands = nBands; img->buffers = buffers; return(NITF_SUCCESS); }
nitf_Record *doRead(const char *inFile) { /* This is the error we hopefully wont receive */ nitf_Error e; /* This is the reader */ nitf_Reader *reader; /* This is the record of the file we are reading */ nitf_Record *record; /* This is the io handle we will give the reader to parse */ nitf_IOHandle io; int count = 0; int numImages; int numTexts; int numDataExtensions; nitf_ListIterator iter; nitf_ListIterator end; nitf_ImageSegment *imageSegment = NULL; nitf_ImageReader *deserializer = NULL; nitf_TextSegment *textSegment = NULL; nitf_DESegment *deSegment = NULL; nitf_SegmentReader *segmentReader = NULL; nitf_SegmentReader *deReader = NULL; reader = nitf_Reader_construct(&e); if (!reader) { nitf_Error_print(&e, stderr, "nitf::Reader::construct() failed"); exit(EXIT_FAILURE); } /* If you did, though, we'll be nice and open it for you */ io = nitf_IOHandle_create(inFile, NITF_ACCESS_READONLY, NITF_OPEN_EXISTING, &e); /* But, oh boy, if you gave us a bad location...! */ if (NITF_INVALID_HANDLE(io)) { /* You had this coming! */ nitf_Error_print(&e, stderr, "nitf::IOHandle::create() failed"); exit(EXIT_FAILURE); } /* Read the file */ record = nitf_Reader_read(reader, io, &e); if (!record) { nitf_Error_print(&e, stderr, "nitf::Reader::read() failed"); exit(EXIT_FAILURE); } if (!nitf_Field_get (record->header->numImages, &numImages, NITF_CONV_INT, NITF_INT32_SZ, &e)) { nitf_Error_print(&e, stderr, "nitf::Field::get() failed"); numImages = 0; } if (!nitf_Field_get (record->header->numTexts, &numTexts, NITF_CONV_INT, NITF_INT32_SZ, &e)) { nitf_Error_print(&e, stderr, "nitf::Field::get() failed"); numTexts = 0; } if (!nitf_Field_get (record->header->numDataExtensions, &numDataExtensions, NITF_CONV_INT, NITF_INT32_SZ, &e)) { nitf_Error_print(&e, stderr, "nitf::Field::get() failed"); numDataExtensions = 0; } if (record->images) { end = nitf_List_end(record->images); for (count = 0; count < numImages; ++count) { iter = nitf_List_at(record->images, count); if (nitf_ListIterator_equals(&iter, &end)) { printf("Out of bounds on iterator [%d]!\n", count); exit(EXIT_FAILURE); } imageSegment = (nitf_ImageSegment *) nitf_ListIterator_get(&iter); deserializer = nitf_Reader_newImageReader(reader, count, &e); if (!deserializer) { nitf_Error_print(&e, stderr, "Couldnt spawn deserializer"); exit(EXIT_FAILURE); } printf("Writing image %d... ", count); /* Write the thing out */ manuallyWriteImageBands(imageSegment, inFile, deserializer, count, &e); nitf_ImageReader_destruct(&deserializer); printf("done.\n"); /* Increment the iterator so we can continue */ nitf_ListIterator_increment(&iter); } } /* loop over texts and read the data to a file */ if (record->texts) { end = nitf_List_end(record->texts); for (count = 0; count < numTexts; ++count) { iter = nitf_List_at(record->texts, count); if (nitf_ListIterator_equals(&iter, &end)) { printf("Out of bounds on iterator [%d]!\n", count); exit(EXIT_FAILURE); } textSegment = (nitf_TextSegment *) nitf_ListIterator_get(&iter); segmentReader = nitf_Reader_newTextReader(reader, count, &e); if (!segmentReader) { nitf_Error_print(&e, stderr, "Couldnt spawn deserializer"); exit(EXIT_FAILURE); } printf("Writing text %d... ", count); /* Write the thing out */ writeTextData(textSegment, inFile, segmentReader, count, &e); nitf_SegmentReader_destruct(&segmentReader); /* Increment the iterator so we can continue */ nitf_ListIterator_increment(&iter); } } /*XXX*/ /* loop over data extensions and read the data to a file */ if (record->dataExtensions) { fprintf(stderr, "XXX Data Ext %d\n", numDataExtensions); end = nitf_List_end(record->dataExtensions); for (count = 0; count < numDataExtensions; ++count) { iter = nitf_List_at(record->dataExtensions, count); if (nitf_ListIterator_equals(&iter, &end)) { printf("Out of bounds on iterator [%d]!\n", count); exit(EXIT_FAILURE); } deSegment = (nitf_DESegment *) nitf_ListIterator_get(&iter); deReader = nitf_Reader_newDEReader(reader, count, &e); if (!deReader) { nitf_Error_print(&e, stderr, "Couldnt spawn deserializer"); exit(EXIT_FAILURE); } printf("Writing data extension %d... ", count); /* Write the thing out */ writeDEData(deSegment, inFile, deReader, count, &e); nitf_SegmentReader_destruct(&segmentReader); /* Increment the iterator so we can continue */ nitf_ListIterator_increment(&iter); } } nitf_Reader_destruct(&reader); return record; }
int main(int argc, char **argv) { /* This is the error we hopefully wont receive */ nitf_Error e; /* Skip factors */ nitf_Uint32 rowSkipFactor = 1; nitf_Uint32 columnSkipFactor = 1; /* This is the reader */ nitf_Reader *reader; /* This is the record of the file we are reading */ nitf_Record *record; /* This is the io handle we will give the reader to parse */ nitf_IOHandle io; int count = 0; int numImages; /* These iterators are for going through the image segments */ nitf_ListIterator iter; nitf_ListIterator end; char* inputFile; NITF_BOOL optz = 0; /* If you didnt give us a nitf file, we're croaking */ if (argc < 2) { printf("Usage: %s <nitf-file> (-o)\n", argv[0]); exit(EXIT_FAILURE); } if (argc == 3) { optz = 1; if (strcmp(argv[1], "-o") == 0) { inputFile = argv[2]; } else if (strcmp(argv[2], "-o") == 0) { inputFile = argv[1]; } else { printf("Usage: %s <nitf-file> (-o)\n", argv[0]); exit(EXIT_FAILURE); } } else inputFile = argv[1]; /* You should use this function to test that you have a valid NITF */ if (nitf_Reader_getNITFVersion( inputFile ) == NITF_VER_UNKNOWN) { printf("This file does not appear to be a valid NITF"); exit(EXIT_FAILURE); } reader = nitf_Reader_construct(&e); if (!reader) { nitf_Error_print(&e, stderr, "Reader creation failed"); exit(EXIT_FAILURE); } /* * As of 2.5, you do not have to use an IOHandle if you use * readIO instead of read() */ io = nitf_IOHandle_create(inputFile, NITF_ACCESS_READONLY, NITF_OPEN_EXISTING, &e); if (NITF_INVALID_HANDLE(io)) { nitf_Error_print(&e, stderr, "IO creation failed"); exit(EXIT_FAILURE); } /* Read the file */ record = nitf_Reader_read(reader, io, &e); if (!record) { nitf_Error_print(&e, stderr, "Read failed"); nitf_IOHandle_close(io); nitf_Reader_destruct(&reader); exit(EXIT_FAILURE); } numImages = nitf_Record_getNumImages(record, &e); if ( NITF_INVALID_NUM_SEGMENTS( numImages ) ) { nitf_Error_print(&e, stderr, "Failed to get the number of images"); nitf_IOHandle_close(io); nitf_Reader_destruct(&reader); nitf_Record_destruct( &record ); exit(EXIT_FAILURE); } /* Set the iterator to traverse the list of image segments */ iter = nitf_List_begin(record->images); /* And set this one to the end, so we'll know when we're done! */ end = nitf_List_end(record->images); for (count = 0; count < numImages; ++count) { nitf_ImageSegment *imageSegment = (nitf_ImageSegment *) nitf_ListIterator_get(&iter); nitf_ImageReader *deserializer = nitf_Reader_newImageReader(reader, count, &e); if (!deserializer) { nitf_Error_print(&e, stderr, "Couldnt spawn deserializer"); exit(EXIT_FAILURE); } printf("Writing image %d... ", count); /* Write the thing out */ writeImage(imageSegment, inputFile, deserializer, count, rowSkipFactor, columnSkipFactor, optz, &e); nitf_ImageReader_destruct(&deserializer); printf("done.\n"); /* Increment the iterator so we can continue */ nitf_ListIterator_increment(&iter); } nitf_Record_destruct(&record); nitf_Reader_destruct(&reader); nitf_IOHandle_close(io); return 0; }
int main(int argc, char *argv[]) { nitf_Reader *reader; /* Reader object */ nitf_Record *record; /* Record used for input and output */ nitf_IOHandle in; /* Input I/O handle */ nitf_ListIterator imgIter; /* Image segment list iterator */ nitf_ListIterator imgEnd; /* Image segment list iterator */ nitf_ImageSegment *seg; /* Image segment object */ nitf_ImageSubheader *subhdr; /* Image subheader object */ nitf_ImageReader *iReader; /* Image reader */ nitf_BlockingInfo *blkInfo; /* Blocking information */ static nitf_Error errorObj; /* Error object for messages */ nitf_Error *error; /* Pointer to the error object */ /* Image information */ char imageMode[NITF_IMODE_SZ+1]; /* Image (blocking) mode */ /* Mask structure and mask components */ nitf_Uint32 imageDataOffset; /* Offset to actual image data past masks */ nitf_Uint32 blockRecordLength; /* Block mask record length */ nitf_Uint32 padRecordLength; /* Pad mask record length */ nitf_Uint32 padPixelValueLength; /* Pad pixel value length in bytes */ nitf_Uint8 *padValue; /* Pad value */ nitf_Uint64 *blockMask; /* Block mask array */ nitf_Uint64 *padMask; /* Pad mask array */ size_t imgCtr = 0; const char* pathname; error = &errorObj; if (argc != 2) { fprintf(stderr, "Usage %s inputFile\n", argv[0]); exit(EXIT_FAILURE); } pathname = argv[1]; /* Get the input record */ in = nitf_IOHandle_create(pathname, NITF_ACCESS_READONLY, NITF_OPEN_EXISTING, error); if (NITF_INVALID_HANDLE(in)) { nitf_Error_print(error, stderr, "Error opening input "); exit(EXIT_FAILURE); } reader = nitf_Reader_construct(error); if (reader == NULL) { nitf_IOHandle_close(in); nitf_Error_print(error, stderr, "Error creating reader "); exit(EXIT_FAILURE); } record = nitf_Reader_read(reader, in, error); if (record == NULL) { nitf_Reader_destruct(&reader); nitf_IOHandle_close(in); nitf_Error_print(error, stderr, "Error reading input "); exit(EXIT_FAILURE); } /* Loop through the image segments */ imgIter = nitf_List_begin(record->images); imgEnd = nitf_List_end(record->images); while (nitf_ListIterator_notEqualTo(&imgIter, &imgEnd)) { /* Get information from the image subheader */ seg = (nitf_ImageSegment *) nitf_ListIterator_get(&imgIter); if (seg == NULL) { fprintf(stderr, "No Image segment\n"); nitf_Reader_destruct(&reader); nitf_IOHandle_close(in); exit(EXIT_FAILURE); } subhdr = seg->subheader; nitf_Field_get(subhdr->imageMode, imageMode, NITF_CONV_STRING, NITF_IMODE_SZ + 1, error); imageMode[NITF_IMODE_SZ] = 0; /* Get an image reader which creates the nitf_ImageIO were the masks are */ iReader = nitf_Reader_newImageReader(reader, imgCtr, NULL, error); if (iReader == NULL) { nitf_Reader_destruct(&reader); nitf_IOHandle_close(in); nitf_Record_destruct(&record); nitf_Error_print(error, stderr, "Error reading input "); exit(EXIT_FAILURE); } blkInfo = nitf_ImageReader_getBlockingInfo(iReader, error); if (blkInfo == NULL) { nitf_ImageReader_destruct(&iReader); nitf_Reader_destruct(&reader); nitf_IOHandle_close(in); nitf_Record_destruct(&record); nitf_Error_print(error, stderr, "Error reading input "); exit(EXIT_FAILURE); } /* Print the blocking information */ printf("Image %s segment %zu:\n", pathname, imgCtr); printf(" Blocking (mode is %s):\n", imageMode); printf(" Block array dimensions (r,c) = %d %d\n", blkInfo->numBlocksPerRow, blkInfo->numBlocksPerCol); printf(" Block dimensions (r,c) = %d,%d\n", blkInfo->numRowsPerBlock, blkInfo->numColsPerBlock); printf(" Block length in bytes %zu\n", blkInfo->length); /* Get the actual information */ if (nitf_ImageIO_getMaskInfo(iReader->imageDeblocker, &imageDataOffset, &blockRecordLength, &padRecordLength, &padPixelValueLength, &padValue, &blockMask, &padMask)) { nitf_Uint32 i; printf(" Masked image:\n"); printf(" Image data offset = %d\n", imageDataOffset); printf(" Block and pad mask record lengths = %u %u\n", blockRecordLength, padRecordLength); printf(" Pad value length = %u\n", padPixelValueLength); printf(" Pad value = "); for (i = 0;i < padPixelValueLength;i++) { printf("%x ", padValue[i]); } printf("\n"); if (blockRecordLength != 0) { dumpMask("Block", blockMask, blkInfo->numBlocksPerRow, blkInfo->numBlocksPerCol); } if (padRecordLength != 0) { dumpMask("Pad", padMask, blkInfo->numBlocksPerRow, blkInfo->numBlocksPerCol); } } else { printf("Not a masked image\n"); } nitf_ImageReader_destruct(&iReader); nrt_ListIterator_increment(&imgIter); ++imgCtr; } /* Clean-up */ nitf_Reader_destruct(&reader); nitf_IOHandle_close(in); nitf_Record_destruct(&record); return(0); }