Exemple #1
0
NITFPRIV(NITF_CLEVEL) checkImageBlock(nitf_ImageSubheader* subhdr,
                                      nitf_Error* error)
{
    int nppbh, nppbv;
    if (!nitf_Field_get(subhdr->NITF_NPPBH, &nppbh, NITF_CONV_INT,
                        sizeof(int), error))
        return NITF_CLEVEL_CHECK_FAILED;


    if (!nitf_Field_get(subhdr->NITF_NPPBV, &nppbv, NITF_CONV_INT,
                        sizeof(int), error))
        return NITF_CLEVEL_CHECK_FAILED;

    if (nppbh <= 0 || nppbv <= 0)
        return NITF_CLEVEL_09;

    if (nppbh <= 2048 && nppbv <= 2048)
        return NITF_CLEVEL_03;

    if (nppbh <= 8192 && nppbv <= 8192)
        return NITF_CLEVEL_05;

    return NITF_CLEVEL_06;

}
Exemple #2
0
NITFPRIV(NITF_CLEVEL) checkImage(nitf_ImageSubheader* subhdr,
                                 nitf_Error* error)
{
    int nrows, ncols;
    if (!nitf_Field_get(subhdr->NITF_NROWS, &nrows, NITF_CONV_INT,
                        sizeof(int), error))
        return NITF_CLEVEL_CHECK_FAILED;


    if (!nitf_Field_get(subhdr->NITF_NCOLS, &ncols, NITF_CONV_INT,
                        sizeof(int), error))
        return NITF_CLEVEL_CHECK_FAILED;

    if (nrows <= 2048 && ncols <= 2048)
        return NITF_CLEVEL_03;

    if (nrows <= 8192 && ncols <= 8192)
        return NITF_CLEVEL_05;

    if (nrows <= 65536 && ncols <= 65536)
        return NITF_CLEVEL_06;

    if (nrows <= 99999999 && ncols <= 99999999)
        return NITF_CLEVEL_07;

    return NITF_CLEVEL_09;

}
Exemple #3
0
NITFPRIV(NITF_CLEVEL) checkRGBImage(nitf_ImageSubheader* subhdr,
                                    nitf_Error* error)
{
    int clevel = NITF_CLEVEL_03;
    int nbands, nbpp;
    char imode = subhdr->imageMode->raw[0];
    if (!nitf_Field_get(subhdr->NITF_NBANDS, &nbands, NITF_CONV_INT,
                        sizeof(int), error))
    {
        return NITF_CLEVEL_CHECK_FAILED;
    }

    if (!nitf_Field_get(subhdr->NITF_NBPP, &nbpp, NITF_CONV_INT,
                        sizeof(int), error))
    {
        return NITF_CLEVEL_CHECK_FAILED;
    }

    if ((memcmp(subhdr->NITF_IC->raw, "C8", 2) == 0 ||
         memcmp(subhdr->NITF_IC->raw, "M8", 2) == 0) &&
        nbpp > 32)
    {
        clevel = NITF_CLEVEL_09;
    }

    if ((memcmp(subhdr->NITF_IC->raw, "C3", 2) == 0 ||
         memcmp(subhdr->NITF_IC->raw, "M3", 2) == 0) &&
        (nbpp > 8 || imode != 'P'))
    {
        clevel = NITF_CLEVEL_09;
    }

    if (nbands != 3)
    {
        clevel = NITF_CLEVEL_09;
    }
    else if (imode != 'B' &&
             imode != 'P' &&
             imode != 'S' &&
             imode != 'R')
    {
        clevel = NITF_CLEVEL_09;
    }
    else
    {
        if (nbpp == 16 || nbpp == 32)
        {
            if (clevel < NITF_CLEVEL_06)
            {
                clevel = NITF_CLEVEL_06;
            }
        }
        else if (nbpp != 8)
        {
            clevel = NITF_CLEVEL_09;
        }
    }

    return clevel;
}
Exemple #4
0
NITFPRIV(NITF_CLEVEL) checkRGBLUTImage(nitf_ImageSubheader* subhdr,
                                       nitf_Error* error)
{
    int clevel = NITF_CLEVEL_03;
    int nbands, nbpp;
    char imode = subhdr->imageMode->raw[0];

    if (!nitf_Field_get(subhdr->NITF_NBANDS, &nbands, NITF_CONV_INT,
                        sizeof(int), error))
        return NITF_CLEVEL_CHECK_FAILED;

    if (!nitf_Field_get(subhdr->NITF_NBPP, &nbpp, NITF_CONV_INT,
                        sizeof(int), error))
        return NITF_CLEVEL_CHECK_FAILED;

    if (memcmp(subhdr->NITF_IC->raw, "NC", 2) != 0 &&
        memcmp(subhdr->NITF_IC->raw, "NM", 2) != 0)
        clevel = NITF_CLEVEL_09;

    if (nbands != 1)
    {
        clevel = NITF_CLEVEL_09;
    }
    else if (nbpp != 1 && nbpp != 8)
    {
        clevel = NITF_CLEVEL_09;
    }
    else if (imode != 'B')
    {
        clevel = NITF_CLEVEL_09;
    }
    return clevel;

}
Exemple #5
0
static int mapped(nitf_TRE* tre, char idx[10][10], int depth, nitf_Error* error)
{
    int npar, npar0;
    nitf_Field* field = nitf_TRE_getField(tre, "NPAR");
    nitf_Field_get(field, &npar, NITF_CONV_INT, sizeof(npar), error);

    field = nitf_TRE_getField(tre, "NPAR0");
    nitf_Field_get(field, &npar0, NITF_CONV_INT, sizeof(npar0), error);
    return ( npar * npar0 );

}
Exemple #6
0
 //! get the value
 void get(NITF_DATA* outval, nitf::ConvType vtype, size_t length) const
 {
     nitf_Error e;
     NITF_BOOL x = nitf_Field_get(getNativeOrThrow(), outval, vtype, length, &e);
     if (!x)
         throw nitf::NITFException(&e);
 }
