static void ReadJP2KImage( GenericImage<P>& img, jas_stream_t* jp2Stream, jas_image_t* jp2Image ) { int width = jas_image_cmptwidth( jp2Image, 0 ); int height = jas_image_cmptheight( jp2Image, 0 ); int numberOfChannels = jas_image_numcmpts( jp2Image ); jas_matrix_t* pixels = nullptr; try { pixels = jas_matrix_create( 1, width ); if ( pixels == nullptr ) throw Error( "Memory allocation error reading JPEG2000 image" ); // Allocate pixel data img.AllocateData( width, height, numberOfChannels, (jas_clrspc_fam( jas_image_clrspc( jp2Image ) ) == JAS_CLRSPC_FAM_GRAY) ? ColorSpace::Gray : ColorSpace::RGB ); for ( int c = 0; c < numberOfChannels; ++c ) { int n = jas_image_cmptprec( jp2Image, c ); bool s = jas_image_cmptsgnd( jp2Image, c ) != 0; for ( int y = 0; y < height; ++y ) { jas_image_readcmpt( jp2Image, c, 0, y, width, 1, pixels ); typename P::sample* f = img.ScanLine( y, c ); if ( n == 8 ) { if ( s ) for ( int x = 0; x < width; ++x ) *f++ = P::ToSample( int8( jas_matrix_get( pixels, 0, x ) ) ); else for ( int x = 0; x < width; ++x ) *f++ = P::ToSample( uint8( jas_matrix_get( pixels, 0, x ) ) ); } else { if ( s ) for ( int x = 0; x < width; ++x ) *f++ = P::ToSample( int16( jas_matrix_get( pixels, 0, x ) ) ); else for ( int x = 0; x < width; ++x ) *f++ = P::ToSample( uint16( jas_matrix_get( pixels, 0, x ) ) ); } } } jas_matrix_destroy( pixels ), pixels = nullptr; } catch ( ... ) { if ( pixels != nullptr ) jas_matrix_destroy( pixels ); throw; } }
static JDIMENSION jpg_get_pixel_rows(j_compress_ptr cinfo, struct jpg_src_s *sinfo) { JSAMPLE *bufptr; int i; int cmptno; int width; int *cmpts; cmpts = sinfo->enc->cmpts; width = jas_image_width(sinfo->image); if (sinfo->error) { return 0; } for (cmptno = 0; cmptno < cinfo->input_components; ++cmptno) { if (jas_image_readcmpt(sinfo->image, cmpts[cmptno], 0, sinfo->row, width, 1, sinfo->data)) { ; } bufptr = (sinfo->buffer[0]) + cmptno; for (i = 0; i < width; ++i) { *bufptr = jas_matrix_get(sinfo->data, 0, i); bufptr += cinfo->input_components; } } ++sinfo->row; return 1; }
double pae(jas_matrix_t *x, jas_matrix_t *y) { double s; double d; int i; int j; s = 0.0; for (i = 0; i < jas_matrix_numrows(x); i++) { for (j = 0; j < jas_matrix_numcols(x); j++) { d = abs(jas_matrix_get(y, i, j) - jas_matrix_get(x, i, j)); if (d > s) { s = d; } } } return s; }
double msen(jas_matrix_t *x, jas_matrix_t *y, int n) { double s; double d; int i; int j; s = 0.0; for (i = 0; i < jas_matrix_numrows(x); i++) { for (j = 0; j < jas_matrix_numcols(x); j++) { d = jas_matrix_get(y, i, j) - jas_matrix_get(x, i, j); if (n == 1) { s += fabs(d); } else if (n == 2) { s += d * d; } else { abort(); } } } return s / ((double) jas_matrix_numrows(x) * jas_matrix_numcols(x)); }
bool Jpeg2KDecoder::readComponent8u( uchar *data, void *_buffer, int step, int cmpt, int maxval, int offset, int ncmpts ) { jas_matrix_t* buffer = (jas_matrix_t*)_buffer; jas_image_t* image = (jas_image_t*)m_image; int xstart = jas_image_cmpttlx( image, cmpt ); int xend = jas_image_cmptbrx( image, cmpt ); int xstep = jas_image_cmpthstep( image, cmpt ); int xoffset = jas_image_tlx( image ); int ystart = jas_image_cmpttly( image, cmpt ); int yend = jas_image_cmptbry( image, cmpt ); int ystep = jas_image_cmptvstep( image, cmpt ); int yoffset = jas_image_tly( image ); int x, y, x1, y1, j; int rshift = cvRound(std::log(maxval/256.)/std::log(2.)); int lshift = MAX(0, -rshift); rshift = MAX(0, rshift); int delta = (rshift > 0 ? 1 << (rshift - 1) : 0) + offset; for( y = 0; y < yend - ystart; ) { jas_seqent_t* pix_row = &jas_matrix_get( buffer, y / ystep, 0 ); uchar* dst = data + (y - yoffset) * step - xoffset; if( xstep == 1 ) { if( maxval == 256 && offset == 0 ) for( x = 0; x < xend - xstart; x++ ) { int pix = pix_row[x]; dst[x*ncmpts] = CV_CAST_8U(pix); } else for( x = 0; x < xend - xstart; x++ ) { int pix = ((pix_row[x] + delta) >> rshift) << lshift; dst[x*ncmpts] = CV_CAST_8U(pix); } } else if( xstep == 2 && offset == 0 ) for( x = 0, j = 0; x < xend - xstart; x += 2, j++ ) { int pix = ((pix_row[j] + delta) >> rshift) << lshift; dst[x*ncmpts] = dst[(x+1)*ncmpts] = CV_CAST_8U(pix); } else for( x = 0, j = 0; x < xend - xstart; j++ )
/* Encode all of the code blocks associated with the current tile. */ int jpc_enc_enccblks(jpc_enc_t *enc) { jpc_enc_tcmpt_t *tcmpt; jpc_enc_tcmpt_t *endcomps; jpc_enc_rlvl_t *lvl; jpc_enc_rlvl_t *endlvls; jpc_enc_band_t *band; jpc_enc_band_t *endbands; jpc_enc_cblk_t *cblk; jpc_enc_cblk_t *endcblks; int i; int j; int mx; int bmx; int v; jpc_enc_tile_t *tile; uint_fast32_t prcno; jpc_enc_prc_t *prc; tile = enc->curtile; endcomps = &tile->tcmpts[tile->numtcmpts]; for (tcmpt = tile->tcmpts; tcmpt != endcomps; ++tcmpt) { endlvls = &tcmpt->rlvls[tcmpt->numrlvls]; for (lvl = tcmpt->rlvls; lvl != endlvls; ++lvl) { if (!lvl->bands) { continue; } endbands = &lvl->bands[lvl->numbands]; for (band = lvl->bands; band != endbands; ++band) { if (!band->data) { continue; } for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { if (!prc->cblks) { continue; } bmx = 0; endcblks = &prc->cblks[prc->numcblks]; for (cblk = prc->cblks; cblk != endcblks; ++cblk) { mx = 0; for (i = 0; i < jas_matrix_numrows(cblk->data); ++i) { for (j = 0; j < jas_matrix_numcols(cblk->data); ++j) { v = abs(jas_matrix_get(cblk->data, i, j)); if (v > mx) { mx = v; } } } if (mx > bmx) { bmx = mx; } cblk->numbps = JAS_MAX(jpc_firstone(mx) + 1 - JPC_NUMEXTRABITS, 0); } for (cblk = prc->cblks; cblk != endcblks; ++cblk) { cblk->numimsbs = band->numbps - cblk->numbps; assert(cblk->numimsbs >= 0); } for (cblk = prc->cblks; cblk != endcblks; ++cblk) { if (jpc_enc_enccblk(enc, cblk->stream, tcmpt, band, cblk)) { return -1; } } } } } } return 0; }
/* Encode all of the code blocks associated with the current tile. */ int jpc_enc_enccblks(jpc_enc_t *enc) { jpc_enc_tcmpt_t *tcmpt; jpc_enc_tcmpt_t *endcomps; jpc_enc_rlvl_t *lvl; jpc_enc_rlvl_t *endlvls; jpc_enc_band_t *band; jpc_enc_band_t *endbands; jpc_enc_cblk_t *endcblks; int k; int bmx; jpc_enc_tile_t *tile; uint_fast32_t prcno; jpc_enc_prc_t *prc; tile = enc->curtile; endcomps = &tile->tcmpts[tile->numtcmpts]; for (tcmpt = tile->tcmpts; tcmpt != endcomps; ++tcmpt) { endlvls = &tcmpt->rlvls[tcmpt->numrlvls]; for (lvl = tcmpt->rlvls; lvl != endlvls; ++lvl) { if (!lvl->bands) { continue; } endbands = &lvl->bands[lvl->numbands]; for (band = lvl->bands; band != endbands; ++band) { if (!band->data) { continue; } for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { int error = 0; if (!prc->cblks) { continue; } bmx = 0; endcblks = &prc->cblks[prc->numcblks]; #pragma omp parallel for reduction(|:error) for (k=0; k<prc->numcblks; ++k) { //OMP: for (cblk = prc->cblks; cblk != endcblks; ++cblk) { int v, mx, i, j; jpc_enc_cblk_t *cblk = &(prc->cblks[k]); //printf("Thread %d working on k %d (numcblks = %d)\n", omp_get_thread_num(), k, prc->numcblks); mx = 0; for (i = 0; i < jas_matrix_numrows(cblk->data); ++i) { for (j = 0; j < jas_matrix_numcols(cblk->data); ++j) { v = abs(jas_matrix_get(cblk->data, i, j)); if (v > mx) { mx = v; } } } /* if (mx > bmx) { bmx = mx; } */ cblk->numbps = JAS_MAX(jpc_firstone(mx) + 1 - JPC_NUMEXTRABITS, 0); //OMP: } //OMP: for (cblk = prc->cblks; cblk != endcblks; ++cblk) { cblk->numimsbs = band->numbps - cblk->numbps; assert(cblk->numimsbs >= 0); //OMP: } //OMP: for (cblk = prc->cblks; cblk != endcblks; ++cblk) { if (jpc_enc_enccblk(enc, cblk->stream, tcmpt, band, cblk)) { error |= 1; //OMP: return -1; } } //printf("Exiting\n"); if(error) return -1; } } } } return 0; }
jas_image_t *makediffimage(jas_matrix_t *origdata, jas_matrix_t *recondata) { jas_image_t *diffimage; jas_matrix_t *diffdata[3]; int width; int height; int i; int j; int k; jas_image_cmptparm_t compparms[3]; jas_seqent_t a; jas_seqent_t b; width = jas_matrix_numcols(origdata); height = jas_matrix_numrows(origdata); for (i = 0; i < 3; ++i) { compparms[i].tlx = 0; compparms[i].tly = 0; compparms[i].hstep = 1; compparms[i].vstep = 1; compparms[i].width = width; compparms[i].height = height; compparms[i].prec = 8; compparms[i].sgnd = jas_false; } if (!(diffimage = jas_image_create(3, compparms, JAS_CLRSPC_SRGB))) { abort(); } for (i = 0; i < 3; ++i) { if (!(diffdata[i] = jas_matrix_create(height, width))) { jas_eprintf("internal error\n"); return 0; } } for (j = 0; j < height; ++j) { for (k = 0; k < width; ++k) { a = jas_matrix_get(origdata, j, k); b = jas_matrix_get(recondata, j, k); if (a > b) { jas_matrix_set(diffdata[0], j, k, 255); jas_matrix_set(diffdata[1], j, k, 0); jas_matrix_set(diffdata[2], j, k, 0); } else if (a < b) { jas_matrix_set(diffdata[0], j, k, 0); jas_matrix_set(diffdata[1], j, k, 255); jas_matrix_set(diffdata[2], j, k, 0); } else { jas_matrix_set(diffdata[0], j, k, a); jas_matrix_set(diffdata[1], j, k, a); jas_matrix_set(diffdata[2], j, k, a); } } } for (i = 0; i < 3; ++i) { if (jas_image_writecmpt(diffimage, i, 0, 0, width, height, diffdata[i])) { return 0; } } return diffimage; }
CPLErr JPEG2000RasterBand::IReadBlock( int nBlockXOff, int nBlockYOff, void * pImage ) { int i, j; // Decode image from the stream, if not yet if ( !poGDS->DecodeImage() ) { return CE_Failure; } // Now we can calculate the pixel offset of the top left by multiplying // block offset with the block size. /* In case the dimensions of the image are not multiple of the block dimensions */ /* take care of not requesting more pixels than available for the blocks at the */ /* right or bottom of the image */ int nWidthToRead = MIN(nBlockXSize, poGDS->nRasterXSize - nBlockXOff * nBlockXSize); int nHeightToRead = MIN(nBlockYSize, poGDS->nRasterYSize - nBlockYOff * nBlockYSize); jas_image_readcmpt( poGDS->psImage, nBand - 1, nBlockXOff * nBlockXSize, nBlockYOff * nBlockYSize, nWidthToRead, nHeightToRead, psMatrix ); int nWordSize = GDALGetDataTypeSize(eDataType) / 8; int nLineSize = nBlockXSize * nWordSize; GByte* ptr = (GByte*)pImage; /* Pad incomplete blocks at the right or bottom of the image */ if (nWidthToRead != nBlockXSize || nHeightToRead != nBlockYSize) memset(pImage, 0, nLineSize * nBlockYSize); for( i = 0; i < nHeightToRead; i++, ptr += nLineSize ) { for( j = 0; j < nWidthToRead; j++ ) { // XXX: We need casting because matrix element always // has 32 bit depth in JasPer // FIXME: what about float values? switch( eDataType ) { case GDT_Int16: { ((GInt16*)ptr)[j] = (GInt16)jas_matrix_get(psMatrix, i, j); } break; case GDT_Int32: { ((GInt32*)ptr)[j] = (GInt32)jas_matrix_get(psMatrix, i, j); } break; case GDT_UInt16: { ((GUInt16*)ptr)[j] = (GUInt16)jas_matrix_get(psMatrix, i, j); } break; case GDT_UInt32: { ((GUInt32*)ptr)[j] = (GUInt32)jas_matrix_get(psMatrix, i, j); } break; case GDT_Byte: default: { ((GByte*)ptr)[j] = (GByte)jas_matrix_get(psMatrix, i, j); } break; } } } return CE_None; }
static void drawview(jas_image_t *image, float vtlx, float vtly, float sx, float sy, pixmap_t *p) { int i; int j; int k; int red; int grn; int blu; int lum; GLshort *datap; int x; int y; int *cmptlut; int numcmpts; int v[4]; int u[4]; int color; cmptlut = gs.cmptlut; switch (jas_image_colorspace(gs.image)) { case JAS_IMAGE_CS_RGB: case JAS_IMAGE_CS_YCBCR: color = 1; numcmpts = 3; break; case JAS_IMAGE_CS_GRAY: default: numcmpts = 1; color = 0; break; } for (i = 0; i < p->height; ++i) { datap = &p->data[(p->height - 1 - i) * (4 * p->width)]; for (j = 0; j < p->width; ++j) { if (!gs.monomode && color) { for (k = 0; k < numcmpts; ++k) { x = vctocc(j, jas_image_cmpttlx(gs.image, cmptlut[k]), jas_image_cmpthstep(gs.image, cmptlut[k]), vtlx, sx); y = vctocc(i, jas_image_cmpttly(gs.image, cmptlut[k]), jas_image_cmptvstep(gs.image, cmptlut[k]), vtly, sy); v[k] = (x >= 0 && x < jas_image_cmptwidth(gs.image, cmptlut[k]) && y >=0 && y < jas_image_cmptheight(gs.image, cmptlut[k])) ? jas_matrix_get(gs.cmpts[cmptlut[k]], y, x) : 0; v[k] <<= 16 - jas_image_cmptprec(gs.image, cmptlut[k]); } switch (jas_image_colorspace(gs.image)) { case JAS_IMAGE_CS_RGB: break; case JAS_IMAGE_CS_YCBCR: u[0] = (1/1.772) * (v[0] + 1.402 * v[2]); u[1] = (1/1.772) * (v[0] - 0.34413 * v[1] - 0.71414 * v[2]); u[2] = (1/1.772) * (v[0] + 1.772 * v[1]); v[0] = u[0]; v[1] = u[1]; v[2] = u[2]; break; } } else { x = vctocc(j, jas_image_cmpttlx(gs.image, gs.cmptno), jas_image_cmpthstep(gs.image, gs.cmptno), vtlx, sx); y = vctocc(i, jas_image_cmpttly(gs.image, gs.cmptno), jas_image_cmptvstep(gs.image, gs.cmptno), vtly, sy); v[0] = (x >= 0 && x < jas_image_cmptwidth(gs.image, gs.cmptno) && y >=0 && y < jas_image_cmptheight(gs.image, gs.cmptno)) ? jas_matrix_get(gs.cmpts[gs.cmptno], y, x) : 0; v[0] <<= 16 - jas_image_cmptprec(gs.image, gs.cmptno); v[1] = v[0]; v[2] = v[0]; v[3] = 0; } for (k = 0; k < 3; ++k) { if (v[k] < 0) { v[k] = 0; } else if (v[k] > 65535) { v[k] = 65535; } } *datap++ = v[0]; *datap++ = v[1]; *datap++ = v[2]; *datap++ = 0; } } }