home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Graphics / Graphics.zip / jpeg2ps2.zip / asc85ec.c next >
C/C++ Source or Header  |  1997-01-23  |  3KB  |  113 lines

  1. /* ASCII85 and Hex encoding for PostScript Level 2 and PDF */
  2. /* (C) Thomas Merz 1994-96 */
  3.  
  4. #include <stdio.h>
  5. #include <fcntl.h>
  6.  
  7. #ifdef DOS
  8. #include <io.h>
  9. #include <stdlib.h>
  10. #endif
  11.  
  12. #include "psimage.h"
  13.  
  14. typedef unsigned char byte;
  15.  
  16. static unsigned char buf[4];
  17. static unsigned long power85[5] = { 1L, 85L, 85L*85, 85L*85*85, 85L*85*85*85};
  18.  
  19. /* read 0-4 Bytes. result: number of bytes read */
  20. static int 
  21. ReadSomeBytes P1(FILE *, in)
  22. {
  23.   register int count, i;
  24.  
  25.   for (count = 0; count < 4; count++) {
  26.     if ((i = getc(in)) == EOF)
  27.       break;
  28.     else
  29.       buf[count] = (byte) i;
  30.   }
  31.   return count;
  32. }
  33.  
  34. static void 
  35. outbyte P2(byte, c, FILE *, out)
  36. {    /* output one byte */
  37.   static int outbytes = 0;
  38.  
  39.   if (fputc(c, out) == EOF) {
  40.     fprintf(stderr, "jpeg2ps: write error - exit!\n");
  41.     exit(1);
  42.   }
  43.   if (++outbytes > 63) {      /* insert line feed */
  44.     fputc('\n', out);
  45.     outbytes = 0;
  46.   }
  47. }
  48.  
  49. int
  50. ASCII85Encode P2(FILE *, in, FILE *, out)
  51. {
  52.   register int i, count;
  53.   unsigned long word, v;
  54.  
  55.   /* 4 bytes read ==> output 5 bytes */
  56.   while ((count = ReadSomeBytes(in)) == 4) {
  57.     word = ((unsigned long)(((unsigned int)buf[0] << 8) + buf[1]) << 16) +
  58.                    (((unsigned int)buf[2] << 8) + buf[3]);
  59.     if (word == 0)
  60.       outbyte('z', out);       /* shortcut for 0 */
  61.     else
  62.       /* calculate 5 ASCII85 bytes and output them */
  63.       for (i = 4; i >= 0; i--) {
  64.     v = word / power85[i];
  65.     outbyte((byte)v + '!', out);
  66.     word -= v * power85[i];
  67.       }
  68.   }
  69.  
  70.   word = 0;
  71.  
  72.   if (count != 0) {   /* 1-3 bytes left */
  73.     for (i = count-1; i >= 0; i--)   /* accumulate bytes */
  74.       word += (unsigned long)buf[i] << 8 * (3-i);
  75.     
  76.     /* encoding as above, but output only count+1 bytes */
  77.     for (i = 4; i >= 4-count; i--) {
  78.       v = word / power85[i];
  79.       outbyte((byte)v + '!', out);
  80.       word -= v * power85[i];
  81.     }
  82.   }
  83.  
  84.   fputc('~', out);    /* EOD marker */
  85.   fputc('>', out);
  86.   return 0;
  87. }
  88.  
  89. void 
  90. ASCIIHexEncode P2(FILE *, in, FILE *, out) {
  91.   static char buffer[512];
  92.   static char BinToHex[] = "0123456789ABCDEF";
  93.   int i, CharsPerLine;
  94.   size_t n;
  95.   unsigned char *p;
  96.  
  97.   CharsPerLine = 0;
  98.   putc('\n', out);
  99.  
  100.   while ((n = fread(buffer, 1, sizeof(buffer), in)) != 0)
  101.     for (i = 0, p = (unsigned char *) buffer; i < n; i++, p++) {
  102.       putc(BinToHex[*p>>4], out);           /* first nibble  */
  103.       putc(BinToHex[*p & 0x0F], out);       /* second nibble */
  104.       if ((CharsPerLine += 2) >= 64) {
  105.         putc('\n', out);
  106.         CharsPerLine = 0;
  107.       }
  108.     }
  109.  
  110.   putc('>', out);         /* EOD marker for PostScript hex strings */
  111. }
  112.  
  113.