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

Linux Cross Reference
Tina4/src/file/iff/iff_io.c

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

  1 /**@(#)Functions to read/write IFF (Image File Format) image files
  2 */
  3 
  4 #include <stdio.h>
  5 #include <string.h>
  6 #include <tina/file.h>
  7 #include <tina/sys.h>
  8 #include <tina/sys_types.h>
  9 #include <tina/sysfuncs.h>
 10 
 11 extern Bool fclose_2(FILE * stream, const char *pathname);
 12 extern Bool fread_imrect_data(const Imrect * imrect, FILE * stream, const char *pathname);
 13 extern Bool fread_padding(const unsigned size, FILE * stream, const char *pathname);
 14 extern Bool fwrite_imrect_data(const Imrect * imrect, FILE * stream, const char *pathname);
 15 extern Bool fwrite_padding(const unsigned size, FILE * stream, const char *pathname);
 16 extern FILE *fopen_2(const char *pathname, const char *mode);
 17 extern unsigned fread_fields(Field_info * field_info, const unsigned nfields, FILE * stream, const char *pathname);
 18 extern unsigned fwrite_fields(Field_info * field_info, const unsigned nfields, FILE * stream, const char *pathname);
 19 
 20 static Iffhdr iffhdr;
 21 static Field_info field_info[] =
 22 {
 23     {Field_info_id, &iffhdr.length, sizeof(iffhdr.length), 1},
 24     {Field_info_id, &iffhdr.type, sizeof(iffhdr.type), 1},
 25     {Field_info_id, &iffhdr.height, sizeof(iffhdr.height), 1},
 26     {Field_info_id, &iffhdr.width, sizeof(iffhdr.width), 1},
 27 };
 28 static unsigned const nfields = sizeof field_info / sizeof(Field_info);
 29 
 30 /* Read iff header from stream into (static global) iffhdr. Return
 31  * iffhdrp (static global)(on error NULL) . Leaves file descriptor at
 32  * start of image data. NB DON'T free iffhdrp (it points to global
 33  * static). */
 34 Iffhdr *iff_read_head(FILE * stream, const char *pathname)
 35 {
 36     unsigned nbytes = fread_fields(field_info, nfields, stream, pathname);
 37 
 38     if (nbytes)
 39     {
 40         int     padding = IFF_HD_LEN - nbytes;
 41 
 42         (void) fread_padding(padding, stream, pathname);
 43     }
 44     return &iffhdr;
 45 }
 46 
 47 /* Create an imrect and read an image file called 'pathname' into it.
 48  * Open file, read header, padding and data, close file. On failure,
 49  * give error message and return NULL. */
 50 Imrect *iff_read_image(const char *pathname)
 51 {
 52     FILE   *stream = fopen_2(pathname, "rb");
 53     Imrect *imrect = NULL;
 54 
 55     if (stream)
 56     {
 57         Iffhdr *iffhdr = iff_read_head(stream, pathname);
 58 
 59         if (iffhdr)
 60         {
 61             Imregion *imregion = NULL;
 62 
 63             imrect = im_alloc(iffhdr->height, iffhdr->width, imregion,
 64                               (Vartype) iffhdr->type);
 65             if (!fread_imrect_data(imrect, stream, pathname))
 66             {
 67                 im_free(imrect);
 68                 imrect = NULL;
 69             }
 70         }
 71         fclose_2(stream, pathname);
 72     }
 73     return imrect;
 74 }
 75 
 76 /* Write iff header to file on stream from (static global) iffhdr.
 77  * Return write_ok (true or false). Handles byte reversal problems.
 78  * Leaves file descriptor at start of image data. NB DON'T free iffhdrp
 79  * (it points to global static). */
 80 Bool    iff_write_head(Imrect * imrect, FILE * stream, const char *pathname)
 81 {
 82     Bool    write_ok = false;
 83     static const Iffhdr iffhdr_default =
 84     {
 85         iffhdr_id,
 86         IFF_HD_LEN,             /* length: std header length */
 87         0,                      /* type */
 88         0,                      /* height */
 89         0,                      /* width */
 90     };
 91     unsigned nbytes = 0;
 92 
 93     iffhdr = iffhdr_default;
 94     iffhdr.type = imrect->vtype;
 95     iffhdr.height = imrect->height;
 96     iffhdr.width = imrect->width;
 97 
 98     nbytes = fwrite_fields(field_info, nfields, stream, pathname);
 99     if (nbytes)
100     {
101         int     padding_size = iffhdr.length - nbytes;
102 
103         if (fwrite_padding(padding_size, stream, pathname))
104         {
105             write_ok = true;
106         }
107     }
108     return write_ok;
109 }
110 
111 /* Write an imrect to  an image file called 'pathname'. Open file,
112  * write header, padding and data, close file. On failure, give error
113  * message and return false. */
114 Bool    iff_write_image(Imrect * imrect, const char *pathname)
115 {
116     Bool    write_ok = false;
117 
118     if (imrect)
119     {
120         FILE   *stream = fopen_2(pathname, "wb");
121 
122         if (stream)
123         {
124             if (iff_write_head(imrect, stream, pathname))
125             {
126                 write_ok = fwrite_imrect_data(imrect, stream, pathname);
127             }
128             (void) fclose_2(stream, pathname);
129         }
130     } else
131     {
132         errorf(non_fatal, "No image to write to: %s\n", pathname);
133     }
134     return write_ok;
135 }
136 

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