gpt4 book ai didi

c++ - 如何使用 openJPEG C++ 逐 block 读取 JP2

转载 作者:行者123 更新时间:2023-11-30 05:34:37 27 4
gpt4 key购买 nike

我想知道:逐 block 读取 jp2 并将数据存储在缓冲区对象中的预期方法是什么?

现在我正在做类似的事情。

/* 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;
}

但是它是如何设计的呢?

最佳答案

嗯,其实很简单。你只需要注意指针数学和缓冲区的大小。

/代码是用 C++ 编写的,支持 MFC 和 WINAPI/

首先你需要开始解码:

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;
}

其次,您应该阅读 CodestreamInfo:

opj_codestream_info_v2_t *pJP2CodeStreamInfo = opj_get_cstr_info(pJP2Codec);

请注意,如果您有多个线程调用此函数,您可能会发生内存泄漏。所以我建议下一个代码将信息传输到堆栈内存中:

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;

当您更改解码分辨率因子时,码流信息会发生变化:

 opj_set_decoded_resolution_factor(pJP2Codec, nResFactor);

分辨率因子 - 以 2^nResFactor 比率使解码图像更小,仅加载 2^n 行和列中的第一个。

我提供下一个代码作为将任何图 block 复制到任何图像 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;
}
}

只需确保检查每个像素的位数并为所有图像组件设置一个 for 子句即可。

关于c++ - 如何使用 openJPEG C++ 逐 block 读取 JP2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34154962/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com