mom_util.c
上传用户:tuheem
上传日期:2007-05-01
资源大小:21889k
文件大小:6k
- #include "mom_util.h"
- Char *emalloc(Int n);
- Char *ecalloc(Int n, Int s);
- Char *erealloc(Char *p, Int n);
- Void CopyImageI(ImageI *image_in, ImageI *image_out);
- Void CopyImageF(ImageF *image_in, ImageF *image_out);
- Void SetConstantImageI(ImageI *image, SInt val);
- Void SetConstantImageF(ImageF *image, Float val);
- Void SubImageI(ImageI *image_in1, ImageI *image_in2, ImageI *image_out);
- Void SubImageF(ImageF *image_in1, ImageF *image_in2, ImageF *image_out);
- Image *
- AllocImage(UInt size_x, UInt size_y, ImageType type)
- {
- Image *image;
-
- image = (Image *) emalloc(sizeof(Image));
-
- image->version = VERSION;
- image->x = size_x;
- image->y = size_y;
- image->upperodd = 0;
- image->grid = 's';
- image->type = type;
- image->data = (ImageData *) emalloc(sizeof(ImageData));
-
- switch(type)
- {
- case SHORT_TYPE:
- image->data->s = (SInt *) ecalloc(size_x*size_y,sizeof(SInt));
- break;
- case FLOAT_TYPE:
- image->data->f = (Float *) ecalloc(size_x*size_y,sizeof(Float));
- break;
- case UCHAR_TYPE:
- image->data->u = (UChar *) ecalloc(size_x*size_y,sizeof(UChar));
- break;
- }
-
- image->f = image->data->s;
-
- return(image);
- }
- Void
- FreeImage(Image *image)
- {
- SInt *ps;
- Float *pf;
- UChar *pu;
- if (image == NULL) return;
- switch(image->type)
- {
- case SHORT_TYPE:
- ps = (SInt *)GetImageData(image);
- if(ps != NULL) free((Char *)ps);
- free((Char *) image->data);
- free((Char *)image);
- break;
- case FLOAT_TYPE:
- pf = (Float *)GetImageData(image);
- if(pf != NULL) free((Char *)pf);
- free((Char *) image->data);
- free((Char *)image);
- break;
- case UCHAR_TYPE:
- pu = (UChar *)GetImageData(image);
- if(pu != NULL) free((Char *)pu);
- free((Char *) image->data);
- free((Char *)image);
- break;
- }
- }
- Void
- CopyImage(Image *image_in, Image *image_out)
- {
- switch(image_out->type)
- {
- case SHORT_TYPE:
- CopyImageI(image_in,image_out);
- break;
- case FLOAT_TYPE:
- CopyImageF(image_in,image_out);
- break;
- }
- }
- Void
- CopyImageI(ImageI *image_in, ImageI *image_out)
- {
- SInt *p_in = image_in->data->s,
- *p_out = image_out->data->s,
- *p_end;
- UInt sx_in = image_in->x,
- sx_out = image_out->x,
- sy_in = image_in->y,
- sy_out = image_out->y,
- sxy_in = sx_in * sy_in;
- p_end = p_in + sxy_in;
- while (p_in != p_end)
- {
- *p_out = *p_in;
- p_in++;
- p_out++;
- }
- }
- Void
- CopyImageF(ImageF *image_in, ImageF *image_out)
- {
- Float *p_in = image_in->data->f,
- *p_out = image_out->data->f,
- *p_end;
- UInt sx_in = image_in->x,
- sx_out = image_out->x,
- sy_in = image_in->y,
- sy_out = image_out->y,
- sxy_in = sx_in * sy_in;
- p_end = p_in + sxy_in;
- while (p_in != p_end)
- {
- *p_out = *p_in;
- p_in++;
- p_out++;
- }
- }
- Void
- SetConstantImage(Image *image, Float val)
- {
- switch(image->type)
- {
- case SHORT_TYPE:
- SetConstantImageI(image,(SInt)val);
- break;
- case FLOAT_TYPE:
- SetConstantImageF(image,val);
- break;
- }
- }
- Void
- SetConstantImageI(ImageI *image, SInt val)
- {
- SInt *p = image->data->s,
- *p_end;
- UInt sxy = image->x * image->y;
- if (val == 0)
- memset (p, 0, sxy * 2);
- else
- {
- p_end = p + sxy;
- while (p != p_end)
- {
- *p = val;
- p++;
- }
- }
- }
- Void
- SetConstantImageF(ImageF *image, Float val)
- {
- Float *p = image->data->f,
- *p_end;
- UInt sxy = image->x * image->y;
- p_end = p + sxy;
- while (p != p_end)
- {
- *p = val;
- p++;
- }
- }
- Void
- SubImage(Image *image_in1, Image *image_in2, Image *image_out)
- {
- switch(image_in1->type)
- {
- case SHORT_TYPE:
- SubImageI(image_in1,image_in2,image_out);
- break;
- case FLOAT_TYPE:
- SubImageF(image_in1,image_in2,image_out);
- break;
- }
- }
- Void
- SubImageI(ImageI *image_in1, ImageI *image_in2, ImageI *image_out)
- {
- SInt *p = image_out->data->s,
- *p1 = image_in1->data->s,
- *p2 = image_in2->data->s,
- *p_end;
- UInt sx_in1 = image_in1->x,
- sx_in2 = image_in2->x,
- sx_out = image_out->x,
- sy_in1 = image_in1->y,
- sy_in2 = image_in2->y,
- sy_out = image_out->y,
- sxy = sx_out * sy_out;
- p_end = p + sxy;
- while (p != p_end)
- {
- *p = *p1 - *p2;
- p++;
- p1++;
- p2++;
- }
- }
- Void
- SubImageF(ImageF *image_in1, ImageF *image_in2, ImageF *image_out)
- {
- Float *p = image_out->data->f,
- *p1 = image_in1->data->f,
- *p2 = image_in2->data->f,
- *p_end;
- UInt sx_in1 = image_in1->x,
- sx_in2 = image_in2->x,
- sx_out = image_out->x,
- sy_in1 = image_in1->y,
- sy_in2 = image_in2->y,
- sy_out = image_out->y,
- sxy = sx_out * sy_out;
- p_end = p + sxy;
- while (p != p_end)
- {
- *p = *p1 - *p2;
- p++;
- p1++;
- p2++;
- }
- }
- Vop *
- SallocVop()
- {
- Vop *vop;
- vop = (Vop *)ecalloc(1,sizeof(Vop));
- return(vop);
- }
- Vop *
- AllocVop(UInt x, UInt y)
- {
- Vop *vop;
- Image *y_chan,
- *u_chan,
- *v_chan;
-
- vop = SallocVop();
- vop->width = x;
- vop->height = y;
-
- y_chan = AllocImage(x,y,SHORT_TYPE);
- u_chan = AllocImage(x/2,y/2,SHORT_TYPE);
- v_chan = AllocImage(x/2,y/2,SHORT_TYPE);
-
- FreeImage(vop->y_chan);
- vop->y_chan = y_chan;
- FreeImage(vop->u_chan);
- vop->u_chan = u_chan;
- FreeImage(vop->v_chan);
- vop->v_chan = v_chan;
- return(vop);
- }
- Void
- SfreeVop (Vop *vop)
- {
- free ((Char*)vop);
-
- return;
- }
- Void
- FreeVop(Vop *vop)
- {
- Image *data=NULL;
- if(vop != NULL) {
-
- data = vop->y_chan;
- FreeImage(data);
- data = vop->u_chan;
- FreeImage(data);
- data = vop->v_chan;
- FreeImage(data);
- SfreeVop(vop);
- }
- return;
- }
- Void
- CopyVopNonImageField(Vop *in,Vop *out)
- {
- out->prediction_type = in->prediction_type;
- }
- Char *
- emalloc(Int n)
- {
- Char *p;
-
- p = (Char *) malloc((UInt)n);
- return p;
- }
- Char *
- ecalloc(Int n, Int s)
- {
- Char *p;
-
- p = (Char *) calloc((UInt)n,(UInt)s);
-
- return p;
- }
- Char *
- erealloc(Char *p, Int n)
- {
- p = (Char *) realloc(p,(UInt)n);
- return p;
- }