/* File : RLC.C Description: Convert BitMap to INTEL RLC Author : Peter H. Michalicka Environment: HP-UX 8.05 */ #include #include #include "tiff.h" #include "vma.h" WORD ic; /* actual RLC column */ WORD rn; /* actual RUN count */ WORD rb[32767]; /* RUN buffer */ int ii; /* rb[ii] */ int is_start; /* Start of RUN encounterd */ BYTE cb; /* external vars */ extern char *msg_fwrite; /* external functions */ #ifdef ANSI int error(char *s1, char *s2); #endif static int iblkofs, blkofs, nblk, iline; static char *ptr; /* Code */ void rlc_init(v) vma *v; { v->LineNo = 1; v->mbd[0].blkno = v->nblocks = nblk = blkofs = 0; ptr = v->bl[0]; /* set defaults */ ic = RLC_START; rb[0] = rn = 0; ii = 1; is_start = FALSE; } int put_buf(v, n, wp) vma *v; int n; WORD *wp; { n <<= 1; if ((blkofs + n) >= v->blocksize) { /* check if fit to block */ if (fwrite(ptr, v->blocksize, 1, v->fp) != 1) return error(msg_fwrite, v->fn); v->nblocks++; if (++nblk >= v->rblocks) /* check resident blocks */ nblk = 0; v->mbd[nblk].blkno = v->nblocks; ptr = v->bl[nblk]; blkofs = 0; } iblkofs = blkofs; memcpy(ptr + blkofs, wp, n); blkofs += n; v->ofs[v->LineNo].blkno = (WORD)v->nblocks; v->ofs[v->LineNo].offset = (WORD)iblkofs; v->LineNo++; return TRUE; } int end_vma(v) vma *v; { if (blkofs) return fwrite(ptr, v->blocksize, 1, v->fp); /* write last block */ return TRUE; } int end_rlc(v) /* set black runend to white */ vma *v; { int i, n, jp; WORD re; if (is_start) rb[ii++] = ic; if (rn) { rb[0] = rn; /* RUN count */ ii--; /* last inserted run */ jp = rn << 1; /* RUN = (sw,ew) */ jp++; /* 1st entry = RUN count */ } else jp = 1; for (i = rn, n = 2; i ; i--) { rb[n]++; /* set black end to white end */ n += 2; } if (! put_buf(v, jp, rb)) return 0; ic = RLC_START; rb[0] = rn = 0; ii = 1; is_start = FALSE; return 1; } int end2rlc(v) /* dont correct white end */ vma *v; { int i, n, jp; WORD re; if (is_start) rb[ii++] = ic; if (rn) { rb[0] = rn; /* RUN count */ ii--; /* last inserted run */ jp = rn << 1; /* RUN = (sw,ew) */ jp++; /* 1st entry = RUN count */ } else jp = 1; if (! put_buf(v, jp, rb)) return 0; ic = RLC_START; rb[0] = rn = 0; ii = 1; is_start = FALSE; return 1; } void to_rlc() /* process BYTE cb */ { WORD jp; /* BitsPos of BYTE cb */ if (cb) { if (! is_start) { if (cb == 0xFF) { rn++; /* RUN count */ is_start = TRUE; rb[ii++] = ic; } else { /* cb != 0xFF */ jp = 0; /* Bit 0 */ while (jp < 8) { if ((cb & 0x80) != 0x00) { /* Bit reached */ rn++; /* RUN count */ is_start = TRUE; rb[ii++] = ic + jp; while (++jp < 8) { cb <<= 1; if ((cb & 0x80) == 0x00) { /* RUN ends */ rb[ii++] = ic + jp; is_start = FALSE; break; /* inner while */ } } /* wend */ } cb <<= 1; jp++; } } } else if (cb != 0xFF) { /* RUN continues */ jp = 0; /* Bit 0 */ while (jp < 8) { if ((cb & 0x80) == 0x00) { /* empty Bit */ rb[ii++] = ic + jp; is_start = FALSE; while (++jp < 8) { cb <<= 1; if ((cb & 0x80) != 0x00) { /* Bit reached */ rn++; /* RUN count */ is_start = TRUE; rb[ii++] = ic + jp; break; /* inner while */ } } /* wend */ } cb <<= 1; jp++; } /* wend */ } /* else cb == 0xFF && RUN continues*/ } else if (is_start) { /* cb == '0x00' */ rb[ii++] = ic; is_start = FALSE; } ic += 8; }