Beispiel #1
0
  private boolean needToCreateIndex(RenderedImage image) {

    SampleModel sampleModel = image.getSampleModel();
    ColorModel colorModel = image.getColorModel();

    return sampleModel.getNumBands() != 1
        || sampleModel.getSampleSize()[0] > 8
        || colorModel.getComponentSize()[0] > 8;
  }
  /**
   * Constructs a <code>ChannelDefinitionBox</code> based on the provided <code>ColorModel</code>.
   */
  public ChannelDefinitionBox(ColorModel colorModel) {
    super(computeLength(colorModel), BOX_TYPE, null);

    // creates the buffers for the channel definitions.
    short length = (short) (colorModel.getComponentSize().length - 1);
    num = (short) (length * (colorModel.isAlphaPremultiplied() ? 3 : 2));
    channels = new short[num];
    types = new int[num];
    associations = new short[num];

    // fills the arrays.
    fillBasedOnBands(length, colorModel.isAlphaPremultiplied(), channels, types, associations);
  }
  public static void main(String args[]) {
    ColorModel cm =
        new ColorModel(32) {
          public int getAlpha(int pixel) {
            return 255;
          }

          public int getBlue(int pixel) {
            return 255;
          }

          public int getGreen(int pixel) {
            return 255;
          }

          public int getRed(int pixel) {
            return 255;
          }
        };

    cm.hasAlpha();
    cm.isAlphaPremultiplied();
    cm.getTransferType();
    cm.getPixelSize();
    cm.getComponentSize();
    cm.getComponentSize();
    cm.getTransparency();
    cm.getNumComponents();
    cm.getNumColorComponents();
    cm.getRed(20);
    cm.getGreen(20);
    cm.getBlue(20);
    cm.getAlpha(20);
    cm.getRGB(20);
    cm.isAlphaPremultiplied();
    cm.isAlphaPremultiplied();

    cm = ColorModel.getRGBdefault();
  }
 /** Computes the length of this box from the provided <code>ColorModel</code>. */
 private static int computeLength(ColorModel colorModel) {
   int length = colorModel.getComponentSize().length - 1;
   return 10 + (colorModel.isAlphaPremultiplied() ? length * 18 : length * 12);
 }
Beispiel #5
0
  public Iterator getImageTypes(int imageIndex) throws IIOException {
    List l; // List of ImageTypeSpecifiers

    Integer imageIndexInteger = new Integer(imageIndex);
    if (imageTypeMap.containsKey(imageIndexInteger)) {
      // Return the cached ITS List.
      l = (List) imageTypeMap.get(imageIndexInteger);
    } else {
      // Create a new ITS List.
      l = new ArrayList(1);

      // Create the ITS and cache if for later use so that this method
      // always returns an Iterator containing the same ITS objects.
      seekToImage(imageIndex);
      ImageTypeSpecifier itsRaw =
          TIFFDecompressor.getRawImageTypeSpecifier(
              photometricInterpretation,
              compression,
              samplesPerPixel,
              bitsPerSample,
              sampleFormat,
              extraSamples,
              colorMap);

      // Check for an ICCProfile field.
      TIFFField iccProfileField = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_ICC_PROFILE);

      // If an ICCProfile field is present change the ImageTypeSpecifier
      // to use it if the data layout is component type.
      if (iccProfileField != null && itsRaw.getColorModel() instanceof ComponentColorModel) {
        // Create a ColorSpace from the profile.
        byte[] iccProfileValue = iccProfileField.getAsBytes();
        ICC_Profile iccProfile = ICC_Profile.getInstance(iccProfileValue);
        ICC_ColorSpace iccColorSpace = new ICC_ColorSpace(iccProfile);

        // Get the raw sample and color information.
        ColorModel cmRaw = itsRaw.getColorModel();
        ColorSpace csRaw = cmRaw.getColorSpace();
        SampleModel smRaw = itsRaw.getSampleModel();

        // Get the number of samples per pixel and the number
        // of color components.
        int numBands = smRaw.getNumBands();
        int numComponents = iccColorSpace.getNumComponents();

        // Replace the ColorModel with the ICC ColorModel if the
        // numbers of samples and color components are amenable.
        if (numBands == numComponents || numBands == numComponents + 1) {
          // Set alpha flags.
          boolean hasAlpha = numComponents != numBands;
          boolean isAlphaPre = hasAlpha && cmRaw.isAlphaPremultiplied();

          // Create a ColorModel of the same class and with
          // the same transfer type.
          ColorModel iccColorModel =
              new ComponentColorModel(
                  iccColorSpace,
                  cmRaw.getComponentSize(),
                  hasAlpha,
                  isAlphaPre,
                  cmRaw.getTransparency(),
                  cmRaw.getTransferType());

          // Prepend the ICC profile-based ITS to the List. The
          // ColorModel and SampleModel are guaranteed to be
          // compatible as the old and new ColorModels are both
          // ComponentColorModels with the same transfer type
          // and the same number of components.
          l.add(new ImageTypeSpecifier(iccColorModel, smRaw));

          // Append the raw ITS to the List if and only if its
          // ColorSpace has the same type and number of components
          // as the ICC ColorSpace.
          if (csRaw.getType() == iccColorSpace.getType()
              && csRaw.getNumComponents() == iccColorSpace.getNumComponents()) {
            l.add(itsRaw);
          }
        } else { // ICCProfile not compatible with SampleModel.
          // Append the raw ITS to the List.
          l.add(itsRaw);
        }
      } else { // No ICCProfile field or raw ColorModel not component.
        // Append the raw ITS to the List.
        l.add(itsRaw);
      }

      // Cache the ITS List.
      imageTypeMap.put(imageIndexInteger, l);
    }

    return l.iterator();
  }
