Esempio n. 1
0
NITFPRIV(NITF_CLEVEL) checkNumImages(nitf_Record* record, nitf_Error* error)
{
    nitf_Uint32 numi = nitf_Record_getNumImages(record, error);
    return (numi > 20) ? NITF_CLEVEL_05 : NITF_CLEVEL_03;
}
Esempio n. 2
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;

}
Esempio n. 3
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;
}