home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: OtherApp / OtherApp.zip / wincam.zip / winc_src.zip / pnm.c < prev    next >
C/C++ Source or Header  |  1997-02-28  |  3KB  |  128 lines

  1. /*
  2.  *
  3.  * routines to output imagemaps to PGM/PNM style output
  4.  *
  5.  * Copyright (C) 1996, Paul G. Fox
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify it
  8.  * under the terms of the GNU General Public License as published by the
  9.  * Free Software Foundation; either version 2 of the License, or (at your
  10.  * option) any later version.
  11.  * 
  12.  * This program is distributed in the hope that it will be useful, but
  13.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU General Public License along
  18.  * with this program; if not, write to the Free Software Foundation, Inc.,
  19.  * 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  * $Header: E:/winc/RCS/pnm.c 1.1 1997/03/01 03:44:14 Derek Exp Derek $
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include "trace.h"
  26.  
  27. /*
  28.  * write a greyscale imagemap in PGM format
  29.  */
  30. int
  31. winc_pgm_output(
  32.     unsigned char *image,
  33.     int rows,
  34.     int cols,
  35.     int maxgrey,
  36.     FILE *out)
  37. {
  38.  
  39.     TRACE("a", __FILE__ ": emitting PGM format\n");
  40.     fprintf(out, "P5\n"
  41.         "%d %d\n"
  42.         "%d\n", cols, rows, maxgrey);
  43.  
  44.     if (ferror(out))
  45.     return EOF;
  46.  
  47.     (void)fwrite(image, 1, rows*cols, out);
  48.  
  49.     if (ferror(out))
  50.     return EOF;
  51.  
  52.     return 0;
  53. }
  54.  
  55.  
  56. /*
  57.  * write an imagemap consisting of R,G,B bytes in PNM format
  58.  */
  59. int
  60. winc_pnm_output(
  61.     unsigned char *image,
  62.     int rows,
  63.     int cols,
  64.     int maxcolor,
  65.     FILE *out)
  66. {
  67.     TRACE("a", __FILE__ ": emitting PNM format\n");
  68.     fprintf(out, "P6\n"
  69.         "%d %d\n"
  70.         "%d\n", cols, rows, maxcolor);
  71.  
  72.     if (ferror(out))
  73.     return EOF;
  74.  
  75.     (void)fwrite(image, 3, rows*cols, out);
  76.  
  77.     if (ferror(out))
  78.     return EOF;
  79.  
  80.     return 0;
  81. }
  82.  
  83. /*
  84.  * write a monochrome bitmap imagemap in PBM format
  85.  */
  86.  
  87. static 
  88. unsigned char bit[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
  89.  
  90. int
  91. winc_pbm_output(
  92.     unsigned char *image,
  93.     unsigned char *tmpout,  /* at least imagesize/8, to hold output bits */
  94.     int rows,
  95.     int cols,
  96.     int cutoff,        /* above cutoff is white, below or equal is black */
  97.     FILE *out)
  98. {
  99.     int i;
  100.     unsigned char *op = tmpout;
  101.  
  102.     TRACE("a", __FILE__ ": emitting PBM format\n");
  103.     fprintf(out, "P4\n"
  104.         "%d %d\n",
  105.         cols, rows);
  106.  
  107.     if (ferror(out))
  108.     return EOF;
  109.  
  110.     *op = 0;
  111.     for (i = 0; i < rows * cols; i++, image++) {
  112.     if (*image > cutoff)
  113.         *op |= bit[i % 8];
  114.     if (i % 8 == 7) {
  115.         op++;
  116.         *op = 0;
  117.     }
  118.     }
  119.  
  120.     (void)fwrite(tmpout, 1, (rows*cols + 7)/8, out);
  121.  
  122.     if (ferror(out))
  123.     return EOF;
  124.  
  125.     return 0;
  126. }
  127.  
  128.