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

Linux Cross Reference
Tina6/tina-libs/tina/file/filePnm_file.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    :  
 37  * Date    :  
 38  * Version :  
 39  * CVS Id  :  
 40  *
 41  * Author  :  paul.bromiley@manchester.ac.uk
 42  *
 43  * Notes   :
 44  *
 45  *      Reading of PNM format files (PPM, PGM and PBM) in both raw and 
 46  *      ASCII formats, and writing in raw format.
 47  *
 48  *      The magic numbers for PPM files are
 49  *
 50  *      P1      a (text-format) PBM file: 1bpp
 51  *      P2      a text-format PGM file
 52  *      P3      a text-format PPM file
 53  *      P4.     a raw-format PBM file: 1bpp
 54  *      P5      a raw-format PGM file 
 55  *      P6      a raw-format PPM file 
 56  *
 57  *      Since we don't have a 'true' binary image type in TINA, PBM files
 58  *      can be read with the same functions as PGM files: note that the header
 59  *      structure is slightly different (no depth field).
 60  *
 61  *      Return type for ppm and pnm reading functions is a pvector of pointers to
 62  *      three images (red, green and blue fields in that order).
 63  *      
 64  *********
 65 */
 66 
 67 #include "filePnm_file.h"
 68 
 69 #if HAVE_CONFIG_H
 70   #include <config.h>
 71 #endif
 72 
 73 #include <stdio.h>
 74 #include <tina/sys/sysPro.h>
 75 #include <tina/sys/sysDef.h>
 76 #include <tina/image/imgPro.h>
 77 #include <tina/image/imgDef.h>
 78 #include <tina/geometry/geomPro.h>
 79 
 80 
 81 /* Reading functions */
 82 
 83 
 84 static int pnm_get_char(FILE *fp)
 85 {
 86         /* Read the next character, skipping any comments */
 87 
 88         int ch;
 89 
 90         ch = getc(fp);
 91   
 92         if (ch == '#') 
 93         {
 94                 do
 95                 { 
 96                         ch = getc(fp);
 97                 } 
 98                 while (ch != '\n' && ch != EOF);
 99         }
100         
101         return ch;
102 }
103 
104 
105 static int pnm_get_integer(FILE *fp)
106 {
107         /* Read a single integer from the ppm header, stripping any whitspace, and 
108         ignoring any comments */
109 
110         int ch;
111         int val;
112 
113         /* Skip any leading whitespace */
114         do 
115         {
116                 ch = pnm_get_char(fp);
117                 if (ch == EOF)
118                 {
119                         return -1;
120                 }
121         } 
122         while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
123 
124         if (ch < '' || ch > '9')
125         {
126                 return -1;
127         }
128 
129         val = ch - '';
130         while ((ch = pnm_get_char(fp)) >= '' && ch <= '9') {
131         val *= 10;
132         val += ch - '';
133         }
134         return val;
135 }
136 
137 
138 static int pnm_read_grey_text(Imrect *im, FILE *fp)
139 {
140         int i, j, value;
141         
142         for(i = im->region->ly; i < im->region->uy; i++)
143         {
144                 for(j = im->region->lx; j < im->region->ux; j++)
145                 {
146                         value = pnm_get_integer(fp);
147                         if(value==-1)
148                         {
149                                 return(1);
150                         }
151                         im_put_pix((float)value, im, i, j);
152                 }
153         }
154         return(0);
155 }
156 
157 
158 static int pnm_read_colour_text(Imrect *im_r, Imrect *im_g, Imrect *im_b, FILE *fp)
159 {
160         int i, j, value_r, value_g, value_b;
161         
162         for(i = im_r->region->ly; i < im_r->region->uy; i++)
163         {
164                 for(j = im_r->region->lx; j < im_r->region->ux; j++)
165                 {
166                         value_r = pnm_get_integer(fp);
167                         value_g = pnm_get_integer(fp);
168                         value_b = pnm_get_integer(fp);
169                         if((value_r==-1)||(value_g==-1)||(value_b==-1))
170                         {
171                                 return(1);
172                         }
173                         im_put_pix((float)value_r, im_r, i, j);
174                         im_put_pix((float)value_g, im_g, i, j);
175                         im_put_pix((float)value_b, im_b, i, j);
176                 }
177         }
178         return(0);
179 }
180 
181 static int pnm_read_grey_raw(Imrect *im, FILE *fp)
182 {
183         int i, j;
184         unsigned char value;
185         
186         for(i = im->region->ly; i < im->region->uy; i++)
187         {
188                 for(j = im->region->lx; j < im->region->ux; j++)
189                 {
190                         if((fscanf(fp, "%c", &value)==0))
191                         {
192                                 return(1);
193                         }
194                         else
195                         {
196                                 im_put_pix((float)value, im, i, j);
197                         }
198                 }
199         }
200         return(0);
201 }
202 
203 static int pnm_read_colour_raw(Imrect *im_r, Imrect *im_g, Imrect *im_b, FILE *fp)
204 {
205         int i, j;
206         unsigned char value_r, value_g, value_b;
207         
208         for(i = im_r->region->ly; i < im_r->region->uy; i++)
209         {
210                 for(j = im_r->region->lx; j < im_r->region->ux; j++)
211                 {
212                         if((fscanf(fp, "%c", &value_r)==0)||(fscanf(fp, "%c", &value_g)==0)
213                                 ||(fscanf(fp, "%c", &value_b)==0))
214                         {
215                                 return(1);
216                         }
217                         else
218                         {
219                                 im_put_pix((float)value_r, im_r, i, j);
220                                 im_put_pix((float)value_g, im_g, i, j);
221                                 im_put_pix((float)value_b, im_b, i, j);
222                         }
223                 }
224         }
225         return(0);
226 }
227 
228 
229 static FILE *open_test(char *fname)
230 {
231         FILE *fp=NULL;
232 
233         if (fname == NULL)
234         {
235                 error("pnm_read_image: invalid filename\n", warning);
236                 return(NULL);
237         }
238 
239         if ((fp = fopen(fname, "r")) == NULL)
240         {
241                 error("pnm_read_image: cannot open file\n", warning);
242                 return(NULL);
243         }
244         
245         return(fp);
246 }
247 
248 
249 Imrect *pbm_read_image(char *fname)
250 {
251         int c, width, height, read_flag=0;
252         FILE *fp=NULL;
253         Imrect *im=NULL;
254 
255         fp=open_test(fname);
256         if(fp==NULL) return(NULL);
257 
258         /* Test the magic number */
259         
260         if (getc(fp) != 'P')
261         {
262                 error("pbm_read_image: unrecognised format\n", warning);
263                 return(NULL);
264         }
265         c = getc(fp); 
266         if((c!='1')&&(c!='4'))
267         {
268                 error("pbm_read_image: unrecognised format\n", warning);
269                 return(NULL);
270         }
271 
272         width = pnm_get_integer(fp);
273         height = pnm_get_integer(fp);
274 
275         if (width <= 0 || height <= 0)
276         {
277                 error("Corrupted PBM header\n", warning);
278         }
279         
280         switch (c) 
281         {
282                 case '1':
283                         im=im_alloc(height, width, NULL, uchar_v);
284                         read_flag = pnm_read_grey_text(im, fp);
285                         break;
286                 case '4':
287                         im=im_alloc(height, width, NULL, uchar_v);
288                         read_flag = pnm_read_grey_raw(im, fp);
289                         break;
290                 default:
291                         error("Format unrecognised\n", warning);
292                         break;
293         }
294         if(read_flag==1)
295         {
296                 error("PBM file corrupted\n", warning);
297                 if(im!=NULL)
298                 {
299                         im_free(im);
300                         return(NULL);
301                 }
302         }
303         return(im);
304 }
305 
306 
307 Imrect *pgm_read_image(char *fname)
308 {
309         int c, width, height, maxval, read_flag=0;
310         FILE *fp=NULL;
311         Imrect *im=NULL;
312 
313         fp=open_test(fname);
314         if(fp==NULL) return(NULL);
315 
316         /* Test the magic number */
317         
318         if (getc(fp) != 'P')
319         {
320                 error("pgm_read_image: unrecognised format\n", warning);
321                 return(NULL);
322         }
323         c = getc(fp); 
324         if((c!='2')&&(c!='5'))
325         {
326                 error("pgm_read_image: unrecognised format\n", warning);
327                 return(NULL);
328         }
329 
330         width = pnm_get_integer(fp);
331         height = pnm_get_integer(fp);
332         maxval = pnm_get_integer(fp);
333 
334         if (width <= 0 || height <= 0 || maxval <= 0)
335         {
336                 error("Corrupted PGM header\n", warning);
337         }
338         
339         switch (c) 
340         {
341                 case '2':
342                         im = im_alloc(height, width, NULL, float_v);
343                         read_flag = pnm_read_grey_text(im, fp);
344                         break;
345                 case '5':
346                         im = im_alloc(height, width, NULL, uchar_v);
347                         read_flag = pnm_read_grey_raw(im, fp);
348                         break;
349                 default:
350                         error("Format unrecognised\n", warning);
351                         break;
352         }
353         if(read_flag==1)
354         {
355                 error("PGM file corrupted\n", warning);
356                 if(im!=NULL)
357                 im_free(im);
358                 return(NULL);
359         }
360         fclose(fp);
361         return(im);
362 }
363 
364 
365 void **ppm_read_image(char *fname)
366 {
367         int c, width, height, maxval, read_flag=0;
368         FILE *fp=NULL;
369         Imrect *im_r=NULL, *im_g=NULL, *im_b=NULL;
370         void **image_vector=NULL;
371 
372         fp=open_test(fname);
373         if(fp==NULL) return(NULL);
374 
375         /* Test the magic number */
376         
377         if (getc(fp) != 'P')
378         {
379                 error("ppm_read_image: unrecognised format\n", warning);
380                 return(NULL);
381         }
382         c = getc(fp); 
383         if((c!='3')&&(c!='6'))
384         {
385                 error("ppm_read_image: unrecognised format\n", warning);
386                 return(NULL);
387         }
388 
389         width = pnm_get_integer(fp);
390         height = pnm_get_integer(fp);
391         maxval = pnm_get_integer(fp);
392 
393         if (width <= 0 || height <= 0 || maxval <= 0)
394         {
395                 error("Corrupted PPM header\n", warning);
396         }
397         
398         switch (c) 
399         {
400                 case '3':
401                         im_r = im_alloc(height, width, NULL, float_v);
402                         im_g = im_alloc(height, width, NULL, float_v);
403                         im_b = im_alloc(height, width, NULL, float_v);
404                         read_flag = pnm_read_colour_text(im_r, im_g, im_b, fp);
405                         break;
406                 case '6':
407                         im_r = im_alloc(height, width, NULL, uchar_v);
408                         im_g = im_alloc(height, width, NULL, uchar_v);
409                         im_b = im_alloc(height, width, NULL, uchar_v);
410                         read_flag = pnm_read_colour_raw(im_r, im_g, im_b, fp);
411                         break;
412                 default:
413                         error("Format unrecognised\n", warning);
414                         break;
415         }
416         if(read_flag==1)
417         {
418                 error("PPM file corrupted\n", warning);
419                 im_free(im_r);
420                 im_free(im_g);
421                 im_free(im_b);
422                 return(NULL);
423         }
424 
425         fclose(fp);
426         
427         image_vector = pvector_alloc(0, 3);
428         image_vector[0] = im_r;
429         image_vector[1] = im_g;
430         image_vector[2] = im_b;
431         return(image_vector);
432 }
433 
434 
435 void **pnm_read_image(char *fname)
436 {
437         int c, width, height, maxval=255, read_flag=0;
438         FILE *fp=NULL;
439         Imrect *im=NULL, *im_r=NULL, *im_g=NULL, *im_b=NULL;
440         void **image_vector=NULL;
441 
442         fp=open_test(fname);
443         if(fp==NULL) return(NULL);
444 
445         /* Test the magic number */
446         
447         if (getc(fp) != 'P')
448         {
449                 error("pnm_read_image: unrecognised format\n", warning);
450                 return(NULL);
451         }
452         c = getc(fp); 
453         if((c!='1')&&(c!='2')&&(c!='3')&&(c!='4')&&(c!='5')&&(c!='6'))
454         {
455                 error("pnm_read_image: unrecognised format\n", warning);
456                 return(NULL);
457         }
458 
459         width = pnm_get_integer(fp);
460         height = pnm_get_integer(fp);
461         
462         if((c!='1')&&(c!='4')) maxval = pnm_get_integer(fp);
463 
464         if (width <= 0 || height <= 0 || maxval <= 0)
465         {
466                 error("Corrupted PNM header\n", warning);
467         }
468         
469         switch (c) 
470         {
471                 case '1':
472                         im=im_alloc(height, width, NULL, uchar_v);
473                         read_flag = pnm_read_grey_text(im, fp);
474                         break;
475         
476                 case '2':
477                         im = im_alloc(height, width, NULL, float_v);
478                         read_flag = pnm_read_grey_text(im, fp);
479                         break;
480 
481                 case '3':
482                         im_r = im_alloc(height, width, NULL, float_v);
483                         im_g = im_alloc(height, width, NULL, float_v);
484                         im_b = im_alloc(height, width, NULL, float_v);
485                         read_flag = pnm_read_colour_text(im_r, im_g, im_b, fp);
486                         break;
487                         
488                 case '4':
489                         im=im_alloc(height, width, NULL, uchar_v);
490                         read_flag = pnm_read_grey_raw(im, fp);
491                         break;
492                         
493                 case '5':
494                         im = im_alloc(height, width, NULL, uchar_v);
495                         read_flag = pnm_read_grey_raw(im, fp);
496                         break;
497 
498                 case '6':
499                         im_r = im_alloc(height, width, NULL, uchar_v);
500                         im_g = im_alloc(height, width, NULL, uchar_v);
501                         im_b = im_alloc(height, width, NULL, uchar_v);
502                         read_flag = pnm_read_colour_raw(im_r, im_g, im_b, fp);
503                         break;
504                 default:
505                         error("Format unrecognised\n", warning);
506                         break;
507         }
508         if(read_flag==1)
509         {
510                 error("PNM file corrupted\n", warning);
511                 if(im!=NULL)
512                 {
513                         im_free(im);
514                 }
515                 else
516                 {
517                         im_free(im_r);
518                         im_free(im_g);
519                         im_free(im_b);
520                 }
521                 return(NULL);
522         }
523 
524         fclose(fp);
525         
526         image_vector = pvector_alloc(0, 3);
527         if(im!=NULL)
528         {
529                 image_vector[0] = im;
530                 image_vector[1] = NULL;
531                 image_vector[2] = NULL;
532         }
533         else
534         {
535                 image_vector[0] = im_r;
536                 image_vector[1] = im_g;
537                 image_vector[2] = im_b;
538         }
539         
540         return(image_vector);
541 }
542 
543 
544 /* Writing functions */
545 
546 
547 static int pbm_write_header(FILE *fp, int height, int width)
548 {
549         fprintf(fp, "P4\n");
550         fprintf(fp, "#Generated by TINA (www.tina-vision.net)\n");
551         fprintf(fp, "%d %d\n", width, height);
552         
553         return(0);
554 }
555 
556 
557 static int pgm_write_header(FILE *fp, int height, int width)
558 {
559         fprintf(fp, "P5\n");
560         fprintf(fp, "#Generated by TINA (www.tina-vision.net)\n");
561         fprintf(fp, "%d %d\n", width, height);
562         fprintf(fp, "255\n");
563         
564         return(0);
565 }
566 
567 
568 static int ppm_write_header(FILE *fp, int height, int width)
569 {
570         fprintf(fp, "P6\n");
571         fprintf(fp, "# Generated by TINA (www.tina-vision.net)\n");
572         fprintf(fp, "%d %d\n", width, height);
573         fprintf(fp, "255\n");
574         
575         return(0);
576 }
577 
578 
579 static int pgmwrite_raw_data(FILE *fp, Imrect *im)
580 {
581         int i, j;
582         double pix;
583 
584         for(i = im->region->ly; i < im->region->uy; i++)
585         {
586                 for(j = im->region->lx; j < im->region->ux; j++)
587                 {
588                         IM_PIX_GET(im, i, j, pix);
589                         fputc((int)pix, fp);
590                 }
591         }
592 
593         return(0);
594 }
595 
596 
597 static int ppmwrite_raw_data(FILE *fp, Imrect *im_r, Imrect *im_g, Imrect *im_b)
598 {
599         int i, j;
600         double pix_r, pix_g, pix_b;
601 
602         for(i = im_r->region->ly; i < im_r->region->uy; i++)
603         {
604                 for(j = im_r->region->lx; j < im_r->region->ux; j++)
605                 {
606                         IM_PIX_GET(im_r, i, j, pix_r);
607                         IM_PIX_GET(im_g, i, j, pix_g);
608                         IM_PIX_GET(im_b, i, j, pix_b);
609                         fputc((int)pix_r, fp);
610                         fputc((int)pix_g, fp);
611                         fputc((int)pix_b, fp);
612                 }
613         }
614 
615         return(0);
616 }
617 
618 
619 int pbm_write_image(Imrect *im, char *fname)
620 {
621         FILE    *fp = NULL;
622         int      width, height;
623         Imrect *chim=NULL, *fpim=NULL;
624 
625         if (fname == NULL)
626         {
627                 error("pbm_write_image : invalid filename", warning);
628                 return(-1);
629         }
630 
631         if (im == NULL)
632         {
633                 error("pbm_write_image : invalid image", warning);
634                 return(-2);
635         }
636 
637         if ((fp = fopen(fname, "w")) == NULL)
638         {
639                 error("pbm_write_image : cannot open file", warning);
640                 return(-3);
641         }
642 
643         width = im->region->ux - im->region->lx;
644         height = im->region->uy - im->region->ly;
645         pbm_write_header(fp, height, width);
646         
647         fpim = im_bthresh(0.5, im);
648         chim = im_cast(fpim, uchar_v);
649         if (pgmwrite_raw_data(fp, chim))
650         {
651                 error("pbm_write_image : data error", warning);
652                 fclose(fp);
653                 return(-4);
654         }
655         im_free(chim); 
656         im_free(fpim);
657 
658         fclose(fp);
659         return(0);
660 }
661 
662 
663 int pgm_write_image(Imrect *im, char *fname)
664 {
665         FILE    *fp = NULL;
666         int      width, height;
667         Imrect *chim=NULL, *fpim=NULL;
668 
669         if (fname == NULL)
670         {
671                 error("pgm_write_image : invalid filename", warning);
672                 return(-1);
673         }
674 
675         if (im == NULL)
676         {
677                 error("pgm_write_image : invalid image", warning);
678                 return(-2);
679         }
680 
681         if ((fp = fopen(fname, "w")) == NULL)
682         {
683                 error("pgm_write_image : cannot open file", warning);
684                 return(-3);
685         }
686 
687         width = im->region->ux - im->region->lx;
688         height = im->region->uy - im->region->ly;
689         pgm_write_header(fp, height, width);
690         
691         if (im->vtype != uchar_v && im->vtype != char_v)
692         {
693                 fpim = im_cast(im, float_v);
694                 imf_scale_inplace(fpim, 0.0, 255.0);
695                 chim = im_cast(fpim, uchar_v);
696                 if (pgmwrite_raw_data(fp, chim))
697                 {
698                         error("pgm_write_image : data error", warning);
699                         fclose(fp);
700                         return(-4);
701                 }
702                 im_free(chim); im_free(fpim);
703         }
704         else
705         {
706                 if (pgmwrite_raw_data(fp, im))
707                 {
708                         error("pgm_write_image : data error", warning);
709                         fclose(fp);
710                         return(-4);
711                 }
712         }
713 
714         fclose(fp);
715         return(0);
716 }
717 
718 
719 static void ppm_scaler(Imrect *im_red, Imrect *im_green, Imrect *im_blue)
720 {
721         int j, k;
722         Imregion *roi=NULL;
723         float scale=1.0;
724         float pix_r, pix_g, pix_b;
725         float min_r, max_r, min_g, max_g, min_b, max_b;
726         
727         roi = im_red->region;
728         imf_minmax(im_red, &min_r, &max_r);
729         imf_minmax(im_green, &min_g, &max_g);
730         imf_minmax(im_blue, &min_b, &max_b);
731         
732         scale = ((float)fmax3(((double)(max_r)), ((double)(max_g)), ((double)(max_b))));
733         
734         for(j = roi->ly; j<roi->uy; j++)
735         {
736                 for(k=roi->lx; k<roi->ux; k++)
737                 {
738                         pix_r = im_get_pixf(im_red, j, k);      
739                         pix_g = im_get_pixf(im_green, j, k);
740                         pix_b = im_get_pixf(im_blue, j, k);
741                 
742                         im_put_pixf((255.0*pix_r/scale), im_red, j, k);
743                         im_put_pixf((255.0*pix_g/scale), im_green, j, k);
744                         im_put_pixf((255.0*pix_b/scale), im_blue, j, k);
745                 }
746         }
747 }
748 
749 
750 int ppm_write_image(void **image_vector, char *fname)
751 {
752         FILE *fp = NULL;
753         int width, height, roitest;
754         Imrect *im_red=NULL, *im_green=NULL, *im_blue=NULL;
755         Imrect *chim_red=NULL, *chim_green=NULL, *chim_blue=NULL;
756         Imrect *fpim_red=NULL, *fpim_green=NULL, *fpim_blue=NULL;
757         Imregion *roi_red=NULL, *roi_green=NULL, *roi_blue=NULL;
758 
759         if (fname == NULL)
760         {
761                 error("ppm_write_image : invalid filename", warning);
762                 return(-1);
763         }
764 
765         im_red = (Imrect *)image_vector[0];
766         im_green = (Imrect *)image_vector[1];
767         im_blue = (Imrect *)image_vector[2];
768         
769         if ((im_red==NULL)||(im_green==NULL)||(im_blue==NULL))
770         {
771                 error("ppm_write_image : invalid image", warning);
772                 return(-2);
773         }
774 
775         if ((fp = fopen(fname, "w")) == NULL)
776         {
777                 error("ppm_write_image : cannot open file", warning);
778                 return(-3);
779         }
780 
781         roi_red = im_red->region;
782         roi_green = im_green->region;
783         roi_blue = im_blue->region;
784         
785         roitest = (roi_red->lx-roi_green->lx)+(roi_red->lx-roi_blue->lx)
786                 +(roi_red->ly-roi_green->ly)+(roi_red->ly-roi_blue->ly)
787                 +(roi_red->ux-roi_green->ux)+(roi_red->ux-roi_blue->ux)
788                 +(roi_red->uy-roi_green->uy)+(roi_red->uy-roi_blue->uy);
789         if(roitest!=0)
790         {
791                 error("ppm_write_images: colour fields do not have same roi\n", warning);
792                 return(-5);
793         }
794         
795         width = im_red->region->ux - im_red->region->lx;
796         height = im_red->region->uy - im_red->region->ly;
797         ppm_write_header(fp, height, width);
798 
799         if (im_red->vtype != uchar_v && im_red->vtype != char_v)
800         {
801                 fpim_red = im_cast(im_red, float_v);
802                 fpim_green = im_cast(im_green, float_v);
803                 fpim_blue = im_cast(im_blue, float_v);
804                 ppm_scaler(fpim_red, fpim_green, fpim_blue);
805                 chim_red = im_cast(fpim_red, uchar_v);
806                 chim_green = im_cast(fpim_green, uchar_v);
807                 chim_blue = im_cast(fpim_blue, uchar_v);
808                 if (ppmwrite_raw_data(fp, chim_red, chim_green, chim_blue))
809                 {
810                         error("pgm_write_image : data error", warning);
811                         fclose(fp);
812                         return(-4);
813                 }
814                 im_free(chim_red); 
815                 im_free(chim_green); 
816                 im_free(chim_blue); 
817                 im_free(fpim_red);
818                 im_free(fpim_green);
819                 im_free(fpim_blue);
820         }
821         else
822         {
823                 if (ppmwrite_raw_data(fp, im_red, im_green, im_blue))
824                 {
825                         error("pgm_write_image : data error", warning);
826                         fclose(fp);
827                         return(-4);
828                 }
829         }
830 
831         fclose(fp);
832         return(0);
833 }
834 

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