home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11.lha / ccs-lib / libsun / rast_dump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-18  |  3.7 KB  |  138 lines

  1. /* rast_dump.c - write a Sun rasterfile
  2. %
  3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4.  
  5. This software is copyright (C) by the Lawrence Berkeley Laboratory.
  6. Permission is granted to reproduce this software for non-commercial
  7. purposes provided that this notice is left intact.
  8.  
  9. It is acknowledged that the U.S. Government has rights to this software
  10. under Contract DE-AC03-765F00098 between the U.S.  Department of Energy
  11. and the University of California.
  12.  
  13. This software is provided as a professional and academic contribution
  14. for joint exchange. Thus, it is experimental, and is provided ``as is'',
  15. with no warranties of any kind whatsoever, no support, no promise of
  16. updates, or printed documentation. By using this software, you
  17. acknowledge that the Lawrence Berkeley Laboratory and Regents of the
  18. University of California shall have no liability with respect to the
  19. infringement of other copyrights by any part of this software.
  20.  
  21. For further information about this notice, contact William Johnston,
  22. Bld. 50B, Rm. 2239, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  23. (wejohnston@lbl.gov)
  24.  
  25. For further information about this software, contact:
  26.     Jin Guojun
  27.     Bld. 50B, Rm. 2275, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  28.     g_jin@lbl.gov
  29.  
  30. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  31. %
  32. % AUTHOR:    Jin Guojun - LBL    10/1/91
  33. */
  34.  
  35. #include "imagedef.h"
  36.  
  37. #ifndef    TOP_BITMAP_VALUE
  38. #define    TOP_BITMAP_VALUE    255
  39. #endif
  40.  
  41. /*    colormap_t    pr_colormap;    */
  42. /*    { int type, lenght; char* map[3] }    SUN raster color map */
  43.  
  44. extern struct rasterfile    srhd;
  45. extern RastSWidth(/*struct rasterfile*    srhd*/);
  46.  
  47.  
  48. #ifndef    RAS_RLE_ECODE
  49. #define    RAS_RLE_ECODE    128
  50. #endif
  51.  
  52. rast_dump_header(struct    rasterfile *srhd, colormap_t *cmap, FILE *fp)
  53. {
  54. if (fwrite(srhd, sizeof(*srhd), 1, fp) < 1)
  55.     return    prgmerr(0, "write RAS header");
  56. if (srhd->ras_maplength && fwrite(cmap->map[0], srhd->ras_maplength, 1, fp) < 1)
  57.     return    prgmerr(0, "write RAS cmap");
  58. return    0;
  59. }
  60.  
  61. pr_dump(struct pixrect *prt, FILE *fp, colormap_t *cmap, int type, int flag)
  62. {
  63. double    junk;
  64. byte    *buf = prt->pr_data->md_image;
  65. register int    d, i, pad;
  66.  
  67. srhd.ras_magic = RAS_MAGIC;
  68. srhd.ras_type = type;
  69. if (cmap)    {
  70.     srhd.ras_maptype = cmap->type;
  71.     srhd.ras_maplength = cmap->length;
  72. } else    srhd.ras_maptype = srhd.ras_maplength = 0;
  73. srhd.ras_width = prt->pr_size.x;
  74. srhd.ras_height= prt->pr_size.y;
  75. srhd.ras_depth = prt->pr_depth;
  76. i = srhd.ras_width*srhd.ras_height;
  77. d = srhd.ras_depth>>3;
  78. if (! d)    d++;
  79. srhd.ras_length = i * d;
  80.  
  81. if (type != RT_BYTE_ENCODED)    {
  82.     if ((pad=rast_dump_header(&srhd, cmap, fp)) < 0)
  83.         return    pad;
  84. }
  85.  
  86. pad = RAST_ODD_PAD(srhd.ras_width);
  87.  
  88. if (type==RT_BYTE_ENCODED) {
  89. int    pad_len = srhd.ras_width * d;
  90. byte*    encode = NZALLOC(i, d, "ras_enc"), *dp = buf;
  91. register int    c, w;
  92. buf = encode;
  93.     for (w=pad_len, i*=d; i-- > 0;)    {
  94.     d = *dp++;    w--;
  95.     for (c=0; d == *dp; dp++, i--, w--)    c++;
  96. #ifdef    RAS_SLOW_DECODE
  97.     if (c)    {
  98.         *buf++ = RAS_RLE_ECODE;
  99.         *buf++ = c;
  100.     }
  101. #else
  102.     if (c)
  103.         if (c > 2 || d==RAS_RLE_ECODE)    {
  104.         *buf++ = RAS_RLE_ECODE;
  105.         *buf++ = c;
  106.         } else    {
  107.         register int    j = c;
  108.         while (j--)    *buf++ = d;
  109.         }
  110. #endif
  111.     *buf++ = d;
  112.     if (d==RAS_RLE_ECODE && !c)
  113.         *buf++ = c;
  114.     if (pad && --w<=0)
  115.         *buf++ = c,    w = pad_len;
  116.     }
  117.     srhd.ras_length = buf - encode;
  118.     d = rast_dump_header(&srhd, cmap, fp) < 0 ||
  119.     fwrite(encode, srhd.ras_length, 1, fp) < 1;
  120.     free(encode);
  121.     i = d ? -1 : srhd.ras_length;
  122. }
  123. else if (srhd.ras_width & d & 1)    {
  124.     for (d *= RastSWidth(&srhd), i=srhd.ras_height; i--; buf+=d) {
  125.         if (fwrite(buf, 1, d, fp) != d)
  126.             return    prgmerr(0, "write rastline %d", i);
  127.         if (pad)
  128.             fwrite(&junk, 1, pad, fp);
  129.     }
  130.     i = srhd.ras_width*srhd.ras_height;
  131. } else    {    d = fwrite(buf, d, i, fp);
  132.         if (d != i)
  133.             return    prgmerr(0, "read rastfile %d", d);
  134. }
  135. return    i;
  136. }
  137.  
  138.