How to read JP2 using openJPEG C++ tile by tile

给你一囗甜甜゛ 提交于 2019-12-11 10:27:35

问题


I wonder: What is the intented way to read jp2 tile by tile, and to store data in buffer objects?

Right now i do something like that.

/* note I already created stream and configured decoder and resolution factor is set to 0 */

opj_codestream_index_t *pJP2CodeStreamIndex = opj_get_cstr_index(pJP2Codec);
opj_codestream_info_v2_t *pJP2CodeStreamInfo = opj_get_cstr_info(pJP2Codec);

CDrawRect myRect
GetDrawRect(&myRect);

int start_x = myRect.left;
int start_y = myRect.top;
int end_x = myRect.right;
int end_y = myRect.bottom;

int StartXTile = start_x / pJP2CodeStreamInfo->tdx;
int StartYTile = start_y / pJP2CodeStreamInfo->tdy;
int EndXTile = ceil((float)end_x / (float)pJP2CodeStreamInfo->tdx);
int EndYTile = ceil((float)end_y / (float)pJP2CodeStreamInfo->tdy);

std::vector<int> TilesToDecode;
for(int i = StartXTile; i < EndXTile; i++)
    for(int j = StartYTile; j < EndYTile; j++)
    {
        int TileNo = i+ j*pJP2CodeStreamInfo->tw;
        TilesToDecode.push_back(TileNo);
    }

for(std::vector<int>::iter Iter = TilesToDecode.begin(); Iter != TilesToDecode.end(); Iter++)
{
    opj_get_decoded_tile(pJP2Codec, pJP2Stream, pJP2Image, (OPJ_UINT32)TileNo);
}

/* some time later, i got read buffer for one component */
while (pDst != pEndDst)
{
    OPJ_UINT32* pSrc = pJP2Image.comps[NumComp].data;
    *pDst = (int)*pSrc;
    pDst += stepDst;
    pSrc += stepSrc;
}

But how it was intended?


回答1:


Well, in fact it was quite simple. U just need to be careful with pointer mathematics and buffers' sizes.

/code is writen in C++ with MFC and WINAPI support/

First you need to start decode:

opj_stream_t* pJP2Stream = NULL;
opj_codec_t* pJP2Codec = NULL;
opj_image_t* pJP2Image = NULL;
opj_dparameters_t jp2dParams;

pJP2Stream = opj_stream_create_default_file_stream(LPCTSTR2PCHAR(Filename), OPJ_TRUE);
if(!pJP2Stream)
{
    pFileInfo->ErrorCode = ERR_OPEN;
    return FALSE;
}

strExt = PathFindExtension(Filename);
if(!strExt.CompareNoCase(_T(".JP2")))
    pJP2Codec = opj_create_decompress(OPJ_CODEC_JP2);
if(!strExt.CompareNoCase(_T(".J2K")))
    pJP2Codec = opj_create_decompress(OPJ_CODEC_J2K);
if(!strExt.CompareNoCase(_T(".JPP")))
    pJP2Codec = opj_create_decompress(OPJ_CODEC_JPP);
if(!strExt.CompareNoCase(_T(".JPT")))
    pJP2Codec = opj_create_decompress(OPJ_CODEC_JPT);
if(!strExt.CompareNoCase(_T(".JPX")))
    pJP2Codec = opj_create_decompress(OPJ_CODEC_JPX);

if(!pJP2Codec)
{
    opj_stream_destroy(pJP2Stream);
    opj_destroy_codec(pJP2Codec);
    return FALSE;
}

//register callbacks
opj_set_info_handler(pJP2Codec, info_callback,00);
opj_set_warning_handler(pJP2Codec, warning_callback,00);
opj_set_error_handler(pJP2Codec, error_callback,00);

opj_set_default_decoder_parameters(&jp2dParams);
if ( !opj_setup_decoder(pJP2Codec, &jp2dParams) )
{
    //(stderr, "ERROR -> opj_compress: failed to setup the decoder\n");
    opj_stream_destroy(pJP2Stream);
    opj_destroy_codec(pJP2Codec);
    return FALSE;
}

if( !opj_read_header(pJP2Stream, pJP2Codec, &pJP2Image) || (pJP2Image->numcomps == 0))
{

    opj_stream_destroy(pJP2Stream);
    opj_destroy_codec(pJP2Codec);
    if (pJP2Image)
        opj_image_destroy(pJP2Image);
    return FALSE;
}

Second you should read CodestreamInfo:

opj_codestream_info_v2_t *pJP2CodeStreamInfo = opj_get_cstr_info(pJP2Codec);