Exemple #7
0
NITFPRIV(NITF_CLEVEL) checkILOC(nitf_ImageSubheader* subhdr, nitf_Error* error)
{
    char iloc[NITF_ILOC_SZ + 1];
    char num[6];
    int rowCoord, colCoord;
    int lastRow, lastCol;
    int nrows, ncols;

    if (!nitf_Field_get(subhdr->NITF_ILOC, iloc,
                        NITF_CONV_STRING,
                        NITF_ILOC_SZ + 1, error))
        return NITF_CLEVEL_CHECK_FAILED;

    num[5] = '\0';
    memcpy(num, iloc, 5);
    rowCoord = atoi(num);
    memcpy(num, &iloc[5], 5);
    colCoord = atoi(num);

    if (!nitf_Field_get(subhdr->NITF_NROWS, &nrows,
                        NITF_CONV_INT, sizeof(int), error))
        return NITF_CLEVEL_CHECK_FAILED;

    if (!nitf_Field_get(subhdr->NITF_NCOLS, &ncols,
                        NITF_CONV_INT, sizeof(int), error))
        return NITF_CLEVEL_CHECK_FAILED;

    /* The Common Coordinate System Extent ranges are referring to the
     * (inclusive) last row/col */
    lastRow = rowCoord + nrows - 1;
    lastCol = colCoord + ncols - 1;

    if (lastRow <= 2047 && lastCol <= 2047)
        return NITF_CLEVEL_03;

    if (lastRow <= 8191 && lastCol <= 8191)
        return NITF_CLEVEL_05;

    if (lastRow <= 65535 && lastCol <= 65535)
        return NITF_CLEVEL_06;

    if (lastRow <= 99999999 && lastCol <= 99999999)
        return NITF_CLEVEL_07;

    return NITF_CLEVEL_09;
}
Exemple #8
0
NITFPRIV(NITF_CLEVEL) checkMonoImage(nitf_ImageSubheader* subhdr,
                                     nitf_Error* error)
{
    int clevel = NITF_CLEVEL_03;
    int nbands, nbpp;
    char imode = subhdr->imageMode->raw[0];

    if (!nitf_Field_get(subhdr->NITF_NBANDS, &nbands, NITF_CONV_INT,
                        sizeof(int), error))
        return NITF_CLEVEL_CHECK_FAILED;

    if (!nitf_Field_get(subhdr->NITF_NBPP, &nbpp, NITF_CONV_INT,
                        sizeof(int), error))
        return NITF_CLEVEL_CHECK_FAILED;

    if ( (memcmp(subhdr->NITF_IC->raw, "C3", 2) == 0) ||
         (memcmp(subhdr->NITF_IC->raw, "M3", 2) == 0) )
    {
        if (nbpp != 8 && nbpp != 12)
            clevel = NITF_CLEVEL_09;
    }

    if (nbands != 1)
    {
        clevel = NITF_CLEVEL_09;
    }
    else if (nbpp != 1 &&
             nbpp != 8 &&
             nbpp != 12 &&
             nbpp != 16 &&
             nbpp != 32 &&
             nbpp != 64)
    {
        clevel = NITF_CLEVEL_09;
    }
    else if (imode != 'B')
    {
        clevel = NITF_CLEVEL_09;
    }
    return clevel;
}
Exemple #9
0
NITF_CXX_GUARD

