Examples of TIFFDirectory


Examples of com.lightcrafts.media.jai.codec.TIFFDirectory

  }

  decodePaletteAsShorts = param.getDecodePaletteAsShorts();

        // Read the specified directory.
        TIFFDirectory dir = param.getIFDOffset() == null ?
            new TIFFDirectory(stream, directory) :
            new TIFFDirectory(stream, param.getIFDOffset().longValue(),
                              directory);

        // Set a property "tiff_directory".
        properties.put("tiff_directory", dir);

  // Get the number of samples per pixel
  TIFFField sfield =
      dir.getField(TIFFImageDecoder.TIFF_SAMPLES_PER_PIXEL);
        int samplesPerPixel = sfield == null ? 1 : (int)sfield.getAsLong(0);

  // Read the TIFF_PLANAR_CONFIGURATION field
  TIFFField planarConfigurationField =
      dir.getField(TIFFImageDecoder.TIFF_PLANAR_CONFIGURATION);
  char[] planarConfiguration = planarConfigurationField == null ?
            new char[] {1} :
            planarConfigurationField.getAsChars();

        // Support planar format (band sequential) only for 1 sample/pixel.
        if (planarConfiguration[0] != 1 && samplesPerPixel != 1) {
            throw new RuntimeException(JaiI18N.getString("TIFFImage0"));
        }

  // Read the TIFF_BITS_PER_SAMPLE field
  TIFFField bitsField =
      dir.getField(TIFFImageDecoder.TIFF_BITS_PER_SAMPLE);
        char[] bitsPerSample = null;
        if(bitsField != null) {
            bitsPerSample = bitsField.getAsChars();
        } else {
            bitsPerSample = new char[] {1};

            // Ensure that all samples have the same bit depth.
            for (int i = 1; i < bitsPerSample.length; i++) {
                if (bitsPerSample[i] != bitsPerSample[0]) {
                    throw new RuntimeException(
               JaiI18N.getString("TIFFImage1"));
                }
            }
        }
        sampleSize = (int)bitsPerSample[0];

  // Read the TIFF_SAMPLE_FORMAT tag to see whether the data might be
  // signed or floating point
  TIFFField sampleFormatField =
      dir.getField(TIFFImageDecoder.TIFF_SAMPLE_FORMAT);

        char[] sampleFormat = null;
  if (sampleFormatField != null) {
      sampleFormat = sampleFormatField.getAsChars();

      // Check that all the samples have the same format
      for (int l=1; l<sampleFormat.length; l++) {
    if (sampleFormat[l] != sampleFormat[0]) {
        throw new RuntimeException(
               JaiI18N.getString("TIFFImage2"));
    }
      }

  } else {
      sampleFormat = new char[] {1};
  }

        // Set the data type based on the sample size and format.
        boolean isValidDataFormat = false;
        switch(sampleSize) {
        case 1:
        case 4:
        case 8:
            if(sampleFormat[0] != 3) {
                // Ignore whether signed or unsigned: treat all as unsigned.
    dataType = DataBuffer.TYPE_BYTE;
                isValidDataFormat = true;
            }
            break;
        case 16:
            if(sampleFormat[0] != 3) {
                dataType = sampleFormat[0] == 2 ?
                    DataBuffer.TYPE_SHORT : DataBuffer.TYPE_USHORT;
                isValidDataFormat = true;
            }
            break;
        case 32:
            dataType = sampleFormat[0] == 3 ?
                DataBuffer.TYPE_FLOAT : DataBuffer.TYPE_INT;
            isValidDataFormat = true;
            break;
        }

        if(!isValidDataFormat) {
            throw new RuntimeException(JaiI18N.getString("TIFFImage3"));
        }

  // Figure out what compression if any, is being used.
  TIFFField compField = dir.getField(TIFFImageDecoder.TIFF_COMPRESSION);
        compression = compField == null ? COMP_NONE : compField.getAsInt(0);

        // Get the photometric interpretation field.
        TIFFField photoInterpField =
            dir.getField(TIFFImageDecoder.TIFF_PHOTOMETRIC_INTERPRETATION);

        // Set the photometric interpretation variable.
        int photometricType;
        if(photoInterpField != null) {
            // Set the variable from the photometric interpretation field.
            photometricType = (int)photoInterpField.getAsLong(0);
        } else {
            // The photometric interpretation field is missing; attempt
            // to infer the type from other information.
            if(dir.getField(TIFFImageDecoder.TIFF_COLORMAP) != null) {
                // There is a colormap so most likely a palette color image.
                photometricType = 3; // RGB Palette
            } else if(sampleSize == 1) {
                // Bilevel image so most likely a document; switch based
                // on the compression type of the image.
                if(compression == COMP_FAX_G3_1D ||
                   compression == COMP_FAX_G3_2D ||
                   compression == COMP_FAX_G4_2D) {
                    photometricType = 0; // WhiteIsZero
                } else {
                    photometricType = 1; // BlackIsZero
                }
            } else if(samplesPerPixel == 3 || samplesPerPixel == 4) {
                // Assume 3 bands is RGB and 4 bands is RGBA.
                photometricType = 2; // RGB
            } else {
                // Default to multi-band grayscale.
                photometricType = 1; // BlackIsZero
            }
        }

        // Determine which kind of image we are dealing with.
        imageType = TYPE_UNSUPPORTED;
  switch(photometricType) {
        case 0: // WhiteIsZero
            isWhiteZero = true;
        case 1: // BlackIsZero
            if(sampleSize == 1 && samplesPerPixel == 1) {
                imageType = TYPE_BILEVEL;
            } else if(sampleSize == 4 && samplesPerPixel == 1) {
                imageType = TYPE_GRAY_4BIT;
            } else if(sampleSize % 8 == 0) {
                if(samplesPerPixel == 1) {
                    imageType = TYPE_GRAY;
                } else if(samplesPerPixel == 2) {
                    imageType = TYPE_GRAY_ALPHA;
                } else {
                    imageType = TYPE_GENERIC;
                }
            }
            break;
        case 2: // RGB
            if(sampleSize % 8 == 0) {
                if(samplesPerPixel == 3) {
                    imageType = TYPE_RGB;
                } else if(samplesPerPixel == 4) {
                    imageType = TYPE_RGB_ALPHA;
                } else {
                    imageType = TYPE_GENERIC;
                }
            }
            break;
        case 3: // RGB Palette
            if(samplesPerPixel == 1 &&
               (sampleSize == 4 || sampleSize == 8 || sampleSize == 16)) {
                imageType = TYPE_PALETTE;
            }
            break;
        case 4: // Transparency mask
            if(sampleSize == 1 && samplesPerPixel == 1) {
                imageType = TYPE_BILEVEL;
            }
            break;
  case 5: // Separated image, usually CMYK
      if (sampleSize == 8 && samplesPerPixel == 4) {
    imageType = TYPE_CMYK;
      }
        case 6: // YCbCr
            if(compression == COMP_JPEG_TTN2 &&
               sampleSize == 8 && samplesPerPixel == 3) {
                // Set color conversion flag.
                colorConvertJPEG = param.getJPEGDecompressYCbCrToRGB();

                // Set type to RGB if color converting.
                imageType = colorConvertJPEG ? TYPE_RGB : TYPE_GENERIC;
            } else {
                TIFFField chromaField = dir.getField(TIFF_YCBCR_SUBSAMPLING);
                if(chromaField != null) {
                    chromaSubH = chromaField.getAsInt(0);
                    chromaSubV = chromaField.getAsInt(1);
                } else {
                    chromaSubH = chromaSubV = 2;
                }

                if(chromaSubH*chromaSubV == 1) {
                    imageType = TYPE_GENERIC;
                } else if(sampleSize == 8 && samplesPerPixel == 3) {
                    imageType = TYPE_YCBCR_SUB;
                }
            }
            break;
        default: // Other including CIE L*a*b*, unknown.
            if(sampleSize % 8 == 0) {
                imageType = TYPE_GENERIC;
            }
        }

        // Bail out if not one of the supported types.
        if(imageType == TYPE_UNSUPPORTED) {
            throw new RuntimeException(JaiI18N.getString("TIFFImage4"));
        }

  // Set basic image layout
        minX = minY = 0;
  width = (int)(getField(dir,
             TIFFImageDecoder.TIFF_IMAGE_WIDTH,
             "Image Width").getAsLong(0));

  height = (int)(getField(dir,
        TIFFImageDecoder.TIFF_IMAGE_LENGTH,
        "Image Length").getAsLong(0));

        // Set a preliminary band count. This may be changed later as needed.
        numBands = samplesPerPixel;

  // Figure out if any extra samples are present.
  TIFFField efield = dir.getField(TIFFImageDecoder.TIFF_EXTRA_SAMPLES);
        int extraSamples = efield == null ? 0 : (int)efield.getAsLong(0);

  if (dir.getField(TIFFImageDecoder.TIFF_TILE_OFFSETS) != null) {
      // Image is in tiled format
            isTiled = true;

            tileWidth = (int)(getField(dir,
               TIFFImageDecoder.TIFF_TILE_WIDTH,
               "Tile Width").getAsLong(0));
      tileHeight = (int)(getField(dir,
          TIFFImageDecoder.TIFF_TILE_LENGTH,
          "Tile Length").getAsLong(0));
      tileOffsets =
    (getField(dir,
       TIFFImageDecoder.TIFF_TILE_OFFSETS,
       "Tile Offsets")).getAsLongs();

      tileByteCounts = getFieldAsLongs(
                 getField(dir,
                          TIFFImageDecoder.TIFF_TILE_BYTE_COUNTS,
                          "Tile Byte Counts"));

        } else {

            // Image is in stripped format, looks like tiles to us
            isTiled = false;

            // Note: Some legacy files may have tile width and height
            // written but use the strip offsets and byte counts fields
            // instead of the tile offsets and byte counts. Therefore
            // we default here to the tile dimensions if they are written.
            tileWidth =
                dir.getField(TIFFImageDecoder.TIFF_TILE_WIDTH) != null ?
                (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_TILE_WIDTH) :
                width;
      TIFFField field =
                dir.getField(TIFFImageDecoder.TIFF_ROWS_PER_STRIP);
      if (field == null) {
    // Default is infinity (2^32 -1), basically the entire image
    // TODO: Can do a better job of tiling here
    tileHeight =
                    dir.getField(TIFFImageDecoder.TIFF_TILE_LENGTH) != null ?
                    (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_TILE_LENGTH):
                    height;
      } else {
    long l = field.getAsLong(0);
    long infinity = 1;
    infinity = (infinity << 32) - 1;
    if (l == infinity || l > height) {
        // 2^32 - 1 (effectively infinity, entire image is 1 strip)
                    // or RowsPerStrip > ImageLength so clamp as having a tile
                    // larger than the image is pointless.
        tileHeight = height;
    } else {
        tileHeight = (int)l;
    }
      }

      TIFFField tileOffsetsField =
    getField(dir,
       TIFFImageDecoder.TIFF_STRIP_OFFSETS,
       "Strip Offsets");
      tileOffsets = getFieldAsLongs(tileOffsetsField);

      TIFFField tileByteCountsField =
                dir.getField(TIFFImageDecoder.TIFF_STRIP_BYTE_COUNTS);
            if(tileByteCountsField == null) {
                // Attempt to infer the number of bytes in each strip.
                int totalBytes = ((sampleSize+7)/8)*numBands*width*height;
                int bytesPerStrip =
                    ((sampleSize+7)/8)*numBands*width*tileHeight;
                int cumulativeBytes = 0;
                tileByteCounts = new long[tileOffsets.length];
                for(int i = 0; i < tileOffsets.length; i++) {
                    tileByteCounts[i] =
                        Math.min(totalBytes - cumulativeBytes,
                                 bytesPerStrip);
                    cumulativeBytes += bytesPerStrip;
                }

                if(compression != COMP_NONE) {
                    // Replace the stream with one that will not throw
                    // an EOFException when it runs past the end.
                    this.stream = new NoEOFStream(stream);
                }
            } else {
                tileByteCounts = getFieldAsLongs(tileByteCountsField);
            }

            // Uncompressed image provided in a single tile: clamp to max bytes.
            int maxBytes = width*height*numBands*((sampleSize + 7)/8);
            if(tileByteCounts.length == 1 &&
               compression == COMP_NONE &&
               tileByteCounts[0] > maxBytes) {
                tileByteCounts[0] = maxBytes;
            }
  }

  // Calculate number of tiles and the tileSize in bytes
        tilesX = (width + tileWidth - 1)/tileWidth;
        tilesY = (height + tileHeight - 1)/tileHeight;
        tileSize = tileWidth * tileHeight * numBands;

  // Check whether big endian or little endian format is used.
  isBigEndian = dir.isBigEndian();

  TIFFField fillOrderField =
      dir.getField(TIFFImageDecoder.TIFF_FILL_ORDER);
  if (fillOrderField != null) {
      fillOrder = fillOrderField.getAsInt(0);
  } else {
      // Default Fill Order
      fillOrder = 1;
  }

  switch(compression) {
        case COMP_NONE:
        case COMP_PACKBITS:
            // Do nothing.
            break;
        case COMP_DEFLATE:
            inflater = new Inflater();
            break;
        case COMP_FAX_G3_1D:
        case COMP_FAX_G3_2D:
        case COMP_FAX_G4_2D:
            if(sampleSize != 1) {
                throw new RuntimeException(JaiI18N.getString("TIFFImage7"));
            }

            // Fax T.4 compression options
            if (compression == 3) {
                TIFFField t4OptionsField =
                    dir.getField(TIFFImageDecoder.TIFF_T4_OPTIONS);
                if (t4OptionsField != null) {
                    tiffT4Options = t4OptionsField.getAsLong(0);
                } else {
                    // Use default value
                    tiffT4Options = 0;
                }
            }

            // Fax T.6 compression options
            if (compression == 4) {
                TIFFField t6OptionsField =
                    dir.getField(TIFFImageDecoder.TIFF_T6_OPTIONS);
                if (t6OptionsField != null) {
                    tiffT6Options = t6OptionsField.getAsLong(0);
                } else {
                    // Use default value
                    tiffT6Options = 0;
                }
            }

            // Fax encoding, need to create the Fax decoder.
            decoder = new TIFFFaxDecoder(fillOrder,
                                         tileWidth, tileHeight);
            break;

        case COMP_LZW:
            // LZW compression used, need to create the LZW decoder.
            TIFFField predictorField =
                dir.getField(TIFFImageDecoder.TIFF_PREDICTOR);

            if (predictorField == null) {
                predictor = 1;
            } else {
                predictor = predictorField.getAsInt(0);

                if (predictor != 1 && predictor != 2) {
                    throw new RuntimeException(JaiI18N.getString("TIFFImage8"));
                }

                if (predictor == 2 && sampleSize != 8) {
                    throw new RuntimeException(sampleSize +
                                               JaiI18N.getString("TIFFImage9"));
                }
            }

            lzwDecoder = new TIFFLZWDecoder(tileWidth, predictor,
                                            samplesPerPixel);
            break;

        case COMP_JPEG_OLD:
            throw new RuntimeException(JaiI18N.getString("TIFFImage15"));

        case COMP_JPEG_TTN2:
            if(!(sampleSize == 8 &&
                 ((imageType == TYPE_GRAY && samplesPerPixel == 1) ||
                  (imageType == TYPE_PALETTE && samplesPerPixel == 1) ||
                  (imageType == TYPE_RGB && samplesPerPixel == 3)))) {
                throw new RuntimeException(JaiI18N.getString("TIFFImage16"));
            }

            // Create decodeParam from JPEGTables field if present.
            if(dir.isTagPresent(TIFF_JPEG_TABLES)) {
                TIFFField jpegTableField = dir.getField(TIFF_JPEG_TABLES);
                byte[] jpegTable = jpegTableField.getAsBytes();
                ByteArrayInputStream tableStream =
                    new ByteArrayInputStream(jpegTable);
                JPEGImageDecoder decoder =
                    JPEGCodec.createJPEGDecoder(tableStream);
View Full Code Here

Examples of com.lightcrafts.media.jai.codec.TIFFDirectory

     * Reads a private IFD from a given offset in the stream.  This
     * method may be used to obtain IFDs that are referenced
     * only by private tag values.
     */
    public TIFFDirectory getPrivateIFD(long offset) throws IOException {
        return new TIFFDirectory(stream, offset, 0);
    }
View Full Code Here

Examples of com.sun.media.imageio.plugins.tiff.TIFFDirectory

        }
        if (w instanceof TIFFImageWriter) {
            TIFFTag ttagRefBW = BaselineTIFFTagSet.getInstance().getTag(BaselineTIFFTagSet.TAG_REFERENCE_BLACK_WHITE);
            TIFFTag ttagPhoto = BaselineTIFFTagSet.getInstance().getTag(BaselineTIFFTagSet.TAG_PHOTOMETRIC_INTERPRETATION);
            TIFFTag ttagCoeff = BaselineTIFFTagSet.getInstance().getTag(BaselineTIFFTagSet.TAG_Y_CB_CR_COEFFICIENTS);
            TIFFDirectory tifd;
            try {
                tifd = metadata == null ? new TIFFDirectory(new TIFFTagSet[]{BaselineTIFFTagSet.getInstance()}, null) : TIFFDirectory.createFromMetadata(metadata);
                if (!_useTIFF_UnforcedRGB) {
                    tifd.removeTIFFField(ttagPhoto.getNumber());
                    tifd.addTIFFField(new TIFFField(ttagPhoto, BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_RGB));
                }
                if (tifd.containsTIFFField(ttagPhoto.getNumber())) {
                    int photometric = tifd.getTIFFField(ttagPhoto.getNumber()).getAsInt(0);
                    float[] referenceBlackWhite;
                    float[] ycbcrCoefficients = null;
                    switch (photometric) {
                        case BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_RGB:
                            referenceBlackWhite = _useTIFF_NOHead_NOFoot ? referenceBlackWhiteRGB_NOHead_NOFoot : referenceBlackWhiteRGB_601_1;
                            break;
                        case BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_Y_CB_CR:
                            referenceBlackWhite = _useTIFF_NOHead_NOFoot ? referenceBlackWhiteYCbCr_NOHead_NOFoot : referenceBlackWhiteYCbCr_601_1;
                            ycbcrCoefficients = _useTIFF_NOHead_NOFoot ? ycbcrCoefficients_709 : ycbcrCoefficients_601_1;
                            break;
                        default:
                            referenceBlackWhite = null;
                            break;
                    }
                    if (referenceBlackWhite != null) {
                        tifd.removeTIFFField(ttagRefBW.getNumber());
                        tifd.addTIFFField(new TIFFField(ttagRefBW, TIFFTag.TIFF_FLOAT, 6, referenceBlackWhite));
                        if (ycbcrCoefficients != null) {
                            tifd.removeTIFFField(ttagCoeff.getNumber());
                            tifd.addTIFFField(new TIFFField(ttagCoeff, TIFFTag.TIFF_FLOAT, 3, ycbcrCoefficients));
                        }
                        if (DebugMap._getInstance().isDebuggerEnabled(Sprite.class) && DebugMap._getInstance().isDebugLevelEnabled(DebugMap._getInstance()._VOID)) {
                            System.out.print("TIFFField ReferenceBlackWhite ");
                            for (float f : referenceBlackWhite) {
                                System.out.print(" " + f);
                            }
                            System.out.println();
                        }
                    }
                }
                if (_useTIFF_EXIF) {
                    TIFFTagSet exif = EXIFTIFFTagSet.getInstance();
                    tifd.addTagSet(exif);
                    tifd.addTIFFField(new TIFFField(exif.getTag(EXIFTIFFTagSet.TAG_EXIF_VERSION), TIFFTag.TIFF_UNDEFINED, 4, EXIFTIFFTagSet.EXIF_VERSION_2_2));
                }
                metadata = tifd.getAsMetadata();
            } catch (IIOInvalidTreeException ex) {
                if (DebugMap._getInstance().isDebuggerEnabled(Sprite.class)) {
                    ex.printStackTrace();
                }
            }
View Full Code Here

Examples of com.sun.media.imageio.plugins.tiff.TIFFDirectory

            }
            if(tagName != null) {
                setAttribute("parentTagName", tagName);
            }

            TIFFDirectory dir = (TIFFDirectory)field.getData();
            TIFFTagSet[] tagSets = dir.getTagSets();
            if(tagSets != null) {
                String tagSetNames = "";
                for(int i = 0; i < tagSets.length; i++) {
                    tagSetNames += tagSets[i].getClass().getName();
                    if(i != tagSets.length - 1) {
View Full Code Here

Examples of com.sun.media.imageio.plugins.tiff.TIFFDirectory

    private synchronized void initialize() {
        if(isInitialized == Boolean.TRUE) return;

        if(isIFD) {
            TIFFDirectory dir = (TIFFDirectory)field.getData();
            TIFFField[] fields = dir.getTIFFFields();
            if(fields != null) {
                TIFFTagSet[] tagSets = dir.getTagSets();
                List tagSetList = Arrays.asList(tagSets);
                int numFields = fields.length;
                for(int i = 0; i < numFields; i++) {
                    TIFFField f = fields[i];
                    int tagNumber = f.getTagNumber();
View Full Code Here

Examples of com.sun.media.imageio.plugins.tiff.TIFFDirectory

    private Node getTIFFTree() {
        String metadataName = TIFF_FORMAT;

        BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();

        TIFFDirectory dir =
            new TIFFDirectory(new TIFFTagSet[] {
                base, EXIFParentTIFFTagSet.getInstance()
            }, null);

        if(sofPresent) {
            // sofProcess -> Compression ?
            int compression = BaselineTIFFTagSet.COMPRESSION_JPEG;
            TIFFField compressionField =
                new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_COMPRESSION),
                              compression);
            dir.addTIFFField(compressionField);

            // samplePrecision -> BitsPerSample
            char[] bitsPerSample = new char[numFrameComponents];
            Arrays.fill(bitsPerSample, (char)(samplePrecision & 0xff));
            TIFFField bitsPerSampleField =
                new TIFFField(
                              base.getTag(BaselineTIFFTagSet.TAG_BITS_PER_SAMPLE),
                              TIFFTag.TIFF_SHORT,
                              bitsPerSample.length,
                              bitsPerSample);
            dir.addTIFFField(bitsPerSampleField);

            // numLines -> ImageLength
            TIFFField imageLengthField =
                new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_IMAGE_LENGTH),
                              numLines);
            dir.addTIFFField(imageLengthField);

            // samplesPerLine -> ImageWidth
            TIFFField imageWidthField =
                new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_IMAGE_WIDTH),
                              samplesPerLine);
            dir.addTIFFField(imageWidthField);

            // numFrameComponents -> SamplesPerPixel
            TIFFField samplesPerPixelField =
                new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_SAMPLES_PER_PIXEL),
                              numFrameComponents);
            dir.addTIFFField(samplesPerPixelField);

            // componentId -> PhotometricInterpretation + ExtraSamples
            IIOMetadataNode chroma = getStandardChromaNode();
            if(chroma != null) {
                IIOMetadataNode csType =
                    (IIOMetadataNode)chroma.getElementsByTagName("ColorSpaceType").item(0);
                String name = csType.getAttribute("name");
                int photometricInterpretation = -1;
                if(name.equals("GRAY")) {
                    photometricInterpretation =
                        BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO;
                } else if(name.equals("YCbCr") || name.equals("PhotoYCC")) {
                    // NOTE: PhotoYCC -> YCbCr
                    photometricInterpretation =
                        BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_Y_CB_CR;
                } else if(name.equals("RGB")) {
                    photometricInterpretation =
                        BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_RGB;
                } else if(name.equals("CMYK") || name.equals("YCCK")) {
                    // NOTE: YCCK -> CMYK
                    photometricInterpretation =
                        BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_CMYK;
                }

                if(photometricInterpretation != -1) {
                    TIFFField photometricInterpretationField =
                        new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_PHOTOMETRIC_INTERPRETATION),
                                      photometricInterpretation);
                    dir.addTIFFField(photometricInterpretationField);
                }

                if(hasAlpha) {
                    char[] extraSamples =
                        new char[] {BaselineTIFFTagSet.EXTRA_SAMPLES_ASSOCIATED_ALPHA};
                    TIFFField extraSamplesField =
                        new TIFFField(
                                      base.getTag(BaselineTIFFTagSet.TAG_EXTRA_SAMPLES),
                                      TIFFTag.TIFF_SHORT,
                                      extraSamples.length,
                                      extraSamples);
                    dir.addTIFFField(extraSamplesField);
                }
            } // chroma != null
        } // sofPresent

        // JFIF APP0 -> Resolution fields.
        if(app0JFIFPresent) {
            long[][] xResolution = new long[][] {{Xdensity, 1}};
            TIFFField XResolutionField =
                new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION),
                              TIFFTag.TIFF_RATIONAL,
                              1,
                              xResolution);
            dir.addTIFFField(XResolutionField);

            long[][] yResolution = new long[][] {{Ydensity, 1}};
            TIFFField YResolutionField =
                new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION),
                              TIFFTag.TIFF_RATIONAL,
                              1,
                              yResolution);
            dir.addTIFFField(YResolutionField);

            int resolutionUnit = BaselineTIFFTagSet.RESOLUTION_UNIT_NONE;
            switch(resUnits) {
            case JFIF_RESUNITS_ASPECT:
                resolutionUnit = BaselineTIFFTagSet.RESOLUTION_UNIT_NONE;
            case JFIF_RESUNITS_DPI:
                resolutionUnit = BaselineTIFFTagSet.RESOLUTION_UNIT_INCH;
                break;
            case JFIF_RESUNITS_DPC:
                resolutionUnit = BaselineTIFFTagSet.RESOLUTION_UNIT_CENTIMETER;
                break;
            }
            TIFFField ResolutionUnitField =
                new TIFFField(base.getTag
                              (BaselineTIFFTagSet.TAG_RESOLUTION_UNIT),
                              resolutionUnit);
            dir.addTIFFField(ResolutionUnitField);
        }

        // DQT + DHT -> JPEGTables.
        byte[] jpegTablesData = null;
        if(dqtPresent || dqtPresent) {
            // Determine length of JPEGTables data.
            int jpegTablesLength = 2; // SOI
            if(dqtPresent) {
                Iterator dqts = qtables.iterator();
                while(dqts.hasNext()) {
                    Iterator qtiter = ((List)dqts.next()).iterator();
                    while(qtiter.hasNext()) {
                        QTable qt = (QTable)qtiter.next();
                        jpegTablesLength += 4 + qt.length;
                    }
                }
            }
            if(dhtPresent) {
                Iterator dhts = htables.iterator();
                while(dhts.hasNext()) {
                    Iterator htiter = ((List)dhts.next()).iterator();
                    while(htiter.hasNext()) {
                        HuffmanTable ht = (HuffmanTable)htiter.next();
                        jpegTablesLength += 4 + ht.length;
                    }
                }
            }
            jpegTablesLength += 2; // EOI

            // Allocate space.
            jpegTablesData = new byte[jpegTablesLength];

            // SOI
            jpegTablesData[0] = (byte)0xff;
            jpegTablesData[1] = (byte)SOI;
            int jpoff = 2;

            if(dqtPresent) {
                Iterator dqts = qtables.iterator();
                while(dqts.hasNext()) {
                    Iterator qtiter = ((List)dqts.next()).iterator();
                    while(qtiter.hasNext()) {
                        jpegTablesData[jpoff++] = (byte)0xff;
                        jpegTablesData[jpoff++] = (byte)DQT;
                        QTable qt = (QTable)qtiter.next();
                        int qtlength = qt.length + 2;
                        jpegTablesData[jpoff++] =
                            (byte)((qtlength & 0xff00) >> 8);
                        jpegTablesData[jpoff++] = (byte)(qtlength & 0xff);
                        jpegTablesData[jpoff++] =
                            (byte)(((qt.elementPrecision & 0xf0) << 4) |
                                   (qt.tableID & 0x0f));
                        int[] table = qt.table.getTable();
                        int qlen = table.length;
                        for(int i = 0; i < qlen; i++) {
                            jpegTablesData[jpoff + zigzag[i]] = (byte)table[i];
                        }
                        jpoff += qlen;
                    }
                }
            }

            if(dhtPresent) {
                Iterator dhts = htables.iterator();
                while(dhts.hasNext()) {
                    Iterator htiter = ((List)dhts.next()).iterator();
                    while(htiter.hasNext()) {
                        jpegTablesData[jpoff++] = (byte)0xff;
                        jpegTablesData[jpoff++] = (byte)DHT;
                        HuffmanTable ht = (HuffmanTable)htiter.next();
                        int htlength = ht.length + 2;
                        jpegTablesData[jpoff++] =
                            (byte)((htlength & 0xff00) >> 8);
                        jpegTablesData[jpoff++] = (byte)(htlength & 0xff);
                        jpegTablesData[jpoff++] =
                            (byte)(((ht.tableClass & 0x0f) << 4) |
                                   (ht.tableID & 0x0f));
                        short[] lengths = ht.table.getLengths();
                        int numLengths = lengths.length;
                        for(int i = 0; i < numLengths; i++) {
                            jpegTablesData[jpoff++] = (byte)lengths[i];
                        }
                        short[] values = ht.table.getValues();
                        int numValues = values.length;
                        for(int i = 0; i < numValues; i++) {
                            jpegTablesData[jpoff++] = (byte)values[i];
                        }
                    }
                }
            }

            jpegTablesData[jpoff++] = (byte)0xff;
            jpegTablesData[jpoff] = (byte)EOI;           
        }
        if(jpegTablesData != null) {
            TIFFField JPEGTablesField =
                new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_JPEG_TABLES),
                              TIFFTag.TIFF_UNDEFINED,
                              jpegTablesData.length,
                              jpegTablesData);
            dir.addTIFFField(JPEGTablesField);
        }

        IIOMetadata tiffMetadata = dir.getAsMetadata();

        if(exifData != null) {
            try {
                Iterator tiffReaders =
                    ImageIO.getImageReadersByFormatName("TIFF");
View Full Code Here

Examples of com.sun.media.jai.codec.TIFFDirectory

  }

  decodePaletteAsShorts = param.getDecodePaletteAsShorts();

        // Read the specified directory.
        TIFFDirectory dir = param.getIFDOffset() == null ?
            new TIFFDirectory(stream, directory) :
            new TIFFDirectory(stream, param.getIFDOffset().longValue(),
                              directory);

        // Set a property "tiff_directory".
        properties.put("tiff_directory", dir);

  // Get the number of samples per pixel
  TIFFField sfield =
      dir.getField(TIFFImageDecoder.TIFF_SAMPLES_PER_PIXEL);
        int samplesPerPixel = sfield == null ? 1 : (int)sfield.getAsLong(0);

  // Read the TIFF_PLANAR_CONFIGURATION field
  TIFFField planarConfigurationField =
      dir.getField(TIFFImageDecoder.TIFF_PLANAR_CONFIGURATION);
  char[] planarConfiguration = planarConfigurationField == null ?
            new char[] {1} :
            planarConfigurationField.getAsChars();

        // Support planar format (band sequential) only for 1 sample/pixel.
        if (planarConfiguration[0] != 1 && samplesPerPixel != 1) {
            throw new RuntimeException(JaiI18N.getString("TIFFImage0"));
        }

  // Read the TIFF_BITS_PER_SAMPLE field
  TIFFField bitsField =
      dir.getField(TIFFImageDecoder.TIFF_BITS_PER_SAMPLE);
        char[] bitsPerSample = null;
        if(bitsField != null) {
            bitsPerSample = bitsField.getAsChars();
        } else {
            bitsPerSample = new char[] {1};

            // Ensure that all samples have the same bit depth.
            for (int i = 1; i < bitsPerSample.length; i++) {
                if (bitsPerSample[i] != bitsPerSample[0]) {
                    throw new RuntimeException(
               JaiI18N.getString("TIFFImage1"));
                }
            }
        }
        sampleSize = (int)bitsPerSample[0];

  // Read the TIFF_SAMPLE_FORMAT tag to see whether the data might be
  // signed or floating point
  TIFFField sampleFormatField =
      dir.getField(TIFFImageDecoder.TIFF_SAMPLE_FORMAT);

        char[] sampleFormat = null;
  if (sampleFormatField != null) {
      sampleFormat = sampleFormatField.getAsChars();

      // Check that all the samples have the same format
      for (int l=1; l<sampleFormat.length; l++) {
    if (sampleFormat[l] != sampleFormat[0]) {
        throw new RuntimeException(
               JaiI18N.getString("TIFFImage2"));
    }
      }

  } else {
      sampleFormat = new char[] {1};
  }

        // Set the data type based on the sample size and format.
        boolean isValidDataFormat = false;
        switch(sampleSize) {
        case 1:
        case 4:
        case 8:
            if(sampleFormat[0] != 3) {
                // Ignore whether signed or unsigned: treat all as unsigned.
    dataType = DataBuffer.TYPE_BYTE;
                isValidDataFormat = true;
            }
            break;
        case 16:
            if(sampleFormat[0] != 3) {
                dataType = sampleFormat[0] == 2 ?
                    DataBuffer.TYPE_SHORT : DataBuffer.TYPE_USHORT;
                isValidDataFormat = true;
            }
            break;
        case 32:
            dataType = sampleFormat[0] == 3 ?
                DataBuffer.TYPE_FLOAT : DataBuffer.TYPE_INT;
            isValidDataFormat = true;
            break;
        }

        if(!isValidDataFormat) {
            throw new RuntimeException(JaiI18N.getString("TIFFImage3"));
        }

  // Figure out what compression if any, is being used.
  TIFFField compField = dir.getField(TIFFImageDecoder.TIFF_COMPRESSION);
        compression = compField == null ? COMP_NONE : compField.getAsInt(0);

        // Get the photometric interpretation field.
        TIFFField photoInterpField =
            dir.getField(TIFFImageDecoder.TIFF_PHOTOMETRIC_INTERPRETATION);

        // Set the photometric interpretation variable.
        int photometricType;
        if(photoInterpField != null) {
            // Set the variable from the photometric interpretation field.
            photometricType = (int)photoInterpField.getAsLong(0);
        } else {
            // The photometric interpretation field is missing; attempt
            // to infer the type from other information.
            if(dir.getField(TIFFImageDecoder.TIFF_COLORMAP) != null) {
                // There is a colormap so most likely a palette color image.
                photometricType = 3; // RGB Palette
            } else if(sampleSize == 1) {
                // Bilevel image so most likely a document; switch based
                // on the compression type of the image.
                if(compression == COMP_FAX_G3_1D ||
                   compression == COMP_FAX_G3_2D ||
                   compression == COMP_FAX_G4_2D) {
                    photometricType = 0; // WhiteIsZero
                } else {
                    photometricType = 1; // BlackIsZero
                }
            } else if(samplesPerPixel == 3 || samplesPerPixel == 4) {
                // Assume 3 bands is RGB and 4 bands is RGBA.
                photometricType = 2; // RGB
            } else {
                // Default to multi-band grayscale.
                photometricType = 1; // BlackIsZero
            }
        }

        // Determine which kind of image we are dealing with.
        imageType = TYPE_UNSUPPORTED;
  switch(photometricType) {
        case 0: // WhiteIsZero
            isWhiteZero = true;
        case 1: // BlackIsZero
            if(sampleSize == 1 && samplesPerPixel == 1) {
                imageType = TYPE_BILEVEL;
            } else if(sampleSize == 4 && samplesPerPixel == 1) {
                imageType = TYPE_GRAY_4BIT;
            } else if(sampleSize % 8 == 0) {
                if(samplesPerPixel == 1) {
                    imageType = TYPE_GRAY;
                } else if(samplesPerPixel == 2) {
                    imageType = TYPE_GRAY_ALPHA;
                } else {
                    imageType = TYPE_GENERIC;
                }
            }
            break;
        case 2: // RGB
            if(sampleSize % 8 == 0) {
                if(samplesPerPixel == 3) {
                    imageType = TYPE_RGB;
                } else if(samplesPerPixel == 4) {
                    imageType = TYPE_RGB_ALPHA;
                } else {
                    imageType = TYPE_GENERIC;
                }
            }
            break;
        case 3: // RGB Palette
            if(samplesPerPixel == 1 &&
               (sampleSize == 4 || sampleSize == 8 || sampleSize == 16)) {
                imageType = TYPE_PALETTE;
            }
            break;
        case 4: // Transparency mask
            if(sampleSize == 1 && samplesPerPixel == 1) {
                imageType = TYPE_BILEVEL;
            }
            break;
  case 5: // Separated image, usually CMYK
      if (sampleSize == 8 && samplesPerPixel == 4) {
    imageType = TYPE_CMYK;
      }
        case 6: // YCbCr
            if(compression == COMP_JPEG_TTN2 &&
               sampleSize == 8 && samplesPerPixel == 3) {
                // Set color conversion flag.
                colorConvertJPEG = param.getJPEGDecompressYCbCrToRGB();

                // Set type to RGB if color converting.
                imageType = colorConvertJPEG ? TYPE_RGB : TYPE_GENERIC;
            } else {
                TIFFField chromaField = dir.getField(TIFF_YCBCR_SUBSAMPLING);
                if(chromaField != null) {
                    chromaSubH = chromaField.getAsInt(0);
                    chromaSubV = chromaField.getAsInt(1);
                } else {
                    chromaSubH = chromaSubV = 2;
                }

                if(chromaSubH*chromaSubV == 1) {
                    imageType = TYPE_GENERIC;
                } else if(sampleSize == 8 && samplesPerPixel == 3) {
                    imageType = TYPE_YCBCR_SUB;
                }
            }
            break;
        default: // Other including CIE L*a*b*, unknown.
            if(sampleSize % 8 == 0) {
                imageType = TYPE_GENERIC;
            }
        }

        // Bail out if not one of the supported types.
        if(imageType == TYPE_UNSUPPORTED) {
            throw new RuntimeException(JaiI18N.getString("TIFFImage4"));
        }

  // Set basic image layout
        minX = minY = 0;
  width = (int)(getField(dir,
             TIFFImageDecoder.TIFF_IMAGE_WIDTH,
             "Image Width").getAsLong(0));

  height = (int)(getField(dir,
        TIFFImageDecoder.TIFF_IMAGE_LENGTH,
        "Image Length").getAsLong(0));

        // Set a preliminary band count. This may be changed later as needed.
        numBands = samplesPerPixel;

  // Figure out if any extra samples are present.
  TIFFField efield = dir.getField(TIFFImageDecoder.TIFF_EXTRA_SAMPLES);
        int extraSamples = efield == null ? 0 : (int)efield.getAsLong(0);

  if (dir.getField(TIFFImageDecoder.TIFF_TILE_OFFSETS) != null) {
      // Image is in tiled format
            isTiled = true;

            tileWidth = (int)(getField(dir,
               TIFFImageDecoder.TIFF_TILE_WIDTH,
               "Tile Width").getAsLong(0));
      tileHeight = (int)(getField(dir,
          TIFFImageDecoder.TIFF_TILE_LENGTH,
          "Tile Length").getAsLong(0));
      tileOffsets =
    (getField(dir,
       TIFFImageDecoder.TIFF_TILE_OFFSETS,
       "Tile Offsets")).getAsLongs();

      tileByteCounts = getFieldAsLongs(
                 getField(dir,
                          TIFFImageDecoder.TIFF_TILE_BYTE_COUNTS,
                          "Tile Byte Counts"));

        } else {

            // Image is in stripped format, looks like tiles to us
            isTiled = false;

            // Note: Some legacy files may have tile width and height
            // written but use the strip offsets and byte counts fields
            // instead of the tile offsets and byte counts. Therefore
            // we default here to the tile dimensions if they are written.
            tileWidth =
                dir.getField(TIFFImageDecoder.TIFF_TILE_WIDTH) != null ?
                (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_TILE_WIDTH) :
                width;
      TIFFField field =
                dir.getField(TIFFImageDecoder.TIFF_ROWS_PER_STRIP);
      if (field == null) {
    // Default is infinity (2^32 -1), basically the entire image
    // TODO: Can do a better job of tiling here
    tileHeight =
                    dir.getField(TIFFImageDecoder.TIFF_TILE_LENGTH) != null ?
                    (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_TILE_LENGTH):
                    height;
      } else {
    long l = field.getAsLong(0);
    long infinity = 1;
    infinity = (infinity << 32) - 1;
    if (l == infinity || l > height) {
        // 2^32 - 1 (effectively infinity, entire image is 1 strip)
                    // or RowsPerStrip > ImageLength so clamp as having a tile
                    // larger than the image is pointless.
        tileHeight = height;
    } else {
        tileHeight = (int)l;
    }
      }

      TIFFField tileOffsetsField =
    getField(dir,
       TIFFImageDecoder.TIFF_STRIP_OFFSETS,
       "Strip Offsets");
      tileOffsets = getFieldAsLongs(tileOffsetsField);

      TIFFField tileByteCountsField =
                dir.getField(TIFFImageDecoder.TIFF_STRIP_BYTE_COUNTS);
            if(tileByteCountsField == null) {
                // Attempt to infer the number of bytes in each strip.
                int totalBytes = ((sampleSize+7)/8)*numBands*width*height;
                int bytesPerStrip =
                    ((sampleSize+7)/8)*numBands*width*tileHeight;
                int cumulativeBytes = 0;
                tileByteCounts = new long[tileOffsets.length];
                for(int i = 0; i < tileOffsets.length; i++) {
                    tileByteCounts[i] =
                        Math.min(totalBytes - cumulativeBytes,
                                 bytesPerStrip);
                    cumulativeBytes += bytesPerStrip;
                }

                if(compression != COMP_NONE) {
                    // Replace the stream with one that will not throw
                    // an EOFException when it runs past the end.
                    this.stream = new NoEOFStream(stream);
                }
            } else {
                tileByteCounts = getFieldAsLongs(tileByteCountsField);
            }

            // Uncompressed image provided in a single tile: clamp to max bytes.
            int maxBytes = width*height*numBands*((sampleSize + 7)/8);
            if(tileByteCounts.length == 1 &&
               compression == COMP_NONE &&
               tileByteCounts[0] > maxBytes) {
                tileByteCounts[0] = maxBytes;
            }
  }

  // Calculate number of tiles and the tileSize in bytes
        tilesX = (width + tileWidth - 1)/tileWidth;
        tilesY = (height + tileHeight - 1)/tileHeight;
        tileSize = tileWidth * tileHeight * numBands;

  // Check whether big endian or little endian format is used.
  isBigEndian = dir.isBigEndian();

  TIFFField fillOrderField =
      dir.getField(TIFFImageDecoder.TIFF_FILL_ORDER);
  if (fillOrderField != null) {
      fillOrder = fillOrderField.getAsInt(0);
  } else {
      // Default Fill Order
      fillOrder = 1;
  }

  switch(compression) {
        case COMP_NONE:
        case COMP_PACKBITS:
            // Do nothing.
            break;
        case COMP_DEFLATE:
            inflater = new Inflater();
            break;
        case COMP_FAX_G3_1D:
        case COMP_FAX_G3_2D:
        case COMP_FAX_G4_2D:
            if(sampleSize != 1) {
                throw new RuntimeException(JaiI18N.getString("TIFFImage7"));
            }

            // Fax T.4 compression options
            if (compression == 3) {
                TIFFField t4OptionsField =
                    dir.getField(TIFFImageDecoder.TIFF_T4_OPTIONS);
                if (t4OptionsField != null) {
                    tiffT4Options = t4OptionsField.getAsLong(0);
                } else {
                    // Use default value
                    tiffT4Options = 0;
                }
            }

            // Fax T.6 compression options
            if (compression == 4) {
                TIFFField t6OptionsField =
                    dir.getField(TIFFImageDecoder.TIFF_T6_OPTIONS);
                if (t6OptionsField != null) {
                    tiffT6Options = t6OptionsField.getAsLong(0);
                } else {
                    // Use default value
                    tiffT6Options = 0;
                }
            }

            // Fax encoding, need to create the Fax decoder.
            decoder = new TIFFFaxDecoder(fillOrder,
                                         tileWidth, tileHeight);
            break;

        case COMP_LZW:
            // LZW compression used, need to create the LZW decoder.
            TIFFField predictorField =
                dir.getField(TIFFImageDecoder.TIFF_PREDICTOR);

            if (predictorField == null) {
                predictor = 1;
            } else {
                predictor = predictorField.getAsInt(0);

                if (predictor != 1 && predictor != 2) {
                    throw new RuntimeException(JaiI18N.getString("TIFFImage8"));
                }

                if (predictor == 2 && sampleSize != 8) {
                    throw new RuntimeException(sampleSize +
                                               JaiI18N.getString("TIFFImage9"));
                }
            }

            lzwDecoder = new TIFFLZWDecoder(tileWidth, predictor,
                                            samplesPerPixel);
            break;

        case COMP_JPEG_OLD:
            throw new RuntimeException(JaiI18N.getString("TIFFImage15"));

        case COMP_JPEG_TTN2:
            if(!(sampleSize == 8 &&
                 ((imageType == TYPE_GRAY && samplesPerPixel == 1) ||
                  (imageType == TYPE_PALETTE && samplesPerPixel == 1) ||
                  (imageType == TYPE_RGB && samplesPerPixel == 3)))) {
                throw new RuntimeException(JaiI18N.getString("TIFFImage16"));
            }

            // Create decodeParam from JPEGTables field if present.
            if(dir.isTagPresent(TIFF_JPEG_TABLES)) {
                TIFFField jpegTableField = dir.getField(TIFF_JPEG_TABLES);
                byte[] jpegTable = jpegTableField.getAsBytes();
                ByteArrayInputStream tableStream =
                    new ByteArrayInputStream(jpegTable);
                JPEGImageDecoder decoder =
                    JPEGCodec.createJPEGDecoder(tableStream);
View Full Code Here

Examples of com.sun.media.jai.codec.TIFFDirectory

     * Reads a private IFD from a given offset in the stream.  This
     * method may be used to obtain IFDs that are referenced
     * only by private tag values.
     */
    public TIFFDirectory getPrivateIFD(long offset) throws IOException {
        return new TIFFDirectory(stream, offset, 0);
    }
View Full Code Here

Examples of com.sun.media.jai.codec.TIFFDirectory

/*  312 */       param = new TIFFDecodeParam();
/*      */     }
/*      */
/*  315 */     this.decodePaletteAsShorts = param.getDecodePaletteAsShorts();
/*      */
/*  318 */     TIFFDirectory dir = param.getIFDOffset() == null ? new TIFFDirectory(stream, directory) : new TIFFDirectory(stream, param.getIFDOffset().longValue(), directory);
/*      */
/*  324 */     this.properties.put("tiff_directory", dir);
/*      */
/*  327 */     TIFFField sfield = dir.getField(277);
/*      */
/*  329 */     int samplesPerPixel = sfield == null ? 1 : (int)sfield.getAsLong(0);
/*      */
/*  332 */     TIFFField planarConfigurationField = dir.getField(284);
/*      */
/*  334 */     char[] planarConfiguration = planarConfigurationField == null ? new char[] { '\001' } : planarConfigurationField.getAsChars();
/*      */
/*  339 */     if ((planarConfiguration[0] != '\001') && (samplesPerPixel != 1)) {
/*  340 */       throw new RuntimeException(JaiI18N.getString("TIFFImage0"));
/*      */     }
/*      */
/*  344 */     TIFFField bitsField = dir.getField(258);
/*      */
/*  346 */     char[] bitsPerSample = null;
/*  347 */     if (bitsField != null) {
/*  348 */       bitsPerSample = bitsField.getAsChars();
/*      */     } else {
/*  350 */       bitsPerSample = new char[] { '\001' };
/*      */
/*  353 */       for (int i = 1; i < bitsPerSample.length; i++) {
/*  354 */         if (bitsPerSample[i] != bitsPerSample[0]) {
/*  355 */           throw new RuntimeException(JaiI18N.getString("TIFFImage1"));
/*      */         }
/*      */       }
/*      */     }
/*      */
/*  360 */     this.sampleSize = bitsPerSample[0];
/*      */
/*  364 */     TIFFField sampleFormatField = dir.getField(339);
/*      */
/*  367 */     char[] sampleFormat = null;
/*  368 */     if (sampleFormatField != null) {
/*  369 */       sampleFormat = sampleFormatField.getAsChars();
/*      */
/*  372 */       for (int l = 1; l < sampleFormat.length; l++) {
/*  373 */         if (sampleFormat[l] != sampleFormat[0]) {
/*  374 */           throw new RuntimeException(JaiI18N.getString("TIFFImage2"));
/*      */         }
/*      */       }
/*      */     }
/*      */     else
/*      */     {
/*  380 */       sampleFormat = new char[] { '\001' };
/*      */     }
/*      */
/*  384 */     boolean isValidDataFormat = false;
/*  385 */     switch (this.sampleSize) {
/*      */     case 1:
/*      */     case 4:
/*      */     case 8:
/*  389 */       if (sampleFormat[0] != '\003')
/*      */       {
/*  391 */         this.dataType = 0;
/*  392 */         isValidDataFormat = true; } break;
/*      */     case 16:
/*  396 */       if (sampleFormat[0] != '\003') {
/*  397 */         this.dataType = (sampleFormat[0] == '\002' ? 2 : 1);
/*      */
/*  399 */         isValidDataFormat = true; } break;
/*      */     case 32:
/*  403 */       this.dataType = (sampleFormat[0] == '\003' ? 4 : 3);
/*      */
/*  405 */       isValidDataFormat = true;
/*      */     }
/*      */
/*  409 */     if (!isValidDataFormat) {
/*  410 */       throw new RuntimeException(JaiI18N.getString("TIFFImage3"));
/*      */     }
/*      */
/*  414 */     TIFFField compField = dir.getField(259);
/*  415 */     this.compression = (compField == null ? 1 : compField.getAsInt(0));
/*      */
/*  418 */     TIFFField photoInterpField = dir.getField(262);
/*      */     int photometricType;
/*      */     int photometricType;
/*  423 */     if (photoInterpField != null)
/*      */     {
/*  425 */       photometricType = (int)photoInterpField.getAsLong(0);
/*      */     }
/*      */     else
/*      */     {
/*      */       int photometricType;
/*  429 */       if (dir.getField(320) != null)
/*      */       {
/*  431 */         photometricType = 3;
/*      */       }
/*      */       else
/*      */       {
/*      */         int photometricType;
/*  432 */         if (this.sampleSize == 1)
/*      */         {
/*      */           int photometricType;
/*  435 */           if ((this.compression == 2) || (this.compression == 3) || (this.compression == 4))
/*      */           {
/*  438 */             photometricType = 0;
/*      */           }
/*  440 */           else photometricType = 1;
/*      */         }
/*      */         else
/*      */         {
/*      */           int photometricType;
/*  442 */           if ((samplesPerPixel == 3) || (samplesPerPixel == 4))
/*      */           {
/*  444 */             photometricType = 2;
/*      */           }
/*      */           else {
/*  447 */             photometricType = 1;
/*      */           }
/*      */         }
/*      */       }
/*      */     }
/*  452 */     this.imageType = -1;
/*  453 */     switch (photometricType) {
/*      */     case 0:
/*  455 */       this.isWhiteZero = true;
/*      */     case 1:
/*  457 */       if ((this.sampleSize == 1) && (samplesPerPixel == 1))
/*  458 */         this.imageType = 0;
/*  459 */       else if ((this.sampleSize == 4) && (samplesPerPixel == 1))
/*  460 */         this.imageType = 1;
/*  461 */       else if (this.sampleSize % 8 == 0)
/*  462 */         if (samplesPerPixel == 1)
/*  463 */           this.imageType = 2;
/*  464 */         else if (samplesPerPixel == 2)
/*  465 */           this.imageType = 3;
/*      */         else
/*  467 */           this.imageType = 8;
/*  467 */       break;
/*      */     case 2:
/*  472 */       if (this.sampleSize % 8 == 0)
/*  473 */         if (samplesPerPixel == 3)
/*  474 */           this.imageType = 5;
/*  475 */         else if (samplesPerPixel == 4)
/*  476 */           this.imageType = 6;
/*      */         else
/*  478 */           this.imageType = 8;
/*  478 */       break;
/*      */     case 3:
/*  483 */       if ((samplesPerPixel == 1) && ((this.sampleSize == 4) || (this.sampleSize == 8) || (this.sampleSize == 16)))
/*      */       {
/*  485 */         this.imageType = 4; } break;
/*      */     case 4:
/*  489 */       if ((this.sampleSize == 1) && (samplesPerPixel == 1))
/*  490 */         this.imageType = 0; break;
/*      */     case 6:
/*  494 */       if ((this.compression == 7) && (this.sampleSize == 8) && (samplesPerPixel == 3))
/*      */       {
/*  497 */         this.colorConvertJPEG = param.getJPEGDecompressYCbCrToRGB();
/*      */
/*  500 */         this.imageType = (this.colorConvertJPEG ? 5 : 8);
/*      */       } else {
/*  502 */         TIFFField chromaField = dir.getField(530);
/*  503 */         if (chromaField != null) {
/*  504 */           this.chromaSubH = chromaField.getAsInt(0);
/*  505 */           this.chromaSubV = chromaField.getAsInt(1);
/*      */         } else {
/*  507 */           this.chromaSubH = (this.chromaSubV = 2);
/*      */         }
/*      */
/*  510 */         if (this.chromaSubH * this.chromaSubV == 1)
/*  511 */           this.imageType = 8;
/*  512 */         else if ((this.sampleSize == 8) && (samplesPerPixel == 3)) {
/*  513 */           this.imageType = 7;
/*      */         }
/*      */       }
/*  516 */       break;
/*      */     case 5:
/*      */     default:
/*  518 */       if (this.sampleSize % 8 == 0) {
/*  519 */         this.imageType = 8;
/*      */       }
/*      */       break;
/*      */     }
/*      */
/*  524 */     if (this.imageType == -1) {
/*  525 */       throw new RuntimeException(JaiI18N.getString("TIFFImage4"));
/*      */     }
/*      */
/*  529 */     this.minX = (this.minY = 0);
/*  530 */     this.width = ((int)getField(dir, 256, "Image Width").getAsLong(0));
/*      */
/*  534 */     this.height = ((int)getField(dir, 257, "Image Length").getAsLong(0));
/*      */
/*  539 */     this.numBands = samplesPerPixel;
/*      */
/*  542 */     TIFFField efield = dir.getField(338);
/*  543 */     int extraSamples = efield == null ? 0 : (int)efield.getAsLong(0);
/*      */
/*  545 */     if (dir.getField(324) != null)
/*      */     {
/*  547 */       this.isTiled = true;
/*      */
/*  549 */       this.tileWidth = ((int)getField(dir, 322, "Tile Width").getAsLong(0));
/*      */
/*  552 */       this.tileHeight = ((int)getField(dir, 323, "Tile Length").getAsLong(0));
/*      */
/*  555 */       this.tileOffsets = getField(dir, 324, "Tile Offsets").getAsLongs();
/*      */
/*  560 */       this.tileByteCounts = getFieldAsLongs(getField(dir, 325, "Tile Byte Counts"));
/*      */     }
/*      */     else
/*      */     {
/*  568 */       this.isTiled = false;
/*      */
/*  574 */       this.tileWidth = (dir.getField(322) != null ? (int)dir.getFieldAsLong(322) : this.width);
/*      */
/*  578 */       TIFFField field = dir.getField(278);
/*      */
/*  580 */       if (field == null)
/*      */       {
/*  583 */         this.tileHeight = (dir.getField(323) != null ? (int)dir.getFieldAsLong(323) : this.height);
/*      */       }
/*      */       else
/*      */       {
/*  588 */         long l = field.getAsLong(0);
/*  589 */         long infinity = 1L;
/*  590 */         infinity = (infinity << 32) - 1L;
/*  591 */         if ((l == infinity) || (l > this.height))
/*      */         {
/*  595 */           this.tileHeight = this.height;
/*      */         }
/*  597 */         else this.tileHeight = ((int)l);
/*      */
/*      */       }
/*      */
/*  601 */       TIFFField tileOffsetsField = getField(dir, 273, "Strip Offsets");
/*      */
/*  605 */       this.tileOffsets = getFieldAsLongs(tileOffsetsField);
/*      */
/*  607 */       TIFFField tileByteCountsField = dir.getField(279);
/*      */
/*  609 */       if (tileByteCountsField == null) {
/*  610 */         if (this.compression == 1)
/*      */         {
/*  612 */           int totalBytes = (this.sampleSize + 7) / 8 * this.numBands * this.width * this.height;
/*  613 */           int bytesPerStrip = (this.sampleSize + 7) / 8 * this.numBands * this.width * this.tileHeight;
/*      */
/*  615 */           int cumulativeBytes = 0;
/*  616 */           this.tileByteCounts = new long[this.tileOffsets.length];
/*  617 */           for (int i = 0; i < this.tileOffsets.length; i++) {
/*  618 */             this.tileByteCounts[i] = Math.min(totalBytes - cumulativeBytes, bytesPerStrip);
/*      */
/*  621 */             cumulativeBytes += bytesPerStrip;
/*      */           }
/*      */         }
/*      */         else
/*      */         {
/*  626 */           tileByteCountsField = getField(dir, 279, "Strip Byte Counts");
/*      */         }
/*      */
/*      */       }
/*      */       else
/*      */       {
/*  632 */         this.tileByteCounts = getFieldAsLongs(tileByteCountsField);
/*      */       }
/*      */
/*  636 */       int maxBytes = this.width * this.height * this.numBands * ((this.sampleSize + 7) / 8);
/*  637 */       if ((this.tileByteCounts.length == 1) && (this.compression == 1) && (this.tileByteCounts[0] > maxBytes))
/*      */       {
/*  640 */         this.tileByteCounts[0] = maxBytes;
/*      */       }
/*      */
/*      */     }
/*      */
/*  645 */     this.tilesX = ((this.width + this.tileWidth - 1) / this.tileWidth);
/*  646 */     this.tilesY = ((this.height + this.tileHeight - 1) / this.tileHeight);
/*  647 */     this.tileSize = (this.tileWidth * this.tileHeight * this.numBands);
/*      */
/*  650 */     this.isBigEndian = dir.isBigEndian();
/*      */
/*  652 */     TIFFField fillOrderField = dir.getField(266);
/*      */
/*  654 */     if (fillOrderField != null) {
/*  655 */       this.fillOrder = fillOrderField.getAsInt(0);
/*      */     }
/*      */     else {
/*  658 */       this.fillOrder = 1;
/*      */     }
/*      */
/*  661 */     switch (this.compression)
/*      */     {
/*      */     case 1:
/*      */     case 32773:
/*  665 */       break;
/*      */     case 32946:
/*  667 */       this.inflater = new Inflater();
/*  668 */       break;
/*      */     case 2:
/*      */     case 3:
/*      */     case 4:
/*  672 */       if (this.sampleSize != 1) {
/*  673 */         throw new RuntimeException(JaiI18N.getString("TIFFImage7"));
/*      */       }
/*      */
/*  677 */       if (this.compression == 3) {
/*  678 */         TIFFField t4OptionsField = dir.getField(292);
/*      */
/*  680 */         if (t4OptionsField != null) {
/*  681 */           this.tiffT4Options = t4OptionsField.getAsLong(0);
/*      */         }
/*      */         else {
/*  684 */           this.tiffT4Options = 0L;
/*      */         }
/*      */
/*      */       }
/*      */
/*  689 */       if (this.compression == 4) {
/*  690 */         TIFFField t6OptionsField = dir.getField(293);
/*      */
/*  692 */         if (t6OptionsField != null) {
/*  693 */           this.tiffT6Options = t6OptionsField.getAsLong(0);
/*      */         }
/*      */         else {
/*  696 */           this.tiffT6Options = 0L;
/*      */         }
/*      */
/*      */       }
/*      */
/*  701 */       this.decoder = new TIFFFaxDecoder(this.fillOrder, this.tileWidth, this.tileHeight);
/*      */
/*  703 */       break;
/*      */     case 5:
/*  707 */       TIFFField predictorField = dir.getField(317);
/*      */
/*  710 */       if (predictorField == null) {
/*  711 */         this.predictor = 1;
/*      */       } else {
/*  713 */         this.predictor = predictorField.getAsInt(0);
/*      */
/*  715 */         if ((this.predictor != 1) && (this.predictor != 2)) {
/*  716 */           throw new RuntimeException(JaiI18N.getString("TIFFImage8"));
/*      */         }
/*      */
/*  719 */         if ((this.predictor == 2) && (this.sampleSize != 8)) {
/*  720 */           throw new RuntimeException(this.sampleSize + JaiI18N.getString("TIFFImage9"));
/*      */         }
/*      */
/*      */       }
/*      */
/*  725 */       this.lzwDecoder = new TIFFLZWDecoder(this.tileWidth, this.predictor, samplesPerPixel);
/*      */
/*  727 */       break;
/*      */     case 6:
/*  730 */       throw new RuntimeException(JaiI18N.getString("TIFFImage15"));
/*      */     case 7:
/*  733 */       if ((this.sampleSize != 8) || (((this.imageType != 2) || (samplesPerPixel != 1)) && ((this.imageType != 4) || (samplesPerPixel != 1)) && ((this.imageType != 5) || (samplesPerPixel != 3))))
/*      */       {
/*  737 */         throw new RuntimeException(JaiI18N.getString("TIFFImage16"));
/*      */       }
/*      */
/*  741 */       if (dir.isTagPresent(347)) {
/*  742 */         TIFFField jpegTableField = dir.getField(347);
/*  743 */         byte[] jpegTable = jpegTableField.getAsBytes();
/*  744 */         ByteArrayInputStream tableStream = new ByteArrayInputStream(jpegTable);
/*      */
/*  746 */         JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(tableStream);
/*      */
View Full Code Here

Examples of com.sun.media.jai.codec.TIFFDirectory

/*      */   }
/*      */
/*      */   public TIFFDirectory getPrivateIFD(long offset)
/*      */     throws IOException
/*      */   {
/*  952 */     return new TIFFDirectory(this.stream, offset, 0);
/*      */   }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.