home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / sun / lib2 / pnm_r.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-24  |  4.6 KB  |  163 lines

  1. /* pnm_read.c - read a portable anymap
  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    8/1/91
  33. */
  34.  
  35. #include "header.def"
  36. #include "imagedef.h"
  37.  
  38. #ifndef    TOP_BITMAP_VALUE
  39. #define    TOP_BITMAP_VALUE    240
  40. #endif
  41.  
  42. void
  43. #ifdef __STDC__
  44. pnm_readpnmrow(FILE* file, xel* linep, int cols, xelval maxval, int format)
  45. #else
  46. pnm_readpnmrow(file, linep, cols, maxval, format)
  47. FILE*    file;
  48. xel*    linep;
  49. xelval    maxval;
  50. int    cols, format;
  51. #endif
  52. {
  53.     switch (PNM_FORMAT_TYPE(format))    {
  54.     case PPM_TYPE:
  55.     ppm_readppmrow(file, (pixel*)linep, cols, (pixval) maxval, format);
  56.     break;
  57.  
  58.     case PGM_TYPE:
  59.     pgm_readpgmrow(file, (gray*)linep, cols, (gray) maxval, format);
  60.     break;
  61.  
  62.     case PBM_TYPE: {
  63.     register int    col=0;
  64.     register bit    *bP, *bitrow = pbm_allocrow(cols);
  65.         pbm_readpbmrow(file, bitrow, cols, format);
  66.         for (bP = bitrow; col < cols; ++col, ++linep, ++bP)
  67.             PNM_ASSIGN1(*linep, *bP==PBM_BLACK ? 0: maxval);
  68.         pbm_freerow(bitrow);
  69.     }    break;
  70.  
  71.     default:    prgmerr(format, "can't happen");
  72.     }
  73. }
  74.  
  75. /*    PNM type !!!
  76. pixel => structure { byte r,g,b } when PPM_MAXMAXVAL != 1023
  77. otherwise    pixel => long
  78. ifdef    PPM
  79.     xel => pixel
  80. else    xel => gray { byte }
  81. */
  82.  
  83. read_pnm_image(img, maxval, format, xels, cht, cmap)
  84. U_IMAGE    *img;
  85. xel**    xels;
  86. colorhash_table    cht;    /* a structure pointer */
  87. cmap_t    *cmap[3];
  88. {
  89. register int    row;
  90. xel*    xtmp=NULL;
  91. int    i, cols=img->width, rows=img->height, cfm=img->color_form;
  92. bool    toill = img->channels==1 && cfm==CFM_ILL;
  93.  
  94. verify_buffer_size(&img->src, cols * rows, img->dpy_channels, "pnm-src");
  95.  
  96. if (cht && !cmap[0])
  97.     rlemap_to_regmap(cmap, &rle_dflt_hdr);
  98. if (!xels | toill)
  99.     xtmp = nzalloc(cols, img->channels*sizeof(xel), "pnm-tmp");
  100.     /*
  101.     *    The variables at this point are:
  102.     *        img->channels 1, 3
  103.     *    mid_type:    HIPS, RLE, COLOR_PS (include RAS & TIFF)
  104.     */
  105.     for (row=0; row < rows; ++row) {
  106.     register int    col=0;
  107.     register byte*    obp = (byte*)img->src + row*cols*img->dpy_channels,
  108.         *ptmp = toill ? (byte*) xtmp : obp;    /* slow down <--> powerful */
  109.     register xel*    xP;
  110.     if (xels)    xP = xels[row];
  111.     else    xP = xtmp,
  112.         pnm_readpnmrow(img->IN_FP, xP, cols,
  113.             maxval>1 ? maxval : TOP_BITMAP_VALUE, format);
  114.     switch (img->channels)    {
  115.     case 1:
  116.         switch (PNM_FORMAT_TYPE(format))    {
  117.         case PPM_TYPE:
  118.             for (; col < cols; ++col, ++xP)    {
  119.             register int    color;
  120.             if (maxval != 255)
  121.                 PPM_DEPTH(*xP, *xP, maxval, 255);
  122.             color = ppm_lookupcolor(cht, xP);
  123.             if (color == -1)    return    prgmerr(0,
  124.                 "color not found ?  row=%d col=%d  r=%d g=%d b=%d",
  125.                 row, col, PPM_GETR(*xP), PPM_GETG(*xP),
  126.                 PPM_GETB(*xP) );
  127.             ptmp[col] = color;
  128.             }
  129.         break;
  130.         default:
  131. #    ifdef    PPM
  132.             for (col=cols; col--;)
  133.                 obp[col] = xP[col].b;
  134. #    else    /* PGM and PBM, xel = byte */
  135.             memcpy(obp, xP, cols);
  136. #    endif
  137.         }
  138.         if (cht && cfm==CFM_SGF)
  139.             map8_gray(obp, obp, cols, cmap);
  140.         else if (toill)
  141.             ras8_to_rle(obp, ptmp, cols, img, reg_cmap, 1);
  142.     break;
  143.     case 3:    if (img->color_form == CFM_ILL && img->dpy_channels == 3)
  144.             any_ilc_to_rle(obp, xP, img, img->channels, No);
  145.         else if (cfm == CFM_SGF)
  146.             ilc_to_gray(obp, xP, cols, NULL, NULL, True);
  147.         else    memcpy(obp, xP, cols * img->channels);
  148. /*            for (; col++ < cols; ++xP) {    same as memcpy
  149.                 *obp++ = PPM_GETR( *xP );
  150.                 *obp++ = PPM_GETG( *xP );
  151.                 *obp++ = PPM_GETB( *xP );
  152.             }
  153. */        break;
  154.  
  155.         default:    return    prgmerr(DEBUGANY, "PNM read ??");
  156.         }
  157.     }
  158. if (xtmp)    free(xtmp);
  159. if (img->mid_type == RLE || img->dpy_depth == 1)
  160.     img->channels = img->dpy_channels;    /* for RLE map_scan()    */
  161. return    rows * cols;
  162. }
  163.