static int numopg(nitf_TRE* tre, char idx[10][10], int depth, nitf_Error* error)
{
    nitf_Field* field;
    int numopg, x;
    char fname[64];
    strcpy(fname, "NUMOPG");
    x = strlen(idx[0]);

    strcat(fname, idx[0]);

    field = nitf_TRE_getField(tre, fname);
    nitf_Field_get(field, &numopg, NITF_CONV_INT, sizeof(numopg), error);
    return ( ((numopg + 1) * numopg) / 2 );

}
Exemple #10
0
NITFPRIV(NITF_CLEVEL) checkFileSize(nitf_Record* record,
                                    nitf_Error* error)
{
    nitf_Int64 fl;
    if (!nitf_Field_get(record->header->NITF_FL, &fl, NITF_CONV_INT, 8, error))
        return NITF_CLEVEL_CHECK_FAILED;

    if (fl <= 52428799)
        return NITF_CLEVEL_03;

    if (fl <= 1073741823)
        return NITF_CLEVEL_05;

    if (fl <= 2147483647)
        return NITF_CLEVEL_06;

    if (fl <= NITF_INT64(10737418239))
        return NITF_CLEVEL_07;

    return NITF_CLEVEL_09;

}
Exemple #11
0
NITFPRIV(int) nitf_TRECursor_evalIf(nitf_TRE* tre,
                                   nitf_TREDescription* desc_ptr,
                                   char idx_str[10][10],
                                   int looping,
                                   nitf_Error* error)
{
    nitf_Field *field;
    nitf_Pair *pair;

    /* temp buf for the label */
    char str[TAG_BUF_LEN];
    char *emptyPtr;
    char *op;
    char *valPtr;

    /* the return status */
    int status = 0;

    /* used as the value for comparing */
    int fieldData;

    /* the value defined int the TRE descrip */
    int treData;

    /* the bit-field for comparing */
    unsigned int bitFieldData;

    /* the bit-field defined in the TRE descrip */
    unsigned int treBitField;

    /* get the data out of the hashtable */
    pair = nitf_TRECursor_getTREPair(tre, desc_ptr->tag, idx_str,
                                     looping, error);
    if (!pair)
    {
        nitf_Error_init(error, "Unable to find tag in TRE hash",
                        NITF_CTXT, NITF_ERR_UNK);
        return NITF_FAILURE;
    }
    field = (nitf_Field *) pair->data;
    assert(strlen(desc_ptr->label) < sizeof(str));

    strcpy(str, desc_ptr->label);
    op = str;

    while (isspace(*op))
        op++;

    /* split the string at the space */
    emptyPtr = strchr(op, ' ');
    *emptyPtr = 0;

    /* remember where the operand is */
    valPtr = emptyPtr + 1;

    /* check if it is a string comparison of either 'eq' or 'ne' */
    if ((strcmp(op, "eq") == 0) || (strcmp(op, "ne") == 0))
    {
        /* must be a string */
        if (field->type == NITF_BCS_N)
        {
            nitf_Error_init(error,
                            "evaluate: can't use eq/ne to compare a number",
                            NITF_CTXT, NITF_ERR_INVALID_PARAMETER);
            return NITF_FAILURE;
        }
        status = strncmp(field->raw, valPtr, field->length);
        status = strcmp(op, "eq") == 0 ? !status : status;
    }
    /* check if it is a logical operator for ints */
    else if ((strcmp(op, "<") == 0) ||
             (strcmp(op, ">") == 0) ||
             (strcmp(op, ">=") == 0) ||
             (strcmp(op, "<=") == 0) ||
             (strcmp(op, "==") == 0) || (strcmp(op, "!=") == 0))
    {
        /* make sure it is a number */
        if (field->type != NITF_BCS_N)
        {
            nitf_Error_init(error,
                            "evaluate: can't use strings for logical expressions",
                            NITF_CTXT, NITF_ERR_INVALID_PARAMETER);
            return NITF_FAILURE;
        }

        treData = NITF_ATO32(valPtr);
        if (!nitf_Field_get
                (field, (char *) &fieldData, NITF_CONV_INT, sizeof(fieldData),
                 error))
        {
            return NITF_FAILURE;
        }

        /* 0 -> equal, <0 -> less true, >0 greater true */
        status = fieldData - treData;

        if (strcmp(op, ">") == 0)
            status = (status > 0);
        else if (strcmp(op, "<") == 0)
            status = (status < 0);
        else if (strcmp(op, ">=") == 0)
            status = (status >= 0);
        else if (strcmp(op, "<=") == 0)
            status = (status <= 0);
        else if (strcmp(op, "==") == 0)
            status = (status == 0);
        else if (strcmp(op, "!=") == 0)
            status = (status != 0);
    }
    /* check if it is a bit-wise operator */
    else if (strcmp(op, "&") == 0)
    {
        /* make sure it is a binary field */
        if (field->type != NITF_BINARY)
        {
            nitf_Error_init(error,
                            "evaluate: must use binary data for bit-wise expressions",
                            NITF_CTXT,
                            NITF_ERR_INVALID_PARAMETER);
            return NITF_FAILURE;
        }

        treBitField = NITF_ATOU32_BASE(valPtr, 0);
        if (!nitf_Field_get(field,
                            (char *)&bitFieldData,
                            NITF_CONV_UINT,
                            sizeof(bitFieldData),
                            error))
        {
            return NITF_FAILURE;
        }

        /* check this bit field */
        status = ((treBitField & bitFieldData) != 0);
    }
    /* otherwise, they used a bad operator */
    else
    {
        nitf_Error_init(error, "evaluate: invalid comparison operator",
                        NITF_CTXT, NITF_ERR_INVALID_PARAMETER);
        return NITF_FAILURE;
    }
    return status;
}
Exemple #12
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;

}
Exemple #13
0
NITFPRIV(int) nitf_TRECursor_evaluatePostfix(nitf_TRE *tre,
                                             char idx[10][10],
                                             int looping,
                                             char *expression,
                                             nitf_Error *error)
{
    nitf_List *parts = NULL;
    nitf_IntStack *stack = NULL;
    int expressionValue;

    /* create the postfix stack */
    stack = nitf_IntStack_construct(error);
    if (!stack)
        goto CATCH_ERROR;

    /* split the expression by spaces */
    parts = nitf_Utils_splitString(expression, 0, error);
    if (!parts)
        goto CATCH_ERROR;

    while(!nitf_List_isEmpty(parts))
    {
        char* expr = (char*) nitf_List_popFront(parts);
        if (strlen(expr) == 1 &&
                (expr[0] == '+' || expr[0] == '-' || expr[0] == '*' ||
                        expr[0] == '/' || expr[0] == '%'))
        {
            int op1, op2, stackSize;
            stackSize = nitf_IntStack_depth(stack, error) + 1;

            if (stackSize == 0)
            {
                /* error for postfix... */
                nitf_Error_init(error,
                        "nitf_TRECursor_evaluatePostfix: invalid expression",
                        NITF_CTXT, NITF_ERR_INVALID_PARAMETER);
                goto CATCH_ERROR;
            }

            op2 = nitf_IntStack_pop(stack, error);
            if (stackSize == 1)
                op1 = 0; /* assume 0 for the first operand of a unary op */
            else
                op1 = nitf_IntStack_pop(stack, error);

            switch(expr[0])
            {
            case '+':
                nitf_IntStack_push(stack, (op1 + op2), error);
                break;
            case '-':
                nitf_IntStack_push(stack, (op1 - op2), error);
                break;
            case '*':
                nitf_IntStack_push(stack, (op1 * op2), error);
                break;
            case '/':
                /* check for divide by zero */
                if (op2 == 0)
                {
                    nitf_Error_init(error,
                            "nitf_TRECursor_evaluatePostfix: attempt to divide by zero",
                            NITF_CTXT, NITF_ERR_INVALID_PARAMETER);
                    goto CATCH_ERROR;
                }
                nitf_IntStack_push(stack, (op1 / op2), error);
                break;
            case '%':
                nitf_IntStack_push(stack, (op1 % op2), error);
                break;
            }
        }
        else
        {
            /* evaluate as an integer and push onto the stack */
            if (nitf_Utils_isNumeric(expr))
            {
                nitf_IntStack_push(stack, NITF_ATO32(expr), error);
            }
            else
            {
                /* must be a dependent field */
                int intVal;
                nitf_Field *field = NULL;
                nitf_Pair *pair = nitf_TRECursor_getTREPair(tre, expr, idx,
                        looping, error);

                if (!pair)
                {
                    nitf_Error_init(error,
                            "nitf_TRECursor_evaluatePostfix: invalid TRE field reference",
                            NITF_CTXT, NITF_ERR_INVALID_PARAMETER);
                    goto CATCH_ERROR;
                }
                field = (nitf_Field *) pair->data;

                /* get the int value */
                if (!nitf_Field_get(field, (char*) &intVal, NITF_CONV_INT,
                         sizeof(intVal), error))
                {
                    goto CATCH_ERROR;
                }
                nitf_IntStack_push(stack, intVal, error);
            }
        }

        /* must cleanup after ourselves */
        if (expr)
            NITF_FREE(expr);
    }

    /* if all is well, the postfix stack should have one value */
    if (nitf_IntStack_depth(stack, error) != 0)
    {
        nitf_Error_init(error, "Invalid postfix expression",
                NITF_CTXT, NITF_ERR_INVALID_PARAMETER);
        goto CATCH_ERROR;
    }

    expressionValue = nitf_IntStack_pop(stack, error);

    nitf_IntStack_destruct(&stack);
    nitf_List_destruct(&parts);

    return expressionValue;

  CATCH_ERROR:
    if (stack) nitf_IntStack_destruct(&stack);
    if (parts) nitf_List_destruct(&parts);
    return -1;
}
Exemple #14
0
/**
 * Helper function for evaluating loops
 * Returns the number of loops that will be processed
 */
