You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2018/02/09 17:06:23 UTC
[08/14] pdfbox-jbig2 git commit: PDFBOX-4098: reformat the source
code, introduce an eclipse formatter definition
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/image/Resizer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/image/Resizer.java b/src/main/java/org/apache/pdfbox/jbig2/image/Resizer.java
index 3afc75f..157957f 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/image/Resizer.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/image/Resizer.java
@@ -23,382 +23,426 @@ import java.awt.image.WritableRaster;
import org.apache.pdfbox.jbig2.Bitmap;
import org.apache.pdfbox.jbig2.util.Utils;
-class Resizer {
+class Resizer
+{
- static final class Mapping {
- /** x and y scales */
- final double scale;
+ static final class Mapping
+ {
+ /** x and y scales */
+ final double scale;
- /** x and y offset used by MAP, private fields */
- final double offset = .5;
+ /** x and y offset used by MAP, private fields */
+ final double offset = .5;
- private final double a0;
- private final double b0;
+ private final double a0;
+ private final double b0;
- Mapping(double a0, double aw, double b0, double bw) {
- this.a0 = a0;
- this.b0 = b0;
- scale = bw / aw;
+ Mapping(double a0, double aw, double b0, double bw)
+ {
+ this.a0 = a0;
+ this.b0 = b0;
+ scale = bw / aw;
- if (scale <= 0.)
- throw new IllegalArgumentException("Negative scales are not allowed");
- }
+ if (scale <= 0.)
+ throw new IllegalArgumentException("Negative scales are not allowed");
+ }
- Mapping(double scaleX) {
- scale = scaleX;
- a0 = b0 = 0;
- }
+ Mapping(double scaleX)
+ {
+ scale = scaleX;
+ a0 = b0 = 0;
+ }
- double mapPixelCenter(final int b) {
- return (b + offset - b0) / scale + a0;
- }
+ double mapPixelCenter(final int b)
+ {
+ return (b + offset - b0) / scale + a0;
+ }
- double dstToSrc(final double b) {
- return (b - b0) / scale + a0;
- }
+ double dstToSrc(final double b)
+ {
+ return (b - b0) / scale + a0;
+ }
- double srcToDst(final double a) {
- return (a - a0) * scale + b0;
- }
- }
-
- /**
- * Order in which to apply filter
- */
- private enum Order {
- AUTO, XY, YX
- }
-
- /** Error tolerance */
- private static final double EPSILON = 1e-7;
-
- /** Number of bits in filter coefficients */
- private int weightBits = 14;
-
- private int weightOne = 1 << weightBits;
-
- /** Number of bits per channel */
- private int bitsPerChannel[] = new int[]{
- 8, 8, 8
- };
-
- private static final int NO_SHIFT[] = new int[16];
-
- private int finalShift[] = new int[]{
- 2 * weightBits - bitsPerChannel[0], 2 * weightBits - bitsPerChannel[1], 2 * weightBits - bitsPerChannel[2]
- };
-
- /**
- * Is x an integer?
- *
- * @param x the double to check
- * @return <code>true</code> if x is an integer, <code>false</code> if not.
- */
- private static boolean isInteger(final double x) {
- return Math.abs(x - Math.floor(x + .5)) < EPSILON;
- }
-
- static final boolean debug = false;
-
- /**
- * Should filters be simplified if possible?
- */
- private final boolean coerce = true;
-
- /**
- * The order in which data is processed.
- *
- * @see Order
- */
- private final Order order = Order.AUTO;
-
- /**
- * Should zeros be trimmed in x filter weight tables?
- */
- private final boolean trimZeros = true;
-
- private final Mapping mappingX;
- private final Mapping mappingY;
-
- /**
- * Creates an instance of {@link Resizer} with one scale factor for both x and y directions.
- *
- * @param scale the scale factor for x and y direction
- */
- public Resizer(double scale) {
- this(scale, scale);
- }
-
- /**
- * Creates an instance of {@link Resizer} with a scale factor for each direction.
- *
- * @param scaleX the scale factor for x direction
- * @param scaleY the scale factor for y direction
- */
- public Resizer(double scaleX, double scaleY) {
- mappingX = new Mapping(scaleX);
- mappingY = new Mapping(scaleY);
- }
-
- private Weighttab[] createXWeights(Rectangle srcBounds, final Rectangle dstBounds, final ParameterizedFilter filter) {
- final int srcX0 = srcBounds.x;
- final int srcX1 = srcBounds.x + srcBounds.width;
-
- final int dstX0 = dstBounds.x;
- final int dstX1 = dstBounds.x + dstBounds.width;
-
- final Weighttab tabs[] = new Weighttab[dstBounds.width];
- for (int dstX = dstX0; dstX < dstX1; dstX++) {
- final double center = mappingX.mapPixelCenter(dstX);
- tabs[dstX - dstX0] = new Weighttab(filter, weightOne, center, srcX0, srcX1 - 1, trimZeros);
+ double srcToDst(final double a)
+ {
+ return (a - a0) * scale + b0;
+ }
}
- return tabs;
- }
-
- /**
- * Checks if our discrete sampling of an arbitrary continuous filter, parameterized by the filter
- * spacing ({@link ParameterizedFilter#scale}), its radius ({@link ParameterizedFilter#support}),
- * and the scale and offset of the coordinate mapping, causes the filter to reduce to point
- * sampling.
- * <p>
- * It reduces if support is less than 1 pixel or if integer scale and translation, and filter is
- * cardinal.
- *
- * @param filter the parameterized filter instance to be simplified
- * @param scale the scale of the coordinate mapping
- * @param offset the offset of the coordinate mapping
- */
- private ParameterizedFilter simplifyFilter(final ParameterizedFilter filter, final double scale, final double offset) {
- if (coerce
- && (filter.support <= .5 || filter.filter.cardinal && isInteger(1. / filter.scale)
- && isInteger(1. / (scale * filter.scale)) && isInteger((offset / scale - .5) / filter.scale)))
- return new ParameterizedFilter(new Filter.Point(), 1., .5, 1);
-
- return filter;
- }
-
- /**
- * Filtered zoom, x direction filtering before y direction filtering
- * <p>
- * Note: when calling {@link Resizer#createXWeights(Rectangle, Rectangle, ParameterizedFilter)},
- * we can trim leading and trailing zeros from the x weight buffers as an optimization, but not
- * for y weight buffers since the split formula is anticipating a constant amount of buffering of
- * source scanlines; trimming zeros in y weight could cause feedback.
- */
- private void resizeXfirst(final Object src, final Rectangle srcBounds, final Object dst, final Rectangle dstBounds,
- final ParameterizedFilter xFilter, final ParameterizedFilter yFilter) {
- // source scanline buffer
- final Scanline buffer = createScanline(src, dst, srcBounds.width);
-
- // accumulator buffer
- final Scanline accumulator = createScanline(src, dst, dstBounds.width);
-
- // a sampled filter for source pixels for each dest x position
- final Weighttab xWeights[] = createXWeights(srcBounds, dstBounds, xFilter);
-
- // Circular buffer of active lines
- final int yBufferSize = yFilter.width + 2;
- final Scanline lineBuffer[] = new Scanline[yBufferSize];
- for (int y = 0; y < yBufferSize; y++) {
- lineBuffer[y] = createScanline(src, dst, dstBounds.width);
- lineBuffer[y].y = -1; /* mark scanline as unread */
+ /**
+ * Order in which to apply filter
+ */
+ private enum Order
+ {
+ AUTO, XY, YX
}
- // range of source and destination scanlines in regions
- final int srcY0 = srcBounds.y;
- final int srcY1 = srcBounds.y + srcBounds.height;
- final int dstY0 = dstBounds.y;
- final int dstY1 = dstBounds.y + dstBounds.height;
+ /** Error tolerance */
+ private static final double EPSILON = 1e-7;
- int yFetched = -1; // used to assert no backtracking
+ /** Number of bits in filter coefficients */
+ private int weightBits = 14;
- // loop over dest scanlines
- for (int dstY = dstY0; dstY < dstY1; dstY++) {
- // a sampled filter for source pixels for each dest x position
- final Weighttab yWeight = new Weighttab(yFilter, weightOne, mappingY.mapPixelCenter(dstY), srcY0, srcY1 - 1, true);
+ private int weightOne = 1 << weightBits;
- accumulator.clear();
+ /** Number of bits per channel */
+ private int bitsPerChannel[] = new int[] { 8, 8, 8 };
- // loop over source scanlines that contribute to this dest scanline
- for (int srcY = yWeight.i0; srcY <= yWeight.i1; srcY++) {
- final Scanline srcBuffer = lineBuffer[srcY % yBufferSize];
+ private static final int NO_SHIFT[] = new int[16];
- if (debug)
- System.out.println(" abuf.y / ayf " + srcBuffer.y + " / " + srcY);
+ private int finalShift[] = new int[] { 2 * weightBits - bitsPerChannel[0],
+ 2 * weightBits - bitsPerChannel[1], 2 * weightBits - bitsPerChannel[2] };
- if (srcBuffer.y != srcY) {
- // scanline needs to be fetched from src raster
- srcBuffer.y = srcY;
+ /**
+ * Is x an integer?
+ *
+ * @param x the double to check
+ * @return <code>true</code> if x is an integer, <code>false</code> if not.
+ */
+ private static boolean isInteger(final double x)
+ {
+ return Math.abs(x - Math.floor(x + .5)) < EPSILON;
+ }
- if (srcY0 + srcY <= yFetched)
- throw new AssertionError("Backtracking from line " + yFetched + " to " + (srcY0 + srcY));
+ static final boolean debug = false;
- buffer.fetch(srcBounds.x, srcY0 + srcY);
+ /**
+ * Should filters be simplified if possible?
+ */
+ private final boolean coerce = true;
- yFetched = srcY0 + srcY;
+ /**
+ * The order in which data is processed.
+ *
+ * @see Order
+ */
+ private final Order order = Order.AUTO;
- // filter it into the appropriate line of linebuf (xfilt)
- buffer.filter(NO_SHIFT, bitsPerChannel, xWeights, srcBuffer);
- }
+ /**
+ * Should zeros be trimmed in x filter weight tables?
+ */
+ private final boolean trimZeros = true;
- // add weighted tbuf into accum (these do yfilt)
- srcBuffer.accumulate(yWeight.weights[srcY - yWeight.i0], accumulator);
- }
+ private final Mapping mappingX;
+ private final Mapping mappingY;
- accumulator.shift(finalShift);
- accumulator.store(dstBounds.x, dstY);
- if (debug)
- System.out.printf("\n");
+ /**
+ * Creates an instance of {@link Resizer} with one scale factor for both x and y directions.
+ *
+ * @param scale the scale factor for x and y direction
+ */
+ public Resizer(double scale)
+ {
+ this(scale, scale);
}
- }
-
- /**
- * Filtered zoom, y direction filtering before x direction filtering
- * */
- private void resizeYfirst(final Object src, final Rectangle srcBounds, final Object dst, final Rectangle dstBounds,
- final ParameterizedFilter xFilter, final ParameterizedFilter yFilter) {
- // destination scanline buffer
- final Scanline buffer = createScanline(src, dst, dstBounds.width);
-
- // accumulator buffer
- final Scanline accumulator = createScanline(src, dst, srcBounds.width);
-
- // a sampled filter for source pixels for each destination x position
- final Weighttab xWeights[] = createXWeights(srcBounds, dstBounds, xFilter);
-
- // Circular buffer of active lines
- final int yBufferSize = yFilter.width + 2;
- final Scanline lineBuffer[] = new Scanline[yBufferSize];
- for (int y = 0; y < yBufferSize; y++) {
- lineBuffer[y] = createScanline(src, dst, srcBounds.width);
-
- // mark scanline as unread
- lineBuffer[y].y = -1;
+
+ /**
+ * Creates an instance of {@link Resizer} with a scale factor for each direction.
+ *
+ * @param scaleX the scale factor for x direction
+ * @param scaleY the scale factor for y direction
+ */
+ public Resizer(double scaleX, double scaleY)
+ {
+ mappingX = new Mapping(scaleX);
+ mappingY = new Mapping(scaleY);
}
- // range of source and destination scanlines in regions
- final int srcY0 = srcBounds.y;
- final int srcY1 = srcBounds.y + srcBounds.height;
- final int dstY0 = dstBounds.y;
- final int dstY1 = dstBounds.y + dstBounds.height;
+ private Weighttab[] createXWeights(Rectangle srcBounds, final Rectangle dstBounds,
+ final ParameterizedFilter filter)
+ {
+ final int srcX0 = srcBounds.x;
+ final int srcX1 = srcBounds.x + srcBounds.width;
+
+ final int dstX0 = dstBounds.x;
+ final int dstX1 = dstBounds.x + dstBounds.width;
+
+ final Weighttab tabs[] = new Weighttab[dstBounds.width];
+ for (int dstX = dstX0; dstX < dstX1; dstX++)
+ {
+ final double center = mappingX.mapPixelCenter(dstX);
+ tabs[dstX - dstX0] = new Weighttab(filter, weightOne, center, srcX0, srcX1 - 1,
+ trimZeros);
+ }
- // used to assert no backtracking
- int yFetched = -1;
+ return tabs;
+ }
- // loop over destination scanlines
- for (int dstY = dstY0; dstY < dstY1; dstY++) {
- // prepare a weighttab for destination y position by a single sampled filter for current y
- // position
- final Weighttab yWeight = new Weighttab(yFilter, weightOne, mappingY.mapPixelCenter(dstY), srcY0, srcY1 - 1, true);
+ /**
+ * Checks if our discrete sampling of an arbitrary continuous filter, parameterized by the filter spacing
+ * ({@link ParameterizedFilter#scale}), its radius ({@link ParameterizedFilter#support}), and the scale and offset
+ * of the coordinate mapping, causes the filter to reduce to point sampling.
+ * <p>
+ * It reduces if support is less than 1 pixel or if integer scale and translation, and filter is cardinal.
+ *
+ * @param filter the parameterized filter instance to be simplified
+ * @param scale the scale of the coordinate mapping
+ * @param offset the offset of the coordinate mapping
+ */
+ private ParameterizedFilter simplifyFilter(final ParameterizedFilter filter, final double scale,
+ final double offset)
+ {
+ if (coerce && (filter.support <= .5 || filter.filter.cardinal
+ && isInteger(1. / filter.scale) && isInteger(1. / (scale * filter.scale))
+ && isInteger((offset / scale - .5) / filter.scale)))
+ return new ParameterizedFilter(new Filter.Point(), 1., .5, 1);
+
+ return filter;
+ }
+
+ /**
+ * Filtered zoom, x direction filtering before y direction filtering
+ * <p>
+ * Note: when calling {@link Resizer#createXWeights(Rectangle, Rectangle, ParameterizedFilter)}, we can trim leading
+ * and trailing zeros from the x weight buffers as an optimization, but not for y weight buffers since the split
+ * formula is anticipating a constant amount of buffering of source scanlines; trimming zeros in y weight could
+ * cause feedback.
+ */
+ private void resizeXfirst(final Object src, final Rectangle srcBounds, final Object dst,
+ final Rectangle dstBounds, final ParameterizedFilter xFilter,
+ final ParameterizedFilter yFilter)
+ {
+ // source scanline buffer
+ final Scanline buffer = createScanline(src, dst, srcBounds.width);
+
+ // accumulator buffer
+ final Scanline accumulator = createScanline(src, dst, dstBounds.width);
+
+ // a sampled filter for source pixels for each dest x position
+ final Weighttab xWeights[] = createXWeights(srcBounds, dstBounds, xFilter);
+
+ // Circular buffer of active lines
+ final int yBufferSize = yFilter.width + 2;
+ final Scanline lineBuffer[] = new Scanline[yBufferSize];
+ for (int y = 0; y < yBufferSize; y++)
+ {
+ lineBuffer[y] = createScanline(src, dst, dstBounds.width);
+ lineBuffer[y].y = -1; /* mark scanline as unread */
+ }
- accumulator.clear();
+ // range of source and destination scanlines in regions
+ final int srcY0 = srcBounds.y;
+ final int srcY1 = srcBounds.y + srcBounds.height;
+ final int dstY0 = dstBounds.y;
+ final int dstY1 = dstBounds.y + dstBounds.height;
- // loop over source scanlines that contribute to this destination scanline
- for (int srcY = yWeight.i0; srcY <= yWeight.i1; srcY++) {
- final Scanline srcBuffer = lineBuffer[srcY % yBufferSize];
- if (srcBuffer.y != srcY) {
- // scanline needs to be fetched from source raster
- srcBuffer.y = srcY;
+ int yFetched = -1; // used to assert no backtracking
- if (srcY0 + srcY <= yFetched)
- throw new AssertionError("Backtracking from line " + yFetched + " to " + (srcY0 + srcY));
+ // loop over dest scanlines
+ for (int dstY = dstY0; dstY < dstY1; dstY++)
+ {
+ // a sampled filter for source pixels for each dest x position
+ final Weighttab yWeight = new Weighttab(yFilter, weightOne,
+ mappingY.mapPixelCenter(dstY), srcY0, srcY1 - 1, true);
- srcBuffer.fetch(srcBounds.x, srcY0 + srcY);
+ accumulator.clear();
- yFetched = srcY0 + srcY;
- }
+ // loop over source scanlines that contribute to this dest scanline
+ for (int srcY = yWeight.i0; srcY <= yWeight.i1; srcY++)
+ {
+ final Scanline srcBuffer = lineBuffer[srcY % yBufferSize];
+
+ if (debug)
+ System.out.println(" abuf.y / ayf " + srcBuffer.y + " / " + srcY);
- if (debug)
- System.out.println(dstY + "[] += " + srcY + "[] * " + yWeight.weights[srcY - yWeight.i0]);
+ if (srcBuffer.y != srcY)
+ {
+ // scanline needs to be fetched from src raster
+ srcBuffer.y = srcY;
- // add weighted source buffer into accumulator (these do y filter)
- srcBuffer.accumulate(yWeight.weights[srcY - yWeight.i0], accumulator);
- }
+ if (srcY0 + srcY <= yFetched)
+ throw new AssertionError(
+ "Backtracking from line " + yFetched + " to " + (srcY0 + srcY));
- // and filter it into the appropriate line of line buffer (x filter)
- accumulator.filter(bitsPerChannel, finalShift, xWeights, buffer);
+ buffer.fetch(srcBounds.x, srcY0 + srcY);
- // store destination scanline into destination raster
- buffer.store(dstBounds.x, dstY);
- if (debug)
- System.out.printf("\n");
+ yFetched = srcY0 + srcY;
+
+ // filter it into the appropriate line of linebuf (xfilt)
+ buffer.filter(NO_SHIFT, bitsPerChannel, xWeights, srcBuffer);
+ }
+
+ // add weighted tbuf into accum (these do yfilt)
+ srcBuffer.accumulate(yWeight.weights[srcY - yWeight.i0], accumulator);
+ }
+
+ accumulator.shift(finalShift);
+ accumulator.store(dstBounds.x, dstY);
+ if (debug)
+ System.out.printf("\n");
+ }
}
- }
-
- /**
- * @param src Source object
- * @param srcBounds Bounds of the source object
- * @param dst Destination object
- * @param dstBounds Bounds of the destination object
- * @param xFilter The filter used for x direction filtering
- * @param yFilter The filter used for y direction filtering
- */
- public void resize(final Object src, final Rectangle srcBounds, final Object dst, Rectangle dstBounds,
- Filter xFilter, Filter yFilter) {
- /*
- * find scale of filter in a space (source space) when minifying, source scale=1/scale, but when
- * magnifying, source scale=1
+
+ /**
+ * Filtered zoom, y direction filtering before x direction filtering
*/
- ParameterizedFilter xFilterParameterized = new ParameterizedFilter(xFilter, mappingX.scale);
- ParameterizedFilter yFilterParameterized = new ParameterizedFilter(yFilter, mappingY.scale);
-
- /* find valid destination window (transformed source + support margin) */
- final Rectangle dstRegion = new Rectangle();
- final int x1 = Utils.ceil(mappingX.srcToDst(srcBounds.x - xFilterParameterized.support) + EPSILON);
- final int y1 = Utils.ceil(mappingY.srcToDst(srcBounds.y - yFilterParameterized.support) + EPSILON);
- final int x2 = Utils.floor(mappingX.srcToDst(srcBounds.x + srcBounds.width + xFilterParameterized.support)
- - EPSILON);
- final int y2 = Utils.floor(mappingY.srcToDst(srcBounds.y + srcBounds.height + yFilterParameterized.support)
- - EPSILON);
- dstRegion.setFrameFromDiagonal(x1, y1, x2, y2);
-
- if (dstBounds.x < dstRegion.x || dstBounds.getMaxX() > dstRegion.getMaxX() || dstBounds.y < dstRegion.y
- || dstBounds.getMaxY() > dstRegion.getMaxY()) {
- /* requested destination window lies outside the valid destination, so clip destination */
- dstBounds = dstBounds.intersection(dstRegion);
- }
+ private void resizeYfirst(final Object src, final Rectangle srcBounds, final Object dst,
+ final Rectangle dstBounds, final ParameterizedFilter xFilter,
+ final ParameterizedFilter yFilter)
+ {
+ // destination scanline buffer
+ final Scanline buffer = createScanline(src, dst, dstBounds.width);
+
+ // accumulator buffer
+ final Scanline accumulator = createScanline(src, dst, srcBounds.width);
+
+ // a sampled filter for source pixels for each destination x position
+ final Weighttab xWeights[] = createXWeights(srcBounds, dstBounds, xFilter);
+
+ // Circular buffer of active lines
+ final int yBufferSize = yFilter.width + 2;
+ final Scanline lineBuffer[] = new Scanline[yBufferSize];
+ for (int y = 0; y < yBufferSize; y++)
+ {
+ lineBuffer[y] = createScanline(src, dst, srcBounds.width);
+
+ // mark scanline as unread
+ lineBuffer[y].y = -1;
+ }
- if (srcBounds.isEmpty() || dstBounds.width <= 0 || dstBounds.height <= 0) {
- return;
+ // range of source and destination scanlines in regions
+ final int srcY0 = srcBounds.y;
+ final int srcY1 = srcBounds.y + srcBounds.height;
+ final int dstY0 = dstBounds.y;
+ final int dstY1 = dstBounds.y + dstBounds.height;
+
+ // used to assert no backtracking
+ int yFetched = -1;
+
+ // loop over destination scanlines
+ for (int dstY = dstY0; dstY < dstY1; dstY++)
+ {
+ // prepare a weighttab for destination y position by a single sampled filter for current y
+ // position
+ final Weighttab yWeight = new Weighttab(yFilter, weightOne,
+ mappingY.mapPixelCenter(dstY), srcY0, srcY1 - 1, true);
+
+ accumulator.clear();
+
+ // loop over source scanlines that contribute to this destination scanline
+ for (int srcY = yWeight.i0; srcY <= yWeight.i1; srcY++)
+ {
+ final Scanline srcBuffer = lineBuffer[srcY % yBufferSize];
+ if (srcBuffer.y != srcY)
+ {
+ // scanline needs to be fetched from source raster
+ srcBuffer.y = srcY;
+
+ if (srcY0 + srcY <= yFetched)
+ throw new AssertionError(
+ "Backtracking from line " + yFetched + " to " + (srcY0 + srcY));
+
+ srcBuffer.fetch(srcBounds.x, srcY0 + srcY);
+
+ yFetched = srcY0 + srcY;
+ }
+
+ if (debug)
+ System.out.println(
+ dstY + "[] += " + srcY + "[] * " + yWeight.weights[srcY - yWeight.i0]);
+
+ // add weighted source buffer into accumulator (these do y filter)
+ srcBuffer.accumulate(yWeight.weights[srcY - yWeight.i0], accumulator);
+ }
+
+ // and filter it into the appropriate line of line buffer (x filter)
+ accumulator.filter(bitsPerChannel, finalShift, xWeights, buffer);
+
+ // store destination scanline into destination raster
+ buffer.store(dstBounds.x, dstY);
+ if (debug)
+ System.out.printf("\n");
+ }
}
- /* check for high-level simplifications of filter */
- xFilterParameterized = simplifyFilter(xFilterParameterized, mappingX.scale, mappingX.offset);
- yFilterParameterized = simplifyFilter(yFilterParameterized, mappingY.scale, mappingY.offset);
-
- /*
- * decide which filtering order (x->y or y->x) is faster for this mapping by counting
- * convolution multiplies
+ /**
+ * @param src Source object
+ * @param srcBounds Bounds of the source object
+ * @param dst Destination object
+ * @param dstBounds Bounds of the destination object
+ * @param xFilter The filter used for x direction filtering
+ * @param yFilter The filter used for y direction filtering
*/
- final boolean orderXY = order != Order.AUTO
- ? order == Order.XY
- : dstBounds.width
- * (srcBounds.height * xFilterParameterized.width + dstBounds.height * yFilterParameterized.width) < dstBounds.height
- * (dstBounds.width * xFilterParameterized.width + srcBounds.width * yFilterParameterized.width);
-
- // choose most efficient filtering order
- if (orderXY) {
- resizeXfirst(src, srcBounds, dst, dstBounds, xFilterParameterized, yFilterParameterized);
- } else {
- resizeYfirst(src, srcBounds, dst, dstBounds, xFilterParameterized, yFilterParameterized);
+ public void resize(final Object src, final Rectangle srcBounds, final Object dst,
+ Rectangle dstBounds, Filter xFilter, Filter yFilter)
+ {
+ /*
+ * find scale of filter in a space (source space) when minifying, source scale=1/scale, but when magnifying,
+ * source scale=1
+ */
+ ParameterizedFilter xFilterParameterized = new ParameterizedFilter(xFilter, mappingX.scale);
+ ParameterizedFilter yFilterParameterized = new ParameterizedFilter(yFilter, mappingY.scale);
+
+ /* find valid destination window (transformed source + support margin) */
+ final Rectangle dstRegion = new Rectangle();
+ final int x1 = Utils
+ .ceil(mappingX.srcToDst(srcBounds.x - xFilterParameterized.support) + EPSILON);
+ final int y1 = Utils
+ .ceil(mappingY.srcToDst(srcBounds.y - yFilterParameterized.support) + EPSILON);
+ final int x2 = Utils.floor(
+ mappingX.srcToDst(srcBounds.x + srcBounds.width + xFilterParameterized.support)
+ - EPSILON);
+ final int y2 = Utils.floor(
+ mappingY.srcToDst(srcBounds.y + srcBounds.height + yFilterParameterized.support)
+ - EPSILON);
+ dstRegion.setFrameFromDiagonal(x1, y1, x2, y2);
+
+ if (dstBounds.x < dstRegion.x || dstBounds.getMaxX() > dstRegion.getMaxX()
+ || dstBounds.y < dstRegion.y || dstBounds.getMaxY() > dstRegion.getMaxY())
+ {
+ /* requested destination window lies outside the valid destination, so clip destination */
+ dstBounds = dstBounds.intersection(dstRegion);
+ }
+
+ if (srcBounds.isEmpty() || dstBounds.width <= 0 || dstBounds.height <= 0)
+ {
+ return;
+ }
+
+ /* check for high-level simplifications of filter */
+ xFilterParameterized = simplifyFilter(xFilterParameterized, mappingX.scale,
+ mappingX.offset);
+ yFilterParameterized = simplifyFilter(yFilterParameterized, mappingY.scale,
+ mappingY.offset);
+
+ /*
+ * decide which filtering order (x->y or y->x) is faster for this mapping by counting convolution multiplies
+ */
+ final boolean orderXY = order != Order.AUTO ? order == Order.XY
+ : dstBounds.width * (srcBounds.height * xFilterParameterized.width
+ + dstBounds.height * yFilterParameterized.width) < dstBounds.height
+ * (dstBounds.width * xFilterParameterized.width
+ + srcBounds.width * yFilterParameterized.width);
+
+ // choose most efficient filtering order
+ if (orderXY)
+ {
+ resizeXfirst(src, srcBounds, dst, dstBounds, xFilterParameterized,
+ yFilterParameterized);
+ }
+ else
+ {
+ resizeYfirst(src, srcBounds, dst, dstBounds, xFilterParameterized,
+ yFilterParameterized);
+ }
}
- }
- private static Scanline createScanline(final Object src, Object dst, final int length) {
- if (src == null)
- throw new IllegalArgumentException("src must not be null");
+ private static Scanline createScanline(final Object src, Object dst, final int length)
+ {
+ if (src == null)
+ throw new IllegalArgumentException("src must not be null");
- if (!(src instanceof Bitmap))
- throw new IllegalArgumentException("src must be from type " + Bitmap.class.getName());
+ if (!(src instanceof Bitmap))
+ throw new IllegalArgumentException("src must be from type " + Bitmap.class.getName());
- if (dst == null)
- throw new IllegalArgumentException("dst must not be null");
+ if (dst == null)
+ throw new IllegalArgumentException("dst must not be null");
- if (!(dst instanceof WritableRaster))
- throw new IllegalArgumentException("dst must be from type " + WritableRaster.class.getName());
+ if (!(dst instanceof WritableRaster))
+ throw new IllegalArgumentException(
+ "dst must be from type " + WritableRaster.class.getName());
- return new BitmapScanline((Bitmap) src, (WritableRaster) dst, length);
- }
+ return new BitmapScanline((Bitmap) src, (WritableRaster) dst, length);
+ }
}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/image/Scanline.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/image/Scanline.java b/src/main/java/org/apache/pdfbox/jbig2/image/Scanline.java
index 19f3565..7a200e4 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/image/Scanline.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/image/Scanline.java
@@ -23,536 +23,602 @@ import java.awt.image.SampleModel;
import java.awt.image.SinglePixelPackedSampleModel;
import java.awt.image.WritableRaster;
-
-abstract class Scanline {
- public interface ScanlineFilter {
- public void filter(int x, int y, int componentIndex, Object data, int length);
- }
-
-
- /**
- * A Scanline for pixel interleaved byte data with three components. Although its name contains
- * "BGR" it doesn't really matter how the components are organized, als long as there are three of
- * them.
- */
- protected static final class ByteBGRScanline extends Scanline {
- private final Raster srcRaster;
- private final WritableRaster dstRaster;
-
- private final int data[];
-
- protected ByteBGRScanline(Raster src, WritableRaster dst, final int length) {
- super(length);
- srcRaster = src;
- dstRaster = dst;
-
- data = new int[3 * length];
+abstract class Scanline
+{
+ public interface ScanlineFilter
+ {
+ public void filter(int x, int y, int componentIndex, Object data, int length);
}
- @Override
- protected void accumulate(final int weight, final Scanline dst) {
- final ByteBGRScanline bcs = (ByteBGRScanline) dst;
+ /**
+ * A Scanline for pixel interleaved byte data with three components. Although its name contains "BGR" it doesn't
+ * really matter how the components are organized, als long as there are three of them.
+ */
+ protected static final class ByteBGRScanline extends Scanline
+ {
+ private final Raster srcRaster;
+ private final WritableRaster dstRaster;
- final int abuf[] = data;
- final int bbuf[] = bcs.data;
+ private final int data[];
- for (int b = 0; b < bbuf.length; b++)
- bbuf[b] += weight * abuf[b];
- }
+ protected ByteBGRScanline(Raster src, WritableRaster dst, final int length)
+ {
+ super(length);
+ srcRaster = src;
+ dstRaster = dst;
- @Override
- protected void clear() {
- final int[] b = data;
- for (int j = 0; j < b.length; j++)
- b[j] = 0;
- }
+ data = new int[3 * length];
+ }
- @Override
- protected void fetch(final int x, final int y) {
- srcRaster.getPixels(x, y, length, 1, data);
- }
+ @Override
+ protected void accumulate(final int weight, final Scanline dst)
+ {
+ final ByteBGRScanline bcs = (ByteBGRScanline) dst;
- @Override
- protected void filter(final int[] preShift, final int[] postShift, final Weighttab[] tabs, final Scanline dst) {
- final ByteBGRScanline bcs = (ByteBGRScanline) dst;
- final int nx = dst.length;
-
- // start sum at 1<<shift-1 for rounding
- final int start[] = new int[]{
- 1 << postShift[0] - 1, 1 << postShift[1] - 1, 1 << postShift[2] - 1
- };
- final int abuf[] = data;
- final int bbuf[] = bcs.data;
-
- // the next two blocks are duplicated except for the missing shift
- // operation if preShift==0.
- if (preShift[0] != 0 || preShift[1] != 0 || preShift[2] != 0)
- for (int bp = 0, b = 0; b < nx; b++) {
- final Weighttab wtab = tabs[b];
- final int an = wtab.weights.length;
-
- int sumr = start[0];
- int sumg = start[1];
- int sumb = start[2];
- for (int wp = 0, ap = wtab.i0 * 3; wp < an && ap < abuf.length; wp++) {
- final int w = wtab.weights[wp];
- sumr += w * (abuf[ap++] >> preShift[0]);
- sumg += w * (abuf[ap++] >> preShift[1]);
- sumb += w * (abuf[ap++] >> preShift[2]);
- }
-
- int t = sumr >> postShift[0];
- bbuf[bp++] = t < 0 ? 0 : t > 255 ? 255 : t;
- t = sumg >> postShift[1];
- bbuf[bp++] = t < 0 ? 0 : t > 255 ? 255 : t;
- t = sumb >> postShift[2];
- bbuf[bp++] = t < 0 ? 0 : t > 255 ? 255 : t;
+ final int abuf[] = data;
+ final int bbuf[] = bcs.data;
+
+ for (int b = 0; b < bbuf.length; b++)
+ bbuf[b] += weight * abuf[b];
}
- else
- for (int bp = 0, b = 0; b < nx; b++) {
- final Weighttab wtab = tabs[b];
- final int an = wtab.weights.length;
-
- int sumr = start[0];
- int sumg = start[1];
- int sumb = start[2];
- for (int wp = 0, ap = wtab.i0 * 3; wp < an && ap < abuf.length; wp++) {
- final int w = wtab.weights[wp];
- sumr += w * abuf[ap++];
- sumg += w * abuf[ap++];
- sumb += w * abuf[ap++];
- }
-
- bbuf[bp++] = sumr >> postShift[0];
- bbuf[bp++] = sumg >> postShift[1];
- bbuf[bp++] = sumb >> postShift[2];
+
+ @Override
+ protected void clear()
+ {
+ final int[] b = data;
+ for (int j = 0; j < b.length; j++)
+ b[j] = 0;
}
- }
- @Override
- protected void shift(final int[] shift) {
- final int half[] = new int[]{
- 1 << shift[0] - 1, 1 << shift[1] - 1, 1 << shift[2] - 1
- };
+ @Override
+ protected void fetch(final int x, final int y)
+ {
+ srcRaster.getPixels(x, y, length, 1, data);
+ }
- final int abuf[] = data;
+ @Override
+ protected void filter(final int[] preShift, final int[] postShift, final Weighttab[] tabs,
+ final Scanline dst)
+ {
+ final ByteBGRScanline bcs = (ByteBGRScanline) dst;
+ final int nx = dst.length;
+
+ // start sum at 1<<shift-1 for rounding
+ final int start[] = new int[] { 1 << postShift[0] - 1, 1 << postShift[1] - 1,
+ 1 << postShift[2] - 1 };
+ final int abuf[] = data;
+ final int bbuf[] = bcs.data;
+
+ // the next two blocks are duplicated except for the missing shift
+ // operation if preShift==0.
+ if (preShift[0] != 0 || preShift[1] != 0 || preShift[2] != 0)
+ for (int bp = 0, b = 0; b < nx; b++)
+ {
+ final Weighttab wtab = tabs[b];
+ final int an = wtab.weights.length;
+
+ int sumr = start[0];
+ int sumg = start[1];
+ int sumb = start[2];
+ for (int wp = 0, ap = wtab.i0 * 3; wp < an && ap < abuf.length; wp++)
+ {
+ final int w = wtab.weights[wp];
+ sumr += w * (abuf[ap++] >> preShift[0]);
+ sumg += w * (abuf[ap++] >> preShift[1]);
+ sumb += w * (abuf[ap++] >> preShift[2]);
+ }
+
+ int t = sumr >> postShift[0];
+ bbuf[bp++] = t < 0 ? 0 : t > 255 ? 255 : t;
+ t = sumg >> postShift[1];
+ bbuf[bp++] = t < 0 ? 0 : t > 255 ? 255 : t;
+ t = sumb >> postShift[2];
+ bbuf[bp++] = t < 0 ? 0 : t > 255 ? 255 : t;
+ }
+ else
+ for (int bp = 0, b = 0; b < nx; b++)
+ {
+ final Weighttab wtab = tabs[b];
+ final int an = wtab.weights.length;
+
+ int sumr = start[0];
+ int sumg = start[1];
+ int sumb = start[2];
+ for (int wp = 0, ap = wtab.i0 * 3; wp < an && ap < abuf.length; wp++)
+ {
+ final int w = wtab.weights[wp];
+ sumr += w * abuf[ap++];
+ sumg += w * abuf[ap++];
+ sumb += w * abuf[ap++];
+ }
+
+ bbuf[bp++] = sumr >> postShift[0];
+ bbuf[bp++] = sumg >> postShift[1];
+ bbuf[bp++] = sumb >> postShift[2];
+ }
+ }
- for (int b = 0; b < abuf.length;) {
- for (int c = 0; c < 3; c++, b++) {
- final int t = abuf[b] + half[c] >> shift[c];
- abuf[b] = t < 0 ? 0 : t > 255 ? 255 : t;
+ @Override
+ protected void shift(final int[] shift)
+ {
+ final int half[] = new int[] { 1 << shift[0] - 1, 1 << shift[1] - 1,
+ 1 << shift[2] - 1 };
+
+ final int abuf[] = data;
+
+ for (int b = 0; b < abuf.length;)
+ {
+ for (int c = 0; c < 3; c++, b++)
+ {
+ final int t = abuf[b] + half[c] >> shift[c];
+ abuf[b] = t < 0 ? 0 : t > 255 ? 255 : t;
+ }
+ }
}
- }
- }
- @Override
- protected void store(final int x, final int y) {
- dstRaster.setPixels(x, y, length, 1, data);
+ @Override
+ protected void store(final int x, final int y)
+ {
+ dstRaster.setPixels(x, y, length, 1, data);
+ }
}
- }
- /**
- * A Scanline for packed integer pixels.
- */
- protected static final class IntegerSinglePixelPackedScanline extends Scanline {
- private final Raster srcRaster;
- private final WritableRaster dstRaster;
+ /**
+ * A Scanline for packed integer pixels.
+ */
+ protected static final class IntegerSinglePixelPackedScanline extends Scanline
+ {
+ private final Raster srcRaster;
+ private final WritableRaster dstRaster;
- private final int data[];
- private final int[] bitMasks;
- private final int[] bitOffsets;
- private final int componentCount;
- private final SinglePixelPackedSampleModel srcSM;
- private final int tmp[];
+ private final int data[];
+ private final int[] bitMasks;
+ private final int[] bitOffsets;
+ private final int componentCount;
+ private final SinglePixelPackedSampleModel srcSM;
+ private final int tmp[];
- protected IntegerSinglePixelPackedScanline(Raster src, WritableRaster dst, final int length) {
- super(length);
- srcRaster = src;
- dstRaster = dst;
+ protected IntegerSinglePixelPackedScanline(Raster src, WritableRaster dst, final int length)
+ {
+ super(length);
+ srcRaster = src;
+ dstRaster = dst;
- srcSM = (SinglePixelPackedSampleModel) srcRaster.getSampleModel();
+ srcSM = (SinglePixelPackedSampleModel) srcRaster.getSampleModel();
- bitMasks = srcSM.getBitMasks();
- bitOffsets = srcSM.getBitOffsets();
- componentCount = bitMasks.length;
+ bitMasks = srcSM.getBitMasks();
+ bitOffsets = srcSM.getBitOffsets();
+ componentCount = bitMasks.length;
- if (componentCount != bitOffsets.length || bitOffsets.length != srcSM.getNumBands())
- throw new IllegalArgumentException("weird: getBitMasks().length != getBitOffsets().length");
+ if (componentCount != bitOffsets.length || bitOffsets.length != srcSM.getNumBands())
+ throw new IllegalArgumentException(
+ "weird: getBitMasks().length != getBitOffsets().length");
- tmp = new int[componentCount];
-
- data = new int[componentCount * length];
- }
+ tmp = new int[componentCount];
- @Override
- protected void accumulate(final int weight, final Scanline dst) {
- final IntegerSinglePixelPackedScanline ispps = (IntegerSinglePixelPackedScanline) dst;
-
- final int abuf[] = data;
- final int bbuf[] = ispps.data;
-
- for (int b = 0; b < bbuf.length; b++)
- bbuf[b] += weight * abuf[b];
- }
+ data = new int[componentCount * length];
+ }
- @Override
- protected void clear() {
- final int[] b = data;
- for (int j = 0; j < b.length; j++)
- b[j] = 0;
- }
+ @Override
+ protected void accumulate(final int weight, final Scanline dst)
+ {
+ final IntegerSinglePixelPackedScanline ispps = (IntegerSinglePixelPackedScanline) dst;
- @Override
- protected void fetch(final int x, final int y) {
- srcRaster.getPixels(x, y, length, 1, data);
- }
+ final int abuf[] = data;
+ final int bbuf[] = ispps.data;
- @Override
- protected void filter(final int[] preShift, final int[] postShift, final Weighttab[] tabs, final Scanline dst) {
- final IntegerSinglePixelPackedScanline ispps = (IntegerSinglePixelPackedScanline) dst;
- final int nx = dst.length;
-
- // start sum at 1<<shift-1 for rounding
- final int start[] = tmp;
- for (int c = 0; c < componentCount; c++)
- start[c] = 1 << postShift[c] - 1;
-
- final int abuf[] = data;
- final int bbuf[] = ispps.data;
-
- // the next two blocks are duplicated except for the missing shift
- // operation if preShift==0.
- boolean hasPreShift = false;
- for (int c = 0; c < componentCount && !hasPreShift; c++)
- hasPreShift |= preShift[c] != 0;
- if (hasPreShift)
- for (int bp = 0, b = 0; b < nx; b++) {
- final Weighttab wtab = tabs[b];
- final int an = wtab.weights.length;
-
- for (int c = 0; c < componentCount; c++) {
- int sum = start[c];
- for (int wp = 0, ap = wtab.i0 * componentCount + c; wp < an && ap < abuf.length; wp++, ap += componentCount)
- sum += wtab.weights[wp] * (abuf[ap] >> preShift[c]);
-
- final int t = sum >> postShift[c];
- bbuf[bp++] = t < 0 ? 0 : t > 255 ? 255 : t;
- }
+ for (int b = 0; b < bbuf.length; b++)
+ bbuf[b] += weight * abuf[b];
}
- else
- for (int bp = 0, b = 0; b < nx; b++) {
- final Weighttab wtab = tabs[b];
- final int an = wtab.weights.length;
-
- for (int c = 0; c < componentCount; c++) {
- int sum = start[c];
- for (int wp = 0, ap = wtab.i0 * componentCount + c; wp < an && ap < abuf.length; wp++, ap += componentCount)
- sum += wtab.weights[wp] * abuf[ap];
-
- bbuf[bp++] = sum >> postShift[c];
- }
+
+ @Override
+ protected void clear()
+ {
+ final int[] b = data;
+ for (int j = 0; j < b.length; j++)
+ b[j] = 0;
}
- }
- @Override
- protected void shift(final int[] shift) {
- final int half[] = tmp;
- for (int c = 0; c < componentCount; c++)
- half[c] = 1 << shift[c] - 1;
+ @Override
+ protected void fetch(final int x, final int y)
+ {
+ srcRaster.getPixels(x, y, length, 1, data);
+ }
- final int abuf[] = data;
+ @Override
+ protected void filter(final int[] preShift, final int[] postShift, final Weighttab[] tabs,
+ final Scanline dst)
+ {
+ final IntegerSinglePixelPackedScanline ispps = (IntegerSinglePixelPackedScanline) dst;
+ final int nx = dst.length;
+
+ // start sum at 1<<shift-1 for rounding
+ final int start[] = tmp;
+ for (int c = 0; c < componentCount; c++)
+ start[c] = 1 << postShift[c] - 1;
+
+ final int abuf[] = data;
+ final int bbuf[] = ispps.data;
+
+ // the next two blocks are duplicated except for the missing shift
+ // operation if preShift==0.
+ boolean hasPreShift = false;
+ for (int c = 0; c < componentCount && !hasPreShift; c++)
+ hasPreShift |= preShift[c] != 0;
+ if (hasPreShift)
+ for (int bp = 0, b = 0; b < nx; b++)
+ {
+ final Weighttab wtab = tabs[b];
+ final int an = wtab.weights.length;
+
+ for (int c = 0; c < componentCount; c++)
+ {
+ int sum = start[c];
+ for (int wp = 0, ap = wtab.i0 * componentCount + c; wp < an
+ && ap < abuf.length; wp++, ap += componentCount)
+ sum += wtab.weights[wp] * (abuf[ap] >> preShift[c]);
+
+ final int t = sum >> postShift[c];
+ bbuf[bp++] = t < 0 ? 0 : t > 255 ? 255 : t;
+ }
+ }
+ else
+ for (int bp = 0, b = 0; b < nx; b++)
+ {
+ final Weighttab wtab = tabs[b];
+ final int an = wtab.weights.length;
+
+ for (int c = 0; c < componentCount; c++)
+ {
+ int sum = start[c];
+ for (int wp = 0, ap = wtab.i0 * componentCount + c; wp < an
+ && ap < abuf.length; wp++, ap += componentCount)
+ sum += wtab.weights[wp] * abuf[ap];
+
+ bbuf[bp++] = sum >> postShift[c];
+ }
+ }
+ }
- for (int b = 0; b < abuf.length;) {
- for (int c = 0; c < componentCount; c++, b++) {
- final int t = abuf[b] + half[c] >> shift[c];
- abuf[b] = t < 0 ? 0 : t > 255 ? 255 : t;
+ @Override
+ protected void shift(final int[] shift)
+ {
+ final int half[] = tmp;
+ for (int c = 0; c < componentCount; c++)
+ half[c] = 1 << shift[c] - 1;
+
+ final int abuf[] = data;
+
+ for (int b = 0; b < abuf.length;)
+ {
+ for (int c = 0; c < componentCount; c++, b++)
+ {
+ final int t = abuf[b] + half[c] >> shift[c];
+ abuf[b] = t < 0 ? 0 : t > 255 ? 255 : t;
+ }
+ }
}
- }
- }
- @Override
- protected void store(final int x, final int y) {
- dstRaster.setPixels(x, y, length, 1, data);
- }
- }
-
- /**
- * A Scanline for packed integer pixels.
- */
- protected static final class GenericRasterScanline extends Scanline {
- private final Raster srcRaster;
- private final WritableRaster dstRaster;
-
- private final int componentCount;
- private final int data[][];
- private final SampleModel srcSM;
- private final SampleModel dstSM;
- private final int channelMask[];
- private final int[] tmp;
- private final ScanlineFilter inputFilter;
-
- protected GenericRasterScanline(Raster src, WritableRaster dst, final int length, int bitsPerChannel[],
- ScanlineFilter inputFilter) {
- super(length);
- srcRaster = src;
- dstRaster = dst;
- this.inputFilter = inputFilter;
-
- srcSM = srcRaster.getSampleModel();
- dstSM = dstRaster.getSampleModel();
-
- componentCount = srcSM.getNumBands();
-
- if (componentCount != dstSM.getNumBands())
- throw new IllegalArgumentException("weird: src raster num bands != dst raster num bands");
-
- tmp = new int[componentCount];
-
- data = new int[componentCount][];
- for (int i = 0; i < data.length; i++)
- data[i] = new int[length];
-
- channelMask = new int[componentCount];
- for (int c = 0; c < componentCount; c++)
- channelMask[c] = (1 << bitsPerChannel[c]) - 1;
+ @Override
+ protected void store(final int x, final int y)
+ {
+ dstRaster.setPixels(x, y, length, 1, data);
+ }
}
- @Override
- protected void accumulate(final int weight, final Scanline dst) {
- final GenericRasterScanline grs = (GenericRasterScanline) dst;
+ /**
+ * A Scanline for packed integer pixels.
+ */
+ protected static final class GenericRasterScanline extends Scanline
+ {
+ private final Raster srcRaster;
+ private final WritableRaster dstRaster;
+
+ private final int componentCount;
+ private final int data[][];
+ private final SampleModel srcSM;
+ private final SampleModel dstSM;
+ private final int channelMask[];
+ private final int[] tmp;
+ private final ScanlineFilter inputFilter;
+
+ protected GenericRasterScanline(Raster src, WritableRaster dst, final int length,
+ int bitsPerChannel[], ScanlineFilter inputFilter)
+ {
+ super(length);
+ srcRaster = src;
+ dstRaster = dst;
+ this.inputFilter = inputFilter;
+
+ srcSM = srcRaster.getSampleModel();
+ dstSM = dstRaster.getSampleModel();
+
+ componentCount = srcSM.getNumBands();
+
+ if (componentCount != dstSM.getNumBands())
+ throw new IllegalArgumentException(
+ "weird: src raster num bands != dst raster num bands");
+
+ tmp = new int[componentCount];
+
+ data = new int[componentCount][];
+ for (int i = 0; i < data.length; i++)
+ data[i] = new int[length];
+
+ channelMask = new int[componentCount];
+ for (int c = 0; c < componentCount; c++)
+ channelMask[c] = (1 << bitsPerChannel[c]) - 1;
+ }
- final int l = grs.data[0].length;
- for (int c = 0; c < componentCount; c++) {
- final int ac[] = data[c];
- final int bc[] = grs.data[c];
+ @Override
+ protected void accumulate(final int weight, final Scanline dst)
+ {
+ final GenericRasterScanline grs = (GenericRasterScanline) dst;
- for (int b = 0; b < l; b++)
- bc[b] += weight * ac[b];
- }
- }
+ final int l = grs.data[0].length;
+ for (int c = 0; c < componentCount; c++)
+ {
+ final int ac[] = data[c];
+ final int bc[] = grs.data[c];
- @Override
- protected void clear() {
- for (int c = 0; c < componentCount; c++) {
- final int[] b = data[c];
- for (int x = 0; x < b.length; x++)
- b[x] = 0;
- }
- }
+ for (int b = 0; b < l; b++)
+ bc[b] += weight * ac[b];
+ }
+ }
- @Override
- protected void fetch(final int x, final int y) {
- for (int c = 0; c < componentCount; c++) {
- srcRaster.getSamples(x, y, length, 1, c, data[c]);
- if (null != inputFilter)
- inputFilter.filter(x, y, c, data[c], length);
- }
- }
+ @Override
+ protected void clear()
+ {
+ for (int c = 0; c < componentCount; c++)
+ {
+ final int[] b = data[c];
+ for (int x = 0; x < b.length; x++)
+ b[x] = 0;
+ }
+ }
- @Override
- protected void filter(final int[] preShift, final int[] postShift, final Weighttab[] tabs, final Scanline dst) {
- final GenericRasterScanline grs = (GenericRasterScanline) dst;
- final int nx = dst.length;
-
- // start sum at 1<<shift-1 for rounding
- final int start[] = tmp;
- for (int c = 0; c < componentCount; c++)
- start[c] = 1 << postShift[c] - 1;
-
- final int l = data[0].length;
-
- // the next two blocks are duplicated except for the missing shift
- // operation if preShift==0.
- boolean hasPreShift = false;
- for (int c = 0; c < componentCount && !hasPreShift; c++)
- hasPreShift |= preShift[c] != 0;
- if (hasPreShift)
- for (int c = 0; c < componentCount; c++) {
- final int ac[] = data[c];
- final int bc[] = grs.data[c];
- final int m = channelMask[c];
- for (int b = 0; b < nx; b++) {
- final Weighttab wtab = tabs[b];
- final int an = wtab.weights.length;
-
- int sum = start[c];
- for (int wp = 0, ap = wtab.i0; wp < an && ap < l; wp++, ap++)
- sum += wtab.weights[wp] * (ac[ap] >> preShift[c]);
-
- final int t = sum >> postShift[c];
- bc[b] = t < 0 ? 0 : t > m ? m : t;
- }
+ @Override
+ protected void fetch(final int x, final int y)
+ {
+ for (int c = 0; c < componentCount; c++)
+ {
+ srcRaster.getSamples(x, y, length, 1, c, data[c]);
+ if (null != inputFilter)
+ inputFilter.filter(x, y, c, data[c], length);
+ }
}
- else
- for (int c = 0; c < componentCount; c++) {
- final int ac[] = data[c];
- final int bc[] = grs.data[c];
- for (int b = 0; b < nx; b++) {
- final Weighttab wtab = tabs[b];
- final int an = wtab.weights.length;
+ @Override
+ protected void filter(final int[] preShift, final int[] postShift, final Weighttab[] tabs,
+ final Scanline dst)
+ {
+ final GenericRasterScanline grs = (GenericRasterScanline) dst;
+ final int nx = dst.length;
+
+ // start sum at 1<<shift-1 for rounding
+ final int start[] = tmp;
+ for (int c = 0; c < componentCount; c++)
+ start[c] = 1 << postShift[c] - 1;
+
+ final int l = data[0].length;
+
+ // the next two blocks are duplicated except for the missing shift
+ // operation if preShift==0.
+ boolean hasPreShift = false;
+ for (int c = 0; c < componentCount && !hasPreShift; c++)
+ hasPreShift |= preShift[c] != 0;
+ if (hasPreShift)
+ for (int c = 0; c < componentCount; c++)
+ {
+ final int ac[] = data[c];
+ final int bc[] = grs.data[c];
+ final int m = channelMask[c];
+ for (int b = 0; b < nx; b++)
+ {
+ final Weighttab wtab = tabs[b];
+ final int an = wtab.weights.length;
+
+ int sum = start[c];
+ for (int wp = 0, ap = wtab.i0; wp < an && ap < l; wp++, ap++)
+ sum += wtab.weights[wp] * (ac[ap] >> preShift[c]);
+
+ final int t = sum >> postShift[c];
+ bc[b] = t < 0 ? 0 : t > m ? m : t;
+ }
+ }
+ else
+ for (int c = 0; c < componentCount; c++)
+ {
+ final int ac[] = data[c];
+ final int bc[] = grs.data[c];
+
+ for (int b = 0; b < nx; b++)
+ {
+ final Weighttab wtab = tabs[b];
+ final int an = wtab.weights.length;
+
+ int sum = start[c];
+ for (int wp = 0, ap = wtab.i0; wp < an && ap < l; wp++, ap++)
+ sum += wtab.weights[wp] * ac[ap];
+
+ bc[b] = sum >> postShift[c];
+ }
+ }
+ }
- int sum = start[c];
- for (int wp = 0, ap = wtab.i0; wp < an && ap < l; wp++, ap++)
- sum += wtab.weights[wp] * ac[ap];
+ @Override
+ protected void shift(final int[] shift)
+ {
+ final int half[] = tmp;
+ for (int c = 0; c < componentCount; c++)
+ half[c] = 1 << shift[c] - 1;
+
+ final int abuf[][] = data;
+
+ final int l = abuf[0].length;
+ for (int c = 0; c < componentCount; c++)
+ {
+ final int ac[] = data[c];
+ final int m = channelMask[c];
+
+ for (int a = 0; a < l; a++)
+ {
+ final int t = ac[a] + half[c] >> shift[c];
+ ac[a] = t < 0 ? 0 : t > m ? m : t;
+ }
+ }
+ }
- bc[b] = sum >> postShift[c];
- }
+ @Override
+ protected void store(final int x, final int y)
+ {
+ final int nx = length;
+ for (int c = 0; c < componentCount; c++)
+ dstRaster.setSamples(x, y, nx, 1, c, data[c]);
}
}
- @Override
- protected void shift(final int[] shift) {
- final int half[] = tmp;
- for (int c = 0; c < componentCount; c++)
- half[c] = 1 << shift[c] - 1;
+ /**
+ * A Scanline for BiLevel input data ({@link MultiPixelPackedSampleModel}) to indexed output data
+ * (<code>sun.awt.image.ByteInterleavedRaster</code>).
+ */
+ protected static final class ByteBiLevelPackedScanline extends Scanline
+ {
+ private final Raster srcRaster;
+ private final WritableRaster dstRaster;
- final int abuf[][] = data;
+ private final int data[];
- final int l = abuf[0].length;
- for (int c = 0; c < componentCount; c++) {
- final int ac[] = data[c];
- final int m = channelMask[c];
+ protected ByteBiLevelPackedScanline(Raster src, WritableRaster dst, final int length)
+ {
+ super(length);
+ srcRaster = src;
+ dstRaster = dst;
- for (int a = 0; a < l; a++) {
- final int t = ac[a] + half[c] >> shift[c];
- ac[a] = t < 0 ? 0 : t > m ? m : t;
+ data = new int[length];
}
- }
- }
-
- @Override
- protected void store(final int x, final int y) {
- final int nx = length;
- for (int c = 0; c < componentCount; c++)
- dstRaster.setSamples(x, y, nx, 1, c, data[c]);
- }
- }
-
- /**
- * A Scanline for BiLevel input data ({@link MultiPixelPackedSampleModel}) to indexed output data
- * (<code>sun.awt.image.ByteInterleavedRaster</code>).
- */
- protected static final class ByteBiLevelPackedScanline extends Scanline {
- private final Raster srcRaster;
- private final WritableRaster dstRaster;
-
- private final int data[];
-
- protected ByteBiLevelPackedScanline(Raster src, WritableRaster dst, final int length) {
- super(length);
- srcRaster = src;
- dstRaster = dst;
-
- data = new int[length];
- }
-
- @Override
- protected void accumulate(final int weight, final Scanline dst) {
- final ByteBiLevelPackedScanline bblps = (ByteBiLevelPackedScanline) dst;
- final int abuf[] = data;
- final int bbuf[] = bblps.data;
+ @Override
+ protected void accumulate(final int weight, final Scanline dst)
+ {
+ final ByteBiLevelPackedScanline bblps = (ByteBiLevelPackedScanline) dst;
- for (int b = 0; b < bbuf.length; b++)
- bbuf[b] += weight * abuf[b];
- }
+ final int abuf[] = data;
+ final int bbuf[] = bblps.data;
- @Override
- protected void clear() {
- final int[] b = data;
- for (int j = 0; j < b.length; j++)
- b[j] = 0;
- }
-
- @Override
- protected void fetch(final int x, final int y) {
- srcRaster.getPixels(x, y, length, 1, data);
- for (int i = 0; i < length; i++)
- if (data[i] != 0)
- data[i] = 255;
- }
+ for (int b = 0; b < bbuf.length; b++)
+ bbuf[b] += weight * abuf[b];
+ }
- @Override
- protected void filter(final int[] preShift, final int[] postShift, final Weighttab[] tabs, final Scanline dst) {
- final ByteBiLevelPackedScanline bblps = (ByteBiLevelPackedScanline) dst;
- final int nx = dst.length;
-
- // start sum at 1<<shift-1 for rounding
- final int start = 1 << postShift[0] - 1;
- final int abuf[] = data;
- final int bbuf[] = bblps.data;
-
- // the next two blocks are duplicated except for the missing shift
- // operation if preShift==0.
- final int preShift0 = preShift[0];
- final int postShift0 = postShift[0];
- if (preShift0 != 0)
- for (int bp = 0, b = 0; b < nx; b++) {
- final Weighttab wtab = tabs[b];
- final int an = wtab.weights.length;
-
- int sum = start;
- for (int wp = 0, ap = wtab.i0; wp < an && ap < abuf.length; wp++) {
- sum += wtab.weights[wp] * (abuf[ap++] >> preShift0);
- }
-
- final int t = sum >> postShift0;
- bbuf[bp++] = t < 0 ? 0 : t > 255 ? 255 : t;
+ @Override
+ protected void clear()
+ {
+ final int[] b = data;
+ for (int j = 0; j < b.length; j++)
+ b[j] = 0;
}
- else
- for (int bp = 0, b = 0; b < nx; b++) {
- final Weighttab wtab = tabs[b];
- final int an = wtab.weights.length;
- int sum = start;
- for (int wp = 0, ap = wtab.i0; wp < an && ap < abuf.length; wp++) {
- sum += wtab.weights[wp] * abuf[ap++];
- }
+ @Override
+ protected void fetch(final int x, final int y)
+ {
+ srcRaster.getPixels(x, y, length, 1, data);
+ for (int i = 0; i < length; i++)
+ if (data[i] != 0)
+ data[i] = 255;
+ }
- bbuf[bp++] = sum >> postShift0;
+ @Override
+ protected void filter(final int[] preShift, final int[] postShift, final Weighttab[] tabs,
+ final Scanline dst)
+ {
+ final ByteBiLevelPackedScanline bblps = (ByteBiLevelPackedScanline) dst;
+ final int nx = dst.length;
+
+ // start sum at 1<<shift-1 for rounding
+ final int start = 1 << postShift[0] - 1;
+ final int abuf[] = data;
+ final int bbuf[] = bblps.data;
+
+ // the next two blocks are duplicated except for the missing shift
+ // operation if preShift==0.
+ final int preShift0 = preShift[0];
+ final int postShift0 = postShift[0];
+ if (preShift0 != 0)
+ for (int bp = 0, b = 0; b < nx; b++)
+ {
+ final Weighttab wtab = tabs[b];
+ final int an = wtab.weights.length;
+
+ int sum = start;
+ for (int wp = 0, ap = wtab.i0; wp < an && ap < abuf.length; wp++)
+ {
+ sum += wtab.weights[wp] * (abuf[ap++] >> preShift0);
+ }
+
+ final int t = sum >> postShift0;
+ bbuf[bp++] = t < 0 ? 0 : t > 255 ? 255 : t;
+ }
+ else
+ for (int bp = 0, b = 0; b < nx; b++)
+ {
+ final Weighttab wtab = tabs[b];
+ final int an = wtab.weights.length;
+
+ int sum = start;
+ for (int wp = 0, ap = wtab.i0; wp < an && ap < abuf.length; wp++)
+ {
+ sum += wtab.weights[wp] * abuf[ap++];
+ }
+
+ bbuf[bp++] = sum >> postShift0;
+ }
}
- }
- @Override
- protected void shift(final int[] shift) {
- final int shift0 = shift[0];
- final int half = 1 << shift0 - 1;
+ @Override
+ protected void shift(final int[] shift)
+ {
+ final int shift0 = shift[0];
+ final int half = 1 << shift0 - 1;
- final int abuf[] = data;
+ final int abuf[] = data;
- for (int b = 0; b < abuf.length; b++) {
- final int t = abuf[b] + half >> shift0;
- abuf[b] = t < 0 ? 0 : t > 255 ? 255 : t;
- }
- }
+ for (int b = 0; b < abuf.length; b++)
+ {
+ final int t = abuf[b] + half >> shift0;
+ abuf[b] = t < 0 ? 0 : t > 255 ? 255 : t;
+ }
+ }
- @Override
- protected void store(final int x, final int y) {
- dstRaster.setPixels(x, y, length, 1, data);
+ @Override
+ protected void store(final int x, final int y)
+ {
+ dstRaster.setPixels(x, y, length, 1, data);
+ }
}
- }
- int y;
- protected final int length;
+ int y;
+ protected final int length;
- protected Scanline(final int width) {
- length = width;
- }
+ protected Scanline(final int width)
+ {
+ length = width;
+ }
- protected final int getWidth() {
- return length;
- }
+ protected final int getWidth()
+ {
+ return length;
+ }
- protected abstract void clear();
+ protected abstract void clear();
- protected abstract void fetch(int x, int y);
+ protected abstract void fetch(int x, int y);
- protected abstract void filter(int[] preShift, int[] postShift, Weighttab[] xweights, Scanline dst);
+ protected abstract void filter(int[] preShift, int[] postShift, Weighttab[] xweights,
+ Scanline dst);
- protected abstract void accumulate(int weight, Scanline dst);
+ protected abstract void accumulate(int weight, Scanline dst);
- protected abstract void shift(int[] finalshift);
+ protected abstract void shift(int[] finalshift);
- protected abstract void store(int x, int y);
-}
\ No newline at end of file
+ protected abstract void store(int x, int y);
+}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/image/Weighttab.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/image/Weighttab.java b/src/main/java/org/apache/pdfbox/jbig2/image/Weighttab.java
index 7cbaa5f..fb1a6f9 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/image/Weighttab.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/image/Weighttab.java
@@ -22,93 +22,104 @@ import static java.lang.Math.min;
import org.apache.pdfbox.jbig2.util.Utils;
-final class Weighttab {
- final int weights[]; /* weight[i] goes with pixel at i0+i */
- final int i0, i1; /* range of samples is [i0..i1-1] */
-
- /*
- * make_weighttab: sample the continuous filter, scaled by ap.scale and positioned at continuous
- * source coordinate cen, for source coordinates in the range [0..len-1], writing the weights into
- * wtab. Scale the weights so they sum to WEIGHTONE, and trim leading and trailing zeros if
- * trimzeros!=0. b is the dest coordinate (for diagnostics).
- */
- public Weighttab(ParameterizedFilter pf, int weightOne, final double center, int a0, final int a1,
- final boolean trimzeros) {
- // find the source coord range of this positioned filter: [i0..i1-1] and clamp to input range
- int i0 = max(pf.minIndex(center), a0);
- int i1 = min(pf.maxIndex(center), a1);
-
- // find scale factor sc to normalize the filter
- double den = 0;
- for (int i = i0; i <= i1; i++)
- den += pf.eval(center, i);
-
- // set sc so that sum of sc*func() is approximately WEIGHTONE
- final double scale = den == 0. ? weightOne : weightOne / den;
-
- // find range of non-zero samples
- if (trimzeros) {
- boolean stillzero = trimzeros;
- int lastnonzero = 0;
- for (int i = i0; i <= i1; i++) {
- /* evaluate the filter function at p */
- final double tr = Utils.clamp(scale * pf.eval(center, i), Short.MIN_VALUE, Short.MAX_VALUE);
-
- final int t = (int) Math.floor(tr + .5);
- if (stillzero && t == 0)
- i0++; /* find first nonzero */
- else {
- stillzero = false;
- if (t != 0)
- lastnonzero = i; /* find last nonzero */
+final class Weighttab
+{
+ final int weights[]; /* weight[i] goes with pixel at i0+i */
+ final int i0, i1; /* range of samples is [i0..i1-1] */
+
+ /*
+ * make_weighttab: sample the continuous filter, scaled by ap.scale and positioned at continuous source coordinate
+ * cen, for source coordinates in the range [0..len-1], writing the weights into wtab. Scale the weights so they sum
+ * to WEIGHTONE, and trim leading and trailing zeros if trimzeros!=0. b is the dest coordinate (for diagnostics).
+ */
+ public Weighttab(ParameterizedFilter pf, int weightOne, final double center, int a0,
+ final int a1, final boolean trimzeros)
+ {
+ // find the source coord range of this positioned filter: [i0..i1-1] and clamp to input range
+ int i0 = max(pf.minIndex(center), a0);
+ int i1 = min(pf.maxIndex(center), a1);
+
+ // find scale factor sc to normalize the filter
+ double den = 0;
+ for (int i = i0; i <= i1; i++)
+ den += pf.eval(center, i);
+
+ // set sc so that sum of sc*func() is approximately WEIGHTONE
+ final double scale = den == 0. ? weightOne : weightOne / den;
+
+ // find range of non-zero samples
+ if (trimzeros)
+ {
+ boolean stillzero = trimzeros;
+ int lastnonzero = 0;
+ for (int i = i0; i <= i1; i++)
+ {
+ /* evaluate the filter function at p */
+ final double tr = Utils.clamp(scale * pf.eval(center, i), Short.MIN_VALUE,
+ Short.MAX_VALUE);
+
+ final int t = (int) Math.floor(tr + .5);
+ if (stillzero && t == 0)
+ i0++; /* find first nonzero */
+ else
+ {
+ stillzero = false;
+ if (t != 0)
+ lastnonzero = i; /* find last nonzero */
+ }
+ }
+
+ i1 = max(lastnonzero, i0);
}
- }
- i1 = max(lastnonzero, i0);
- }
-
- // initialize weight table of appropriate length
- weights = new int[i1 - i0 + 1];
+ // initialize weight table of appropriate length
+ weights = new int[i1 - i0 + 1];
- // compute the discrete, sampled filter coefficients
- int sum = 0;
- for (int idx = 0, i = i0; i <= i1; i++) {
- /* evaluate the filter function at p */
- final double tr = Utils.clamp(scale * pf.eval(center, i), Short.MIN_VALUE, Short.MAX_VALUE);
+ // compute the discrete, sampled filter coefficients
+ int sum = 0;
+ for (int idx = 0, i = i0; i <= i1; i++)
+ {
+ /* evaluate the filter function at p */
+ final double tr = Utils.clamp(scale * pf.eval(center, i), Short.MIN_VALUE,
+ Short.MAX_VALUE);
- final int t = (int) Math.floor(tr + .5);
- weights[idx++] = t; /* add weight to table */
- sum += t;
- }
+ final int t = (int) Math.floor(tr + .5);
+ weights[idx++] = t; /* add weight to table */
+ sum += t;
+ }
- if (sum == 0) {
- i1 = i0;
- weights[0] = weightOne;
- } else if (sum != weightOne) {
- /*
- * Fudge the center slightly to make sum=WEIGHTONE exactly. Is this the best way to normalize
- * a discretely sampled continuous filter?
- */
- int i = (int) (center + .5);
- if (i >= i1)
- i = i1 - 1;
- if (i < i0)
- i = i0;
- final int t = weightOne - sum;
- if (Resizer.debug)
- System.out.printf("[%d]+=%d ", i, t);
- weights[i - i0] += t; /* fudge center sample */
- }
+ if (sum == 0)
+ {
+ i1 = i0;
+ weights[0] = weightOne;
+ }
+ else if (sum != weightOne)
+ {
+ /*
+ * Fudge the center slightly to make sum=WEIGHTONE exactly. Is this the best way to normalize a discretely
+ * sampled continuous filter?
+ */
+ int i = (int) (center + .5);
+ if (i >= i1)
+ i = i1 - 1;
+ if (i < i0)
+ i = i0;
+ final int t = weightOne - sum;
+ if (Resizer.debug)
+ System.out.printf("[%d]+=%d ", i, t);
+ weights[i - i0] += t; /* fudge center sample */
+ }
- this.i0 = i0 - a0;
- this.i1 = i1 - a0;
+ this.i0 = i0 - a0;
+ this.i1 = i1 - a0;
- if (Resizer.debug) {
- System.out.printf("\t");
- for (int idx = 0, i = i0; i < i1; i++, idx++)
- System.out.printf("%5d ", weights[idx]);
- System.out.printf("\n");
+ if (Resizer.debug)
+ {
+ System.out.printf("\t");
+ for (int idx = 0, i = i0; i < i1; i++, idx++)
+ System.out.printf("%5d ", weights[idx]);
+ System.out.printf("\n");
+ }
}
- }
-
-}
\ No newline at end of file
+
+}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/io/DefaultInputStreamFactory.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/io/DefaultInputStreamFactory.java b/src/main/java/org/apache/pdfbox/jbig2/io/DefaultInputStreamFactory.java
index b92b033..06bd618 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/io/DefaultInputStreamFactory.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/io/DefaultInputStreamFactory.java
@@ -24,14 +24,19 @@ import javax.imageio.stream.FileCacheImageInputStream;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.MemoryCacheImageInputStream;
-public class DefaultInputStreamFactory implements InputStreamFactory {
+public class DefaultInputStreamFactory implements InputStreamFactory
+{
- public ImageInputStream getInputStream(InputStream is) {
- try {
- return new FileCacheImageInputStream(is, null);
- } catch (IOException e) {
- return new MemoryCacheImageInputStream(is);
+ public ImageInputStream getInputStream(InputStream is)
+ {
+ try
+ {
+ return new FileCacheImageInputStream(is, null);
+ }
+ catch (IOException e)
+ {
+ return new MemoryCacheImageInputStream(is);
+ }
}
- }
}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/io/InputStreamFactory.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/io/InputStreamFactory.java b/src/main/java/org/apache/pdfbox/jbig2/io/InputStreamFactory.java
index 7b2e5c5..8377798 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/io/InputStreamFactory.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/io/InputStreamFactory.java
@@ -22,6 +22,7 @@ import java.io.InputStream;
import javax.imageio.stream.ImageInputStream;
-public interface InputStreamFactory {
- public ImageInputStream getInputStream(InputStream is) throws IOException;
+public interface InputStreamFactory
+{
+ public ImageInputStream getInputStream(InputStream is) throws IOException;
}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/io/SubInputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/io/SubInputStream.java b/src/main/java/org/apache/pdfbox/jbig2/io/SubInputStream.java
index a7782bc..e51d44b 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/io/SubInputStream.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/io/SubInputStream.java
@@ -23,128 +23,143 @@ import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageInputStreamImpl;
/**
- * A wrapper for an {@link ImageInputStream} which is able to provide a view of a specific part of
- * the wrapped stream. Read accesses to the wrapped stream are synchronized, so that users of this
- * stream need to deal with synchronization against other users of the same instance, but not
- * against other users of the wrapped stream.
+ * A wrapper for an {@link ImageInputStream} which is able to provide a view of a specific part of the wrapped stream.
+ * Read accesses to the wrapped stream are synchronized, so that users of this stream need to deal with synchronization
+ * against other users of the same instance, but not against other users of the wrapped stream.
*/
-public class SubInputStream extends ImageInputStreamImpl {
-
- protected final ImageInputStream wrappedStream;
-
- /**
- * The position in the wrapped stream at which the window starts. Offset is an absolut value.
- */
- protected final long offset;
-
- /**
- * The length of the window. Length is an relative value.
- */
- protected final long length;
-
- /**
- * A buffer which is used to improve read performance.
- */
- private final byte buffer[] = new byte[4096];
-
- /**
- * Location of the first byte in the buffer with respect to the start of the stream.
- */
- long bufferBase;
-
- /**
- * Location of the last byte in the buffer with respect to the start of the stream.
- */
- long bufferTop;
-
- /**
- * Construct a new SubInputStream which provides a view of the wrapped stream.
- *
- * @param iis - The stream to be wrapped.
- * @param offset - The absolute position in the wrapped stream at which the sub-stream starts.
- * @param length - The length of the sub-stream.
- */
- public SubInputStream(ImageInputStream iis, long offset, long length) {
- assert null != iis;
- assert length >= 0;
- assert offset >= 0;
-
- this.wrappedStream = iis;
- this.offset = offset;
- this.length = length;
- }
-
- @Override
- public int read() throws IOException {
- if (streamPos >= length) {
- return -1;
+public class SubInputStream extends ImageInputStreamImpl
+{
+
+ protected final ImageInputStream wrappedStream;
+
+ /**
+ * The position in the wrapped stream at which the window starts. Offset is an absolut value.
+ */
+ protected final long offset;
+
+ /**
+ * The length of the window. Length is an relative value.
+ */
+ protected final long length;
+
+ /**
+ * A buffer which is used to improve read performance.
+ */
+ private final byte buffer[] = new byte[4096];
+
+ /**
+ * Location of the first byte in the buffer with respect to the start of the stream.
+ */
+ long bufferBase;
+
+ /**
+ * Location of the last byte in the buffer with respect to the start of the stream.
+ */
+ long bufferTop;
+
+ /**
+ * Construct a new SubInputStream which provides a view of the wrapped stream.
+ *
+ * @param iis - The stream to be wrapped.
+ * @param offset - The absolute position in the wrapped stream at which the sub-stream starts.
+ * @param length - The length of the sub-stream.
+ */
+ public SubInputStream(ImageInputStream iis, long offset, long length)
+ {
+ assert null != iis;
+ assert length >= 0;
+ assert offset >= 0;
+
+ this.wrappedStream = iis;
+ this.offset = offset;
+ this.length = length;
}
- if (streamPos >= bufferTop || streamPos < bufferBase) {
- if (!fillBuffer()) {
- return -1;
- }
- }
+ @Override
+ public int read() throws IOException
+ {
+ if (streamPos >= length)
+ {
+ return -1;
+ }
- int read = 0xff & buffer[(int) (streamPos - bufferBase)];
+ if (streamPos >= bufferTop || streamPos < bufferBase)
+ {
+ if (!fillBuffer())
+ {
+ return -1;
+ }
+ }
- streamPos++;
+ int read = 0xff & buffer[(int) (streamPos - bufferBase)];
- return read;
- }
+ streamPos++;
- @Override
- public int read(byte[] b, int off, int len) throws IOException {
- if (streamPos >= length) {
- return -1;
+ return read;
}
- synchronized (wrappedStream) {
- if (wrappedStream.getStreamPosition() != streamPos + offset) {
- wrappedStream.seek(streamPos + offset);
- }
-
- int toRead = (int) Math.min(len, length - streamPos);
- int read = wrappedStream.read(b, off, toRead);
- streamPos += read;
+ @Override
+ public int read(byte[] b, int off, int len) throws IOException
+ {
+ if (streamPos >= length)
+ {
+ return -1;
+ }
+
+ synchronized (wrappedStream)
+ {
+ if (wrappedStream.getStreamPosition() != streamPos + offset)
+ {
+ wrappedStream.seek(streamPos + offset);
+ }
+
+ int toRead = (int) Math.min(len, length - streamPos);
+ int read = wrappedStream.read(b, off, toRead);
+ streamPos += read;
+
+ return read;
+ }
+ }
- return read;
+ /**
+ * Fill the buffer at the current stream position.
+ *
+ * @throws IOException
+ * @return Boolean flag. {@code true} if successful, {@code false} if not.
+ */
+ private boolean fillBuffer() throws IOException
+ {
+ synchronized (wrappedStream)
+ {
+ if (wrappedStream.getStreamPosition() != streamPos + offset)
+ {
+ wrappedStream.seek(streamPos + offset);
+ }
+
+ bufferBase = streamPos;
+ int toRead = (int) Math.min(buffer.length, length - streamPos);
+ int read = wrappedStream.read(buffer, 0, toRead);
+ bufferTop = bufferBase + read;
+
+ return read > 0;
+ }
}
- }
-
- /**
- * Fill the buffer at the current stream position.
- *
- * @throws IOException
- * @return Boolean flag. {@code true} if successful, {@code false} if not.
- */
- private boolean fillBuffer() throws IOException {
- synchronized (wrappedStream) {
- if (wrappedStream.getStreamPosition() != streamPos + offset) {
- wrappedStream.seek(streamPos + offset);
- }
-
- bufferBase = streamPos;
- int toRead = (int) Math.min(buffer.length, length - streamPos);
- int read = wrappedStream.read(buffer, 0, toRead);
- bufferTop = bufferBase + read;
-
- return read > 0;
+
+ @Override
+ public long length()
+ {
+ return length;
}
- }
-
- @Override
- public long length() {
- return length;
- }
-
- /**
- * Skips remaining bits in the current byte.
- */
- public void skipBits() {
- if (bitOffset != 0) {
- bitOffset = 0;
- streamPos++;
+
+ /**
+ * Skips remaining bits in the current byte.
+ */
+ public void skipBits()
+ {
+ if (bitOffset != 0)
+ {
+ bitOffset = 0;
+ streamPos++;
+ }
}
- }
}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/segments/EndOfStripe.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/segments/EndOfStripe.java b/src/main/java/org/apache/pdfbox/jbig2/segments/EndOfStripe.java
index 704137e..6e6f19c 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/segments/EndOfStripe.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/segments/EndOfStripe.java
@@ -28,22 +28,27 @@ import org.apache.pdfbox.jbig2.io.SubInputStream;
/**
* This segment flags an end of stripe (see JBIG2 ISO standard, 7.4.9).
*/
-public class EndOfStripe implements SegmentData {
-
- private SubInputStream subInputStream;
- private int lineNumber;
-
- private void parseHeader() throws IOException, IntegerMaxValueException, InvalidHeaderValueException {
- lineNumber = (int) (subInputStream.readBits(32) & 0xffffffff);
- }
-
- public void init(SegmentHeader header, SubInputStream sis) throws IntegerMaxValueException,
- InvalidHeaderValueException, IOException {
- this.subInputStream = sis;
- parseHeader();
- }
-
- public int getLineNumber() {
- return lineNumber;
- }
+public class EndOfStripe implements SegmentData
+{
+
+ private SubInputStream subInputStream;
+ private int lineNumber;
+
+ private void parseHeader()
+ throws IOException, IntegerMaxValueException, InvalidHeaderValueException
+ {
+ lineNumber = (int) (subInputStream.readBits(32) & 0xffffffff);
+ }
+
+ public void init(SegmentHeader header, SubInputStream sis)
+ throws IntegerMaxValueException, InvalidHeaderValueException, IOException
+ {
+ this.subInputStream = sis;
+ parseHeader();
+ }
+
+ public int getLineNumber()
+ {
+ return lineNumber;
+ }
}