~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/file/aiff/aiff_io.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**@(#)Functions to read/write AIFF (Aivru Image File Format) image files
  2 */
  3 
  4 #include <stdlib.h>
  5 #include <string.h>
  6 #include <values.h>
  7 #include <tina/file.h>
  8 #include <tina/sys.h>
  9 #include <tina/sysfuncs.h>
 10 #include <time.h>
 11 
 12 extern Bool fclose_2(FILE * stream, const char *pathname);
 13 extern Bool fread_imrect_data(const Imrect * imrect, FILE * stream, const char *pathname);
 14 extern Bool fread_padding(const unsigned size, FILE * stream, const char *pathname);
 15 extern Bool fwrite_imrect_data(const Imrect * imrect, FILE * stream, const char *pathname);
 16 extern Bool fwrite_padding(const unsigned size, FILE * stream, const char *pathname);
 17 extern FILE *fopen_2(const char *pathname, const char *mode);
 18 extern unsigned fread_fields(Field_info * field_info, const unsigned nfields, FILE * stream, const char *pathname);
 19 extern unsigned fwrite_fields(Field_info * field_info, const unsigned nfields, FILE * stream, const char *pathname);
 20 
 21 static Aiffhdr aiffhdr;
 22 static Field_info field_info[] =
 23 {
 24     {Field_info_id, &aiffhdr.magic, sizeof(aiffhdr.magic), 1},
 25     {Field_info_id, &aiffhdr.length, sizeof(aiffhdr.length), 1},
 26     {Field_info_id, &aiffhdr.type, sizeof(aiffhdr.type), 1},
 27     {Field_info_id, &aiffhdr.height, sizeof(aiffhdr.height), 1},
 28     {Field_info_id, &aiffhdr.width, sizeof(aiffhdr.width), 1},
 29     {Field_info_id, &aiffhdr.lx, sizeof(aiffhdr.lx), 4},
 30     {Field_info_id, &aiffhdr.source_id, sizeof(aiffhdr.source_id), 1},
 31     {Field_info_id, &aiffhdr.process_id, sizeof(aiffhdr.process_id), 1},
 32     {Field_info_id, &aiffhdr.date, sizeof(aiffhdr.date), 1},
 33     {Field_info_id, &aiffhdr.time, sizeof(aiffhdr.time), 1},
 34     {Field_info_id, &aiffhdr.title, sizeof(aiffhdr.title), 1},
 35 };
 36 static unsigned const nfields = sizeof field_info / sizeof(Field_info);
 37 
 38 
 39 /* Added data_magic for image data swab in aiff_read_image_stream
 40  * a.lacey 20.11.96
 41  */
 42 
 43 static unsigned short data_magic = AIFF_MAGIC;
 44 
 45 
 46 /* Read aiff header from stream into (static global) aiffhdr. Return
 47  * aiffhdrp (static global)(on error NULL). Handles byte reversal
 48  * problems. Leaves file descriptor at start of image data. NB DON'T
 49  * free aiffhdrp (it points to global static). */
 50 Aiffhdr *aiff_read_head(FILE * stream, const char *pathname)
 51 {
 52     Aiffhdr *aiffhdrp = NULL;
 53 
 54     unsigned nbytes = fread_fields(field_info, nfields, stream, pathname);
 55 
 56     if (nbytes)
 57     {
 58         int     padding = AIFF_HD_LEN - nbytes;
 59 
 60         aiffhdrp = &aiffhdr;
 61         /* Handle possible byte reversal */
 62         if (aiffhdr.magic == AIFF_AMIGC)
 63         {                       /* Header (but not image) byte reversed */
 64             /* swab should be defined in stdlib */
 65 /*          extern void swab(const char *from, char *to, int nbytes);
 66 NAT 1/5/95 changed for compatability with PC compiler 1/5/95
 67 */
 68 /*
 69             extern void swab(char *from, char *to, int nbytes);
 70 */
 71 
 72             swab((char *) &aiffhdr.magic, (char *) &aiffhdr.magic, nbytes);
 73 
 74 /* Added data_magic for image data swab in aiff_read_image_stream
 75  * a.lacey 20.11.96
 76  */
 77             data_magic = AIFF_AMIGC;
 78         }
 79         if (aiffhdr.magic != AIFF_MAGIC)
 80         {
 81             aiffhdrp = NULL;
 82         }
 83         (void) fread_padding(padding, stream, pathname);
 84     }
 85     return aiffhdrp;
 86 }
 87 
 88 
 89 /* Create an imrect and read an image file called 'pathname' into it.
 90  * Open file, read header, padding and data, close file. On failure,
 91  * give error message and return NULL. */
 92 Imrect *aiff_read_image_stream(FILE * stream, const char *pathname)
 93 {
 94     Imrect *imrect = NULL;
 95 
 96     Aiffhdr *aiffhdr = aiff_read_head(stream, pathname);
 97 
 98     if (aiffhdr)
 99     {
100         Imregion *imregion = roi_alloc(aiffhdr->lx, aiffhdr->ly,
101                                        aiffhdr->ux, aiffhdr->uy);
102 
103         imrect = im_alloc(aiffhdr->height, aiffhdr->width, imregion,
104                           (Vartype) aiffhdr->type);
105         rfree((void *) imregion);
106         if (!fread_imrect_data(imrect, stream, pathname))
107         {
108             im_free(imrect);
109             imrect = NULL;
110         }
111 
112 /* Reverse bytes when little endian
113  * a.lacey 20.11.96
114  */
115         if (data_magic == AIFF_AMIGC)
116           {    
117             int lx = imrect->region->lx;
118             int ux = imrect->region->ux;
119             int ly = imrect->region->ly;
120             int uy = imrect->region->uy;
121             unsigned int size = var_size(imrect->vtype);
122             int cols = ux - lx;
123             int y;
124 
125             for(y = ly; y < uy; y++)
126               {
127                 void*  row = NULL;
128                 char   temp[40];
129                 int    x;
130                 unsigned int i;
131                 
132                 IM_PIX_GETP(imrect, y, lx, row);
133         
134                 for(x = 0; x < cols; x++)
135                   {
136                     for(i = 0; i < size; i++)
137                       temp[i] = *(((char *)row) + (x*size+i));
138                     for(i = 1; i <= size; i++)
139                       *(((char *)row) + (x*size + size-i)) = temp[i-1];
140                   }
141               }
142 
143             data_magic = AIFF_MAGIC;
144           }
145     }
146     return imrect;
147 }
148 
149 
150 /* Create an imrect and read an image file called 'pathname' into it.
151  * Open file, read header, padding and data, close file. On failure,
152  * give error message and return NULL. */
153 Imrect *aiff_read_image(const char *pathname)
154 {
155     FILE   *stream = fopen_2(pathname, "rb");
156     Imrect *imrect = NULL;
157 
158     if (stream)
159     {
160         imrect = aiff_read_image_stream(stream, pathname);
161         fclose_2(stream, pathname);
162     }
163     return imrect;
164 }
165 
166 /* Write aiff header to file on stream from (static global) aiffhdr.
167  * Return write_ok (true or false). Handles byte reversal problems.
168  * Leaves file descriptor at start of image data. NB DON'T free
169  * aiffhdrp (it points to global static). */
170 Bool    aiff_write_head(Imrect * imrect, FILE * stream, const char *pathname)
171 {
172     Bool    write_ok = false;
173     Imregion *imregion = imrect->region;
174     char    times[10];
175     char    dates[10];
176     static const Aiffhdr aiffhdr_default =
177     {
178         aiffhdr_id,
179         AIFF_MAGIC,             /* magic */
180         AIFF_HD_LEN,            /* length: std header length */
181         0,                      /* type */
182         0,                      /* height */
183         0,                      /* width */
184         0,                      /* lx */
185         0,                      /* ux */
186         0,                      /* ly */
187         0,                      /* uy */
188         AIFF_UNKNOWN,           /* source_id */
189         AIFF_UNKNOWN,           /* process_id */
190         "",                     /* date */
191         "",                     /* time */
192         "No title",             /* title */
193     };
194     unsigned nbytes = 0;
195 
196     time_t  tm;
197     struct tm *tp;
198 
199     (void) time(&tm);
200     tp = localtime(&tm);
201 
202     sprintf(times, "%02d:%02d:%02d", tp->tm_hour, tp->tm_min, tp->tm_sec);
203     sprintf(dates, "%02d/%02d/%02d", tp->tm_mday, tp->tm_mon + 1, tp->tm_year);
204     aiffhdr = aiffhdr_default;
205 
206     strncpy(aiffhdr.time, times, 8);
207     strncpy(aiffhdr.date, dates, 8);
208 
209     if (imrect->height<MAXSHORT&&imrect->width<MAXSHORT)
210     {
211        aiffhdr.type = imrect->vtype;
212        aiffhdr.height = imrect->height;
213        aiffhdr.width = imrect->width;
214        aiffhdr.lx = imregion->lx;
215        aiffhdr.ly = imregion->ly;
216        aiffhdr.ux = imregion->ux;
217        aiffhdr.uy = imregion->uy;
218 
219        nbytes = fwrite_fields(field_info, nfields, stream, pathname);
220        if (nbytes)
221        {
222            int     padding_size = aiffhdr.length - nbytes;
223 
224            if (fwrite_padding(padding_size, stream, pathname))
225            {
226                write_ok = true;
227            }
228        }
229     }
230 
231     return write_ok;
232 }
233 
234 /* Write an imrect to  an image file called 'pathname'. Open file,
235  * write header, padding and data, close file. On failure, give error
236  * message and return false. NB stream must be valid. */
237 Bool    aiff_write_image_stream(Imrect * imrect, FILE * stream, const char *pathname)
238 {
239     Bool    write_ok = false;
240     extern Bool fwrite_imrect_data(const Imrect * imrect, FILE * stream, const char *pathname);
241 
242     if (imrect)
243     {
244         if (aiff_write_head(imrect, stream, pathname))
245         {
246             write_ok = fwrite_imrect_data(imrect, stream, pathname);
247         }
248     } else
249     {
250         errorf(non_fatal, "No image to write to: %s\n", pathname);
251     }
252     return write_ok;
253 }
254 
255 /* Write an imrect to  an image file called 'pathname'. Open file,
256  * write header, padding and data, close file. On failure, give error
257  * message and return false. */
258 Bool    aiff_write_image(Imrect * imrect, const char *pathname)
259 {
260     Bool    write_ok = false;
261     FILE   *stream = fopen_2(pathname, "wb");
262 
263     if (stream)
264     {
265         write_ok = aiff_write_image_stream(imrect, stream, pathname);
266         (void) fclose_2(stream, pathname);
267     }
268     return write_ok;
269 }
270 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.