/**
   * Constructor creates an instance to be used for fill operations.
   *
   * @param shading the shading type to be used
   * @param colorModel the color model to be used
   * @param xform transformation for user to device space
   * @param matrix the pattern matrix concatenated with that of the parent content stream
   * @param deviceBounds the bounds of the area to paint, in device units
   * @throws IOException if there is an error getting the color space or doing color conversion.
   */
  public AxialShadingContext(
      PDShadingType2 shading,
      ColorModel colorModel,
      AffineTransform xform,
      Matrix matrix,
      Rectangle deviceBounds)
      throws IOException {
    super(shading, colorModel, xform, matrix);
    this.axialShadingType = shading;
    coords = shading.getCoords().toFloatArray();

    // domain values
    if (shading.getDomain() != null) {
      domain = shading.getDomain().toFloatArray();
    } else {
      // set default values
      domain = new float[] {0, 1};
    }
    // extend values
    COSArray extendValues = shading.getExtend();
    if (shading.getExtend() != null) {
      extend = new boolean[2];
      extend[0] = ((COSBoolean) extendValues.get(0)).getValue();
      extend[1] = ((COSBoolean) extendValues.get(1)).getValue();
    } else {
      // set default values
      extend = new boolean[] {false, false};
    }
    // calculate some constants to be used in getRaster
    x1x0 = coords[2] - coords[0];
    y1y0 = coords[3] - coords[1];
    d1d0 = domain[1] - domain[0];
    denom = Math.pow(x1x0, 2) + Math.pow(y1y0, 2);

    try {
      // get inverse transform to be independent of current user / device space
      // when handling actual pixels in getRaster()
      rat = matrix.createAffineTransform().createInverse();
      rat.concatenate(xform.createInverse());
    } catch (NoninvertibleTransformException ex) {
      LOG.error(ex, ex);
    }

    // shading space -> device space
    AffineTransform shadingToDevice = (AffineTransform) xform.clone();
    shadingToDevice.concatenate(matrix.createAffineTransform());

    // worst case for the number of steps is opposite diagonal corners, so use that
    double dist =
        Math.sqrt(
            Math.pow(deviceBounds.getMaxX() - deviceBounds.getMinX(), 2)
                + Math.pow(deviceBounds.getMaxY() - deviceBounds.getMinY(), 2));
    factor = (int) Math.ceil(dist);

    // build the color table for the given number of steps
    colorTable = calcColorTable();
  }
  /**
   * Constructor creates an instance to be used for fill operations.
   *
   * @param shading the shading type to be used
   * @param colorModel the color model to be used
   * @param xform transformation for user to device space
   * @param ctm the transformation matrix
   * @param dBounds device bounds
   * @param pageHeight height of the current page
   */
  public RadialShadingContext(
      PDShadingType3 shading,
      ColorModel colorModel,
      AffineTransform xform,
      Matrix ctm,
      int pageHeight,
      Rectangle dBounds)
      throws IOException {
    super(shading, colorModel, xform, ctm, pageHeight, dBounds);
    this.radialShadingType = shading;
    coords = shading.getCoords().toFloatArray();

    if (ctm != null) {
      // the shading is used in combination with the sh-operator
      // transform the coords from shading to user space
      ctm.createAffineTransform().transform(coords, 0, coords, 0, 1);
      ctm.createAffineTransform().transform(coords, 3, coords, 3, 1);
      // scale radius to user space
      coords[2] *= ctm.getXScale();
      coords[5] *= ctm.getXScale();

      // move the 0,0-reference
      coords[1] = pageHeight - coords[1];
      coords[4] = pageHeight - coords[4];
    } else {
      // the shading is used as pattern colorspace in combination
      // with a fill-, stroke- or showText-operator
      float translateY = (float) xform.getTranslateY();
      // move the 0,0-reference including the y-translation from user to device space
      coords[1] = pageHeight + translateY - coords[1];
      coords[4] = pageHeight + translateY - coords[4];
    }

    // transform the coords from user to device space
    xform.transform(coords, 0, coords, 0, 1);
    xform.transform(coords, 3, coords, 3, 1);

    // scale radius to device space
    coords[2] *= xform.getScaleX();
    coords[5] *= xform.getScaleX();

    // a radius is always positive
    coords[2] = Math.abs(coords[2]);
    coords[5] = Math.abs(coords[5]);

    // domain values
    if (this.radialShadingType.getDomain() != null) {
      domain = shading.getDomain().toFloatArray();
    } else {
      // set default values
      domain = new float[] {0, 1};
    }

    // extend values
    COSArray extendValues = shading.getExtend();
    if (shading.getExtend() != null) {
      extend = new boolean[2];
      extend[0] = ((COSBoolean) extendValues.get(0)).getValue();
      extend[1] = ((COSBoolean) extendValues.get(1)).getValue();
    } else {
      // set default values
      extend = new boolean[] {false, false};
    }
    // calculate some constants to be used in getRaster
    x1x0 = coords[3] - coords[0];
    y1y0 = coords[4] - coords[1];
    r1r0 = coords[5] - coords[2];
    x1x0pow2 = Math.pow(x1x0, 2);
    y1y0pow2 = Math.pow(y1y0, 2);
    r0pow2 = Math.pow(coords[2], 2);
    denom = x1x0pow2 + y1y0pow2 - Math.pow(r1r0, 2);
    d1d0 = domain[1] - domain[0];

    // get background values if available
    COSArray bg = shading.getBackground();
    if (bg != null) {
      background = bg.toFloatArray();
      rgbBackground = convertToRGB(background);
    }
    longestDistance = getLongestDis();
    colorTable = calcColorTable();
  }
 /**
  * This will get a stream (or the reference to a stream) from the array.
  *
  * @param index The index of the requested stream
  * @return The stream object or a reference to a stream
  */
 public COSBase get(int index) {
   return streams.get(index);
 }