be careful if u have several threads calling this function you might get a memory leak. So i suggest next code to transfer information into stack mem:

opj_codestream_info_v2_t JP2CodeStreamInfo;
opj_codestream_info_v2_t *pJP2CodeStreamInfo = NULL;

pJP2CodeStreamInfo = opj_get_cstr_info(pJP2Codec);
if(pJP2CodeStreamInfo)
{
    JP2CodeStreamInfo = *pJP2CodeStreamInfo;
    opj_destroy_cstr_info(&pJP2CodeStreamInfo);
}
else
    return FALSE;

Codestream Info changes when you alter decode resolution factor:

 opj_set_decoded_resolution_factor(pJP2Codec, nResFactor);

Resolution factor - makes decoded images smaller in 2^nResFactor ratio, loading only first of 2^n rows and columls.

I offer next code as universal solution to copy of any tile to any image block.

        long posSrcOffsetX = (start_x > JP2Image.comps[NumBand].x0 ? (start_x - JP2Image.comps[NumBand].x0) : 0) >> JP2Image.comps[NumBand].factor >> (iDifRes);
        long posSrcOffsetY = (start_y > JP2Image.comps[NumBand].y0 ? (start_y - JP2Image.comps[NumBand].y0) : 0) >> JP2Image.comps[NumBand].factor >> (iDifRes);
        long posDstOffsetX = ((JP2Image.comps[NumBand].x0 > start_x? JP2Image.comps[NumBand].x0 - start_x : 0) >> JP2Image.comps[NumBand].factor >> (iDifRes)) ;
        long posDstOffsetY = ((JP2Image.comps[NumBand].y0 > start_y? JP2Image.comps[NumBand].y0 - start_y : 0) >> JP2Image.comps[NumBand].factor >> (iDifRes)) ;

        long posSrcEndX = (bInnerBlock ? ((JP2Image.comps[NumBand].x0 >> JP2Image.comps[NumBand].factor) + (end_x >> JP2Image.comps[NumBand].factor)) : JP2Image.comps[NumBand].w) >>(iDifRes) ;
        long posSrcEndY = (bInnerBlock ? ((JP2Image.comps[NumBand].y0 >> JP2Image.comps[NumBand].factor) + (end_y >> JP2Image.comps[NumBand].factor)) : JP2Image.comps[NumBand].h) >>(iDifRes) ;

        long TileEndX = JP2Image.comps[NumBand].x0 + (JP2Image.comps[NumBand].w << JP2Image.comps[NumBand].factor);
        long TileEndY = JP2Image.comps[NumBand].y0 + (JP2Image.comps[NumBand].h << JP2Image.comps[NumBand].factor);

        long posDstEndX = (TileEndX > end_x ? end_x - start_x : TileEndX - start_x) >> JP2Image.comps[NumBand].factor >> (iDifRes) ;
        long posDstEndY = (TileEndY > end_y ? end_y - start_y : TileEndY - start_y) >> JP2Image.comps[NumBand].factor >> (iDifRes) ;

        long stepSrcX = 1 << (iDifRes);
        long stepSrcY = JP2Image.comps[NumBand].w << (iDifRes);
        long stepDstX = 1;
        long stepDstY = 64;

        DWORD boundX = posDstEndX - posDstOffsetX;
        DWORD boundY = posDstEndY - posDstOffsetY;

        for(i = 0 ; i < boundY ; i++ )
                    {
                        pSrc = JP2Image.comps[NumBand].data + (posSrcOffsetX)*stepSrcX + ((posSrcOffsetY+i) * stepSrcY);
                        pDst = pDstLine + (posDstOffsetX + ((posDstOffsetY+i) * stepDstY))*PixelSize;
                        for ( j = 0; j < boundX ; j++ )
                        {
                            iValue = *(pSrc + j* stepSrcX);

                            iValue += JP2Image.comps[NumBand].sgnd ? (1 << (JP2Image.comps[NumBand].prec - 1)) : 0; 

                            if (iValue > dMax)
                                iValue = (int)dMax; 
                            else 
                                if (iValue < dMin) 
                                    iValue = (int)dMin;

                            UCHAR bufVal = (UCHAR)iValue;
                            *(UCHAR*)pDst = (UCHAR)iValue;

                            pDst += stepDstX*PixelSize;
                        }
                    } 

Just be sure to check bits per pixel and to have a for clause for all image components.



来源:https://stackoverflow.com/questions/34154962/how-to-read-jp2-using-openjpeg-c-tile-by-tile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!