/* encodings.c * */ #include #include FILE *fpout = stdout; AppendHeader(fp, string) FILE *fp; char *string; { fpout = fp; while (*string != (char) 0) append_byte(*string++); } AppendIdentifier(string) char *string; { append_sequence_descriptor(sequenceIdentifier, strlen(string)); while (*string != (char) 0) append_byte(*string++); } AppendString(string) char *string; { append_sequence_descriptor(sequenceString, strlen(string)); while (*string != (char) 0) append_byte(*string++); } AppendComment(string) char *string; { append_sequence_descriptor(sequenceComment, strlen(string)); while (*string != (char) 0) append_byte(*string++); } AppendOp(opcode) int opcode; { if (opcode > SHORT_OP_LIMIT) { /* has to be coded as a long op */ append_int((LONG_OP << 8) | opcode, 2); } else { /* small enough to be a short op */ append_byte(SHORT_OP | opcode); } } AppendInteger(number) int number; { if (number < INTEGER_MIN || number > INTEGER_MAX) { append_sequence_descriptor(sequenceInteger, bytes_in_int(number)); append_int(number, bytes_in_int(number)); } else { append_int(number+INTEGER_ZERO, 2); } } AppendRational(value, divisor) int value, divisor; { int len_value, len_divisor, len; len_value = bytes_in_int(value); len_divisor = bytes_in_int(divisor); len = len_value > len_divisor ? len_value : len_divisor; append_sequence_descriptor(sequenceRational, 2*len); append_int(value, len); append_int(divisor, len); } AppendPPV(length, bitsperPixel, scanlength) int length, bitsperPixel, scanlength; { append_sequence_descriptor(sequencePPV, length); append_int(bitsperPixel, 2); append_int(scanlength, 2); } AppendCPV(length, breaktable, nrange, scanlength) int length, breaktable, nrange, scanlength; { append_sequence_descriptor(sequenceCPV, length); append_int(breaktable, 2); append_int(nrange, 2); append_int(scanlength, 2); } /* * The remainder of this file contains lower level primitives: */ #define err0 "encoding: sequence type is out-of-range.\n" #define err1 "encoding: sequence length is out-of-range.\n" append_sequence_descriptor(type, length) int type; int length; { if (type < 0 || type > 31) error(err0); if (length < 0 || length > LONG_SEQUENCE_LIMIT) error(err1); if (length > SHORT_SEQUENCE_LIMIT) { /* too big to fit in a short sequence */ append_byte(LONG_SEQUENCE | type); append_int(length, 3); } else { append_byte(SHORT_SEQUENCE | type); append_byte(length); } } append_int(d, n) int d; int n; { switch (n) { case 4: append_byte((d >> 24) & 0xff); case 3: append_byte((d >> 16) & 0xff); case 2: append_byte((d >> 8) & 0xff); case 1: append_byte(d & 0xff); } } bytes_in_int(value) int value; { /* this routine assumes 4 bytes in an int and two's complement notation */ int mask, i; if (value < 0) { /* takes the same space as its one's complemented value */ value = ~value; } if (value == 0) { /* avoids infinite looping */ return(1); } for (i = 4, mask = 0xff800000; (value & mask) == 0; i--, mask >>= 8) ; return(i); } append_byte(value) int value; { unsigned char v; v = value & 0377; putc(v, fpout); } /* Change Log * * William LeFebvre, 24-May-1984 12:15:19, Created Interpress library version. * K. Knox, 17-Dec-84 16:44:03, Greatly modified for use with RES files. * K. Knox, 1-Oct-85 14:34:56, Add AppendCPV function. * K. Knox, 2-Oct-85 14:12:04, Changed from file descriptor to file stream. * * */