nitf_Record *readRecord(char *file) { nitf_IOHandle in; /* Handle for input */ nitf_Reader *reader; /* Reader object */ nitf_Record *record; /* The result */ nitf_Error error; /* For errors */ in = nitf_IOHandle_create(file, 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) { nitf_Error_print(&error, stderr, "Error creating reader "); exit(EXIT_FAILURE); } record = nitf_Reader_read(reader, in, &error); if (!record) { nitf_Error_print(&error, stderr, "Error reading input "); exit(EXIT_FAILURE); } nitf_IOHandle_close(in); nitf_Reader_destruct(&reader); return(record); }
int main(int argc, char **argv) { int rc = 0; int argIt = 0, i = 0, num = 0, dump = 0; char *fname = NULL; nrt_Error error; nrt_IOInterface *io = NULL; nitf_Reader *reader = NULL; nitf_Record *record = NULL; nrt_Uint8 *buf = NULL; for (argIt = 1; argIt < argc; ++argIt) { if (strcmp(argv[argIt], "--dump") == 0) dump = 1; else if (!fname) { fname = argv[argIt]; } } if (!fname) { nrt_Error_init(&error, "Usage: [--x0 --y0 --x1 --y1] <j2k-file>", NRT_CTXT, NRT_ERR_INVALID_OBJECT); goto CATCH_ERROR; } if (nitf_Reader_getNITFVersion(fname) == NITF_VER_UNKNOWN) { nrt_Error_init(&error, "This file does not appear to be a valid NITF", NRT_CTXT, NRT_ERR_INVALID_OBJECT); goto CATCH_ERROR; } if (!(io = nrt_IOHandleAdapter_open(fname, NRT_ACCESS_READONLY, NRT_OPEN_EXISTING, &error))) goto CATCH_ERROR; if (!(reader = nitf_Reader_construct(&error))) goto CATCH_ERROR; if (!(record = nitf_Reader_readIO(reader, io, &error))) goto CATCH_ERROR; num = nitf_Record_getNumImages(record, &error); if (num > 0) { nitf_ListIterator iter = nitf_List_begin(record->images); nitf_ListIterator end = nitf_List_end(record->images); for (i = 0; nitf_ListIterator_notEqualTo(&iter, &end); ++i) { nitf_ImageSegment *segment = (nitf_ImageSegment *) nitf_ListIterator_get(&iter); nitf_ImageSubheader *subheader = segment->subheader; if (strcmp(subheader->imageCompression->raw, "C8") == 0) { j2k_Reader *j2kReader = NULL; j2k_Container *container = NULL; nrt_Uint32 cmpIt, nComponents; printf("Image %d contains J2K compressed data\n", (i + 1)); printf("Offset: %d\n", segment->imageOffset); if (!nrt_IOInterface_seek(io, segment->imageOffset, NRT_SEEK_SET, &error)) goto CATCH_ERROR; if (!(j2kReader = j2k_Reader_openIO(io, &error))) goto CATCH_ERROR; if (!(container = j2k_Reader_getContainer(j2kReader, &error))) goto CATCH_ERROR; printf("grid width:\t%d\n", j2k_Container_getGridWidth(container, &error)); printf("grid height:\t%d\n", j2k_Container_getGridHeight(container, &error)); printf("tile width:\t%d\n", j2k_Container_getTileWidth(container, &error)); printf("tile height:\t%d\n", j2k_Container_getTileHeight(container, &error)); printf("x tiles:\t%d\n", j2k_Container_getTilesX(container, &error)); printf("y tiles:\t%d\n", j2k_Container_getTilesY(container, &error)); printf("image type:\t%d\n", j2k_Container_getImageType(container, &error)); nComponents = j2k_Container_getNumComponents(container, &error); printf("components:\t%d\n", nComponents); for(cmpIt = 0; cmpIt < nComponents; ++cmpIt) { j2k_Component* c = j2k_Container_getComponent(container, cmpIt, &error); printf("===component %d===\n", (cmpIt + 1)); printf("width:\t\t%d\n", j2k_Component_getWidth(c, &error)); printf("height:\t\t%d\n", j2k_Component_getHeight(c, &error)); printf("precision:\t%d\n", j2k_Component_getPrecision(c, &error)); printf("x0:\t\t%d\n", j2k_Component_getOffsetX(c, &error)); printf("y0:\t\t%d\n", j2k_Component_getOffsetY(c, &error)); printf("x separation:\t%d\n", j2k_Component_getSeparationX(c, &error)); printf("y separation:\t%d\n", j2k_Component_getSeparationY(c, &error)); printf("signed:\t\t%d\n", j2k_Component_isSigned(c, &error)); } if (dump) { char namePrefix[NRT_MAX_PATH]; nrt_Uint32 width, height; nrt_Uint64 bufSize; if (buf) { NRT_FREE(buf); buf = NULL; } width = j2k_Container_getWidth(container, &error); height = j2k_Container_getWidth(container, &error); if ((bufSize = j2k_Reader_readRegion(j2kReader, 0, 0, width, height, &buf, &error)) == 0) { goto CATCH_ERROR; } NRT_SNPRINTF(namePrefix, NRT_MAX_PATH, "image-%d", (i + 1)); if (!writeFile(0, 0, width, height, buf, bufSize, namePrefix, &error)) { goto CATCH_ERROR; } } } nitf_ListIterator_increment(&iter); } } goto CLEANUP; CATCH_ERROR: { nrt_Error_print(&error, stdout, "Exiting..."); rc = 1; } CLEANUP: { if (reader) nitf_Reader_destruct(&reader); if (record) nitf_Record_destruct(&record); if (io) nrt_IOInterface_destruct(&io); } return rc; }
int main(int argc, char *argv[]) { char *inFile; /* Input filename */ char *outFile; /* Output filename */ char *compCode; /* Compression code NC or NM */ char *padValueString = NULL; /* Pad value string */ nitf_Reader *reader; /* Reader object */ nitf_Record *record; /* Record used for input and output */ imgInfo *imgs; /* Image segment information */ nitf_FileHeader *fileHdr; /* File header */ nitf_Uint32 numImages; /* Total number of image segments */ nitf_ListIterator imgIter; /* Image segment list iterator */ nitf_IOHandle in; /* Input I/O handle */ nitf_IOHandle out; /* Output I/O handle */ nitf_Writer *writer; /* Writer object for output */ static nitf_Error errorObj; /* Error object for messages */ nitf_Error *error; /* Pointer to the error object */ int i; error = &errorObj; compCode = "NM"; if (argc == 3) { inFile = argv[1]; outFile = argv[2]; } else if (argc == 4) { if (strcmp(argv[1], "-r") != 0) { fprintf(stderr, "Usage %s [-r] [-p padValue] inputFile outputFile\n", argv[0]); exit(EXIT_FAILURE); } inFile = argv[2]; outFile = argv[3]; compCode = "NC"; } else if (argc == 5) { if (strcmp(argv[1], "-p") != 0) { fprintf(stderr, "Usage %s [-r] [-p padValue] inputFile outputFile\n", argv[0]); exit(EXIT_FAILURE); } inFile = argv[3]; outFile = argv[4]; padValueString = argv[2]; } else { fprintf(stderr, "Usage %s [-r] inputFile outputFile\n", argv[0]); exit(EXIT_FAILURE); } /* Get the input record */ in = nitf_IOHandle_create(inFile, 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) { nitf_Error_print(error, stderr, "Error creating reader "); exit(EXIT_FAILURE); } record = nitf_Reader_read(reader, in, error); if (!record) { nitf_Error_print(error, stderr, "Error reading input "); exit(EXIT_FAILURE); } fileHdr = record->header; nitf_Field_get(fileHdr->numImages, &numImages, NITF_CONV_UINT, NITF_INT32_SZ, error); /* Get information about all image segments and set-up for write */ imgs = (imgInfo *) NITF_MALLOC(numImages * sizeof(imgInfo)); if (imgs == NULL) { nitf_Error_print(error, stderr, "Error allocating image info "); exit(EXIT_FAILURE); } for (i = 0;i < numImages;i++) { imgs[i].index = i; imgIter = nitf_List_at(record->images, i); imgs[i].seg = (nitf_ImageSegment *) nitf_ListIterator_get(&imgIter); if (!imgs[i].seg) { fprintf(stderr, "No Image segment in file\n"); exit(EXIT_FAILURE); } imgs[i].subhdr = imgs[i].seg->subheader; if (padValueString != NULL) { if (!decodePadValue(&(imgs[i]), padValueString, imgs[i].padValue, error)) { nitf_Error_print(error, stderr, "Error allocating image info "); exit(EXIT_FAILURE); } } else memset(imgs[i].padValue, 0, MAX_PAD); } /* Read all of the image data into buffers */ for (i = 0;i < numImages;i++) if (!readImageSegment(&(imgs[i]), reader, error)) { nitf_Error_print(error, stderr, "Error reading image segment "); exit(EXIT_FAILURE); } /* Set compression type to NM or NC (has to happen afetr read) */ for (i = 0;i < numImages;i++) { if (!nitf_ImageSubheader_setCompression(imgs[i].subhdr, compCode, "", error)) { nitf_Error_print(error, stderr, "No DE segment in file "); exit(EXIT_FAILURE); } } /* Create output */ out = nitf_IOHandle_create(outFile, NITF_ACCESS_WRITEONLY, NITF_CREATE, error); if (NITF_INVALID_HANDLE(out)) { nitf_Error_print(error, stderr, "Could not create output file "); exit(EXIT_FAILURE); } /* Setup write and write */ writer = nitf_Writer_construct(error); if (writer == NULL) { nitf_Error_print(error, stderr, "Write setup failed "); exit(EXIT_FAILURE); } if (!nitf_Writer_prepare(writer, record, out, error)) { nitf_Error_print(error, stderr, "Write setup failed "); exit(EXIT_FAILURE); } for (i = 0;i < numImages;i++) if (!makeImageSource(&(imgs[i]), writer, error)) { nitf_Error_print(error, stderr, "Write setup failed "); exit(EXIT_FAILURE); } /* Set pad pixel if padValue string is not NULL */ if (padValueString != NULL) for (i = 0;i < numImages;i++) { } if (!nitf_Writer_write(writer, error)) { nitf_Error_print(error, stderr, "Write error "); exit(EXIT_FAILURE); } /* Clean-up */ nitf_Record_destruct(&record); nitf_IOHandle_close(out); return 0; }
int main(int argc, char **argv) { /* Get the error object */ nitf_Error error; /* so I can remember what Im doing with args */ const char* treName; const char* fieldName; /* This is the reader object */ nitf_Reader *reader; nitf_Record *record; /* The IO handle */ nitf_IOHandle io; int num; /* Check argv and make sure we are happy */ if (argc != 4) { printf("Usage: %s <nitf-file> <TRE> <field>\n", argv[0]); exit(EXIT_FAILURE); } if (nitf_Reader_getNITFVersion(argv[1]) == NITF_VER_UNKNOWN) { printf("File: %s is not a NITF\n", argv[1]); exit(EXIT_FAILURE); } treName = argv[2]; fieldName = argv[3]; io = nitf_IOHandle_create(argv[1], NITF_ACCESS_READONLY, NITF_OPEN_EXISTING, &error); if (NITF_INVALID_HANDLE(io)) { nitf_Error_print(&error, stdout, "Exiting..."); exit(EXIT_FAILURE); } reader = nitf_Reader_construct(&error); if (!reader) { nitf_Error_print(&error, stdout, "Exiting (1) ..."); exit(EXIT_FAILURE); } #if NITF_VERBOSE_READER printf("Here are the loaded handlers\n"); printf("* * * * * * * * * * * * * * * *\n"); nitf_HashTable_print(reader->reg->treHandlers); printf("* * * * * * * * * * * * * * * *\n"); #endif record = nitf_Reader_read(reader, io, &error); lookForTREField(record->header->extendedSection, treName, fieldName); lookForTREField(record->header->userDefinedSection, treName, fieldName); if (!nitf_Field_get(record->header->numImages, &num, NITF_CONV_INT, NITF_INT32_SZ, &error)) goto CATCH_ERROR; /* And now show the image information */ if (num > 0) { /* Walk each image and show */ nitf_ListIterator iter = nitf_List_begin(record->images); nitf_ListIterator end = nitf_List_end(record->images); while (nitf_ListIterator_notEqualTo(&iter, &end)) { nitf_ImageSegment *segment = (nitf_ImageSegment *) nitf_ListIterator_get(&iter); lookForTREField(segment->subheader->extendedSection, treName, fieldName); lookForTREField(segment->subheader->userDefinedSection, treName, fieldName); nitf_ListIterator_increment(&iter); } } else { printf("No image in file!\n"); } nitf_IOHandle_close(io); nitf_Record_destruct(&record); nitf_Reader_destruct(&reader); return 0; CATCH_ERROR: printf("!!! we had a problem reading the file !!!\n"); nitf_Error_print(&error, stdout, "Exiting..."); exit(EXIT_FAILURE); }
int main(int argc, char **argv) { /* 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; /* These iterators are for going through the image segments */ nitf_ListIterator iter; nitf_ListIterator end; /* If you didnt give us a nitf file, we're croaking */ if (argc != 2) { printf("Usage: %s <nitf-file>\n", argv[0]); exit(EXIT_FAILURE); } 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(argv[1], 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); } /* 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); /* While we are not done... */ while ( nitf_ListIterator_notEqualTo(&iter, &end) ) { /* Get the image segment as its proper object */ nitf_ImageSegment* imageSegment = (nitf_ImageSegment*)nitf_ListIterator_get(&iter); assert( imageSegment->subheader); if ( imageSegment->subheader->userDefinedSection ) findInExtensions( imageSegment->subheader->userDefinedSection ); else printf("Nothing found in user defined section!\n"); if ( imageSegment->subheader->extendedSection ) findInExtensions( imageSegment->subheader->extendedSection ); else printf("Nothing found in extended section!\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; /* The reader object */ nitf_Writer* writer; /* The writer object */ nitf_Record* record; /* a record object */ nitf_Record* record2; nitf_ListIterator iter; /* current pos iterator */ nitf_ListIterator end; /* end of list iterator */ nitf_ImageSegment* segment; /* the image segment */ NITF_BOOL success; /* status bool */ nitf_IOHandle input_io; /* input IOHandle */ nitf_IOHandle output_io; /* output IOHandle */ nitf_Error error; /* error object */ /* Check argv and make sure we are happy */ if (argc != 3) { printf("Usage: %s <input-file> <output-file> \n", argv[0]); exit(EXIT_FAILURE); } input_io = nitf_IOHandle_create(argv[1], NITF_ACCESS_READONLY, NITF_OPEN_EXISTING, &error); if ( NITF_INVALID_HANDLE(input_io)) { goto CATCH_ERROR; } output_io = nitf_IOHandle_create(argv[2], NITF_ACCESS_WRITEONLY, NITF_CREATE | NITF_TRUNCATE, &error); if ( NITF_INVALID_HANDLE(output_io)) { goto CATCH_ERROR; } reader = nitf_Reader_construct(&error); if (!reader) { goto CATCH_ERROR; } writer = nitf_Writer_construct(&error); if (!writer) { goto CATCH_ERROR; } record = nitf_Record_construct(&error); if (!record) { goto CATCH_ERROR; } assert(nitf_Reader_read(reader, input_io, record, &error)); showFileHeader(record->header); /* Write to the file */ success = nitf_Writer_writeHeader(writer, record->header, output_io, &error); if (!success) goto CATCH_ERROR; /* ------ IMAGES ------ */ iter = nitf_List_begin(record->images); end = nitf_List_end(record->images); while (nitf_ListIterator_notEqualTo(&iter, &end)) { /* Cast it to an imageSegment... */ segment = (nitf_ImageSegment*)nitf_ListIterator_get(&iter); /* write the image subheader */ if (!nitf_Writer_writeImageSubheader(writer, segment->subheader, record->header->NITF_FVER->raw, output_io, &error)) { goto CATCH_ERROR; } /* Now, write the image */ if (!writeImage(segment, input_io, output_io)) { goto CATCH_ERROR; } nitf_ListIterator_increment(&iter); } nitf_IOHandle_close(input_io); nitf_IOHandle_close(output_io); nitf_Record_destruct(&record); nitf_Writer_destruct(&writer); /* Open the file we just wrote to, and dump it to screen */ input_io = nitf_IOHandle_create(argv[2], NITF_ACCESS_READONLY, NITF_OPEN_EXISTING, &error); if (NITF_INVALID_HANDLE(input_io)) { goto CATCH_ERROR; } record2 = nitf_Record_construct(&error); if (!record2) { goto CATCH_ERROR; } assert(nitf_Reader_readHeader(reader, input_io, record2, &error)); showFileHeader(record2->header); nitf_IOHandle_close(input_io); nitf_Record_destruct(&record2); nitf_Reader_destruct(&reader); return 0; CATCH_ERROR: if (input_io) nitf_IOHandle_close(input_io); if (output_io) nitf_IOHandle_close(output_io); if (record2) nitf_Record_destruct(&record2); if (reader) nitf_Reader_destruct(&reader); if (record) nitf_Record_destruct(&record); if (writer) nitf_Writer_destruct(&writer); nitf_Error_print(&error, stdout, "Exiting..."); exit(EXIT_FAILURE); }
int main(int argc, char **argv) { /* Get the error object */ nitf_Error error; /* This is the reader object */ nitf_Reader* reader; nitf_Record* record; /* The IO handle */ nitf_IOHandle io; int num; /* Check argv and make sure we are happy */ if ( argc != 2 ) { printf("Usage: %s <nitf-file>\n", argv[0]); exit(EXIT_FAILURE); } io = nitf_IOHandle_create(argv[1], NITF_ACCESS_READONLY, NITF_OPEN_EXISTING, &error); if ( NITF_INVALID_HANDLE( io ) ) { nitf_Error_print(&error, stdout, "Exiting..."); exit( EXIT_FAILURE ); } reader = nitf_Reader_construct(&error); if (!reader) { nitf_Error_print(&error, stdout, "Exiting (1) ..."); exit( EXIT_FAILURE ); } /* record = nitf_Record_construct(&error); if (!record) { nitf_Error_print(&error, stdout, "Exiting (2) ..."); nitf_Reader_destruct(&reader); exit( EXIT_FAILURE ); }*/ #if NITF_VERBOSE_READER printf("Here are the loaded handlers\n"); printf("* * * * * * * * * * * * * * * *\n"); nitf_HashTable_print(reader->reg->treHandlers); printf("* * * * * * * * * * * * * * * *\n"); #endif if ( ! (record = nitf_Reader_read(reader, io, &error )) ) { nitf_Error_print(&error, stdout, "Exiting ..."); exit(EXIT_FAILURE); } printf("User defined: in file header\n"); showExtSection(record->header->userDefinedSection); printf("Extended defined: in file header\n"); showExtSection(record->header->extendedSection); if (!nitf_Field_get(record->header->numImages, &num, NITF_CONV_INT, NITF_INT32_SZ, &error)) nitf_Error_print(&error, stdout, "Skipping b/c Invalid numImages"); /* And now show the image information */ else if (num > 0) { /* Walk each image and show */ nitf_ListIterator iter = nitf_List_begin(record->images); nitf_ListIterator end = nitf_List_end(record->images); while ( nitf_ListIterator_notEqualTo(&iter, &end) ) { nitf_ImageSegment* segment = (nitf_ImageSegment*)nitf_ListIterator_get(&iter); printf("User defined: in image segment\n"); showExtSection(segment->subheader->userDefinedSection); printf("Extended defined: in image segment\n"); showExtSection(segment->subheader->extendedSection); nitf_ListIterator_increment(&iter); } } else { printf("No image in file!\n"); } nitf_IOHandle_close(io); nitf_Record_destruct(&record); if ( !nitf_List_isEmpty(reader->warningList)) { /* Iterator to a list */ nitf_ListIterator it; /* Iterator to the end of list */ nitf_ListIterator endList; it = nitf_List_begin(reader->warningList); /* End of list pointer */ endList = nitf_List_end(reader->warningList); printf("WARNINGS: "); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); /* While we are not at the end */ while ( nitf_ListIterator_notEqualTo( &it, &endList ) ) { /* Get the last data */ char* p = (char*)nitf_ListIterator_get(&it); /* Make sure */ assert(p != NULL); /* Show the data */ printf("\tFound problem: [%s]\n\n", p); /* Increment the list iterator */ nitf_ListIterator_increment(&it); } printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); } nitf_Reader_destruct(&reader); return 0; }
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; }
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 reader object */ nitf_Reader* reader; nitf_Record* record; /* The IO handle */ nitf_IOHandle io; /* Get the error object */ nitf_Error error; /* Check argv and make sure we are happy */ if ( argc != 2 ) { printf("Usage: %s <nitf-file>\n", argv[0]); exit(EXIT_FAILURE); } io = nitf_IOHandle_create(argv[1], NITF_ACCESS_READONLY, NITF_OPEN_EXISTING, &error); if ( NITF_INVALID_HANDLE( io ) ) { nitf_Error_print(&error, stdout, "Exiting..."); exit( EXIT_FAILURE ); } reader = nitf_Reader_construct(&error); if (!reader) { nitf_Error_print(&error, stdout, "Exiting (1) ..."); exit( EXIT_FAILURE ); } printf("Here are the loaded handlers\n"); printf("* * * * * * * * * * * * * * * *\n"); { nitf_PluginRegistry* reg = nitf_PluginRegistry_getInstance(&error); nitf_HashTable_print( reg->treHandlers); } printf("* * * * * * * * * * * * * * * *\n"); record = nitf_Reader_read(reader, io, &error); if (!record) { nitf_Error_print(&error, stderr, "Failed to read the file"); exit(EXIT_FAILURE); } /* Now show the header */ showFileHeader(record->header); /* And now show the image information */ if (record->header->numImages) { /* Walk each image and show */ nitf_ListIterator iter = nitf_List_begin(record->images); nitf_ListIterator end = nitf_List_end(record->images); while ( nitf_ListIterator_notEqualTo(&iter, &end) ) { nitf_ImageSegment* segment = (nitf_ImageSegment*)nitf_ListIterator_get(&iter); nitf_ImageSubheader* dolly = nitf_ImageSubheader_clone(segment->subheader, &error); if (!dolly) { nitf_Error_print(&error, stdout, "During cloning!"); exit(EXIT_FAILURE); } SHOW_IMSUB(segment->subheader); SHOW_IMSUB(dolly); nitf_ImageSubheader_destruct(&dolly); nitf_ListIterator_increment(&iter); } } else { printf("No image in file!\n"); } nitf_IOHandle_close(io); nitf_Record_destruct(&record); nitf_Reader_destruct(&reader); 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 desIter; /* Iterator for getting the DES */ nitf_DESegment *des; /* DE segment to read */ nitf_DEReader *deReader; /* DE reader object for reading data */ char *data; /* Data buffer */ static nitf_Error errorObj; /* Error object for messages */ nitf_Error *error; /* Pointer to the error object */ error = &errorObj; if (argc != 2) { fprintf(stderr, "Usage %s inputFile\n", argv[0]); exit(EXIT_FAILURE); } /* Get the input record */ in = nitf_IOHandle_create(argv[1], 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) { nitf_IOHandle_close(in); nitf_Error_print(error, stderr, "Error creating reader "); exit(EXIT_FAILURE); } record = nitf_Reader_read(reader, in, error); if (!record) { nitf_Reader_destruct(&reader); nitf_IOHandle_close(in); nitf_Error_print(error, stderr, "Error reading input "); exit(EXIT_FAILURE); } /* Print the user header */ desIter = nitf_List_at(record->dataExtensions, 0); des = (nitf_DESegment *) nitf_ListIterator_get(&desIter); if (!des) { nitf_Reader_destruct(&reader); nitf_Record_destruct(&record); nitf_IOHandle_close(in); nitf_Error_print(error, stderr, "No DE segment in file "); exit(EXIT_FAILURE); } nitf_TRE_print(des->subheader->subheaderFields, error); /* Read the data and print it */ deReader = nitf_Reader_newDEReader(reader, 0, error); if (!deReader) { nitf_Reader_destruct(&reader); nitf_Record_destruct(&record); nitf_IOHandle_close(in); nitf_Error_print(error, stderr, "Could not create DEReader "); exit(EXIT_FAILURE); } fprintf(stdout, "Data length = %d\n", deReader->user->dataLength); fprintf(stdout, "Virtual data length = %d\n", deReader->user->virtualLength); fprintf(stdout, "File offset = %lld\n", deReader->user->baseOffset); data = (char *) NITF_MALLOC(deReader->user->virtualLength + 2); if (!data) { nitf_DEReader_destruct(&deReader); nitf_Reader_destruct(&reader); nitf_Record_destruct(&record); nitf_IOHandle_close(in); nitf_Error_print(error, stderr, "Could not allocate data buffer "); exit(EXIT_FAILURE); } if (!nitf_DEReader_read (deReader, data, deReader->user->virtualLength, error)) { NITF_FREE(data); nitf_DEReader_destruct(&deReader); nitf_Reader_destruct(&reader); nitf_Record_destruct(&record); nitf_IOHandle_close(in); nitf_Error_print(error, stderr, "Read failed "); exit(EXIT_FAILURE); } fprintf(stdout, "Data: |%s|\n", data); /* Try some seeks */ if (nitf_DEReader_seek(deReader, (nitf_Off) 4, SEEK_SET, error) == (nitf_Off) - 1) { NITF_FREE(data); nitf_DEReader_destruct(&deReader); nitf_Reader_destruct(&reader); nitf_Record_destruct(&record); nitf_IOHandle_close(in); nitf_Error_print(error, stderr, "Read failed "); exit(EXIT_FAILURE); } if (!nitf_DEReader_read(deReader, data, 1, error)) { NITF_FREE(data); nitf_DEReader_destruct(&deReader); nitf_Reader_destruct(&reader); nitf_Record_destruct(&record); nitf_IOHandle_close(in); nitf_Error_print(error, stderr, "Read failed "); exit(EXIT_FAILURE); } data[1] = 0; fprintf(stdout, "Data after set seek: |%s|\n", data); /* The last read advanced to position 5 so relative position10 should be the last byte in the file */ if (nitf_DEReader_seek(deReader, (nitf_Off) 10, SEEK_CUR, error) == (nitf_Off) - 1) { NITF_FREE(data); nitf_DEReader_destruct(&deReader); nitf_Reader_destruct(&reader); nitf_Record_destruct(&record); nitf_IOHandle_close(in); nitf_Error_print(error, stderr, "Read failed "); exit(EXIT_FAILURE); } if (!nitf_DEReader_read(deReader, data, 1, error)) { NITF_FREE(data); nitf_DEReader_destruct(&deReader); nitf_Reader_destruct(&reader); nitf_Record_destruct(&record); nitf_IOHandle_close(in); nitf_Error_print(error, stderr, "Read failed "); exit(EXIT_FAILURE); } data[1] = 0; fprintf(stdout, "Data after current seek: |%s|\n", data); if (nitf_DEReader_seek(deReader, (nitf_Off) - 2, SEEK_END, error) == (nitf_Off) - 1) { NITF_FREE(data); nitf_DEReader_destruct(&deReader); nitf_Reader_destruct(&reader); nitf_Record_destruct(&record); nitf_IOHandle_close(in); nitf_Error_print(error, stderr, "Read failed "); exit(EXIT_FAILURE); } if (!nitf_DEReader_read(deReader, data, 1, error)) { NITF_FREE(data); nitf_DEReader_destruct(&deReader); nitf_Reader_destruct(&reader); nitf_Record_destruct(&record); nitf_IOHandle_close(in); nitf_Error_print(error, stderr, "Read failed "); exit(EXIT_FAILURE); } data[1] = 0; fprintf(stdout, "Data after end seek: |%s|\n", data); /* Clean-up */ NITF_FREE(data); nitf_DEReader_destruct(&deReader); nitf_Reader_destruct(&reader); nitf_Record_destruct(&record); nitf_IOHandle_close(in); return 0; }
/* This test case just reads in a NITF file, clones it * then destructs the clone and original. * If you compile the library with the NITF_DEBUG flag set, * then you can make sure there are no memory leaks. * * In the future, we should use the cloned record to test * the functionality of a clone. */ int main(int argc, char **argv) { /* Get the error object */ nitf_Error error; /* This is the reader object */ nitf_Reader* reader; nitf_Record* record; nitf_Record* cloneRecord; /* The IO handle */ nitf_IOHandle io; int num; /* Check argv and make sure we are happy */ if ( argc != 2 ) { printf("Usage: %s <nitf-file>\n", argv[0]); exit(EXIT_FAILURE); } io = nitf_IOHandle_create(argv[1], NITF_ACCESS_READONLY, NITF_OPEN_EXISTING, &error); if ( NITF_INVALID_HANDLE( io ) ) { nitf_Error_print(&error, stdout, "Exiting..."); exit( EXIT_FAILURE ); } reader = nitf_Reader_construct(&error); if (!reader) { nitf_Error_print(&error, stdout, "Exiting(1) ..."); exit( EXIT_FAILURE ); } /* read the record */ record = nitf_Reader_read(reader, io, &error ); if (!record) goto CATCH_ERROR; cloneRecord = nitf_Record_clone(record, &error); if (!cloneRecord) { nitf_Error_print(&error, stdout, "Exiting(3) ..."); exit( EXIT_FAILURE ); } printf("Destructing Cloned Record\n"); nitf_Record_destruct(&cloneRecord); nitf_IOHandle_close(io); printf("Destructing Original Record\n"); nitf_Record_destruct(&record); nitf_Reader_destruct(&reader); return 0; CATCH_ERROR: printf("!!! we had a problem reading the file !!!\n"); nitf_Error_print(&error, stdout, "Exiting..."); exit(EXIT_FAILURE); }
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); }