Exemplo n.º 1
0
/*
 * Class:     nitf_ImageSource
 * Method:    getBandSources
 * Signature: ()[Lnitf/BandSource;
 */
JNIEXPORT jobjectArray JNICALL Java_nitf_ImageSource_getBandSources
    (JNIEnv * env, jobject self)
{
    nitf_ImageSource *imageSource = _GetObj(env, self);

    jclass bandSourceClass = (*env)->FindClass(env, "nitf/BandSource");
    nitf_Error error;
    jobjectArray sources;
    jobject element;
    jint index = 0;
    jint num, actualNum = 0;
    jmethodID methodID;
    nitf_ListIterator iter, end;
    jlong address;      /* temp address */

    num = imageSource->size;

    /* iterate over the bandsources to get the correct count */
    iter = nitf_List_begin(imageSource->bandSources);
    end = nitf_List_end(imageSource->bandSources);
    while (nitf_ListIterator_notEqualTo(&iter, &end))
    {
        actualNum++;
        nitf_ListIterator_increment(&iter);
    }

    /* check to see if the size member is WRONG */
    if (num != actualNum)
    {
        num = actualNum;
        /* set the size member to the RIGHT value */
        imageSource->size = num;
    }

    sources = (*env)->NewObjectArray(env, num, bandSourceClass, NULL);
    methodID = (*env)->GetStaticMethodID(env, bandSourceClass, "getByAddress",
        "(J)Lnitf/BandSource;");

    index = 0;
    /* Now, iterate over the bandsources to create an array */
    iter = nitf_List_begin(imageSource->bandSources);
    end = nitf_List_end(imageSource->bandSources);
    while (nitf_ListIterator_notEqualTo(&iter, &end))
    {
        address = (jlong)((nitf_BandSource *)nitf_ListIterator_get(&iter));
        element = (*env)->CallStaticObjectMethod(env, bandSourceClass,
                methodID, address);
        (*env)->SetObjectArrayElement(env, sources, index++, element);
        nitf_ListIterator_increment(&iter);
    }
    return sources;

  CATCH_ERROR:
    _ThrowNITFException(env, error.message);
    return NULL;
}
Exemplo n.º 2
0
NITFPRIV(NITF_CLEVEL) checkCCSExtent(nitf_Record* record,
                                     nitf_Error* error)
{
    int i = 0;
    int clevel = NITF_CLEVEL_03;
    nitf_ListIterator it = nitf_List_begin(record->images);
    nitf_ListIterator end = nitf_List_end(record->images);

    while (nitf_ListIterator_notEqualTo(&it, &end))
    {
        nitf_ImageSegment* imageSegment =
            (nitf_ImageSegment*)nitf_ListIterator_get(&it);

        int result = checkILOC(imageSegment->subheader, error);
        if (result == NITF_CLEVEL_CHECK_FAILED )
            return result;

        if ( result > clevel )
        {
            clevel = result;
        }
        ++i;
        nitf_ListIterator_increment(&it);

    }
    return clevel;

}
Exemplo n.º 3
0
NITFPRIV(void) polyDestroy(NITF_DATA* data)
{
    /* TODO!! */
    cgm_PolyLineElement* poly = (cgm_PolyLineElement*)data;
    nitf_ListIterator it, end;

    nitf_List* list = (nitf_List*)poly->vertices;

    if (poly->attributes)
        cgm_LineAttributes_destruct( &(poly->attributes) );

    it = nitf_List_begin(list);
    end = nitf_List_end(list);

    while (nitf_ListIterator_notEqualTo(&it, &end))
    {
        cgm_Vertex* v = (cgm_Vertex*)nitf_ListIterator_get(&it);
        cgm_Vertex_destruct(&v);
        nitf_ListIterator_increment(&it);
    }

    nitf_List_destruct(&list);

    NITF_FREE(data);
}
Exemplo n.º 4
0
/*
 * Class:     nitf_ImageSource
 * Method:    getSize
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_nitf_ImageSource_getSize
    (JNIEnv * env, jobject self)
{
    nitf_ImageSource *imageSource = _GetObj(env, self);
    jint num, actualNum = 0;
    nitf_ListIterator iter, end;

    num = imageSource->size;

    /* iterate over the bandsources to get the correct count */
    iter = nitf_List_begin(imageSource->bandSources);
    end = nitf_List_end(imageSource->bandSources);
    while (nitf_ListIterator_notEqualTo(&iter, &end))
    {
        actualNum++;
        nitf_ListIterator_increment(&iter);
    }

    /* check to see if the size member is WRONG */
    if (num != actualNum)
    {
        /* set the size member to the RIGHT value */
        imageSource->size = num;
    }

    return imageSource->size;
}
Exemplo n.º 5
0
/*
 * Class:     nitf_Record
 * Method:    getGraphics
 * Signature: ()[Lnitf/GraphicSegment;
 */
