📄 dwtobj.cpp
字号:
int i;
if (initGL)
CHECKSUCC(initOpenGL());
if (initGLEW)
CHECKSUCC(initglew());
CHECKSUCC(setImageDimensions(imagewidth, imageheight));
CHECKSUCC(Init_Cg(forward));
m_busefbo = false;
if ( glewIsExtensionSupported("GL_EXT_framebuffer_object") )
m_busefbo = true;
if ( m_busefbo )
{
m_busefbo = initFBO(32);
if ( !m_busefbo )
{
DELTEXTURES(2, m_fbort);
// delete the FBO
glDeleteFramebuffersEXT(1, &m_fbo);
}
}
if ( !m_busefbo )
CHECKSUCC(initRenderTexture(32));
CHECKSUCC(createImgTex(imagebuffer));
CHECKSUCC(createFilterTex(forward));
// dwt or idwt starts and ends at particular levels you specify
if (forward)
{
CHECKSUCC(createLookupTex(mode, startx, starty, endx, endy));
for (i = beg_level; i <= end_level; i++)
CHECKSUCC(forwarddwt(i, startx, starty, endx, endy));
}
else
{
CHECKSUCC(createInvLookupTex(mode, startx, starty, endx, endy));
for (i = beg_level; i >= end_level; i--)
CHECKSUCC(inversedwt(i, startx, starty, endx, endy));
}
getbuffer(imagebuffer);
return true;
}
//------------------------------------------------------------------------------
// Function : CDwtObj::OneTimeDWT
// Description : one-stop solution for dwt
//------------------------------------------------------------------------------
// [in] mode -- boundary extension mode (symper, per)
// [in] imagewidth, imageheight -- image dimension
// [in|out] imagebuffer -- pointer for input buffer, when returned it holds the output
// [in] level -- how many levels for dwt, e.x for forward with 3 levels, it will do level 0, then 1, then 2, for inverse, it will do 2, then 1, then 0
// [in] isforward -- ture for forward transform , false for inverse
// [in] initGL -- initialize opengl? ( in opengl app, usually false)
// [in] initGLEW -- initialize glew, the extension libary?
// [in] startx, endx, starty, endy -- the indices of x and y direction
//
// Modification history
//
// 15 March 2006 (Liang Wan)
// -- Add comments on timing computation for DWT encoding
//
bool CDwtObj::dwtallinone(extmode mode, int imagewidth, int imageheight, float *imagebuffer, int level, bool isforward,
bool initGL, bool initGLEW, int startx, int starty, int endx, int endy)
{
int i;
// Timing for OpenGL, CG initialization
// clock_t t1 = clock();
if (initGL)
CHECKSUCC(initOpenGL());
if (initGLEW)
CHECKSUCC(initglew());
CHECKSUCC(setImageDimensions(imagewidth, imageheight));
CHECKSUCC(Init_Cg(isforward ? forward : inverse));
// Timing for OpenGL, CG initialization
// clock_t t2 = clock();
// clock_t totaltime = t2 - t1;
// printf("OpenGL: %d.%.3d s\n", totaltime/CLOCKS_PER_SEC, (totaltime%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC);
m_busefbo = false;
if ( glewIsExtensionSupported("GL_EXT_framebuffer_object") )
m_busefbo = true;
if ( m_busefbo )
{
m_busefbo = initFBO(32);
if ( !m_busefbo )
{
DELTEXTURES(2, m_fbort);
// delete the FBO
glDeleteFramebuffersEXT(1, &m_fbo);
}
}
if ( !m_busefbo )
CHECKSUCC(initRenderTexture(32));
CHECKSUCC(createImgTex(imagebuffer));
CHECKSUCC(createFilterTex(isforward ? forward : inverse));
if (isforward)
{
CHECKSUCC(createLookupTex(mode, startx, starty, endx, endy));
// Timing for texture preparation
// t1 = clock();
// totaltime = t1 - t2;
// printf("Texture: %d.%.3d s\n", totaltime/CLOCKS_PER_SEC, (totaltime%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC);
for (i = 0; i < level; i++)
CHECKSUCC(forwarddwt(i, startx, starty, endx, endy));
// Timing for GPU DWT
// t2 = clock();
// totaltime = t2 - t1;
// printf("DWT: %d.%.3d s\n", totaltime/CLOCKS_PER_SEC, (totaltime%CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC);
}
else
{
CHECKSUCC(createInvLookupTex(mode, startx, starty, endx, endy));
for (i = level - 1; i >= 0; i--)
CHECKSUCC(inversedwt(i, startx, starty, endx, endy));
}
getbuffer(imagebuffer);
return true;
}
//------------------------------------------------------------------------------
// Function : CDwtObj::finalcleanup
// Description : delete the textures
//------------------------------------------------------------------------------
//
bool CDwtObj::finalcleanup()
{
//delete textures and rendertextures
DELTEXTURES(1, &m_colLookupTexID);
DELTEXTURES(1, &m_rowLookupTexID);
DELTEXTURES(1, &m_imageTexID);
DELTEXTURES(1, &m_decomFilterTexID);
DELTEXTURES(1, &m_reconFilterTexID);
DELTEXTURES(1, &m_LookupTexID);
if ( m_busefbo )
{
DELTEXTURES(2, m_fbort);
// delete the FBO
glDeleteFramebuffersEXT(1, &m_fbo);
}
else
{
if (m_rt[0] != NULL)
delete m_rt[0];
if (m_rt[1] != NULL)
delete m_rt[1];
}
// delete resident cg programs
CGprogram program = cgGetFirstProgram(cgContext);
CGprogram tmpprog = program;
while (tmpprog != 0)
{
tmpprog = cgGetNextProgram(program);
cgDestroyProgram(program);
program = tmpprog;
}
// destroy cg context
if (cgIsContext(cgContext))
cgDestroyContext(cgContext);
return true;
}
//------------------------------------------------------------------------------
// Function : CDwtObj::createInvLookupTex
// Description : Create the Indirect Addressing Texture for inverse dwt
//------------------------------------------------------------------------------
// [in] mode --boundary extension mode (symper, per)
// [in] startx, endx, starty, endy -- the indices of x and y direction
bool CDwtObj::createInvLookupTex(extmode mode, int startx, int starty, int endx, int endy)
{
float *tex1 = NULL;
int texwidth, texheight;
createIDATexture(mode, &tex1, m_imageWidth, m_imageHeight, texwidth, texheight, startx, starty, endx, endy);
m_ida_texwidth = texwidth;
m_ida_texheight = texheight;
DELTEXTURES(1, &m_LookupTexID);
glGenTextures(1, &m_LookupTexID);
// Create the Lookup Texture for IDWT
glBindTexture (GL_TEXTURE_RECTANGLE_NV, m_LookupTexID);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D (GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, texwidth, texheight, 0, GL_RGBA, GL_FLOAT, tex1);
delete[]tex1;
return true;
}
//------------------------------------------------------------------------------
// Function : CDwtObj::inverse_dwt
// Description : do inverse dwt
//------------------------------------------------------------------------------
// [in] level-- level 0 for whole image, level 1 for upper left 1/4 image, and so on
// [in] startx, endx, starty, endy -- the indices of x and y direction
//
bool CDwtObj::inversedwt(int level, int startx, int starty, int endx, int endy)
{
int quadWidth, quadHeight, tmp;
calLength(startx, endx, level, &quadWidth, &tmp);
calLength(starty, endy, level, &quadHeight, &tmp);
// Horizontal Reconstruction
if (quadWidth > 1)
{
if ( m_busefbo )
{
if ( m_iReadBuffer == 0 )
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
else
glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
}
else
m_rt[m_iReadBuffer]->BeginCapture();
cgGLBindProgram (m_invdwtrowProg);
cgGLEnableProfile(m_fpProfile);
cgGLSetParameter1f(m_invdwtrow_level_Param, (float) level);
if ( m_busefbo )
cgGLSetTextureParameter(m_invdwtrow_imageTex_Param, m_fbort[1 - m_iReadBuffer]);
else
cgGLSetTextureParameter(m_invdwtrow_imageTex_Param, m_rt[1 - m_iReadBuffer]->GetTextureID());
cgGLEnableTextureParameter(m_invdwtrow_imageTex_Param);
cgGLSetTextureParameter (m_invdwtrow_filterTex_Param, m_reconFilterTexID);
cgGLEnableTextureParameter(m_invdwtrow_filterTex_Param);
cgGLSetTextureParameter (m_invdwtrow_lookup_Param, m_LookupTexID);
cgGLEnableTextureParameter(m_invdwtrow_lookup_Param);
RASTER_QUAD;
cgGLDisableTextureParameter(m_invdwtrow_imageTex_Param);
cgGLDisableTextureParameter(m_invdwtrow_filterTex_Param);
cgGLDisableTextureParameter(m_invdwtrow_lookup_Param);
cgGLDisableProfile(m_fpProfile);
if ( !m_busefbo )
m_rt[m_iReadBuffer]->EndCapture();
m_iReadBuffer = (m_iReadBuffer + 1) % 2;
}
else // pass through if not enough length
{
if ( m_busefbo )
{
if ( m_iReadBuffer == 0 )
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
else
glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
}
else
m_rt[m_iReadBuffer]->BeginCapture();
cgGLBindProgram(m_fillProg);
cgGLEnableProfile(m_fpProfile);
if ( m_busefbo )
cgGLSetTextureParameter(m_fill_Tex_Param, m_fbort[1 - m_iReadBuffer]);
else
cgGLSetTextureParameter(m_fill_Tex_Param, m_rt[1 - m_iReadBuffer]->GetTextureID());
cgGLEnableTextureParameter(m_fill_Tex_Param);
RASTER_QUAD;
cgGLDisableTextureParameter(m_fill_Tex_Param);
cgGLDisableProfile(m_fpProfile);
if ( !m_busefbo )
m_rt[m_iReadBuffer]->EndCapture();
m_iReadBuffer = (m_iReadBuffer + 1) % 2;
}
// Vertical reconstruction
if (quadHeight > 1)
{
if ( m_busefbo )
{
if ( m_iReadBuffer == 0 )
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
else
glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
}
else
m_rt[m_iReadBuffer]->BeginCapture();
cgGLBindProgram (m_invdwtcolProg);
cgGLEnableProfile(m_fpProfile);
cgGLSetParameter1f(m_invdwtcol_level_Param, level);
cgGLSetParameter1f(m_invdwtcol_texwidth_Param, m_ida_texwidth);
cgGLSetParameter1f(m_invdwtcol_texheight_Param, m_ida_texheight);
if ( m_busefbo )
cgGLSetTextureParameter(m_invdwtcol_imageTex_Param, m_fbort[1 - m_iReadBuffer]);
else
cgGLSetTextureParameter(m_invdwtcol_imageTex_Param, m_rt[1 - m_iReadBuffer]->GetTextureID());
cgGLEnableTextureParameter(m_invdwtcol_imageTex_Param);
cgGLSetTextureParameter (m_invdwtcol_filterTex_Param, m_reconFilterTexID);
cgGLEnableTextureParameter(m_invdwtcol_filterTex_Param);
cgGLSetTextureParameter (m_invdwtcol_lookup_Param, m_LookupTexID);
cgGLEnableTextureParameter(m_invdwtcol_lookup_Param);
RASTER_QUAD;
cgGLDisableTextureParameter(m_invdwtcol_imageTex_Param);
cgGLDisableTextureParameter(m_invdwtcol_filterTex_Param);
cgGLDisableTextureParameter(m_invdwtcol_lookup_Param);
cgGLDisableProfile(m_fpProfile);
if ( !m_busefbo )
m_rt[m_iReadBuffer]->EndCapture();
m_iReadBuffer = (m_iReadBuffer + 1) % 2;
}
else // pass through if not enough length
{
if ( m_busefbo )
{
if ( m_iReadBuffer == 0 )
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
else
glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
}
else
m_rt[m_iReadBuffer]->BeginCapture();
cgGLBindProgram (m_fillProg);
cgGLEnableProfile(m_fpProfile);
if ( m_busefbo )
cgGLSetTextureParameter(m_fill_Tex_Param, m_fbort[1 - m_iReadBuffer]);
else
cgGLSetTextureParameter(m_fill_Tex_Param, m_rt[1 - m_iReadBuffer]->GetTextureID());
cgGLEnableTextureParameter(m_fill_Tex_Param);
RASTER_QUAD;
cgGLDisableTextureParameter(m_fill_Tex_Param);
cgGLDisableProfile(m_fpProfile);
if ( !m_busefbo )
m_rt[m_iReadBuffer]->EndCapture();
m_iReadBuffer = (m_iReadBuffer + 1) % 2;
}
return true;
}
//------------------------------------------------------------------------------
// Function : CDwtObj::initialize
// Description : do the initialization, after return, user can call forward or inverse dwt functions
//-------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -