home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 2
/
FFMCD02.bin
/
new
/
comm
/
misc
/
elcheapofax
/
rcs
/
tofax.c,v
< prev
next >
Wrap
Text File
|
1993-12-21
|
21KB
|
656 lines
head 1.2;
access;
symbols
OCT93:1.2;
locks;
comment @ * @;
1.2
date 93.06.11.16.33.37; author Rhialto; state Exp;
branches;
next 1.1;
1.1
date 93.06.11.14.53.53; author Rhialto; state Exp;
branches;
next ;
desc
@Convert bitmaps to g3 data
@
1.2
log
@First real RCS checkin
@
text
@/* Derived from: */
/*
* Copyright 1992 DigiBoard, Inc. All rights reserved
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted.
* This software is provided "as is" without express or implied warranty.
*/
/* $Id$
* $Log$
*/
/* gdevdfax.c */
/* DigiBoard, Inc. DigiFAX driver for Ghostscript. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "g3.h"
#include "faxfile.h"
/**********************************************************************/
/*
* Generic fax output library
*/
/**********************************************************************/
typedef struct faxout
{
FILE *fp;
int fax_byte;
int fax_weight;
int pages;
int raw; /* 1: no header, 2: no EndOfPage */
char *stdiobuf;
} FAXOUT;
struct faxout *faxout_open (char *, int);
struct faxout *faxout_open_fp (FILE *, int);
int faxout_begin_page (FAXOUT *, int);
int faxout_eolcode (FAXOUT *);
int faxout_end_page (FAXOUT *);
int faxout_close (FAXOUT *);
unsigned short fax_ushort (unsigned short);
void tofax(struct faxout *faxp, unsigned char *p, int linebits);
void putwhitespan(register FAXOUT *faxp, int c);
void putblackspan(register FAXOUT *faxp, int c);
void putcode(register FAXOUT *faxp, tableentry *te);
void puteol(register FAXOUT *faxp);
void putbit(register FAXOUT *faxp, int d);
void flushbits(register FAXOUT *faxp);
/*************************************************************************
Internal routines
*************************************************************************/
/************************Coding FAX Routines*************************/
/*
* faxp = faxout_open(filename, raw);
* faxp = faxout_open_fp(fp, raw);
* faxout_begin_page(faxp, resolution);
* for(;;) tofax(faxp, linebuf, linebits);
* faxout_end_page(faxp);
* faxout_close(faxp);
*/
FAXOUT *
faxout_open_fp(FILE *fp, int raw)
{
register FAXOUT *faxp;
faxp = (FAXOUT *) malloc(sizeof(*faxp));
faxp->fp = fp;
faxp->fax_byte = 0;
faxp->fax_weight = 0x80;
faxp->pages = 0;
faxp->raw = raw;
#ifdef _DCC
setvbuf(fp, NULL, _IOFBF, 16384);
#else
if (faxp->stdiobuf = malloc(16384))
setvbuf(fp, faxp->stdiobuf, _IOFBF, 16384);
#endif
return (faxp);
}
FAXOUT *
faxout_open(char *filename, int raw)
{
register FILE *fp;
if (filename)
fp = fopen(filename, "wb");
else
fp = stdout;
if (!fp) return (NULL);
return(faxout_open_fp(fp, raw));
}
int
faxout_begin_page(FAXOUT *faxp, int resolution)
{
if (faxp->raw == 0) {
FAXHDR hdr;
memset(&hdr, 0, sizeof(hdr));
memcpy(hdr.id.magic, FAXMAGIC, sizeof(hdr.id.magic));
hdr.info.pagenum = fax_ushort(++faxp->pages);
hdr.info.msbfirst = 1;
hdr.info.hires = resolution;
hdr.jtinfo.jthires = resolution ? 0x40 : 0;
fwrite((char*)&hdr, sizeof(hdr), 1, faxp->fp);
}
puteol(faxp);
return (0);
}
int
faxout_end_page(FAXOUT *faxp)
{
flushbits(faxp);
if (faxp->raw < 2) {
puteol(faxp);
puteol(faxp);
puteol(faxp);
puteol(faxp);
puteol(faxp);
flushbits(faxp);
}
return (0);
}
int
faxout_close(FAXOUT *faxp)
{
unsigned short p = fax_ushort(faxp->pages);
fflush(faxp->fp);
if (faxp->raw == 0 && fseek(faxp->fp, (long) OFFSET_PAGES, 0) == 0)
fwrite((char*)&p, sizeof(p), 1, faxp->fp);
fclose(faxp->fp);
#ifndef _DCC
if (faxp->stdiobuf)
free(faxp->stdiobuf);
#endif
free(faxp);
return (0);
}
unsigned short
fax_ushort(unsigned short v)
{
static unsigned short x = 0x1122;
static unsigned short *xp = &x;
if ( *((unsigned char *)xp) == 0x22)
return (v);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