home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TIFF / TFTOOL.ZIP / TIFFDIAG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-11-02  |  5.1 KB  |  216 lines

  1. /* TiffDiag.c - Tiff diagnostics routines
  2.  */
  3. #include "aldtypes.h"
  4. #include "imtypes.h"
  5. #include "imag.h"
  6. #include "imtiff.h"
  7. #include "tiff.h"
  8. #include "ImErr.h"
  9. #include "ImErr2.h"        /* application-dependent! for WarningAlert proto and IA_PLACE_IMAGE */
  10.  
  11. extern char ErrorMessages[];
  12.  
  13. #define MAX_RECOMMENDED_STRIP    (12*1024L)
  14.  
  15. /* check the tiff structure for bad values and combinations of values
  16.  */
  17. RC CheckTiff (x)
  18. register IMAG    *x;
  19. {
  20.         register RC        err = SUCCESS;
  21.         
  22.         if (!x->eNewSubfileType) {
  23.             WARN ((HWND)NULL, IM_WARNING, IM_NO_NEWSUBFILETYPE);
  24.         }
  25.  
  26.         /* ImageWidth,ImageLength
  27.          */
  28.         if (!x->eImageWidth) {
  29.             DBMSG(("CheckTiff: no width\n"));
  30.             err = IM_NO_WIDTH;
  31.             goto cu0;
  32.         }
  33.         if (!x->eImageLength) {
  34.             DBMSG(("CheckTiff: no length\n"));
  35.             err = IM_NO_LENGTH;
  36.             goto cu0;
  37.         }
  38.         if (x->iImageWidth == 0) {
  39.             DBMSG(("CheckTiff: 0 width\n"));
  40.             err = IM_BAD_WIDTH;
  41.             goto cu0;
  42.         }
  43.         if (x->iImageLength == 0) {
  44.             DBMSG(("CheckTiff: 0 length\n"));
  45.             err = IM_BAD_LENGTH;
  46.             goto cu0;
  47.         }
  48.  
  49.         if (!x->eStripOffsets) {
  50.             DBMSG(("CheckTiff: no offsets\n"));
  51.             err = IM_NO_OFFSETS;
  52.             goto cu0;
  53.         }
  54.         
  55.         /* check bit depth */
  56.         {
  57.             WORD    Bits;
  58.             
  59.             Bits = x->iBitsPerSample;
  60.             if (Bits == 1 || Bits == 4 || Bits == 6 || Bits == 8) {
  61.                 if (Bits == 6) {
  62.                     WARN ((HWND)NULL, IM_WARNING, IM_FADING_BITDEPTH);
  63.                 }
  64.             } else {
  65.                 DBMSG(("CheckTiff: bad bitspersample\n"));
  66.                 err = IM_BAD_BPS;
  67.                 goto cu0;
  68.             }
  69.         }
  70.         
  71.         /* if Samples > 1, replicate BitsPerSample, to keep everything kosher
  72.          */
  73.         if (x->iSamples != 1 && x->iSamples != 3) {
  74.             DBMSG(("CheckTiff: bad spp\n"));
  75.             err = IM_BAD_SPP;
  76.             goto cu0;
  77.         }
  78.         if (x->iSamples > 1) {
  79.             WORD    bpsBuf[4];    /* 3 for RGB, 4 for CMYK */
  80.             RC        err2;
  81.             register int        ii;
  82.             
  83.             if (x->tf[X_BITSPERSAMPLE].Tlength != x->iSamples) {
  84.             
  85.                 WARN ((HWND)NULL, IM_WARNING, IM_BAD_NUM_BITS);
  86.             }
  87.             
  88.             if (x->iPhotometricInterpretation != TIFFRGB) {
  89.                 WARN ((HWND)NULL, IM_WARNING, IM_COLOR_CLASH);
  90.             }
  91.         }
  92.         
  93.         /* planarconfiguration
  94.          */
  95.         if (x->ePlanar && x->iSamples > 1 && x->iPlanar != CHUNKY) {
  96.             DBMSG(("CheckTiff: bad planar\n"));
  97.             err = IM_BAD_PLANAR;
  98.             goto cu0;
  99.         }
  100.         
  101.         /* photometric */
  102.         {
  103.             WORD    Photo;
  104.             
  105.             Photo = x->iPhotometricInterpretation;
  106.             if (Photo != WHITEZERO && Photo != BLACKZERO && Photo != TIFFRGB) {
  107.                 DBMSG(("CheckTiff: unsupported photometricinterp = %u\n", Photo));
  108.                 err = IM_BAD_PHOTO;
  109.                 goto cu0;
  110.             }
  111.             if (!x->ePhotometricInterpretation) {
  112.                 WARN ((HWND)NULL, IM_WARNING, IM_NO_PHOTO);
  113.             }
  114.         }
  115.         
  116.         /* compression
  117.          */
  118.         {
  119.             WORD    Compr;
  120.             
  121.             Compr = x->iCompression;
  122.             
  123.             if (Compr == PACKINTOBYTES) {
  124.                 WARN ((HWND)NULL, IM_WARNING, IM_NO_COMPR);        /* no compression */
  125.             } else if (Compr == CCITT1D || Compr == COMPR5 || Compr == TIFFPACKBITS) {
  126.                 /* ok */
  127.             } else {
  128.                 WARN ((HWND)NULL, IM_WARNING, IM_BAD_COMPR);    /* unknown compression */
  129.             }
  130.             
  131.             if (Compr == TIFFPACKBITS && x->iBitsPerSample != 1) {
  132.                 WARN ((HWND)NULL, IM_WARNING, IM_PB_BITSNOTONE);    /* unknown compression */
  133.             }
  134.         }
  135.         
  136.         /* Predictor */
  137.         {
  138.             WORD    Pred;
  139.             
  140.             Pred = x->iPredictor;
  141.             if (Pred == PREDICTOR_NONE) {
  142.             } else if (Pred == PREDICTOR_HDIFF) {
  143.                 if (x->iBitsPerSample != 8) {
  144.                     DBMSG(("CheckTiff: horiz diff but bits=%u\n", x->iBitsPerSample));
  145.                     err = IM_PRED_MISMATCH;
  146.                     goto cu0;
  147.                 }
  148.             } else {
  149.                 DBMSG(("CheckTiff: unknown predictor\n"));
  150.                 err = IM_BAD_PREDICT;
  151.                 goto cu0;
  152.             }
  153.         }
  154.         
  155.         /* make RowsPerStrip no larger than ImageLength, for future convenience
  156.          */
  157.         if (x->iRowsPerStrip > x->iImageLength)
  158.             x->iRowsPerStrip = x->iImageLength;
  159.         
  160.         /* check strip size
  161.          *
  162.          * TODO: do we want to store dwStripBytes around for future convenience?
  163.          * If so, make sure all the other imports do the same ... or do it in fr.c?
  164.          */
  165.         {
  166.             DWORD    dwBytesPerRow, dwStripBytes;
  167.             
  168.             dwBytesPerRow = UBPR(x->iImageWidth, x->iBitsPerSample, x->iSamples);
  169.             dwStripBytes = (DWORD)x->iRowsPerStrip * dwBytesPerRow;
  170.             
  171.             if (dwStripBytes > MAX_RECOMMENDED_STRIP) {
  172.                 WARN ((HWND)NULL, IM_WARNING, IM_LARGE_STRIP);
  173.             }        
  174.         }
  175.         
  176.         /* complain if there are no StripByteCounts
  177.          */
  178.         if (!x->eStripByteCounts) {
  179.             WARN ((HWND)NULL, IM_WARNING, IM_NO_BYTECOUNTS);
  180.         }
  181.         
  182.         /* fatal error if advertised number of offsets or counts does not
  183.          * equal the computed number
  184.          */
  185.         {
  186.             WORD    NumStrips;
  187.             
  188.             if (x->iRowsPerStrip == 0) {
  189.                 DBMSG(("CheckTiff: rps = 0\n"));
  190.                 err = IM_BAD_ROWSPERSTRIP;
  191.                 goto cu0;
  192.             }
  193.             NumStrips = (x->iImageLength + x->iRowsPerStrip - 1) / x->iRowsPerStrip;
  194.             
  195.             if (x->eStripOffsets) {
  196.                 if (NumStrips != (WORD)x->tf[X_STRIPOFFSETS].Tlength) {
  197.                     DBMSG(("CheckTiff: bad num offsets\n"));
  198.                     err = IM_BAD_NUM_OFF;
  199.                     goto cu0;
  200.                 }
  201.             }
  202.             if (x->eStripByteCounts) {
  203.                 if (NumStrips != (WORD)x->tf[X_STRIPBYTECOUNTS].Tlength) {
  204.                     DBMSG(("CheckTiff: bad num counts\n"));
  205.                     err = IM_BAD_NUM_COUNTS;
  206.                     goto cu0;
  207.                 }
  208.             }
  209.         }
  210.         
  211.         /* return
  212.          */
  213. cu0:    return err;
  214.  
  215. } /* end of CheckTiff */
  216.