home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / lib / tiff_r.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-02  |  4.8 KB  |  151 lines

  1. /*    TIFF_Read . 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    8/1/91
  33. */
  34.  
  35. #include "header.def"
  36. #include "imagedef.h"
  37.  
  38. #define    obuf    img->src
  39. #define    GetTiffValue(f, v)    TIFFGetField(TiffIn, f, v)
  40.  
  41. /*    load routine is not dependent on img->channels,
  42. %    so, header_handle only need set up img->dpy_chaanels.
  43. */
  44.  
  45. read_tiff_image(img, colormap, TiffIn, TiffRGB, OsameI8)
  46. U_IMAGE    *img;
  47. cmap_t    *colormap[3];
  48. TIFF    *TiffIn;
  49. {
  50. byte    *inbuf;
  51. int    pm, maxval, width, height, row,
  52.     samples = TiffIn->tif_dir.td_samplesperpixel;
  53. unsigned short    config, bitspersample;
  54. register byte    *obp;
  55.  
  56. GetTiffValue(TIFFTAG_IMAGEWIDTH, &width);
  57. GetTiffValue(TIFFTAG_IMAGELENGTH, &height);
  58. verify_buffer_size(&obuf, width*height, img->dpy_channels, "tiff-src");
  59.  
  60. GetTiffValue(TIFFTAG_BITSPERSAMPLE, &bitspersample);
  61. maxval = (1<<bitspersample) - 1;
  62. GetTiffValue(TIFFTAG_PLANARCONFIG, &config);
  63.  
  64. switch (config) {
  65. case PLANARCONFIG_CONTIG: {
  66. register int    col, shift=bitspersample, pad=shift & 1, scale = 8 - shift + pad;
  67. #define    Next_Sample(bit_shift, bits_ps, ibp)    \
  68.     {    \
  69.         if (!bit_shift)    {    bit_shift=8,    ibp++;    }    \
  70.         bit_shift -= bits_ps;    \
  71.     }
  72.  
  73.     inbuf = (byte*)nzalloc(1, TIFFScanlineSize(TiffIn), "tiff-cell-buf");
  74.     msg("pmetric %d\n", pm=TiffIn->tif_dir.td_photometric);
  75.     for (row=0; row < height; row++) {
  76.     register byte    *ibp=inbuf;
  77.         obp = (byte*)obuf + width*row*img->dpy_channels;
  78.         if (TIFFReadScanline(TiffIn, ibp, row, 0) < 0)
  79.             break;
  80.         shift = 8;
  81.         switch (pm)    {
  82.         case PHOTOMETRIC_MINISWHITE:
  83.             if (scale) for (col=width; col--;)    {
  84.             register int    v;
  85.                 Next_Sample(shift, bitspersample, ibp);
  86.                 v = maxval - ((*ibp >> shift) & maxval) << scale;
  87.                 *obp++ = v - (v > 0) * pad;
  88.             }
  89.             else for (col=width; col--;)    /* speed 8-bit proc. */
  90.                 *obp++ = maxval - *ibp++;
  91.             break;
  92.         case PHOTOMETRIC_MINISBLACK:
  93.             if (scale) for (col=width; col--;)    {
  94.             register int    v;
  95.                 Next_Sample(shift, bitspersample, ibp);
  96.                 v = ((*ibp >> shift) & maxval) << scale;
  97.                 *obp++ = v - (v > 0) * pad;
  98.             }
  99.             else for (col=width; col--;)    /* speed 256 */
  100.                 *obp++ = *ibp++;
  101.             break;
  102.     /*    case PHOTOMETRIC_MASK:    */
  103.  
  104.         case PHOTOMETRIC_RGB:
  105.         case PHOTOMETRIC_SEPARATED:
  106.         case PHOTOMETRIC_YCBCR:
  107.             if (img->mid_type == COLOR_PS)
  108.             memcpy(obp, ibp, width * img->dpy_channels);
  109.             else if (img->color_dpy)    {    /* for safety */
  110.             if (img->color_form == CFM_ILL)
  111.                 any_ilc_to_rle(obp, ibp, img, samples, No);
  112.             else    ilc_transfer(obp, ibp, width, samples, No, 3);
  113.             } else    ilc_to_gray(obp, ibp, width, samples==4,
  114.                         NULL /* no mapping */, True);
  115.             break;
  116.         case PHOTOMETRIC_PALETTE:    /* indexed    */
  117.         if (!img->color_dpy)
  118.             map8_gray(obp, ibp, width, colormap);
  119.         else if (OsameI8)
  120.             memcpy(obp, ibp, width * img->dpy_channels);
  121.         else
  122.             snf_to_rle(obp, ibp, width, bitspersample, colormap);
  123.         break;
  124.         default:    return    prgmerr(0, "unknown tiff format");
  125.         }
  126.     }
  127.     }    break;
  128. case PLANARCONFIG_SEPARATE:    {
  129. register int    s;
  130. int rowbytes = TIFFScanlineSize(TiffIn);
  131.  
  132.     inbuf = (byte*)nzalloc(samples, rowbytes, "tiff-line-buf");
  133.     for (row = 0; row < height; row++) {
  134.         obp = (byte*)obuf + width*row*img->dpy_channels;
  135.         for (s=0; s < samples; s++)
  136.         if (TIFFReadScanline(TiffIn, inbuf+s*rowbytes, row, s)<0)
  137.             return    prgmerr(DEBUGANY, "Tiff read scanline");
  138.         if (img->color_dpy || img->dpy_channels==samples)
  139.         memcpy(obp, inbuf, width * samples);
  140.         else
  141.         ill_to_gray(obp, inbuf, inbuf + rowbytes, inbuf + 2*rowbytes,
  142.             width);
  143.     }
  144.     }    break;
  145. default:    return    prgmerr(0, "not a applicable plan %d\n", config);
  146. }
  147. if (img->mid_type == RLE)
  148.     img->channels = img->dpy_channels;
  149. return    width * row;
  150. }
  151.