home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / BarlaA / sw / OLD / Simo / SVM_mono / pnmio.c < prev    next >
C/C++ Source or Header  |  2002-06-25  |  4KB  |  246 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "image2d.h"
  4.  
  5.  
  6. /*char    *getnext(FILE * p);*/
  7. /*int number(const char *s);*/
  8.  
  9. /* pnm header reader
  10.  * based upon John Burns ppm reader
  11.  *
  12.  * give newly oped file pointer returns relavent information
  13.  * through pointers to width, height, depth and type
  14.  *
  15.  * width is the number of columns in the image
  16.  * height is the number of rows in the image
  17.  * depth is the maximum value represented in the image (e.g 255 for 8
  18. bit)
  19.  * and type is the character that differentiates the image type ('1' to
  20. '6')
  21.  */
  22.  
  23. static char *getnext(FILE * p)
  24. {
  25.  static char     buf[8192];
  26.  char           *bp = buf;
  27.  int             c;
  28.  
  29.  buf[0] = 0;
  30.  start:
  31.  while (c = getc(p), c != EOF && isspace(c));
  32.  
  33.  if (feof(p)) return NULL;
  34.  /* Was start of a string so get the first character back */
  35.  
  36.  
  37.  if (c == '#') {
  38.   while (c = getc(p), c != EOF && c != '\n');
  39.   if (feof(p)) return NULL;
  40.   goto start;
  41.  } else
  42.   ungetc(c, p);
  43.  
  44.  bp = buf;
  45.  do {
  46.   c = getc(p);
  47.   if (c == EOF)
  48.    break;
  49.   if (isspace(c))
  50.    break;
  51.   *bp++ = c;
  52.  } while (bp < buf + sizeof(buf) - 1);
  53.  if (isspace(c)) ungetc(c, p);
  54.  *bp = '\0';
  55.  return buf;
  56. }
  57.  
  58.  
  59. static int number(const char *s)
  60. {
  61.     while (*s)
  62.     {
  63.         if (!isdigit(*s)) return 0;
  64.          s++;
  65.     }
  66.  
  67.     return 1;
  68. }
  69.  
  70.  
  71.  
  72. int ReadHeaderPN(FILE *fp, int *width, int *height, int *depth, char *type)
  73. {
  74.     char           *h;
  75.     int             c;
  76.  
  77.     h = getnext(fp);
  78.     if (h[0]!='P') return 0;
  79.     *type = h[1];
  80.     h = getnext(fp);
  81.     if (!number(h)) return 0;
  82.     *width = atoi(h);
  83.  
  84.     h = getnext(fp);
  85.     if (!number(h)) return 0;
  86.     *height = atoi(h);
  87.     h = getnext(fp);
  88.     if (!number(h)) return 0;
  89.     *depth = atoi(h);
  90.  
  91.     while (c = getc(fp))
  92.     {
  93.         if (c != '\n' && c != EOF) continue;
  94.         if (feof(fp))
  95.         {
  96.             /*fprintf(stderr, "Bad PN file\n");*/
  97.             return 0;
  98.         }
  99.         break;
  100.     }
  101.  
  102.     return 1;
  103. }
  104.  
  105.  
  106.  
  107. static Image2D *ReadImageRaw(FILE *fp, int w, int h, int  spp)
  108. {
  109.     Image2D *im;
  110.     unsigned char **data;
  111.     int byteWidth = w*spp;
  112.     int i;
  113.  
  114.     im = ImageAlloc(w, h);
  115.     if (im==NULL)
  116.     return NULL;
  117.  
  118.     data = (unsigned char **)im->data;
  119.     for (i=0; i<h; ++i)
  120.     fread(data[i], sizeof(unsigned char), byteWidth, fp);
  121.  
  122.     return im;
  123. }
  124.  
  125. static Image2D *ReadImagePN(FILE *fp)
  126. {
  127.     int w, h, depth;
  128.     char type;
  129.  
  130.     if (!ReadHeaderPN(fp, &w, &h, &depth, &type))
  131.         return NULL;
  132.  
  133.     switch (type)
  134.     {
  135.     case '5': return ReadImageRaw(fp, w, h, 1);
  136.     case '6': return ReadImageRaw(fp, w, h, 3);
  137.     default : return(NULL);
  138.     }
  139. }
  140.  
  141.  
  142.  
  143. static int WriteImagePN(FILE *fp, Image2D *im)
  144. {
  145.     unsigned char **data;
  146.     int byteWidth;
  147.     int i;
  148.  
  149.     if (im==NULL)
  150.     return 0;
  151.  
  152.     fprintf(fp, "P%s\n %d %d 255\n", (im->samplesPerPixel==3) ? "6" : "5",
  153.             im->width, im->height);
  154.  
  155.     data = (unsigned char **)im->data;
  156.     byteWidth = im->width*im->samplesPerPixel;
  157.  
  158.     for (i=0; i<im->height; ++i)
  159.         fwrite(data[i], sizeof(unsigned char), byteWidth, fp);
  160.  
  161.     return 1;
  162. }
  163.  
  164.  
  165. static int read_pnm_line(FILE *fp, unsigned char *p, int *control)
  166. {
  167.     static int w, h, depth;
  168.     static char type;
  169.     static int byteWidth;
  170.  
  171.     if (*control == 0)
  172.     {
  173.         if (!ReadHeaderPN(fp, &w, &h, &depth, &type))
  174.         return 0;
  175.  
  176.         switch (type)
  177.         {
  178.         case '5':
  179.             byteWidth = w;
  180.             break;
  181.         case '6':
  182.             byteWidth = 3*w;
  183.             break;
  184.         }
  185.         *control = 1;
  186.     }
  187.     fread(p, sizeof(unsigned char), byteWidth, fp);
  188.  
  189.     return 1;
  190. }
  191.  
  192.  
  193. int read_size(char *filename, int *width, int *height)
  194. {
  195.    FILE *fp;
  196.    int depth;
  197.    char type;
  198.  
  199.    fp = fopen(filename, "r");
  200.    if (fp==NULL) return 0;
  201.    ReadHeaderPN(fp, width, height, &depth, &type);
  202.  
  203.    return 1;
  204.  
  205. }
  206.  
  207.  
  208. Image2D *open_pnm_file(char *filename)
  209. {
  210.     FILE *fp;
  211.     Image2D *im;
  212.  
  213.     if ((fp=fopen(filename, "rb"))==NULL)
  214.     {
  215.         return NULL;
  216.     }
  217.  
  218.     im = ReadImagePN(fp);
  219.     fclose(fp);
  220.  
  221.  
  222.     strcpy(im->name,filename);
  223.  
  224.     return im;
  225. }
  226.  
  227.  
  228. int write_file_pnm(char *name, Image2D *im)
  229. {
  230.     FILE *fp;
  231.     int out;
  232.  
  233.     if ((fp=fopen(name, "wb"))==NULL)
  234.     {
  235.         return 0;
  236.     }
  237.  
  238.     out = WriteImagePN(fp, im);
  239.     fclose(fp);
  240.  
  241.     return out;
  242. }
  243.  
  244.  
  245.  
  246.