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

Linux Cross Reference
Tina6/tina-libs/tina/file/fileAiff_io.c

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

  1 /**********
  2  * 
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  * 
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-libs/tina/file/fileAiff_io.c,v $
 37  * Date    :  $Date: 2007/02/15 01:52:29 $
 38  * Version :  $Revision: 1.5 $
 39  * CVS Id  :  $Id: fileAiff_io.c,v 1.5 2007/02/15 01:52:29 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes   :
 44  * 
 45  *  Functions to read/write AIFF (Aivru Image File Format) image files
 46  *
 47  *********
 48 */
 49 
 50 #if HAVE_CONFIG_H
 51   #include <config.h>
 52 #endif
 53 
 54 #include <stdlib.h>
 55 #include <string.h>
 56 #include <time.h>
 57 #include <limits.h>
 58 #include <unistd.h>
 59 #include <tina/sys/sysPro.h>
 60 #include <tina/sys/sysDef.h>
 61 #include <tina/image/imgPro.h>
 62 #include <tina/image/imgDef.h>
 63 #include <tina/file/file_IffDef.h>
 64 #include <tina/file/file_AiffDef.h>
 65 #include <tina/file/file_UtilPro.h>
 66 #include "fileAiff_io.h"
 67 
 68 static Aiffhdr aiffhdr;
 69 static Field_info field_info[] =
 70 {
 71     {Field_info_id, &aiffhdr.magic, sizeof(aiffhdr.magic), 1},
 72     {Field_info_id, &aiffhdr.length, sizeof(aiffhdr.length), 1},
 73     {Field_info_id, &aiffhdr.type, sizeof(aiffhdr.type), 1},
 74     {Field_info_id, &aiffhdr.height, sizeof(aiffhdr.height), 1},
 75     {Field_info_id, &aiffhdr.width, sizeof(aiffhdr.width), 1},
 76     {Field_info_id, &aiffhdr.lx, sizeof(aiffhdr.lx), 4},
 77     {Field_info_id, &aiffhdr.source_id, sizeof(aiffhdr.source_id), 1},
 78     {Field_info_id, &aiffhdr.process_id, sizeof(aiffhdr.process_id), 1},
 79     {Field_info_id, &aiffhdr.date, sizeof(aiffhdr.date), 1},
 80     {Field_info_id, &aiffhdr.time, sizeof(aiffhdr.time), 1},
 81     {Field_info_id, &aiffhdr.title, sizeof(aiffhdr.title), 1},
 82 };
 83 static unsigned const nfields = sizeof field_info / sizeof(Field_info);
 84 
 85 
 86 /* Added data_magic for image data swab in aiff_read_image_stream
 87  * a.lacey 20.11.96
 88  */
 89 
 90 static unsigned short data_magic = AIFF_MAGIC;
 91 
 92 /* Read aiff header from stream into (static global) aiffhdr. Return
 93  * aiffhdrp (static global)(on error NULL). Handles byte reversal
 94  * problems. Leaves file descriptor at start of image data. NB DON'T
 95  * free aiffhdrp (it points to global static). */
 96 Aiffhdr *aiff_read_head(FILE * stream, const char *pathname)
 97 {
 98     Aiffhdr *aiffhdrp = NULL;
 99 
100     unsigned nbytes = fread_fields(field_info, nfields, stream, pathname);
101 
102     if (nbytes)
103     {
104         int     padding = AIFF_HD_LEN - nbytes;
105 
106         aiffhdrp = &aiffhdr;
107         /* Handle possible byte reversal */
108         if (aiffhdr.magic == AIFF_AMIGC)
109         {   /* Header (but not image) byte reversed
110                swab should be defined in stdlib 
111                extern void swab(const char *from, char *to, int nbytes);
112                NAT 1/5/95 changed for compatability with PC compiler 1/5/95
113                extern void swab(char *from, char *to, int nbytes);
114                No... defined in unistd.h PAB 23/1/2005
115                Look at this: a decade of TINA development in a single comment! */
116 
117             swab((char *) &aiffhdr.magic, (char *) &aiffhdr.magic, nbytes);
118 
119 /* Added data_magic for image data swab in aiff_read_image_stream
120  * a.lacey 20.11.96
121  */
122             data_magic = AIFF_AMIGC;
123         }
124         if (aiffhdr.magic != AIFF_MAGIC)
125         {
126             aiffhdrp = NULL;
127         }
128         (void) fread_padding(padding, stream, pathname);
129     }
130     return aiffhdrp;
131 }
132 
133 
134 /* Create an imrect and read an image file called 'pathname' into it.
135  * Open file, read header, padding and data, close file. On failure,
136  * give error message and return NULL. */
137 Imrect *aiff_read_image_stream(FILE * stream, const char *pathname)
138 {
139     Imrect *imrect = NULL;
140 
141     Aiffhdr *aiffhdr = aiff_read_head(stream, pathname);
142 
143     if (aiffhdr)
144     {
145         Imregion *imregion = roi_alloc(aiffhdr->lx, aiffhdr->ly,
146                                        aiffhdr->ux, aiffhdr->uy);
147 
148         imrect = im_alloc(aiffhdr->height, aiffhdr->width, imregion,
149                           (Vartype) aiffhdr->type);
150         rfree((void *) imregion);
151         if (!fread_imrect_data(imrect, stream, pathname))
152         {
153             im_free(imrect);
154             imrect = NULL;
155         }
156 
157 /* Reverse bytes when little endian
158  * a.lacey 20.11.96
159  */
160         if (data_magic == AIFF_AMIGC)
161           {    
162             int lx = imrect->region->lx;
163             int ux = imrect->region->ux;
164             int ly = imrect->region->ly;
165             int uy = imrect->region->uy;
166             unsigned int size = var_size(imrect->vtype);
167             int cols = ux - lx;
168             int y;
169 
170             for(y = ly; y < uy; y++)
171               {
172                 void*  row = NULL;
173                 char   temp[40];
174                 int    x;
175                 unsigned int i;
176                 
177                 IM_PIX_GETP(imrect, y, lx, row);
178         
179                 for(x = 0; x < cols; x++)
180                   {
181                     for(i = 0; i < size; i++)
182                       temp[i] = *(((char *)row) + (x*size+i));
183                     for(i = 1; i <= size; i++)
184                       *(((char *)row) + (x*size + size-i)) = temp[i-1];
185                   }
186               }
187 
188             data_magic = AIFF_MAGIC;
189           }
190     }
191     return imrect;
192 }
193 
194 
195 /* Create an imrect and read an image file called 'pathname' into it.
196  * Open file, read header, padding and data, close file. On failure,
197  * give error message and return NULL. */
198 Imrect *aiff_read_image(const char *pathname)
199 {
200     FILE   *stream = fopen_2(pathname, "rb");
201     Imrect *imrect = NULL;
202 
203     if (stream)
204     {
205         imrect = aiff_read_image_stream(stream, pathname);
206         fclose_2(stream, pathname);
207     }
208     return imrect;
209 }
210 
211 /* Write aiff header to file on stream from (static global) aiffhdr.
212  * Return write_ok (true or false). Handles byte reversal problems.
213  * Leaves file descriptor at start of image data. NB DON'T free
214  * aiffhdrp (it points to global static). */
215 Bool    aiff_write_head(Imrect * imrect, FILE * stream, const char *pathname)
216 {
217     Bool    write_ok = false;
218     Imregion *imregion = imrect->region;
219     char    times[10];
220     char    dates[10];
221     static const Aiffhdr aiffhdr_default =                      /* static data! */
222     {
223         aiffhdr_id,
224         AIFF_MAGIC,             /* magic */
225         AIFF_HD_LEN,            /* length: std header length */
226         0,                      /* type */
227         0,                      /* height */
228         0,                      /* width */
229         0,                      /* lx */
230         0,                      /* ux */
231         0,                      /* ly */
232         0,                      /* uy */
233         AIFF_UNKNOWN,           /* source_id */
234         AIFF_UNKNOWN,           /* process_id */
235         "",                     /* date */
236         "",                     /* time */
237         "No title",             /* title */
238     };
239     unsigned nbytes = 0;
240 
241     time_t  tm;
242     struct tm *tp;
243 
244     (void) time(&tm);
245     tp = localtime(&tm);
246 
247     sprintf(times, "%02d:%02d:%02d", tp->tm_hour, tp->tm_min, tp->tm_sec);
248     sprintf(dates, "%02d/%02d/%02d", tp->tm_mday, tp->tm_mon + 1, tp->tm_year);
249     aiffhdr = aiffhdr_default;
250 
251     strncpy(aiffhdr.time, times, 8);
252     strncpy(aiffhdr.date, dates, 8);
253 
254     if (imrect->height<SHRT_MAX&&imrect->width<SHRT_MAX)
255     {
256        aiffhdr.type = imrect->vtype;
257        aiffhdr.height = imrect->height;
258        aiffhdr.width = imrect->width;
259        aiffhdr.lx = imregion->lx;
260        aiffhdr.ly = imregion->ly;
261        aiffhdr.ux = imregion->ux;
262        aiffhdr.uy = imregion->uy;
263 
264        nbytes = fwrite_fields(field_info, nfields, stream, pathname);
265        if (nbytes)
266        {
267            int     padding_size = aiffhdr.length - nbytes;
268 
269            if (fwrite_padding(padding_size, stream, pathname))
270            {
271                write_ok = true;
272            }
273        }
274     }
275 
276     return write_ok;
277 }
278 
279 /* Write an imrect to  an image file called 'pathname'. Open file,
280  * write header, padding and data, close file. On failure, give error
281  * message and return false. NB stream must be valid. */
282 Bool    aiff_write_image_stream(Imrect * imrect, FILE * stream, const char *pathname)
283 {
284     Bool    write_ok = false;
285     extern Bool fwrite_imrect_data(const Imrect * imrect, FILE * stream, const char *pathname);
286 
287     if (imrect)
288     {
289         if (aiff_write_head(imrect, stream, pathname))
290         {
291             write_ok = fwrite_imrect_data(imrect, stream, pathname);
292         }
293     } else
294     {
295         errorf(non_fatal, "No image to write to: %s\n", pathname);
296     }
297     return write_ok;
298 }
299 
300 /* Write an imrect to  an image file called 'pathname'. Open file,
301  * write header, padding and data, close file. On failure, give error
302  * message and return false. */
303 Bool    aiff_write_image(Imrect * imrect, const char *pathname)
304 {
305     Bool    write_ok = false;
306     FILE   *stream = fopen_2(pathname, "wb");
307 
308     if (stream)
309     {
310         write_ok = aiff_write_image_stream(imrect, stream, pathname);
311         (void) fclose_2(stream, pathname);
312     }
313     return write_ok;
314 }
315 

~ [ 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.