/** * 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(); }
/** * Calculate the color on the axial line and store them in an array. * * @return an array, index denotes the relative position, the corresponding value is the color on * the axial line * @throws IOException if the color conversion fails. */ private int[] calcColorTable() throws IOException { int[] map = new int[factor + 1]; if (factor == 0 || d1d0 == 0) { float[] values = axialShadingType.evalFunction(domain[0]); map[0] = convertToRGB(values); } else { for (int i = 0; i <= factor; i++) { float t = domain[0] + d1d0 * i / factor; float[] values = axialShadingType.evalFunction(t); map[i] = convertToRGB(values); } } return map; }
/** * Returns the function. * * @throws java.io.IOException if we were not able to create the function. */ public PDFunction getFunction() throws IOException { return axialShadingType.getFunction(); }