JNIEXPORT jobjectArray JNICALL Java_nitf_Record_getGraphics
    (JNIEnv * env, jobject self)
{
    nitf_Record *record = _GetObj(env, self);

    jclass segmentClass = (*env)->FindClass(env, "nitf/GraphicSegment");

    jobjectArray graphics;
    jobject segment;
    jint count = 0;
    nitf_GraphicSegment *graphicSegment;
    nitf_ListIterator iter, end;

    /* get the initial count */
    iter = nitf_List_begin(record->graphics);
    end = nitf_List_end(record->graphics);
    while (nitf_ListIterator_notEqualTo(&iter, &end))
    {
        count++;
        nitf_ListIterator_increment(&iter);
    }

    /* make the array */
    graphics = (*env)->NewObjectArray(env, count, segmentClass, NULL);

    iter = nitf_List_begin(record->graphics);
    end = nitf_List_end(record->graphics);
    count = 0;
    while (nitf_ListIterator_notEqualTo(&iter, &end))
    {
        jmethodID methodID =
            (*env)->GetMethodID(env, segmentClass, "<init>", "(J)V");
        /*  Get the image segment as its proper object  */
        graphicSegment =
            (nitf_GraphicSegment *) nitf_ListIterator_get(&iter);

        segment = (*env)->NewObject(env,
                                    segmentClass,
                                    methodID, (jlong) graphicSegment);
        (*env)->SetObjectArrayElement(env, graphics, count, segment);
        count++;

        /*  Increment the iterator so we can continue  */
        nitf_ListIterator_increment(&iter);
    }
    return graphics;
}
Exemplo n.º 6
0
void findInExtensions(nitf_Extensions* ext)
{
    /*  These iterators are for going through the image segments  */
    nitf_ListIterator  iter;
    nitf_ListIterator  end;
    nitf_List* list;
    assert( ext );

    list = nitf_Extensions_get(ext, "ACFTB");
    if (list)
    {
        /*  Set the iterator to traverse the list of image segments  */
        iter = nitf_List_begin(list);
        /*  And set this one to the end, so we'll know when we're done!  */
        end  = nitf_List_end(list);

        /*  While we are not done...  */
        while ( nitf_ListIterator_notEqualTo(&iter, &end) )
        {
            nitf_TRE* tre;

            printf("Found ACFTB instance\n");

            /*  Get the image segment as its proper object  */
            tre = (nitf_TRE*)nitf_ListIterator_get(&iter);
            if ( nitf_HashTable_exists( tre->hash, "raw_data" ) )
            {
                printf("Your plugin for ACFTB was not loaded so the data is contained in the RAW section\n");
            }
            else
            {
                nitf_Pair* mission = nitf_HashTable_find( tre->hash,
                                     "ACMSNID" );
                if (! mission )
                {
                    printf("Error: no Mission ID available\n");
                    nitf_HashTable_print( tre->hash );

                }


                else
                {
                    nitf_Field* field = (nitf_Field*)mission->data;

                    printf("Mission ID: [%.*s]\n", field->length, field->raw);
                }
            }

            /*  Increment the iterator so we can continue  */
            nitf_ListIterator_increment(&iter);
        }
    }
    else
    {
        printf("No ACFTB\n");
    }

}
Exemplo n.º 7
0
/*
 * Class:     nitf_Record
 * Method:    getImages
 * Signature: ()[Lnitf/ImageSegment;
 */
