📄 imagebox.cpp
字号:
}
}
}
next:
/* quantize the min_x and min_y with Nx and Ny */
if(min_x%Nx!=0) min_x=min_x/Nx*Nx;
if(min_y%Ny!=0) min_y=min_y/Ny*Ny;
if(min_x>max_x || min_y> max_y) {
return(DWT_NOVALID_PIXEL); /* no valid pixel */
}
origin_x = min_x;
origin_y = min_y;
virtual_width = max_x-min_x+1;
virtual_height = max_y-min_y+1;
/* fprIntf(stderr, "x=%d y=%d w=%d h=%d x2=%d y2=%d\n", min_x, min_y, virtual_width, virtual_height, max_x, max_y); */
}
else { /* rectangular region */
origin_x = 0;
origin_y = 0;
virtual_width = RealWidth;
virtual_height = RealHeight;
}
/* first ajust the dimension to be multiple of blocksize */
virtual_width = (virtual_width+(blocksize)-1)/blocksize*blocksize;
virtual_height = (virtual_height+(blocksize)-1)/blocksize*blocksize;
if ((mask = (UChar *)malloc(sizeof(UChar)*virtual_width*virtual_height)) == NULL) {
return(DWT_MEMORY_FAILED);
}
memset(mask, (Char )0, sizeof(UChar)*virtual_width*virtual_height);
/* calculate clip area */
max_y = origin_y+virtual_height;
max_y = (max_y<real_height)?max_y:real_height;
rows = max_y - origin_y;
max_x = origin_x+virtual_width;
max_x = (max_x<real_width)?max_x:real_width;
cols = max_x - origin_x;
/* fill out data */
for(i=0, j=origin_y*real_width+origin_x; i< rows*virtual_width;
i+=virtual_width, j+=real_width) {
if(Shape != RECTANGULAR) {
f = InMask+j+cols;
for(a = InMask+j, b=mask+i; a < f; a++, b++) {
if(*a == (UChar) Shape) *b = DWT_IN;
}
}
else
memset(mask+i, (Char )DWT_IN, cols);
}
*VirtualWidth = virtual_width;
*VirtualHeight = virtual_height;
*OriginX = origin_x;
*OriginY = origin_y;
*OutMask = mask;
return(DWT_OK);
}
/* Function: ExtendMaskBox()
Description: extend the size of bounding box of the mask of image object
to 2^nLevels;
Input:
InMask -- Input image mask
InWidth, InHeight -- the size of the Input Mask;
Nx, Ny -- specify that OriginX and OriginY must be multiple of Nx and Ny
to accomdate different color compoents configuration:
Examples:
for 420: Nx Ny
Y: 2 2
U,V: 1 1
for 422:
Y: 2 1
for 444:
Y,U,V: 1 1
for mono:
Y: 1 1
for YUV9:
Y: 4 4
U,V: 1 1 nLevels -- levels of decomposition
Output:
OutMask -- Output image mask, extended area is marked as Don't-care
OutWidth, OutHeight -- size of the output mask
Return: DWT_OK if no error;
Note: The caller should free the memory OutMask allocated by this program
after it finishes using them.
*/
Int VTCIMAGEBOX:: ExtendMaskBox(UChar *InMask, UChar **OutMask,
Int InWidth, Int InHeight,
Int Nx, Int Ny,
Int *OutWidth, Int *OutHeight,
Int nLevels)
{
Int out_width, out_height;
UChar *mask;
Int blocksize = 1 << nLevels;
Int i, j;
UChar *a, *b, *f;
if(blocksize%Nx!=0) blocksize = LCM(blocksize,Nx);
if(blocksize%Ny!=0) blocksize = LCM(blocksize,Ny);
/* first ajust the dimension to be multiple of blocksize */
out_width = (InWidth+(blocksize)-1)/blocksize*blocksize;
out_height = (InHeight+(blocksize)-1)/blocksize*blocksize;
if ((mask = (UChar *)malloc(sizeof(UChar)*out_width*out_height)) == NULL) {
return(DWT_MEMORY_FAILED);
}
memset(mask, (Char )0, sizeof(UChar)*out_width*out_height);
/* fill out data */
for(i=0, j=0; i< InHeight*out_width;
i+=out_width, j+=InWidth) {
f = InMask+j+InWidth;
for(a = InMask+j, b=mask+i; a < f; a++, b++) {
if(*a == (UChar) DWT_IN) *b = DWT_IN;
}
}
*OutWidth = out_width;
*OutHeight = out_height;
*OutMask = mask;
return(DWT_OK);
}
/* Function: PutBox()
Description: put the bounding box of the image object back
Input:
InImage -- Input image data, data type defined by DataType;
InMask -- Input image mask
RealWidth, RealHeight -- the size of the actual image;
DataType -- 0 - UChar 1- UShort for InImage and OutImage
Shape -- if -1, the SHAPE is rectangular, else arbitary shape and
Shape defines the mask value of an object, useful for
multiple objects coding
VirtualWidth, VirtualHeight -- size of the output image
OriginX, OriginY -- origins of the output image relative to the original
image
Output:
OutImage -- Output image contains the bounding box image
OutMask -- Output image mask
NULL if Shape == RECTANGULAR;
Return: DWT_OK if no error;
*/
Int VTCIMAGEBOX:: PutBox(Void *InImage, UChar *InMask, Void *OutImage, UChar *OutMask,
Int RealWidth, Int RealHeight,
Int VirtualWidth, Int VirtualHeight,
Int OriginX, Int OriginY, Int DataType, Int Shape)
{
Int origin_x, origin_y;
Int virtual_width, virtual_height;
UChar *data, *indata;
UChar *mask = NULL;
Int wordsize = (DataType==DWT_USHORT_ENUM)?2:1;
Int i, j;
Int real_width, real_height;
Int max_x, max_y;
Int rows, cols;
UChar *a, *b, *c, *f;
real_width = RealWidth;
real_height = RealHeight;
virtual_width = VirtualWidth;
virtual_height = VirtualHeight;
origin_x = OriginX;
origin_y = OriginY;
/* allocate proper memory and initialize to zero*/
data = (UChar *)OutImage;
memset(data, (Char )0, sizeof(UChar)*real_width*real_height*wordsize);
if(Shape != RECTANGULAR) {
mask = OutMask;
memset(mask, (Char)0, sizeof(UChar)*real_width*real_height);
}
/* calculate clip area */
max_y = origin_y+virtual_height;
max_y = (max_y<real_height)?max_y:real_height;
rows = max_y - origin_y;
max_x = origin_x+virtual_width;
max_x = (max_x<real_width)?max_x:real_width;
cols = max_x - origin_x;
indata = (UChar *)InImage;
/* fill out data */
for(i=0, j=origin_y*real_width+origin_x; i< rows*virtual_width;
i+=virtual_width, j+=real_width) {
f = InMask+i+cols;
for(a = data +j*wordsize, b = indata + i*wordsize, c= InMask+i;
c< f; c++, a+=wordsize, b+=wordsize) {
if(*c == DWT_IN) {
memcpy(a, b, wordsize);
}
}
if(Shape != RECTANGULAR) {
for(a=InMask+i, b = mask+j; a<f; a++, b++) {
if(*a == DWT_IN) *b=(UChar) Shape;
}
}
}
return(DWT_OK);
}
/* find the least common multiples of two Integers */
Int VTCIMAGEBOX:: LCM(Int x, Int y)
{
return(x*y/GCD(x,y));
}
/* find the greatest common divisor of two Integers */
Int VTCIMAGEBOX:: GCD(Int x, Int y)
{
Int i;
Int k;
Int d=x<y?x:y; /* the lesser of x and y */
d = (Int) sqrt((double)d)+1;
k = 1;
for(i=d;i>1;i--) {
if(x%i==0 && y%i==0) {
k=i;
break;
}
}
return(k);
}
/* Function: SubsampleMask()
Description: Subsample the Mask;
Input:
InMask -- Input mask;
Nx -- Horizontal subsampling rate;
Ny -- vertical subsampling rate;
Shape -- Integer number specify the object in a mask;
Output:
OutMask -- Output mask, memory space provided by caller function;
return: DWT_OK if no error;
*/
Void VTCIMAGEBOX:: SubsampleMask(UChar *InMask, UChar **OutMask,
Int Width, Int Height,
FILTER *filter)
{
UChar *a, *b;
Int width, height;
Int i,j, k;
width = (Width >>1);
height = (Height >>1);
a = (UChar *)malloc(Width*Height*sizeof(UChar));
b = (UChar *)malloc(width*height*sizeof(UChar));
if(a == NULL || b == NULL)
exit(printf("Error allocation memory\n"));
// ret=do_DWTMask(InMask, a, Width, Height, 1, &filter);
// if(ret!=DWT_OK)
// exit(printf("DWT error code = %d\n", ret));
for(i=0,j=0, k=0; i< height; i++,j+=Width, k+=width){
memcpy(b+k, a+j, width);
}
free(a);
*OutMask = b;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -