00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include "vid_sim.h"
00045 #include "video_codec.h"
00046 extern video_codec *VidSt;
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
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
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
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
00102
00103
00104
00105
00106
00107
00108
00109
00110
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
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 void WriteImage(PictImage *image, char *filename)
00143
00144 {
00145 int status;
00146 FILE *f_out;
00147
00148
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
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
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
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
00181
00182
00183
00184
00185
00186
00187
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
00222
00223
00224
00225
00226
00227
00228
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