JNIEXPORT jobjectArray JNICALL Java_nitf_Record_getImages
    (JNIEnv * env, jobject self)
{
    nitf_Record *record = _GetObj(env, self);
    jclass segmentClass = (*env)->FindClass(env, "nitf/ImageSegment");

    jobjectArray images;
    jobject segment;
    jint count = 0;
    nitf_ImageSegment *imageSegment;
    nitf_ListIterator iter, end;

    /* get the initial count */
    iter = nitf_List_begin(record->images);
    end = nitf_List_end(record->images);
    while (nitf_ListIterator_notEqualTo(&iter, &end))
    {
        count++;
        nitf_ListIterator_increment(&iter);
    }

    /* make the array */
    images = (*env)->NewObjectArray(env, count, segmentClass, NULL);

    iter = nitf_List_begin(record->images);
    end = nitf_List_end(record->images);
    count = 0;
    while (nitf_ListIterator_notEqualTo(&iter, &end))
    {
        jmethodID methodID =
            (*env)->GetMethodID(env, segmentClass, "<init>", "(J)V");
        imageSegment = (nitf_ImageSegment *) nitf_ListIterator_get(&iter);

        segment = (*env)->NewObject(env,
                                    segmentClass,
                                    methodID, (jlong) imageSegment);
        (*env)->SetObjectArrayElement(env, images, count, segment);
        count++;

        nitf_ListIterator_increment(&iter);
    }
    return images;
}
Exemplo n.º 8
0
void lookForTREField(nitf_Extensions* ext, 
		     const char* tag, 
		     const char* pattern)
{
    nitf_ListIterator current, last;

    nitf_List* list = nitf_Extensions_getTREsByName(ext, tag);
    if (list == NULL) return;
    
    current = nitf_List_begin(list);
    last = nitf_List_end(list);

    while (nitf_ListIterator_notEqualTo(&current, &last))
    {
	nitf_Error error;
	nitf_TRE* tre = nitf_ListIterator_get(&current);
	nitf_ListIterator currentInst;
	nitf_ListIterator lastInst;
	
	nitf_List* found = nitf_TRE_find(tre, pattern, &error);
	if (!found) return;
	

	currentInst = nitf_List_begin(found);
	lastInst = nitf_List_end(found);
	
	while (nitf_ListIterator_notEqualTo(&currentInst, &lastInst))
	{
	    nitf_Pair* pair = nitf_ListIterator_get(&currentInst);
	    nitf_Field* field = (nitf_Field*)pair->data;
	    printf("Found: %s [%.*s]\n", pair->key, (int)field->length, field->raw);
	    nitf_ListIterator_increment(&currentInst);

	}
	nitf_ListIterator_increment(&current);
    }


}
Exemplo n.º 9
0
NITFPRIV(void) polyPrint(NITF_DATA* data)
{
    cgm_PolyLineElement* poly = (cgm_PolyLineElement*)data;
    nitf_ListIterator it, end;
    nitf_List* list = (nitf_List*)poly->vertices;

    if (poly->attributes)
        cgm_LineAttributes_print( poly->attributes );

    it = nitf_List_begin(list);
    end = nitf_List_end(list);

    while (nitf_ListIterator_notEqualTo(&it, &end))
    {
        cgm_Vertex* v = (cgm_Vertex*)nitf_ListIterator_get(&it);
        printf("\t");
        cgm_Vertex_print(v);
        nitf_ListIterator_increment(&it);
    }
}
Exemplo n.º 10
0
/*
 * Class:     nitf_ImageSubheader
 * Method:    getImageComments
 * Signature: ()[Lnitf/Field;
 */
