⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 picture_functions.c

📁 H.263的压缩算法
💻 C
📖 第 1 页 / 共 2 页
字号:
/********************************************************************** * *	Name:        InterpolateImage *	Description:    Interpolates a complete image for easier half *                      pel prediction *	 *	Input:	        pointer to image structure *	Returns:        pointer to interpolated image *	Side effects:   allocates memory to interpolated image * *	Date: 950207        Author: Karl.Lillevold@nta.no * ***********************************************************************/unsigned char *InterpolateImage(unsigned char *image, int width, int height, int RTYPE){  unsigned char *ipol_image, *ii, *oo;  int i,j;  ipol_image = (unsigned char *)malloc(sizeof(char)*width*height*4);  ii = ipol_image;  oo = image;  /* main image */  for (j = 0; j < height-1; j++) {    for (i = 0; i  < width-1; i++) {      *(ii + (i<<1)) = *(oo + i);      *(ii + (i<<1)+1) = (*(oo + i) + *(oo + i + 1) + 1 - RTYPE)>>1;      *(ii + (i<<1)+(width<<1)) = (*(oo + i) + *(oo + i + width) + 1 - RTYPE)>>1;      *(ii + (i<<1)+1+(width<<1)) = (*(oo+i) + *(oo+i+1) +          *(oo+i+width) + *(oo+i+1+width) + 2 - RTYPE)>>2;    }    /* last pels on each line */    *(ii+ (width<<1) - 2) = *(oo + width - 1);    *(ii+ (width<<1) - 1) = *(oo + width - 1);    *(ii+ (width<<1)+ (width<<1)-2) = (*(oo+width-1)+*(oo+width+width-1)+1-RTYPE)>>1;    *(ii+ (width<<1)+ (width<<1)-1) = (*(oo+width-1)+*(oo+width+width-1)+1-RTYPE)>>1;    ii += (width<<2);    oo += width;  }  /* last lines */  for (i = 0; i < width-1; i++) {    *(ii+ (i<<1)) = *(oo + i);        *(ii+ (i<<1)+1) = (*(oo + i) + *(oo + i + 1) + 1 - RTYPE)>>1;    *(ii+ (width<<1)+ (i<<1)) = *(oo + i);        *(ii+ (width<<1)+ (i<<1)+1) = (*(oo + i) + *(oo + i + 1) + 1 - RTYPE)>>1;            }  /* bottom right corner pels */  *(ii + (width<<1) - 2) = *(oo + width -1);  *(ii + (width<<1) - 1) = *(oo + width -1);  *(ii + (width<<2) - 2) = *(oo + width -1);  *(ii + (width<<2) - 1) = *(oo + width -1);  return ipol_image;}/********************************************************************** * *	Name:        MakeEdgeImage *	Description:    Copies edge pels for use with unrestricted *                      motion vector mode *	 *	Input:	        pointer to source image, destination image *                      width, height, edge *	Returns:        *	Side effects: * *	Date: 950219        Author: Karl.Lillevold@nta.no * ***********************************************************************/void MakeEdgeImage(unsigned char *src, unsigned char *dst, int width,           int height, int edge){  int i,j;  unsigned char *p1,*p2,*p3,*p4;  unsigned char *o1,*o2,*o3,*o4;  /* center image */  p1 = dst;  o1 = src;  for (j = 0; j < height;j++) {    memcpy(p1,o1,width);    p1 += width + (edge<<1);    o1 += width;  }  /* left and right edges */  p1 = dst-1;  o1 = src;  for (j = 0; j < height;j++) {    for (i = 0; i < edge; i++) {      *(p1 - i) = *o1;      *(p1 + width + i + 1) = *(o1 + width - 1);    }    p1 += width + (edge<<1);    o1 += width;  }          /* top and bottom edges */  p1 = dst;  p2 = dst + (width + (edge<<1))*(height-1);  o1 = src;  o2 = src + width*(height-1);  for (j = 0; j < edge;j++) {    p1 = p1 - (width + (edge<<1));    p2 = p2 + (width + (edge<<1));    for (i = 0; i < width; i++) {      *(p1 + i) = *(o1 + i);      *(p2 + i) = *(o2 + i);    }  }      /* corners */  p1 = dst - (width+(edge<<1)) - 1;  p2 = p1 + width + 1;  p3 = dst + (width+(edge<<1))*(height)-1;  p4 = p3 + width + 1;  o1 = src;  o2 = o1 + width - 1;  o3 = src + width*(height-1);  o4 = o3 + width - 1;  for (j = 0; j < edge; j++) {    for (i = 0; i < edge; i++) {      *(p1 - i) = *o1;      *(p2 + i) = *o2;      *(p3 - i) = *o3;      *(p4 + i) = *o4;     }    p1 = p1 - (width + (edge<<1));    p2 = p2 - (width + (edge<<1));    p3 = p3 + width + (edge<<1);    p4 = p4 + width + (edge<<1);  }}/********************************************************************** * *	Name:		ReadImage *	Description:	Reads one qcif image from disk * *	Input:		filename of sequence, frame no. to be read, *			headerlength of sequence *	Returns:	Pointer to start of raw YUV-data *	Side effects:	Memory allocated to image-data * *	Date: 940108	Author:	Karl.Lillevold@nta.no * *      Modified:       guyc@ee.ubc.ca 8/97 to read yuv files if necessary * ***********************************************************************/unsigned char * ReadImage (char *filename, int frame_no, int headerlength){  FILE *im_file = NULL;  int im_size = pels * lines * 3 / 2;  unsigned char *qcif;  int status;  char YUVname[255], tmp[255];  FILE *YUVfile;  if ((qcif = (unsigned char *) malloc (sizeof (char) * im_size)) == NULL)  {    fprintf (stderr, "Couldn't allocate memory to image\n");    exit (-1);  }  if ((im_file = fopen (filename, "rb")) == NULL)  {    /* Try using separate file names (yuv files) */    sprintf (YUVname, filename, frame_no);    strcpy (tmp, YUVname);    sprintf (YUVname, "%s.yuv", tmp);    if ((YUVfile = fopen (YUVname, "rb")) == NULL)    {      fprintf (stderr, "Unable to open image_file yuv: %s\n", YUVname);      exit (-1);    }    fprintf (stdout, "Reading image no: %d\n", frame_no);    if ((status = fread (qcif, sizeof (char), im_size, YUVfile)) != im_size)    {      fclose (YUVfile);      fprintf (stderr, "Error in reading yuv image from %s \n", YUVname);      exit (-1);    }    fclose (YUVfile);  } else  {    rewind (im_file);    /* Find the correct image */    status = fseek (im_file, (headerlength + frame_no * im_size), 0);    if (status != 0)    {      fprintf (stderr, "Error in seeking image no: %d\n", frame_no);      fprintf (stderr, "From file: %s\n", filename);      exit (-1);    }    /* Read image */    fprintf (stdout, "Reading image no: %d\n", frame_no);    if ((status = fread (qcif, sizeof (char), im_size, im_file)) != im_size)    {      fprintf (stderr, "Error in reading image no: %d\n", frame_no);      fprintf (stderr, "From file: %s\n", filename);      exit (-1);    }    fclose (im_file);  }  return (qcif);}/********************************************************************** * *	Name:		FillImage *	Description:	fills Y, Cb and Cr of a PictImage struct * *	Input:		pointer to raw image * *	Returns:	pointer to filled PictImage *	Side effects:	allocates memory to PictImage *                      raw image is freed * *	Date: 940109	Author:	Karl.Lillevold@nta.no * ***********************************************************************/PictImage * FillImage (unsigned char *in){  PictImage *Pict;  Pict = InitImage (pels * lines);  memcpy (Pict->lum, in, pels * lines);  memcpy (Pict->Cb, (in + pels * lines), pels * lines / 4);  memcpy (Pict->Cr, (in + pels * lines + pels * lines / 4), pels * lines / 4);  free (in);  return (Pict);}/********************************************************************** * *	Name:		WriteImage *	Description:	Writes PictImage struct to disk * *	Input:		pointer to image data to be stored, filename *			to be used on the disk, image size *	Returns: *	Side effects: * *	Date: 930115	Author: Karl.Lillevold@nta.no * ***********************************************************************/void WriteImage (PictImage * image, char *filename){  int status;  FILE *f_out;  /* Opening file */  if ((f_out = fopen (filename, "ab")) == NULL)  {    fprintf (stderr, "%s%s\n", "Error in opening file: ", filename);    exit (-1);  }  /* Writing lum to file */  if ((status = fwrite (image->lum, sizeof (char), pels * lines, f_out)) != pels * lines)  {    fprintf (stderr, "%s%s\n", "Error in writing to file: ", filename);    exit (-1);  }  /* Writing Cb to file */  if ((status = fwrite (image->Cb, sizeof (char), pels * lines / 4, f_out)) != pels * lines / 4)  {    fprintf (stderr, "%s%s\n", "Error in writing to file: ", filename);    exit (-1);  }  /* Writing Cr to file */  if ((status = fwrite (image->Cr, sizeof (char), pels * lines / 4, f_out)) != pels * lines / 4)  {    fprintf (stderr, "%s%s\n", "Error in writing to file: ", filename);    exit (-1);  }  fclose (f_out);  return;}/********************************************************************** * *	Name:		InitImage *	Description:	Allocates memory for structure of 4:2:0-image * *	Input:	        image size *	Returns:	pointer to new structure *	Side effects:	memory allocated to structure * *	Date: 930115  	Author: Karl.Lillevold@nta.no * ***********************************************************************/PictImage * InitImage (int size){  PictImage *new;  if ((new = (PictImage *) malloc (sizeof (PictImage))) == NULL)  {    fprintf (stderr, "Couldn't allocate (PictImage *)\n");    exit (-1);  }  if ((new->lum = (unsigned char *) malloc (sizeof (char) * size)) == NULL)  {    fprintf (stderr, "Couldn't allocate memory for luminance\n");    exit (-1);  }  if ((new->Cr = (unsigned char *) malloc (sizeof (char) * size / 4)) == NULL)  {    fprintf (stderr, "Couldn't allocate memory for Cr\n");    exit (-1);  }  if ((new->Cb = (unsigned char *) malloc (sizeof (char) * size / 4)) == NULL)  {    fprintf (stderr, "Couldn't allocate memory for Cb\n");    exit (-1);  }  return (new);}/********************************************************************** * *	Name:		FreeImage *	Description:	Frees memory allocated to structure of 4:2:0-image * *	Input:		pointer to structure *	Returns: *	Side effects:	memory of structure freed * *	Date: 930115	Author: Karl.Lillevold@nta.no * ***********************************************************************/void FreeImage (PictImage * image){  free (image->lum);  free (image->Cr);  free (image->Cb);  free (image);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -