home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11.lha / ccs-lib / lib / rast_w.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-08  |  4.4 KB  |  174 lines

  1. /*    rast_w . c
  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    12/1/1991
  33. */
  34.  
  35. #include "header.def"
  36. #include "imagedef.h"
  37.  
  38. #define    cols    img->width
  39. #define    rows    img->height
  40.  
  41. colormap_t*
  42. create_pr_colormap()
  43. {
  44. register int    l = MaxColors;
  45. colormap_t*    pr_cmap = (colormap_t*)ZALLOC(1, sizeof(colormap_t), "pr-map");
  46.     pr_cmap->type = RMT_EQUAL_RGB;
  47.     pr_cmap->length = l;
  48.     pr_cmap->map[0] = (byte*)ZALLOC(l, 3, "0");
  49.     pr_cmap->map[1] = pr_cmap->map[0] + l;
  50.     pr_cmap->map[2] = pr_cmap->map[1] + l;
  51. return    pr_cmap;
  52. }
  53.  
  54. void
  55. free_pr_colormap(colormap_t* pr_cmap)
  56. {
  57. if (pr_cmap)
  58.     free(pr_cmap->map[0]),
  59.     free(pr_cmap);
  60. }
  61.  
  62. colormap_t*
  63. set_pr_colormap(cmap_t*    rg_map[3], register int    colors)
  64. {
  65. colormap_t* pr_cmap = create_pr_colormap();
  66.  
  67.     memcpy(pr_cmap->map[0], rg_map[0], colors);
  68.     memcpy(pr_cmap->map[1], rg_map[1], colors);
  69.     memcpy(pr_cmap->map[2], rg_map[2], colors);
  70. return    pr_cmap;
  71. }
  72.  
  73. colormap_t*
  74. set_gs_colormap()
  75. {
  76. colormap_t* pr_cmap = create_pr_colormap();
  77. register int    i;
  78.     for (i=0; i < MaxColors; ++i)
  79.     pr_cmap->map[0][i] = pr_cmap->map[1][i] =
  80.     pr_cmap->map[2][i] = i;
  81. return    pr_cmap;
  82. }
  83.  
  84. write_rast(U_IMAGE *img, bool alpha, int pr_t)
  85. {
  86. int    depth=8, linesize, pr_type=RT_BYTE_ENCODED, row, colw=cols;
  87. colormap_t    *pr_cmap=NULL;
  88. struct pixrect    *pr;
  89. byte*    data;
  90. register byte    *inbp, *obp;
  91.  
  92.     switch (img->color_form) {
  93.     case CFM_ALPHA:
  94.     case CFM_ILL:
  95.     case CFM_ILC:
  96.         depth = alpha ? 32 : 24;
  97.         pr_type = RT_STANDARD;
  98.         break;
  99.     case CFM_SCF:
  100.         if (!reg_cmap[0] && rle_dflt_hdr.ncmap)
  101.             rlemap_to_regmap(reg_cmap, &rle_dflt_hdr),
  102.             img->cmaplen = 1 << rle_dflt_hdr.cmaplen;
  103.         pr_cmap = set_pr_colormap(reg_cmap, 1<<min_bits(img->cmaplen-1));
  104.         break;
  105.     case CFM_SGF:
  106. #ifdef    PIXRECT_NEED_CMAP
  107.         pr_cmap = set_gs_colormap();    /* for Sun pixrect lib    */
  108. #endif
  109.         break;
  110.     case CFM_BITMAP:
  111.             depth = 1;
  112.         if (img->in_color == CFM_BITMAP)
  113.             colw = (colw + 7) >> 3;
  114.         break;
  115.     default:prgmerr(-1, "format error %d", img->color_form);
  116.     }
  117.  
  118.     if (pr_t)    pr_type = pr_t;
  119.  
  120.     message("create %d-bit Sun-Raster image\n", depth);
  121.  
  122.     if (!(pr=mem_create(cols, rows, depth)))
  123.         return    prgmerr(0, "create new pixrect");
  124.     data = pr->pr_data->md_image;
  125.     linesize = pr->pr_data->md_linebytes;
  126.     if (colw < cols)    depth = 8;    /* transfer by coping    */
  127.  
  128.     for (row=0, inbp=(byte*)img->src; row < rows; ++row) {
  129.     register int    col=colw;
  130.     obp = data;
  131.     switch (depth) {
  132.     case 1: {
  133.     register int    bitcount = 7;
  134.         for (*obp=col=0; col < cols; col++) {
  135.         if (! inbp[col])
  136.             *obp |= 1 << bitcount;
  137.         if (--bitcount < 0) {
  138.             *++obp = 0;
  139.             bitcount = 7;
  140.         }
  141.         }
  142.     }    break;
  143.     case 8:
  144.         memcpy(obp, inbp, col);    break;
  145.     case 24:
  146.     case 32:
  147.         if (img->color_form==CFM_ILC)
  148.             ilc_transfer(obp, inbp, colw, 3, Yes, depth>>3),
  149.             inbp += colw << 1;
  150.     else    {    /* ILL    */
  151.     register byte    *g=inbp+colw, *b=g+colw;
  152.         while (col--) {
  153.             if (alpha)
  154.                 *obp++ = (*b | *g | *inbp) ? 255 : 0;
  155.             *obp++ = *b++,
  156.             *obp++ = *g++,
  157.             *obp++ = *inbp++;
  158.         }
  159.         inbp = g;
  160.     }    break;
  161.     default:    return    prgmerr(-'n', "not implemented");
  162.     }
  163.  
  164.     data += linesize;
  165.     inbp += colw;
  166.     }    /* end of for    */
  167.  
  168.     depth = pr_dump(pr, img->OUT_FP, pr_cmap, pr_type, 0);
  169.     free_pr_colormap(pr_cmap);
  170.     if (depth==PIX_ERR)
  171.         return    prgmerr(0, "RAS writing error %d", depth);
  172. return    depth;
  173. }
  174.