[comp.sys.mac.programmer] PICT file format

cgh@csg.UWaterloo.CA (Christopher G. Healey) (07/23/90)

   We're trying to get information on the format of PICT files, specifically
how bitmap images are saved in a PICT file.

   We have to convert a number of PC screen image files into Macintosh PICT
files on a PC. We need to write a conversion utility which will generate
a PICT file. Unfortunately, for a number of reasons, we cannot move the PC
image files to the Mac and create the PICT file there, it must be done on
the PC.

   TechNotes #21 gives a brief explination of the PICT format, and states
that CopyBits is used to represent bitmap images. However, there are still
a number of unanswered questions, namely:

 - When a CopyBits command is stored in a PICT file, how is the bitmap
   data itself stored? It seems that simple byte stuffing (8 pixels per
   byte) is used up to a certain image size, but that PackBits is used
   for larger images. If this is true, at what image size is PackBits used?

 - How large can a single CopyBits image be? IM says it should be no
   larger than 3K (this means we would have to use a number of CopyBits
   commands to represent the whole image). Does this limit also apply
   to PICT files?

   Any information or references would be greatly appreciated.

Christopher G. Healey
(cgh@csg.uwaterloo.ca)

masinter@parc.xerox.com (Larry Masinter) (07/24/90)

Ken Yap (at U of Rochester at the time, now in Oz) wrote a pict output
filter for pbmplus. See the Frequently Asked Questions in
comp.graphics for more details on how to get pbmplus. He mailed it to
me and, presumably, to Jef, but it hasn't shown up in the "official"
pbmplus yet.

Since it says "permission to redistribute" in the copyright notice,
here 'tis. 

#!/bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #!/bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	pict.h
#	picttoppm.1
#	picttoppm.c
#	ppmtopict.1
#	ppmtopict.c
# This archive created: Thu May 10 17:22:00 1990
# By:	Ken Yap ()
export PATH; PATH=/bin:$PATH
echo shar: extracting "'pict.h'" '(3343 characters)'
if test -f 'pict.h'
then
	echo shar: over-writing existing file "'pict.h'"
fi
cat << \SHAR_EOF > 'pict.h'
/* pict.h - header file for PICT2 files
*/

#ifndef _PICT_H_
#define _PICT_H_

#define HEADER_SIZE		512

#define	RUN_THRESH		3
#define	MAX_RUN			128		/* 0xff = 2, 0xfe = 3, etc */
#define	MAX_COUNT		128		/* 0x00 = 1, 0x01 = 2, etc */

/* Opcodes */
#define PICT_NOP		0x00
#define PICT_clipRgn		0x01
#define PICT_bkPat		0x02
#define PICT_txFont		0x03
#define PICT_txFace		0x04
#define PICT_txMode		0x05
#define PICT_spExtra		0x06
#define PICT_pnSize		0x07
#define PICT_pnMode		0x08
#define PICT_pnPat		0x09
#define PICT_thePat		0x0A
#define PICT_ovSize		0x0B
#define PICT_origin		0x0C
#define PICT_txSize		0x0D
#define PICT_fgColor		0x0E
#define PICT_bkColor		0x0F
#define PICT_txRatio		0x10
#define PICT_picVersion		0x11
#define	PICT_blPixPat		0x12
#define	PICT_pnPixPat		0x13
#define	PICT_fillPixPat		0x14
#define	PICT_pnLocHFrac		0x15
#define	PICT_chExtra		0x16
#define	PICT_rgbFgCol		0x1A
#define	PICT_rgbBkCol		0x1B
#define	PICT_hiliteMode		0x1C
#define	PICT_hiliteColor	0x1D
#define	PICT_defHilite		0x1E
#define	PICT_opColor		0x1F
#define PICT_line		0x20
#define PICT_line_from		0x21
#define PICT_short_line		0x22
#define PICT_short_line_from	0x23
#define PICT_long_text		0x28
#define PICT_DH_text		0x29
#define PICT_DV_text		0x2A
#define PICT_DHDV_text		0x2B
#define PICT_frameRect		0x30
#define PICT_paintRect		0x31
#define PICT_eraseRect		0x32
#define PICT_invertRect		0x33
#define PICT_fillRect		0x34
#define PICT_frameSameRect	0x38
#define PICT_paintSameRect	0x39
#define PICT_eraseSameRect	0x3A
#define PICT_invertSameRect	0x3B
#define PICT_fillSameRect	0x3C
#define PICT_frameRRect		0x40
#define PICT_paintRRect		0x41
#define PICT_eraseRRect		0x42
#define PICT_invertRRect	0x43
#define PICT_fillRRect		0x44
#define PICT_frameSameRRect	0x48
#define PICT_paintSameRRect	0x49
#define PICT_eraseSameRRect	0x4A
#define PICT_invertSameRRect	0x4B
#define PICT_fillSameRRect	0x4C
#define PICT_frameOval		0x50
#define PICT_paintOval		0x51
#define PICT_eraseOval		0x52
#define PICT_invertOval		0x53
#define PICT_fillOval		0x54
#define PICT_frameSameOval	0x58
#define PICT_paintSameOval	0x59
#define PICT_eraseSameOval	0x5A
#define PICT_invertSameOval	0x5B
#define PICT_fillSameOval	0x5C
#define PICT_frameArc		0x60
#define PICT_paintArc		0x61
#define PICT_eraseArc		0x62
#define PICT_invertArc		0x63
#define PICT_fillArc		0x64
#define PICT_frameSameArc	0x68
#define PICT_paintSameArc	0x69
#define PICT_eraseSameArc	0x6A
#define PICT_invertSameArc	0x6B
#define PICT_fillSameArc	0x6C
#define PICT_framePoly		0x70
#define PICT_paintPoly		0x71
#define PICT_erasePoly		0x72
#define PICT_invertPoly		0x73
#define PICT_fillPoly		0x74
#define PICT_frameSamePoly	0x78
#define PICT_paintSamePoly	0x79
#define PICT_eraseSamePoly	0x7A
#define PICT_invertSamePoly	0x7B
#define PICT_fillSamePoly	0x7C
#define PICT_frameRgn		0x80
#define PICT_paintRgn		0x81
#define PICT_eraseRgn		0x82
#define PICT_invertRgn		0x83
#define PICT_fillRgn		0x84
#define PICT_frameSameRgn	0x88
#define PICT_paintSameRgn	0x89
#define PICT_eraseSameRgn	0x8A
#define PICT_invertSameRgn	0x8B
#define PICT_fillSameRgn	0x8C
#define PICT_BitsRect		0x90
#define PICT_BitsRgn		0x91
#define PICT_PackBitsRect	0x98
#define PICT_PackBitsRgn	0x99
#define PICT_shortComment	0xA0
#define PICT_longComment	0xA1
#define PICT_EndOfPicture	0xFF
#define	PICT_headerOp		0x0C00

