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

📄 io.c

📁 基于H.263的图像压缩编解码的C源码
💻 C
字号:
/************************************************************************
 *
 *  io.c, part of tmn (TMN encoder)
 *  
 ************************************************************************/

#include"sim.h"

/**********************************************************************
 *
 *      Name:         Check_image_sequence
 *      Description:  Check image sequence from disk
 *	
 *      Input:        filename of sequence, frame of start and end to
 *                    be read, headerlength of sequence
 *      Returns:      True if image sequence are right, then False will
 *                    return
 *      Side effects: retuen True or False
 *
 ***********************************************************************/

int Check_image_sequence(char *filename, int frame_start, int frame_end, int headerlength)
{
  FILE *im_file = NULL;
  int i, error=1;
  char tmp_Y[80], tmp_U[80], tmp_V[80];

  for(i=frame_start;i<=frame_end;i++){
        sprintf(tmp_Y,"%s%d.Y",filename,i);
        sprintf(tmp_U,"%s%d.U",filename,i);
        sprintf(tmp_V,"%s%d.V",filename,i);

        im_file = fopen(tmp_Y,"rb");
        if(im_file==NULL){
                error = 0;
                printf("Don't open image %s !\n", tmp_Y);
        }
		fseek(im_file,0L,SEEK_END);
        if(ftell(im_file) != (long)(pels*lines)){
                error = 0;
                printf("File %s size is mismatching !\n", tmp_Y);
        }
        fclose(im_file);

        im_file = fopen(tmp_U,"rb");
        if(im_file==NULL){
                error = 0;
                printf("Don't open image %s !\n", tmp_U);
        }
		fseek(im_file,0L,SEEK_END);
        if(ftell(im_file) != (long)(pels*lines/4)){
                error = 0;
                printf("File %s size is mismatching !\n", tmp_U);
        }
        fclose(im_file);

        im_file = fopen(tmp_V,"rb");
        if(im_file==NULL){
                error = 0;
                printf("Don't open image %s !\n", tmp_V);
        }
		fseek(im_file,0L,SEEK_END);
        if(ftell(im_file) != (long)(pels*lines/4)){
                error = 0;
                printf("File %s size is mismatching !\n", tmp_V);
        }
        fclose(im_file);
        if(error == 0) break;
  }
  if(error == 1) return(1);
  else return(0);
}

/**********************************************************************
 *
 *      Name:         ReadImage        
 *      Description:  Reads one raw 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: 980629
 *
 ***********************************************************************/

unsigned char *ReadImage(char *filename, int frame_no, int headerlength)

{
  FILE *im_file = NULL;
  int im_size = pels*lines*3/2;
  unsigned char *raw, *raw_Y, *raw_U, *raw_V;
  char tmp_Y[80], tmp_U[80],tmp_V[80];
  int status;

  if ((raw = (unsigned char *)malloc(sizeof(char)*im_size)) == NULL) {
    fprintf(stderr,"Couldn't allocate memory to image !\n");
    exit(-1);
  }

  if ((raw_Y = (unsigned char *)malloc(sizeof(char)*pels*lines)) == NULL) {
    fprintf(stderr,"Couldn't allocate memory to image .Y !\n");
    exit(-1);
  }

  if ((raw_U = (unsigned char *)malloc(sizeof(char)*(pels*lines)/4)) == NULL) {
    fprintf(stderr,"Couldn't allocate memory to image .U !\n");
    exit(-1);
  }

  if ((raw_V = (unsigned char *)malloc(sizeof(char)*(pels*lines)/4)) == NULL) {
    fprintf(stderr,"Couldn't allocate memory to image .V !\n");
    exit(-1);
  }

  sprintf(tmp_Y,"%s%d.Y",filename,frame_no);
  sprintf(tmp_U,"%s%d.U",filename,frame_no);
  sprintf(tmp_V,"%s%d.V",filename,frame_no);

  fprintf(stdout,"Reading image no: %d\n",frame_no);
  if(global_file_save_flag == 1)
     fprintf(message_in,"%d,",frame_no);

  /* ------------ Read Y compoent to buffer ----------- */
  im_file = fopen(tmp_Y,"rb");
  if ((status = fread(raw_Y, sizeof(char), 
              pels*lines, im_file)) != pels*lines) {
    fprintf(stderr,"Error in reading image no: %d.Y\n",frame_no);
    fprintf(stderr,"From file: %s.\n",filename);
    exit(-1);
  }
  fclose(im_file);

  /* ------------ Read U compoent to buffer ----------- */
  im_file = fopen(tmp_U,"rb");
  if ((status = fread(raw_U, sizeof(char), 
              (pels*lines)/4, im_file)) != (pels*lines)/4) {
    fprintf(stderr,"Error in reading image no: %d.\n",frame_no);
    fprintf(stderr,"From file: %s.\n",filename);
    exit(-1);
  }
  fclose(im_file);

  /* ------------ Read V compoent to buffer ----------- */
  im_file = fopen(tmp_V,"rb");
  if ((status = fread(raw_V, sizeof(char), 
              (pels*lines)/4, im_file)) != (pels*lines)/4) {
    fprintf(stderr,"Error in reading image no: %d.\n",frame_no);
    fprintf(stderr,"From file: %s.\n",filename);
    exit(-1);
  }
  fclose(im_file);

  memcpy(raw,raw_Y,pels*lines);
  memcpy(raw+pels*lines,raw_U,(pels*lines)/4);
  memcpy(raw+pels*lines+(pels*lines)/4,raw_V,(pels*lines)/4);

  return raw;
}

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

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:	
 *
 ***********************************************************************/

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

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

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 + -