ftp.nice.ch/pub/next/tools/printer/DeskJet.2.03.N.bs.tar.gz#/DeskJet_2.03/compress.c

This is compress.c in view mode; [Download] [Up]

/* DeskJet compression code */
/* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
   Distributed by Free Software Foundation, Inc.

This file is part of Ghostscript.

Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing.  Refer
to the Ghostscript General Public License for full details.

Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License.  A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities.  It should be in a file named COPYING.  Among other
things, the copyright notice and this notice must be preserved on all
copies.  */

/* Mode 2 Row compression routine for the HP DeskJet & LaserJet IIp. */
/* Compresses data from row up to end_row, storing the result */
/* starting at compressed.  Returns the number of bytes stored. */
/* Runs of K<=127 literal bytes are encoded as K-1 followed by */
/* the bytes; runs of 2<=K<=127 identical bytes are encoded as */
/* 257-K followed by the byte. */
/* In the worst case, the result is N+(N/127)+1 bytes long, */
/* where N is the original byte count (end_row - row). */
int
mode2compress(char *row, char *end_row, char *compressed)
{	register char *i_exam = row; /* byte being examined in the row to compress */
	char *stop_exam = end_row - 4; /* stop scanning for similar bytes here */
	register char *cptr = compressed; /* output pointer into compressed bytes */

	while ( i_exam < end_row )
	   {	/* Search ahead in the input looking for a run */
		/* of at least 4 identical bytes. */
		char *i_compr = i_exam;
		char *i_next;		/* end of run */
		char byte_value;
		while ( i_exam <= stop_exam &&
			((byte_value = *i_exam) != i_exam[1] ||
			 byte_value != i_exam[2] ||
			 byte_value != i_exam[3]) )
		  i_exam++;

		/* Find out how long the run is */
		if ( i_exam > stop_exam )	/* no run */
			i_next = i_exam = end_row;
		else
		   {	i_next = i_exam + 4;
			while ( i_next < end_row && *i_next == byte_value )
				i_next++;
		   }

		/* Now [i_compr..i_exam) should be encoded as dissimilar, */
		/* and [i_exam..i_next) should be encoded as similar. */
		/* Note that either of these ranges may be empty. */

		while ( i_compr < i_exam )
		   {	/* Encode up to 127 dissimilar bytes */
			int count = i_exam - i_compr;
			if ( count > 127 ) count = 127;
			*cptr++ = count - 1;
			while ( count > 0 )
			   {	*cptr++ = *i_compr++;
				count--;
			   }
		   }

		while ( i_exam < i_next )
		   {	/* Encode up to 127 similar bytes */
			int count = i_next - i_exam;
			if ( count > 127 ) count = 127;
			*cptr++ = 1 - count;
			*cptr++ = byte_value;
			i_exam += count;
		   }
	   }
	return (cptr - compressed);
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.