#endif /*_PICT_H_*/
SHAR_EOF
if test 3343 -ne "`wc -c 'pict.h'`"
then
	echo shar: error transmitting "'pict.h'" '(should have been 3343 characters)'
fi
echo shar: extracting "'picttoppm.1'" '(1403 characters)'
if test -f 'picttoppm.1'
then
	echo shar: over-writing existing file "'picttoppm.1'"
fi
cat << \SHAR_EOF > 'picttoppm.1'
.TH PICTTOPPM 1 "15 April 1990"
.SH NAME
picttoppm - convert a Mac PICT2 file into a portable pixmap
.SH SYNOPSIS
picttoppm [-nocval] [-extraskip skip] [pictfile]
.SH DESCRIPTION
Reads a Mac PICT2 file as input.
Produces a portable pixmap as output.
.PP
Since PICT2 has many non-raster primitives,
.B picttoppm
will only deal with raster opcodes and ignore opcodes
it does not recognize.
This is sufficient to convert images, however.
.SH OPTIONS
The first 512 bytes of a PICT data fork contain application specific
data which is skipped by picttoppm. You can request an additional skip
with -extraskip.
.PP
Some PICT2 files have color lookup tables without values in each entry.
The value is the index of the entry in the CLUT. -nocval tells picttoppm
to make this assumption instead of reading the value from the entry.
Normally this type of color lookup table is detected automatically.
However if your pictures come out all one color, try this option.
.SH "SEE ALSO"
ppmtopict(1), ppm(5)
.SH AUTHOR
Copyright (C) 1990 by Ken Yap.

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided
that the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.  This software is provided "as is" without express or
implied warranty.
SHAR_EOF
if test 1403 -ne "`wc -c 'picttoppm.1'`"
then
	echo shar: error transmitting "'picttoppm.1'" '(should have been 1403 characters)'
fi
echo shar: extracting "'picttoppm.c'" '(16800 characters)'
if test -f 'picttoppm.c'
then
	echo shar: over-writing existing file "'picttoppm.c'"
fi
cat << \SHAR_EOF > 'picttoppm.c'
/*
** picttoppm.c - read a PICT2 file and produce a portable pixmap
**
** Copyright (C) 1990 by Ken Yap.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#include <stdio.h>
#include "ppm.h"
#include "pict.h"
#ifdef SYSV
#include <string.h>
#else /*SYSV*/
#include <strings.h>
#endif /*SYSV*/

#define max(a,b) ((a) > (b) ? (a) : (b))

