NITFAPI(nitf_SegmentSource *) nitf_SegmentFileSource_construct ( nitf_IOHandle handle, nitf_Off start, int byteSkip, nitf_Error * error ) { static nitf_IDataSource iFileSource = { &FileSource_read, &FileSource_destruct, &FileSource_getSize, &FileSource_setSize }; FileSourceImpl *impl = NULL; nitf_SegmentSource *segmentSource = NULL; impl = (FileSourceImpl *) NITF_MALLOC(sizeof(FileSourceImpl)); if (!impl) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } if (!(impl->io = nitf_IOHandleAdapter_construct(handle, NRT_ACCESS_READONLY, error))) return NULL; impl->byteSkip = byteSkip >= 0 ? byteSkip : 0; impl->mark = impl->start = (start >= 0 ? start : 0); impl->fileSize = nitf_IOInterface_getSize(impl->io, error); if (!NITF_IO_SUCCESS(impl->fileSize)) { NITF_FREE(impl); return NULL; } /* figure out the actual # oif bytes represented by the source */ impl->size = impl->fileSize / (impl->byteSkip + 1); segmentSource = (nitf_SegmentSource *) NITF_MALLOC(sizeof(nitf_SegmentSource)); if (!segmentSource) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } segmentSource->data = impl; segmentSource->iface = &iFileSource; return segmentSource; }
nitf_RESubheader_clone(nitf_RESubheader * source, nitf_Error * error) { nitf_Uint32 subLen; nitf_RESubheader *subhdr = NULL; if (source) { subhdr = (nitf_RESubheader *) NITF_MALLOC(sizeof(nitf_RESubheader)); if (!subhdr) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } subhdr->securityGroup = nitf_FileSecurity_clone(source->securityGroup, error); if (!subhdr->securityGroup) goto CATCH_ERROR; /* Copy some fields */ _NITF_CLONE_FIELD(subhdr, source, NITF_RE); _NITF_CLONE_FIELD(subhdr, source, NITF_RESTAG); _NITF_CLONE_FIELD(subhdr, source, NITF_RESVER); _NITF_CLONE_FIELD(subhdr, source, NITF_RESCLAS); /* And some ints */ _NITF_CLONE_FIELD(subhdr, source, NITF_RESSHL); subhdr->dataLength = source->dataLength; subhdr->subheaderFields = NULL; NITF_TRY_GET_UINT32(source->subheaderFieldsLength, &subLen, error); if (source->subheaderFields) { subhdr->subheaderFields = (char* )NITF_MALLOC(subLen); if (!subhdr->subheaderFields) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } memcpy(subhdr->subheaderFields, source->subheaderFields, subLen); } return subhdr; } CATCH_ERROR: return NULL; }
NITFAPI(nitf_SegmentSource *) nitf_SegmentReaderSource_construct ( nitf_SegmentReader *reader, nitf_Error * error ) { static nitf_IDataSource iSource = { &SegmentReader_read, &SegmentReader_destruct, &SegmentReader_getSize, &SegmentReader_setSize }; nitf_SegmentSource *segmentSource = NULL; segmentSource = (nitf_SegmentSource *) NITF_MALLOC(sizeof(nitf_SegmentSource)); if (!segmentSource) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } segmentSource->data = reader; segmentSource->iface = &iSource; return segmentSource; }
NITFAPI(cgm_Picture*) cgm_Picture_construct(const char* name, nitf_Error* error) { cgm_Picture* picture = (cgm_Picture*)NITF_MALLOC(sizeof(cgm_Picture)); if (!picture) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } picture->name = NULL; picture->colorSelectionMode = CGM_DIRECT; picture->edgeWidthSpec = CGM_ABSOLUTE; picture->lineWidthSpec = CGM_ABSOLUTE; picture->vdcExtent = NULL; picture->body = NULL; if (name) { picture->name = (char*)NITF_MALLOC( strlen( name ) + 1 ); strcpy(picture->name, name); } return picture; }
NITFAPI(nitf_TREPrivateData *) nitf_TREPrivateData_construct( nitf_Error * error) { nitf_TREPrivateData *priv = (nitf_TREPrivateData*) NITF_MALLOC( sizeof(nitf_TREPrivateData)); if (!priv) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } priv->length = 0; priv->descriptionName = NULL; priv->description = NULL; priv->userData = NULL; /* create the hashtable for the fields */ priv->hash = nitf_HashTable_construct(NITF_TRE_HASH_SIZE, error); if (!priv->hash) { nitf_TREPrivateData_destruct(&priv); return NULL; } /* ??? change policy? */ nitf_HashTable_setPolicy(priv->hash, NITF_DATA_ADOPT); return priv; }
NITFPRIV(NITF_BOOL) defaultInit(nitf_TRE* tre, const char* id, nitf_Error * error) { nitf_TREDescription* descr; /* create a new private data struct */ tre->priv = nitf_TREPrivateData_construct(error); if (!tre->priv) return NITF_FAILURE; descr = (nitf_TREDescription *) NITF_MALLOC(2 * sizeof(nitf_TREDescription)); if (!descr) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NITF_FAILURE; } descr[0].data_type = NITF_BINARY; descr[0].data_count = NITF_TRE_GOBBLE; descr[0].label = _NITF_DEFAULT_TRE_LABEL; descr[0].tag = NITF_TRE_RAW; descr[1].data_type = NITF_END; descr[1].data_count = 0; descr[1].label = NULL; descr[1].tag = NULL; ((nitf_TREPrivateData*)tre->priv)->description = descr; return NITF_SUCCESS; }
NITFAPI(cgm_PictureBody*) cgm_PictureBody_construct(nitf_Error* error) { cgm_PictureBody* body = (cgm_PictureBody*)NITF_MALLOC(sizeof(cgm_PictureBody)); if (!body) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } body->auxColor = NULL; body->elements = NULL; body->elements = nitf_List_construct(error); if (!body->elements) { cgm_PictureBody_destruct(&body); return NULL; } body->auxColor = cgm_Color_construct(-1, -1, -1, error); if (!body->auxColor) { cgm_PictureBody_destruct(&body); return NULL; } body->transparency = 1; return body; }
NITFAPI(nitf_ImageSegment *) nitf_ImageSegment_construct(nitf_Error * error) { nitf_ImageSegment *segment = (nitf_ImageSegment *) NITF_MALLOC(sizeof(nitf_ImageSegment)); if (!segment) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } /* This object gets created NOW */ segment->subheader = NULL; /* The image offset in the file */ segment->imageOffset = 0; /* The image end (offset + length image) */ segment->imageEnd = 0; segment->subheader = nitf_ImageSubheader_construct(error); if (!segment->subheader) { nitf_ImageSegment_destruct(&segment); return NULL; } /* Yes! We have a success */ return segment; }
NITFAPI(nitf_DownSampler *) nitf_PixelSkip_construct(nitf_Uint32 rowSkip, nitf_Uint32 colSkip, nitf_Error * error) { static nitf_IDownSampler iPixelSkip = { &PixelSkip_apply, &PixelSkip_destruct }; nitf_DownSampler *downsampler; downsampler = (nitf_DownSampler *) NITF_MALLOC(sizeof(nitf_DownSampler)); if (!downsampler) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } downsampler->rowSkip = rowSkip; downsampler->colSkip = colSkip; downsampler->multiBand = 0; downsampler->minBands = 1; downsampler->maxBands = 0; downsampler->types = NITF_DOWNSAMPLER_TYPE_ALL; downsampler->data = NULL; downsampler->iface = &iPixelSkip; return downsampler; }
NITFAPI(nitf_BandInfo *) nitf_BandInfo_construct(nitf_Error * error) { /* Start by allocating the struct */ nitf_BandInfo *info = (nitf_BandInfo *) NITF_MALLOC(sizeof(nitf_BandInfo)); /* Return now if we have a problem above */ if (!info) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } /* This MUST happen before we construct fields */ /* in case destruction is required */ info->lut = NULL; _NITF_CONSTRUCT_FIELD(info, NITF_IREPBAND, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(info, NITF_ISUBCAT, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(info, NITF_IFC, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(info, NITF_IMFLT, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(info, NITF_NLUTS, NITF_BCS_N); _NITF_CONSTRUCT_FIELD(info, NITF_NELUT, NITF_BCS_N); return info; CATCH_ERROR: /* destruct if it was allocated */ if (info) nitf_BandInfo_destruct(&info); return NULL; }
NITFAPI(nitf_BandSource *) nitf_IOSource_construct(nitf_IOInterface *io, nitf_Off start, int numBytesPerPixel, int pixelSkip, nitf_Error * error) { static nitf_IDataSource iIOSource = { &IOSource_read, &IOSource_destruct, &IOSource_getSize, &IOSource_setSize }; IOSourceImpl *impl; nitf_BandSource *bandSource; impl = (IOSourceImpl *) NITF_MALLOC(sizeof(IOSourceImpl)); if (!impl) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } impl->io = io; impl->numBytesPerPixel = numBytesPerPixel > 0 ? numBytesPerPixel : 1; impl->pixelSkip = pixelSkip >= 0 ? pixelSkip : 0; impl->mark = impl->start = (start >= 0 ? start : 0); impl->size = nitf_IOInterface_getSize(io, error); if (!NITF_IO_SUCCESS(impl->size)) { NITF_FREE(impl); return NULL; } bandSource = (nitf_BandSource *) NITF_MALLOC(sizeof(nitf_BandSource)); if (!bandSource) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } bandSource->data = impl; bandSource->iface = &iIOSource; return bandSource; }
NITFAPI(NITF_BOOL) nitf_Field_resizeField(nitf_Field *field, size_t newLength, nitf_Error *error) { char fill = 0; /* it must be resizable */ if (!field->resizable) return NITF_FAILURE; if (field && newLength != field->length) { if (field->raw) NITF_FREE(field->raw); field->raw = NULL; /* re-malloc */ field->raw = (char *) NITF_MALLOC(newLength + 1); if (!field->raw) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); goto CATCH_ERROR; } /* set the new length */ field->length = newLength; field->raw[newLength] = 0; /* terminating null byte */ switch (field->type) { case NITF_BCS_A: fill = ' '; break; case NITF_BCS_N: fill = '0'; break; case NITF_BINARY: fill = 0; break; default: nitf_Error_initf(error, NITF_CTXT, NITF_ERR_INVALID_PARAMETER, "Invalid type [%d]", field->type); goto CATCH_ERROR; } memset(field->raw, fill, field->length); } return NITF_SUCCESS; CATCH_ERROR: return NITF_FAILURE; }
nitf_GraphicSubheader_clone(nitf_GraphicSubheader * source, nitf_Error * error) { nitf_GraphicSubheader *subhdr = NULL; if (source) { subhdr = (nitf_GraphicSubheader *) NITF_MALLOC(sizeof(nitf_GraphicSubheader)); if (!subhdr) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } subhdr->securityGroup = nitf_FileSecurity_clone(source->securityGroup, error); /* Copy some fields */ _NITF_CLONE_FIELD(subhdr, source, NITF_SY); _NITF_CLONE_FIELD(subhdr, source, NITF_SID); _NITF_CLONE_FIELD(subhdr, source, NITF_SNAME); _NITF_CLONE_FIELD(subhdr, source, NITF_SSCLAS); _NITF_CLONE_FIELD(subhdr, source, NITF_ENCRYP); _NITF_CLONE_FIELD(subhdr, source, NITF_SFMT); _NITF_CLONE_FIELD(subhdr, source, NITF_SSTRUCT); _NITF_CLONE_FIELD(subhdr, source, NITF_SDLVL); _NITF_CLONE_FIELD(subhdr, source, NITF_SALVL); _NITF_CLONE_FIELD(subhdr, source, NITF_SLOC); _NITF_CLONE_FIELD(subhdr, source, NITF_SBND1); _NITF_CLONE_FIELD(subhdr, source, NITF_SCOLOR); _NITF_CLONE_FIELD(subhdr, source, NITF_SBND2); _NITF_CLONE_FIELD(subhdr, source, NITF_SRES2); _NITF_CLONE_FIELD(subhdr, source, NITF_SXSHDL); _NITF_CLONE_FIELD(subhdr, source, NITF_SXSOFL); /* init to NULL for safety */ subhdr->extendedSection = NULL; if (source->extendedSection) { subhdr->extendedSection = nitf_Extensions_clone(source->extendedSection, error); if (!subhdr->extendedSection) goto CATCH_ERROR; } return subhdr; } CATCH_ERROR: nitf_GraphicSubheader_destruct(&subhdr); return NULL; }
nitf_TextSubheader_clone(nitf_TextSubheader * source, nitf_Error * error) { nitf_TextSubheader *subhdr = NULL; if (source) { subhdr = (nitf_TextSubheader *) NITF_MALLOC(sizeof(nitf_TextSubheader)); if (!subhdr) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } subhdr->securityGroup = nitf_FileSecurity_clone(source->securityGroup, error); if (!subhdr->securityGroup) goto CATCH_ERROR; /* Copy some fields */ _NITF_CLONE_FIELD(subhdr, source, NITF_TE); _NITF_CLONE_FIELD(subhdr, source, NITF_TEXTID); _NITF_CLONE_FIELD(subhdr, source, NITF_TXTALVL); _NITF_CLONE_FIELD(subhdr, source, NITF_TXTDT); _NITF_CLONE_FIELD(subhdr, source, NITF_TXTITL); _NITF_CLONE_FIELD(subhdr, source, NITF_TSCLAS); _NITF_CLONE_FIELD(subhdr, source, NITF_ENCRYP); _NITF_CLONE_FIELD(subhdr, source, NITF_TXTFMT); _NITF_CLONE_FIELD(subhdr, source, NITF_TXSHDL); _NITF_CLONE_FIELD(subhdr, source, NITF_TXSOFL); /* init to NULL in case */ subhdr->extendedSection = NULL; if (source->extendedSection) { subhdr->extendedSection = nitf_Extensions_clone(source->extendedSection, error); if (!subhdr->extendedSection) goto CATCH_ERROR; } return subhdr; } CATCH_ERROR: nitf_TextSubheader_destruct(&subhdr); return NULL; }
NITFAPI(nitf_BandSource *) nitf_MemorySource_construct(char *data, nitf_Off size, nitf_Off start, int numBytesPerPixel, int pixelSkip, nitf_Error * error) { static nitf_IDataSource iMemorySource = { &MemorySource_read, &MemorySource_destruct, &MemorySource_getSize, &MemorySource_setSize }; MemorySourceImpl *impl; nitf_BandSource *bandSource; impl = (MemorySourceImpl *) NITF_MALLOC(sizeof(MemorySourceImpl)); if (!impl) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } impl->data = data; impl->size = size; impl->numBytesPerPixel = numBytesPerPixel > 0 ? numBytesPerPixel : 1; impl->mark = impl->start = (start >= 0 ? start : 0); impl->pixelSkip = pixelSkip >= 0 ? pixelSkip : 0; bandSource = (nitf_BandSource *) NITF_MALLOC(sizeof(nitf_BandSource)); if (!bandSource) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } bandSource->data = impl; bandSource->iface = &iMemorySource; return bandSource; }
NITFAPI(cgm_VertexClose*) cgm_VertexClose_construct(nitf_Error* error) { cgm_VertexClose* v = (cgm_VertexClose*)NITF_MALLOC(sizeof(cgm_VertexClose)); if (!v) { nitf_Error_init(error, NITF_STRERROR( NITF_ERRNO ), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } v->x = v->y = -1; v->edgeOutFlag = CGM_EDGE_CLOSE_TYPE_NOT_SET; return v; }
nitf_GraphicSubheader_construct(nitf_Error * error) { /* Start by allocating the header */ nitf_GraphicSubheader *subhdr = (nitf_GraphicSubheader *) NITF_MALLOC(sizeof(nitf_GraphicSubheader)); /* Return now if we have a problem above */ if (!subhdr) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); goto CATCH_ERROR; } subhdr->extendedSection = NULL; subhdr->securityGroup = NULL; subhdr->securityGroup = nitf_FileSecurity_construct(error); if (!subhdr->securityGroup) goto CATCH_ERROR; _NITF_CONSTRUCT_FIELD(subhdr, NITF_SY, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_SID, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_SNAME, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_SSCLAS, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_ENCRYP, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_SFMT, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_SSTRUCT, NITF_BCS_N); _NITF_CONSTRUCT_FIELD(subhdr, NITF_SDLVL, NITF_BCS_N); _NITF_CONSTRUCT_FIELD(subhdr, NITF_SALVL, NITF_BCS_N); _NITF_CONSTRUCT_FIELD(subhdr, NITF_SLOC, NITF_BCS_N); _NITF_CONSTRUCT_FIELD(subhdr, NITF_SBND1, NITF_BCS_N); _NITF_CONSTRUCT_FIELD(subhdr, NITF_SCOLOR, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_SBND2, NITF_BCS_N); _NITF_CONSTRUCT_FIELD(subhdr, NITF_SRES2, NITF_BCS_N); _NITF_CONSTRUCT_FIELD(subhdr, NITF_SXSHDL, NITF_BCS_N); _NITF_CONSTRUCT_FIELD(subhdr, NITF_SXSOFL, NITF_BCS_N); subhdr->extendedSection = nitf_Extensions_construct(error); if (!subhdr->extendedSection) goto CATCH_ERROR; return subhdr; CATCH_ERROR: if (subhdr) nitf_GraphicSubheader_destruct(&subhdr); return NULL; }
NITFAPI(nitf_SegmentWriter *) nitf_SegmentWriter_construct(nitf_Error *error) { static nitf_IWriteHandler iWriteHandler = { &SegmentWriter_write, &SegmentWriter_destruct }; SegmentWriterImpl *impl = NULL; nitf_SegmentWriter *segmentWriter = NULL; impl = (SegmentWriterImpl *) NITF_MALLOC(sizeof(SegmentWriterImpl)); if (!impl) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); goto CATCH_ERROR; } impl->segmentSource = NULL; segmentWriter = (nitf_SegmentWriter *) NITF_MALLOC(sizeof(nitf_SegmentWriter)); if (!segmentWriter) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); goto CATCH_ERROR; } segmentWriter->data = impl; segmentWriter->iface = &iWriteHandler; return segmentWriter; CATCH_ERROR: if (impl) NITF_FREE(impl); return NULL; }
nitf_TextSubheader_construct(nitf_Error * error) { /* Start by allocating the header */ nitf_TextSubheader *subhdr = (nitf_TextSubheader *) NITF_MALLOC(sizeof(nitf_TextSubheader)); /* Return now if we have a problem above */ if (!subhdr) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } subhdr->extendedSection = NULL; subhdr->securityGroup = nitf_FileSecurity_construct(error); if (!subhdr->securityGroup) { nitf_TextSubheader_destruct(&subhdr); goto CATCH_ERROR; } _NITF_CONSTRUCT_FIELD(subhdr, NITF_TE, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_TEXTID, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_TXTALVL, NITF_BCS_N); _NITF_CONSTRUCT_FIELD(subhdr, NITF_TXTDT, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_TXTITL, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_TSCLAS, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_ENCRYP, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_TXTFMT, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_TXSHDL, NITF_BCS_N); _NITF_CONSTRUCT_FIELD(subhdr, NITF_TXSOFL, NITF_BCS_N); subhdr->extendedSection = nitf_Extensions_construct(error); if (!subhdr->extendedSection) goto CATCH_ERROR; return subhdr; CATCH_ERROR: nitf_TextSubheader_destruct(&subhdr); return NULL; }
NITFPRIV(NITF_BOOL) SegmentWriter_write(NITF_DATA * data, nitf_IOInterface* io, nitf_Error * error) { size_t size, bytesLeft; size_t readSize = READ_SIZE; size_t bytesToRead = READ_SIZE; char* buf = NULL; SegmentWriterImpl *impl = (SegmentWriterImpl *) data; size = (*impl->segmentSource->iface->getSize)(impl->segmentSource->data, error); bytesLeft = size; buf = (char*) NITF_MALLOC(readSize); if (!buf) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); goto CATCH_ERROR; } while (bytesLeft > 0) { if (bytesLeft < readSize) bytesToRead = (size_t)bytesLeft; else bytesToRead = readSize; /* read the bytes */ if (!(*(impl->segmentSource->iface->read)) (impl->segmentSource->data, buf, bytesToRead, error)) { goto CATCH_ERROR; } /* write them */ if (!nitf_IOInterface_write(io, buf, (int)bytesToRead, error)) goto CATCH_ERROR; bytesLeft -= bytesToRead; } NITF_FREE(buf); return NITF_SUCCESS; CATCH_ERROR: if (buf) NITF_FREE(buf); return NITF_FAILURE; }
NITFPRIV(void*) implMemAlloc(size_t size, nitf_Error* error) { void * p = NITF_MALLOC(size); if (!p) { nitf_Error_init(error, NITF_STRERROR( NITF_ERRNO ), NITF_CTXT, NITF_ERR_MEMORY); } else { memset(p, 0, size); } return p; }
/* this writes the text data from a text segment to a file */ void writeTextData(nitf_TextSegment * segment, const char *fileName, nitf_SegmentReader * reader, int textNumber, nitf_Error * error) { size_t toRead; char * buf = NULL; char * outName = NULL; nitf_IOHandle file; toRead = (size_t)(segment->end - segment->offset); buf = (char*)NITF_MALLOC(toRead + 1); if (!buf) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return; } /* get the data */ if (nitf_SegmentReader_read(reader, buf, toRead, error) != NITF_SUCCESS) { /* TODO populate error */ goto CATCH_ERROR; } outName = makeBandName(fileName, "text", textNumber, -1); file = nitf_IOHandle_create(outName, NITF_ACCESS_WRITEONLY, NITF_CREATE, error); freeBandName(&outName); if (NITF_INVALID_HANDLE(file)) { goto CATCH_ERROR; } if (!nitf_IOHandle_write(file, (const char*)buf, toRead, error)) goto CATCH_ERROR; nitf_IOHandle_close(file); CATCH_ERROR: if (buf) NITF_FREE(buf); return; }
/* * The idea here is we will speed it up by creating a temporary buffer * for reading from the io interface. Even with the allocation, this should * be much faster than seeking every time. * * The basic idea is that we allocate the temporary buffer to the request * size * the skip factor. It should be noted that the tradeoff here is that, * for very large read values, this may be really undesirable, especially for * large skip factors. * * If this proves to be a problem, I will revert it back to a seek/read paradigm * -DP */ NITFPRIV(NITF_BOOL) IOSource_offsetRead(IOSourceImpl * source, void* buf, nitf_Off size, nitf_Error * error) { /* we do not multiply the pixelSkip by numBytesPerPixel, b/c this * read method takes in size as number of bytes, not number of pixels */ /* TODO - this *could* be smaller, but this should be ok for now */ nitf_Off tsize = size * (source->pixelSkip + 1); nitf_Uint8* tbuf; nitf_Uint8* bufPtr = (nitf_Uint8*)buf; nitf_Off lmark = 0; int i = 0; int j = 0; if (tsize + source->mark > source->size) tsize = source->size - source->mark; tbuf = (nitf_Uint8 *) NITF_MALLOC((size_t)tsize); if (!tbuf) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NITF_FAILURE; } if (!nitf_IOInterface_read(source->io, tbuf, (size_t)tsize, error)) { NITF_FREE(tbuf); return NITF_FAILURE; } /* Downsize for buf */ while (i < size) { for (j = 0; j < source->numBytesPerPixel; ++j, ++i, ++lmark) { bufPtr[i] = tbuf[lmark]; } lmark += (source->pixelSkip * source->numBytesPerPixel); } source->mark += lmark; NITF_FREE(tbuf); return NITF_SUCCESS; }
NITFAPI(cgm_Text*) cgm_Text_construct(const char* text, nitf_Error* error) { cgm_Text* v = (cgm_Text*)NITF_MALLOC(sizeof(cgm_Text)); if (!v) { nitf_Error_init(error, NITF_STRERROR( NITF_ERRNO ), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } v->x = v->y = -1; v->str = NULL; if (text) { v->str = (char*)NITF_MALLOC( strlen( text ) + 1 ); strcpy(v->str, text); } return v; }
NITFPRIV(NITF_BOOL) toReal(nitf_Field * field, NITF_DATA * outData, size_t length, nitf_Error * error) { NITF_BOOL status = NITF_SUCCESS; char* tmpBuf = NULL; switch (field->type) { case NITF_BCS_A: case NITF_BCS_N: tmpBuf = NITF_MALLOC(field->length + 1); if (!tmpBuf) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); status = NITF_FAILURE; break; } memcpy(tmpBuf, field->raw, field->length); tmpBuf[field->length] = 0; if (length == sizeof(float)) *((float *) outData) = (float) atof(tmpBuf); else if (length == sizeof(double)) *((double *) outData) = (double) atof(tmpBuf); else { nitf_Error_init(error, "toReal -> incorrect length", NITF_CTXT, NITF_ERR_INVALID_PARAMETER); status = NITF_FAILURE; } NITF_FREE(tmpBuf); break; case NITF_BINARY: memcpy(outData, field->raw, length); break; default: nitf_Error_init(error, "Unknown conversion", NITF_CTXT, NITF_ERR_INVALID_PARAMETER); status = NITF_FAILURE; break; } return status; }
NITFPRIV(NITF_BOOL) defaultClone(nitf_TRE *source, nitf_TRE *tre, nitf_Error* error) { nitf_TREPrivateData *sourcePriv = NULL; nitf_TREPrivateData *trePriv = NULL; if (!tre || !source || !source->priv) return NITF_FAILURE; sourcePriv = (nitf_TREPrivateData*)source->priv; /* this clones the hash */ if (!(trePriv = nitf_TREPrivateData_clone(sourcePriv, error))) return NITF_FAILURE; /* just copy over the optional length */ trePriv->length = sourcePriv->length; /* setup the description how we want it */ trePriv->description = (nitf_TREDescription *) NITF_MALLOC( 2 * sizeof(nitf_TREDescription)); if (!trePriv->description) { nitf_TREPrivateData_destruct(&trePriv); nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NITF_FAILURE; } trePriv->description[0].data_type = NITF_BINARY; trePriv->description[0].data_count = sourcePriv->description[0].data_count; trePriv->description[0].label = _NITF_DEFAULT_TRE_LABEL; trePriv->description[0].tag = NITF_TRE_RAW; trePriv->description[1].data_type = NITF_END; trePriv->description[1].data_count = 0; trePriv->description[1].label = NULL; trePriv->description[1].tag = NULL; tre->priv = trePriv; return NITF_SUCCESS; }
NITFAPI(nitf_ImageSegment *) nitf_ImageSegment_clone(nitf_ImageSegment * source, nitf_Error * error) { nitf_ImageSegment *segment = NULL; if (source) { segment = (nitf_ImageSegment *) NITF_MALLOC(sizeof(nitf_ImageSegment)); if (!segment) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } /* Just in case we self-destruct */ segment->subheader = NULL; /* The image offset in the file */ segment->imageOffset = source->imageOffset; /* The image end (offset + length image) */ segment->imageEnd = source->imageEnd; segment->subheader = nitf_ImageSubheader_clone(source->subheader, error); if (!segment->subheader) { nitf_ImageSegment_destruct(&segment); return NULL; } } else { nitf_Error_initf(error, NITF_CTXT, NITF_ERR_INVALID_OBJECT, "Trying to clone NULL pointer"); } /* Yes! We have a success */ return segment; }
NITFAPI(nitf_RESubheader *) nitf_RESubheader_construct(nitf_Error * error) { /* Start by allocating the header */ nitf_RESubheader *subhdr = (nitf_RESubheader *) NITF_MALLOC(sizeof(nitf_RESubheader)); /* Return now if we have a problem above */ if (!subhdr) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); goto CATCH_ERROR; } subhdr->securityGroup = nitf_FileSecurity_construct(error); if (!subhdr->securityGroup) { nitf_RESubheader_destruct(&subhdr); goto CATCH_ERROR; } _NITF_CONSTRUCT_FIELD(subhdr, NITF_RE, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_RESTAG, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_RESVER, NITF_BCS_N); _NITF_CONSTRUCT_FIELD(subhdr, NITF_RESCLAS, NITF_BCS_A); _NITF_CONSTRUCT_FIELD(subhdr, NITF_RESSHL, NITF_BCS_N); subhdr->subheaderFields = NULL; subhdr->dataLength = 0; return subhdr; CATCH_ERROR: /* destruct if it was allocated */ if (subhdr) nitf_RESubheader_destruct(&subhdr); return NULL; }
cgm_LineAttributes_construct(nitf_Error* error) { cgm_LineAttributes* atts = (cgm_LineAttributes*)NITF_MALLOC(sizeof(cgm_LineAttributes)); if (!atts) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NULL; } atts->lineWidth = -1; atts->lineType = CGM_TYPE_NOT_SET; atts->lineColor = cgm_Color_construct(-1, -1, -1, error); if (!atts->lineColor) { cgm_LineAttributes_destruct(&atts); return NULL; } return atts; }
/* * The idea here is we will speed it up by creating a temporary buffer * for reading from the io handle. Even with the allocation, this should * be much faster than seeking every time. * * The basic idea is that we allocate the temporary buffer to the request * size * the skip factor. It should be noted that the tradeoff here is that, * for very large read values, this may be really undesirable, especially for * large skip factors. * * If this proves to be a problem, I will revert it back to a seek/read paradigm * -DP */ NITFPRIV(NITF_BOOL) FileSource_offsetRead(FileSourceImpl * fileSource, char *buf, nitf_Off size, nitf_Error * error) { nitf_Off tsize = size * (fileSource->byteSkip + 1); char *tbuf; nitf_Off lmark = 0; int i = 0; if (tsize + fileSource->mark > fileSource->size) tsize = fileSource->size - fileSource->mark; tbuf = (char *) NITF_MALLOC(tsize); if (!tbuf) { nitf_Error_init(error, NITF_STRERROR(NITF_ERRNO), NITF_CTXT, NITF_ERR_MEMORY); return NITF_FAILURE; } if (!nitf_IOInterface_read(fileSource->io, tbuf, tsize, error)) { NITF_FREE(tbuf); return NITF_FAILURE; } /* Downsize for buf */ while (i < size) { buf[i++] = *(tbuf + lmark++); lmark += (fileSource->byteSkip); } fileSource->mark += lmark; NITF_FREE(tbuf); return NITF_SUCCESS; }