NITFPRIV(int) nitf_TRECursor_evalCondLength(nitf_TRE* tre,
                                           nitf_TREDescription* desc_ptr,
                                           char idx_str[10][10],
                                           int looping,
                                           nitf_Error* error)
{
    int computedLength;
    /* temp buf used for manipulating the loop label */
    char str[TAG_BUF_LEN];
    nitf_Pair *pair;
    nitf_Field *field;

    char *op;
    char *valPtr;

    /* used for the possible data in the description label */
    int funcVal;

    pair = nitf_TRECursor_getTREPair(tre, desc_ptr->tag,
                                     idx_str, looping, error);
    if (!pair)
    {
        nitf_Error_init(error,
                        "nitf_TRECursor_evalCondLength: invalid TRE reference",
                        NITF_CTXT, NITF_ERR_INVALID_PARAMETER);
        return NITF_FAILURE;
    }
    field = (nitf_Field *) pair->data;

    /* get the int value */
    if (!nitf_Field_get
            (field, (char *) &computedLength, NITF_CONV_INT,
             sizeof(computedLength), error))
    {
        return NITF_FAILURE;
    }

    /* if the label is not empty, then apply some functionality */
    if (desc_ptr->label && strlen(desc_ptr->label) != 0)
    {
        assert(strlen(desc_ptr->label) < sizeof(str));

        strcpy(str, desc_ptr->label);
        op = str;
        while (isspace(*op))
            op++;
        if ((*op == '+') ||
                (*op == '-') || (*op == '*') || (*op == '/') || (*op == '%'))
        {
            valPtr = op + 1;
            while (isspace(*valPtr))
                valPtr++;

            funcVal = NITF_ATO32(valPtr);

            switch (*op)
            {
                case '+':
                    computedLength += funcVal;
                    break;
                case '-':
                    computedLength -= funcVal;
                    break;
                case '*':
                    computedLength *= funcVal;
                    break;
                case '/':
                    /* check for divide by zero */
                    if (funcVal == 0)
                    {
                        nitf_Error_init(error,
                                        "nitf_TRECursor_evalCondLength: attempt to divide by zero",
                                        NITF_CTXT, NITF_ERR_INVALID_PARAMETER);
                        return NITF_FAILURE;
                    }
                    computedLength /= funcVal;
                    break;
                case '%':
                    computedLength %= funcVal;
                    break;
                default:
                    break;
            }
        }
        else
        {
            nitf_Error_init(error,
                            "nitf_TRECursor_evalCondLength: invalid operator",
                            NITF_CTXT, NITF_ERR_INVALID_PARAMETER);
            return NITF_FAILURE;
        }
    }
    return computedLength < 0 ? 0 : computedLength;
}
Exemple #15
0
int main(int argc, char *argv[])
{
    nitf_FileHeader *fhdr;          /* File header supplying fields */
    nitf_ImageSubheader *subhdr;    /* Subheader supplying fields */
    nitf_Uint32 valueU32Before;     /* Value buffer */
    nitf_Uint32 valueU32After;      /* Value buffer */
    nitf_Uint64 valueU64Before;     /* Value buffer */
    nitf_Uint64 valueU64After;      /* Value buffer */
    char *valueStrBefore;           /* Value buffer */
    /* Value buffer */
    static char valueStrAfter[STR_LEN + 2];
    nitf_Uint32 valueStrLen;        /* Value buffer */
    static nitf_Error errorObj;     /* Error object for messages */
    nitf_Error *error;              /* Pointer to the error object */

    error = &errorObj;

    fhdr = nitf_FileHeader_construct(error);
    if (fhdr == NULL)
    {
        nitf_Error_print(error, stdout, "Error creating image subheader");
        exit(EXIT_FAILURE);
    }

    subhdr = nitf_ImageSubheader_construct(error);
    if (subhdr == NULL)
    {
        nitf_Error_print(error, stdout, "Error creating image subheader");
        exit(EXIT_FAILURE);
    }

    /*    Set some fields (should work) */
    valueU32Before = 12345;
    if (nitf_Field_setUint32(subhdr->NITF_XBANDS, valueU32Before, error))
        fprintf(stdout,
                "Set of XBANDS via nitf_Field_setUint32 worked as expected\n");
    else
        nitf_Error_print(error, stdout,
                         "Unexpected error setting XBANDS via nitf_Field_setUint32");

    nitf_Field_get(subhdr->NITF_XBANDS, (NITF_DATA *) & valueU32After,
                   NITF_CONV_UINT, NITF_INT32_SZ, error);
    fprintf(stdout,
            "Set of XBANDS via nitf_Field_setUint32 original %d readback %d\n",
            valueU32Before, valueU32After);

    valueU32Before = 1234;
    if (nitf_Field_setUint32(subhdr->NITF_COMRAT, valueU32Before, error))
        fprintf(stdout,
                "Set of COMRAT via nitf_Field_setUint32 worked as expected\n");
    else
        nitf_Error_print(error, stdout,
                         "Unexpected error setting COMRAT via nitf_Field_setUint32");

    nitf_Field_get(subhdr->NITF_COMRAT, (NITF_DATA *) & valueU32After,
                   NITF_CONV_UINT, NITF_INT32_SZ, error);
    fprintf(stdout,
            "Set of COMRAT via nitf_Field_setUint32 original %d readback %d\n",
            valueU32Before, valueU32After);

    valueU64Before = 11234567890ll;
    if (nitf_Field_setUint64(fhdr->NITF_FL, valueU64Before, error))
        fprintf(stdout,
                "Set of FL via nitf_Field_setUint64 worked as expected\n");
    else
        nitf_Error_print(error, stdout,
                         "Unexpected error setting FL via nitf_Field_setUint64");

    nitf_Field_get(fhdr->NITF_FL, (NITF_DATA *) & valueU64After,
                   NITF_CONV_UINT, NITF_INT64_SZ, error);
    fprintf(stdout,
            "Set of FL via nitf_Field_setUint64 original %llu readback %llu\n",
            valueU64Before, valueU64After);

    valueStrBefore = "TestStr";
    if (nitf_Field_setString(subhdr->NITF_IID2, valueStrBefore, error))
        fprintf(stdout,
                "Set of IID2 via nitf_Field_setString worked as expected\n");
    else
        nitf_Error_print(error, stdout,
                         "Unexpected error setting IID2 via nitf_Field_setString");

    nitf_Field_get(subhdr->NITF_IID2, (NITF_DATA *) valueStrAfter,
                   NITF_CONV_STRING, STR_LEN, error);
    fprintf(stdout,
            "Set of IID2 via nitf_Field_setString original %s readback %s\n",
            valueStrBefore, valueStrAfter);

    valueStrBefore = "1234";
    if (nitf_Field_setString(subhdr->NITF_NCOLS, valueStrBefore, error))
        fprintf(stdout,
                "Set of NCOLS via nitf_Field_setString worked as expected\n");
    else
        nitf_Error_print(error, stdout,
                         "Unexpected error setting NCOLS via nitf_Field_setString");

    nitf_Field_get(subhdr->NITF_NCOLS, (NITF_DATA *) valueStrAfter,
                   NITF_CONV_STRING, STR_LEN, error);
    fprintf(stdout,
            "Set of NCOLS via nitf_Field_setString original %s readback %s\n",
            valueStrBefore, valueStrAfter);

    /*    Set some fields (should fail) */

    valueU32Before = 123;
    if (nitf_Field_setUint32(fhdr->NITF_FBKGC, valueU32Before, error))
        fprintf(stdout,
                "Set of FBKGC via nitf_Field_setUnit32 worked expected error\n");
    else
        nitf_Error_print(error, stdout,
                         "Expected error setting FBKGC via nitf_Field_setUint32");

    valueStrBefore = "123";
    if (nitf_Field_setString(fhdr->NITF_FBKGC, valueStrBefore, error))
        fprintf(stdout,
                "Set of FBKGC via nitf_Field_setString worked expected error\n");
    else
        nitf_Error_print(error, stdout,
                         "Expected error setting FBKGC via nitf_Field_setString");

    valueStrBefore = "12g";
    if (nitf_Field_setString(subhdr->NITF_NCOLS, valueStrBefore, error))
        fprintf(stdout,
                "Set of NCOLS via nitf_Field_setString worked expected error\n");
    else
        nitf_Error_print(error, stdout,
                         "Expected error setting NCOLS via nitf_Field_setString");

    valueU32Before = 3;
    if (nitf_Field_setUint32(subhdr->NITF_NBANDS, valueU32Before, error))
        fprintf(stdout,
                "Set of NBANDS via nitf_Field_setUint32 worked expected error\n");
    else
        nitf_Error_print(error, stdout,
                         "Expected error setting NBANDS via nitf_Field_setUint32");

    return 0;

}
Exemple #16
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);
}
Exemple #17
0
/**
 * Helper function for evaluating loops
 * Returns the number of loops that will be processed
 */
