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

  1. /* rast_r.c - read a Sun rasterfile and produce raw data
  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 "header.def"
  36. #include "imagedef.h"
  37.  
  38. #ifndef    TOP_BITMAP_VALUE
  39. #define    TOP_BITMAP_VALUE    255
  40. #endif
  41.  
  42. /*    colormap_t    pr_colormap;    */
  43. /*    { int type, lenght; char* map[3] }    SUN raster color map */
  44.  
  45. struct rasterfile    srhd;
  46. static    bool    grayscale;
  47.  
  48. RastSWidth(srhd)
  49. struct rasterfile*    srhd;
  50. {
  51. register int    w = srhd->ras_width;
  52.     if (srhd->ras_depth == 1)    w = w+7 >> 3;
  53. return    w;
  54. }
  55.  
  56. rast_header_handle(job, img, ac, av, assist)
  57. U_IMAGE    *img;
  58. int    ac;
  59. char    **av;
  60. VType    *assist;
  61. {
  62.     switch (job) {
  63.     case HEADER_READ:
  64.     case HEADER_FREAD:
  65.     if (!av)
  66.         fseek(img->IN_FP, 0, 0);
  67.     if ((*img->read)(&srhd, sizeof(srhd), 1, img->IN_FP) < 1 ||
  68.         srhd.ras_magic != RAS_MAGIC) {
  69.         if (DEBUGANY)    prgmerr(0, "not a raster header");
  70.         return    EOF;
  71.     }
  72.     if (srhd.ras_width < 1 || srhd.ras_height < 1)
  73.         return    prgmerr(0, "invalid cols: %d, or rows: %d",
  74.             srhd.ras_width, srhd.ras_height);
  75.     grayscale = 1;
  76.     if (srhd.ras_maplength) {    /* If there are colormaps, read them. */
  77.     register int    i, j=srhd.ras_maplength / 3;
  78.         if (!read_ras_cmap(img, j, 3))
  79.             return    prgmerr(0, "unable to read colormap data");
  80.         for (i=0; i < j && grayscale; i++)
  81.             grayscale = (reg_cmap[0][i]==reg_cmap[1][i] &&
  82.             reg_cmap[1][i]==reg_cmap[2][i]);
  83.         img->mono_img = (grayscale &= srhd.ras_depth < 24);
  84.     }
  85.     /*========================================================
  86.     byte    red[CMAPSIZE],    // red colormap entries
  87.         green[CMAPSIZE],// Green ditto
  88.         blue[CMAPSIZE];    // see a pattern?
  89.     switch(rashdr.ras_maptype) {
  90.     case RMT_RAW:    RAW_COLOR_MAP
  91.         for (i=0; i < shdr.ras_maplength; i++)
  92.             getc(rasfile);    // true color ?
  93.     case RMT_NONE:
  94.         for (i=0; i < 256; i++)
  95.             red[i] = green[i] = blue[i] = i;
  96.         break;
  97.     case RMT_EQUAL_RGB:    RGB_COLOR_MAP
  98.         for (i=0; i < srhd.ras_maplength/3; i++)
  99.             red[i] = getc(rasfile);
  100.         fread(green, i, 1, rastfile);
  101.         fread(blue, i, 1, rastfile);
  102.         break;
  103.     default:
  104.     prgmerr(EX_DATAERR, "Unknown color map type (%d)", rashdr.ras_maptype);
  105.     }
  106.     =============================================================*/
  107.  
  108.     img->width = srhd.ras_width;
  109.     img->height = srhd.ras_height;
  110.     img->pxl_in = 1;
  111.     img->in_type = RAS;
  112.     img->in_form = IFMT_SGF;
  113.     img->channels = 3;
  114.     switch(srhd.ras_depth)    {
  115.     case 1:    img->in_color = CFM_BITMAP;
  116.         img->in_form = IFMT_BITMAP;
  117.         img->binary_img = 1;
  118.         goto    next18;
  119.     case 8:    if (grayscale)
  120.             img->in_color = CFM_SGF;
  121.         else    img->in_color = CFM_SCF,
  122.             img->in_form = IFMT_SCF;
  123. next18:        if (grayscale || !img->color_dpy || assist)    {
  124.         register int    i = srhd.ras_maplength;    img->cmaplen = i/3;
  125.             if (assist && i)
  126.             img->color_form = img->in_color;
  127.             img->channels = img->mono_img = 1;
  128.         }
  129.         break;
  130.     case 24:
  131.         img->in_color = CFM_ILC;
  132.         img->in_form = IFMT_ILC;
  133.         break;
  134.     case 32:img->in_color = CFM_ALPHA;
  135.         img->in_form = IFMT_ALPHA;
  136.     break;
  137.     default:
  138.     return    prgmerr(0, "invalid depth: %d; Only handle 1, 8, 24, or 32.",
  139.             srhd.ras_depth);
  140.     }
  141.     /* loading depends on depth, so set up dpy_channels for conversion */
  142.     break;
  143.     case HEADER_TO:
  144.     srhd.ras_width = img->width;
  145.     srhd.ras_height = img->height;
  146.     if ((srhd.ras_depth = img->channels << 3) == 8)
  147.         srhd.ras_maplength = img->cmaplen * img->channels,
  148.         srhd.ras_maptype = RMT_EQUAL_RGB,
  149.         srhd.ras_type = RT_BYTE_ENCODED;
  150.     else    srhd.ras_type = RT_FORMAT_RGB;
  151.     break;
  152.     case HEADER_WRITE:
  153.     default:    return    prgmerr(0, "no such a job %d\n", job);
  154.     }
  155. return    0;
  156. }
  157.  
  158.  
  159. #ifndef    RAS_RLE_ECODE
  160. #define    RAS_RLE_ECODE    128
  161. #endif
  162.  
  163. load_rastfile(buf, srhd, len, fp)
  164. byte    *buf;
  165. struct rasterfile*    srhd;
  166. register int    len;
  167. FILE    *fp;
  168. {
  169. double    junk;
  170. register int    p = srhd->ras_depth>>3,
  171.     pad = RAST_ODD_PAD(srhd->ras_width),
  172.     i = srhd->ras_width*srhd->ras_height;
  173. if (!len)    len = i;
  174. if (! p)    p++;
  175. if (srhd->ras_type==RT_BYTE_ENCODED) {
  176. int    pad_len = srhd->ras_width * p;
  177. register int    c, w;
  178.     for (w=pad_len, i*=p; i-- > 0 && len--;)    {
  179.     p = getc(fp);
  180.     if (p==RAS_RLE_ECODE) {
  181.         c = getc(fp);    len--;
  182.         if (c)    {
  183.         p = getc(fp);    len--;
  184.         while (c--)
  185.             *buf++ = p, i--, w--;
  186.         }
  187.     }
  188.     *buf++ = p;
  189.     if (pad && --w<=0)
  190.         c = getc(fp),    w = pad_len;
  191.     }
  192. }
  193. else if (srhd->ras_width & p & 1)
  194.     for (p *= RastSWidth(srhd), len=srhd->ras_height; len--; buf+=p) {
  195.         if (fread(buf, 1, p, fp) != p)
  196.             return    prgmerr(0, "load rastline %d", len);
  197.         fread(&junk, 1, pad, fp);
  198.     }
  199.     else {    p = fread(buf, 1, len, fp);
  200.         if (p != len)
  201.             return    prgmerr(0, "read rastfile %d", p);
  202.     }
  203. return    srhd->ras_width * srhd->ras_height;
  204. }
  205.  
  206.  
  207. #ifndef    V_ASSIGN
  208. #define    V_ASSIGN(p, r,g,b)    (p) = ((MType)(r)<<20) | ((MType)(g)<<10) | (MType)(b)
  209. #endif
  210.  
  211. read_rast_image(img, srhd, OsameI8)
  212. U_IMAGE    *img;
  213. struct rasterfile*    srhd;
  214. {
  215. int    i, depth=srhd->ras_depth, row, rows, cols, mask, maxval=255,
  216.     zero, one=0, swap=srhd->ras_type!=RT_FORMAT_RGB && depth>8;
  217. register int    col = cols = srhd->ras_width;
  218.  
  219. row = rows = srhd->ras_height;
  220.  
  221.     /* check buf size */
  222. verify_buffer_size(&img->src, row*col, img->dpy_channels, "ras-src");
  223. switch (depth) {    /* Check the depth and color map */
  224. case 1:
  225.     if (srhd->ras_maptype==RMT_NONE && !srhd->ras_maplength) {
  226.         maxval = 1;
  227.         zero = TOP_BITMAP_VALUE;
  228.     }
  229.     else if (srhd->ras_maptype == RMT_EQUAL_RGB &&
  230.         srhd->ras_maplength == 6)
  231.         if (grayscale)
  232.         zero = reg_cmap[0][0],
  233.         one = reg_cmap[0][1];
  234.         else
  235.         V_ASSIGN(zero, reg_cmap[0][0], reg_cmap[1][0], reg_cmap[2][0]),
  236.         V_ASSIGN(one, reg_cmap[0][1], reg_cmap[1][1], reg_cmap[2][1]);
  237.     else    goto    cmaperr;
  238.     break;
  239. case 8:
  240.     if (grayscale || srhd->ras_maptype==RMT_EQUAL_RGB)    break;
  241. cmaperr:
  242.     return    prgmerr(0,
  243.         "depth-%d has a non-standard colormap-type %d length %d",
  244.         depth, srhd->ras_maptype, srhd->ras_maplength);
  245. case 24:
  246. case 32:
  247.     if (srhd->ras_maptype != RMT_NONE || srhd->ras_maplength)
  248.         prgmerr(0, "strange: depth-%d rasterfile with colormaps", depth);
  249.     break;
  250.     default:return    prgmerr(0,
  251.         "invalid depth: %d.  Can only handle 1, 8, 24, or 32",    depth);
  252. }
  253.  
  254. /*    converting    */
  255. #define    Next_Sample(mask, ibp)    \
  256.     if (!(mask>>=1))    {    mask = 0x80;    ibp++;    }
  257.  
  258.     if (depth==8 && (OsameI8 || !srhd->ras_maplength || img->mid_type==COLOR_PS))
  259.     return    load_rastfile(img->src, srhd, srhd->ras_length, img->IN_FP);
  260.  
  261.     else {
  262.     byte*    ibuf = nzalloc(img->width*img->height, depth==1 ? 1 : depth>>3,
  263.                 "ras-ibuf");
  264.     register byte    *ibp = ibuf, *obp = (byte*)img->src;
  265.  
  266.     if ((i=load_rastfile(ibuf, srhd, srhd->ras_length, img->IN_FP)) < 0)
  267.         return    i;
  268.     if (!swap && img->in_color == img->color_form)    {
  269.         img->src = ibp;    ibuf = obp;
  270.     }
  271.     else for (row=0; row < rows; ++row) {
  272.     switch (depth) {
  273.     case 1:
  274.         mask = 0x80;
  275.         for (col=0; col < cols; col++)    {
  276.             *obp++ = (*ibp & mask) ? one : zero;
  277.             Next_Sample(mask, ibp);
  278.         }
  279.         if (mask & 0x7F)    ibp++;
  280.         break;
  281.     case 8:
  282.         if (grayscale)
  283.             for (col=0; col < cols; col++)
  284.             *obp++ = reg_cmap[0][*ibp++];
  285.         else{
  286.             if (img->color_form != CFM_SGF)
  287.             ras8_to_rle(obp, ibp, col, img, reg_cmap, 1);
  288.             else
  289.             map8_gray(obp, ibp, col, reg_cmap);
  290.             ibp += col;
  291.             obp += col * img->dpy_channels;
  292.         }
  293.         break;
  294.     case 24:
  295.     case 32:
  296.         if (img->color_form==CFM_ILC)    /* BGR -> RGB */
  297.             ilc_transfer(obp, ibp, cols, depth>>3, swap, 3);
  298.         else if (img->color_form!=CFM_SGF || img->mid_type==COLOR_PS)
  299.             any_ilc_to_rle(obp, ibp, img, depth>>3, swap);
  300.         else
  301.             ilc_to_gray(obp, ibp, col, depth==32,
  302.                 srhd->ras_maplength ? reg_cmap : NULL, swap);
  303.         ibp += col * (depth>>3);
  304.         obp += col * img->dpy_channels;
  305.         break;
  306.     default:free(ibuf);
  307.         return    prgmerr(0, "cannot happen");
  308.     }
  309.     }    /* end for */
  310.     free(ibuf);
  311.     }
  312. if (!img->color_dpy || img->dpy_depth == 1)
  313.     img->channels = 1;
  314. return    cols * row;
  315. }
  316.