J2KAPI(j2k_Container*) j2k_Container_construct(nrt_Uint32 gridWidth, nrt_Uint32 gridHeight, nrt_Uint32 numComponents, j2k_Component** components, nrt_Uint32 tileWidth, nrt_Uint32 tileHeight, int imageType, nrt_Error *error) { j2k_Container *container = NULL; ContainerImpl *impl = NULL; container = (j2k_Container*) J2K_MALLOC(sizeof(j2k_Container)); if (!container) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); goto CATCH_ERROR; } memset(container, 0, sizeof(j2k_Container)); /* create the Container interface */ impl = (ContainerImpl *) J2K_MALLOC(sizeof(ContainerImpl)); if (!impl) { nrt_Error_init(error, NRT_STRERROR(NRT_ERRNO), NRT_CTXT, NRT_ERR_MEMORY); goto CATCH_ERROR; } memset(impl, 0, sizeof(ContainerImpl)); container->data = impl; container->iface = &ContainerInterface; impl->gridWidth = gridWidth; impl->gridHeight = gridHeight; impl->nComponents = numComponents; impl->components = components; impl->tileWidth = tileWidth; impl->tileHeight = tileHeight; impl->xTiles = gridWidth / tileWidth + (gridWidth % tileWidth == 0 ? 0 : 1); impl->yTiles = gridHeight / tileHeight + (gridHeight % tileHeight == 0 ? 0 : 1); impl->imageType = imageType; return container; CATCH_ERROR: { if (container) { j2k_Container_destruct(&container); } return NULL; } }
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); } }
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; }