JNIEXPORT jobjectArray JNICALL Java_nitf_ImageSubheader_getImageComments
    (JNIEnv * env, jobject self)
{
    nitf_ImageSubheader *header = _GetObj(env, self);
    nitf_Uint32 numComments;
    nitf_Error error;
    jint i;
    jobjectArray array;
    jobject fieldObj;
    nitf_ListIterator iter, end;
    jclass fieldClass = (*env)->FindClass(env, "nitf/Field");

    NITF_TRY_GET_UINT32(header->numImageComments, &numComments, &error);
    if (numComments > 0)
    {
        array =
            (*env)->NewObjectArray(env, numComments, fieldClass, NULL);
        
        iter = nitf_List_begin(header->imageComments);
        end = nitf_List_end(header->imageComments);
        for (i = 0; nitf_ListIterator_notEqualTo(&iter, &end); ++i)
        {
            fieldObj = _GetFieldObj(env,
                (nitf_Field*) nitf_ListIterator_get(&iter));
            (*env)->SetObjectArrayElement(env, array, i, fieldObj);
            nitf_ListIterator_increment(&iter);
        }
    }
    else
    {
        array = (*env)->NewObjectArray(env, 0, fieldClass, NULL);
    }
    return array;

  CATCH_ERROR:
    _ThrowNITFException(env, error.message);
    return NULL;
}
Exemplo n.º 11
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;

}
Exemplo n.º 12
0
nitf_TREPrivateData_clone(nitf_TREPrivateData *source, nitf_Error * error)
{
    nitf_TREPrivateData *priv = NULL;

    /* temporary nitf_List pointer */
    nitf_List *lPtr;

    /* temporary nitf_Field pointer */
    nitf_Field *field;

    /* temporary nitf_Pair pointer */
    nitf_Pair *pair;

    /* iterator to front of list */
    nitf_ListIterator iter;

    /* iterator to back of list */
    nitf_ListIterator end;

    /* used for iterating */
    int i;

    if (source)
    {
        priv = nitf_TREPrivateData_construct(error);
        if (!priv)
            goto CATCH_ERROR;

        if (!nitf_TREPrivateData_setDescriptionName(
                priv, source->descriptionName, error))
        {
            goto CATCH_ERROR;
        }

        /*  Copy the entire contents of the hash  */
        for (i = 0; i < source->hash->nbuckets; i++)
        {
            /*  Foreach chain in the hash table...  */
            lPtr = source->hash->buckets[i];
            iter = nitf_List_begin(lPtr);
            end = nitf_List_end(lPtr);

            /*  And while they are different...  */
            while (nitf_ListIterator_notEqualTo(&iter, &end))
            {
                /*  Retrieve the field at the iterator...  */
                pair = (nitf_Pair *) nitf_ListIterator_get(&iter);

                /*  Cast it back to a field...  */
                field = (nitf_Field *) pair->data;

                /* clone the field */
                field = nitf_Field_clone(field, error);

                /*  If that failed, we need to destruct  */
                if (!field)
                    goto CATCH_ERROR;

                /*  Yes, now we can insert the new field!  */
                if (!nitf_HashTable_insert(priv->hash,
                                           pair->key, field, error))
                {
                    goto CATCH_ERROR;
                }
                nitf_ListIterator_increment(&iter);
            }
        }
    }
    else
    {
        nitf_Error_initf(error,
                         NITF_CTXT,
                         NITF_ERR_INVALID_OBJECT,
                         "Trying to clone NULL pointer");
    }
    return priv;

  CATCH_ERROR:
    if (priv)
        nitf_TREPrivateData_destruct(&priv);
    return NULL;
}
Exemplo n.º 13
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);
}
Exemplo n.º 14
0
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;
}
Exemplo n.º 15
0
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;
}
Exemplo n.º 16
0
void showImageSubheader(nitf_ImageSubheader* imsub)
{
    int q; /* iterator */
    int nbands, xbands;
    nitf_Error error;
    nitf_ListIterator iter, end;

    printf("Read image into imsub\n");
    SHOW_VAL( imsub->filePartType );
    SHOW_VAL( imsub->imageId );
    SHOW_VAL( imsub->imageDateAndTime );
    SHOW_VAL( imsub->targetId );
    SHOW_VAL( imsub->imageTitle );
    SHOW_VAL( imsub->imageSecurityClass );
    SHOW_VAL( imsub->encrypted );
    SHOW_VAL( imsub->imageSource );
    SHOW_VAL( imsub->numRows );
    SHOW_VAL( imsub->numCols );
    SHOW_VAL( imsub->pixelValueType );
    SHOW_VAL( imsub->imageRepresentation );
    SHOW_VAL( imsub->imageCategory );
    SHOW_VAL( imsub->actualBitsPerPixel );
    SHOW_VAL( imsub->pixelJustification );
    SHOW_VAL( imsub->imageCoordinateSystem );
    SHOW_VAL( imsub->cornerCoordinates );

    SHOW_VAL( imsub->numImageComments );
    iter = nitf_List_begin(imsub->imageComments);
    end = nitf_List_end(imsub->imageComments);
    while (nitf_ListIterator_notEqualTo(&iter, &end))
    {
        nitf_Field* commentField = (nitf_Field*) nitf_ListIterator_get(&iter);
        SHOW_VAL(commentField);
        nitf_ListIterator_increment(&iter);
    }

    SHOW_VAL( imsub->imageCompression );
    SHOW_VAL( imsub->compressionRate );

    SHOW_VAL( imsub->numImageBands );
    SHOW_VAL( imsub->numMultispectralImageBands );

    NITF_TRY_GET_UINT32(imsub->numImageBands, &nbands, &error);
    NITF_TRY_GET_UINT32(imsub->numMultispectralImageBands, &xbands, &error);
    nbands += xbands;
    for (q = 0; q < nbands; q++)
    {
        SHOW_VAL( imsub->bandInfo[q]->representation);
        SHOW_VAL( imsub->bandInfo[q]->subcategory );
        SHOW_VAL( imsub->bandInfo[q]->imageFilterCondition );
        SHOW_VAL( imsub->bandInfo[q]->imageFilterCode );
        SHOW_VAL( imsub->bandInfo[q]->numLUTs );
        SHOW_VAL( imsub->bandInfo[q]->bandEntriesPerLUT );
    }

    /*  Skip band stuff for now  */
    SHOW_VAL( imsub->imageSyncCode );
    SHOW_VAL( imsub->imageMode );
    SHOW_VAL( imsub->numBlocksPerRow );
    SHOW_VAL( imsub->numBlocksPerCol );
    SHOW_VAL( imsub->numPixelsPerHorizBlock );
    SHOW_VAL( imsub->numPixelsPerVertBlock );
    SHOW_VAL( imsub->numBitsPerPixel );
    SHOW_VAL( imsub->imageDisplayLevel );
    SHOW_VAL( imsub->imageAttachmentLevel );
    SHOW_VAL( imsub->imageLocation );
    SHOW_VAL( imsub->imageMagnification );
    SHOW_VAL( imsub->userDefinedImageDataLength );
    SHOW_VAL( imsub->userDefinedOverflow );
    SHOW_VAL( imsub->extendedHeaderLength );
    SHOW_VAL( imsub->extendedHeaderOverflow );

    return;

CATCH_ERROR:
    printf("Error processing\n");

}
Exemplo n.º 17
0
NITFPRIV(NITF_CLEVEL) checkSpecificImageAttributes(nitf_Record* record,
                                         nitf_Error* error)
{
    NITF_CLEVEL clevel = NITF_CLEVEL_03;


    nitf_ListIterator it = nitf_List_begin(record->images);
    nitf_ListIterator end = nitf_List_end(record->images);

    int i = 0;

    while (nitf_ListIterator_notEqualTo(&it, &end) )
    {

        NITF_CLEVEL result = NITF_CLEVEL_UNKNOWN;
        char irep[ NITF_IREP_SZ + 1];

        nitf_ImageSegment* imageSegment =
            (nitf_ImageSegment*)nitf_ListIterator_get(&it);

        if (!nitf_Field_get(imageSegment->subheader->NITF_IREP,
                            irep, NITF_CONV_STRING,
                            NITF_IREP_SZ + 1, error))
            return NITF_CLEVEL_CHECK_FAILED;
        nitf_Utils_trimString(irep);

        if (strcmp( irep, "MONO") == 0)
        {

            result = checkMonoImage(imageSegment->subheader, error);
        }
        else if (strcmp( irep, "RGB") == 0)
        {
            result = checkRGBImage(imageSegment->subheader, error);
        }
        else if (strcmp( irep, "RGB/LUT" ) == 0)
        {
            result = checkRGBLUTImage(imageSegment->subheader, error);
        }
        else if (strcmp( irep, "MULTI" ) == 0)
        {
            result = checkMultiImage(imageSegment->subheader, error);
        }
        else
        {
            /* What happens for these other reps ? */
            return result;

        }

        if ( result == NITF_CLEVEL_CHECK_FAILED ) return result;

        if ( result > clevel )
        {
            clevel = result;

        }
        ++i;
        nitf_ListIterator_increment(&it);
    }
    return clevel;
}
Exemplo n.º 18
0
void doWrite(nitf_Record * record, char *inRootFile, char *outFile)
{
    nitf_ListIterator iter;

    nitf_ImageWriter *iWriter;
    nitf_ImageSource *iSource;

    nitf_SegmentWriter *segmentWriter;
    nitf_SegmentSource *segmentSource;

    nitf_ListIterator end;
    int i;
    int numImages;
    int numTexts;
    int numDataExtensions;
    nitf_Writer *writer = NULL;
    nitf_Error error;
    nitf_IOHandle output_io = nitf_IOHandle_create(outFile,
                              NITF_ACCESS_WRITEONLY,
                              NITF_CREATE,
                              &error);

    if (NITF_INVALID_HANDLE(output_io))
    {
        goto CATCH_ERROR;
    }

    writer = nitf_Writer_construct(&error);
    if (!writer)
    {
        goto CATCH_ERROR;
    }
    if (!nitf_Writer_prepare(writer, record, output_io, &error))
    {
        goto CATCH_ERROR;
    }

    if (!nitf_Field_get
            (record->header->numImages, &numImages, NITF_CONV_INT,
             NITF_INT32_SZ, &error))
    {
        nitf_Error_print(&error, stderr, "nitf::Value::get() failed");
        numImages = 0;
    }

    if (!nitf_Field_get
            (record->header->numTexts, &numTexts, NITF_CONV_INT,
             NITF_INT32_SZ, &error))
    {
        nitf_Error_print(&error, stderr, "nitf::Value::get() failed");
        numTexts = 0;
    }

    if (!nitf_Field_get
            (record->header->numDataExtensions, &numDataExtensions, NITF_CONV_INT,
             NITF_INT32_SZ, &error))
    {
        nitf_Error_print(&error, stderr, "nitf::Value::get() failed");
        numDataExtensions = 0;
    }

    if (record->images)
    {
        end = nitf_List_end(record->images);
        for (i = 0; i < numImages; i++)
        {
            int nbands;
            nitf_ImageSegment *imseg = NULL;
            iter = nitf_List_at(record->images, i);
            assert(nitf_ListIterator_notEqualTo(&iter, &end));

            imseg = (nitf_ImageSegment *) nitf_ListIterator_get(&iter);
            assert(imseg);

            if (!nitf_Field_get
                    (imseg->subheader->numImageBands, &nbands, NITF_CONV_INT,
                     NITF_INT32_SZ, &error))
                goto CATCH_ERROR;

            iWriter = nitf_Writer_newImageWriter(writer, i, &error);
            if (!iWriter)
            {
                goto CATCH_ERROR;
            }
            iSource = setupBands(nbands, i, inRootFile);
            if (!iSource)
                goto CATCH_ERROR;
            if (!nitf_ImageWriter_attachSource(iWriter, iSource, &error))
                goto CATCH_ERROR;
        }
    }

    if (record->texts)
    {
        end = nitf_List_end(record->texts);
        for (i = 0; i < numTexts; i++)
        {
            nitf_TextSegment *textSeg = NULL;
            char *inFile = NULL;
            nitf_IOHandle sourceHandle;

            iter = nitf_List_at(record->texts, i);
            assert(nitf_ListIterator_notEqualTo(&iter, &end));

            textSeg = (nitf_TextSegment *) nitf_ListIterator_get(&iter);
            assert(textSeg);

            segmentWriter = nitf_Writer_newTextWriter(writer, i, &error);
            if (!segmentWriter)
            {
                goto CATCH_ERROR;
            }

            /* setup file */
            inFile = makeBandName(inRootFile, "text", i, -1);
            sourceHandle =
                nitf_IOHandle_create(inFile, NITF_ACCESS_READONLY,
                                     NITF_OPEN_EXISTING, &error);
            if (NITF_INVALID_HANDLE(sourceHandle))
                goto CATCH_ERROR;

            freeBandName(&inFile);

            segmentSource = nitf_SegmentFileSource_construct(sourceHandle, 0, 0, &error);
            if (!segmentSource)
                goto CATCH_ERROR;
            if (!nitf_SegmentWriter_attachSource(segmentWriter, segmentSource, &error))
                goto CATCH_ERROR;
        }
    }

    if (record->dataExtensions)
    {
        end = nitf_List_end(record->dataExtensions);
        for (i = 0; i < numDataExtensions; i++)
        {
            nitf_DESegment *DESeg = NULL;
            char *inFile = NULL;
            nitf_IOHandle sourceHandle;

            iter = nitf_List_at(record->dataExtensions, i);
            assert(nitf_ListIterator_notEqualTo(&iter, &end));

            DESeg = (nitf_DESegment *) nitf_ListIterator_get(&iter);
            assert(DESeg);

            segmentWriter = nitf_Writer_newDEWriter(writer, i, &error);
            if (!segmentWriter)
            {
                goto CATCH_ERROR;
            }

            /* setup file */
            inFile = makeBandName(inRootFile, "DE", i, -1);
            sourceHandle =
                nitf_IOHandle_create(inFile, NITF_ACCESS_READONLY,
                                     NITF_OPEN_EXISTING, &error);
            if (NITF_INVALID_HANDLE(sourceHandle))
                goto CATCH_ERROR;

            freeBandName(&inFile);

            segmentSource = nitf_SegmentFileSource_construct(sourceHandle, 0, 0, &error);
            if (!segmentSource)
                goto CATCH_ERROR;
            if (!nitf_SegmentWriter_attachSource(segmentWriter, segmentSource, &error))
                goto CATCH_ERROR;

        }
    }

    if (!nitf_Writer_write(writer, &error))
    {
        goto CATCH_ERROR;
    }


    /*nitf_ImageSource_destruct(&iSource);*/
    nitf_IOHandle_close(output_io);
    nitf_Writer_destruct(&writer);
    return;
CATCH_ERROR:
    nitf_Error_print(&error, stderr, "During write");
    exit(EXIT_FAILURE);
}
Exemplo n.º 19
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);
}
Exemplo n.º 20
0
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;
}
Exemplo n.º 21
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;
}
Exemplo n.º 22
0
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;

}
Exemplo n.º 23
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);
}