Main Page | Class List | File List | Class Members | File Members

vid_io.c

Go to the documentation of this file.
00001 /************************************************************************
00002  *
00003  *  vid_io.c, part of tmn (TMN encoder)
00004  *  Copyright (C) 1995, 1996  Telenor R&D, Norway
00005  *        Karl Olav Lillevold <Karl.Lillevold@nta.no>
00006  *  
00007  *  Contacts: 
00008  *  Karl Olav Lillevold               <Karl.Lillevold@nta.no>, or
00009  *  Robert Danielsen                  <Robert.Danielsen@nta.no>
00010  *
00011  *  Telenor Research and Development  http://www.nta.no/brukere/DVC/
00012  *  P.O.Box 83                        tel.:   +47 63 84 84 00
00013  *  N-2007 Kjeller, Norway            fax.:   +47 63 81 00 76
00014  *  
00015  ************************************************************************/
00016 
00017 /*
00018  * Disclaimer of Warranty
00019  *
00020  * These software programs are available to the user without any
00021  * license fee or royalty on an "as is" basis.  Telenor Research and
00022  * Development disclaims any and all warranties, whether express,
00023  * implied, or statuary, including any implied warranties or
00024  * merchantability or of fitness for a particular purpose.  In no
00025  * event shall the copyright-holder be liable for any incidental,
00026  * punitive, or consequential damages of any kind whatsoever arising
00027  * from the use of these programs.
00028  *
00029  * This disclaimer of warranty extends to the user of these programs
00030  * and user's customers, employees, agents, transferees, successors,
00031  * and assigns.
00032  *
00033  * Telenor Research and Development does not represent or warrant that
00034  * the programs furnished hereunder are free of infringement of any
00035  * third-party patents.
00036  *
00037  * Commercial implementations of H.263, including shareware, are
00038  * subject to royalty fees to patent holders.  Many of these patents
00039  * are general enough such that they are unavoidable regardless of
00040  * implementation design.
00041  * */
00042 
00043 
00044 #include "vid_sim.h"
00045 #include "video_codec.h"
00046 extern video_codec *VidSt;
00047 
00048 /**********************************************************************
00049  *
00050  *      Name:        ReadImage        
00051  *      Description:    Reads one raw image from disk
00052  *      
00053  *      Input:        filename of sequence, frame no. to be read,
00054  *        headerlength of sequence
00055  *      Returns:        Pointer to start of raw YUV-data
00056  *      Side effects:   Memory allocated to image-data
00057  *
00058  *      Date: 940108    Author: Karl.Lillevold@nta.no
00059  *
00060  ***********************************************************************/
00061 
00062 unsigned char *ReadImage(char *filename, int frame_no, int headerlength)
00063 
00064 {
00065   FILE *im_file = NULL;
00066   int im_size = (VidSt->pels)*(VidSt->lines)*3/2;
00067   unsigned char *raw;
00068   int status;
00069 
00070   if ((raw = (unsigned char *)malloc(sizeof(char)*im_size)) == NULL) {
00071     fprintf(stderr,"Couldn't allocate memory to image\n");
00072     exit(-1);
00073   }
00074   if ((im_file = fopen(filename,"rb")) == NULL) {
00075     fprintf(stderr,"Unable to open image_file: %s\n",filename);
00076     exit(-1);
00077   }
00078   rewind(im_file);
00079   /* Find the correct image */
00080   status = fseek(im_file,headerlength + (frame_no) * im_size,0);
00081   if (status != 0) {
00082     fprintf(stderr,"Error in seeking image no: %d\n",frame_no);
00083     fprintf(stderr,"From file: %s\n",filename);
00084     exit(-1);
00085   }
00086   /* Read image */
00087   fprintf(stdout,"Reading image no: %d\n",frame_no);
00088   if ((status = fread(raw, sizeof(char), 
00089               im_size, im_file)) != im_size) {
00090     fprintf(stderr,"Error in reading image no: %d\n",frame_no);
00091     fprintf(stderr,"From file: %s\n",filename);
00092     exit(-1);
00093   }
00094 
00095   fclose(im_file);
00096   return raw;
00097 }
00098 
00099 /**********************************************************************
00100  *
00101  *      Name:        FillImage
00102  *      Description:    fills Y, Cb and Cr of a PictImage struct
00103  *      
00104  *      Input:        pointer to raw image
00105  *        
00106  *      Returns:        pointer to filled PictImage
00107  *      Side effects:   allocates memory to PictImage
00108  *                      raw image is freed
00109  *
00110  *      Date: 940109    Author: Karl.Lillevold@nta.no
00111  *
00112  ***********************************************************************/
00113 
00114 PictImage *FillImage(unsigned char *in)
00115 {
00116   PictImage *Pict;
00117 
00118   Pict = InitImage((VidSt->pels)*(VidSt->lines));
00119 
00120   memcpy(Pict->lum, in, (VidSt->pels)*(VidSt->lines));
00121   memcpy(Pict->Cb, in + (VidSt->pels)*(VidSt->lines), (VidSt->pels)*(VidSt->lines)/4);
00122   memcpy(Pict->Cr, in + (VidSt->pels)*(VidSt->lines) + (VidSt->pels)*(VidSt->lines)/4, (VidSt->pels)*(VidSt->lines)/4);
00123 
00124   free(in);
00125   return(Pict);
00126 }
00127 
00128 /**********************************************************************
00129  *
00130  *      Name:        WriteImage
00131  *      Description:    Writes PictImage struct to disk
00132  *      
00133  *      Input:        pointer to image data to be stored, filename
00134  *        to be used on the disk, image size
00135  *      Returns:        
00136  *      Side effects:   
00137  *
00138  *      Date: 930115    Author: Karl.Lillevold@nta.no
00139  *
00140  ***********************************************************************/
00141 
00142 void WriteImage(PictImage *image, char *filename)
00143 
00144 {
00145   int status;
00146   FILE *f_out;
00147 
00148   /* Opening file */
00149   if ((f_out = fopen(filename,"ab")) == NULL) {
00150     fprintf(stderr,"%s%s\n","Error in opening file: ",filename);
00151     exit(-1);
00152   }
00153 
00154   /* Writing lum to file */
00155   if ((status = fwrite(image->lum,sizeof(char),(VidSt->pels)*(VidSt->lines),f_out)) 
00156       != (VidSt->pels)*(VidSt->lines)) {
00157     fprintf(stderr,"%s%s\n","Error in writing to file: ",filename);
00158     exit(-1);
00159   }
00160   /* Writing Cb to file */
00161   if ((status = fwrite(image->Cb,sizeof(char),(VidSt->pels)*(VidSt->lines)/4,f_out)) 
00162       != (VidSt->pels)*(VidSt->lines)/4) {
00163     fprintf(stderr,"%s%s\n","Error in writing to file: ",filename);
00164     exit(-1);
00165   }
00166   /* Writing Cr to file */
00167   if ((status = fwrite(image->Cr,sizeof(char),(VidSt->pels)*(VidSt->lines)/4,f_out)) 
00168       != (VidSt->pels)*(VidSt->lines)/4) {
00169     fprintf(stderr,"%s%s\n","Error in writing to file: ",filename);
00170     exit(-1);
00171   }
00172 
00173   fclose(f_out);
00174   return;
00175 }
00176 
00177 
00178 /**********************************************************************
00179  *
00180  *      Name:        InitImage
00181  *      Description:    Allocates memory for structure of 4:2:0-image
00182  *      
00183  *      Input:          image size
00184  *      Returns:        pointer to new structure
00185  *      Side effects:   memory allocated to structure
00186  *
00187  *      Date: 930115        Author: Karl.Lillevold@nta.no
00188  *
00189  ***********************************************************************/
00190 
00191 PictImage *InitImage(int size)
00192 {
00193   PictImage *new;
00194 
00195   if ((new = (PictImage *)malloc(sizeof(PictImage))) == NULL) {
00196     fprintf(stderr,"Couldn't allocate (PictImage *)\n");
00197     exit(-1);
00198   }
00199   if ((new->lum = (unsigned char *)malloc(sizeof(char)*size)) 
00200       == NULL) {
00201     fprintf(stderr,"Couldn't allocate memory for luminance\n");
00202     exit(-1);
00203   }
00204   if ((new->Cr = (unsigned char *)malloc(sizeof(char)*size/4)) 
00205       == NULL) {
00206     fprintf(stderr,"Couldn't allocate memory for Cr\n");
00207     exit(-1);
00208   }
00209   if ((new->Cb = (unsigned char *)malloc(sizeof(char)*size/4)) 
00210       == NULL) {
00211     fprintf(stderr,"Couldn't allocate memory for Cb\n");
00212     exit(-1);
00213   }
00214 
00215   return new;
00216 }
00217 
00218 
00219 /**********************************************************************
00220  *
00221  *      Name:        FreeImage
00222  *      Description:    Frees memory allocated to structure of 4:2:0-image
00223  *      
00224  *      Input:        pointer to structure
00225  *      Returns:
00226  *      Side effects:   memory of structure freed
00227  *
00228  *      Date: 930115    Author: Karl.Lillevold@nta.no
00229  *
00230  ***********************************************************************/
00231 
00232 void FreeImage(PictImage *image)
00233 
00234 {
00235   free(image->lum);
00236   free(image->Cr);
00237   free(image->Cb);
00238   free(image);
00239 }
00240 

Generated on Sun Jul 16 16:27:45 2006 by  doxygen 1.3.9.1