Source file: /~heha/hs/dos/hp2xx_hs.zip/to_pbm.c

/**
 ** to_pbm.c: PortableBitMap (PBM) converter part of project "hp2xx"
 **
 ** 92/04/14  V 1.00  CHL  Originating: Copied from to_pcx.c and to_gnu.c
 ** 92/04/16  V 1.01  CHL  Better error handling
 ** 92/05/17  V 1.01b HWW  Output to stdout if outfile == '-'
 ** 92/05/19  V 1.01c HWW  Abort if color mode
 ** 94/02/10  V 2.00  IJMP Add colour/use binary mode
 **			   (IJMP = Ian_MacPhedran@engr.usask.ca)
 ** 94/02/14  V 2.10  HWW  Adapted to changes in hp2xx.h
 **/

#include <stdio.h>
#include <stdlib.h>
#include "bresnham.h"
#include "hp2xx.h"

#define GGE >>=
#define MAXOUTPUTROWS 70



int PicBuf_to_PBM (const GEN_PAR *pg, const OUT_PAR *po) {
 FILE           *fd;
 DevPt		p;
 int		byte_c, linelen;
 const Byte	*line;
 PicBuf		*pb;
#ifdef PBMascii
 int             bit, row_count = 0;
 char	       *ppm[] = { "1 1 1", "0 0 0", "1 0 0", "0 1 0",
		"0 0 1", "0 1 1", "1 0 1", "1 1 0"};
#else
 int	       ppm[][3] = { {255, 255, 255}, {0,0,0}, {255,0,0}, {0,255,0},
		{0,0,255},{0,255,255},{255,0,255},{255,255,0}};
#endif /*PBMascii*/
 Byte		colour;

 if (!pg||!po) return ERROR;
 pb = po->picbuf;
 if (!pb) return ERROR;

 if (!pg->quiet)
	Eprintf("\nWriting PBM output: %s\n", po->outfile);
 if (*po->outfile != '-') {
  fd=FOPEN(po->outfile, WRITE_BIN);
  if (!fd) goto ERROR_EXIT;
 }else fd = stdout;

 if (pb->depth > 1) {
#ifdef PBMascii
  if (fprintf(fd, "P3\n")== EOF) goto ERROR_EXIT;
  if (fprintf(fd, "%d %d\n1\n", pb->pext.x, pb->pext.y)== EOF)
    goto ERROR_EXIT;
#else
  if (fprintf(fd, "P6\n")== EOF) goto ERROR_EXIT;
  if (fprintf(fd, "%d %d\n255\n", pb->pext.x, pb->pext.y)== EOF)
    goto ERROR_EXIT;
#endif /* PBMascii */

  for (p.y=0; p.y<pb->pext.y; p.y++) {
   if (!pg->quiet) ShowPercent(p.y,pb->pext.y);
   for (p.x=0; p.x<pb->pext.x; p.x++) {
    colour=GetPixel_from_PicBuf(pb,&p);
#ifdef PBMascii
    if (fprintf(fd,"%s",ppm[colour])==EOF) goto ERROR_EXIT;
#else
    if (fprintf(fd,"%c%c%c",ppm[colour][0],ppm[colour][1],
      ppm[colour][2]) == EOF) goto ERROR_EXIT;
#endif /* PBMascii */
#ifdef PBMascii
    row_count++;
    if (row_count >= MAXOUTPUTROWS) {
     row_count = 0;
     if(putc('\n', fd)== EOF) goto ERROR_EXIT;
    }else{
     if(putc(' ', fd)== EOF) goto ERROR_EXIT;
    }
#endif /* PBMascii */
   }
#ifdef PBMascii
   row_count = 0;
   putc('\n', fd);
#endif /* PBMascii */
  }
 }else{
#ifdef PBMascii
  if (fprintf(fd, "P1\n")== EOF)
#else
  if (fprintf(fd, "P4\n")== EOF)
#endif /* PBMascii */
  goto ERROR_EXIT;
  if (fprintf(fd, "%d %d\n", (pb->nb) * 8, pb->pext.y)==EOF) goto ERROR_EXIT;

  for (p.y=0; p.y<pb->pext.y; p.y++) {
   if (!pg->quiet) ShowPercent(p.y,pb->pext.y);
   for (p.x=0; p.x<pb->pext.x; p.x+=pb->ksi) {
    line=GetTileLine(pb,&p,&linelen);

#ifdef PBMascii
    for (byte_c=0; byte_c<linelen; byte_c++) {
     for (bit=128; bit; bit>>=1, x++) {
      if(putc(bit&line[byte_c]?'1':'0', fd)== EOF) goto ERROR_EXIT;
      row_count++;
      if (row_count >= MAXOUTPUTROWS) {
       row_count = 0;
       if(putc('\n', fd)== EOF) goto ERROR_EXIT;
      }
     }
    }
#else
    if(fwrite(line,linelen,1,fd)!=1) goto ERROR_EXIT;
#endif /* PBMascii */
   }
#ifdef PBMascii
   row_count = 0;
   putc('\n', fd);
#endif /* PBMascii */
  }
 }
 fflush(fd);

 if (!pg->quiet) Eprintf("\n");
 if (fd != stdout) fclose(fd);
 return 0;

ERROR_EXIT:
 PError ("write_PBM");
/*ERROR_EXIT_2:*/
 return ERROR;
}
Detected encoding: ASCII (7 bit)2