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:19 UTC
[04/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/segments/Table.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/segments/Table.java b/src/main/java/org/apache/pdfbox/jbig2/segments/Table.java
index 43924e2..f26295b 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/segments/Table.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/segments/Table.java
@@ -28,70 +28,82 @@ import org.apache.pdfbox.jbig2.io.SubInputStream;
/**
* This class represents a "Table" segment. It handles custom tables, see Annex B.
*/
-public class Table implements SegmentData {
+public class Table implements SegmentData
+{
- private SubInputStream subInputStream;
+ private SubInputStream subInputStream;
- /** Code table flags, B.2.1, page 87 */
- private int htOutOfBand;
- private int htPS;
- private int htRS;
+ /** Code table flags, B.2.1, page 87 */
+ private int htOutOfBand;
+ private int htPS;
+ private int htRS;
- /** Code table lowest value, B.2.2, page 87 */
- private int htLow;
+ /** Code table lowest value, B.2.2, page 87 */
+ private int htLow;
- /** Code table highest value, B.2.3, page 87 */
- private int htHigh;
+ /** Code table highest value, B.2.3, page 87 */
+ private int htHigh;
- private void parseHeader() throws IOException, InvalidHeaderValueException, IntegerMaxValueException {
- int bit;
+ private void parseHeader()
+ throws IOException, InvalidHeaderValueException, IntegerMaxValueException
+ {
+ int bit;
- /* Bit 7 */
- if ((bit = subInputStream.readBit()) == 1) {
- throw new InvalidHeaderValueException("B.2.1 Code table flags: Bit 7 must be zero, but was " + bit);
- }
+ /* Bit 7 */
+ if ((bit = subInputStream.readBit()) == 1)
+ {
+ throw new InvalidHeaderValueException(
+ "B.2.1 Code table flags: Bit 7 must be zero, but was " + bit);
+ }
- /* Bit 4-6 */
- htRS = (int) ((subInputStream.readBits(3) + 1) & 0xf);
+ /* Bit 4-6 */
+ htRS = (int) ((subInputStream.readBits(3) + 1) & 0xf);
- /* Bit 1-3 */
- htPS = (int) ((subInputStream.readBits(3) + 1) & 0xf);
+ /* Bit 1-3 */
+ htPS = (int) ((subInputStream.readBits(3) + 1) & 0xf);
- /* Bit 0 */
- htOutOfBand = (int) subInputStream.readBit();
+ /* Bit 0 */
+ htOutOfBand = (int) subInputStream.readBit();
- htLow = (int) subInputStream.readBits(32); // & 0xffffffff);
- htHigh = (int) subInputStream.readBits(32); // & 0xffffffff);
- }
+ htLow = (int) subInputStream.readBits(32); // & 0xffffffff);
+ htHigh = (int) subInputStream.readBits(32); // & 0xffffffff);
+ }
- public void init(SegmentHeader header, SubInputStream sis) throws InvalidHeaderValueException, IOException,
- IntegerMaxValueException {
- subInputStream = sis;
+ public void init(SegmentHeader header, SubInputStream sis)
+ throws InvalidHeaderValueException, IOException, IntegerMaxValueException
+ {
+ subInputStream = sis;
- parseHeader();
- }
+ parseHeader();
+ }
- public int getHtOOB() {
- return htOutOfBand;
- }
+ public int getHtOOB()
+ {
+ return htOutOfBand;
+ }
- public int getHtPS() {
- return htPS;
- }
+ public int getHtPS()
+ {
+ return htPS;
+ }
- public int getHtRS() {
- return htRS;
- }
+ public int getHtRS()
+ {
+ return htRS;
+ }
- public int getHtLow() {
- return htLow;
- }
+ public int getHtLow()
+ {
+ return htLow;
+ }
- public int getHtHigh() {
- return htHigh;
- }
+ public int getHtHigh()
+ {
+ return htHigh;
+ }
- public SubInputStream getSubInputStream() {
- return subInputStream;
- }
+ public SubInputStream getSubInputStream()
+ {
+ return subInputStream;
+ }
}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/segments/TextRegion.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/segments/TextRegion.java b/src/main/java/org/apache/pdfbox/jbig2/segments/TextRegion.java
index 200b036..7cf94c0 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/segments/TextRegion.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/segments/TextRegion.java
@@ -45,942 +45,1156 @@ import org.apache.pdfbox.jbig2.util.log.LoggerFactory;
/**
* This class represented the segment type "Text region", 7.4.3, page 56.
*/
-public class TextRegion implements Region {
-
- private final Logger log = LoggerFactory.getLogger(TextRegion.class);
-
- private SubInputStream subInputStream;
-
- /** Region segment information field, 7.4.1 */
- private RegionSegmentInformation regionInfo;
-
- /** Text region segment flags, 7.4.3.1.1 */
- private short sbrTemplate;
- private short sbdsOffset; /* 6.4.8 */
- private short defaultPixel;
- private CombinationOperator combinationOperator;
- private short isTransposed;
- private short referenceCorner;
- private short logSBStrips;
- private boolean useRefinement;
- private boolean isHuffmanEncoded;
-
- /** Text region segment Huffman flags, 7.4.3.1.2 */
- private short sbHuffRSize;
- private short sbHuffRDY;
- private short sbHuffRDX;
- private short sbHuffRDHeight;
- private short sbHuffRDWidth;
- private short sbHuffDT;
- private short sbHuffDS;
- private short sbHuffFS;
-
- /** Text region refinement AT flags, 7.4.3.1.3 */
- private short sbrATX[];
- private short sbrATY[];
-
- /** Number of symbol instances, 7.4.3.1.4 */
- private long amountOfSymbolInstances;
-
- /** Further parameters */
- private long currentS;
- private int sbStrips;
- private int amountOfSymbols;
-
- private Bitmap regionBitmap;
- private ArrayList<Bitmap> symbols = new ArrayList<Bitmap>();
+public class TextRegion implements Region
+{
+
+ private final Logger log = LoggerFactory.getLogger(TextRegion.class);
+
+ private SubInputStream subInputStream;
+
+ /** Region segment information field, 7.4.1 */
+ private RegionSegmentInformation regionInfo;
+
+ /** Text region segment flags, 7.4.3.1.1 */
+ private short sbrTemplate;
+ private short sbdsOffset; /* 6.4.8 */
+ private short defaultPixel;
+ private CombinationOperator combinationOperator;
+ private short isTransposed;
+ private short referenceCorner;
+ private short logSBStrips;
+ private boolean useRefinement;
+ private boolean isHuffmanEncoded;
+
+ /** Text region segment Huffman flags, 7.4.3.1.2 */
+ private short sbHuffRSize;
+ private short sbHuffRDY;
+ private short sbHuffRDX;
+ private short sbHuffRDHeight;
+ private short sbHuffRDWidth;
+ private short sbHuffDT;
+ private short sbHuffDS;
+ private short sbHuffFS;
+
+ /** Text region refinement AT flags, 7.4.3.1.3 */
+ private short sbrATX[];
+ private short sbrATY[];
+
+ /** Number of symbol instances, 7.4.3.1.4 */
+ private long amountOfSymbolInstances;
+
+ /** Further parameters */
+ private long currentS;
+ private int sbStrips;
+ private int amountOfSymbols;
+
+ private Bitmap regionBitmap;
+ private ArrayList<Bitmap> symbols = new ArrayList<Bitmap>();
+
+ private ArithmeticDecoder arithmeticDecoder;
+ private ArithmeticIntegerDecoder integerDecoder;
+ private GenericRefinementRegion genericRefinementRegion;
+
+ private CX cxIADT;
+ private CX cxIAFS;
+ private CX cxIADS;
+ private CX cxIAIT;
+ private CX cxIARI;
+ private CX cxIARDW;
+ private CX cxIARDH;
+ private CX cxIAID;
+ private CX cxIARDX;
+ private CX cxIARDY;
+ private CX cx;
+
+ /** codeTable including a code to each symbol used in that region */
+ private int symbolCodeLength;
+ private FixedSizeTable symbolCodeTable;
+ private SegmentHeader segmentHeader;
+
+ /** User-supplied tables * */
+ private HuffmanTable fsTable;
+ private HuffmanTable dsTable;
+ private HuffmanTable table;
+ private HuffmanTable rdwTable;
+ private HuffmanTable rdhTable;
+ private HuffmanTable rdxTable;
+ private HuffmanTable rdyTable;
+ private HuffmanTable rSizeTable;
+
+ public TextRegion()
+ {
+ }
- private ArithmeticDecoder arithmeticDecoder;
- private ArithmeticIntegerDecoder integerDecoder;
- private GenericRefinementRegion genericRefinementRegion;
+ public TextRegion(SubInputStream subInputStream, SegmentHeader segmentHeader)
+ {
+ this.subInputStream = subInputStream;
+ this.regionInfo = new RegionSegmentInformation(subInputStream);
+ this.segmentHeader = segmentHeader;
+ }
- private CX cxIADT;
- private CX cxIAFS;
- private CX cxIADS;
- private CX cxIAIT;
- private CX cxIARI;
- private CX cxIARDW;
- private CX cxIARDH;
- private CX cxIAID;
- private CX cxIARDX;
- private CX cxIARDY;
- private CX cx;
+ private void parseHeader()
+ throws IOException, InvalidHeaderValueException, IntegerMaxValueException
+ {
- /** codeTable including a code to each symbol used in that region */
- private int symbolCodeLength;
- private FixedSizeTable symbolCodeTable;
- private SegmentHeader segmentHeader;
+ regionInfo.parseHeader();
- /** User-supplied tables * */
- private HuffmanTable fsTable;
- private HuffmanTable dsTable;
- private HuffmanTable table;
- private HuffmanTable rdwTable;
- private HuffmanTable rdhTable;
- private HuffmanTable rdxTable;
- private HuffmanTable rdyTable;
- private HuffmanTable rSizeTable;
+ readRegionFlags();
- public TextRegion() {
- }
+ if (isHuffmanEncoded)
+ {
+ readHuffmanFlags();
+ }
- public TextRegion(SubInputStream subInputStream, SegmentHeader segmentHeader) {
- this.subInputStream = subInputStream;
- this.regionInfo = new RegionSegmentInformation(subInputStream);
- this.segmentHeader = segmentHeader;
- }
+ readUseRefinement();
- private void parseHeader() throws IOException, InvalidHeaderValueException, IntegerMaxValueException {
+ readAmountOfSymbolInstances();
- regionInfo.parseHeader();
+ /* 7.4.3.1.7 */
+ getSymbols();
- readRegionFlags();
+ computeSymbolCodeLength();
- if (isHuffmanEncoded) {
- readHuffmanFlags();
+ this.checkInput();
}
- readUseRefinement();
-
- readAmountOfSymbolInstances();
-
- /* 7.4.3.1.7 */
- getSymbols();
-
- computeSymbolCodeLength();
-
- this.checkInput();
- }
-
- private void readRegionFlags() throws IOException {
- /* Bit 15 */
- sbrTemplate = (short) subInputStream.readBit();
+ private void readRegionFlags() throws IOException
+ {
+ /* Bit 15 */
+ sbrTemplate = (short) subInputStream.readBit();
- /* Bit 10-14 */
- sbdsOffset = (short) (subInputStream.readBits(5));
- if (sbdsOffset > 0x0f) {
- sbdsOffset -= 0x20;
- }
+ /* Bit 10-14 */
+ sbdsOffset = (short) (subInputStream.readBits(5));
+ if (sbdsOffset > 0x0f)
+ {
+ sbdsOffset -= 0x20;
+ }
- /* Bit 9 */
- defaultPixel = (short) subInputStream.readBit();
+ /* Bit 9 */
+ defaultPixel = (short) subInputStream.readBit();
- /* Bit 7-8 */
- combinationOperator = CombinationOperator.translateOperatorCodeToEnum((short) (subInputStream.readBits(2) & 0x3));
+ /* Bit 7-8 */
+ combinationOperator = CombinationOperator
+ .translateOperatorCodeToEnum((short) (subInputStream.readBits(2) & 0x3));
- /* Bit 6 */
- isTransposed = (short) subInputStream.readBit();
+ /* Bit 6 */
+ isTransposed = (short) subInputStream.readBit();
- /* Bit 4-5 */
- referenceCorner = (short) (subInputStream.readBits(2) & 0x3);
+ /* Bit 4-5 */
+ referenceCorner = (short) (subInputStream.readBits(2) & 0x3);
- /* Bit 2-3 */
- logSBStrips = (short) (subInputStream.readBits(2) & 0x3);
- sbStrips = (1 << logSBStrips);
+ /* Bit 2-3 */
+ logSBStrips = (short) (subInputStream.readBits(2) & 0x3);
+ sbStrips = (1 << logSBStrips);
- /* Bit 1 */
- if (subInputStream.readBit() == 1) {
- useRefinement = true;
- }
+ /* Bit 1 */
+ if (subInputStream.readBit() == 1)
+ {
+ useRefinement = true;
+ }
- /* Bit 0 */
- if (subInputStream.readBit() == 1) {
- isHuffmanEncoded = true;
+ /* Bit 0 */
+ if (subInputStream.readBit() == 1)
+ {
+ isHuffmanEncoded = true;
+ }
}
- }
- private void readHuffmanFlags() throws IOException {
- /* Bit 15 */
- subInputStream.readBit(); // Dirty read...
+ private void readHuffmanFlags() throws IOException
+ {
+ /* Bit 15 */
+ subInputStream.readBit(); // Dirty read...
- /* Bit 14 */
- sbHuffRSize = (short) subInputStream.readBit();
+ /* Bit 14 */
+ sbHuffRSize = (short) subInputStream.readBit();
- /* Bit 12-13 */
- sbHuffRDY = (short) (subInputStream.readBits(2) & 0xf);
+ /* Bit 12-13 */
+ sbHuffRDY = (short) (subInputStream.readBits(2) & 0xf);
- /* Bit 10-11 */
- sbHuffRDX = (short) (subInputStream.readBits(2) & 0xf);
+ /* Bit 10-11 */
+ sbHuffRDX = (short) (subInputStream.readBits(2) & 0xf);
- /* Bit 8-9 */
- sbHuffRDHeight = (short) (subInputStream.readBits(2) & 0xf);
+ /* Bit 8-9 */
+ sbHuffRDHeight = (short) (subInputStream.readBits(2) & 0xf);
- /* Bit 6-7 */
- sbHuffRDWidth = (short) (subInputStream.readBits(2) & 0xf);
+ /* Bit 6-7 */
+ sbHuffRDWidth = (short) (subInputStream.readBits(2) & 0xf);
- /* Bit 4-5 */
- sbHuffDT = (short) (subInputStream.readBits(2) & 0xf);
+ /* Bit 4-5 */
+ sbHuffDT = (short) (subInputStream.readBits(2) & 0xf);
- /* Bit 2-3 */
- sbHuffDS = (short) (subInputStream.readBits(2) & 0xf);
+ /* Bit 2-3 */
+ sbHuffDS = (short) (subInputStream.readBits(2) & 0xf);
- /* Bit 0-1 */
- sbHuffFS = (short) (subInputStream.readBits(2) & 0xf);
- }
+ /* Bit 0-1 */
+ sbHuffFS = (short) (subInputStream.readBits(2) & 0xf);
+ }
- private void readUseRefinement() throws IOException {
- if (useRefinement && sbrTemplate == 0) {
- sbrATX = new short[2];
- sbrATY = new short[2];
+ private void readUseRefinement() throws IOException
+ {
+ if (useRefinement && sbrTemplate == 0)
+ {
+ sbrATX = new short[2];
+ sbrATY = new short[2];
- /* Byte 0 */
- sbrATX[0] = subInputStream.readByte();
+ /* Byte 0 */
+ sbrATX[0] = subInputStream.readByte();
- /* Byte 1 */
- sbrATY[0] = subInputStream.readByte();
+ /* Byte 1 */
+ sbrATY[0] = subInputStream.readByte();
- /* Byte 2 */
- sbrATX[1] = subInputStream.readByte();
+ /* Byte 2 */
+ sbrATX[1] = subInputStream.readByte();
- /* Byte 3 */
- sbrATY[1] = subInputStream.readByte();
+ /* Byte 3 */
+ sbrATY[1] = subInputStream.readByte();
+ }
}
- }
- private void readAmountOfSymbolInstances() throws IOException {
- amountOfSymbolInstances = subInputStream.readBits(32) & 0xffffffff;
-
- // sanity check: don't decode more than one symbol per pixel
- long pixels = (long) regionInfo.getBitmapWidth() * (long) regionInfo.getBitmapHeight();
- if (pixels < amountOfSymbolInstances) {
- log.warn("Limiting number of decoded symbol instances to one per pixel (" + pixels + " instead of "
- + amountOfSymbolInstances + ")");
- amountOfSymbolInstances = pixels;
- }
- }
-
- private void getSymbols() throws IOException, IntegerMaxValueException, InvalidHeaderValueException {
- if (segmentHeader.getRtSegments() != null) {
- initSymbols();
+ private void readAmountOfSymbolInstances() throws IOException
+ {
+ amountOfSymbolInstances = subInputStream.readBits(32) & 0xffffffff;
+
+ // sanity check: don't decode more than one symbol per pixel
+ long pixels = (long) regionInfo.getBitmapWidth() * (long) regionInfo.getBitmapHeight();
+ if (pixels < amountOfSymbolInstances)
+ {
+ log.warn("Limiting number of decoded symbol instances to one per pixel (" + pixels
+ + " instead of " + amountOfSymbolInstances + ")");
+ amountOfSymbolInstances = pixels;
+ }
}
- }
- private void computeSymbolCodeLength() throws IOException {
- if (isHuffmanEncoded) {
- symbolIDCodeLengths();
- } else {
- symbolCodeLength = (int) Math.ceil((Math.log(amountOfSymbols) / Math.log(2)));
+ private void getSymbols()
+ throws IOException, IntegerMaxValueException, InvalidHeaderValueException
+ {
+ if (segmentHeader.getRtSegments() != null)
+ {
+ initSymbols();
+ }
}
- }
-
- private void checkInput() throws InvalidHeaderValueException {
- if (!useRefinement) {
- if (sbrTemplate != 0) {
- log.info("sbrTemplate should be 0");
- sbrTemplate = 0;
- }
+ private void computeSymbolCodeLength() throws IOException
+ {
+ if (isHuffmanEncoded)
+ {
+ symbolIDCodeLengths();
+ }
+ else
+ {
+ symbolCodeLength = (int) Math.ceil((Math.log(amountOfSymbols) / Math.log(2)));
+ }
}
- if (sbHuffFS == 2 || sbHuffRDWidth == 2 || sbHuffRDHeight == 2 || sbHuffRDX == 2 || sbHuffRDY == 2) {
- throw new InvalidHeaderValueException("Huffman flag value of text region segment is not permitted");
- }
+ private void checkInput() throws InvalidHeaderValueException
+ {
- if (!useRefinement) {
- if (sbHuffRSize != 0) {
- log.info("sbHuffRSize should be 0");
- sbHuffRSize = 0;
- }
- if (sbHuffRDY != 0) {
- log.info("sbHuffRDY should be 0");
- sbHuffRDY = 0;
- }
- if (sbHuffRDX != 0) {
- log.info("sbHuffRDX should be 0");
- sbHuffRDX = 0;
- }
- if (sbHuffRDWidth != 0) {
- log.info("sbHuffRDWidth should be 0");
- sbHuffRDWidth = 0;
- }
- if (sbHuffRDHeight != 0) {
- log.info("sbHuffRDHeight should be 0");
- sbHuffRDHeight = 0;
- }
- }
- }
+ if (!useRefinement)
+ {
+ if (sbrTemplate != 0)
+ {
+ log.info("sbrTemplate should be 0");
+ sbrTemplate = 0;
+ }
+ }
- public Bitmap getRegionBitmap() throws IOException, IntegerMaxValueException, InvalidHeaderValueException {
+ if (sbHuffFS == 2 || sbHuffRDWidth == 2 || sbHuffRDHeight == 2 || sbHuffRDX == 2
+ || sbHuffRDY == 2)
+ {
+ throw new InvalidHeaderValueException(
+ "Huffman flag value of text region segment is not permitted");
+ }
- if (!isHuffmanEncoded) {
- setCodingStatistics();
+ if (!useRefinement)
+ {
+ if (sbHuffRSize != 0)
+ {
+ log.info("sbHuffRSize should be 0");
+ sbHuffRSize = 0;
+ }
+ if (sbHuffRDY != 0)
+ {
+ log.info("sbHuffRDY should be 0");
+ sbHuffRDY = 0;
+ }
+ if (sbHuffRDX != 0)
+ {
+ log.info("sbHuffRDX should be 0");
+ sbHuffRDX = 0;
+ }
+ if (sbHuffRDWidth != 0)
+ {
+ log.info("sbHuffRDWidth should be 0");
+ sbHuffRDWidth = 0;
+ }
+ if (sbHuffRDHeight != 0)
+ {
+ log.info("sbHuffRDHeight should be 0");
+ sbHuffRDHeight = 0;
+ }
+ }
}
- createRegionBitmap();
- decodeSymbolInstances();
-
- /* 4) */
- return regionBitmap;
- }
+ public Bitmap getRegionBitmap()
+ throws IOException, IntegerMaxValueException, InvalidHeaderValueException
+ {
- private void setCodingStatistics() throws IOException {
- if (cxIADT == null)
- cxIADT = new CX(512, 1);
+ if (!isHuffmanEncoded)
+ {
+ setCodingStatistics();
+ }
- if (cxIAFS == null)
- cxIAFS = new CX(512, 1);
+ createRegionBitmap();
+ decodeSymbolInstances();
- if (cxIADS == null)
- cxIADS = new CX(512, 1);
+ /* 4) */
+ return regionBitmap;
+ }
- if (cxIAIT == null)
- cxIAIT = new CX(512, 1);
+ private void setCodingStatistics() throws IOException
+ {
+ if (cxIADT == null)
+ cxIADT = new CX(512, 1);
- if (cxIARI == null)
- cxIARI = new CX(512, 1);
+ if (cxIAFS == null)
+ cxIAFS = new CX(512, 1);
- if (cxIARDW == null)
- cxIARDW = new CX(512, 1);
+ if (cxIADS == null)
+ cxIADS = new CX(512, 1);
- if (cxIARDH == null)
- cxIARDH = new CX(512, 1);
+ if (cxIAIT == null)
+ cxIAIT = new CX(512, 1);
- if (cxIAID == null)
- cxIAID = new CX(1 << symbolCodeLength, 1);
-
- if (cxIARDX == null)
- cxIARDX = new CX(512, 1);
-
- if (cxIARDY == null)
- cxIARDY = new CX(512, 1);
+ if (cxIARI == null)
+ cxIARI = new CX(512, 1);
- if (arithmeticDecoder == null)
- arithmeticDecoder = new ArithmeticDecoder(subInputStream);
+ if (cxIARDW == null)
+ cxIARDW = new CX(512, 1);
- if (integerDecoder == null)
- integerDecoder = new ArithmeticIntegerDecoder(arithmeticDecoder);
- }
+ if (cxIARDH == null)
+ cxIARDH = new CX(512, 1);
- private void createRegionBitmap() {
+ if (cxIAID == null)
+ cxIAID = new CX(1 << symbolCodeLength, 1);
- /* 6.4.5 */
- final int width = regionInfo.getBitmapWidth();
- final int height = regionInfo.getBitmapHeight();
- regionBitmap = new Bitmap(width, height);
+ if (cxIARDX == null)
+ cxIARDX = new CX(512, 1);
- /* 1) */
- if (defaultPixel != 0) {
- Arrays.fill(regionBitmap.getByteArray(), (byte) 0xff);
- }
- }
+ if (cxIARDY == null)
+ cxIARDY = new CX(512, 1);
- private final long decodeStripT() throws IOException, InvalidHeaderValueException {
+ if (arithmeticDecoder == null)
+ arithmeticDecoder = new ArithmeticDecoder(subInputStream);
- long stripT = 0;
- /* 2) */
- if (isHuffmanEncoded) {
- /* 6.4.6 */
- if (sbHuffDT == 3) {
- // TODO test user-specified table
- if (table == null) {
- int dtNr = 0;
-
- if (sbHuffFS == 3) {
- dtNr++;
- }
-
- if (sbHuffDS == 3) {
- dtNr++;
- }
-
- table = getUserTable(dtNr);
- }
- stripT = table.decode(subInputStream);
- } else {
- stripT = StandardTables.getTable(11 + sbHuffDT).decode(subInputStream);
- }
- } else {
- stripT = integerDecoder.decode(cxIADT);
+ if (integerDecoder == null)
+ integerDecoder = new ArithmeticIntegerDecoder(arithmeticDecoder);
}
- return stripT * -(sbStrips);
- }
+ private void createRegionBitmap()
+ {
- private void decodeSymbolInstances() throws IOException, InvalidHeaderValueException, IntegerMaxValueException {
-
- long stripT = decodeStripT();
-
- /* Last two sentences of 6.4.5 2) */
- long firstS = 0;
- long instanceCounter = 0;
-
- /* 6.4.5 3 a) */
- while (instanceCounter < amountOfSymbolInstances) {
-
- final long dT = decodeDT();
- stripT += dT;
- long dfS = 0;
-
- /* 3 c) symbol instances in the strip */
- boolean first = true;
- currentS = 0;
-
- // do until OOB
- for (;;) {
- /* 3 c) i) - first symbol instance in the strip */
- if (first) {
- /* 6.4.7 */
- dfS = decodeDfS();
- firstS += dfS;
- currentS = firstS;
- first = false;
- /* 3 c) ii) - the remaining symbol instances in the strip */
- } else {
- /* 6.4.8 */
- final long idS = decodeIdS();
-
- /*
- * If result is OOB, then all the symbol instances in this strip have been decoded; proceed to step
- * 3 d) respectively 3 b). Also exit, if the expected number of instances have been decoded.
- *
- * The latter exit condition guards against pathological cases where a strip's S never contains OOB
- * and thus never terminates as illustrated in
- * https://bugs.chromium.org/p/chromium/issues/detail?id=450971 case pdfium-loop2.pdf.
- */
- if (idS == Long.MAX_VALUE || instanceCounter >= amountOfSymbolInstances)
- break;
-
- currentS += (idS + sbdsOffset);
- }
-
- /* 3 c) iii) */
- final long currentT = decodeCurrentT();
- final long t = stripT + currentT;
-
- /* 3 c) iv) */
- final long id = decodeID();
-
- /* 3 c) v) */
- final long r = decodeRI();
- /* 6.4.11 */
- final Bitmap ib = decodeIb(r, id);
-
- /* vi) */
- blit(ib, t);
-
- instanceCounter++;
- }
- }
- }
-
- private final long decodeDT() throws IOException {
- /* 3) b) */
- /* 6.4.6 */
- long dT;
- if (isHuffmanEncoded) {
- if (sbHuffDT == 3) {
- dT = table.decode(subInputStream);
- } else {
- dT = StandardTables.getTable(11 + sbHuffDT).decode(subInputStream);
- }
- } else {
- dT = integerDecoder.decode(cxIADT);
- }
-
- return (dT * sbStrips);
- }
-
- private final long decodeDfS() throws IOException, InvalidHeaderValueException {
- if (isHuffmanEncoded) {
- if (sbHuffFS == 3) {
- if (fsTable == null) {
- fsTable = getUserTable(0);
- }
- return fsTable.decode(subInputStream);
- } else {
- return StandardTables.getTable(6 + sbHuffFS).decode(subInputStream);
- }
- } else {
- return integerDecoder.decode(cxIAFS);
- }
- }
-
- private final long decodeIdS() throws IOException, InvalidHeaderValueException {
- if (isHuffmanEncoded) {
- if (sbHuffDS == 3) {
- // TODO test user-specified table
- if (dsTable == null) {
- int dsNr = 0;
- if (sbHuffFS == 3) {
- dsNr++;
- }
-
- dsTable = getUserTable(dsNr);
- }
- return dsTable.decode(subInputStream);
-
- } else {
- return StandardTables.getTable(8 + sbHuffDS).decode(subInputStream);
- }
- } else {
- return integerDecoder.decode(cxIADS);
- }
- }
-
- private final long decodeCurrentT() throws IOException {
- if (sbStrips != 1) {
- if (isHuffmanEncoded) {
- return subInputStream.readBits(logSBStrips);
- } else {
- return integerDecoder.decode(cxIAIT);
- }
- }
-
- return 0;
- }
-
- private final long decodeID() throws IOException {
- if (isHuffmanEncoded) {
- if (symbolCodeTable == null) {
- return subInputStream.readBits(symbolCodeLength);
- }
-
- return symbolCodeTable.decode(subInputStream);
- } else {
- return integerDecoder.decodeIAID(cxIAID, symbolCodeLength);
- }
- }
-
- private final long decodeRI() throws IOException {
- if (useRefinement) {
- if (isHuffmanEncoded) {
- return subInputStream.readBit();
- } else {
- return integerDecoder.decode(cxIARI);
- }
- }
- return 0;
- }
-
- private final Bitmap decodeIb(long r, long id) throws IOException, InvalidHeaderValueException,
- IntegerMaxValueException {
- Bitmap ib;
-
- if (r == 0) {
- ib = symbols.get((int) id);
- } else {
- /* 1) - 4) */
- final long rdw = decodeRdw();
- final long rdh = decodeRdh();
- final long rdx = decodeRdx();
- final long rdy = decodeRdy();
-
- /* 5) */
- /* long symInRefSize = 0; */
- if (isHuffmanEncoded) {
- /* symInRefSize = */decodeSymInRefSize();
- subInputStream.skipBits();
- }
+ /* 6.4.5 */
+ final int width = regionInfo.getBitmapWidth();
+ final int height = regionInfo.getBitmapHeight();
+ regionBitmap = new Bitmap(width, height);
- /* 6) */
- final Bitmap ibo = symbols.get((int) id);
- final int wo = ibo.getWidth();
- final int ho = ibo.getHeight();
-
- final int genericRegionReferenceDX = (int) ((rdw >> 1) + rdx);
- final int genericRegionReferenceDY = (int) ((rdh >> 1) + rdy);
-
- if (genericRefinementRegion == null) {
- genericRefinementRegion = new GenericRefinementRegion(subInputStream);
- }
-
- genericRefinementRegion.setParameters(cx, arithmeticDecoder, sbrTemplate, (int) (wo + rdw), (int) (ho + rdh),
- ibo, genericRegionReferenceDX, genericRegionReferenceDY, false, sbrATX, sbrATY);
-
- ib = genericRefinementRegion.getRegionBitmap();
-
- /* 7 */
- if (isHuffmanEncoded) {
- subInputStream.skipBits();
- }
- }
- return ib;
- }
-
- private final long decodeRdw() throws IOException, InvalidHeaderValueException {
- if (isHuffmanEncoded) {
- if (sbHuffRDWidth == 3) {
- // TODO test user-specified table
- if (rdwTable == null) {
- int rdwNr = 0;
- if (sbHuffFS == 3) {
- rdwNr++;
- }
-
- if (sbHuffDS == 3) {
- rdwNr++;
- }
-
- if (sbHuffDT == 3) {
- rdwNr++;
- }
-
- rdwTable = getUserTable(rdwNr);
- }
- return rdwTable.decode(subInputStream);
-
- } else {
- return StandardTables.getTable(14 + sbHuffRDWidth).decode(subInputStream);
- }
- } else {
- return integerDecoder.decode(cxIARDW);
- }
- }
-
- private final long decodeRdh() throws IOException, InvalidHeaderValueException {
- if (isHuffmanEncoded) {
- if (sbHuffRDHeight == 3) {
- if (rdhTable == null) {
- int rdhNr = 0;
-
- if (sbHuffFS == 3) {
- rdhNr++;
- }
-
- if (sbHuffDS == 3) {
- rdhNr++;
- }
-
- if (sbHuffDT == 3) {
- rdhNr++;
- }
-
- if (sbHuffRDWidth == 3) {
- rdhNr++;
- }
-
- rdhTable = getUserTable(rdhNr);
+ /* 1) */
+ if (defaultPixel != 0)
+ {
+ Arrays.fill(regionBitmap.getByteArray(), (byte) 0xff);
}
- return rdhTable.decode(subInputStream);
- } else {
- return StandardTables.getTable(14 + sbHuffRDHeight).decode(subInputStream);
- }
- } else {
- return integerDecoder.decode(cxIARDH);
}
- }
- private final long decodeRdx() throws IOException, InvalidHeaderValueException {
- if (isHuffmanEncoded) {
- if (sbHuffRDX == 3) {
- if (rdxTable == null) {
- int rdxNr = 0;
- if (sbHuffFS == 3) {
- rdxNr++;
- }
+ private final long decodeStripT() throws IOException, InvalidHeaderValueException
+ {
+
+ long stripT = 0;
+ /* 2) */
+ if (isHuffmanEncoded)
+ {
+ /* 6.4.6 */
+ if (sbHuffDT == 3)
+ {
+ // TODO test user-specified table
+ if (table == null)
+ {
+ int dtNr = 0;
+
+ if (sbHuffFS == 3)
+ {
+ dtNr++;
+ }
+
+ if (sbHuffDS == 3)
+ {
+ dtNr++;
+ }
+
+ table = getUserTable(dtNr);
+ }
+ stripT = table.decode(subInputStream);
+ }
+ else
+ {
+ stripT = StandardTables.getTable(11 + sbHuffDT).decode(subInputStream);
+ }
+ }
+ else
+ {
+ stripT = integerDecoder.decode(cxIADT);
+ }
- if (sbHuffDS == 3) {
- rdxNr++;
- }
+ return stripT * -(sbStrips);
+ }
- if (sbHuffDT == 3) {
- rdxNr++;
- }
+ private void decodeSymbolInstances()
+ throws IOException, InvalidHeaderValueException, IntegerMaxValueException
+ {
+
+ long stripT = decodeStripT();
+
+ /* Last two sentences of 6.4.5 2) */
+ long firstS = 0;
+ long instanceCounter = 0;
+
+ /* 6.4.5 3 a) */
+ while (instanceCounter < amountOfSymbolInstances)
+ {
+
+ final long dT = decodeDT();
+ stripT += dT;
+ long dfS = 0;
+
+ /* 3 c) symbol instances in the strip */
+ boolean first = true;
+ currentS = 0;
+
+ // do until OOB
+ for (;;)
+ {
+ /* 3 c) i) - first symbol instance in the strip */
+ if (first)
+ {
+ /* 6.4.7 */
+ dfS = decodeDfS();
+ firstS += dfS;
+ currentS = firstS;
+ first = false;
+ /* 3 c) ii) - the remaining symbol instances in the strip */
+ }
+ else
+ {
+ /* 6.4.8 */
+ final long idS = decodeIdS();
+
+ /*
+ * If result is OOB, then all the symbol instances in this strip have been decoded; proceed to step
+ * 3 d) respectively 3 b). Also exit, if the expected number of instances have been decoded.
+ *
+ * The latter exit condition guards against pathological cases where a strip's S never contains OOB
+ * and thus never terminates as illustrated in
+ * https://bugs.chromium.org/p/chromium/issues/detail?id=450971 case pdfium-loop2.pdf.
+ */
+ if (idS == Long.MAX_VALUE || instanceCounter >= amountOfSymbolInstances)
+ break;
+
+ currentS += (idS + sbdsOffset);
+ }
+
+ /* 3 c) iii) */
+ final long currentT = decodeCurrentT();
+ final long t = stripT + currentT;
+
+ /* 3 c) iv) */
+ final long id = decodeID();
+
+ /* 3 c) v) */
+ final long r = decodeRI();
+ /* 6.4.11 */
+ final Bitmap ib = decodeIb(r, id);
+
+ /* vi) */
+ blit(ib, t);
+
+ instanceCounter++;
+ }
+ }
+ }
- if (sbHuffRDWidth == 3) {
- rdxNr++;
- }
+ private final long decodeDT() throws IOException
+ {
+ /* 3) b) */
+ /* 6.4.6 */
+ long dT;
+ if (isHuffmanEncoded)
+ {
+ if (sbHuffDT == 3)
+ {
+ dT = table.decode(subInputStream);
+ }
+ else
+ {
+ dT = StandardTables.getTable(11 + sbHuffDT).decode(subInputStream);
+ }
+ }
+ else
+ {
+ dT = integerDecoder.decode(cxIADT);
+ }
- if (sbHuffRDHeight == 3) {
- rdxNr++;
- }
+ return (dT * sbStrips);
+ }
- rdxTable = getUserTable(rdxNr);
+ private final long decodeDfS() throws IOException, InvalidHeaderValueException
+ {
+ if (isHuffmanEncoded)
+ {
+ if (sbHuffFS == 3)
+ {
+ if (fsTable == null)
+ {
+ fsTable = getUserTable(0);
+ }
+ return fsTable.decode(subInputStream);
+ }
+ else
+ {
+ return StandardTables.getTable(6 + sbHuffFS).decode(subInputStream);
+ }
+ }
+ else
+ {
+ return integerDecoder.decode(cxIAFS);
}
- return rdxTable.decode(subInputStream);
- } else {
- return StandardTables.getTable(14 + sbHuffRDX).decode(subInputStream);
- }
- } else {
- return integerDecoder.decode(cxIARDX);
}
- }
-
- private final long decodeRdy() throws IOException, InvalidHeaderValueException {
- if (isHuffmanEncoded) {
- if (sbHuffRDY == 3) {
- if (rdyTable == null) {
- int rdyNr = 0;
- if (sbHuffFS == 3) {
- rdyNr++;
- }
- if (sbHuffDS == 3) {
- rdyNr++;
- }
+ private final long decodeIdS() throws IOException, InvalidHeaderValueException
+ {
+ if (isHuffmanEncoded)
+ {
+ if (sbHuffDS == 3)
+ {
+ // TODO test user-specified table
+ if (dsTable == null)
+ {
+ int dsNr = 0;
+ if (sbHuffFS == 3)
+ {
+ dsNr++;
+ }
+
+ dsTable = getUserTable(dsNr);
+ }
+ return dsTable.decode(subInputStream);
+
+ }
+ else
+ {
+ return StandardTables.getTable(8 + sbHuffDS).decode(subInputStream);
+ }
+ }
+ else
+ {
+ return integerDecoder.decode(cxIADS);
+ }
+ }
- if (sbHuffDT == 3) {
- rdyNr++;
- }
+ private final long decodeCurrentT() throws IOException
+ {
+ if (sbStrips != 1)
+ {
+ if (isHuffmanEncoded)
+ {
+ return subInputStream.readBits(logSBStrips);
+ }
+ else
+ {
+ return integerDecoder.decode(cxIAIT);
+ }
+ }
- if (sbHuffRDWidth == 3) {
- rdyNr++;
- }
+ return 0;
+ }
- if (sbHuffRDHeight == 3) {
- rdyNr++;
- }
+ private final long decodeID() throws IOException
+ {
+ if (isHuffmanEncoded)
+ {
+ if (symbolCodeTable == null)
+ {
+ return subInputStream.readBits(symbolCodeLength);
+ }
- if (sbHuffRDX == 3) {
- rdyNr++;
- }
+ return symbolCodeTable.decode(subInputStream);
+ }
+ else
+ {
+ return integerDecoder.decodeIAID(cxIAID, symbolCodeLength);
+ }
+ }
- rdyTable = getUserTable(rdyNr);
+ private final long decodeRI() throws IOException
+ {
+ if (useRefinement)
+ {
+ if (isHuffmanEncoded)
+ {
+ return subInputStream.readBit();
+ }
+ else
+ {
+ return integerDecoder.decode(cxIARI);
+ }
}
- return rdyTable.decode(subInputStream);
- } else {
- return StandardTables.getTable(14 + sbHuffRDY).decode(subInputStream);
- }
- } else {
- return integerDecoder.decode(cxIARDY);
+ return 0;
}
- }
- private final long decodeSymInRefSize() throws IOException, InvalidHeaderValueException {
- if (sbHuffRSize == 0) {
- return StandardTables.getTable(1).decode(subInputStream);
- } else {
- if (rSizeTable == null) {
- int rSizeNr = 0;
+ private final Bitmap decodeIb(long r, long id)
+ throws IOException, InvalidHeaderValueException, IntegerMaxValueException
+ {
+ Bitmap ib;
- if (sbHuffFS == 3) {
- rSizeNr++;
+ if (r == 0)
+ {
+ ib = symbols.get((int) id);
}
-
- if (sbHuffDS == 3) {
- rSizeNr++;
+ else
+ {
+ /* 1) - 4) */
+ final long rdw = decodeRdw();
+ final long rdh = decodeRdh();
+ final long rdx = decodeRdx();
+ final long rdy = decodeRdy();
+
+ /* 5) */
+ /* long symInRefSize = 0; */
+ if (isHuffmanEncoded)
+ {
+ /* symInRefSize = */decodeSymInRefSize();
+ subInputStream.skipBits();
+ }
+
+ /* 6) */
+ final Bitmap ibo = symbols.get((int) id);
+ final int wo = ibo.getWidth();
+ final int ho = ibo.getHeight();
+
+ final int genericRegionReferenceDX = (int) ((rdw >> 1) + rdx);
+ final int genericRegionReferenceDY = (int) ((rdh >> 1) + rdy);
+
+ if (genericRefinementRegion == null)
+ {
+ genericRefinementRegion = new GenericRefinementRegion(subInputStream);
+ }
+
+ genericRefinementRegion.setParameters(cx, arithmeticDecoder, sbrTemplate,
+ (int) (wo + rdw), (int) (ho + rdh), ibo, genericRegionReferenceDX,
+ genericRegionReferenceDY, false, sbrATX, sbrATY);
+
+ ib = genericRefinementRegion.getRegionBitmap();
+
+ /* 7 */
+ if (isHuffmanEncoded)
+ {
+ subInputStream.skipBits();
+ }
}
+ return ib;
+ }
- if (sbHuffDT == 3) {
- rSizeNr++;
+ private final long decodeRdw() throws IOException, InvalidHeaderValueException
+ {
+ if (isHuffmanEncoded)
+ {
+ if (sbHuffRDWidth == 3)
+ {
+ // TODO test user-specified table
+ if (rdwTable == null)
+ {
+ int rdwNr = 0;
+ if (sbHuffFS == 3)
+ {
+ rdwNr++;
+ }
+
+ if (sbHuffDS == 3)
+ {
+ rdwNr++;
+ }
+
+ if (sbHuffDT == 3)
+ {
+ rdwNr++;
+ }
+
+ rdwTable = getUserTable(rdwNr);
+ }
+ return rdwTable.decode(subInputStream);
+
+ }
+ else
+ {
+ return StandardTables.getTable(14 + sbHuffRDWidth).decode(subInputStream);
+ }
}
-
- if (sbHuffRDWidth == 3) {
- rSizeNr++;
+ else
+ {
+ return integerDecoder.decode(cxIARDW);
}
+ }
- if (sbHuffRDHeight == 3) {
- rSizeNr++;
+ private final long decodeRdh() throws IOException, InvalidHeaderValueException
+ {
+ if (isHuffmanEncoded)
+ {
+ if (sbHuffRDHeight == 3)
+ {
+ if (rdhTable == null)
+ {
+ int rdhNr = 0;
+
+ if (sbHuffFS == 3)
+ {
+ rdhNr++;
+ }
+
+ if (sbHuffDS == 3)
+ {
+ rdhNr++;
+ }
+
+ if (sbHuffDT == 3)
+ {
+ rdhNr++;
+ }
+
+ if (sbHuffRDWidth == 3)
+ {
+ rdhNr++;
+ }
+
+ rdhTable = getUserTable(rdhNr);
+ }
+ return rdhTable.decode(subInputStream);
+ }
+ else
+ {
+ return StandardTables.getTable(14 + sbHuffRDHeight).decode(subInputStream);
+ }
}
-
- if (sbHuffRDX == 3) {
- rSizeNr++;
+ else
+ {
+ return integerDecoder.decode(cxIARDH);
}
+ }
- if (sbHuffRDY == 3) {
- rSizeNr++;
+ private final long decodeRdx() throws IOException, InvalidHeaderValueException
+ {
+ if (isHuffmanEncoded)
+ {
+ if (sbHuffRDX == 3)
+ {
+ if (rdxTable == null)
+ {
+ int rdxNr = 0;
+ if (sbHuffFS == 3)
+ {
+ rdxNr++;
+ }
+
+ if (sbHuffDS == 3)
+ {
+ rdxNr++;
+ }
+
+ if (sbHuffDT == 3)
+ {
+ rdxNr++;
+ }
+
+ if (sbHuffRDWidth == 3)
+ {
+ rdxNr++;
+ }
+
+ if (sbHuffRDHeight == 3)
+ {
+ rdxNr++;
+ }
+
+ rdxTable = getUserTable(rdxNr);
+ }
+ return rdxTable.decode(subInputStream);
+ }
+ else
+ {
+ return StandardTables.getTable(14 + sbHuffRDX).decode(subInputStream);
+ }
+ }
+ else
+ {
+ return integerDecoder.decode(cxIARDX);
}
-
- rSizeTable = getUserTable(rSizeNr);
- }
- return rSizeTable.decode(subInputStream);
}
- }
-
- private final void blit(Bitmap ib, long t) {
- if (isTransposed == 0 && (referenceCorner == 2 || referenceCorner == 3)) {
- currentS += ib.getWidth() - 1;
- } else if (isTransposed == 1 && (referenceCorner == 0 || referenceCorner == 2)) {
- currentS += ib.getHeight() - 1;
+ private final long decodeRdy() throws IOException, InvalidHeaderValueException
+ {
+ if (isHuffmanEncoded)
+ {
+ if (sbHuffRDY == 3)
+ {
+ if (rdyTable == null)
+ {
+ int rdyNr = 0;
+ if (sbHuffFS == 3)
+ {
+ rdyNr++;
+ }
+
+ if (sbHuffDS == 3)
+ {
+ rdyNr++;
+ }
+
+ if (sbHuffDT == 3)
+ {
+ rdyNr++;
+ }
+
+ if (sbHuffRDWidth == 3)
+ {
+ rdyNr++;
+ }
+
+ if (sbHuffRDHeight == 3)
+ {
+ rdyNr++;
+ }
+
+ if (sbHuffRDX == 3)
+ {
+ rdyNr++;
+ }
+
+ rdyTable = getUserTable(rdyNr);
+ }
+ return rdyTable.decode(subInputStream);
+ }
+ else
+ {
+ return StandardTables.getTable(14 + sbHuffRDY).decode(subInputStream);
+ }
+ }
+ else
+ {
+ return integerDecoder.decode(cxIARDY);
+ }
}
- /* vii) */
- long s = currentS;
+ private final long decodeSymInRefSize() throws IOException, InvalidHeaderValueException
+ {
+ if (sbHuffRSize == 0)
+ {
+ return StandardTables.getTable(1).decode(subInputStream);
+ }
+ else
+ {
+ if (rSizeTable == null)
+ {
+ int rSizeNr = 0;
+
+ if (sbHuffFS == 3)
+ {
+ rSizeNr++;
+ }
+
+ if (sbHuffDS == 3)
+ {
+ rSizeNr++;
+ }
+
+ if (sbHuffDT == 3)
+ {
+ rSizeNr++;
+ }
+
+ if (sbHuffRDWidth == 3)
+ {
+ rSizeNr++;
+ }
+
+ if (sbHuffRDHeight == 3)
+ {
+ rSizeNr++;
+ }
+
+ if (sbHuffRDX == 3)
+ {
+ rSizeNr++;
+ }
+
+ if (sbHuffRDY == 3)
+ {
+ rSizeNr++;
+ }
+
+ rSizeTable = getUserTable(rSizeNr);
+ }
+ return rSizeTable.decode(subInputStream);
+ }
- /* viii) */
- if (isTransposed == 1) {
- final long swap = t;
- t = s;
- s = swap;
}
- if (referenceCorner != 1) {
- if (referenceCorner == 0) {
- // BL
- t -= ib.getHeight() - 1;
- } else if (referenceCorner == 2) {
- // BR
- t -= ib.getHeight() - 1;
- s -= ib.getWidth() - 1;
- } else if (referenceCorner == 3) {
- // TR
- s -= ib.getWidth() - 1;
- }
- }
+ private final void blit(Bitmap ib, long t)
+ {
+ if (isTransposed == 0 && (referenceCorner == 2 || referenceCorner == 3))
+ {
+ currentS += ib.getWidth() - 1;
+ }
+ else if (isTransposed == 1 && (referenceCorner == 0 || referenceCorner == 2))
+ {
+ currentS += ib.getHeight() - 1;
+ }
- Bitmaps.blit(ib, regionBitmap, (int) s, (int) t, combinationOperator);
+ /* vii) */
+ long s = currentS;
- /* x) */
- if (isTransposed == 0 && (referenceCorner == 0 || referenceCorner == 1)) {
- currentS += ib.getWidth() - 1;
- }
+ /* viii) */
+ if (isTransposed == 1)
+ {
+ final long swap = t;
+ t = s;
+ s = swap;
+ }
- if (isTransposed == 1 && (referenceCorner == 1 || referenceCorner == 3)) {
- currentS += ib.getHeight() - 1;
- }
+ if (referenceCorner != 1)
+ {
+ if (referenceCorner == 0)
+ {
+ // BL
+ t -= ib.getHeight() - 1;
+ }
+ else if (referenceCorner == 2)
+ {
+ // BR
+ t -= ib.getHeight() - 1;
+ s -= ib.getWidth() - 1;
+ }
+ else if (referenceCorner == 3)
+ {
+ // TR
+ s -= ib.getWidth() - 1;
+ }
+ }
- }
+ Bitmaps.blit(ib, regionBitmap, (int) s, (int) t, combinationOperator);
- private void initSymbols() throws IOException, IntegerMaxValueException, InvalidHeaderValueException {
- for (final SegmentHeader segment : segmentHeader.getRtSegments()) {
- if (segment.getSegmentType() == 0) {
- final SymbolDictionary sd = (SymbolDictionary) segment.getSegmentData();
+ /* x) */
+ if (isTransposed == 0 && (referenceCorner == 0 || referenceCorner == 1))
+ {
+ currentS += ib.getWidth() - 1;
+ }
- sd.cxIAID = cxIAID;
- symbols.addAll(sd.getDictionary());
- }
- }
- amountOfSymbols = symbols.size();
- }
+ if (isTransposed == 1 && (referenceCorner == 1 || referenceCorner == 3))
+ {
+ currentS += ib.getHeight() - 1;
+ }
- private HuffmanTable getUserTable(final int tablePosition) throws InvalidHeaderValueException, IOException {
- int tableCounter = 0;
+ }
- for (final SegmentHeader referredToSegmentHeader : segmentHeader.getRtSegments()) {
- if (referredToSegmentHeader.getSegmentType() == 53) {
- if (tableCounter == tablePosition) {
- final Table t = (Table) referredToSegmentHeader.getSegmentData();
- return new EncodedTable(t);
- } else {
- tableCounter++;
+ private void initSymbols()
+ throws IOException, IntegerMaxValueException, InvalidHeaderValueException
+ {
+ for (final SegmentHeader segment : segmentHeader.getRtSegments())
+ {
+ if (segment.getSegmentType() == 0)
+ {
+ final SymbolDictionary sd = (SymbolDictionary) segment.getSegmentData();
+
+ sd.cxIAID = cxIAID;
+ symbols.addAll(sd.getDictionary());
+ }
}
- }
+ amountOfSymbols = symbols.size();
}
- return null;
- }
- private void symbolIDCodeLengths() throws IOException {
- /* 1) - 2) */
- final List<Code> runCodeTable = new ArrayList<Code>();
-
- for (int i = 0; i < 35; i++) {
- final int prefLen = (int) (subInputStream.readBits(4) & 0xf);
- if (prefLen > 0) {
- runCodeTable.add(new Code(prefLen, 0, i, false));
- }
+ private HuffmanTable getUserTable(final int tablePosition)
+ throws InvalidHeaderValueException, IOException
+ {
+ int tableCounter = 0;
+
+ for (final SegmentHeader referredToSegmentHeader : segmentHeader.getRtSegments())
+ {
+ if (referredToSegmentHeader.getSegmentType() == 53)
+ {
+ if (tableCounter == tablePosition)
+ {
+ final Table t = (Table) referredToSegmentHeader.getSegmentData();
+ return new EncodedTable(t);
+ }
+ else
+ {
+ tableCounter++;
+ }
+ }
+ }
+ return null;
}
- if (JBIG2ImageReader.DEBUG)
- log.debug(HuffmanTable.codeTableToString(runCodeTable));
-
- HuffmanTable ht = new FixedSizeTable(runCodeTable);
-
- /* 3) - 5) */
- long previousCodeLength = 0;
+ private void symbolIDCodeLengths() throws IOException
+ {
+ /* 1) - 2) */
+ final List<Code> runCodeTable = new ArrayList<Code>();
+
+ for (int i = 0; i < 35; i++)
+ {
+ final int prefLen = (int) (subInputStream.readBits(4) & 0xf);
+ if (prefLen > 0)
+ {
+ runCodeTable.add(new Code(prefLen, 0, i, false));
+ }
+ }
- int counter = 0;
- final List<Code> sbSymCodes = new ArrayList<Code>();
- while (counter < amountOfSymbols) {
- final long code = ht.decode(subInputStream);
- if (code < 32) {
- if (code > 0) {
- sbSymCodes.add(new Code((int) code, 0, counter, false));
+ if (JBIG2ImageReader.DEBUG)
+ log.debug(HuffmanTable.codeTableToString(runCodeTable));
+
+ HuffmanTable ht = new FixedSizeTable(runCodeTable);
+
+ /* 3) - 5) */
+ long previousCodeLength = 0;
+
+ int counter = 0;
+ final List<Code> sbSymCodes = new ArrayList<Code>();
+ while (counter < amountOfSymbols)
+ {
+ final long code = ht.decode(subInputStream);
+ if (code < 32)
+ {
+ if (code > 0)
+ {
+ sbSymCodes.add(new Code((int) code, 0, counter, false));
+ }
+
+ previousCodeLength = code;
+ counter++;
+ }
+ else
+ {
+
+ long runLength = 0;
+ long currCodeLength = 0;
+ if (code == 32)
+ {
+ runLength = 3 + subInputStream.readBits(2);
+ if (counter > 0)
+ {
+ currCodeLength = previousCodeLength;
+ }
+ }
+ else if (code == 33)
+ {
+ runLength = 3 + subInputStream.readBits(3);
+ }
+ else if (code == 34)
+ {
+ runLength = 11 + subInputStream.readBits(7);
+ }
+
+ for (int j = 0; j < runLength; j++)
+ {
+ if (currCodeLength > 0)
+ {
+ sbSymCodes.add(new Code((int) currCodeLength, 0, counter, false));
+ }
+ counter++;
+ }
+ }
}
- previousCodeLength = code;
- counter++;
- } else {
+ /* 6) - Skip over remaining bits in the last Byte read */
+ subInputStream.skipBits();
- long runLength = 0;
- long currCodeLength = 0;
- if (code == 32) {
- runLength = 3 + subInputStream.readBits(2);
- if (counter > 0) {
- currCodeLength = previousCodeLength;
- }
- } else if (code == 33) {
- runLength = 3 + subInputStream.readBits(3);
- } else if (code == 34) {
- runLength = 11 + subInputStream.readBits(7);
- }
+ /* 7) */
+ symbolCodeTable = new FixedSizeTable(sbSymCodes);
- for (int j = 0; j < runLength; j++) {
- if (currCodeLength > 0) {
- sbSymCodes.add(new Code((int) currCodeLength, 0, counter, false));
- }
- counter++;
- }
- }
}
- /* 6) - Skip over remaining bits in the last Byte read */
- subInputStream.skipBits();
+ public void init(SegmentHeader header, SubInputStream sis)
+ throws InvalidHeaderValueException, IntegerMaxValueException, IOException
+ {
+ this.segmentHeader = header;
+ this.subInputStream = sis;
+ this.regionInfo = new RegionSegmentInformation(subInputStream);
+ parseHeader();
+ }
+
+ protected void setContexts(CX cx, CX cxIADT, CX cxIAFS, CX cxIADS, CX cxIAIT, CX cxIAID,
+ CX cxIARDW, CX cxIARDH, CX cxIARDX, CX cxIARDY)
+ {
+ this.cx = cx;
+
+ this.cxIADT = cxIADT;
+ this.cxIAFS = cxIAFS;
+ this.cxIADS = cxIADS;
+ this.cxIAIT = cxIAIT;
+
+ this.cxIAID = cxIAID;
- /* 7) */
- symbolCodeTable = new FixedSizeTable(sbSymCodes);
+ this.cxIARDW = cxIARDW;
+ this.cxIARDH = cxIARDH;
+ this.cxIARDX = cxIARDX;
+ this.cxIARDY = cxIARDY;
+ }
- }
+ protected void setParameters(ArithmeticDecoder arithmeticDecoder,
+ ArithmeticIntegerDecoder iDecoder, boolean isHuffmanEncoded, boolean sbRefine, int sbw,
+ int sbh, long sbNumInstances, int sbStrips, int sbNumSyms, short sbDefaultPixel,
+ short sbCombinationOperator, short transposed, short refCorner, short sbdsOffset,
+ short sbHuffFS, short sbHuffDS, short sbHuffDT, short sbHuffRDWidth,
+ short sbHuffRDHeight, short sbHuffRDX, short sbHuffRDY, short sbHuffRSize,
+ short sbrTemplate, short sbrATX[], short sbrATY[], ArrayList<Bitmap> sbSyms,
+ int sbSymCodeLen)
+ {
+
+ this.arithmeticDecoder = arithmeticDecoder;
+
+ this.integerDecoder = iDecoder;
+
+ this.isHuffmanEncoded = isHuffmanEncoded;
+ this.useRefinement = sbRefine;
+
+ this.regionInfo.setBitmapWidth(sbw);
+ this.regionInfo.setBitmapHeight(sbh);
+
+ this.amountOfSymbolInstances = sbNumInstances;
+ this.sbStrips = sbStrips;
+ this.amountOfSymbols = sbNumSyms;
+ this.defaultPixel = sbDefaultPixel;
+ this.combinationOperator = CombinationOperator
+ .translateOperatorCodeToEnum(sbCombinationOperator);
+ this.isTransposed = transposed;
+ this.referenceCorner = refCorner;
+ this.sbdsOffset = sbdsOffset;
+
+ this.sbHuffFS = sbHuffFS;
+ this.sbHuffDS = sbHuffDS;
+ this.sbHuffDT = sbHuffDT;
+ this.sbHuffRDWidth = sbHuffRDWidth;
+ this.sbHuffRDHeight = sbHuffRDHeight;
+ this.sbHuffRDX = sbHuffRDX;
+ this.sbHuffRDY = sbHuffRDY;
+ this.sbHuffRSize = sbHuffRSize;
+
+ this.sbrTemplate = sbrTemplate;
+ this.sbrATX = sbrATX;
+ this.sbrATY = sbrATY;
+
+ this.symbols = sbSyms;
+ this.symbolCodeLength = sbSymCodeLen;
+ }
- public void init(SegmentHeader header, SubInputStream sis) throws InvalidHeaderValueException,
- IntegerMaxValueException, IOException {
- this.segmentHeader = header;
- this.subInputStream = sis;
- this.regionInfo = new RegionSegmentInformation(subInputStream);
- parseHeader();
- }
-
- protected void setContexts(CX cx, CX cxIADT, CX cxIAFS, CX cxIADS, CX cxIAIT, CX cxIAID, CX cxIARDW, CX cxIARDH,
- CX cxIARDX, CX cxIARDY) {
- this.cx = cx;
-
- this.cxIADT = cxIADT;
- this.cxIAFS = cxIAFS;
- this.cxIADS = cxIADS;
- this.cxIAIT = cxIAIT;
-
- this.cxIAID = cxIAID;
-
- this.cxIARDW = cxIARDW;
- this.cxIARDH = cxIARDH;
- this.cxIARDX = cxIARDX;
- this.cxIARDY = cxIARDY;
- }
-
- protected void setParameters(ArithmeticDecoder arithmeticDecoder, ArithmeticIntegerDecoder iDecoder,
- boolean isHuffmanEncoded, boolean sbRefine, int sbw, int sbh, long sbNumInstances, int sbStrips, int sbNumSyms,
- short sbDefaultPixel, short sbCombinationOperator, short transposed, short refCorner, short sbdsOffset,
- short sbHuffFS, short sbHuffDS, short sbHuffDT, short sbHuffRDWidth, short sbHuffRDHeight, short sbHuffRDX,
- short sbHuffRDY, short sbHuffRSize, short sbrTemplate, short sbrATX[], short sbrATY[], ArrayList<Bitmap> sbSyms,
- int sbSymCodeLen) {
-
- this.arithmeticDecoder = arithmeticDecoder;
-
- this.integerDecoder = iDecoder;
-
- this.isHuffmanEncoded = isHuffmanEncoded;
- this.useRefinement = sbRefine;
-
- this.regionInfo.setBitmapWidth(sbw);
- this.regionInfo.setBitmapHeight(sbh);
-
- this.amountOfSymbolInstances = sbNumInstances;
- this.sbStrips = sbStrips;
- this.amountOfSymbols = sbNumSyms;
- this.defaultPixel = sbDefaultPixel;
- this.combinationOperator = CombinationOperator.translateOperatorCodeToEnum(sbCombinationOperator);
- this.isTransposed = transposed;
- this.referenceCorner = refCorner;
- this.sbdsOffset = sbdsOffset;
-
- this.sbHuffFS = sbHuffFS;
- this.sbHuffDS = sbHuffDS;
- this.sbHuffDT = sbHuffDT;
- this.sbHuffRDWidth = sbHuffRDWidth;
- this.sbHuffRDHeight = sbHuffRDHeight;
- this.sbHuffRDX = sbHuffRDX;
- this.sbHuffRDY = sbHuffRDY;
- this.sbHuffRSize = sbHuffRSize;
-
- this.sbrTemplate = sbrTemplate;
- this.sbrATX = sbrATX;
- this.sbrATY = sbrATY;
-
- this.symbols = sbSyms;
- this.symbolCodeLength = sbSymCodeLen;
- }
-
- public RegionSegmentInformation getRegionInfo() {
- return regionInfo;
- }
+ public RegionSegmentInformation getRegionInfo()
+ {
+ return regionInfo;
+ }
}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/util/CombinationOperator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/util/CombinationOperator.java b/src/main/java/org/apache/pdfbox/jbig2/util/CombinationOperator.java
index 1d40717..888dfff 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/util/CombinationOperator.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/util/CombinationOperator.java
@@ -20,21 +20,24 @@ package org.apache.pdfbox.jbig2.util;
/**
* This enumeration keeps the available logical operator defined in the JBIG2 ISO standard.
*/
-public enum CombinationOperator {
- OR, AND, XOR, XNOR, REPLACE;
+public enum CombinationOperator
+{
+ OR, AND, XOR, XNOR, REPLACE;
- public static CombinationOperator translateOperatorCodeToEnum(short combinationOperatorCode) {
- switch (combinationOperatorCode){
- case 0 :
- return OR;
- case 1 :
- return AND;
- case 2 :
- return XOR;
- case 3 :
- return XNOR;
- default :
- return REPLACE;
+ public static CombinationOperator translateOperatorCodeToEnum(short combinationOperatorCode)
+ {
+ switch (combinationOperatorCode)
+ {
+ case 0:
+ return OR;
+ case 1:
+ return AND;
+ case 2:
+ return XOR;
+ case 3:
+ return XNOR;
+ default:
+ return REPLACE;
+ }
}
- }
}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/util/DictionaryViewer.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/util/DictionaryViewer.java b/src/main/java/org/apache/pdfbox/jbig2/util/DictionaryViewer.java
index 2659082..2177d78 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/util/DictionaryViewer.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/util/DictionaryViewer.java
@@ -24,36 +24,42 @@ import org.apache.pdfbox.jbig2.TestImage;
import org.apache.pdfbox.jbig2.image.Bitmaps;
/**
- * This class is for debug purpose only. The {@code DictionaryViewer} is able to show a single
- * bitmap or all symbol bitmaps.
+ * This class is for debug purpose only. The {@code DictionaryViewer} is able to show a single bitmap or all symbol
+ * bitmaps.
*/
-class DictionaryViewer {
+class DictionaryViewer
+{
- public static void show(Bitmap b) {
- new TestImage(Bitmaps.asBufferedImage(b));
- }
+ public static void show(Bitmap b)
+ {
+ new TestImage(Bitmaps.asBufferedImage(b));
+ }
- public static void show(List<Bitmap> symbols) {
- int width = 0;
- int height = 0;
+ public static void show(List<Bitmap> symbols)
+ {
+ int width = 0;
+ int height = 0;
- for (Bitmap b : symbols) {
- width += b.getWidth();
+ for (Bitmap b : symbols)
+ {
+ width += b.getWidth();
- if (b.getHeight() > height) {
- height = b.getHeight();
- }
- }
+ if (b.getHeight() > height)
+ {
+ height = b.getHeight();
+ }
+ }
- Bitmap result = new Bitmap(width, height);
+ Bitmap result = new Bitmap(width, height);
- int xOffset = 0;
+ int xOffset = 0;
- for (Bitmap b : symbols) {
- Bitmaps.blit(b, result, xOffset, 0, CombinationOperator.REPLACE);
- xOffset += b.getWidth();
- }
+ for (Bitmap b : symbols)
+ {
+ Bitmaps.blit(b, result, xOffset, 0, CombinationOperator.REPLACE);
+ xOffset += b.getWidth();
+ }
- new TestImage(Bitmaps.asBufferedImage(result));
- }
-}
\ No newline at end of file
+ new TestImage(Bitmaps.asBufferedImage(result));
+ }
+}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/util/ServiceLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/util/ServiceLookup.java b/src/main/java/org/apache/pdfbox/jbig2/util/ServiceLookup.java
index 0c3f78e..35852fa 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/util/ServiceLookup.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/util/ServiceLookup.java
@@ -20,24 +20,29 @@ package org.apache.pdfbox.jbig2.util;
import java.util.Iterator;
import java.util.ServiceLoader;
-public class ServiceLookup<B> {
+public class ServiceLookup<B>
+{
- public Iterator<B> getServices(Class<B> cls) {
- return getServices(cls, null);
- }
+ public Iterator<B> getServices(Class<B> cls)
+ {
+ return getServices(cls, null);
+ }
- public Iterator<B> getServices(Class<B> cls, ClassLoader clsLoader) {
- Iterator<B> services = ServiceLoader.load(cls).iterator();
+ public Iterator<B> getServices(Class<B> cls, ClassLoader clsLoader)
+ {
+ Iterator<B> services = ServiceLoader.load(cls).iterator();
- if (!services.hasNext()) {
- services = ServiceLoader.load(cls, cls.getClass().getClassLoader()).iterator();
- }
+ if (!services.hasNext())
+ {
+ services = ServiceLoader.load(cls, cls.getClass().getClassLoader()).iterator();
+ }
- if (!services.hasNext() && clsLoader != null) {
- services = ServiceLoader.load(cls, clsLoader).iterator();
- }
+ if (!services.hasNext() && clsLoader != null)
+ {
+ services = ServiceLoader.load(cls, clsLoader).iterator();
+ }
- return services;
- }
+ return services;
+ }
}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/util/Utils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/util/Utils.java b/src/main/java/org/apache/pdfbox/jbig2/util/Utils.java
index daeb3b3..eed7537 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/util/Utils.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/util/Utils.java
@@ -20,84 +20,89 @@ package org.apache.pdfbox.jbig2.util;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
-public class Utils {
+public class Utils
+{
- /**
- * Create a rectangle with the same area as the given input rectangle but with all of its edges
- * snapped (rounded) to the integer grid. The resulting rectangle is guaranteed to cover
- * <em>all</em> of the input rectangle's area, so that
- * <code>enlargeToGrid(r).contains(r) == true</code> holds. This can be depicted as the edges
- * being stretched in an outward direction.
- *
- * @param r the given rectangle
- * @return the resulting rectangle
- */
- public static Rectangle enlargeRectToGrid(Rectangle2D r) {
- final int x0 = floor(r.getMinX());
- final int y0 = floor(r.getMinY());
- final int x1 = ceil(r.getMaxX());
- final int y1 = ceil(r.getMaxY());
- return new Rectangle(x0, y0, x1 - x0, y1 - y0);
- }
-
- /**
- * Return a new rectangle which covers the area of the given rectangle with an additional margin
- * on the sides.
- *
- * @param r the given rectangle
- * @param marginX horizontal value of the additional margin
- * @param marginY vertical value of the additional margin
- * @return the resulting rectangle
- */
- public static Rectangle2D dilateRect(Rectangle2D r, double marginX, double marginY) {
- return new Rectangle2D.Double(r.getX() - marginX, r.getY() - marginY, r.getWidth() + 2 * marginX, r.getHeight() + 2
- * marginY);
- }
-
- /**
- * Clamp the value into the range [min..max].
- *
- * @param value input value
- * @param min minimal value
- * @param max maximal value
- * @return the clamped value
- */
- public static double clamp(double value, double min, double max) {
- return Math.min(max, Math.max(value, min));
- }
-
- private static final int BIG_ENOUGH_INT = 16 * 1024;
- private static final double BIG_ENOUGH_FLOOR = BIG_ENOUGH_INT;
- private static final double BIG_ENOUGH_ROUND = BIG_ENOUGH_INT + 0.5;
-
- /**
- * A fast implementation of {@link Math#floor(double)}.
- *
- * @param x the argument
- * @return resulting floor value
- */
- public static int floor(double x) {
- return (int) (x + BIG_ENOUGH_FLOOR) - BIG_ENOUGH_INT;
- }
+ /**
+ * Create a rectangle with the same area as the given input rectangle but with all of its edges snapped (rounded) to
+ * the integer grid. The resulting rectangle is guaranteed to cover <em>all</em> of the input rectangle's area, so
+ * that <code>enlargeToGrid(r).contains(r) == true</code> holds. This can be depicted as the edges being stretched
+ * in an outward direction.
+ *
+ * @param r the given rectangle
+ * @return the resulting rectangle
+ */
+ public static Rectangle enlargeRectToGrid(Rectangle2D r)
+ {
+ final int x0 = floor(r.getMinX());
+ final int y0 = floor(r.getMinY());
+ final int x1 = ceil(r.getMaxX());
+ final int y1 = ceil(r.getMaxY());
+ return new Rectangle(x0, y0, x1 - x0, y1 - y0);
+ }
- /**
- * A fast implementation of {@link Math#round(double)}.
- *
- * @param x the argument
- * @return rounded value
- */
- public static int round(double x) {
- return (int) (x + BIG_ENOUGH_ROUND) - BIG_ENOUGH_INT;
- }
+ /**
+ * Return a new rectangle which covers the area of the given rectangle with an additional margin on the sides.
+ *
+ * @param r the given rectangle
+ * @param marginX horizontal value of the additional margin
+ * @param marginY vertical value of the additional margin
+ * @return the resulting rectangle
+ */
+ public static Rectangle2D dilateRect(Rectangle2D r, double marginX, double marginY)
+ {
+ return new Rectangle2D.Double(r.getX() - marginX, r.getY() - marginY,
+ r.getWidth() + 2 * marginX, r.getHeight() + 2 * marginY);
+ }
+
+ /**
+ * Clamp the value into the range [min..max].
+ *
+ * @param value input value
+ * @param min minimal value
+ * @param max maximal value
+ * @return the clamped value
+ */
+ public static double clamp(double value, double min, double max)
+ {
+ return Math.min(max, Math.max(value, min));
+ }
+
+ private static final int BIG_ENOUGH_INT = 16 * 1024;
+ private static final double BIG_ENOUGH_FLOOR = BIG_ENOUGH_INT;
+ private static final double BIG_ENOUGH_ROUND = BIG_ENOUGH_INT + 0.5;
+
+ /**
+ * A fast implementation of {@link Math#floor(double)}.
+ *
+ * @param x the argument
+ * @return resulting floor value
+ */
+ public static int floor(double x)
+ {
+ return (int) (x + BIG_ENOUGH_FLOOR) - BIG_ENOUGH_INT;
+ }
+
+ /**
+ * A fast implementation of {@link Math#round(double)}.
+ *
+ * @param x the argument
+ * @return rounded value
+ */
+ public static int round(double x)
+ {
+ return (int) (x + BIG_ENOUGH_ROUND) - BIG_ENOUGH_INT;
+ }
+
+ /**
+ * A fast implementation of {@link Math#ceil(double)}.
+ *
+ * @param x the argument
+ * @return resulting ceil value
+ */
+ public static int ceil(double x)
+ {
+ return BIG_ENOUGH_INT - (int) (BIG_ENOUGH_FLOOR - x);
+ }
- /**
- * A fast implementation of {@link Math#ceil(double)}.
- *
- * @param x the argument
- * @return resulting ceil value
- */
- public static int ceil(double x) {
- return BIG_ENOUGH_INT - (int) (BIG_ENOUGH_FLOOR - x);
- }
-
}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/util/cache/Cache.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/util/cache/Cache.java b/src/main/java/org/apache/pdfbox/jbig2/util/cache/Cache.java
index 6454784..726e4e9 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/util/cache/Cache.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/util/cache/Cache.java
@@ -17,31 +17,32 @@
package org.apache.pdfbox.jbig2.util.cache;
-public interface Cache {
+public interface Cache
+{
-/**
- *
- * @param key value to be used as key
- * @param value value to be stored
- * @param sizeEstimate estimated size
- *
- * @return the old object, that was replaced if present. Otherwise {@code null}.
- */
- Object put(Object key, Object value, int sizeEstimate);
+ /**
+ *
+ * @param key value to be used as key
+ * @param value value to be stored
+ * @param sizeEstimate estimated size
+ *
+ * @return the old object, that was replaced if present. Otherwise {@code null}.
+ */
+ Object put(Object key, Object value, int sizeEstimate);
- Object get(Object key);
+ Object get(Object key);
- /**
- * Removes all mappings from a map (optional operation).
- *
- * @throws UnsupportedOperationException if {@code clear()} is not supported by the map.
- */
- void clear();
+ /**
+ * Removes all mappings from a map (optional operation).
+ *
+ * @throws UnsupportedOperationException if {@code clear()} is not supported by the map.
+ */
+ void clear();
- /**
- *
- * @param key the key of the to be removed object
- * @return the removed object, if present. Otherwise {@code null}.
- */
- Object remove(Object key);
-}
\ No newline at end of file
+ /**
+ *
+ * @param key the key of the to be removed object
+ * @return the removed object, if present. Otherwise {@code null}.
+ */
+ Object remove(Object key);
+}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/util/cache/CacheBridge.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/util/cache/CacheBridge.java b/src/main/java/org/apache/pdfbox/jbig2/util/cache/CacheBridge.java
index 19338b1..171488f 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/util/cache/CacheBridge.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/util/cache/CacheBridge.java
@@ -17,8 +17,9 @@
package org.apache.pdfbox.jbig2.util.cache;
-public interface CacheBridge {
+public interface CacheBridge
+{
- Cache getCache();
+ Cache getCache();
}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/util/cache/CacheFactory.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/util/cache/CacheFactory.java b/src/main/java/org/apache/pdfbox/jbig2/util/cache/CacheFactory.java
index 718c04a..a39d37e 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/util/cache/CacheFactory.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/util/cache/CacheFactory.java
@@ -22,34 +22,40 @@ import java.util.Iterator;
import org.apache.pdfbox.jbig2.util.ServiceLookup;
/**
- * Retrieves a {@link Cache} via registered {@link CacheBridge} through
- * <code>META-INF/services</code> lookup.
+ * Retrieves a {@link Cache} via registered {@link CacheBridge} through <code>META-INF/services</code> lookup.
*/
-public class CacheFactory {
-
- private static CacheBridge cacheBridge;
-
- private static ClassLoader clsLoader;
-
- public static Cache getCache(ClassLoader clsLoader) {
- if (null == cacheBridge) {
- final ServiceLookup<CacheBridge> serviceLookup = new ServiceLookup<CacheBridge>();
- final Iterator<CacheBridge> cacheBridgeServices = serviceLookup.getServices(CacheBridge.class, clsLoader);
-
- if (!cacheBridgeServices.hasNext()) {
- throw new IllegalStateException("No implementation of " + CacheBridge.class
- + " was avaliable using META-INF/services lookup");
- }
- cacheBridge = cacheBridgeServices.next();
+public class CacheFactory
+{
+
+ private static CacheBridge cacheBridge;
+
+ private static ClassLoader clsLoader;
+
+ public static Cache getCache(ClassLoader clsLoader)
+ {
+ if (null == cacheBridge)
+ {
+ final ServiceLookup<CacheBridge> serviceLookup = new ServiceLookup<CacheBridge>();
+ final Iterator<CacheBridge> cacheBridgeServices = serviceLookup
+ .getServices(CacheBridge.class, clsLoader);
+
+ if (!cacheBridgeServices.hasNext())
+ {
+ throw new IllegalStateException("No implementation of " + CacheBridge.class
+ + " was avaliable using META-INF/services lookup");
+ }
+ cacheBridge = cacheBridgeServices.next();
+ }
+ return cacheBridge.getCache();
}
- return cacheBridge.getCache();
- }
- public static Cache getCache() {
- return getCache(clsLoader != null ? clsLoader : CacheBridge.class.getClassLoader());
- }
+ public static Cache getCache()
+ {
+ return getCache(clsLoader != null ? clsLoader : CacheBridge.class.getClassLoader());
+ }
- public static void setClassLoader(ClassLoader clsLoader) {
- CacheFactory.clsLoader = clsLoader;
- }
+ public static void setClassLoader(ClassLoader clsLoader)
+ {
+ CacheFactory.clsLoader = clsLoader;
+ }
}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCache.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCache.java b/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCache.java
index 35a1407..8374848 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCache.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCache.java
@@ -20,31 +20,37 @@ package org.apache.pdfbox.jbig2.util.cache;
import java.lang.ref.SoftReference;
import java.util.HashMap;
-public class SoftReferenceCache implements Cache {
-
- private HashMap<Object, SoftReference<?>> cache = new HashMap<Object, SoftReference<?>>();
-
- public Object put(Object key, Object value, int sizeEstimate) {
- SoftReference<Object> softReference = new SoftReference<Object>(value);
- SoftReference<?> oldValue = cache.put(key, softReference);
- return getValueNullSafe(oldValue);
- }
-
- public Object get(Object key) {
- SoftReference<?> softReference = cache.get(key);
- return getValueNullSafe(softReference);
- }
-
- public void clear() {
- cache.clear();
- }
-
- public Object remove(Object key) {
- SoftReference<?> removedObj = cache.remove(key);
- return getValueNullSafe(removedObj);
- }
-
- private Object getValueNullSafe(SoftReference<?> softReference) {
- return softReference == null ? null : softReference.get();
- }
+public class SoftReferenceCache implements Cache
+{
+
+ private HashMap<Object, SoftReference<?>> cache = new HashMap<Object, SoftReference<?>>();
+
+ public Object put(Object key, Object value, int sizeEstimate)
+ {
+ SoftReference<Object> softReference = new SoftReference<Object>(value);
+ SoftReference<?> oldValue = cache.put(key, softReference);
+ return getValueNullSafe(oldValue);
+ }
+
+ public Object get(Object key)
+ {
+ SoftReference<?> softReference = cache.get(key);
+ return getValueNullSafe(softReference);
+ }
+
+ public void clear()
+ {
+ cache.clear();
+ }
+
+ public Object remove(Object key)
+ {
+ SoftReference<?> removedObj = cache.remove(key);
+ return getValueNullSafe(removedObj);
+ }
+
+ private Object getValueNullSafe(SoftReference<?> softReference)
+ {
+ return softReference == null ? null : softReference.get();
+ }
}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCacheBridge.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCacheBridge.java b/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCacheBridge.java
index 59f039f..4fb9226 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCacheBridge.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/util/cache/SoftReferenceCacheBridge.java
@@ -17,12 +17,14 @@
package org.apache.pdfbox.jbig2.util.cache;
-public class SoftReferenceCacheBridge implements CacheBridge {
+public class SoftReferenceCacheBridge implements CacheBridge
+{
- private static final SoftReferenceCache cache = new SoftReferenceCache();
+ private static final SoftReferenceCache cache = new SoftReferenceCache();
- public Cache getCache() {
- return cache;
- }
+ public Cache getCache()
+ {
+ return cache;
+ }
}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/util/log/JDKLogger.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/util/log/JDKLogger.java b/src/main/java/org/apache/pdfbox/jbig2/util/log/JDKLogger.java
index 70f3e0e..97002c1 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/util/log/JDKLogger.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/util/log/JDKLogger.java
@@ -19,70 +19,87 @@ package org.apache.pdfbox.jbig2.util.log;
import java.util.logging.Level;
-public class JDKLogger implements Logger {
- final java.util.logging.Logger wrappedLogger;
-
- public JDKLogger(java.util.logging.Logger logger) {
- wrappedLogger = logger;
- }
-
- public void debug(String msg) {
- wrappedLogger.log(Level.FINE, msg);
- }
-
- public void debug(String msg, Throwable t) {
- wrappedLogger.log(Level.FINE, msg, t);
- }
-
- public void info(String msg) {
- wrappedLogger.log(Level.INFO, msg);
- }
-
- public void info(String msg, Throwable t) {
- wrappedLogger.log(Level.INFO, msg, t);
- }
-
- public void warn(String msg) {
- wrappedLogger.log(Level.WARNING, msg);
- }
-
- public void warn(String msg, Throwable t) {
- wrappedLogger.log(Level.WARNING, msg, t);
- }
-
- public void fatal(String msg) {
- wrappedLogger.log(Level.SEVERE, msg);
- }
-
- public void fatal(String msg, Throwable t) {
- wrappedLogger.log(Level.SEVERE, msg, t);
- }
-
- public void error(String msg) {
- wrappedLogger.log(Level.SEVERE, msg);
- }
-
- public void error(String msg, Throwable t) {
- wrappedLogger.log(Level.SEVERE, msg, t);
- }
-
- public boolean isDebugEnabled() {
- return wrappedLogger.isLoggable(Level.FINE);
- }
-
- public boolean isInfoEnabled() {
- return wrappedLogger.isLoggable(Level.INFO);
- }
-
- public boolean isWarnEnabled() {
- return wrappedLogger.isLoggable(Level.WARNING);
- }
-
- public boolean isFatalEnabled() {
- return wrappedLogger.isLoggable(Level.SEVERE);
- }
-
- public boolean isErrorEnabled() {
- return wrappedLogger.isLoggable(Level.SEVERE);
- }
+public class JDKLogger implements Logger
+{
+ final java.util.logging.Logger wrappedLogger;
+
+ public JDKLogger(java.util.logging.Logger logger)
+ {
+ wrappedLogger = logger;
+ }
+
+ public void debug(String msg)
+ {
+ wrappedLogger.log(Level.FINE, msg);
+ }
+
+ public void debug(String msg, Throwable t)
+ {
+ wrappedLogger.log(Level.FINE, msg, t);
+ }
+
+ public void info(String msg)
+ {
+ wrappedLogger.log(Level.INFO, msg);
+ }
+
+ public void info(String msg, Throwable t)
+ {
+ wrappedLogger.log(Level.INFO, msg, t);
+ }
+
+ public void warn(String msg)
+ {
+ wrappedLogger.log(Level.WARNING, msg);
+ }
+
+ public void warn(String msg, Throwable t)
+ {
+ wrappedLogger.log(Level.WARNING, msg, t);
+ }
+
+ public void fatal(String msg)
+ {
+ wrappedLogger.log(Level.SEVERE, msg);
+ }
+
+ public void fatal(String msg, Throwable t)
+ {
+ wrappedLogger.log(Level.SEVERE, msg, t);
+ }
+
+ public void error(String msg)
+ {
+ wrappedLogger.log(Level.SEVERE, msg);
+ }
+
+ public void error(String msg, Throwable t)
+ {
+ wrappedLogger.log(Level.SEVERE, msg, t);
+ }
+
+ public boolean isDebugEnabled()
+ {
+ return wrappedLogger.isLoggable(Level.FINE);
+ }
+
+ public boolean isInfoEnabled()
+ {
+ return wrappedLogger.isLoggable(Level.INFO);
+ }
+
+ public boolean isWarnEnabled()
+ {
+ return wrappedLogger.isLoggable(Level.WARNING);
+ }
+
+ public boolean isFatalEnabled()
+ {
+ return wrappedLogger.isLoggable(Level.SEVERE);
+ }
+
+ public boolean isErrorEnabled()
+ {
+ return wrappedLogger.isLoggable(Level.SEVERE);
+ }
}
http://git-wip-us.apache.org/repos/asf/pdfbox-jbig2/blob/30839c32/src/main/java/org/apache/pdfbox/jbig2/util/log/JDKLoggerBridge.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/pdfbox/jbig2/util/log/JDKLoggerBridge.java b/src/main/java/org/apache/pdfbox/jbig2/util/log/JDKLoggerBridge.java
index bc82d9e..f096734 100644
--- a/src/main/java/org/apache/pdfbox/jbig2/util/log/JDKLoggerBridge.java
+++ b/src/main/java/org/apache/pdfbox/jbig2/util/log/JDKLoggerBridge.java
@@ -17,10 +17,12 @@
package org.apache.pdfbox.jbig2.util.log;
-public class JDKLoggerBridge implements LoggerBridge {
+public class JDKLoggerBridge implements LoggerBridge
+{
- public Logger getLogger(Class<?> classToBeLogged) {
- return new JDKLogger(java.util.logging.Logger.getLogger(classToBeLogged.getName()));
- }
+ public Logger getLogger(Class<?> classToBeLogged)
+ {
+ return new JDKLogger(java.util.logging.Logger.getLogger(classToBeLogged.getName()));
+ }
}