int is_jp2(unsigned char *idata, const int ilen) { jas_stream_t *in; int ret; if (jas_init()){ fprintf(stderr, "ERROR : is_jp2: init : jas\n"); return(-1); } /* The input image is to be read from buffer stream. */ in = jas_stream_memopen((char *)idata, ilen); if (in == NULL){ fprintf(stderr, "ERROR : is_jp2: failed to open jas stream\n"); return(-2); } /* Check is JP2 format. */ ret = jas_image_getfmt(in); /* General clean up. */ (void) jas_stream_close(in); return(ret); }
static void readJpc(const char * const inputFilename, jas_image_t ** const jasperPP) { jas_image_t * jasperP; jas_stream_t *instream; const char * options; if ( strcmp(inputFilename, "-") == 0) { /* The input image is to be read from standard input. */ instream = jas_stream_fdopen(fileno(stdin), "rb"); if (instream == NULL) pm_error("error: cannot reopen standard input"); } else { instream = jas_stream_fopen(inputFilename, "rb"); if (instream == NULL ) pm_error("cannot open input image file '%s'", inputFilename); } if (jas_image_getfmt(instream) != jas_image_strtofmt((char*)"jpc")) pm_error("Input is not JPEG-2000 code stream"); options = ""; jasperP = jas_image_decode(instream, jas_image_strtofmt((char*)"jpc"), (char*)options); if (jasperP == NULL) pm_error("Unable to interpret JPEG-2000 input. " "The Jasper library jas_image_decode() subroutine failed."); jas_stream_close(instream); *jasperPP = jasperP; }
bool jasperConvert(jas_image_t* &image, jas_stream_t* &out, const char* data, unsigned size, int outfmt, const char* outopts) { jas_stream_t *in; // kDebug(YAHOO_RAW_DEBUG) << "Got data - size=" << size; if(!(in = jas_stream_memopen(const_cast<char*>(data), size))) { kDebug(YAHOO_RAW_DEBUG) << "Could not open jasper input stream"; return false; } int infmt; infmt = jas_image_getfmt(in); if (infmt < 0) { jas_stream_close(in); kDebug(YAHOO_RAW_DEBUG) << "Failed to recognize input webcam image format"; return false; } if (!(image = jas_image_decode(in, infmt, 0))) { kDebug(YAHOO_RAW_DEBUG) << "Unable to decode image"; jas_stream_close(in); return false; } /* kDebug(YAHOO_RAW_DEBUG) << "jasper: decoded image: " << jas_image_width(image) << "x" << jas_image_height(image) << " bytes: " << jas_image_rawsize(image) << " components:" << jas_image_numcmpts(image); */ char* out_img = NULL; if(!(out = jas_stream_memopen(out_img, 0))) { kDebug(YAHOO_RAW_DEBUG) << "Could not open output stream"; jas_stream_close(in); return false; } if (jas_image_encode(image, out, outfmt, const_cast<char*>(outopts))) { kDebug(YAHOO_RAW_DEBUG) << "Unable to convert image"; jas_stream_close(in); jas_stream_close(out); jas_image_destroy(image); return false; } jas_stream_flush(out); jas_stream_close(in); return true; }
int imFileFormatJP2::Open(const char* file_name) { this->stream = jas_binfile_open(file_name, 0); if (this->stream == NULL) return IM_ERR_OPEN; this->fmtid = jas_image_getfmt(this->stream); if (this->fmtid < 0) { jas_stream_close(this->stream); return IM_ERR_FORMAT; } strcpy(this->compression, "JPEG-2000"); this->image_count = 1; return IM_ERR_NONE; }
int main(int argc, char **argv) { jas_image_t *image; cmdopts_t *cmdopts; jas_stream_t *in; jas_stream_t *out; clock_t startclk; clock_t endclk; long dectime; long enctime; int_fast16_t numcmpts; int i; /* Determine the base name of this command. */ if ((cmdname = strrchr(argv[0], '/'))) { ++cmdname; } else { cmdname = argv[0]; } if (jas_init()) { errprint(0, "error: cannot initialize jasper library\n"); abort(); } /* set our error callback */ jas_set_error_cb(errprint); /* Parse the command line options. */ if (!(cmdopts = cmdopts_parse(argc, argv))) { jas_eprintf("error: cannot parse command line\n"); exit(EXIT_FAILURE); } if (cmdopts->version) { jas_eprintf("%s\n", JAS_VERSION); jas_eprintf("libjasper %s\n", jas_getversion()); exit(EXIT_SUCCESS); } jas_setdbglevel(cmdopts->debug); if (cmdopts->verbose) { cmdinfo(); } /* Open the input image file. */ if (cmdopts->infile) { /* The input image is to be read from a file. */ if (!(in = jas_stream_fopen(cmdopts->infile, "rb"))) { jas_eprintf("error: cannot open input image file %s\n", cmdopts->infile); exit(EXIT_FAILURE); } } else { /* The input image is to be read from standard input. */ if (!(in = jas_stream_fdopen(0, "rb"))) { jas_eprintf("error: cannot open standard input\n"); exit(EXIT_FAILURE); } } /* Open the output image file. */ if (cmdopts->outfile) { /* The output image is to be written to a file. */ if (!(out = jas_stream_fopen(cmdopts->outfile, "w+b"))) { jas_eprintf("error: cannot open output image file %s\n", cmdopts->outfile); exit(EXIT_FAILURE); } } else { /* The output image is to be written to standard output. */ if (!(out = jas_stream_fdopen(1, "w+b"))) { jas_eprintf("error: cannot open standard output\n"); exit(EXIT_FAILURE); } } if (cmdopts->infmt < 0) { if ((cmdopts->infmt = jas_image_getfmt(in)) < 0) { jas_eprintf("error: input image has unknown format\n"); exit(EXIT_FAILURE); } } /* Get the input image data. */ startclk = clock(); if (!(image = jas_image_decode(in, cmdopts->infmt, cmdopts->inopts))) { jas_eprintf("error: cannot load image data\n"); exit(EXIT_FAILURE); } endclk = clock(); dectime = endclk - startclk; /* If requested, throw away all of the components except one. Why might this be desirable? It is a hack, really. None of the image formats other than the JPEG-2000 ones support images with two, four, five, or more components. This hack allows such images to be decoded with the non-JPEG-2000 decoders, one component at a time. */ numcmpts = jas_image_numcmpts(image); if (cmdopts->cmptno >= 0 && cmdopts->cmptno < numcmpts) { for (i = numcmpts - 1; i >= 0; --i) { if (i != cmdopts->cmptno) { jas_image_delcmpt(image, i); } } } if (cmdopts->srgb) { jas_image_t *newimage; jas_cmprof_t *outprof; jas_eprintf("forcing conversion to sRGB\n"); if (!(outprof = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB))) { jas_eprintf("cannot create sRGB profile\n"); exit(EXIT_FAILURE); } if (!(newimage = jas_image_chclrspc(image, outprof, JAS_CMXFORM_INTENT_PER))) { jas_eprintf("cannot convert to sRGB\n"); exit(EXIT_FAILURE); } jas_image_destroy(image); jas_cmprof_destroy(outprof); image = newimage; } /* Generate the output image data. */ startclk = clock(); if (jas_image_encode(image, out, cmdopts->outfmt, cmdopts->outopts)) { jas_eprintf("error: cannot encode image\n"); exit(EXIT_FAILURE); } jas_stream_flush(out); endclk = clock(); enctime = endclk - startclk; if (cmdopts->verbose) { jas_eprintf("decoding time = %f\n", dectime / (double) CLOCKS_PER_SEC); jas_eprintf("encoding time = %f\n", enctime / (double) CLOCKS_PER_SEC); } /* If this fails, we don't care. */ (void) jas_stream_close(in); /* Close the output image stream. */ if (jas_stream_close(out)) { jas_eprintf("error: cannot close output image file\n"); exit(EXIT_FAILURE); } cmdopts_destroy(cmdopts); jas_image_destroy(image); jas_image_clearfmts(); /* Success at last! :-) */ return EXIT_SUCCESS; }
int main(int argc, char **argv) { int fmtid; int id; char *infile; jas_stream_t *instream; jas_image_t *image; int width; int height; int depth; int numcmpts; int verbose; char *fmtname; if (jas_init()) { errprint(0, "error: cannot initialize jasper library\n"); abort(); } /* set our error callback */ jas_set_error_cb(errprint); cmdname = argv[0]; infile = 0; verbose = 0; /* Parse the command line options. */ while ((id = jas_getopt(argc, argv, opts)) >= 0) { switch (id) { case OPT_VERBOSE: verbose = 1; break; case OPT_VERSION: printf("%s\n", JAS_VERSION); exit(EXIT_SUCCESS); break; case OPT_INFILE: infile = jas_optarg; break; case OPT_HELP: default: usage(); break; } } /* Open the image file. */ if (infile) { /* The image is to be read from a file. */ if (!(instream = jas_stream_fopen(infile, "rb"))) { jas_eprintf("cannot open input image file %s\n", infile); exit(EXIT_FAILURE); } } else { /* The image is to be read from standard input. */ if (!(instream = jas_stream_fdopen(0, "rb"))) { jas_eprintf("cannot open standard input\n"); exit(EXIT_FAILURE); } } if ((fmtid = jas_image_getfmt(instream)) < 0) { jas_eprintf("unknown image format\n"); } /* Decode the image. */ if (!(image = jas_image_decode(instream, fmtid, 0))) { jas_eprintf("cannot load image\n"); return EXIT_FAILURE; } /* Close the image file. */ jas_stream_close(instream); numcmpts = jas_image_numcmpts(image); width = jas_image_cmptwidth(image, 0); height = jas_image_cmptheight(image, 0); depth = jas_image_cmptprec(image, 0); if (!(fmtname = jas_image_fmttostr(fmtid))) { abort(); } jas_eprintf("%s %d %d %d %d %ld\n", fmtname, numcmpts, width, height, depth, (long) jas_image_rawsize(image)); jas_image_destroy(image); jas_image_clearfmts(); return EXIT_SUCCESS; }
GDALDataset *JPEG2000Dataset::Open( GDALOpenInfo * poOpenInfo ) { int iFormat; char *pszFormatName = NULL; jas_stream_t *sS; if (!Identify(poOpenInfo)) return NULL; JPEG2000Init(); if( !(sS = JPEG2000_VSIL_fopen( poOpenInfo->pszFilename, "rb" )) ) { return NULL; } iFormat = jas_image_getfmt( sS ); if ( !(pszFormatName = jas_image_fmttostr( iFormat )) ) { jas_stream_close( sS ); return NULL; } if ( strlen( pszFormatName ) < 3 || (!EQUALN( pszFormatName, "jp2", 3 ) && !EQUALN( pszFormatName, "jpc", 3 ) && !EQUALN( pszFormatName, "pgx", 3 )) ) { CPLDebug( "JPEG2000", "JasPer reports file is format type `%s'.", pszFormatName ); jas_stream_close( sS ); return NULL; } /* -------------------------------------------------------------------- */ /* Confirm the requested access is supported. */ /* -------------------------------------------------------------------- */ if( poOpenInfo->eAccess == GA_Update ) { jas_stream_close(sS); CPLError( CE_Failure, CPLE_NotSupported, "The JPEG2000 driver does not support update access to existing" " datasets.\n" ); return NULL; } /* -------------------------------------------------------------------- */ /* Create a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ JPEG2000Dataset *poDS; int *paiDepth = NULL, *pabSignedness = NULL; int iBand; poDS = new JPEG2000Dataset(); poDS->psStream = sS; poDS->iFormat = iFormat; if ( EQUALN( pszFormatName, "jp2", 3 ) ) { // XXX: Hack to read JP2 boxes from input file. JasPer hasn't public // API call for such things, so we will use internal JasPer functions. jp2_box_t *box; box = 0; while ( ( box = jp2_box_get(poDS->psStream) ) ) { switch (box->type) { case JP2_BOX_IHDR: poDS->nBands = box->data.ihdr.numcmpts; poDS->nRasterXSize = box->data.ihdr.width; poDS->nRasterYSize = box->data.ihdr.height; CPLDebug( "JPEG2000", "IHDR box found. Dump: " "width=%d, height=%d, numcmpts=%d, bpp=%d", (int)box->data.ihdr.width, (int)box->data.ihdr.height, (int)box->data.ihdr.numcmpts, (box->data.ihdr.bpc & 0x7F) + 1 ); /* ISO/IEC 15444-1:2004 §I.5.3.1 specifies that 255 means that all */ /* components have not the same bit depth and/or sign and that a */ /* BPCC box must then follow to specify them for each component */ if ( box->data.ihdr.bpc != 255 ) { paiDepth = (int *)CPLMalloc(poDS->nBands * sizeof(int)); pabSignedness = (int *)CPLMalloc(poDS->nBands * sizeof(int)); for ( iBand = 0; iBand < poDS->nBands; iBand++ ) { paiDepth[iBand] = (box->data.ihdr.bpc & 0x7F) + 1; pabSignedness[iBand] = box->data.ihdr.bpc >> 7; CPLDebug( "JPEG2000", "Component %d: bpp=%d, signedness=%d", iBand, paiDepth[iBand], pabSignedness[iBand] ); } } break; case JP2_BOX_BPCC: CPLDebug( "JPEG2000", "BPCC box found. Dump:" ); if ( !paiDepth && !pabSignedness ) { paiDepth = (int *) CPLMalloc( box->data.bpcc.numcmpts * sizeof(int) ); pabSignedness = (int *) CPLMalloc( box->data.bpcc.numcmpts * sizeof(int) ); for( iBand = 0; iBand < (int)box->data.bpcc.numcmpts; iBand++ ) { paiDepth[iBand] = (box->data.bpcc.bpcs[iBand] & 0x7F) + 1; pabSignedness[iBand] = box->data.bpcc.bpcs[iBand] >> 7; CPLDebug( "JPEG2000", "Component %d: bpp=%d, signedness=%d", iBand, paiDepth[iBand], pabSignedness[iBand] ); } } break; case JP2_BOX_PCLR: CPLDebug( "JPEG2000", "PCLR box found. Dump: number of LUT entries=%d, " "number of resulting channels=%d", (int)box->data.pclr.numlutents, box->data.pclr.numchans ); poDS->nBands = box->data.pclr.numchans; if ( paiDepth ) CPLFree( paiDepth ); if ( pabSignedness ) CPLFree( pabSignedness ); paiDepth = (int *) CPLMalloc( box->data.pclr.numchans * sizeof(int) ); pabSignedness = (int *) CPLMalloc( box->data.pclr.numchans * sizeof(int) ); for( iBand = 0; iBand < (int)box->data.pclr.numchans; iBand++ ) { paiDepth[iBand] = (box->data.pclr.bpc[iBand] & 0x7F) + 1; pabSignedness[iBand] = box->data.pclr.bpc[iBand] >> 7; CPLDebug( "JPEG2000", "Component %d: bpp=%d, signedness=%d", iBand, paiDepth[iBand], pabSignedness[iBand] ); } break; }
static gboolean query_jp2 (const gchar *path, gint *width, gint *height, gint *depth, jas_image_t **jas_image) { gboolean ret; jas_stream_t *in; int image_fmt; jas_image_t *image; jas_cmprof_t *output_profile; jas_image_t *cimage; int numcmpts; int i; gboolean b; in = NULL; cimage = image = NULL; output_profile = NULL; ret = FALSE; do { in = jas_stream_fopen (path, "rb"); if (!in) { g_warning ("Unable to open image file '%s'", path); break; } image_fmt = jas_image_getfmt (in); if (image_fmt < 0) { g_warning (_("Unknown JPEG-2000 image format in '%s'"), path); break; } image = jas_image_decode (in, image_fmt, NULL); if (!image) { g_warning (_("Unable to open JPEG-2000 image in '%s'"), path); break; } output_profile = jas_cmprof_createfromclrspc (JAS_CLRSPC_SRGB); if (!output_profile) { g_warning (_("Unable to create output color profile for '%s'"), path); break; } cimage = jas_image_chclrspc (image, output_profile, JAS_CMXFORM_INTENT_PER); if (!cimage) { g_warning (_("Unable to convert image to sRGB color space " "when processing '%s'"), path); break; } numcmpts = jas_image_numcmpts (cimage); if (numcmpts != 3) { g_warning (_("Unsupported non-RGB JPEG-2000 file with " "%d components in '%s'"), numcmpts, path); break; } *width = jas_image_cmptwidth (cimage, 0); *height = jas_image_cmptheight (cimage, 0); *depth = jas_image_cmptprec (cimage, 0); if ((*depth != 8) && (*depth != 16)) { g_warning (_("Unsupported JPEG-2000 file with depth %d in '%s'"), *depth, path); break; } b = FALSE; for (i = 1; i < 3; i++) { if ((jas_image_cmptprec (cimage, i) != *depth) || (jas_image_cmptwidth (cimage, i) != *width) || (jas_image_cmptheight (cimage, i) != *height)) { g_warning (_("Components of input image '%s' don't match"), path); b = TRUE; break; } } if (b) break; ret = TRUE; } while (FALSE); /* structured goto */ if (jas_image) *jas_image = cimage; else if (cimage) jas_image_destroy (cimage); if (image) jas_image_destroy (image); if (output_profile) jas_cmprof_destroy (output_profile); if (in) jas_stream_close (in); return ret; }
bool FetchTIFFCornersWithJP2K(const char * inFileName, double corners[8], int& post_pos) { jas_stream_t *inStream; jas_image_t *image; if(jas_init() != 0 ) { //If it failed then return error return -1; } //If the data stream cannot be created if((inStream = jas_stream_fopen(inFileName,"rb"))==false) { return false; } //Get the format ID int formatId; //If there are any errors in getting the format if((formatId = jas_image_getfmt(inStream)) < 0) { //It is an invalid format return false; } //If the image cannot be decoded if((image = jas_image_decode(inStream, formatId, 0)) == false) { //Return an error return false; } //If it turns out the .jp2 never had any geological data exit if(image->aux_buf.size == 0) { return false; } #if DUMP_GTIF FILE * foo = fopen("temp.tiff","wb"); if(foo) { fwrite(image->aux_buf.buf, 1, image->aux_buf.size, foo); fclose(foo); } #endif //Create the handle to be used in XTIFFClientOpen MemJASGeoFile jasHandle(&image->aux_buf); //Create a TIFF handle TIFF * tif = XTIFFClientOpen(inFileName,"r",&jasHandle,MemJASGeoRead, MemJASGeoWrite, MemJASGeoSeek, MemJASGeoClose, MemJASGeoSize, MemJASGeoMapFile, MemJASGeoUnmapFile); int postType = dem_want_Area; //Pass in our TIF handle, post type, width, and height if(FetchTIFFCornersWithTIFF(tif,corners,postType,(image->brx_-image->tlx_),(image->bry_-image->tly_))==false) { return false; } //Shut downthe stream jas_stream_close(inStream); //Unintialize jasper jas_cleanup(); //It all worked! return true; }
bool CxImageJAS::Decode(CxFile *hFile, uint32_t imagetype) { if (hFile == NULL) return false; jas_image_t *image=0; jas_stream_t *in=0; jas_matrix_t **bufs=0; int32_t i,error=0; int32_t fmt; //jas_setdbglevel(0); cx_try { if (jas_init()) cx_throw("cannot initialize jasper"); in = jas_stream_fdopen(0, "rb"); if (!in) cx_throw("error: cannot open standard input"); CxFileJas src(hFile,in); fmt = jas_image_getfmt(in); if (fmt<0) cx_throw("error: unknowm format"); image = jas_image_decode(in, fmt, 0); if (!image){ fmt = -1; cx_throw("error: cannot load image data"); } char szfmt[4]; *szfmt = '\0'; strncpy(szfmt,jas_image_fmttostr(fmt),3); szfmt[3] = '\0'; fmt = -1; #if CXIMAGE_SUPPORT_JP2 if (strcmp(szfmt,"jp2")==0) fmt = CXIMAGE_FORMAT_JP2; #endif #if CXIMAGE_SUPPORT_JPC if (strcmp(szfmt,"jpc")==0) fmt = CXIMAGE_FORMAT_JPC; #endif #if CXIMAGE_SUPPORT_RAS if (strcmp(szfmt,"ras")==0) fmt = CXIMAGE_FORMAT_RAS; #endif #if CXIMAGE_SUPPORT_PNM if (strcmp(szfmt,"pnm")==0) fmt = CXIMAGE_FORMAT_PNM; #endif #if CXIMAGE_SUPPORT_PGX if (strcmp(szfmt,"pgx")==0) fmt = CXIMAGE_FORMAT_PGX; #endif //if (fmt<0) // cx_throw("error: unknowm format"); int32_t x,y,w,h,depth,cmptno; w = jas_image_cmptwidth(image,0); h = jas_image_cmptheight(image,0); depth = jas_image_cmptprec(image,0); if (info.nEscape == -1){ head.biWidth = w; head.biHeight= h; info.dwType = fmt<0 ? 0 : fmt; cx_throw("output dimensions returned"); } if (image->numcmpts_ > 64 || image->numcmpts_ < 0) cx_throw("error: too many components"); // <LD> 01/Jan/2005: Always force conversion to sRGB. Seems to be required for many types of JPEG2000 file. // if (depth!=1 && depth!=4 && depth!=8) if (image->numcmpts_>=3 && depth <=8) { jas_image_t *newimage; jas_cmprof_t *outprof; //jas_eprintf("forcing conversion to sRGB\n"); outprof = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB); if (!outprof) { cx_throw("cannot create sRGB profile"); } newimage = jas_image_chclrspc(image, outprof, JAS_CMXFORM_INTENT_PER); if (!newimage) { jas_cmprof_destroy(outprof); // <LD> 01/Jan/2005: Destroy color profile on error. cx_throw("cannot convert to sRGB"); } jas_image_destroy(image); jas_cmprof_destroy(outprof); image = newimage; } bufs = (jas_matrix_t **)calloc(image->numcmpts_, sizeof(jas_matrix_t**)); for (i = 0; i < image->numcmpts_; ++i) { bufs[i] = jas_matrix_create(1, w); if (!bufs[i]) { cx_throw("error: cannot allocate memory"); } } int32_t nshift = (depth>8) ? (depth-8) : 0; if (image->numcmpts_==3 && image->cmpts_[0]->width_ == image->cmpts_[1]->width_ && image->cmpts_[1]->width_ == image->cmpts_[2]->width_ && image->cmpts_[0]->height_ == image->cmpts_[1]->height_ && image->cmpts_[1]->height_ == image->cmpts_[2]->height_ && image->cmpts_[0]->prec_ == image->cmpts_[1]->prec_ && image->cmpts_[1]->prec_ == image->cmpts_[2]->prec_ ) { if(!Create(w,h,24,fmt)) cx_throw(""); RGBQUAD c; for (y=0; y<h; y++) { for (cmptno = 0; cmptno < image->numcmpts_; ++cmptno) { jas_image_readcmpt(image, cmptno, 0, y, w, 1, bufs[cmptno]); } for (x=0; x<w; x++){ c.rgbRed = (uint8_t)((jas_matrix_getv(bufs[0], x)>>nshift)); c.rgbGreen = (uint8_t)((jas_matrix_getv(bufs[1], x)>>nshift)); c.rgbBlue = (uint8_t)((jas_matrix_getv(bufs[2], x)>>nshift)); SetPixelColor(x,h-1-y,c); } } } else { info.nNumFrames = image->numcmpts_; if ((info.nFrame<0)||(info.nFrame>=info.nNumFrames)){ cx_throw("wrong frame!"); } for (cmptno=0; cmptno<=info.nFrame; cmptno++) { w = jas_image_cmptwidth(image,cmptno); h = jas_image_cmptheight(image,cmptno); depth = jas_image_cmptprec(image,cmptno); if (depth>8) depth=8; if(!Create(w,h,depth,imagetype)) cx_throw(""); SetGrayPalette(); for (y=0; y<h; y++) { jas_image_readcmpt(image, cmptno, 0, y, w, 1, bufs[0]); for (x=0; x<w; x++){ SetPixelIndex(x,h-1-y,(uint8_t)((jas_matrix_getv(bufs[0], x)>>nshift))); } } } } } cx_catch {