IPPM.c
资源名称:ilib [点击查看]
上传用户:changbiao
上传日期:2007-01-13
资源大小:141k
文件大小:4k
源码类别:

图片显示

开发平台:

C/C++

  1. /*
  2.  * IPPMC.c
  3.  *
  4.  * Image library
  5.  *
  6.  * Description:
  7.  * PPM/PGM routines.  PPM is 24-bit color.  PGM is 8-bit grayscale.
  8.  * If someone asks to write a color image to PGM, it will be written
  9.  * as PPM.
  10.  *
  11.  * History:
  12.  * 23-Jul-99 Craig Knudsen cknudsen@radix.net
  13.  * Added support for reading PGM.
  14.  * 20-May-96 Craig Knudsen cknudsen@radix.net
  15.  * Created
  16.  *
  17.  ****************************************************************************/
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <ctype.h>
  21. #include <memory.h>
  22. #include "Ilib.h"
  23. #include "IlibP.h"
  24. IError _IWritePPM ( fp, image, options )
  25. FILE *fp;
  26. IImageP *image;
  27. IOptions options;
  28. {
  29.   int r, c;
  30.   unsigned char *ptr;
  31.   unsigned int red, green, blue;
  32.   if ( options & IOPTION_ASCII ) {
  33.     fprintf ( fp, "P3n" );
  34.     if ( image->comments )
  35.       fprintf ( fp, "# %sn", image->comments );
  36.     fprintf ( fp, "%d %dn255n", image->width, image->height );
  37.     for ( r = 0; r < image->height; r++ ) {
  38.       for ( c = 0; c < image->width; c++ ) {
  39.         if ( image->greyscale ) {
  40.           ptr = image->data + ( r * image->width ) + c;
  41.           red = green = blue = (unsigned int) *ptr;
  42.         }
  43.         else {
  44.           ptr = image->data + ( r * image->width * 3 ) + ( c * 3 );
  45.           red = (unsigned int) *ptr;
  46.           green = (unsigned int) *( ptr + 1 );
  47.           blue = (unsigned int) *( ptr + 2 );
  48.         }
  49.         fprintf ( fp, "%d %d %dn", red, green, blue );
  50.       }
  51.     }
  52.   }
  53.   else {
  54.     /* we could write PPM for greyscale, but right PGM since its 1/3 size */
  55.     if ( image->greyscale ) {
  56.       fprintf ( fp, "P5n" );
  57.       if ( image->comments )
  58.         fprintf ( fp, "# %sn", image->comments );
  59.       fprintf ( fp, "%d %dn255n", image->width, image->height );
  60.       if ( fwrite ( image->data, 1, image->width * image->height, fp ) <= 0 )
  61.         return ( IErrorWriting );
  62.     } else {
  63.       fprintf ( fp, "P6n" );
  64.       if ( image->comments )
  65.         fprintf ( fp, "# %sn", image->comments );
  66.       fprintf ( fp, "%d %dn255n", image->width, image->height );
  67.       if ( fwrite ( image->data, 3, image->width * image->height, fp ) <= 0 )
  68.         return ( IErrorWriting );
  69.      }
  70.   }
  71.   return ( INoError );
  72. }
  73. IError _IReadPPM ( fp, options, image_return )
  74. FILE *fp;
  75. IOptions options;
  76. IImageP **image_return;
  77. {
  78.   char data[1024];
  79.   IImageP *image;
  80.   int w, h;
  81.   int i;
  82.   unsigned int temp;
  83.   unsigned char *ptr;
  84.   char *p;
  85.   char *comments = NULL;
  86.   int maxcolors = 255;
  87.   int greyscale = 0;
  88.   fgets ( data, 1024, fp );
  89.   if ( strncmp ( data, "P5", 2 ) == 0 ) {
  90.     greyscale = 1;
  91.     options |= IOPTION_GREYSCALE;
  92.   } else if ( strncmp ( data, "P6", 2 ) )
  93.     return ( IFileInvalid );
  94.   fgets ( data, 1024, fp );
  95.   while ( data[0] == '#' ) {
  96.     if ( comments == NULL ) {
  97.       for ( p = data + 1; *p != ' ' && *p != ''; p++) ;
  98.       if ( strlen ( p ) ) {
  99.         comments = (char *) malloc ( strlen ( p ) + 1 );
  100.         strcpy ( comments, p );
  101.       }
  102.     }
  103.     fgets ( data, 1024, fp );
  104.   }
  105.   /* should now contain width and height */
  106.   if ( sscanf ( data, "%d %d", &w, &h ) != 2 )
  107.     return ( IFileInvalid );
  108.   fgets ( data, 1024, fp );
  109.   while ( data[0] == '#' )
  110.     fgets ( data, 1024, fp );
  111.   /* should now contain maxcolor value */
  112.   if ( sscanf ( data, "%d", &maxcolors ) != 1 )
  113.     return ( IFileInvalid );
  114.   image = (IImageP *) ICreateImage ( w, h, options );
  115.   if ( comments )
  116.     image->comments = comments;
  117.   else {
  118.     image->comments = (char *) malloc ( strlen ( IDEFAULT_COMMENT ) + 1 );
  119.     strcpy ( image->comments, IDEFAULT_COMMENT );
  120.   }
  121.   if ( greyscale )
  122.     fread ( image->data, 1, w * h, fp );
  123.   else
  124.     fread ( image->data, 1, w * h * 3, fp );
  125.   /* Normalize to 255 if not already */
  126.   if ( maxcolors != 255 ) {
  127.     for ( i = 0; i < ( w * h * 3 ); i++ ) {
  128.       ptr = image->data + i;
  129.       temp = *ptr;
  130.       temp = 255 * temp / maxcolors;
  131.       *ptr = (unsigned char) temp;
  132.     }
  133.   }
  134.   
  135.   *image_return = image;
  136.   return ( INoError );
  137. }