File: io.c

package info (click to toggle)
vic 2.8ucl4-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,864 kB
  • ctags: 9,033
  • sloc: ansic: 56,989; cpp: 44,560; tcl: 5,550; sh: 1,382; perl: 1,329; makefile: 357
file content (329 lines) | stat: -rw-r--r-- 8,948 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*****************************************************************
 *
 *  io.c, part of tmn (TMN encoder)
 *
 *  Copyright (C) 1997  University of BC, Canada
 *
 *  Contacts: 
 *  Michael Gallant                   <mikeg@ee.ubc.ca>
 *  Guy Cote                          <guyc@ee.ubc.ca>
 *  Berna Erol                        <bernae@ee.ubc.ca>
 *
 *  UBC Image Processing Laboratory   http://www.ee.ubc.ca/image
 *  2356 Main Mall                    tel.: +1 604 822 4051
 *  Vancouver BC Canada V6T1Z4        fax.: +1 604 822 5949
 *
 *  Copyright (C) 1995, 1996  Telenor R&D, Norway
 *
 *  Contacts:
 *  Robert Danielsen                  <Robert.Danielsen@nta.no>
 *
 *  Telenor Research and Development  http://www.nta.no/brukere/DVC/
 *  P.O.Box 83                        tel.:   +47 63 84 84 00
 *  N-2007 Kjeller, Norway            fax.:   +47 63 81 00 76
 *
 ************************************************************************/

/* Disclaimer of Warranty
 * 
 * These software programs are available to the user without any license fee
 * or royalty on an "as is" basis. The University of British Columbia
 * disclaims any and all warranties, whether express, implied, or
 * statuary, including any implied warranties or merchantability or of
 * fitness for a particular purpose.  In no event shall the
 * copyright-holder be liable for any incidental, punitive, or
 * consequential damages of any kind whatsoever arising from the use of
 * these programs.
 * 
 * This disclaimer of warranty extends to the user of these programs and
 * user's customers, employees, agents, transferees, successors, and
 * assigns.
 * 
 * The University of British Columbia does not represent or warrant that the
 * programs furnished hereunder are free of infringement of any
 * third-party patents.
 * 
 * Commercial implementations of H.263, including shareware, are subject to
 * royalty fees to patent holders.  Many of these patents are general
 * enough such that they are unavoidable regardless of implementation
 * design.
 * 
 */

#include        "sim.h"

#ifdef VIC
/*extern */
unsigned char *h263_frame;
#endif

/**********************************************************************
 *
 *	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)
{
#ifdef VIC
  return h263_frame;
#else
  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);
#endif
}


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

#ifdef VIC
  if (in != h263_frame)
#endif
  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)
{
#ifndef VIC
  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);
#endif
  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);
}

/**********************************************************************
 *
 *	Name:		StripName
 *	Description:	Removes characters behind '.' and in front of
 *			(including) the last '/'.
 *
 *	Input:		pointer to character string
 *	Returns:	pointer to new character string
 *	Side effects:
 *
 *	Date:	1992	Author:	Karl.Lillevold@nta.no
 *
 ***********************************************************************/


char *
 StripName (char *s)
{
  char *t;
  char *tmp;
  int counter = 0;
  int mark = 0;

  tmp = (char *) malloc (sizeof (char) * 60);
  strcpy (tmp, s);
  t = tmp;
  while ((*t != '\0') && (*t != '.') && (*t != ' '))
  {
    counter++;
    if (*t == '/')
      mark = counter;
    t++;
  }
  *t = '\0';
  strncpy (tmp, (tmp + mark), (counter - mark + 1));

  return (tmp);
}