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

Linux Cross Reference
Tina4/src/file/pgm/pgm_file.c

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

  1 /*
  2                 pgm_file.c
  3 
  4                 reading and writing of PGM format files
  5 
  6                 a.lacey
  7                 28.1.96; 22.11.96
  8 */
  9 
 10 
 11 #include <stdio.h>
 12 #include <tina/sys.h>
 13 #include <tina/file.h>
 14 #include <tina/sysfuncs.h>
 15 
 16 
 17 
 18 int                              pgm_read_header(FILE *fp, int *height, int *width)
 19 {
 20         char     pgm_head_char; 
 21         int      pgm_head_val, grey_level;
 22         char     buffer[256];
 23 
 24 
 25         fgets(buffer, 256, fp);
 26         sscanf(buffer, "%c%d", &pgm_head_char, &pgm_head_val);
 27 
 28         if ((pgm_head_char != 'P') || (pgm_head_val != 5))
 29         {
 30           printf("pgm_read_header: unexpected header info\n");
 31         }
 32 
 33         fgets(buffer,256,fp);
 34         while (buffer[0]=='#')
 35           fgets(buffer,256,fp);
 36                 
 37         sscanf(buffer,"%d %d",width, height);
 38 
 39         fgets(buffer,256,fp);
 40         while (buffer[0]=='#')
 41           fgets(buffer,256,fp);
 42                 
 43         sscanf(buffer, "%d", &grey_level);
 44 /*
 45         format("pgm header: width: %d, height : %d, grey-levels : %d\n", 
 46                                  *width, *height, grey_level);
 47 */
 48         return(0);
 49 }
 50 
 51 
 52 int                              pgm_write_header(FILE *fp, int height, int width)
 53 {
 54 
 55         fprintf(fp, "P5\n%d %d\n255\n", width, height);
 56 
 57         return(0);
 58 }
 59 
 60 
 61 
 62 int                              ucread_raw_data(FILE *fp, Imrect *im)
 63 {
 64         int                              i, j;
 65         unsigned char    pix;
 66 
 67         for(i = im->region->ly; i < im->region->uy; i++)
 68                 for(j = im->region->lx; j < im->region->ux; j++)
 69                 {
 70                         if (fscanf(fp, "%c", &pix) == 0)
 71                                 return(-1);
 72 
 73                         IM_PIX_SET(im, i, j, pix);
 74                 }
 75 
 76         return(0);
 77                 
 78 }
 79 
 80 
 81 int                              ucwrite_raw_data(FILE *fp, Imrect *im)
 82 {
 83         int                     i, j;
 84         double     pix;
 85 
 86         for(i = im->region->ly; i < im->region->uy; i++)
 87                 for(j = im->region->lx; j < im->region->ux; j++)
 88                 {
 89                         IM_PIX_GET(im, i, j, pix);
 90                         fputc((int)pix, fp);
 91                 }
 92 
 93         return(0);
 94                 
 95 }
 96 
 97 
 98 
 99 Imrect          *pgm_read_image(char *fname)
100 {
101         Imrect  *im = NULL;
102         FILE    *fp = NULL;
103         int      width, height;
104 
105         if (fname == NULL)
106         {
107                 error("pgm_read_image : invalid filename", warning);
108                 return(NULL);
109         }
110 
111         if ((fp = fopen(fname, "r")) == NULL)
112         {
113                 error("pgm_read_image : cannot open file", warning);
114                 return(NULL);
115         }
116 
117         if (pgm_read_header(fp, &height, &width))
118         {
119                 error("pgm_read_image : header error", warning);
120                 fclose(fp);
121                 return(NULL);
122         }
123 
124         if ((im = im_alloc(height, width, NULL, uchar_v)) == NULL)
125         {
126                 error("pgm_read_image : cannot allocate image", warning);
127                 fclose(fp);
128                 return(NULL);
129         }
130 
131         if (ucread_raw_data(fp, im))
132         {
133                 error("pgm_read_image : data error", warning);
134                 fclose(fp);
135                 return(NULL);
136         }
137 
138         fclose(fp);
139         return(im);
140 }
141 
142 
143 
144 int     pgm_write_image(Imrect *im, char *fname)
145 {
146         FILE    *fp = NULL;
147         int      width, height;
148 
149         if (fname == NULL)
150         {
151                 error("pgm_write_image : invalid filename", warning);
152                 return(-1);
153         }
154 
155         if (im == NULL)
156         {
157                 error("pgm_write_image : invalid image", warning);
158                 return(-2);
159         }
160 
161         if ((fp = fopen(fname, "w")) == NULL)
162         {
163                 error("pgm_write_image : cannot open file", warning);
164                 return(-3);
165         }
166 
167         width = im->region->ux - im->region->lx;
168         height = im->region->uy - im->region->ly;
169         pgm_write_header(fp, height, width);
170 
171   if (im->vtype != uchar_v && im->vtype != char_v)
172                 {
173                         Imrect *chim, *fpim;
174 
175                         fpim = im_cast(im, float_v);
176                         imf_scale_inplace(fpim, 0.0, 255.0);
177                         chim = im_cast(fpim, uchar_v);
178                         if (ucwrite_raw_data(fp, chim))
179                                 {
180                                         error("pgm_write_image : data error", warning);
181                                         fclose(fp);
182                                         return(-4);
183                                 }
184                         im_free(chim); im_free(fpim);
185                 }
186 
187         else
188                 {
189                         if (ucwrite_raw_data(fp, im))
190                                 {
191                                         error("pgm_write_image : data error", warning);
192                                         fclose(fp);
193                                         return(-4);
194                                 }
195                 }
196 
197         fclose(fp);
198         return(0);
199 }
200 
201 

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