main(argc, argv)
int argc;
char *argv[];
{
	FILE *ifd;
	int argn, picsize, bytesRead, oldBytesRead, endOfPicture, gotBits, opcode;
	int x1, y1, x2, y2;
	int rows, cols;
	int nocvals, extraskip, version;
	char *usage = "[-nocvals -extraskip <n>] [pictfile]";

	pm_progname = argv[0];

	argn = 1;
	nocvals = 0;
	extraskip = 0;

	/* Check for flags. */
	if (argn < argc && argv[argn][0] == '-')
	{
		if (strncmp(argv[argn],"-extraskip",max(strlen(argv[argn]),2)) == 0)
		{
			argn++;
			if (argn == argc || sscanf(argv[argn], "%d", &extraskip) != 1)
				pm_usage(usage);
		}
		if (strncmp(argv[argn],"-nocvals",max(strlen(argv[argn]),2)) == 0)
			nocvals = 1;
		else
			pm_usage(usage);
		argn++;
	}

	if (argn < argc)
	{
		ifd = pm_openr(argv[argn]);
		argn++;
	}
	else
		ifd = stdin;

	if (argn != argc)
		pm_usage(usage);

	/* Skip blank header. */
	(void)skipBytes(ifd, extraskip);
	(void)skipBytes(ifd, HEADER_SIZE);

	/* Read size. */
	bytesRead = getBigShort(ifd, &picsize);

	/* Read picFrame. */
	bytesRead += getRect(ifd, &x1, &y1, &x2, &y2);
	rows = y2 - y1;
	cols = x2 - x1;

	endOfPicture = 0;
	gotBits = 0;
	while (! endOfPicture)
	{
		bytesRead += getBigShort(ifd, &opcode);
		oldBytesRead = bytesRead;
		switch (opcode)
		{
		/* This group of opcodes is handled correctly. */

		case PICT_NOP:
			break;

		case PICT_picVersion:
			bytesRead += getBigShort(ifd, &version);
			if (version != 0x02ff)
				pm_error("unknown PICT version - %d", version, 0,0,0,0);
			break;

		case PICT_shortComment:
			bytesRead += skipBytes(ifd, 2);
			break;

		case PICT_longComment:
			{
				int size;
				bytesRead += skipBytes(ifd, 2);
				bytesRead += getBigShort(ifd, &size);
				bytesRead += skipBytes(ifd, size);
				break;
			}

		case PICT_BitsRect:
		case PICT_BitsRgn:
		case PICT_PackBitsRect:
		case PICT_PackBitsRgn:
			bytesRead += getBits(ifd, opcode, rows, cols, nocvals);
			gotBits = 1;
			break;

		case PICT_EndOfPicture:
			endOfPicture = 1;
			break;

		/* This group of opcodes is skipped correctly. */

		case PICT_clipRgn:
			pm_message("clipRgn - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 10);
			break;

		case PICT_bkPat:
			pm_message("bkPat - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 8);
			break;

		case PICT_txFace:
			pm_message("txFace - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 1);
			break;

		case PICT_pnSize:
			pm_message("pnPat - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 4);
			break;


		case PICT_pnPat:
			pm_message("pnPat - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 8);
			break;

		case PICT_thePat:
			pm_message("thePat - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 8);
			break;

		case PICT_origin:
			pm_message("origin - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 4);
			break;

		case PICT_pnLocHFrac:
			pm_message("pnLocHFrac - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 2);
			break;

		case PICT_chExtra:
			pm_message("chExtra - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 2);
			break;

		case PICT_hiliteMode:
			pm_message("hiliteMode - skipping", 0,0,0,0,0);
			break;

		case PICT_defHilite:
			pm_message("defHilite - skipping", 0,0,0,0,0);
			break;

		case PICT_headerOp:
			bytesRead += getHeader(ifd);
			break;

		case PICT_frameRect:
			pm_message("frameRect - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 8);
			break;

		case PICT_paintRect:
			pm_message("paintRect - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 8);
			break;

		case PICT_eraseRect:
			pm_message("eraseRect - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 8);
			break;

		case PICT_invertRect:
			pm_message("invertRect - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 8);
			break;

		case PICT_fillRect:
			pm_message("fillRect - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 8);
			break;

		case PICT_frameSameRect:
			pm_message("frameSameRect - skipping", 0,0,0,0,0);
			break;

		case PICT_paintSameRect:
			pm_message("paintSameRect - skipping", 0,0,0,0,0);
			break;

		case PICT_eraseSameRect:
			pm_message("eraseSameRect - skipping", 0,0,0,0,0);
			break;

		case PICT_invertSameRect:
			pm_message("invertSameRect - skipping", 0,0,0,0,0);
			break;

		case PICT_fillSameRect:
			pm_message("fillSameRect - skipping", 0,0,0,0,0);
			break;

		case PICT_frameRRect:
			pm_message("frameRRect - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 8);
			break;

		case PICT_paintRRect:
			pm_message("paintRRect - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 8);
			break;

		case PICT_eraseRRect:
			pm_message("eraseRRect - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 8);
			break;

		case PICT_invertRRect:
			pm_message("invertRRect - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 8);
			break;

		case PICT_fillRRect:
			pm_message("fillRRect - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 8);
			break;

		case PICT_frameSameRRect:
			pm_message("frameSameRRect - skipping", 0,0,0,0,0);
			break;

		case PICT_paintSameRRect:
			pm_message("paintSameRRect - skipping", 0,0,0,0,0);
			break;

		case PICT_eraseSameRRect:
			pm_message("eraseSameRRect - skipping", 0,0,0,0,0);
			break;

		case PICT_invertSameRRect:
			pm_message("invertSameRRect - skipping", 0,0,0,0,0);
			break;

		case PICT_fillSameRRect:
			pm_message("fillSameRRect - skipping", 0,0,0,0,0);
			break;

		case PICT_frameRgn:
			pm_message("frameRgn - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 10);
			break;

		case PICT_paintRgn:
			pm_message("paintRgn - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 10);
			break;

		case PICT_eraseRgn:
			pm_message("eraseRgn - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 10);
			break;

		case PICT_invertRgn:
			pm_message("invertRgn - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 10);
			break;

		case PICT_fillRgn:
			pm_message("fillRgn - skipping", 0,0,0,0,0);
			bytesRead += skipBytes(ifd, 10);
			break;

		case PICT_frameSameRgn:
			pm_message("frameSameRgn - skipping", 0,0,0,0,0);
			break;

		case PICT_paintSameRgn:
			pm_message("paintSameRgn - skipping", 0,0,0,0,0);
			break;

		case PICT_eraseSameRgn:
			pm_message("eraseSameRgn - skipping", 0,0,0,0,0);
			break;

		case PICT_invertSameRgn:
			pm_message("invertSameRgn - skipping", 0,0,0,0,0);
			break;

		case PICT_fillSameRgn:
			pm_message("fillSameRgn - skipping", 0,0,0,0,0);
			break;


		/* And this group of opcodes is of unknown length, and can't
		** be handled or skipped correctly. */

		case PICT_blPixPat:
			pm_error("blPixPat - can't handle", 0,0,0,0,0);
		case PICT_pnPixPat:
			pm_error("pnPixPat - can't handle", 0,0,0,0,0);
		case PICT_fillPixPat:
			pm_error("fillPixPat - can't handle", 0,0,0,0,0);
		case PICT_rgbFgCol:
			pm_error("rgbFgCol - can't handle", 0,0,0,0,0);
		case PICT_rgbBkCol:
			pm_error("rgbBkCol - can't handle", 0,0,0,0,0);
		case PICT_hiliteColor:
			pm_error("hiliteColor - can't handle", 0,0,0,0,0);
		case PICT_opColor:
			pm_error("opColor - can't handle", 0,0,0,0,0);
		case PICT_txFont:
			pm_error("txFont - can't handle", 0,0,0,0,0);
		case PICT_txMode:
			pm_error("txMode - can't handle", 0,0,0,0,0);
		case PICT_spExtra:
			pm_error("spExtra - can't handle", 0,0,0,0,0);
		case PICT_pnMode:
			pm_error("pnMode - can't handle", 0,0,0,0,0);
		case PICT_ovSize:
			pm_error("ovSize - can't handle", 0,0,0,0,0);
		case PICT_txSize:
			pm_error("txSize - can't handle", 0,0,0,0,0);
		case PICT_fgColor:
			pm_error("fgColor - can't handle", 0,0,0,0,0);
		case PICT_bkColor:
			pm_error("bkColor - can't handle", 0,0,0,0,0);
		case PICT_txRatio:
			pm_error("txRatio - can't handle", 0,0,0,0,0);
		case PICT_line:
			pm_error("line - can't handle", 0,0,0,0,0);
		case PICT_line_from:
			pm_error("from - can't handle", 0,0,0,0,0);
		case PICT_short_line:
			pm_error("line - can't handle", 0,0,0,0,0);
		case PICT_short_line_from:
			pm_error("from - can't handle", 0,0,0,0,0);
		case PICT_long_text:
			pm_error("text - can't handle", 0,0,0,0,0);
		case PICT_DH_text:
			pm_error("text - can't handle", 0,0,0,0,0);
		case PICT_DV_text:
			pm_error("text - can't handle", 0,0,0,0,0);
		case PICT_DHDV_text:
			pm_error("text - can't handle", 0,0,0,0,0);
		case PICT_frameOval:
			pm_error("frameOval - can't handle", 0,0,0,0,0);
		case PICT_paintOval:
			pm_error("paintOval - can't handle", 0,0,0,0,0);
		case PICT_eraseOval:
			pm_error("eraseOval - can't handle", 0,0,0,0,0);
		case PICT_invertOval:
			pm_error("invertOval - can't handle", 0,0,0,0,0);
		case PICT_fillOval:
			pm_error("fillOval - can't handle", 0,0,0,0,0);
		case PICT_frameSameOval:
			pm_error("frameSameOval - can't handle", 0,0,0,0,0);
		case PICT_paintSameOval:
			pm_error("paintSameOval - can't handle", 0,0,0,0,0);
		case PICT_eraseSameOval:
			pm_error("eraseSameOval - can't handle", 0,0,0,0,0);
		case PICT_invertSameOval:
			pm_error("invertSameOval - can't handle", 0,0,0,0,0);
		case PICT_fillSameOval:
			pm_error("fillSameOval - can't handle", 0,0,0,0,0);
		case PICT_frameArc:
			pm_error("frameArc - can't handle", 0,0,0,0,0);
		case PICT_paintArc:
			pm_error("paintArc - can't handle", 0,0,0,0,0);
		case PICT_eraseArc:
			pm_error("eraseArc - can't handle", 0,0,0,0,0);
		case PICT_invertArc:
			pm_error("invertArc - can't handle", 0,0,0,0,0);
		case PICT_fillArc:
			pm_error("fillArc - can't handle", 0,0,0,0,0);
		case PICT_frameSameArc:
			pm_error("frameSameArc - can't handle", 0,0,0,0,0);
		case PICT_paintSameArc:
			pm_error("paintSameArc - can't handle", 0,0,0,0,0);
		case PICT_eraseSameArc:
			pm_error("eraseSameArc - can't handle", 0,0,0,0,0);
		case PICT_invertSameArc:
			pm_error("invertSameArc - can't handle", 0,0,0,0,0);
		case PICT_fillSameArc:
			pm_error("fillSameArc - can't handle", 0,0,0,0,0);
		case PICT_framePoly:
			pm_error("framePoly - can't handle", 0,0,0,0,0);
		case PICT_paintPoly:
			pm_error("paintPoly - can't handle", 0,0,0,0,0);
		case PICT_erasePoly:
			pm_error("erasePoly - can't handle", 0,0,0,0,0);
		case PICT_invertPoly:
			pm_error("invertPoly - can't handle", 0,0,0,0,0);
		case PICT_fillPoly:
			pm_error("fillPoly - can't handle", 0,0,0,0,0);
		case PICT_frameSamePoly:
			pm_error("frameSamePoly - can't handle", 0,0,0,0,0);
		case PICT_paintSamePoly:
			pm_error("paintSamePoly - can't handle", 0,0,0,0,0);
		case PICT_eraseSamePoly:
			pm_error("eraseSamePoly - can't handle", 0,0,0,0,0);
		case PICT_invertSamePoly:
			pm_error("invertSamePoly - can't handle", 0,0,0,0,0);
		case PICT_fillSamePoly:
			pm_error("fillSamePoly - can't handle", 0,0,0,0,0);

		default:
			pm_error(
			    "unknown PICT2 opcode - 0x%04x - can't handle", opcode,
			    0,0,0,0);
		}

		if ((bytesRead - oldBytesRead) & 1)
			bytesRead += skipBytes(ifd, 1);
	}

	pm_close(ifd);

	if (! gotBits)
		pm_error("no bits rectangles seen", 0,0,0,0,0);
	if (bytesRead % 65536L != picsize)
		pm_message( "bytes read (%d) mod 65536 != piczise (%d) - warning only",
		    bytesRead, picsize, 0,0,0);

	exit(0);
}

getHeader(fd)
FILE *fd;
{
	return (skipBytes(fd, 24));
}

struct cmap
{
	int	val, red, green, blue;
};

getBits(fd, opcode, rows, cols, nocvals)
FILE *fd;
int opcode, rows, cols, nocvals;
{
	int oldbr, byteCount;
	int rowBytes, version, packType, pixelType, pixelSize;
	int cmpCount, cmpSize, mode;
	long packSize, planeBytes, pmTable, pmReserved;
	int bnd_x1, bnd_y1, bnd_x2, bnd_y2;
	int src_x1, src_y1, src_x2, src_y2;
	int dst_x1, dst_y1, dst_x2, dst_y2;
	int padRight, b, br, row;
	pixel *pixelrow;
	pixval maxcol;
	register int col;
	register pixel *pP;
	struct cmap colortable[256];

	br = getBigShort(fd, &rowBytes);
	if (! (rowBytes & 0x8000))
		pm_error("rowBytes does not have high bit set -- %04x", rowBytes,
		    0,0,0,0);
	rowBytes &= ~0x8000;

	br += getRect(fd, &bnd_x1, &bnd_y1, &bnd_x2, &bnd_y2);
	br += getBigShort(fd, &version);
	br += getBigShort(fd, &packType);
	br += getBigLong(fd, &packSize);
	br += skipBytes(fd, 8);		/* hRes and vRes */
	br += getBigShort(fd, &pixelType);
	br += getBigShort(fd, &pixelSize);
	if (pixelSize != 1 && pixelSize != 2 && pixelSize != 4 && pixelSize != 8)
		pm_error("illegal pixelSize -- %d", pixelSize, 0, 0, 0, 0);
	br += getBigShort(fd, &cmpCount);
	br += getBigShort(fd, &cmpSize);
	br += getBigLong(fd, &planeBytes);
	br += getBigLong(fd, &pmTable);
	br += getBigLong(fd, &pmReserved);
	bzero((char *)colortable, sizeof(colortable));
	br += getColorTable(fd, colortable, &maxcol, nocvals);
	br += getRect(fd, &src_x1, &src_y1, &src_x2, &src_y2);
	br += getRect(fd, &dst_x1, &dst_y1, &dst_x2, &dst_y2);

	br += getBigShort(fd, &mode);
	if (mode != 0)
		pm_message("bits rectangle mode %d != 0 -- ignoring", mode, 0,0,0,0);

	if (opcode == PICT_BitsRgn || opcode == PICT_PackBitsRgn)
		br += skipBytes(fd, 10);

	padRight = rowBytes * 8 / pixelSize - (bnd_x2 - bnd_x1);

	(void)getInit(opcode);
	ppm_writeppminit(stdout, cols, rows, 255);
	pixelrow = ppm_allocrow(cols);

	for (row = bnd_y1; row < bnd_y2; row++)
	{
		br += getInitRow(fd, rowBytes, &byteCount);
		oldbr = br;
		for (col = bnd_x1, pP = pixelrow; col < bnd_x2; col++, pP++)
		{
			br += getPel(fd, &b, pixelSize);
			if (0 <= col - bnd_x1 && col - bnd_x1 < cols)
				PPM_ASSIGN( *pP,
					colortable[b].red, colortable[b].green,
					colortable[b].blue);
		}
		for (col = 0; col < padRight; col++)
			br += getPel(fd, &b, pixelSize);
		ppm_writeppmrow(stdout, pixelrow, cols, maxcol);
		if ((br - oldbr) != byteCount)
			pm_error("row %d: inconsistency between byteCount and bytes read -- %d %d",
				row, byteCount, br - oldbr, 0,0);
	}
	return (br);
}

getColorTable(fd, ct, maxcol, nocvals)
FILE *fd;
struct cmap ct[];
pixval *maxcol;
int nocvals;
{
	register int i;
	int br, v, c;
	long ctSeed;
	int ctFlags, ctSize;

	br = getBigLong(fd, &ctSeed);
	br += getBigShort(fd, &ctFlags);
	br += getBigShort(fd, &ctSize);
	for (i = 0; i <= ctSize; ++i)
	{
		br += getBigShort(fd, &v);
		if (nocvals || ctFlags & 0x8000)
			v = i;
		else
			v &= 0xff;
		ct[v].val = v;
		br += getBigShort(fd, &c);
		ct[v].red = c / 256;
		br += getBigShort(fd, &c);
		ct[v].green = c / 256;
		br += getBigShort(fd, &c);
		ct[v].blue = c / 256;
	}
	*maxcol = ctSize;
	return (br);
}

getByte(fd, cP)
FILE *fd;
int *cP;
{
	*cP = getc(fd);
	if (*cP == EOF)
		pm_error("premature EOF", 0,0,0,0,0);
	return (1);
}

skipBytes(fd, n)
FILE *fd;
int n;
{
	int i, j;

	for (i = 0; i < n; i++)
		(void)getByte(fd, &j);
	return (n);
}

getBigShort(fd, sP)
FILE *fd;
int *sP;
{
	int br, h, l;

	br = getByte(fd, &h);
	br += getByte(fd, &l);
	*sP = (h << 8) + l;
	return (br);
}

getBigLong(fd, lP)
FILE *fd;
long *lP;
{
	int br, b1, b2, b3, b4;

	br = getByte(fd, &b1);
	br += getByte(fd, &b2);
	br += getByte(fd, &b3);
	br += getByte(fd, &b4);
	*lP = (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
	return br;
}

getRect(fd, x1P, y1P, x2P, y2P)
FILE *fd;
int *x1P, *y1P, *x2P, *y2P;
{
	int br;

	br = getBigShort(fd, y1P);
	br += getBigShort(fd, x1P);
	br += getBigShort(fd, y2P);
	br += getBigShort(fd, x2P);
	return (br);
}

int packed;
int count, repeat, item, bitshift;

getInit(opcode)
int opcode;
{
	packed = (opcode == PICT_PackBitsRect || opcode == PICT_PackBitsRgn);
	bitshift = 0;
	count = 0;
	return (0);
}

getInitRow(fd, rowBytes, bc)
FILE *fd;
int rowBytes;
int *bc;
{
	int br;

	br = 0;
	if (packed)
	{
		if (rowBytes > 250)
			br += getBigShort(fd, bc);
		else
			br += getByte(fd, bc);
	}
	return (br);
}

getPel(fd, iP, size)
FILE *fd;
int *iP;
int size;
{
	int br;
	static int mask[9] =
	{ 
		0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff
	};

	br = 0;
	if (bitshift <= 0)
	{
		if (packed)
		{
			if (count <= 0)
			{
				br += getByte(fd, &count);
				if (count < 128)
				{
					repeat = 0;
					br += getByte(fd, &item);
				}
				else
				{
					repeat = 1;
					br += getByte(fd, &item);
					count = 256 - count;
				}
			}
			else
			{
				if (! repeat)
				{
					br += getByte(fd, &item);
				}
				count--;
			}
		}
		else
			br += getByte(fd, &item);
		bitshift = 8;
	}
	*iP = (item >> (bitshift - size)) & mask[size];
	bitshift -= size;
	return (br);
}
SHAR_EOF
if test 16800 -ne "`wc -c 'picttoppm.c'`"
then
	echo shar: error transmitting "'picttoppm.c'" '(should have been 16800 characters)'
fi
echo shar: extracting "'ppmtopict.1'" '(1303 characters)'
if test -f 'ppmtopict.1'
then
	echo shar: over-writing existing file "'ppmtopict.1'"
fi
cat << \SHAR_EOF > 'ppmtopict.1'
.TH PPMTOPICT 1 "15 April 1990"
.SH NAME
ppmtopict - convert a portable pixmap into a Mac PICT2 file
.SH SYNOPSIS
ppmtopict [ppmfile]
.SH DESCRIPTION
Reads a portable pixmap as input.
Produces a Mac PICT2 file as output.
.PP
The generated file is only the data fork of a picture.
You will need a program such as
.B mcvert
to generate a Macbinary or a BinHex file that contains the necessary
information to identify the file as a PICT file to MacOS.
.PP
Even though PICT2 supports 2 and 4 bits per pixel,
.B ppmtopict
always generates a 8 bits per pixel file.
.SH BUGS
The picture size field is only correct if the output is to a file
since writing into this field requires seeking backwards on a file.
However the PICT documentation seems to suggest that this field is
not critical anyway since it is only the lower 16 bits of the picture size.
.SH "SEE ALSO"
picttoppm(1), ppm(5), mcvert(1)
.SH AUTHOR
Copyright (C) 1990 by Ken Yap.

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided
that the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.  This software is provided "as is" without express or
implied warranty.
SHAR_EOF
if test 1303 -ne "`wc -c 'ppmtopict.1'`"
then
	echo shar: error transmitting "'ppmtopict.1'" '(should have been 1303 characters)'
fi
echo shar: extracting "'ppmtopict.c'" '(7066 characters)'
if test -f 'ppmtopict.c'
then
	echo shar: over-writing existing file "'ppmtopict.c'"
fi
cat << \SHAR_EOF > 'ppmtopict.c'
/*
** ppmtopict.c - read a portable pixmap and produce a PICT2 file.
**
** Copyright (C) 1990 by Ken Yap.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#include <stdio.h>
#include "ppm.h"
#include "ppmcmap.h"
#include "pict.h"

#define MAXCOLORS 256
colorhash_table cht;

main(argc, argv)
int argc;
char *argv[];
{
	FILE *ifd;
	int argn, rows, cols, colors, i, row, oc;
	register pixel **pixels;
	char *packed;
	pixval maxval;
	long lmaxval, rval, gval, bval;
	colorhist_vector chv;
	char *malloc();

	pm_progname = argv[0];

	argn = 1;
	if (argn < argc)
	{
		ifd = pm_openr(argv[1]);
		argn++;
	}
	else
		ifd = stdin;
	if (argn != argc)
		pm_usage("[ppmfile]");

	pixels = ppm_readppm(ifd, &cols, &rows, &maxval);
	if (cols < 8)
		pm_error("ppm input too narrow, must be >= 8 pixels wide", 0,0,0,0);
	lmaxval = (long)maxval;
	pm_close(ifd);

	/* Figure out the colormap. */
	fprintf(stderr, "(Computing colormap...");
	fflush(stderr);
	chv = ppm_computecolorhist(pixels, cols, rows, MAXCOLORS, &colors);
	if (chv == (colorhist_vector) 0)
	{
		fprintf(stderr, "  Too many colors!  Try running the pixmap through 'ppmquant 256'.\n");
		exit(1);
	}
	fprintf(stderr, "  Done.  %d colors found.)\n", colors);
	/* Make a hash table for fast color lookup. */
	cht = ppm_colorhisttocolorhash(chv, colors);

	/* write the header */
	putFill(stdout, HEADER_SIZE);

	/* write picSize and picFrame */
	putShort(stdout, 0);
	putRect(stdout, 0, 0, rows, cols);

	/* write version op and version */
	putShort(stdout, PICT_picVersion);
	putShort(stdout, 0x02FF);
	putShort(stdout, PICT_headerOp);
	putLong(stdout, -1L);
	putFixed(stdout, 0, 0);
	putFixed(stdout, 0, 0);
	putFixed(stdout, cols, 0);
	putFixed(stdout, rows, 0);
	putFill(stdout, 4);

	/* seems to be needed by many PICT2 programs */
	putShort(stdout, PICT_clipRgn);
	putShort(stdout, 10);
	putRect(stdout, 0, 0, rows, cols);

	/* write picture */
	putShort(stdout, PICT_PackBitsRect);
	putShort(stdout, cols | 0x8000);
	putRect(stdout, 0, 0, rows, cols);
	putShort(stdout, 0);	/* pmVersion */
	putShort(stdout, 0);	/* packType */
	putLong(stdout, 0L);	/* packSize */
	putFixed(stdout, 72, 0);	/* hRes */
	putFixed(stdout, 72, 0);	/* vRes */
	putShort(stdout, 0);	/* pixelType */
	putShort(stdout, 8);	/* pixelSize */
	putShort(stdout, 1);	/* cmpCount */
	putShort(stdout, 8);	/* cmpSize */
	putLong(stdout, 0L);	/* planeBytes */
	putLong(stdout, 0L);	/* pmTable */
	putLong(stdout, 0L);	/* pmReserved */
	putLong(stdout, 0L);	/* ctSeed */
	putShort(stdout, 0);	/* ctFlags */
	putShort(stdout, colors-1);	/* ctSize */

	/* Write out the colormap. */
	for (i = 0; i < colors; i++)
	{
		putShort(stdout, i);
		rval = PPM_GETR(chv[i].color);
		gval = PPM_GETG(chv[i].color);
		bval = PPM_GETB(chv[i].color);
		if (lmaxval != 65535L)
		{
			rval = rval * 65535L / lmaxval;
			gval = gval * 65535L / lmaxval;
			bval = bval * 65535L / lmaxval;
		}
		putShort(stdout, (short)rval);
		putShort(stdout, (short)gval);
		putShort(stdout, (short)bval);
	}

	putRect(stdout, 0, 0, rows, cols);	/* srcRect */
	putRect(stdout, 0, 0, rows, cols);	/* dstRect */
	putShort(stdout, 0);			/* mode */

	/* Finally, write out the data. */
	packed = malloc((unsigned)(cols+cols/MAX_COUNT+1));
	oc = 0;
	for (row = 0; row < rows; row++)
		oc += putRow(stdout, row, cols, pixels[row], packed);

	/* if we wrote an odd number of pixdata bytes, pad */
	if (oc & 1)
		putc(0, stdout);
	putShort(stdout, PICT_EndOfPicture);

	lmaxval = ftell(stdout) - HEADER_SIZE;
	if (fseek(stdout, (long)HEADER_SIZE, 0) >= 0)
		putShort(stdout, (short)(lmaxval & 0xffff));

	exit(0);
}

putFill(fd, n)
FILE *fd;
int n;
{
	register int i;

	for (i = 0; i < n; i++)
		putc(0, fd);
}

putShort(fd, i)
FILE *fd;
int i;
{
	putc((i >> 8) & 0xff, fd);
	putc(i & 0xff, fd);
}

putLong(fd, i)
FILE *fd;
long i;
{
	putc((int)((i >> 24) & 0xff), fd);
	putc(((int)(i >> 16) & 0xff), fd);
	putc(((int)(i >> 8) & 0xff), fd);
	putc((int)(i & 0xff), fd);
}

putFixed(fd, in, frac)
FILE *fd;
int in, frac;
{
	putShort(fd, in);
	putShort(fd, frac);
}

putRect(fd, x1, x2, y1, y2)
FILE *fd;
int x1, x2, y1, y2;
{
	putShort(fd, x1);
	putShort(fd, x2);
	putShort(fd, y1);
	putShort(fd, y2);
}

#define	RUNLENGTH
#ifdef	RUNLENGTH

#define		runtochar(c)	(257-(c))
#define		counttochar(c)	((c)-1)

int putRow(fd, row, cols, rowpixels, packed)
FILE *fd;
int row, cols;
pixel *rowpixels;
char *packed;
{
	register int i;
	int packcols, count, run, rep, oc;
	register pixel *pP;
	pixel lastp;
	register char *p;

	run = count = 0;
	for (cols--, i = cols, pP = rowpixels + cols, p = packed, lastp = *pP;
		i >= 0; i--, lastp = *pP, pP--)
	{
		if (PPM_EQUAL(lastp, *pP))
			run++;
		else if (run < RUN_THRESH)
		{
			while (run > 0)
			{
				*p++ = ppm_lookupcolor(cht, lastp);
				run--;
				count++;
				if (count == MAX_COUNT)
				{
					*p++ = counttochar(MAX_COUNT);
					count -= MAX_COUNT;
				}
			}
			run = 1;
		}
		else
		{
			if (count > 0)
				*p++ = counttochar(count);
			count = 0;
			while (run > 0)
			{
				rep = run > MAX_RUN ? MAX_RUN : run;
				*p++ = ppm_lookupcolor(cht, lastp);
				*p++ = runtochar(rep);
				run -= rep;
			}
			run = 1;
		}
	}
	if (run < RUN_THRESH)
	{
		while (run > 0)
		{
			*p++ = ppm_lookupcolor(cht, lastp);
			run--;
			count++;
			if (count == MAX_COUNT)
			{
				*p++ = counttochar(MAX_COUNT);
				count -= MAX_COUNT;
			}
		}
	}
	else
	{
		if (count > 0)
			*p++ = counttochar(count);
		count = 0;
		while (run > 0)
		{
			rep = run > MAX_RUN ? MAX_RUN : run;
			*p++ = ppm_lookupcolor(cht, lastp);
			*p++ = runtochar(rep);
				run -= rep;
		}
		run = 1;
	}
	if (count > 0)
		*p++ = counttochar(count);

	packcols = p - packed;		/* how many did we write? */
	if (cols > 250)
	{
		putShort(fd, packcols);
		oc = packcols + 2;
	}
	else
	{
		putc(packcols, fd);
		oc = packcols + 1;
	}

	/* now write out the packed row */
	while(p != packed)
	{
		--p;
		putc(*p, fd);
	}

	return (oc);
}

#else	/* RUNLENGTH */

/* real dumb putRow with no compression */
int putRow(fd, row, cols, rowpixels, packed)
FILE *fd;
int row, cols;
pixel *rowpixels;
char *packed;
{
	register int i, j, bc, oc;
	register pixel *pP;

	bzero(aux, cols);
	bc = cols + (cols + MAX_COUNT - 1) / MAX_COUNT;
	if (bc > 250)
	{
		putShort(fd, bc);
		oc = bc + 2;
	}
	else
	{
		putc(bc, fd);
		oc = bc + 1;
	}
	for (i = 0, pP = rowpixels; i < cols;)
	{
		if (cols - i > MAX_COUNT)
		{
			putc(MAX_COUNT - 1, fd);
			for (j = 0; j < MAX_COUNT; j++)
			{
				putc(ppm_lookupcolor(cht, *pP), fd);
				pP++;
			}
			i += MAX_COUNT;
		}
		else
		{
			putc(cols - i - 1, fd);
			for (j = 0; j < cols - i; j++)
			{
				putc(ppm_lookupcolor(cht, *pP), fd);
				pP++;
			}
			i = cols;
		}
	}
	return (oc);
}
#endif	/* RUNLENGTH */
SHAR_EOF
if test 7066 -ne "`wc -c 'ppmtopict.c'`"
then
	echo shar: error transmitting "'ppmtopict.c'" '(should have been 7066 characters)'
fi
#	End of shell archive
exit 0



--
Larry Masinter (masinter@parc.xerox.com)
Xerox Palo Alto Research Center (PARC)
3333 Coyote Hill Road; Palo Alto, CA USA 94304
Fax: (415) 494-4333

matoh@sssab.se (Mats Ohrman) (07/26/90)

masinter@parc.xerox.com (Larry Masinter) writes:
>Ken Yap (at U of Rochester at the time, now in Oz) wrote a pict output
>filter for pbmplus. See the Frequently Asked Questions in
>comp.graphics for more details on how to get pbmplus. He mailed it to
>me and, presumably, to Jef, but it hasn't shown up in the "official"
>pbmplus yet.

>#ifdef SYSV
>#include <string.h>
>#else /*SYSV*/
>#include <strings.h>
>#endif /*SYSV*/

>	bzero((char *)colortable, sizeof(colortable));
        ^^^^^

Hope this gets ifdef-ed too, before it becomes "offical"...
[ Half a :-) ]

______________________________________________________________________________
       _                            :  matoh@sssab.se
/ Mats Ohrman,                      :  {mcvax,munnari,uunet}!sunic!sssab!matoh
Scandinavian System Support AB,     :  Phone:  Nat.     013-11 16 60 
Box 535, S-581 06 Linkoping, Sweden :          Int.  +46 13 11 16 60