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

Linux Cross Reference
Tina5/tina-libs/tina/file/fileIff_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/fileIff_io.c,v $
 37  * Date    :  $Date: 2005/01/23 19:10:21 $
 38  * Version :  $Revision: 1.4 $
 39  * CVS Id  :  $Id: fileIff_io.c,v 1.4 2005/01/23 19:10:21 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes   :
 44  * 
 45  *  Functions to read/write IFF (Image File Format) image files
 46  *********
 47 */
 48 
 49 #include "fileIff_io.h"
 50 
 51 #if HAVE_CONFIG_H
 52   #include <config.h>
 53 #endif
 54 
 55 #include <stdio.h>
 56 #include <string.h>
 57 #include <tina/sys/sysPro.h>
 58 #include <tina/sys/sysDef.h>
 59 #include <tina/image/imgPro.h>
 60 #include <tina/image/imgDef.h>
 61 #include <tina/file/file_IffDef.h>
 62 #include <tina/file/file_AiffDef.h>
 63 #include <tina/file/file_UtilPro.h>
 64 
 65 static Iffhdr iffhdr;
 66 static Field_info field_info[] =
 67 {
 68     {Field_info_id, &iffhdr.length, sizeof(iffhdr.length), 1},
 69     {Field_info_id, &iffhdr.type, sizeof(iffhdr.type), 1},
 70     {Field_info_id, &iffhdr.height, sizeof(iffhdr.height), 1},
 71     {Field_info_id, &iffhdr.width, sizeof(iffhdr.width), 1},
 72 };
 73 static unsigned const nfields = sizeof field_info / sizeof(Field_info);         /* static data! */
 74 
 75 /* Read iff header from stream into (static global) iffhdr. Return
 76  * iffhdrp (static global)(on error NULL) . Leaves file descriptor at
 77  * start of image data. NB DON'T free iffhdrp (it points to global
 78  * static). */
 79 Iffhdr *iff_read_head(FILE * stream, const char *pathname)
 80 {
 81     unsigned nbytes = fread_fields(field_info, nfields, stream, pathname);
 82 
 83     if (nbytes)
 84     {
 85         int     padding = IFF_HD_LEN - nbytes;
 86 
 87         (void) fread_padding(padding, stream, pathname);
 88     }
 89     return &iffhdr;
 90 }
 91 
 92 /* Create an imrect and read an image file called 'pathname' into it.
 93  * Open file, read header, padding and data, close file. On failure,
 94  * give error message and return NULL. */
 95 Imrect *iff_read_image(const char *pathname)
 96 {
 97     FILE   *stream = fopen_2(pathname, "rb");
 98     Imrect *imrect = NULL;
 99 
100     if (stream)
101     {
102         Iffhdr *iffhdr = iff_read_head(stream, pathname);
103 
104         if (iffhdr)
105         {
106             Imregion *imregion = NULL;
107 
108             imrect = im_alloc(iffhdr->height, iffhdr->width, imregion,
109                               (Vartype) iffhdr->type);
110             if (!fread_imrect_data(imrect, stream, pathname))
111             {
112                 im_free(imrect);
113                 imrect = NULL;
114             }
115         }
116         fclose_2(stream, pathname);
117     }
118     return imrect;
119 }
120 
121 /* Write iff header to file on stream from (static global) iffhdr.
122  * Return write_ok (true or false). Handles byte reversal problems.
123  * Leaves file descriptor at start of image data. NB DON'T free iffhdrp
124  * (it points to global static). */
125 Bool    iff_write_head(Imrect * imrect, FILE * stream, const char *pathname)
126 {
127     Bool    write_ok = false;
128     static const Iffhdr iffhdr_default =                /* static data! */
129     {
130         iffhdr_id,
131         IFF_HD_LEN,             /* length: std header length */
132         0,                      /* type */
133         0,                      /* height */
134         0,                      /* width */
135     };
136     unsigned nbytes = 0;
137 
138     iffhdr = iffhdr_default;
139     iffhdr.type = imrect->vtype;
140     iffhdr.height = imrect->height;
141     iffhdr.width = imrect->width;
142 
143     nbytes = fwrite_fields(field_info, nfields, stream, pathname);
144     if (nbytes)
145     {
146         int     padding_size = iffhdr.length - nbytes;
147 
148         if (fwrite_padding(padding_size, stream, pathname))
149         {
150             write_ok = true;
151         }
152     }
153     return write_ok;
154 }
155 
156 /* Write an imrect to  an image file called 'pathname'. Open file,
157  * write header, padding and data, close file. On failure, give error
158  * message and return false. */
159 Bool    iff_write_image(Imrect * imrect, const char *pathname)
160 {
161     Bool    write_ok = false;
162 
163     if (imrect)
164     {
165         FILE   *stream = fopen_2(pathname, "wb");
166 
167         if (stream)
168         {
169             if (iff_write_head(imrect, stream, pathname))
170             {
171                 write_ok = fwrite_imrect_data(imrect, stream, pathname);
172             }
173             (void) fclose_2(stream, pathname);
174         }
175     } else
176     {
177         errorf(non_fatal, "No image to write to: %s\n", pathname);
178     }
179     return write_ok;
180 }
181 

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