Beispiel #6
0
  /**
   * Writes any extension blocks, the Image Descriptor, the image data, and optionally the header
   * (Signature and Logical Screen Descriptor) and trailer (Block Terminator).
   *
   * @param writeHeader Whether to write the header.
   * @param writeTrailer Whether to write the trailer.
   * @param sm The stream metadata or <code>null</code> if <code>writeHeader</code> is <code>false
   *     </code>.
   * @param iioimage The image and image metadata.
   * @param p The write parameters.
   * @throws IllegalArgumentException if the number of bands is not 1.
   * @throws IllegalArgumentException if the number of bits per sample is greater than 8.
   * @throws IllegalArgumentException if the color component size is greater than 8.
   * @throws IllegalArgumentException if <code>writeHeader</code> is <code>true</code> and <code>sm
   *     </code> is <code>null</code>.
   * @throws IllegalArgumentException if <code>writeHeader</code> is <code>false</code> and a
   *     sequence is not being written.
   */
  private void write(
      boolean writeHeader,
      boolean writeTrailer,
      IIOMetadata sm,
      IIOImage iioimage,
      ImageWriteParam p)
      throws IOException {
    clearAbortRequest();

    RenderedImage image = iioimage.getRenderedImage();

    // Check for ability to encode image.
    if (needToCreateIndex(image)) {
      image = PaletteBuilder.createIndexedImage(image);
      iioimage.setRenderedImage(image);
    }

    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    // Determine source region and destination dimensions.
    Rectangle sourceBounds =
        new Rectangle(image.getMinX(), image.getMinY(), image.getWidth(), image.getHeight());
    Dimension destSize = new Dimension();
    computeRegions(sourceBounds, destSize, p);

    // Convert any provided image metadata.
    GIFWritableImageMetadata imageMetadata = null;
    if (iioimage.getMetadata() != null) {
      imageMetadata = new GIFWritableImageMetadata();
      convertMetadata(IMAGE_METADATA_NAME, iioimage.getMetadata(), imageMetadata);
      // Converted rgb image can use palette different from global.
      // In order to avoid color artefacts we want to be sure we use
      // appropriate palette. For this we initialize local color table
      // from current color and sample models.
      // At this point we can guarantee that local color table can be
      // build because image was already converted to indexed or
      // gray-scale representations
      if (imageMetadata.localColorTable == null) {
        imageMetadata.localColorTable = createColorTable(colorModel, sampleModel);

        // in case of indexed image we should take care of
        // transparent pixels
        if (colorModel instanceof IndexColorModel) {
          IndexColorModel icm = (IndexColorModel) colorModel;
          int index = icm.getTransparentPixel();
          imageMetadata.transparentColorFlag = (index != -1);
          if (imageMetadata.transparentColorFlag) {
            imageMetadata.transparentColorIndex = index;
          }
          /* NB: transparentColorFlag might have not beed reset for
             greyscale images but explicitly reseting it here
             is potentially not right thing to do until we have way
             to find whether current value was explicitly set by
             the user.
          */
        }
      }
    }

    // Global color table values.
    byte[] globalColorTable = null;

    // Write the header (Signature+Logical Screen Descriptor+
    // Global Color Table).
    if (writeHeader) {
      if (sm == null) {
        throw new IllegalArgumentException("Cannot write null header!");
      }

      GIFWritableStreamMetadata streamMetadata = (GIFWritableStreamMetadata) sm;

      // Set the version if not set.
      if (streamMetadata.version == null) {
        streamMetadata.version = "89a";
      }

      // Set the Logical Screen Desriptor if not set.
      if (streamMetadata.logicalScreenWidth == GIFMetadata.UNDEFINED_INTEGER_VALUE) {
        streamMetadata.logicalScreenWidth = destSize.width;
      }

      if (streamMetadata.logicalScreenHeight == GIFMetadata.UNDEFINED_INTEGER_VALUE) {
        streamMetadata.logicalScreenHeight = destSize.height;
      }

      if (streamMetadata.colorResolution == GIFMetadata.UNDEFINED_INTEGER_VALUE) {
        streamMetadata.colorResolution =
            colorModel != null ? colorModel.getComponentSize()[0] : sampleModel.getSampleSize()[0];
      }

      // Set the Global Color Table if not set, i.e., if not
      // provided in the stream metadata.
      if (streamMetadata.globalColorTable == null) {
        if (isWritingSequence && imageMetadata != null && imageMetadata.localColorTable != null) {
          // Writing a sequence and a local color table was
          // provided in the metadata of the first image: use it.
          streamMetadata.globalColorTable = imageMetadata.localColorTable;
        } else if (imageMetadata == null || imageMetadata.localColorTable == null) {
          // Create a color table.
          streamMetadata.globalColorTable = createColorTable(colorModel, sampleModel);
        }
      }

      // Set the Global Color Table. At this point it should be
      // A) the global color table provided in stream metadata, if any;
      // B) the local color table of the image metadata, if any, if
      //    writing a sequence;
      // C) a table created on the basis of the first image ColorModel
      //    and SampleModel if no local color table is available; or
      // D) null if none of the foregoing conditions obtain (which
      //    should only be if a sequence is not being written and
      //    a local color table is provided in image metadata).
      globalColorTable = streamMetadata.globalColorTable;

      // Write the header.
      int bitsPerPixel;
      if (globalColorTable != null) {
        bitsPerPixel = getNumBits(globalColorTable.length / 3);
      } else if (imageMetadata != null && imageMetadata.localColorTable != null) {
        bitsPerPixel = getNumBits(imageMetadata.localColorTable.length / 3);
      } else {
        bitsPerPixel = sampleModel.getSampleSize(0);
      }
      writeHeader(streamMetadata, bitsPerPixel);
    } else if (isWritingSequence) {
      globalColorTable = theStreamMetadata.globalColorTable;
    } else {
      throw new IllegalArgumentException("Must write header for single image!");
    }

    // Write extension blocks, Image Descriptor, and image data.
    writeImage(
        iioimage.getRenderedImage(), imageMetadata, p, globalColorTable, sourceBounds, destSize);

    // Write the trailer.
    if (writeTrailer) {
      writeTrailer();
    }
  }