home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TIFF / DVTIF1.ZIP / UNPACKBI.C < prev   
Encoding:
C/C++ Source or Header  |  1986-12-12  |  2.6 KB  |  105 lines

  1. /*****************************************************************************
  2.  * winmacp.c -- macpaint subroutine(s?) -- in particular, unpackbits()
  3.  *
  4.  * Copyright (C) 1986 Aldus Corporation.  All rights reserved.
  5.  *
  6.  *      $Revision:   1.3  $
  7.  *      $Author:   sec  $
  8.  *      $Date:   12 Dec 1986 11:50:38  $
  9.  *
  10.  *      $Log:   I:/pm2/wgr/vcs/winmacp.cv  $
  11.  * 
  12.  *    Rev 1.3   12 Dec 1986 11:50:38   sec
  13.  * don't quit at > 127-byte lines
  14.  * 
  15.  *    Rev 1.2   09 Dec 1986 13:11:48   sec
  16.  * I think I just added some checks.
  17.  * 
  18.  *    Rev 1.1   12 Nov 1986  9:43:30   sec
  19.  * made it safer, and changed the name to aUnpackBits
  20.  * 
  21.  *    Rev 1.0   10 Nov 1986 20:06:30   sec
  22.  * Initial revision.
  23.  */
  24.  
  25. #include "AldTypes.h"
  26. #include "PageMake.h"        /* pagemaker types and global data */
  27. #include "AldUtils.h"
  28. #define IMPDEFS
  29. #include "errdefs.h"
  30.  
  31. RC aUnpackBits ARGS((LPSTR *, LPSTR *, short));
  32. RC aUnpackBits (plpSrc, plpDst, dstBytes)
  33. LPSTR    *plpSrc;
  34. LPSTR    *plpDst;
  35. short    dstBytes;
  36. {
  37.         register LPSTR        lpSrc = *plpSrc;
  38.         register LPSTR        lpDst = *plpDst;
  39.         register char        cc;
  40.         register short        count;
  41.         register short        outsofar = 0;
  42.         RC                    err = SUCCESS;
  43.  
  44. #if 0
  45.         /* DEBUGGING check only:
  46.          */
  47.         if (dstBytes <= 0) {
  48.             DBMSG(("UnpackBits: dstBytes=%d\n",dstBytes));
  49.             err = IE_BUG;
  50.             goto cu0;
  51.         }
  52. #endif
  53.  
  54.         while (outsofar < dstBytes) {
  55.  
  56.             cc = *(char FAR *)lpSrc++;
  57.  
  58.             /* if -127 <= BYTE <= -1, replicate the next byte -n+1 times
  59.              */
  60.             if (cc & '\200') {
  61.                 count = -(short)cc + 1;    /* relies on sign-extension!!! */
  62.                 if (count <= 0 || count > 127) {
  63.                     DBMSG(("UnpackBits: bad replicate count\n"));
  64.                     DBMSG((" cc = %x  count=%d\n", (unsigned)(cc & 0xff),
  65.                      count));
  66.                     err = IE_BUG;
  67.                     goto cu0;
  68.                 }
  69.                 outsofar += count;
  70.                 if (outsofar > dstBytes) {
  71.                     DBMSG(("UnpackBits: overflow\n"));
  72.                     err = IE_DATA_OVERRUN;
  73.                     goto cu0;
  74.                 }
  75.                 lmemset (lpDst, (BYTE)*lpSrc, (WORD)count);
  76.                 lpSrc++;
  77.             }
  78.  
  79.             /* else if 0 <= BYTE <= 127, copy the next n+1 bytes literally
  80.              */
  81.             else {
  82.                 count = (short)cc + 1;
  83.                 if (count <= 0 || count > 127) {
  84.                     DBMSG(("UnpackBits: bad literal count\n"));
  85.                     DBMSG((" cc = %x  count=%d\n", (unsigned)(cc & 0xff),
  86.                      count));
  87.                     err = IE_BUG;
  88.                     goto cu0;
  89.                 }
  90.                 outsofar += count;
  91.                 if (outsofar > dstBytes) {
  92.                     DBMSG(("UnpackBits: overflow\n"));
  93.                     err = IE_DATA_OVERRUN;
  94.                     goto cu0;
  95.                 }
  96.                 lmemcpy (lpDst, lpSrc, (WORD)count);
  97.                 lpSrc += count;
  98.             }
  99.             lpDst += count;
  100.         }
  101.         *plpSrc = lpSrc;
  102.         *plpDst = lpDst;
  103. cu0:    return err;
  104. }
  105.