J2KAPI(j2k_Reader*) j2k_Reader_open(const char *fname, nrt_Error *error) { j2k_Reader *reader = NULL; nrt_IOInterface *io = NULL; if (!fname) { nrt_Error_init(error, "NULL filename", NRT_CTXT, NRT_ERR_INVALID_OBJECT); goto CATCH_ERROR; } if (!(io = nrt_IOHandleAdapter_open(fname, NRT_ACCESS_READONLY, NRT_OPEN_EXISTING, error))) goto CATCH_ERROR; if (!(reader = j2k_Reader_openIO(io, error))) goto CATCH_ERROR; ((OpenJPEGReaderImpl*) reader->data)->ownIO = 1; return reader; CATCH_ERROR: { if (io) nrt_IOInterface_destruct(&io); if (reader) j2k_Reader_destruct(&reader); return NULL; } }
OpenJPEGWriter_destruct(J2K_USER_DATA * data) { if (data) { OpenJPEGWriterImpl* const impl = (OpenJPEGWriterImpl*) data; OpenJPEG_cleanup(&impl->stream, &impl->codec, &impl->image); nrt_IOInterface_destruct(&impl->compressed); J2K_FREE(data); } }
NITFPRIV(void) FileSource_destruct(NITF_DATA * data) { nitf_Error error; if (data) { IOSourceImpl* const source = (IOSourceImpl*)data; if (source->io) { nrt_IOInterface_close(source->io, &error); nrt_IOInterface_destruct(&source->io); source->io = NULL; } NITF_FREE(data); } }
NITFPRIV(void) FileSource_destruct(NITF_DATA * data) { nitf_Error error; if (data) { FileSourceImpl *fileSource = (FileSourceImpl *) data; if (fileSource->io) { nrt_IOInterface_close(fileSource->io, &error); nrt_IOInterface_destruct(&fileSource->io); fileSource->io = NULL; } NITF_FREE(data); } }
OpenJPEGReader_destruct(J2K_USER_DATA * data) { if (data) { OpenJPEGReaderImpl* const impl = (OpenJPEGReaderImpl*) data; if (impl->io && impl->ownIO) { nrt_IOInterface_destruct(&impl->io); impl->io = NULL; } if(impl->container) { j2k_Container_destruct(&impl->container); impl->container = NULL; } J2K_FREE(data); } }
J2K_BOOL readFile(const char* filename, char **buf, nrt_Uint64 *bufSize, nrt_Error *error) { J2K_BOOL rc = J2K_TRUE; nrt_IOInterface *io = NULL; if (!(io = nrt_IOHandleAdapter_open(filename, NRT_ACCESS_READONLY, NRT_OPEN_EXISTING, error))) goto CATCH_ERROR; *bufSize = nrt_IOInterface_getSize(io, error); if (!(*buf = (char*)J2K_MALLOC(*bufSize))) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); goto CATCH_ERROR; } if (!nrt_IOInterface_read(io, *buf, *bufSize, error)) { goto CATCH_ERROR; } goto CLEANUP; CATCH_ERROR: { rc = J2K_FALSE; if (*buf) J2K_FREE(*buf); *buf = NULL; } CLEANUP: { if (io) nrt_IOInterface_destruct(&io); } return rc; }
int main(int argc, char **argv) { int rc = 0; int argIt = 0, i = 0, num = 0, dump = 0; char *fname = NULL; nrt_Error error; nrt_IOInterface *io = NULL; nitf_Reader *reader = NULL; nitf_Record *record = NULL; nrt_Uint8 *buf = NULL; for (argIt = 1; argIt < argc; ++argIt) { if (strcmp(argv[argIt], "--dump") == 0) dump = 1; else if (!fname) { fname = argv[argIt]; } } if (!fname) { nrt_Error_init(&error, "Usage: [--x0 --y0 --x1 --y1] <j2k-file>", NRT_CTXT, NRT_ERR_INVALID_OBJECT); goto CATCH_ERROR; } if (nitf_Reader_getNITFVersion(fname) == NITF_VER_UNKNOWN) { nrt_Error_init(&error, "This file does not appear to be a valid NITF", NRT_CTXT, NRT_ERR_INVALID_OBJECT); goto CATCH_ERROR; } if (!(io = nrt_IOHandleAdapter_open(fname, NRT_ACCESS_READONLY, NRT_OPEN_EXISTING, &error))) goto CATCH_ERROR; if (!(reader = nitf_Reader_construct(&error))) goto CATCH_ERROR; if (!(record = nitf_Reader_readIO(reader, io, &error))) goto CATCH_ERROR; num = nitf_Record_getNumImages(record, &error); if (num > 0) { nitf_ListIterator iter = nitf_List_begin(record->images); nitf_ListIterator end = nitf_List_end(record->images); for (i = 0; nitf_ListIterator_notEqualTo(&iter, &end); ++i) { nitf_ImageSegment *segment = (nitf_ImageSegment *) nitf_ListIterator_get(&iter); nitf_ImageSubheader *subheader = segment->subheader; if (strcmp(subheader->imageCompression->raw, "C8") == 0) { j2k_Reader *j2kReader = NULL; j2k_Container *container = NULL; nrt_Uint32 cmpIt, nComponents; printf("Image %d contains J2K compressed data\n", (i + 1)); printf("Offset: %d\n", segment->imageOffset); if (!nrt_IOInterface_seek(io, segment->imageOffset, NRT_SEEK_SET, &error)) goto CATCH_ERROR; if (!(j2kReader = j2k_Reader_openIO(io, &error))) goto CATCH_ERROR; if (!(container = j2k_Reader_getContainer(j2kReader, &error))) goto CATCH_ERROR; printf("grid width:\t%d\n", j2k_Container_getGridWidth(container, &error)); printf("grid height:\t%d\n", j2k_Container_getGridHeight(container, &error)); printf("tile width:\t%d\n", j2k_Container_getTileWidth(container, &error)); printf("tile height:\t%d\n", j2k_Container_getTileHeight(container, &error)); printf("x tiles:\t%d\n", j2k_Container_getTilesX(container, &error)); printf("y tiles:\t%d\n", j2k_Container_getTilesY(container, &error)); printf("image type:\t%d\n", j2k_Container_getImageType(container, &error)); nComponents = j2k_Container_getNumComponents(container, &error); printf("components:\t%d\n", nComponents); for(cmpIt = 0; cmpIt < nComponents; ++cmpIt) { j2k_Component* c = j2k_Container_getComponent(container, cmpIt, &error); printf("===component %d===\n", (cmpIt + 1)); printf("width:\t\t%d\n", j2k_Component_getWidth(c, &error)); printf("height:\t\t%d\n", j2k_Component_getHeight(c, &error)); printf("precision:\t%d\n", j2k_Component_getPrecision(c, &error)); printf("x0:\t\t%d\n", j2k_Component_getOffsetX(c, &error)); printf("y0:\t\t%d\n", j2k_Component_getOffsetY(c, &error)); printf("x separation:\t%d\n", j2k_Component_getSeparationX(c, &error)); printf("y separation:\t%d\n", j2k_Component_getSeparationY(c, &error)); printf("signed:\t\t%d\n", j2k_Component_isSigned(c, &error)); } if (dump) { char namePrefix[NRT_MAX_PATH]; nrt_Uint32 width, height; nrt_Uint64 bufSize; if (buf) { NRT_FREE(buf); buf = NULL; } width = j2k_Container_getWidth(container, &error); height = j2k_Container_getWidth(container, &error); if ((bufSize = j2k_Reader_readRegion(j2kReader, 0, 0, width, height, &buf, &error)) == 0) { goto CATCH_ERROR; } NRT_SNPRINTF(namePrefix, NRT_MAX_PATH, "image-%d", (i + 1)); if (!writeFile(0, 0, width, height, buf, bufSize, namePrefix, &error)) { goto CATCH_ERROR; } } } nitf_ListIterator_increment(&iter); } } goto CLEANUP; CATCH_ERROR: { nrt_Error_print(&error, stdout, "Exiting..."); rc = 1; } CLEANUP: { if (reader) nitf_Reader_destruct(&reader); if (record) nitf_Record_destruct(&record); if (io) nrt_IOInterface_destruct(&io); } return rc; }
int main(int argc, char **argv) { int rc = 0; int argIt = 0; char *inName = NULL, *outName = NULL; nrt_Error error; j2k_Component *component = NULL; j2k_Container *container = NULL; j2k_Writer *writer = NULL; j2k_WriterOptions options; char *buf = NULL; nrt_Uint64 bufSize; nrt_IOInterface *outIO = NULL; nrt_Uint32 width, height, precision, tileWidth, tileHeight; for (argIt = 1; argIt < argc; ++argIt) { if (!inName) { inName = argv[argIt]; } else if (!outName) { outName = argv[argIt]; } } /* hardcoded for now... */ width = 128; height = 128; precision = 8; tileWidth = width; tileHeight = height; if (!inName || !outName) { nrt_Error_initf(&error, NRT_CTXT, NRT_ERR_INVALID_OBJECT, "Usage: %s <raw-input> <output-j2k>", argv[0]); goto CATCH_ERROR; } if (!(component = j2k_Component_construct(width, height, precision, 0, 0, 0, 1, 1, &error))) { goto CATCH_ERROR; } if (!(container = j2k_Container_construct(width, height, 1, &component, tileWidth, tileHeight, J2K_TYPE_MONO, &error))) { goto CATCH_ERROR; } memset(&options, 0, sizeof(j2k_WriterOptions)); /* TODO set some options here */ if (!(writer = j2k_Writer_construct(container, &options, &error))) { goto CATCH_ERROR; } if (!readFile(inName, &buf, &bufSize, &error)) { goto CATCH_ERROR; } if (!j2k_Writer_setTile(writer, 0, 0, (nrt_Uint8*)buf, (nrt_Uint32)bufSize, &error)) { goto CATCH_ERROR; } if (!(outIO = nrt_IOHandleAdapter_open(outName, NRT_ACCESS_WRITEONLY, NRT_CREATE, &error))) goto CATCH_ERROR; if (!j2k_Writer_write(writer, outIO, &error)) goto CATCH_ERROR; goto CLEANUP; CATCH_ERROR: { nrt_Error_print(&error, stdout, "Exiting..."); rc = 1; } CLEANUP: { if (container) j2k_Container_destruct(&container); if (writer) j2k_Writer_destruct(&writer); if (buf) J2K_FREE(buf); if (outIO) nrt_IOInterface_destruct(&outIO); } return rc; }