NITFPRIV(int) nitf_TRECursor_evalLoops(nitf_TRE* tre,
                                      nitf_TREDescription* desc_ptr,
                                      char idx_str[10][10],
                                      int looping, nitf_Error* error)
{
    int loops;

    /* temp buf used for manipulating the loop label */
    char str[TAG_BUF_LEN];

    nitf_Pair *pair;
    nitf_Field *field;

    char *op;
    char *valPtr;

    /* used for the possible data in the loop label */
    int loopVal;

    /* if the user wants a constant value */
    if (desc_ptr->label && strcmp(desc_ptr->label, NITF_CONST_N) == 0)
    {
        loops = NITF_ATO32(desc_ptr->tag);
    }

    else if (desc_ptr->label && strcmp(desc_ptr->label, NITF_FUNCTION) == 0)
    {
        NITF_TRE_CURSOR_COUNT_FUNCTION fn =
            (NITF_TRE_CURSOR_COUNT_FUNCTION)desc_ptr->tag;


        loops = (*fn)(tre, idx_str, looping, error);

        if (loops == -1)
            return NITF_FAILURE;
    }

    else
    {
        pair = nitf_TRECursor_getTREPair(tre, desc_ptr->tag,
                                         idx_str, looping, error);
        if (!pair)
        {
            nitf_Error_init(error,
                            "nitf_TRECursor_evalLoops: invalid TRE loop counter",
                            NITF_CTXT, NITF_ERR_INVALID_PARAMETER);
            return NITF_FAILURE;
        }
        field = (nitf_Field *) pair->data;

        /* get the int value */
        if (!nitf_Field_get
                (field, (char *) &loops, NITF_CONV_INT, sizeof(loops), error))
        {
            return NITF_FAILURE;
        }

        /* if the label is not empty, then apply some functionality */
        if (desc_ptr->label && strlen(desc_ptr->label) != 0)
        {
            assert(strlen(desc_ptr->label) < sizeof(str));

            strcpy(str, desc_ptr->label);
            op = str;
            while (isspace(*op))
                op++;

            if ((*op == '+') ||
                    (*op == '-') ||
                    (*op == '*') || (*op == '/') || (*op == '%'))
            {
                valPtr = op + 1;
                while (isspace(*valPtr))
                    valPtr++;

                loopVal = NITF_ATO32(valPtr);

                switch (*op)
                {
                    case '+':
                        loops += loopVal;
                        break;
                    case '-':
                        loops -= loopVal;
                        break;
                    case '*':
                        loops *= loopVal;
                        break;
                    case '/':
                        /* check for divide by zero */
                        if (loopVal == 0)
                        {
                            nitf_Error_init(error,
                                            "nitf_TRECursor_evalLoops: attempt to divide by zero",
                                            NITF_CTXT,
                                            NITF_ERR_INVALID_PARAMETER);
                            return NITF_FAILURE;
                        }
                        loops /= loopVal;
                        break;
                    case '%':
                        loops %= loopVal;
                        break;
                    default:
                        break;
                }
            }
            else
            {
                nitf_Error_init(error, "nitf_TRECursor_evalLoops: invalid operator",
                                NITF_CTXT, NITF_ERR_INVALID_PARAMETER);
                return NITF_FAILURE;
            }
        }
    }
    return loops < 0 ? 0 : loops;
}
Exemple #18
0
NITFPRIV(NITF_CLEVEL) checkMultiImage(nitf_ImageSubheader* subhdr,
                                      nitf_Error* error)
{
    int clevel = NITF_CLEVEL_03;
    int nbands, nbpp;
    char imode = subhdr->imageMode->raw[0];

    if (!nitf_Field_get(subhdr->NITF_NBANDS, &nbands, NITF_CONV_INT,
                        sizeof(int), error))
        return NITF_CLEVEL_CHECK_FAILED;

    if (!nitf_Field_get(subhdr->NITF_NBPP, &nbpp, NITF_CONV_INT,
                        sizeof(int), error))
        return NITF_CLEVEL_CHECK_FAILED;

    /*  Note that the rest of this check is covered below */
    if (memcmp(subhdr->NITF_IC->raw, "C8", 2) == 0 ||
        memcmp(subhdr->NITF_IC->raw, "M8", 2) == 0)
    {
        if (imode == 'B' || nbpp > 32)
            clevel = NITF_CLEVEL_09;

    }
    /*  Normal JPEG */
    else if (memcmp(subhdr->NITF_IC->raw, "C3", 2) == 0 ||
             memcmp(subhdr->NITF_IC->raw, "M3", 2) == 0)
    {
        if (nbpp != 8 && nbpp != 12)
            return NITF_CLEVEL_09;
    }

    if (nbands < 2)
    {
        clevel = NITF_CLEVEL_09;
    }
    else if (nbands < 10)
    {
        if (clevel < NITF_CLEVEL_03)
            clevel = NITF_CLEVEL_03;
    }
    else
    {
        if (nbands <= 255)
        {
            if (clevel < NITF_CLEVEL_06)
                clevel = NITF_CLEVEL_06;
        }
        else if (nbands <= 999)
        {
            if (clevel < NITF_CLEVEL_07)
                clevel = NITF_CLEVEL_07;
        }
        else clevel = NITF_CLEVEL_09;


    }

    if (nbpp != 8 &&
        nbpp != 16 &&
        nbpp != 32 &&
        nbpp != 64)
    {

        clevel = NITF_CLEVEL_09;
    }
    else if (imode != 'B')
    {
        clevel = NITF_CLEVEL_09;
    }
    return clevel;
}
Exemple #19
0
NITF_BOOL readImageSegment(imgInfo *img, nitf_Reader *reader, nitf_Error *error)
{
    nitf_Uint32 nBits;         /* Bits/pixel */
    nitf_Uint32 nBands;        /* Number of bands */
    nitf_Uint32 xBands;        /* Number of extended bands */
    nitf_Uint32 nRows;         /* Number of rows */
    nitf_Uint32 nColumns;      /* Number of columns */
    size_t subimageSize;       /* Image band size in bytes */
    nitf_SubWindow *subimage;  /* Sub-image object specifying full image */
    nitf_DownSampler *pixelSkip; /* Downsample for sub-window */
    nitf_Uint32 *bandList;     /* List of bands for read */
    nitf_Uint32 band;          /* Current band */
    nitf_Uint8 **buffers;      /* Read buffer one/band */
    /* Image reader */
    nitf_ImageReader *deserializer;
    int padded;                /* Argument for read */
    int i;


    /*  Get dimension and band info */

    nitf_Field_get(img->subhdr->numBitsPerPixel,
                   &nBits, NITF_CONV_UINT, NITF_INT32_SZ, error);
    nitf_Field_get(img->subhdr->numImageBands,
                   &nBands, NITF_CONV_UINT, NITF_INT32_SZ, error);
    nitf_Field_get(img->subhdr->numMultispectralImageBands,
                   &xBands, NITF_CONV_UINT, NITF_INT32_SZ, error);
    nBands += xBands;
    nitf_Field_get(img->subhdr->numRows,
                   &nRows, NITF_CONV_UINT, NITF_INT32_SZ, error);
    nitf_Field_get(img->subhdr->numCols,
                   &nColumns, NITF_CONV_UINT, NITF_INT32_SZ, error);
    img->bytes = NITF_NBPP_TO_BYTES(nBits);
    subimageSize = nRows * nColumns * img->bytes;
    img->imgSize = subimageSize;


    /*  Setup sub-window */

    subimage = nitf_SubWindow_construct(error);
    if (subimage == NULL)
        return(NITF_FAILURE);

    bandList = (nitf_Uint32 *) NITF_MALLOC(sizeof(nitf_Uint32 *) * nBands);
    if (bandList == NULL)
        return(NITF_FAILURE);

    subimage->startCol = 0;
    subimage->startRow = 0;
    subimage->numRows = nRows;
    subimage->numCols = nColumns;
    for (band = 0;band < nBands;band++)
        bandList[band] = band;
    subimage->bandList = bandList;
    subimage->numBands = nBands;

    pixelSkip = nitf_PixelSkip_construct(1, 1, error);
    if (pixelSkip == NULL)
        return(NITF_FAILURE);

    if (!nitf_SubWindow_setDownSampler(subimage, pixelSkip, error))
        return(NITF_FAILURE);

    /*  Set-up buffers (one/band) */

    buffers = (nitf_Uint8 **) NITF_MALLOC(nBands * sizeof(nitf_Uint8*));
    if (buffers == NULL)
        return(NITF_FAILURE);

    for (i = 0;i < nBands;i++)
    {
        buffers[i] = (nitf_Uint8 *) malloc(subimageSize);
        if (buffers[i] == NULL)
            return(NITF_FAILURE);
    }

    deserializer = nitf_Reader_newImageReader(reader, img->index, error);
    if (deserializer == NULL)
        return(NITF_FAILURE);

    if (!nitf_ImageReader_read(deserializer, subimage, buffers, &padded, error))
        return(NITF_FAILURE);

    img->nBands = nBands;
    img->buffers = buffers;
    return(NITF_SUCCESS);
}
Exemple #20
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;
}
Exemple #21
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;
}
Exemple #22
0
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;

}
Exemple #23
0
NITFAPI(char *) nitf_TREUtils_getRawData(nitf_TRE * tre, nitf_Uint32* treLength, nitf_Error * error)
{
    int status = 1;
    int offset = 0;
    nitf_Uint32 length;
    int tempLength;

    /* data buffer - Caller must free this */
    char *data = NULL;

    /* temp data buffer */
    char *tempBuf = NULL;

    /* temp nitf_Pair */
    nitf_Pair *pair;
    
    /* temp nitf_Field */
    nitf_Field *field;

    /* the cursor */
    nitf_TRECursor cursor;

    /* get actual length of TRE */
    length = nitf_TREUtils_computeLength(tre);
    *treLength = length;

    if (length <= 0)
    {
        nitf_Error_init(error, "TRE has invalid length",
                NITF_CTXT, NITF_ERR_INVALID_OBJECT);
        return NULL;
    }

    /* allocate the memory - this does not get freed in this function */
    data = (char *) NITF_MALLOC(length + 1);
    if (!data)
    {
        nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO),
                NITF_CTXT, NITF_ERR_MEMORY);
        goto CATCH_ERROR;
    }
    memset(data, 0, length + 1);

    cursor = nitf_TRECursor_begin(tre);
    while (!nitf_TRECursor_isDone(&cursor) && status && offset < length)
    {
        if (nitf_TRECursor_iterate(&cursor, error) == NITF_SUCCESS)
        {
            pair = nitf_HashTable_find(((nitf_TREPrivateData*)tre->priv)->hash,
                    cursor.tag_str);
            if (pair && pair->data)
            {
                tempLength = cursor.length;
                if (tempLength == NITF_TRE_GOBBLE)
                {
                    tempLength = length - offset;
                }
                field = (nitf_Field *) pair->data;

                /* get the raw data */
                tempBuf = NITF_MALLOC(tempLength);
                if (!tempBuf)
                {
                    nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO),
                            NITF_CTXT, NITF_ERR_MEMORY);
                    goto CATCH_ERROR;
                }
                /* get the data as raw buf */
                nitf_Field_get(field, (NITF_DATA *) tempBuf,
                        NITF_CONV_RAW, tempLength, error);

                /* first, check to see if we need to swap bytes */
                if (field->type == NITF_BINARY)
                {
                    if (tempLength == NITF_INT16_SZ)
                    {
                        nitf_Int16 int16 =
                            (nitf_Int16)NITF_HTONS(*((nitf_Int16 *) tempBuf));
                        memcpy(tempBuf, (char*)&int16, tempLength);
                    }
                    else if (tempLength == NITF_INT32_SZ)
                    {
                        nitf_Int32 int32 =
                            (nitf_Int32)NITF_HTONL(*((nitf_Int32 *) tempBuf));
                        memcpy(tempBuf, (char*)&int32, tempLength);
                    }
                    else
                    {
                        /* TODO what to do??? 8 bit is ok, but what about 64? */
                        /* for now, just let it go through... */
                    }
                }

                /* now, memcpy the data */
                memcpy(data + offset, tempBuf, tempLength);
                offset += tempLength;

                /* free the buf */
                NITF_FREE(tempBuf);
            }
            else
            {
                nitf_Error_init(error,
                "Failed due to missing TRE field(s)",
                NITF_CTXT, NITF_ERR_INVALID_OBJECT);
                goto CATCH_ERROR;
            }
        }
    }
    nitf_TRECursor_cleanup(&cursor);
    return data;

  /* deal with errors here */
  CATCH_ERROR:
    if (data)
        NITF_FREE(data);
    return NULL;
}
Exemple #24
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);
}
Exemple #25
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);
}