libjpedal-jbig2-20100117/0000755000175000017500000000000011213455610014546 5ustar moellermoellerlibjpedal-jbig2-20100117/org/0000755000175000017500000000000011213455610015335 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/0000755000175000017500000000000011213455610016574 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/0000755000175000017500000000000011213455610017571 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/decoders/0000755000175000017500000000000011213455610021361 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/decoders/ArithmeticDecoder.java0000644000175000017500000002703111213455610025606 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * ArithmeticDecoder.java * --------------- */ package org.jpedal.jbig2.decoders; import java.io.IOException; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.io.StreamReader; import org.jpedal.jbig2.util.BinaryOperation; public class ArithmeticDecoder { private StreamReader reader; public ArithmeticDecoderStats genericRegionStats, refinementRegionStats; public ArithmeticDecoderStats iadhStats, iadwStats, iaexStats, iaaiStats, iadtStats, iaitStats, iafsStats, iadsStats, iardxStats, iardyStats, iardwStats, iardhStats, iariStats, iaidStats; int contextSize[] = { 16, 13, 10, 10 }, referredToContextSize[] = { 13, 10 }; long buffer0, buffer1; long c, a; long previous; int counter; private ArithmeticDecoder() {} public ArithmeticDecoder(StreamReader reader) { this.reader = reader; genericRegionStats = new ArithmeticDecoderStats(1 << 1); refinementRegionStats = new ArithmeticDecoderStats(1 << 1); iadhStats = new ArithmeticDecoderStats(1 << 9); iadwStats = new ArithmeticDecoderStats(1 << 9); iaexStats = new ArithmeticDecoderStats(1 << 9); iaaiStats = new ArithmeticDecoderStats(1 << 9); iadtStats = new ArithmeticDecoderStats(1 << 9); iaitStats = new ArithmeticDecoderStats(1 << 9); iafsStats = new ArithmeticDecoderStats(1 << 9); iadsStats = new ArithmeticDecoderStats(1 << 9); iardxStats = new ArithmeticDecoderStats(1 << 9); iardyStats = new ArithmeticDecoderStats(1 << 9); iardwStats = new ArithmeticDecoderStats(1 << 9); iardhStats = new ArithmeticDecoderStats(1 << 9); iariStats = new ArithmeticDecoderStats(1 << 9); iaidStats = new ArithmeticDecoderStats(1 << 1); } public void resetIntStats(int symbolCodeLength) { iadhStats.reset(); iadwStats.reset(); iaexStats.reset(); iaaiStats.reset(); iadtStats.reset(); iaitStats.reset(); iafsStats.reset(); iadsStats.reset(); iardxStats.reset(); iardyStats.reset(); iardwStats.reset(); iardhStats.reset(); iariStats.reset(); if (iaidStats.getContextSize() == 1 << (symbolCodeLength + 1)) { iaidStats.reset(); } else { iaidStats = new ArithmeticDecoderStats(1 << (symbolCodeLength + 1)); } } public void resetGenericStats(int template, ArithmeticDecoderStats previousStats) { int size = contextSize[template]; if (previousStats != null && previousStats.getContextSize() == size) { if (genericRegionStats.getContextSize() == size) { genericRegionStats.overwrite(previousStats); } else { genericRegionStats = previousStats.copy(); } } else { if (genericRegionStats.getContextSize() == size) { genericRegionStats.reset(); } else { genericRegionStats = new ArithmeticDecoderStats(1 << size); } } } public void resetRefinementStats(int template, ArithmeticDecoderStats previousStats) { int size = referredToContextSize[template]; if (previousStats != null && previousStats.getContextSize() == size) { if (refinementRegionStats.getContextSize() == size) { refinementRegionStats.overwrite(previousStats); } else { refinementRegionStats = previousStats.copy(); } } else { if (refinementRegionStats.getContextSize() == size) { refinementRegionStats.reset(); } else { refinementRegionStats = new ArithmeticDecoderStats(1 << size); } } } public void start() throws IOException { buffer0 = reader.readByte(); buffer1 = reader.readByte(); c = BinaryOperation.bit32Shift((buffer0 ^ 0xff), 16, BinaryOperation.LEFT_SHIFT); readByte(); c = BinaryOperation.bit32Shift(c, 7, BinaryOperation.LEFT_SHIFT); counter -= 7; a = 0x80000000l; } public DecodeIntResult decodeInt(ArithmeticDecoderStats stats) throws IOException { long value; previous = 1; int s = decodeIntBit(stats); if (decodeIntBit(stats) != 0) { if (decodeIntBit(stats) != 0) { if (decodeIntBit(stats) != 0) { if (decodeIntBit(stats) != 0) { if (decodeIntBit(stats) != 0) { value = 0; for (int i = 0; i < 32; i++) { value = BinaryOperation.bit32Shift(value, 1, BinaryOperation.LEFT_SHIFT) | decodeIntBit(stats); } value += 4436; } else { value = 0; for (int i = 0; i < 12; i++) { value = BinaryOperation.bit32Shift(value, 1, BinaryOperation.LEFT_SHIFT) | decodeIntBit(stats); } value += 340; } } else { value = 0; for (int i = 0; i < 8; i++) { value = BinaryOperation.bit32Shift(value, 1, BinaryOperation.LEFT_SHIFT) | decodeIntBit(stats); } value += 84; } } else { value = 0; for (int i = 0; i < 6; i++) { value = BinaryOperation.bit32Shift(value, 1, BinaryOperation.LEFT_SHIFT) | decodeIntBit(stats); } value += 20; } } else { value = decodeIntBit(stats); value = BinaryOperation.bit32Shift(value, 1, BinaryOperation.LEFT_SHIFT) | decodeIntBit(stats); value = BinaryOperation.bit32Shift(value, 1, BinaryOperation.LEFT_SHIFT) | decodeIntBit(stats); value = BinaryOperation.bit32Shift(value, 1, BinaryOperation.LEFT_SHIFT) | decodeIntBit(stats); value += 4; } } else { value = decodeIntBit(stats); value = BinaryOperation.bit32Shift(value, 1, BinaryOperation.LEFT_SHIFT) | decodeIntBit(stats); } int decodedInt; if (s != 0) { if (value == 0) { return new DecodeIntResult((int) value, false); } decodedInt = (int) -value; } else { decodedInt = (int) value; } return new DecodeIntResult(decodedInt, true); } public long decodeIAID(long codeLen, ArithmeticDecoderStats stats) throws IOException { previous = 1; for (long i = 0; i < codeLen; i++) { int bit = decodeBit(previous, stats); previous = BinaryOperation.bit32Shift(previous, 1, BinaryOperation.LEFT_SHIFT) | bit; } return previous - (1 << codeLen); } public int decodeBit(long context, ArithmeticDecoderStats stats) throws IOException { int iCX = BinaryOperation.bit8Shift(stats.getContextCodingTableValue((int) context), 1, BinaryOperation.RIGHT_SHIFT); int mpsCX = stats.getContextCodingTableValue((int) context) & 1; int qe = qeTable[iCX]; a -= qe; int bit; if (c < a) { if ((a & 0x80000000) != 0) { bit = mpsCX; } else { if (a < qe) { bit = 1 - mpsCX; if (switchTable[iCX] != 0) { stats.setContextCodingTableValue((int) context, (nlpsTable[iCX] << 1) | (1 - mpsCX)); } else { stats.setContextCodingTableValue((int) context, (nlpsTable[iCX] << 1) | mpsCX); } } else { bit = mpsCX; stats.setContextCodingTableValue((int) context, (nmpsTable[iCX] << 1) | mpsCX); } do { if (counter == 0) { readByte(); } a = BinaryOperation.bit32Shift(a, 1, BinaryOperation.LEFT_SHIFT); c = BinaryOperation.bit32Shift(c, 1, BinaryOperation.LEFT_SHIFT); counter--; } while ((a & 0x80000000) == 0); } } else { c -= a; if (a < qe) { bit = mpsCX; stats.setContextCodingTableValue((int) context, (nmpsTable[iCX] << 1) | mpsCX); } else { bit = 1 - mpsCX; if (switchTable[iCX] != 0) { stats.setContextCodingTableValue((int) context, (nlpsTable[iCX] << 1) | (1 - mpsCX)); } else { stats.setContextCodingTableValue((int) context, (nlpsTable[iCX] << 1) | mpsCX); } } a = qe; do { if (counter == 0) { readByte(); } a = BinaryOperation.bit32Shift(a, 1, BinaryOperation.LEFT_SHIFT); c = BinaryOperation.bit32Shift(c, 1, BinaryOperation.LEFT_SHIFT); counter--; } while ((a & 0x80000000) == 0); } return bit; } private void readByte() throws IOException { if (buffer0 == 0xff) { if (buffer1 > 0x8f) { counter = 8; } else { buffer0 = buffer1; buffer1 = reader.readByte(); c = c + 0xfe00 - (BinaryOperation.bit32Shift(buffer0, 9, BinaryOperation.LEFT_SHIFT)); counter = 7; } } else { buffer0 = buffer1; buffer1 = reader.readByte(); c = c + 0xff00 - (BinaryOperation.bit32Shift(buffer0, 8, BinaryOperation.LEFT_SHIFT)); counter = 8; } } private int decodeIntBit(ArithmeticDecoderStats stats) throws IOException { int bit; bit = decodeBit(previous, stats); if (previous < 0x100) { previous = BinaryOperation.bit32Shift(previous, 1, BinaryOperation.LEFT_SHIFT) | bit; } else { previous = (((BinaryOperation.bit32Shift(previous, 1, BinaryOperation.LEFT_SHIFT)) | bit) & 0x1ff) | 0x100; } return bit; } int qeTable[] = { 0x56010000, 0x34010000, 0x18010000, 0x0AC10000, 0x05210000, 0x02210000, 0x56010000, 0x54010000, 0x48010000, 0x38010000, 0x30010000, 0x24010000, 0x1C010000, 0x16010000, 0x56010000, 0x54010000, 0x51010000, 0x48010000, 0x38010000, 0x34010000, 0x30010000, 0x28010000, 0x24010000, 0x22010000, 0x1C010000, 0x18010000, 0x16010000, 0x14010000, 0x12010000, 0x11010000, 0x0AC10000, 0x09C10000, 0x08A10000, 0x05210000, 0x04410000, 0x02A10000, 0x02210000, 0x01410000, 0x01110000, 0x00850000, 0x00490000, 0x00250000, 0x00150000, 0x00090000, 0x00050000, 0x00010000, 0x56010000 }; int nmpsTable[] = { 1, 2, 3, 4, 5, 38, 7, 8, 9, 10, 11, 12, 13, 29, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 45, 46 }; int nlpsTable[] = { 1, 6, 9, 12, 29, 33, 6, 14, 14, 14, 17, 18, 20, 21, 14, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 46 }; int switchTable[] = { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; } libjpedal-jbig2-20100117/org/jpedal/jbig2/decoders/ArithmeticDecoderStats.java0000644000175000017500000000657111213455610026633 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * ArithmeticDecoderStats.java * --------------- */ package org.jpedal.jbig2.decoders; public class ArithmeticDecoderStats { private int contextSize; private int[] codingContextTable; public ArithmeticDecoderStats(int contextSize) { this.contextSize = contextSize; this.codingContextTable = new int[contextSize]; reset(); } public void reset() { for (int i = 0; i < contextSize; i++) { codingContextTable[i] = 0; } } public void setEntry(int codingContext, int i, int moreProbableSymbol) { codingContextTable[codingContext] = (i << i) + moreProbableSymbol; } public int getContextCodingTableValue(int index){ return codingContextTable[index]; } public void setContextCodingTableValue(int index, int value){ codingContextTable[index] = value; } public int getContextSize() { return contextSize; } public void overwrite(ArithmeticDecoderStats stats) { System.arraycopy(stats.codingContextTable, 0, codingContextTable, 0, contextSize); } public ArithmeticDecoderStats copy() { ArithmeticDecoderStats stats = new ArithmeticDecoderStats(contextSize); System.arraycopy(codingContextTable, 0, stats.codingContextTable, 0, contextSize); return stats; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/decoders/DecodeIntResult.java0000644000175000017500000000523311213455610025264 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * DecodeIntResult.java * --------------- */ package org.jpedal.jbig2.decoders; public class DecodeIntResult { private int intResult; private boolean booleanResult; public DecodeIntResult(int intResult, boolean booleanResult) { this.intResult = intResult; this.booleanResult = booleanResult; } public int intResult() { return intResult; } public boolean booleanResult() { return booleanResult; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/decoders/HuffmanDecoder.java0000644000175000017500000002271311213455610025103 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * HuffmanDecoder.java * --------------- */ package org.jpedal.jbig2.decoders; import java.io.IOException; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.io.StreamReader; public class HuffmanDecoder { public static int jbig2HuffmanLOW = 0xfffffffd; public static int jbig2HuffmanOOB = 0xfffffffe; public static int jbig2HuffmanEOT = 0xffffffff; private StreamReader reader; private static HuffmanDecoder ref; private HuffmanDecoder() {} public HuffmanDecoder(StreamReader reader) { this.reader = reader; } public DecodeIntResult decodeInt(int[][] table) throws IOException { int length = 0, prefix = 0; for (int i = 0; table[i][2] != jbig2HuffmanEOT; i++) { for (; length < table[i][1]; length++) { int bit = reader.readBit(); prefix = (prefix << 1) | bit; } if (prefix == table[i][3]) { if (table[i][2] == jbig2HuffmanOOB) { return new DecodeIntResult(-1, false); } int decodedInt; if (table[i][2] == jbig2HuffmanLOW) { int readBits = reader.readBits(32); decodedInt = table[i][0] - readBits; } else if (table[i][2] > 0) { int readBits = reader.readBits(table[i][2]); decodedInt = table[i][0] + readBits; } else { decodedInt = table[i][0]; } return new DecodeIntResult(decodedInt, true); } } return new DecodeIntResult(-1, false); } public static int[][] buildTable(int[][] table, int length) { int i, j, k, prefix; int[] tab; for (i = 0; i < length; i++) { for (j = i; j < length && table[j][1] == 0; j++); if (j == length) { break; } for (k = j + 1; k < length; k++) { if (table[k][1] > 0 && table[k][1] < table[j][1]) { j = k; } } if (j != i) { tab = table[j]; for (k = j; k > i; k--) { table[k] = table[k - 1]; } table[i] = tab; } } table[i] = table[length]; i = 0; prefix = 0; table[i++][3] = prefix++; for (; table[i][2] != jbig2HuffmanEOT; i++) { prefix <<= table[i][1] - table[i - 1][1]; table[i][3] = prefix++; } return table; } public static int huffmanTableA[][] = { { 0, 1, 4, 0x000 }, { 16, 2, 8, 0x002 }, { 272, 3, 16, 0x006 }, { 65808, 3, 32, 0x007 }, { 0, 0, jbig2HuffmanEOT, 0 } }; public static int huffmanTableB[][] = { { 0, 1, 0, 0x000 }, { 1, 2, 0, 0x002 }, { 2, 3, 0, 0x006 }, { 3, 4, 3, 0x00e }, { 11, 5, 6, 0x01e }, { 75, 6, 32, 0x03e }, { 0, 6, jbig2HuffmanOOB, 0x03f }, { 0, 0, jbig2HuffmanEOT, 0 } }; public static int huffmanTableC[][] = { { 0, 1, 0, 0x000 }, { 1, 2, 0, 0x002 }, { 2, 3, 0, 0x006 }, { 3, 4, 3, 0x00e }, { 11, 5, 6, 0x01e }, { 0, 6, jbig2HuffmanOOB, 0x03e }, { 75, 7, 32, 0x0fe }, { -256, 8, 8, 0x0fe }, { -257, 8, jbig2HuffmanLOW, 0x0ff }, { 0, 0, jbig2HuffmanEOT, 0 } }; public static int huffmanTableD[][] = { { 1, 1, 0, 0x000 }, { 2, 2, 0, 0x002 }, { 3, 3, 0, 0x006 }, { 4, 4, 3, 0x00e }, { 12, 5, 6, 0x01e }, { 76, 5, 32, 0x01f }, { 0, 0, jbig2HuffmanEOT, 0 } }; public static int huffmanTableE[][] = { { 1, 1, 0, 0x000 }, { 2, 2, 0, 0x002 }, { 3, 3, 0, 0x006 }, { 4, 4, 3, 0x00e }, { 12, 5, 6, 0x01e }, { 76, 6, 32, 0x03e }, { -255, 7, 8, 0x07e }, { -256, 7, jbig2HuffmanLOW, 0x07f }, { 0, 0, jbig2HuffmanEOT, 0 } }; public static int huffmanTableF[][] = { { 0, 2, 7, 0x000 }, { 128, 3, 7, 0x002 }, { 256, 3, 8, 0x003 }, { -1024, 4, 9, 0x008 }, { -512, 4, 8, 0x009 }, { -256, 4, 7, 0x00a }, { -32, 4, 5, 0x00b }, { 512, 4, 9, 0x00c }, { 1024, 4, 10, 0x00d }, { -2048, 5, 10, 0x01c }, { -128, 5, 6, 0x01d }, { -64, 5, 5, 0x01e }, { -2049, 6, jbig2HuffmanLOW, 0x03e }, { 2048, 6, 32, 0x03f }, { 0, 0, jbig2HuffmanEOT, 0 } }; public static int huffmanTableG[][] = { { -512, 3, 8, 0x000 }, { 256, 3, 8, 0x001 }, { 512, 3, 9, 0x002 }, { 1024, 3, 10, 0x003 }, { -1024, 4, 9, 0x008 }, { -256, 4, 7, 0x009 }, { -32, 4, 5, 0x00a }, { 0, 4, 5, 0x00b }, { 128, 4, 7, 0x00c }, { -128, 5, 6, 0x01a }, { -64, 5, 5, 0x01b }, { 32, 5, 5, 0x01c }, { 64, 5, 6, 0x01d }, { -1025, 5, jbig2HuffmanLOW, 0x01e }, { 2048, 5, 32, 0x01f }, { 0, 0, jbig2HuffmanEOT, 0 } }; public static int huffmanTableH[][] = { { 0, 2, 1, 0x000 }, { 0, 2, jbig2HuffmanOOB, 0x001 }, { 4, 3, 4, 0x004 }, { -1, 4, 0, 0x00a }, { 22, 4, 4, 0x00b }, { 38, 4, 5, 0x00c }, { 2, 5, 0, 0x01a }, { 70, 5, 6, 0x01b }, { 134, 5, 7, 0x01c }, { 3, 6, 0, 0x03a }, { 20, 6, 1, 0x03b }, { 262, 6, 7, 0x03c }, { 646, 6, 10, 0x03d }, { -2, 7, 0, 0x07c }, { 390, 7, 8, 0x07d }, { -15, 8, 3, 0x0fc }, { -5, 8, 1, 0x0fd }, { -7, 9, 1, 0x1fc }, { -3, 9, 0, 0x1fd }, { -16, 9, jbig2HuffmanLOW, 0x1fe }, { 1670, 9, 32, 0x1ff }, { 0, 0, jbig2HuffmanEOT, 0 } }; public static int huffmanTableI[][] = { { 0, 2, jbig2HuffmanOOB, 0x000 }, { -1, 3, 1, 0x002 }, { 1, 3, 1, 0x003 }, { 7, 3, 5, 0x004 }, { -3, 4, 1, 0x00a }, { 43, 4, 5, 0x00b }, { 75, 4, 6, 0x00c }, { 3, 5, 1, 0x01a }, { 139, 5, 7, 0x01b }, { 267, 5, 8, 0x01c }, { 5, 6, 1, 0x03a }, { 39, 6, 2, 0x03b }, { 523, 6, 8, 0x03c }, { 1291, 6, 11, 0x03d }, { -5, 7, 1, 0x07c }, { 779, 7, 9, 0x07d }, { -31, 8, 4, 0x0fc }, { -11, 8, 2, 0x0fd }, { -15, 9, 2, 0x1fc }, { -7, 9, 1, 0x1fd }, { -32, 9, jbig2HuffmanLOW, 0x1fe }, { 3339, 9, 32, 0x1ff }, { 0, 0, jbig2HuffmanEOT, 0 } }; public static int huffmanTableJ[][] = { { -2, 2, 2, 0x000 }, { 6, 2, 6, 0x001 }, { 0, 2, jbig2HuffmanOOB, 0x002 }, { -3, 5, 0, 0x018 }, { 2, 5, 0, 0x019 }, { 70, 5, 5, 0x01a }, { 3, 6, 0, 0x036 }, { 102, 6, 5, 0x037 }, { 134, 6, 6, 0x038 }, { 198, 6, 7, 0x039 }, { 326, 6, 8, 0x03a }, { 582, 6, 9, 0x03b }, { 1094, 6, 10, 0x03c }, { -21, 7, 4, 0x07a }, { -4, 7, 0, 0x07b }, { 4, 7, 0, 0x07c }, { 2118, 7, 11, 0x07d }, { -5, 8, 0, 0x0fc }, { 5, 8, 0, 0x0fd }, { -22, 8, jbig2HuffmanLOW, 0x0fe }, { 4166, 8, 32, 0x0ff }, { 0, 0, jbig2HuffmanEOT, 0 } }; public static int huffmanTableK[][] = { { 1, 1, 0, 0x000 }, { 2, 2, 1, 0x002 }, { 4, 4, 0, 0x00c }, { 5, 4, 1, 0x00d }, { 7, 5, 1, 0x01c }, { 9, 5, 2, 0x01d }, { 13, 6, 2, 0x03c }, { 17, 7, 2, 0x07a }, { 21, 7, 3, 0x07b }, { 29, 7, 4, 0x07c }, { 45, 7, 5, 0x07d }, { 77, 7, 6, 0x07e }, { 141, 7, 32, 0x07f }, { 0, 0, jbig2HuffmanEOT, 0 } }; public static int huffmanTableL[][] = { { 1, 1, 0, 0x000 }, { 2, 2, 0, 0x002 }, { 3, 3, 1, 0x006 }, { 5, 5, 0, 0x01c }, { 6, 5, 1, 0x01d }, { 8, 6, 1, 0x03c }, { 10, 7, 0, 0x07a }, { 11, 7, 1, 0x07b }, { 13, 7, 2, 0x07c }, { 17, 7, 3, 0x07d }, { 25, 7, 4, 0x07e }, { 41, 8, 5, 0x0fe }, { 73, 8, 32, 0x0ff }, { 0, 0, jbig2HuffmanEOT, 0 } }; public static int huffmanTableM[][] = { { 1, 1, 0, 0x000 }, { 2, 3, 0, 0x004 }, { 7, 3, 3, 0x005 }, { 3, 4, 0, 0x00c }, { 5, 4, 1, 0x00d }, { 4, 5, 0, 0x01c }, { 15, 6, 1, 0x03a }, { 17, 6, 2, 0x03b }, { 21, 6, 3, 0x03c }, { 29, 6, 4, 0x03d }, { 45, 6, 5, 0x03e }, { 77, 7, 6, 0x07e }, { 141, 7, 32, 0x07f }, { 0, 0, jbig2HuffmanEOT, 0 } }; public static int huffmanTableN[][] = { { 0, 1, 0, 0x000 }, { -2, 3, 0, 0x004 }, { -1, 3, 0, 0x005 }, { 1, 3, 0, 0x006 }, { 2, 3, 0, 0x007 }, { 0, 0, jbig2HuffmanEOT, 0 } }; public static int huffmanTableO[][] = { { 0, 1, 0, 0x000 }, { -1, 3, 0, 0x004 }, { 1, 3, 0, 0x005 }, { -2, 4, 0, 0x00c }, { 2, 4, 0, 0x00d }, { -4, 5, 1, 0x01c }, { 3, 5, 1, 0x01d }, { -8, 6, 2, 0x03c }, { 5, 6, 2, 0x03d }, { -24, 7, 4, 0x07c }, { 9, 7, 4, 0x07d }, { -25, 7, jbig2HuffmanLOW, 0x07e }, { 25, 7, 32, 0x07f }, { 0, 0, jbig2HuffmanEOT, 0 } }; } libjpedal-jbig2-20100117/org/jpedal/jbig2/decoders/JBIG2StreamDecoder.java0000644000175000017500000005046411213455610025474 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * JBIG2StreamDecoder.java * --------------- */ package org.jpedal.jbig2.decoders; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.image.JBIG2Bitmap; import org.jpedal.jbig2.io.StreamReader; import org.jpedal.jbig2.segment.Segment; import org.jpedal.jbig2.segment.SegmentHeader; import org.jpedal.jbig2.segment.extensions.ExtensionSegment; import org.jpedal.jbig2.segment.pageinformation.PageInformationSegment; import org.jpedal.jbig2.segment.pattern.PatternDictionarySegment; import org.jpedal.jbig2.segment.region.generic.GenericRegionSegment; import org.jpedal.jbig2.segment.region.halftone.HalftoneRegionSegment; import org.jpedal.jbig2.segment.region.refinement.RefinementRegionSegment; import org.jpedal.jbig2.segment.region.text.TextRegionSegment; import org.jpedal.jbig2.segment.stripes.EndOfStripeSegment; import org.jpedal.jbig2.segment.symboldictionary.SymbolDictionarySegment; import org.jpedal.jbig2.util.BinaryOperation; public class JBIG2StreamDecoder { private StreamReader reader; private boolean noOfPagesKnown; private boolean randomAccessOrganisation; private int noOfPages = -1; private List segments = new ArrayList(); private List bitmaps = new ArrayList(); private byte[] globalData; private ArithmeticDecoder arithmeticDecoder; private HuffmanDecoder huffmanDecoder; private MMRDecoder mmrDecoder; public static boolean debug = false; public void movePointer(int i){ reader.movePointer(i); } public void setGlobalData(byte[] data) { globalData = data; } public void decodeJBIG2(byte[] data) throws IOException, JBIG2Exception { reader = new StreamReader(data); resetDecoder(); boolean validFile = checkHeader(); if (JBIG2StreamDecoder.debug) System.out.println("validFile = " + validFile); if (!validFile) { /** * Assume this is a stream from a PDF so there is no file header, * end of page segments, or end of file segments. Organisation must * be sequential, and the number of pages is assumed to be 1. */ noOfPagesKnown = true; randomAccessOrganisation = false; noOfPages = 1; /** check to see if there is any global data to be read */ if (globalData != null) { /** set the reader to read from the global data */ reader = new StreamReader(globalData); huffmanDecoder = new HuffmanDecoder(reader); mmrDecoder = new MMRDecoder(reader); arithmeticDecoder = new ArithmeticDecoder(reader); /** read in the global data segments */ readSegments(); /** set the reader back to the main data */ reader = new StreamReader(data); } else { /** * There's no global data, so move the file pointer back to the * start of the stream */ reader.movePointer(-8); } } else { /** * We have the file header, so assume it is a valid stand-alone * file. */ if (JBIG2StreamDecoder.debug) System.out.println("==== File Header ===="); setFileHeaderFlags(); if (JBIG2StreamDecoder.debug) { System.out.println("randomAccessOrganisation = " + randomAccessOrganisation); System.out.println("noOfPagesKnown = " + noOfPagesKnown); } if (noOfPagesKnown) { noOfPages = getNoOfPages(); if (JBIG2StreamDecoder.debug) System.out.println("noOfPages = " + noOfPages); } } huffmanDecoder = new HuffmanDecoder(reader); mmrDecoder = new MMRDecoder(reader); arithmeticDecoder = new ArithmeticDecoder(reader); /** read in the main segment data */ readSegments(); } public HuffmanDecoder getHuffmanDecoder() { return huffmanDecoder; } public MMRDecoder getMMRDecoder() { return mmrDecoder; } public ArithmeticDecoder getArithmeticDecoder() { return arithmeticDecoder; } private void resetDecoder() { noOfPagesKnown = false; randomAccessOrganisation = false; noOfPages = -1; segments.clear(); bitmaps.clear(); } private void readSegments() throws IOException, JBIG2Exception { if (JBIG2StreamDecoder.debug) System.out.println("==== Segments ===="); boolean finished = false; while (!reader.isFinished() && !finished) { SegmentHeader segmentHeader = new SegmentHeader(); if (JBIG2StreamDecoder.debug) System.out.println("==== Segment Header ===="); readSegmentHeader(segmentHeader); // read the Segment data Segment segment = null; int segmentType = segmentHeader.getSegmentType(); int[] referredToSegments = segmentHeader.getReferredToSegments(); int noOfReferredToSegments = segmentHeader.getReferredToSegmentCount(); switch (segmentType) { case Segment.SYMBOL_DICTIONARY: if (JBIG2StreamDecoder.debug) System.out.println("==== Segment Symbol Dictionary ===="); segment = new SymbolDictionarySegment(this); segment.setSegmentHeader(segmentHeader); break; case Segment.INTERMEDIATE_TEXT_REGION: if (JBIG2StreamDecoder.debug) System.out.println("==== Intermediate Text Region ===="); segment = new TextRegionSegment(this, false); segment.setSegmentHeader(segmentHeader); break; case Segment.IMMEDIATE_TEXT_REGION: if (JBIG2StreamDecoder.debug) System.out.println("==== Immediate Text Region ===="); segment = new TextRegionSegment(this, true); segment.setSegmentHeader(segmentHeader); break; case Segment.IMMEDIATE_LOSSLESS_TEXT_REGION: if (JBIG2StreamDecoder.debug) System.out.println("==== Immediate Lossless Text Region ===="); segment = new TextRegionSegment(this, true); segment.setSegmentHeader(segmentHeader); break; case Segment.PATTERN_DICTIONARY: if (JBIG2StreamDecoder.debug) System.out.println("==== Pattern Dictionary ===="); segment = new PatternDictionarySegment(this); segment.setSegmentHeader(segmentHeader); break; case Segment.INTERMEDIATE_HALFTONE_REGION: if (JBIG2StreamDecoder.debug) System.out.println("==== Intermediate Halftone Region ===="); segment = new HalftoneRegionSegment(this, false); segment.setSegmentHeader(segmentHeader); break; case Segment.IMMEDIATE_HALFTONE_REGION: if (JBIG2StreamDecoder.debug) System.out.println("==== Immediate Halftone Region ===="); segment = new HalftoneRegionSegment(this, true); segment.setSegmentHeader(segmentHeader); break; case Segment.IMMEDIATE_LOSSLESS_HALFTONE_REGION: if (JBIG2StreamDecoder.debug) System.out.println("==== Immediate Lossless Halftone Region ===="); segment = new HalftoneRegionSegment(this, true); segment.setSegmentHeader(segmentHeader); break; case Segment.INTERMEDIATE_GENERIC_REGION: if (JBIG2StreamDecoder.debug) System.out.println("==== Intermediate Generic Region ===="); segment = new GenericRegionSegment(this, false); segment.setSegmentHeader(segmentHeader); break; case Segment.IMMEDIATE_GENERIC_REGION: if (JBIG2StreamDecoder.debug) System.out.println("==== Immediate Generic Region ===="); segment = new GenericRegionSegment(this, true); segment.setSegmentHeader(segmentHeader); break; case Segment.IMMEDIATE_LOSSLESS_GENERIC_REGION: if (JBIG2StreamDecoder.debug) System.out.println("==== Immediate Lossless Generic Region ===="); segment = new GenericRegionSegment(this, true); segment.setSegmentHeader(segmentHeader); break; case Segment.INTERMEDIATE_GENERIC_REFINEMENT_REGION: if (JBIG2StreamDecoder.debug) System.out.println("==== Intermediate Generic Refinement Region ===="); segment = new RefinementRegionSegment(this, false, referredToSegments, noOfReferredToSegments); segment.setSegmentHeader(segmentHeader); break; case Segment.IMMEDIATE_GENERIC_REFINEMENT_REGION: if (JBIG2StreamDecoder.debug) System.out.println("==== Immediate Generic Refinement Region ===="); segment = new RefinementRegionSegment(this, true, referredToSegments, noOfReferredToSegments); segment.setSegmentHeader(segmentHeader); break; case Segment.IMMEDIATE_LOSSLESS_GENERIC_REFINEMENT_REGION: if (JBIG2StreamDecoder.debug) System.out.println("==== Immediate lossless Generic Refinement Region ===="); segment = new RefinementRegionSegment(this, true, referredToSegments, noOfReferredToSegments); segment.setSegmentHeader(segmentHeader); break; case Segment.PAGE_INFORMATION: if (JBIG2StreamDecoder.debug) System.out.println("==== Page Information Dictionary ===="); segment = new PageInformationSegment(this); segment.setSegmentHeader(segmentHeader); break; case Segment.END_OF_PAGE: continue; case Segment.END_OF_STRIPE: if (JBIG2StreamDecoder.debug) System.out.println("==== End of Stripes ===="); segment = new EndOfStripeSegment(this); segment.setSegmentHeader(segmentHeader); break; case Segment.END_OF_FILE: if (JBIG2StreamDecoder.debug) System.out.println("==== End of File ===="); finished = true; continue; case Segment.PROFILES: if (JBIG2StreamDecoder.debug) System.out.println("PROFILES UNIMPLEMENTED"); break; case Segment.TABLES: if (JBIG2StreamDecoder.debug) System.out.println("TABLES UNIMPLEMENTED"); break; case Segment.EXTENSION: if (JBIG2StreamDecoder.debug) System.out.println("==== Extensions ===="); segment = new ExtensionSegment(this); segment.setSegmentHeader(segmentHeader); break; default: System.out.println("Unknown Segment type in JBIG2 stream"); break; } if (!randomAccessOrganisation) { segment.readSegment(); } segments.add(segment); } if (randomAccessOrganisation) { for (Iterator it = segments.iterator(); it.hasNext();) { Segment segment = (Segment) it.next(); segment.readSegment(); } } } public PageInformationSegment findPageSegement(int page) { for (Iterator it = segments.iterator(); it.hasNext();) { Segment segment = (Segment) it.next(); SegmentHeader segmentHeader = segment.getSegmentHeader(); if (segmentHeader.getSegmentType() == segment.PAGE_INFORMATION && segmentHeader.getPageAssociation() == page) { return (PageInformationSegment) segment; } } return null; } public Segment findSegment(int segmentNumber) { for (Iterator it = segments.iterator(); it.hasNext();) { Segment segment = (Segment) it.next(); if (segment.getSegmentHeader().getSegmentNumber() == segmentNumber) { return segment; } } return null; } private void readSegmentHeader(SegmentHeader segmentHeader) throws IOException, JBIG2Exception { handleSegmentNumber(segmentHeader); handleSegmentHeaderFlags(segmentHeader); handleSegmentReferredToCountAndRententionFlags(segmentHeader); handleReferedToSegmentNumbers(segmentHeader); handlePageAssociation(segmentHeader); if (segmentHeader.getSegmentType() != Segment.END_OF_FILE) handleSegmentDataLength(segmentHeader); } private void handlePageAssociation(SegmentHeader segmentHeader) throws IOException { int pageAssociation; boolean isPageAssociationSizeSet = segmentHeader.isPageAssociationSizeSet(); if (isPageAssociationSizeSet) { // field is 4 bytes long short[] buf = new short[4]; reader.readByte(buf); pageAssociation = BinaryOperation.getInt32(buf); } else { // field is 1 byte long pageAssociation = reader.readByte(); } segmentHeader.setPageAssociation(pageAssociation); if (JBIG2StreamDecoder.debug) System.out.println("pageAssociation = " + pageAssociation); } private void handleSegmentNumber(SegmentHeader segmentHeader) throws IOException { short[] segmentBytes = new short[4]; reader.readByte(segmentBytes); int segmentNumber = BinaryOperation.getInt32(segmentBytes); if (JBIG2StreamDecoder.debug) System.out.println("SegmentNumber = " + segmentNumber); segmentHeader.setSegmentNumber(segmentNumber); } private void handleSegmentHeaderFlags(SegmentHeader segmentHeader) throws IOException { short segmentHeaderFlags = reader.readByte(); // System.out.println("SegmentHeaderFlags = " + SegmentHeaderFlags); segmentHeader.setSegmentHeaderFlags(segmentHeaderFlags); } private void handleSegmentReferredToCountAndRententionFlags(SegmentHeader segmentHeader) throws IOException, JBIG2Exception { short referedToSegmentCountAndRetentionFlags = reader.readByte(); int referredToSegmentCount = (referedToSegmentCountAndRetentionFlags & 224) >> 5; // 224 // = // 11100000 short[] retentionFlags = null; /** take off the first three bits of the first byte */ short firstByte = (short) (referedToSegmentCountAndRetentionFlags & 31); // 31 = // 00011111 if (referredToSegmentCount <= 4) { // short form retentionFlags = new short[1]; retentionFlags[0] = firstByte; } else if (referredToSegmentCount == 7) { // long form short[] longFormCountAndFlags = new short[4]; /** add the first byte of the four */ longFormCountAndFlags[0] = firstByte; for (int i = 1; i < 4; i++) // add the next 3 bytes to the array longFormCountAndFlags[i] = reader.readByte(); /** get the count of the referred to Segments */ referredToSegmentCount = BinaryOperation.getInt32(longFormCountAndFlags); /** calculate the number of bytes in this field */ int noOfBytesInField = (int) Math.ceil(4 + ((referredToSegmentCount + 1) / 8d)); // System.out.println("noOfBytesInField = " + noOfBytesInField); int noOfRententionFlagBytes = noOfBytesInField - 4; retentionFlags = new short[noOfRententionFlagBytes]; reader.readByte(retentionFlags); } else { // error throw new JBIG2Exception("Error, 3 bit Segment count field = " + referredToSegmentCount); } segmentHeader.setReferredToSegmentCount(referredToSegmentCount); if (JBIG2StreamDecoder.debug) System.out.println("referredToSegmentCount = " + referredToSegmentCount); segmentHeader.setRententionFlags(retentionFlags); if (JBIG2StreamDecoder.debug) System.out.print("retentionFlags = "); if (JBIG2StreamDecoder.debug) { for (int i = 0; i < retentionFlags.length; i++) System.out.print(retentionFlags[i] + " "); System.out.println(""); } } private void handleReferedToSegmentNumbers(SegmentHeader segmentHeader) throws IOException { int referredToSegmentCount = segmentHeader.getReferredToSegmentCount(); int[] referredToSegments = new int[referredToSegmentCount]; int segmentNumber = segmentHeader.getSegmentNumber(); if (segmentNumber <= 256) { for (int i = 0; i < referredToSegmentCount; i++) referredToSegments[i] = reader.readByte(); } else if (segmentNumber <= 65536) { short[] buf = new short[2]; for (int i = 0; i < referredToSegmentCount; i++) { reader.readByte(buf); referredToSegments[i] = BinaryOperation.getInt16(buf); } } else { short[] buf = new short[4]; for (int i = 0; i < referredToSegmentCount; i++) { reader.readByte(buf); referredToSegments[i] = BinaryOperation.getInt32(buf); } } segmentHeader.setReferredToSegments(referredToSegments); if (JBIG2StreamDecoder.debug) { System.out.print("referredToSegments = "); for (int i = 0; i < referredToSegments.length; i++) System.out.print(referredToSegments[i] + " "); System.out.println(""); } } private int getNoOfPages() throws IOException { short[] noOfPages = new short[4]; reader.readByte(noOfPages); return BinaryOperation.getInt32(noOfPages); } private void handleSegmentDataLength(SegmentHeader segmentHeader) throws IOException { short[] buf = new short[4]; reader.readByte(buf); int dateLength = BinaryOperation.getInt32(buf); segmentHeader.setDataLength(dateLength); if (JBIG2StreamDecoder.debug) System.out.println("dateLength = " + dateLength); } private void setFileHeaderFlags() throws IOException { short headerFlags = reader.readByte(); if ((headerFlags & 0xfc) != 0) { System.out.println("Warning, reserved bits (2-7) of file header flags are not zero " + headerFlags); } int fileOrganisation = headerFlags & 1; randomAccessOrganisation = fileOrganisation == 0; int pagesKnown = headerFlags & 2; noOfPagesKnown = pagesKnown == 0; } private boolean checkHeader() throws IOException { short[] controlHeader = new short[] { 151, 74, 66, 50, 13, 10, 26, 10 }; short[] actualHeader = new short[8]; reader.readByte(actualHeader); return Arrays.equals(controlHeader, actualHeader); } public int readBits(int num) throws IOException { return reader.readBits(num); } public int readBit() throws IOException { return reader.readBit(); } public void readByte(short[] buff) throws IOException { reader.readByte(buff); } public void consumeRemainingBits() throws IOException { reader.consumeRemainingBits(); } public short readByte() throws java.io.IOException { return reader.readByte(); } public void appendBitmap(JBIG2Bitmap bitmap) { bitmaps.add(bitmap); } public JBIG2Bitmap findBitmap(int bitmapNumber) { for (Iterator it = bitmaps.iterator(); it.hasNext();) { JBIG2Bitmap bitmap = (JBIG2Bitmap) it.next(); if (bitmap.getBitmapNumber() == bitmapNumber) { return bitmap; } } return null; } public JBIG2Bitmap getPageAsJBIG2Bitmap(int i) { JBIG2Bitmap pageBitmap = findPageSegement(1).getPageBitmap(); return pageBitmap; } public boolean isNumberOfPagesKnown() { return noOfPagesKnown; } public int getNumberOfPages() { return noOfPages; } public boolean isRandomAccessOrganisationUsed() { return randomAccessOrganisation; } public List getAllSegments() { return segments; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/decoders/MMRDecoder.java0000644000175000017500000005621111213455610024152 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * MMRDecoder.java * --------------- */ package org.jpedal.jbig2.decoders; import java.io.IOException; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.io.StreamReader; import org.jpedal.jbig2.util.BinaryOperation; public class MMRDecoder { private StreamReader reader; private long bufferLength = 0, buffer = 0, noOfBytesRead = 0; private MMRDecoder() {} public MMRDecoder(StreamReader reader) { this.reader = reader; } public void reset() { bufferLength = 0; noOfBytesRead = 0; buffer = 0; } public void skipTo(int length) throws IOException { while (noOfBytesRead < length) { reader.readByte(); noOfBytesRead++; } } public long get24Bits() throws IOException { while (bufferLength < 24) { buffer = ((BinaryOperation.bit32Shift(buffer, 8, BinaryOperation.LEFT_SHIFT)) | (reader.readByte() & 0xff)); bufferLength += 8; noOfBytesRead++; } return (BinaryOperation.bit32Shift(buffer, (int) (bufferLength - 24), BinaryOperation.RIGHT_SHIFT)) & 0xffffff; } public int get2DCode() throws IOException { int[] tuple; if (bufferLength == 0) { buffer = (reader.readByte() & 0xff); bufferLength = 8; noOfBytesRead++; int lookup = (int) ((BinaryOperation.bit32Shift(buffer, 1, BinaryOperation.RIGHT_SHIFT)) & 0x7f); tuple = twoDimensionalTable1[lookup]; } else if (bufferLength == 8) { int lookup = (int) ((BinaryOperation.bit32Shift(buffer, 1, BinaryOperation.RIGHT_SHIFT)) & 0x7f); tuple = twoDimensionalTable1[lookup]; } else { int lookup = (int) ((BinaryOperation.bit32Shift(buffer, (int) (7 - bufferLength), BinaryOperation.LEFT_SHIFT)) & 0x7f); tuple = twoDimensionalTable1[lookup]; if (tuple[0] < 0 || tuple[0] > (int) bufferLength) { int right = (reader.readByte() & 0xff); long left = (BinaryOperation.bit32Shift(buffer, 8, BinaryOperation.LEFT_SHIFT)); buffer = left | right; bufferLength += 8; noOfBytesRead++; int look = (int) (BinaryOperation.bit32Shift(buffer, (int) (bufferLength - 7), BinaryOperation.RIGHT_SHIFT) & 0x7f); tuple = twoDimensionalTable1[look]; } } if (tuple[0] < 0) { if(JBIG2StreamDecoder.debug) System.out.println("Bad two dim code in JBIG2 MMR stream"); return 0; } bufferLength -= tuple[0]; return tuple[1]; } public int getWhiteCode() throws IOException { int[] tuple; long code; if (bufferLength == 0) { buffer = (reader.readByte() & 0xff); bufferLength = 8; noOfBytesRead++; } while (true) { if (bufferLength >= 7 && ((BinaryOperation.bit32Shift(buffer, (int) (bufferLength - 7), BinaryOperation.RIGHT_SHIFT)) & 0x7f) == 0) { if (bufferLength <= 12) { code = BinaryOperation.bit32Shift(buffer, (int) (12 - bufferLength), BinaryOperation.LEFT_SHIFT); } else { code = BinaryOperation.bit32Shift(buffer, (int) (bufferLength - 12), BinaryOperation.RIGHT_SHIFT); } tuple = whiteTable1[(int) (code & 0x1f)]; } else { if (bufferLength <= 9) { code = BinaryOperation.bit32Shift(buffer, (int) (9 - bufferLength), BinaryOperation.LEFT_SHIFT); } else { code = BinaryOperation.bit32Shift(buffer, (int) (bufferLength - 9), BinaryOperation.RIGHT_SHIFT); } int lookup = (int) (code & 0x1ff); if (lookup >= 0) tuple = whiteTable2[lookup]; else tuple = whiteTable2[whiteTable2.length + lookup]; } if (tuple[0] > 0 && tuple[0] <= (int) bufferLength) { bufferLength -= tuple[0]; return tuple[1]; } if (bufferLength >= 12) { break; } buffer = ((BinaryOperation.bit32Shift(buffer, 8, BinaryOperation.LEFT_SHIFT)) | reader.readByte() & 0xff); bufferLength += 8; noOfBytesRead++; } if(JBIG2StreamDecoder.debug) System.out.println("Bad white code in JBIG2 MMR stream"); bufferLength--; return 1; } public int getBlackCode() throws IOException { int[] tuple; long code; if (bufferLength == 0) { buffer = (reader.readByte() & 0xff); bufferLength = 8; noOfBytesRead++; } while (true) { if (bufferLength >= 6 && ((BinaryOperation.bit32Shift(buffer, (int) (bufferLength - 6), BinaryOperation.RIGHT_SHIFT)) & 0x3f) == 0) { if (bufferLength <= 13) { code = BinaryOperation.bit32Shift(buffer, (int) (13 - bufferLength), BinaryOperation.LEFT_SHIFT); } else { code = BinaryOperation.bit32Shift(buffer, (int) (bufferLength - 13), BinaryOperation.RIGHT_SHIFT); } tuple = blackTable1[(int) (code & 0x7f)]; } else if (bufferLength >= 4 && ((buffer >> (bufferLength - 4)) & 0x0f) == 0) { if (bufferLength <= 12) { code = BinaryOperation.bit32Shift(buffer, (int) (12 - bufferLength), BinaryOperation.LEFT_SHIFT); } else { code = BinaryOperation.bit32Shift(buffer, (int) (bufferLength - 12), BinaryOperation.RIGHT_SHIFT); } int lookup = (int) ((code & 0xff) - 64); if (lookup >= 0) tuple = blackTable2[lookup]; else tuple = blackTable1[blackTable1.length + lookup]; } else { if (bufferLength <= 6) { code = BinaryOperation.bit32Shift(buffer, (int) (6 - bufferLength), BinaryOperation.LEFT_SHIFT); } else { code = BinaryOperation.bit32Shift(buffer, (int) (bufferLength - 6), BinaryOperation.RIGHT_SHIFT); } int lookup = (int) (code & 0x3f); if (lookup >= 0) tuple = blackTable3[lookup]; else tuple = blackTable2[blackTable2.length + lookup]; } if (tuple[0] > 0 && tuple[0] <= (int) bufferLength) { bufferLength -= tuple[0]; return tuple[1]; } if (bufferLength >= 13) { break; } buffer = ((BinaryOperation.bit32Shift(buffer, 8, BinaryOperation.LEFT_SHIFT)) | (reader.readByte() & 0xff)); bufferLength += 8; noOfBytesRead++; } if(JBIG2StreamDecoder.debug) System.out.println("Bad black code in JBIG2 MMR stream"); bufferLength--; return 1; } public static int ccittEndOfLine = -2; public static final int twoDimensionalPass = 0; public static final int twoDimensionalHorizontal = 1; public static final int twoDimensionalVertical0 = 2; public static final int twoDimensionalVerticalR1 = 3; public static final int twoDimensionalVerticalL1 = 4; public static final int twoDimensionalVerticalR2 = 5; public static final int twoDimensionalVerticalL2 = 6; public static final int twoDimensionalVerticalR3 = 7; public static final int twoDimensionalVerticalL3 = 8; private int twoDimensionalTable1[][] = { { -1, -1 }, { -1, -1 }, { 7, twoDimensionalVerticalL3 }, { 7, twoDimensionalVerticalR3 }, { 6, twoDimensionalVerticalL2 }, { 6, twoDimensionalVerticalL2 }, { 6, twoDimensionalVerticalR2 }, { 6, twoDimensionalVerticalR2 }, { 4, twoDimensionalPass }, { 4, twoDimensionalPass }, { 4, twoDimensionalPass }, { 4, twoDimensionalPass }, { 4, twoDimensionalPass }, { 4, twoDimensionalPass }, { 4, twoDimensionalPass }, { 4, twoDimensionalPass }, { 3, twoDimensionalHorizontal }, { 3, twoDimensionalHorizontal }, { 3, twoDimensionalHorizontal }, { 3, twoDimensionalHorizontal }, { 3, twoDimensionalHorizontal }, { 3, twoDimensionalHorizontal }, { 3, twoDimensionalHorizontal }, { 3, twoDimensionalHorizontal }, { 3, twoDimensionalHorizontal }, { 3, twoDimensionalHorizontal }, { 3, twoDimensionalHorizontal }, { 3, twoDimensionalHorizontal }, { 3, twoDimensionalHorizontal }, { 3, twoDimensionalHorizontal }, { 3, twoDimensionalHorizontal }, { 3, twoDimensionalHorizontal }, { 3, twoDimensionalVerticalL1 }, { 3, twoDimensionalVerticalL1 }, { 3, twoDimensionalVerticalL1 }, { 3, twoDimensionalVerticalL1 }, { 3, twoDimensionalVerticalL1 }, { 3, twoDimensionalVerticalL1 }, { 3, twoDimensionalVerticalL1 }, { 3, twoDimensionalVerticalL1 }, { 3, twoDimensionalVerticalL1 }, { 3, twoDimensionalVerticalL1 }, { 3, twoDimensionalVerticalL1 }, { 3, twoDimensionalVerticalL1 }, { 3, twoDimensionalVerticalL1 }, { 3, twoDimensionalVerticalL1 }, { 3, twoDimensionalVerticalL1 }, { 3, twoDimensionalVerticalL1 }, { 3, twoDimensionalVerticalR1 }, { 3, twoDimensionalVerticalR1 }, { 3, twoDimensionalVerticalR1 }, { 3, twoDimensionalVerticalR1 }, { 3, twoDimensionalVerticalR1 }, { 3, twoDimensionalVerticalR1 }, { 3, twoDimensionalVerticalR1 }, { 3, twoDimensionalVerticalR1 }, { 3, twoDimensionalVerticalR1 }, { 3, twoDimensionalVerticalR1 }, { 3, twoDimensionalVerticalR1 }, { 3, twoDimensionalVerticalR1 }, { 3, twoDimensionalVerticalR1 }, { 3, twoDimensionalVerticalR1 }, { 3, twoDimensionalVerticalR1 }, { 3, twoDimensionalVerticalR1 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 }, { 1, twoDimensionalVertical0 } }; /** white run lengths */ private int whiteTable1[][] = { { -1, -1 }, { 12, ccittEndOfLine }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { 11, 1792 }, { 11, 1792 }, { 12, 1984 }, { 12, 2048 }, { 12, 2112 }, { 12, 2176 }, { 12, 2240 }, { 12, 2304 }, { 11, 1856 }, { 11, 1856 }, { 11, 1920 }, { 11, 1920 }, { 12, 2368 }, { 12, 2432 }, { 12, 2496 }, { 12, 2560 } }; private int whiteTable2[][] = { { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { 8, 29 }, { 8, 29 }, { 8, 30 }, { 8, 30 }, { 8, 45 }, { 8, 45 }, { 8, 46 }, { 8, 46 }, { 7, 22 }, { 7, 22 }, { 7, 22 }, { 7, 22 }, { 7, 23 }, { 7, 23 }, { 7, 23 }, { 7, 23 }, { 8, 47 }, { 8, 47 }, { 8, 48 }, { 8, 48 }, { 6, 13 }, { 6, 13 }, { 6, 13 }, { 6, 13 }, { 6, 13 }, { 6, 13 }, { 6, 13 }, { 6, 13 }, { 7, 20 }, { 7, 20 }, { 7, 20 }, { 7, 20 }, { 8, 33 }, { 8, 33 }, { 8, 34 }, { 8, 34 }, { 8, 35 }, { 8, 35 }, { 8, 36 }, { 8, 36 }, { 8, 37 }, { 8, 37 }, { 8, 38 }, { 8, 38 }, { 7, 19 }, { 7, 19 }, { 7, 19 }, { 7, 19 }, { 8, 31 }, { 8, 31 }, { 8, 32 }, { 8, 32 }, { 6, 1 }, { 6, 1 }, { 6, 1 }, { 6, 1 }, { 6, 1 }, { 6, 1 }, { 6, 1 }, { 6, 1 }, { 6, 12 }, { 6, 12 }, { 6, 12 }, { 6, 12 }, { 6, 12 }, { 6, 12 }, { 6, 12 }, { 6, 12 }, { 8, 53 }, { 8, 53 }, { 8, 54 }, { 8, 54 }, { 7, 26 }, { 7, 26 }, { 7, 26 }, { 7, 26 }, { 8, 39 }, { 8, 39 }, { 8, 40 }, { 8, 40 }, { 8, 41 }, { 8, 41 }, { 8, 42 }, { 8, 42 }, { 8, 43 }, { 8, 43 }, { 8, 44 }, { 8, 44 }, { 7, 21 }, { 7, 21 }, { 7, 21 }, { 7, 21 }, { 7, 28 }, { 7, 28 }, { 7, 28 }, { 7, 28 }, { 8, 61 }, { 8, 61 }, { 8, 62 }, { 8, 62 }, { 8, 63 }, { 8, 63 }, { 8, 0 }, { 8, 0 }, { 8, 320 }, { 8, 320 }, { 8, 384 }, { 8, 384 }, { 5, 10 }, { 5, 10 }, { 5, 10 }, { 5, 10 }, { 5, 10 }, { 5, 10 }, { 5, 10 }, { 5, 10 }, { 5, 10 }, { 5, 10 }, { 5, 10 }, { 5, 10 }, { 5, 10 }, { 5, 10 }, { 5, 10 }, { 5, 10 }, { 5, 11 }, { 5, 11 }, { 5, 11 }, { 5, 11 }, { 5, 11 }, { 5, 11 }, { 5, 11 }, { 5, 11 }, { 5, 11 }, { 5, 11 }, { 5, 11 }, { 5, 11 }, { 5, 11 }, { 5, 11 }, { 5, 11 }, { 5, 11 }, { 7, 27 }, { 7, 27 }, { 7, 27 }, { 7, 27 }, { 8, 59 }, { 8, 59 }, { 8, 60 }, { 8, 60 }, { 9, 1472 }, { 9, 1536 }, { 9, 1600 }, { 9, 1728 }, { 7, 18 }, { 7, 18 }, { 7, 18 }, { 7, 18 }, { 7, 24 }, { 7, 24 }, { 7, 24 }, { 7, 24 }, { 8, 49 }, { 8, 49 }, { 8, 50 }, { 8, 50 }, { 8, 51 }, { 8, 51 }, { 8, 52 }, { 8, 52 }, { 7, 25 }, { 7, 25 }, { 7, 25 }, { 7, 25 }, { 8, 55 }, { 8, 55 }, { 8, 56 }, { 8, 56 }, { 8, 57 }, { 8, 57 }, { 8, 58 }, { 8, 58 }, { 6, 192 }, { 6, 192 }, { 6, 192 }, { 6, 192 }, { 6, 192 }, { 6, 192 }, { 6, 192 }, { 6, 192 }, { 6, 1664 }, { 6, 1664 }, { 6, 1664 }, { 6, 1664 }, { 6, 1664 }, { 6, 1664 }, { 6, 1664 }, { 6, 1664 }, { 8, 448 }, { 8, 448 }, { 8, 512 }, { 8, 512 }, { 9, 704 }, { 9, 768 }, { 8, 640 }, { 8, 640 }, { 8, 576 }, { 8, 576 }, { 9, 832 }, { 9, 896 }, { 9, 960 }, { 9, 1024 }, { 9, 1088 }, { 9, 1152 }, { 9, 1216 }, { 9, 1280 }, { 9, 1344 }, { 9, 1408 }, { 7, 256 }, { 7, 256 }, { 7, 256 }, { 7, 256 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 4, 3 }, { 5, 128 }, { 5, 128 }, { 5, 128 }, { 5, 128 }, { 5, 128 }, { 5, 128 }, { 5, 128 }, { 5, 128 }, { 5, 128 }, { 5, 128 }, { 5, 128 }, { 5, 128 }, { 5, 128 }, { 5, 128 }, { 5, 128 }, { 5, 128 }, { 5, 8 }, { 5, 8 }, { 5, 8 }, { 5, 8 }, { 5, 8 }, { 5, 8 }, { 5, 8 }, { 5, 8 }, { 5, 8 }, { 5, 8 }, { 5, 8 }, { 5, 8 }, { 5, 8 }, { 5, 8 }, { 5, 8 }, { 5, 8 }, { 5, 9 }, { 5, 9 }, { 5, 9 }, { 5, 9 }, { 5, 9 }, { 5, 9 }, { 5, 9 }, { 5, 9 }, { 5, 9 }, { 5, 9 }, { 5, 9 }, { 5, 9 }, { 5, 9 }, { 5, 9 }, { 5, 9 }, { 5, 9 }, { 6, 16 }, { 6, 16 }, { 6, 16 }, { 6, 16 }, { 6, 16 }, { 6, 16 }, { 6, 16 }, { 6, 16 }, { 6, 17 }, { 6, 17 }, { 6, 17 }, { 6, 17 }, { 6, 17 }, { 6, 17 }, { 6, 17 }, { 6, 17 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 4 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 6, 14 }, { 6, 14 }, { 6, 14 }, { 6, 14 }, { 6, 14 }, { 6, 14 }, { 6, 14 }, { 6, 14 }, { 6, 15 }, { 6, 15 }, { 6, 15 }, { 6, 15 }, { 6, 15 }, { 6, 15 }, { 6, 15 }, { 6, 15 }, { 5, 64 }, { 5, 64 }, { 5, 64 }, { 5, 64 }, { 5, 64 }, { 5, 64 }, { 5, 64 }, { 5, 64 }, { 5, 64 }, { 5, 64 }, { 5, 64 }, { 5, 64 }, { 5, 64 }, { 5, 64 }, { 5, 64 }, { 5, 64 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 }, { 4, 7 } }; /** black run lengths */ int blackTable1[][] = { { -1, -1 }, { -1, -1 }, { 12, ccittEndOfLine }, { 12, ccittEndOfLine }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { 11, 1792 }, { 11, 1792 }, { 11, 1792 }, { 11, 1792 }, { 12, 1984 }, { 12, 1984 }, { 12, 2048 }, { 12, 2048 }, { 12, 2112 }, { 12, 2112 }, { 12, 2176 }, { 12, 2176 }, { 12, 2240 }, { 12, 2240 }, { 12, 2304 }, { 12, 2304 }, { 11, 1856 }, { 11, 1856 }, { 11, 1856 }, { 11, 1856 }, { 11, 1920 }, { 11, 1920 }, { 11, 1920 }, { 11, 1920 }, { 12, 2368 }, { 12, 2368 }, { 12, 2432 }, { 12, 2432 }, { 12, 2496 }, { 12, 2496 }, { 12, 2560 }, { 12, 2560 }, { 10, 18 }, { 10, 18 }, { 10, 18 }, { 10, 18 }, { 10, 18 }, { 10, 18 }, { 10, 18 }, { 10, 18 }, { 12, 52 }, { 12, 52 }, { 13, 640 }, { 13, 704 }, { 13, 768 }, { 13, 832 }, { 12, 55 }, { 12, 55 }, { 12, 56 }, { 12, 56 }, { 13, 1280 }, { 13, 1344 }, { 13, 1408 }, { 13, 1472 }, { 12, 59 }, { 12, 59 }, { 12, 60 }, { 12, 60 }, { 13, 1536 }, { 13, 1600 }, { 11, 24 }, { 11, 24 }, { 11, 24 }, { 11, 24 }, { 11, 25 }, { 11, 25 }, { 11, 25 }, { 11, 25 }, { 13, 1664 }, { 13, 1728 }, { 12, 320 }, { 12, 320 }, { 12, 384 }, { 12, 384 }, { 12, 448 }, { 12, 448 }, { 13, 512 }, { 13, 576 }, { 12, 53 }, { 12, 53 }, { 12, 54 }, { 12, 54 }, { 13, 896 }, { 13, 960 }, { 13, 1024 }, { 13, 1088 }, { 13, 1152 }, { 13, 1216 }, { 10, 64 }, { 10, 64 }, { 10, 64 }, { 10, 64 }, { 10, 64 }, { 10, 64 }, { 10, 64 }, { 10, 64 } }; int blackTable2[][] = { { 8, 13 }, { 8, 13 }, { 8, 13 }, { 8, 13 }, { 8, 13 }, { 8, 13 }, { 8, 13 }, { 8, 13 }, { 8, 13 }, { 8, 13 }, { 8, 13 }, { 8, 13 }, { 8, 13 }, { 8, 13 }, { 8, 13 }, { 8, 13 }, { 11, 23 }, { 11, 23 }, { 12, 50 }, { 12, 51 }, { 12, 44 }, { 12, 45 }, { 12, 46 }, { 12, 47 }, { 12, 57 }, { 12, 58 }, { 12, 61 }, { 12, 256 }, { 10, 16 }, { 10, 16 }, { 10, 16 }, { 10, 16 }, { 10, 17 }, { 10, 17 }, { 10, 17 }, { 10, 17 }, { 12, 48 }, { 12, 49 }, { 12, 62 }, { 12, 63 }, { 12, 30 }, { 12, 31 }, { 12, 32 }, { 12, 33 }, { 12, 40 }, { 12, 41 }, { 11, 22 }, { 11, 22 }, { 8, 14 }, { 8, 14 }, { 8, 14 }, { 8, 14 }, { 8, 14 }, { 8, 14 }, { 8, 14 }, { 8, 14 }, { 8, 14 }, { 8, 14 }, { 8, 14 }, { 8, 14 }, { 8, 14 }, { 8, 14 }, { 8, 14 }, { 8, 14 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 10 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 7, 11 }, { 9, 15 }, { 9, 15 }, { 9, 15 }, { 9, 15 }, { 9, 15 }, { 9, 15 }, { 9, 15 }, { 9, 15 }, { 12, 128 }, { 12, 192 }, { 12, 26 }, { 12, 27 }, { 12, 28 }, { 12, 29 }, { 11, 19 }, { 11, 19 }, { 11, 20 }, { 11, 20 }, { 12, 34 }, { 12, 35 }, { 12, 36 }, { 12, 37 }, { 12, 38 }, { 12, 39 }, { 11, 21 }, { 11, 21 }, { 12, 42 }, { 12, 43 }, { 10, 0 }, { 10, 0 }, { 10, 0 }, { 10, 0 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 }, { 7, 12 } }; int blackTable3[][] = { { -1, -1 }, { -1, -1 }, { -1, -1 }, { -1, -1 }, { 6, 9 }, { 6, 8 }, { 5, 7 }, { 5, 7 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 6 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, { 3, 1 }, { 3, 1 }, { 3, 1 }, { 3, 1 }, { 3, 1 }, { 3, 1 }, { 3, 1 }, { 3, 1 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 3, 4 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 3 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 }, { 2, 2 } }; } libjpedal-jbig2-20100117/org/jpedal/jbig2/examples/0000755000175000017500000000000011213455610021407 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/examples/jai/0000755000175000017500000000000011213455610022152 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/examples/jai/JBIG2ReaderPluginTester.java0000644000175000017500000004232411213455610027310 0ustar moellermoellerpackage org.jpedal.jbig2.examples.jai; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.File; import java.util.Iterator; import javax.imageio.ImageIO; import javax.imageio.ImageReadParam; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JSpinner; import javax.swing.SpinnerNumberModel; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; public class JBIG2ReaderPluginTester extends JFrame { final static String appTitle = "JBIG2 Reader Plug-in Tester"; final static int FORMAT_NAME = 0; final static int INPUT = 1; final static int MIME_TYPE = 2; final static int SUFFIX = 3; // Most recently read PCX image. BufferedImage biImage; // Offset in destination image where future decoded pixels will be placed. int dstOffX, dstOffY; // Image height and width. int height, width; // Source region definition. int srcX, srcY, srcWidth, srcHeight = 1; // Subsampling horizontal and vertical periods. int xSS = 1, ySS = 1; // Application status bar -- holds path and name of most recent PCX file. JLabel lblStatus; // Current method for getting an image reader. int method = FORMAT_NAME; // The picture panel displays the contents of the most recently read PCX // image. PicPanel pp; // The scroll pane allows the user to scroll around images that are bigger // than the picture panel. JScrollPane jsp; // Construct the PCXRPT GUI and indirectly start AWT helper threads. public JBIG2ReaderPluginTester(String title) { // Pass application title to superclass so that it appears on the title // bar. super(title); // Terminate the application when the user clicks the tiny x button on // the title bar. setDefaultCloseOperation(EXIT_ON_CLOSE); // Construct an open file chooser. Initialize the starting directory to // the current directory. final JFileChooser fcOpen = new JFileChooser(); fcOpen.setCurrentDirectory(new File(System.getProperty("user.dir"))); // Construct the application's menu bar. JMenuBar mb = new JMenuBar(); // The only menu to appear on the menu bar is File. The user invokes // menu items on this menu to open PCX images, configure the PCX reader // plug-in, and terminate the application. JMenu menuFile = new JMenu("File"); // Create and install the open menu item. JMenuItem miOpen = new JMenuItem("Open..."); ActionListener openl; openl = new ActionListener() { public void actionPerformed(ActionEvent e) { // Present the "open" file chooser without any file selected. If // the user cancels this file chooser, exit this method. fcOpen.setSelectedFile(null); if (fcOpen.showOpenDialog(JBIG2ReaderPluginTester.this) != JFileChooser.APPROVE_OPTION) return; // Attempt to read the image from the selected file. If something // goes wrong, doOpen() presents an appropriate error message and // false returns. Exit this method. if (!doOpen(fcOpen.getSelectedFile())) return; // Provide the user with assorted information. lblStatus.setText("Width: " + width + ", Height: " + height + ", File: " + fcOpen.getSelectedFile().getAbsolutePath()); // Display the new PCX image in the picture panel. The picture // panel automatically adjusts its dimensions, causing the // scrollpane to determine if scrollbars should be displayed. pp.setBufferedImage(biImage); // Reset the scroll positions so that the image's upper-left // corner is visible. jsp.getHorizontalScrollBar().setValue(0); jsp.getVerticalScrollBar().setValue(0); } }; miOpen.addActionListener(openl); menuFile.add(miOpen); // Create and install the configure menu item. JMenuItem miConfigure = new JMenuItem("Configure..."); ActionListener cfgl; cfgl = new ActionListener() { public void actionPerformed(ActionEvent e) { CfgDialog cfgdlg = new CfgDialog(JBIG2ReaderPluginTester.this, dstOffX, dstOffY, method, srcX, srcY, srcWidth, srcHeight, xSS, ySS); cfgdlg.setVisible(true); if (cfgdlg.isCanceled()) return; dstOffX = cfgdlg.getDstOffX(); dstOffY = cfgdlg.getDstOffY(); method = cfgdlg.getMethod(); srcX = cfgdlg.getSrcX(); srcY = cfgdlg.getSrcY(); srcWidth = cfgdlg.getSrcWidth(); srcHeight = cfgdlg.getSrcHeight(); xSS = cfgdlg.getXSS(); ySS = cfgdlg.getYSS(); } }; miConfigure.addActionListener(cfgl); menuFile.add(miConfigure); menuFile.addSeparator(); // Create and install the exit menu item. JMenuItem miExit = new JMenuItem("Exit"); ActionListener exitl; exitl = new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }; miExit.addActionListener(exitl); menuFile.add(miExit); // Add the file menu to the menu bar. mb.add(menuFile); // Install the menu bar. setJMenuBar(mb); // Create an initial picture panel that does not display a picture, but // has a default size. pp = new PicPanel(null); // Indirectly add the picture panel, by way of a scrollpane, to the // application's contentpane. getContentPane().add(jsp = new JScrollPane(pp)); // Create a status bar that displays the method used to obtain an image // reader, the height and width of the current image, and the path and // name of the current image's file. Initialize the status bar text to // one space so that it will be displayed at a height corresponding to // its current font's size. Surround the status bar with an etched // border to visually separate this component from the picture panel. lblStatus = new JLabel(" "); lblStatus.setBorder(BorderFactory.createEtchedBorder()); // Add the status bar to the bottom of the application's contentpane. getContentPane().add(lblStatus, BorderLayout.SOUTH); // Resize all components to their preferred sizes. pack(); // Display GUI and start GUI processing. setVisible(true); } // Open the specified JBIG2 file and read the file's JBIG2 image. boolean doOpen(File file) { if (!file.exists()) { JOptionPane.showMessageDialog(JBIG2ReaderPluginTester.this, "File does not exist!", appTitle, JOptionPane.ERROR_MESSAGE); return false; } try { // Validate file extension. String path = file.getAbsolutePath().toLowerCase(); if (!path.endsWith(".jbig2") && !path.endsWith(".jb2")) { JOptionPane.showMessageDialog(JBIG2ReaderPluginTester.this, "Incorrect file extension!", appTitle, JOptionPane.ERROR_MESSAGE); return false; } // Obtain an appropriate reader. ImageInputStream iis = ImageIO.createImageInputStream(file); Iterator iter; if (method == FORMAT_NAME) iter = ImageIO.getImageReadersByFormatName("jbig2"); else if (method == MIME_TYPE) iter = ImageIO.getImageReadersByMIMEType("image/x-jbig2"); else if (method == SUFFIX) iter = ImageIO.getImageReadersBySuffix("jbig2"); else iter = ImageIO.getImageReaders(iis); // Validate existence of reader. A reader will not be returned by // getImageReaders() if JBIG2ImageReaderSpi's canDecodeInput() method // returns false. if (!iter.hasNext()) { JOptionPane.showMessageDialog(JBIG2ReaderPluginTester.this, "Unable to obtain reader!", appTitle, JOptionPane.ERROR_MESSAGE); return false; } // Extract reader. ImageReader reader = (ImageReader) iter.next(); // Configure reader's input source. reader.setInput(iis, true); // Configure reader parameters. ImageReadParam irp = reader.getDefaultReadParam(); if (!(dstOffX == 0 && dstOffY == 0)) irp.setDestinationOffset(new Point(dstOffX, dstOffY)); if (srcWidth != 0) irp.setSourceRegion(new Rectangle(srcX, srcY, srcWidth, srcHeight)); if (!(xSS == 1 && ySS == 1)) irp.setSourceSubsampling(xSS, ySS, 0, 0); // Read the image. biImage = reader.read(0, irp); // Obtain the image's width and height. width = reader.getWidth(0); height = reader.getHeight(0); // Cleanup. reader.dispose(); return true; } catch (Exception e) { JOptionPane.showMessageDialog(JBIG2ReaderPluginTester.this, e.getMessage(), appTitle, JOptionPane.ERROR_MESSAGE); return false; } } // Application entry point. public static void main(String[] args) { // Create the application's GUI and start the application. new JBIG2ReaderPluginTester(appTitle); } } class CfgDialog extends JDialog { private final static int MAX_DSTOFFX = 9999; private final static int MAX_DSTOFFY = 9999; private final static int MAX_XSS = 9999; private final static int MAX_YSS = 9999; private final static int MAX_SRCX = 9999; private final static int MAX_SRCY = 9999; private final static int MAX_SRCWIDTH = 9999; private final static int MAX_SRCHEIGHT = 9999; private boolean canceled; private int dstOffX, dstOffY; private int srcHeight, srcWidth, srcX, srcY; private int xSS, ySS; private int method; CfgDialog(JFrame f, int dstOffX, int dstOffY, int method, int srcX, int srcY, int srcWidth, int srcHeight, int xSS, int ySS) { // Assign title to dialog box's title bar and ensure dialog box is // modal. super(f, "Configure", true); // Create a main layout panel that divides the GUI into several // sections, where each section has the same width and height. JPanel pnlLayout = new JPanel(); pnlLayout.setLayout(new GridLayout(5, 1)); // Create and install the destination offset section. JPanel pnl = new JPanel(); Border bd = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED); pnl.setBorder(BorderFactory.createTitledBorder(bd, "Destination Offset")); pnl.add(new JLabel("X")); final JSpinner spnDstOffX = new JSpinner(new SpinnerNumberModel(dstOffX, 0, MAX_DSTOFFX, 1)); pnl.add(spnDstOffX); pnl.add(new JLabel("Y")); final JSpinner spnDstOffY = new JSpinner(new SpinnerNumberModel(dstOffY, 0, MAX_DSTOFFY, 1)); pnl.add(spnDstOffY); pnlLayout.add(pnl); // Create and install the method section. pnl = new JPanel(); bd = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED); pnl.setBorder(BorderFactory.createTitledBorder(bd, "Method")); final JRadioButton rbChoice1 = new JRadioButton("Format name"); if (method == JBIG2ReaderPluginTester.FORMAT_NAME) rbChoice1.setSelected(true); pnl.add(rbChoice1); final JRadioButton rbChoice2 = new JRadioButton("Input"); if (method == JBIG2ReaderPluginTester.INPUT) rbChoice2.setSelected(true); pnl.add(rbChoice2); final JRadioButton rbChoice3 = new JRadioButton("MIME type"); if (method == JBIG2ReaderPluginTester.MIME_TYPE) rbChoice3.setSelected(true); pnl.add(rbChoice3); final JRadioButton rbChoice4 = new JRadioButton("Suffix"); if (method == JBIG2ReaderPluginTester.SUFFIX) rbChoice4.setSelected(true); pnl.add(rbChoice4); final ButtonGroup bg = new ButtonGroup(); bg.add(rbChoice1); bg.add(rbChoice2); bg.add(rbChoice3); bg.add(rbChoice4); pnlLayout.add(pnl); // Create and install the source region section. pnl = new JPanel(); bd = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED); pnl.setBorder(BorderFactory.createTitledBorder(bd, "Source Region")); pnl.add(new JLabel("Src X")); final JSpinner spnSrcX = new JSpinner(new SpinnerNumberModel(srcX, 0, MAX_SRCX, 1)); pnl.add(spnSrcX); pnl.add(new JLabel("Src Y")); final JSpinner spnSrcY = new JSpinner(new SpinnerNumberModel(srcY, 0, MAX_SRCY, 1)); pnl.add(spnSrcY); pnl.add(new JLabel("Src Width")); final JSpinner spnSrcWidth = new JSpinner(new SpinnerNumberModel(srcWidth, 0, MAX_SRCWIDTH, 1)); pnl.add(spnSrcWidth); pnl.add(new JLabel("Src Height")); final JSpinner spnSrcHeight = new JSpinner(new SpinnerNumberModel(srcHeight, 1, MAX_SRCHEIGHT, 1)); pnl.add(spnSrcHeight); pnlLayout.add(pnl); // Create and install the source subsampling section. pnl = new JPanel(); bd = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED); pnl.setBorder(BorderFactory.createTitledBorder(bd, "Source Subsampling")); pnl.add(new JLabel("X Subsampling")); final JSpinner spnXSS = new JSpinner(new SpinnerNumberModel(xSS, 1, MAX_XSS, 1)); pnl.add(spnXSS); pnl.add(new JLabel("Y Subsampling")); final JSpinner spnYSS = new JSpinner(new SpinnerNumberModel(ySS, 1, MAX_YSS, 1)); pnl.add(spnYSS); pnlLayout.add(pnl); // Create and install the button section. pnl = new JPanel(); pnl.setLayout(new FlowLayout(FlowLayout.RIGHT)); JButton btn = new JButton("OK"); pnl.add(btn); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { canceled = false; if (rbChoice1.isSelected()) CfgDialog.this.method = JBIG2ReaderPluginTester.FORMAT_NAME; else if (rbChoice2.isSelected()) CfgDialog.this.method = JBIG2ReaderPluginTester.INPUT; else if (rbChoice3.isSelected()) CfgDialog.this.method = JBIG2ReaderPluginTester.MIME_TYPE; else CfgDialog.this.method = JBIG2ReaderPluginTester.SUFFIX; CfgDialog.this.dstOffX = ((Integer) spnDstOffX.getValue()).intValue(); CfgDialog.this.dstOffY = ((Integer) spnDstOffY.getValue()).intValue(); CfgDialog.this.xSS = ((Integer) spnXSS.getValue()).intValue(); CfgDialog.this.ySS = ((Integer) spnYSS.getValue()).intValue(); CfgDialog.this.srcX = ((Integer) spnSrcX.getValue()).intValue(); CfgDialog.this.srcY = ((Integer) spnSrcY.getValue()).intValue(); CfgDialog.this.srcWidth = ((Integer) spnSrcWidth.getValue()).intValue(); CfgDialog.this.srcHeight = ((Integer) spnSrcHeight.getValue()).intValue(); dispose(); } }); btn = new JButton("Cancel"); pnl.add(btn); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { canceled = true; dispose(); } }); pnlLayout.add(pnl); // Add main layout panel to content pane. getContentPane().add(pnlLayout); // Resize dialog box to union of collective preferred sizes of all // contained components. pack(); } int getDstOffX() { return dstOffX; } int getDstOffY() { return dstOffY; } int getMethod() { return method; } int getSrcHeight() { return srcHeight; } int getSrcWidth() { return srcWidth; } int getSrcX() { return srcX; } int getSrcY() { return srcY; } int getXSS() { return xSS; } int getYSS() { return ySS; } boolean isCanceled() { return canceled; } } class PicPanel extends JPanel { // Dimensions of picture panel's preferred size. final static int WIDTH = 600; final static int HEIGHT = 440; // Reference to BufferedImage whose image is displayed in panel. If null // reference, nothing is displayed in panel area. private BufferedImage bi; // Create a picture panel component. PicPanel(BufferedImage bi) { // Install the buffered image for this panel. setBufferedImage(bi); } // Retrieve this component's preferred size for layout purposes. public Dimension getPreferredSize() { // When the program starts, there is no installed buffered image so a // default preferred size is chosen. After a buffered image has been // installed, that buffered image's size is returned as the preferred // size. if (bi == null) return new Dimension(WIDTH, HEIGHT); else return new Dimension(bi.getWidth(), bi.getHeight()); } // Redraw the picture panel. public void paintComponent(Graphics g) { // Paint the component's background to prevent artifacts from appearing. super.paintComponent(g); // If a buffered image has been installed, paint its contents on the // panel. if (bi != null) g.drawImage(bi, 0, 0, this); } // Install a new buffered image into the picture panel. public void setBufferedImage(BufferedImage bi) { // Save the buffered image for future painting. this.bi = bi; // The following method call invalidates this component and then adds // this component's validateRoot (the JScrollPane in which the picture // panel is contained) to a list of components that need to be validated. // Validation results in a call to this component's getPreferredSize() // method; this information will be used by the scrollpane to determine // if its scrollbars should be shown. revalidate(); // Paint the new image on the panel. repaint(); } } libjpedal-jbig2-20100117/org/jpedal/jbig2/examples/pdf/0000755000175000017500000000000011213455610022160 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/examples/pdf/PDFSegment.java0000644000175000017500000000623011213455610024760 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * PDFSegment.java * --------------- */ package org.jpedal.jbig2.examples.pdf; import java.io.ByteArrayOutputStream; import java.io.IOException; public class PDFSegment { private ByteArrayOutputStream header = new ByteArrayOutputStream(); private ByteArrayOutputStream data = new ByteArrayOutputStream(); private int segmentDataLength; public void writeToHeader(short bite) { header.write(bite); } public void writeToHeader(short[] bites) throws IOException { for (int i = 0; i < bites.length; i++) header.write(bites[i]); } public void writeToData(short bite) { data.write(bite); } public ByteArrayOutputStream getHeader() { return header; } public ByteArrayOutputStream getData() { return data; } public void setDataLength(int segmentDataLength) { this.segmentDataLength = segmentDataLength; } public int getSegmentDataLength() { return segmentDataLength; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/examples/viewer/0000755000175000017500000000000011213455610022710 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/examples/viewer/res/0000755000175000017500000000000011213455610023501 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/examples/viewer/res/back.gif0000644000175000017500000000014711213455610025072 0ustar moellermoellerGIF89a‘==nff™™™Ìÿÿÿ!ù,8œ©ËíˆrR‚½Íu·]Þrç—Rk—Ï ·‘WÇa–Ëw2ðl*]PHÜùvǦóù,;libjpedal-jbig2-20100117/org/jpedal/jbig2/examples/viewer/res/end.gif0000644000175000017500000000022311213455610024733 0ustar moellermoellerGIF89a¢==n™™Ìÿÿÿff™ÿÿÿ!ù,XHºÜþ0Ê9ÃF¼ƒ†@0€Þ”Nø=¡¸*ª)fMÜÍôb“xNì¬Ó`8ÔSB"@Cñ”æÏœ-¥ÓWMxe·žèVë cuÔTIÖ‰Y>Š|N¯Û' ;libjpedal-jbig2-20100117/org/jpedal/jbig2/examples/viewer/res/fback.gif0000644000175000017500000000023111213455610025232 0ustar moellermoellerGIF89a¢==n™™Ìÿÿÿff™ÿÿÿ!ù,^HºÜþ0ÊI€— Üî0pHF@0‚Jº¢z©Ì–±Ýæ¯Ç¿5UÏò[ÈÙ.†L›Ê]3)pM£­)P‹ÍBA¢/Jì>ÂJôíl¦µÏØO¥Û}¬÷Š~ÏŸ$;libjpedal-jbig2-20100117/org/jpedal/jbig2/examples/viewer/res/fforward.gif0000644000175000017500000000023211213455610025777 0ustar moellermoellerGIF89a¢==n™™Ìÿÿÿff™ÿÿÿ!ù,_HºÜþ0Ê)´”€ñv×€bØ¢Ó:¤Úùšá¬¸v'6ž;žÅ÷S…Äb¬& u«VÕ…°ËW× ^–³¨ñ--ÎxV­¢GÚ|fÏïû ;libjpedal-jbig2-20100117/org/jpedal/jbig2/examples/viewer/res/forward.gif0000644000175000017500000000016611213455610025637 0ustar moellermoellerGIF89a¢==n™™Ìÿÿÿff™ÿÿÿ!ù,;HºÜþ0ÊI¨ AØ\ãW¸ŒCi¢©I°mÇØLS3 p°¾×OJð~|« 2¹\5WD—tJ­F;libjpedal-jbig2-20100117/org/jpedal/jbig2/examples/viewer/res/open.png0000644000175000017500000000144311213455610025152 0ustar moellermoeller‰PNG  IHDRÄ´l;bKGDùC» pHYs × ×B(›xtIMEÕ -À’Cá°IDAT8ËÕ•ËK”aÆï7#ó53*¦yË!(ФV]47¹ŽW.£UYÐ.hQ@ТM+‰‚IÇM´ " 3u4,Lkpœqsî3ßåm13:æ^hÑY¼¼œsxžsž÷ûà ÑßßÚÄx.„ @lj°åêàààÓ]wu]ò\¿qj»†'î‘ÌÚj sÈíù°`«‰Ôãñ8c^oɆ#M¶É_Ág½}=†ÜïÎ sͦVR]]³mSǹó‡Þ¾þØÛ×Sª|ÈíØ¬X„#›Žjªª–-,Ì#„!P„‚(’ûDëÉ’¤¾™©kÀV`UU'K‹gíÔÕÕcÆŽu”Rn/ð= j‡]e‹K‹¤d9“ku¬emU-¹ÒÝœ¹Ü½‰¡Ù|oæ#‘U­½­ƒh4ÊÛ9¸uåâ¾<|ïáp«ˆd2YmáÇÁP˜î"™Öðùc{m¬R‰g¤n|ápHs5¹Hݺ¿¬È´nñ+CnOÀ0LÝï÷39¤³­¹GÐJ{‰TÆlJ¼ºiÐ5]“H’¦Š«¶œÕ„†” ‘ ¬SåîBˆ¢ÚºG°Û,øa ðYN»wy9t´¥ù8öª:ü‘iÝ,xŠ…@» Džoc·ƒÎ2tC21ýíç·'`ÍÍ!>Åã±Þ†c¬Ä²"™¢¯In2‰Däï¹\¾ YC266¡Õ&Gï|lóû'Ë+ÊÍö†ÅÒ6¦Í¯X8ź ¢( µ•64ÃäËôÄòK·çQ¸fjjÊQßP¯¼˜I–z^È‚¶!Ö'ÍÕt]BlÉ À´±p8<ûy|üŽÃ9}{¯6ÓWf¿ŽËê—ùae¤ø-)€ÔÌ‹àP›¾á¡k·žŽÞîîUÊß7°«•ú…J¥â™ØHpx Ü:Ýß¾~Î)ÿ%R+Ÿš@´Ö瀯€ `4Ü»1>>~WkÝ©µ¦T*ÇØX]æôÙ+,ÎNè¹ @&»I&»Y'«áÜŽ»°­µ$“II¥R²´´$€l¬. /ž=@g'‰WOê›H$$7>ôyà¨'A*•’t:í…B’ï^ ‘HÄ1ASSS5ÿªW›¥T*I:–µµµzÒÜÜœ¬¯¯K8vD"1MSr¹œX–%ù|^‰D-o8á9EJ)”Rd³Ùº¯”bdd€`0Èüü<½½½Žxí4á¹`Y†a4%†AµZ­û^qÓ4éêêb@¡IaÄb1úúúPJ±°°€R Û¶›ªUJ100О‚­­- …BSe5ßMàŽ›¦I Ø_AcÅýýý(¥ˆF£»ˆ8*7ŒÝQo[A¹\¦X,¶ìs£¯x.—Ãï÷·V ”¢££ƒh4ê©À¶ízÕÀƒƒƒE-loo£µþ£7ðù|-„gff‚ÝÝÝžóíEànÓôô4ÀG`Çë×}xÓÆBÙïD€û@pÌÝ"8 ø3{_¹:àbÚ²@ȶÀGöÀJ`ï‘TjûàŸÛO+\h…¡» éIEND®B`‚libjpedal-jbig2-20100117/org/jpedal/jbig2/examples/viewer/res/save.png0000644000175000017500000000171411213455610025150 0ustar moellermoeller‰PNG  IHDRàw=øbKGDÿÿÿ ½§“ pHYsaa¨?§itIMEÕLÆ•ÛYIDATHǽ•Ï‹[UÇ?÷¾—/™—qÚNšâzP·"Š©?™EŠ …êÂMAÄ]±nÜtSDDtéPa("•V‹ ¥µˆ¨ X™L’÷ã¾{Ïuñ’4™4X:âð^.çï=ç{Î÷ÀÿdOþ?ü=„€R#ÿѧçØéí°õɇœ9yx$I@0úo³”/åSil5âÔ[ï°yü)úý>'_x ¤á8…ܤüñûoôz»ô¾þ†» `Z„¤)&j‚-0Ã…ÇÉÞ/Ž~?åÚµ«\¹|y² ØpºNÞ{*•*ïºeTtÇ 7€Æ”ck”»ÚåQeû<Î:DdìUôÀ9‡x¡Z«Òl6’¥&Uµj­ŠÒι‰+#"Ð Ä•èFã_2bÐ#M]tÚ›}EÑmµb&Ÿ9Óãñ " ƒõûþ &Þyœ8šKK·\á§IžpâðÞxb«•p­È’d.Í[5ëd1É"BQtºwrîóÏ0…™ £uÌœçYNÔlhÅÞ>'^<¶°¬¶×P.V£Ù(0ЊÜ4¢ú¤”Zk¬µTkuŒ1h­qvAÊ+Ä Ãá”æ«/¶PZÇ1Ë­ÎIÉ“"‚÷žÂZrcH‡CŽ=¶ÉÑvwq‰¼*;@œðý…ó<ÿì“<±yœ0 ©G‰” }v{;˜,å‡KßñþÇ[¬¬æ×_~fcc»xЬµà!Ë2~ôqêQÓ¯œb¯·À«¯½Îööuò4!Kî½ÿAÞ~ï:͘„á|‰ô´Ð•ÓX~=jEçA”ä:¼J¼Ô¢V­Nä$ÐÁ\c„Ó·”WäÆ ÎQ–¿®ÿI”÷0&Cœ¥¹Óî¬S¯7ÊF *dfsÚ5øñÒE”÷[PCš$$I‚s2³ 0 && page <= decoder.getNumberOfPages()) { image = decoder.getPageAsBufferedImage(page - 1); currentPage = page; setScalingAndRotation(); navToolbar.setCurrentPage(currentPage); } } public int getCurrentPage() { return currentPage; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/examples/viewer/NavigationToolbar.java0000644000175000017500000001327411213455610027204 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * NavigationToolbar.java * --------------- */ package org.jpedal.jbig2.examples.viewer; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JToolBar; import org.jpedal.jbig2.JBIG2Decoder; public class NavigationToolbar extends JToolBar { protected static final int FIRSTPAGE = 0; protected static final int FBACKPAGE = 1; protected static final int BACKPAGE = 2; protected static final int FORWARDPAGE = 3; protected static final int FFORWARDPAGE = 4; protected static final int LASTPAGE = 5; protected static final int SETPAGE = 6; protected JTextField currentPageBox = new JTextField(4); private JLabel totalNoOfPages = new JLabel(); private JBIG2Viewer viewer; public NavigationToolbar(JBIG2Viewer viewer) { this.viewer = viewer; totalNoOfPages.setText("of 1"); currentPageBox.setText("1"); add(Box.createHorizontalGlue()); addButton("Rewind To Start", "/org/jpedal/jbig2/examples/viewer/res/start.gif", FIRSTPAGE); addButton("Back 10 Pages", "/org/jpedal/jbig2/examples/viewer/res/fback.gif", FBACKPAGE); addButton("Back", "/org/jpedal/jbig2/examples/viewer/res/back.gif", BACKPAGE); add(new JLabel("Page")); currentPageBox.setMaximumSize(new Dimension(5, 50)); currentPageBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { executeCommand(SETPAGE); } }); add(currentPageBox); add(totalNoOfPages); addButton("Forward", "/org/jpedal/jbig2/examples/viewer/res/forward.gif", FORWARDPAGE); addButton("Forward 10 Pages", "/org/jpedal/jbig2/examples/viewer/res/fforward.gif", FFORWARDPAGE); addButton("Fast Forward To End", "/org/jpedal/jbig2/examples/viewer/res/end.gif", LASTPAGE); add(Box.createHorizontalGlue()); // close(); } public void setTotalNoOfPages(int noOfPages) { totalNoOfPages.setText("of " + noOfPages); } public void setCurrentPage(int currentPage) { currentPageBox.setText(String.valueOf(currentPage)); } private void addButton(String tooltip, String url, final int type) { JButton button = new JButton(); button.setIcon(new ImageIcon(getClass().getResource(url))); button.setToolTipText(tooltip); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { executeCommand(type); } }); add(button); } public void executeCommand(int type) { JBIG2Decoder decoder = viewer.getDecoder(); switch (type) { case FIRSTPAGE: viewer.displayPage(1); break; case FBACKPAGE: viewer.displayPage(viewer.getCurrentPage() - 10); break; case BACKPAGE: viewer.displayPage(viewer.getCurrentPage() - 1); break; case FORWARDPAGE: viewer.displayPage(viewer.getCurrentPage() + 1); break; case FFORWARDPAGE: viewer.displayPage(viewer.getCurrentPage() + 10); break; case LASTPAGE: viewer.displayPage(decoder.getNumberOfPages()); break; case SETPAGE: int page = -1; try { page = Integer.parseInt(currentPageBox.getText()); } catch (NumberFormatException e) { } if (page >= 1 && page <= decoder.getNumberOfPages()) { viewer.displayPage(page); } else { currentPageBox.setText(String.valueOf(viewer.getCurrentPage())); } break; } } } libjpedal-jbig2-20100117/org/jpedal/jbig2/image/0000755000175000017500000000000011213455610020653 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/image/BitmapPointer.java0000644000175000017500000000701511213455610024276 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * BitmapPointer.java * --------------- */ package org.jpedal.jbig2.image; public class BitmapPointer { private int x, y, width, height, bits, count; private JBIG2Bitmap bitmap; public BitmapPointer(JBIG2Bitmap bitmap) { this.bitmap = bitmap; this.height = bitmap.getHeight(); this.width = bitmap.getWidth(); } public void setPointer(int x, int y) { this.x = x; this.y = y; count = 0; } public int nextPixel() { // fairly certain the byte can be cached here - seems to work fine. only // problem would be if cached pixel was modified, and the modified // version needed. // if (y < 0 || y >= height || x >= width) { // return 0; // } else if (x < 0) { // x++; // return 0; // } // // if (count == 0 && width - x >= 8) { // bits = bitmap.getPixelByte(x, y); // count = 8; // } else { // count = 0; // } // // if (count > 0) { // int b = bits & 0x01; // count--; // bits >>= 1; // x++; // return b; // } // // int pixel = bitmap.getPixel(x, y); // x++; // // return pixel; if (y < 0 || y >= height || x >= width) { return 0; } else if (x < 0) { x++; return 0; } int pixel = bitmap.getPixel(x, y); x++; return pixel; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/image/JBIG2Bitmap.java0000644000175000017500000012746611213455610023470 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * JBIG2Bitmap.java * --------------- */ package org.jpedal.jbig2.image; import java.awt.image.BufferedImage; import java.awt.image.DataBuffer; import java.awt.image.DataBufferByte; import java.awt.image.Raster; import java.awt.image.WritableRaster; import java.io.IOException; import java.util.Arrays; import java.util.BitSet; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.decoders.ArithmeticDecoder; import org.jpedal.jbig2.decoders.DecodeIntResult; import org.jpedal.jbig2.decoders.HuffmanDecoder; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.decoders.MMRDecoder; import org.jpedal.jbig2.util.BinaryOperation; public final class JBIG2Bitmap { private int width, height, line; private int bitmapNumber; //private FastBitSet data; private BitSet data; private static int counter = 0; private ArithmeticDecoder arithmeticDecoder; private HuffmanDecoder huffmanDecoder; private MMRDecoder mmrDecoder; public JBIG2Bitmap(int width, int height, ArithmeticDecoder arithmeticDecoder, HuffmanDecoder huffmanDecoder, MMRDecoder mmrDecoder) { this.width = width; this.height = height; this.arithmeticDecoder = arithmeticDecoder; this.huffmanDecoder = huffmanDecoder; this.mmrDecoder = mmrDecoder; this.line = (width + 7) >> 3; this.data = new BitSet(width * height); } public void readBitmap(boolean useMMR, int template, boolean typicalPredictionGenericDecodingOn, boolean useSkip, JBIG2Bitmap skipBitmap, short[] adaptiveTemplateX, short[] adaptiveTemplateY, int mmrDataLength) throws IOException, JBIG2Exception { if (useMMR) { //MMRDecoder mmrDecoder = MMRDecoder.getInstance(); mmrDecoder.reset(); int[] referenceLine = new int[width + 2]; int[] codingLine = new int[width + 2]; codingLine[0] = codingLine[1] = width; for (int row = 0; row < height; row++) { int i = 0; for (; codingLine[i] < width; i++) { referenceLine[i] = codingLine[i]; } referenceLine[i] = referenceLine[i + 1] = width; int referenceI = 0; int codingI = 0; int a0 = 0; do { int code1 = mmrDecoder.get2DCode(), code2, code3; switch (code1) { case MMRDecoder.twoDimensionalPass: if (referenceLine[referenceI] < width) { a0 = referenceLine[referenceI + 1]; referenceI += 2; } break; case MMRDecoder.twoDimensionalHorizontal: if ((codingI & 1) != 0) { code1 = 0; do { code1 += code3 = mmrDecoder.getBlackCode(); } while (code3 >= 64); code2 = 0; do { code2 += code3 = mmrDecoder.getWhiteCode(); } while (code3 >= 64); } else { code1 = 0; do { code1 += code3 = mmrDecoder.getWhiteCode(); } while (code3 >= 64); code2 = 0; do { code2 += code3 = mmrDecoder.getBlackCode(); } while (code3 >= 64); } if (code1 > 0 || code2 > 0) { a0 = codingLine[codingI++] = a0 + code1; a0 = codingLine[codingI++] = a0 + code2; while (referenceLine[referenceI] <= a0 && referenceLine[referenceI] < width) { referenceI += 2; } } break; case MMRDecoder.twoDimensionalVertical0: a0 = codingLine[codingI++] = referenceLine[referenceI]; if (referenceLine[referenceI] < width) { referenceI++; } break; case MMRDecoder.twoDimensionalVerticalR1: a0 = codingLine[codingI++] = referenceLine[referenceI] + 1; if (referenceLine[referenceI] < width) { referenceI++; while (referenceLine[referenceI] <= a0 && referenceLine[referenceI] < width) { referenceI += 2; } } break; case MMRDecoder.twoDimensionalVerticalR2: a0 = codingLine[codingI++] = referenceLine[referenceI] + 2; if (referenceLine[referenceI] < width) { referenceI++; while (referenceLine[referenceI] <= a0 && referenceLine[referenceI] < width) { referenceI += 2; } } break; case MMRDecoder.twoDimensionalVerticalR3: a0 = codingLine[codingI++] = referenceLine[referenceI] + 3; if (referenceLine[referenceI] < width) { referenceI++; while (referenceLine[referenceI] <= a0 && referenceLine[referenceI] < width) { referenceI += 2; } } break; case MMRDecoder.twoDimensionalVerticalL1: a0 = codingLine[codingI++] = referenceLine[referenceI] - 1; if (referenceI > 0) { referenceI--; } else { referenceI++; } while (referenceLine[referenceI] <= a0 && referenceLine[referenceI] < width) { referenceI += 2; } break; case MMRDecoder.twoDimensionalVerticalL2: a0 = codingLine[codingI++] = referenceLine[referenceI] - 2; if (referenceI > 0) { referenceI--; } else { referenceI++; } while (referenceLine[referenceI] <= a0 && referenceLine[referenceI] < width) { referenceI += 2; } break; case MMRDecoder.twoDimensionalVerticalL3: a0 = codingLine[codingI++] = referenceLine[referenceI] - 3; if (referenceI > 0) { referenceI--; } else { referenceI++; } while (referenceLine[referenceI] <= a0 && referenceLine[referenceI] < width) { referenceI += 2; } break; default: if (JBIG2StreamDecoder.debug) System.out.println("Illegal code in JBIG2 MMR bitmap data"); break; } } while (a0 < width); codingLine[codingI++] = width; for (int j = 0; codingLine[j] < width; j += 2) { for (int col = codingLine[j]; col < codingLine[j + 1]; col++) { setPixel(col, row, 1); } } } if (mmrDataLength >= 0) { mmrDecoder.skipTo(mmrDataLength); } else { if (mmrDecoder.get24Bits() != 0x001001) { if (JBIG2StreamDecoder.debug) System.out.println("Missing EOFB in JBIG2 MMR bitmap data"); } } } else { //ArithmeticDecoder arithmeticDecoder = ArithmeticDecoder.getInstance(); BitmapPointer cxPtr0 = new BitmapPointer(this), cxPtr1 = new BitmapPointer(this); BitmapPointer atPtr0 = new BitmapPointer(this), atPtr1 = new BitmapPointer(this), atPtr2 = new BitmapPointer(this), atPtr3 = new BitmapPointer(this); long ltpCX = 0; if (typicalPredictionGenericDecodingOn) { switch (template) { case 0: ltpCX = 0x3953; break; case 1: ltpCX = 0x079a; break; case 2: ltpCX = 0x0e3; break; case 3: ltpCX = 0x18a; break; } } boolean ltp = false; long cx, cx0, cx1, cx2; for (int row = 0; row < height; row++) { if (typicalPredictionGenericDecodingOn) { int bit = arithmeticDecoder.decodeBit(ltpCX, arithmeticDecoder.genericRegionStats); if (bit != 0) { ltp = !ltp; } if (ltp) { duplicateRow(row, row - 1); continue; } } int pixel; switch (template) { case 0: cxPtr0.setPointer(0, row - 2); cx0 = cxPtr0.nextPixel(); cx0 = (BinaryOperation.bit32Shift(cx0, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr0.nextPixel(); cxPtr1.setPointer(0, row - 1); cx1 = cxPtr1.nextPixel(); cx1 = (BinaryOperation.bit32Shift(cx1, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr1.nextPixel(); cx1 = (BinaryOperation.bit32Shift(cx1, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr1.nextPixel(); cx2 = 0; atPtr0.setPointer(adaptiveTemplateX[0], row + adaptiveTemplateY[0]); atPtr1.setPointer(adaptiveTemplateX[1], row + adaptiveTemplateY[1]); atPtr2.setPointer(adaptiveTemplateX[2], row + adaptiveTemplateY[2]); atPtr3.setPointer(adaptiveTemplateX[3], row + adaptiveTemplateY[3]); for (int col = 0; col < width; col++) { cx = (BinaryOperation.bit32Shift(cx0, 13, BinaryOperation.LEFT_SHIFT)) | (BinaryOperation.bit32Shift(cx1, 8, BinaryOperation.LEFT_SHIFT)) | (BinaryOperation.bit32Shift(cx2, 4, BinaryOperation.LEFT_SHIFT)) | (atPtr0.nextPixel() << 3) | (atPtr1.nextPixel() << 2) | (atPtr2.nextPixel() << 1) | atPtr3.nextPixel(); if (useSkip && skipBitmap.getPixel(col, row) != 0) { pixel = 0; } else { pixel = arithmeticDecoder.decodeBit(cx, arithmeticDecoder.genericRegionStats); if (pixel != 0) { setPixel(col, row, 1); } } cx0 = ((BinaryOperation.bit32Shift(cx0, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr0.nextPixel()) & 0x07; cx1 = ((BinaryOperation.bit32Shift(cx1, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr1.nextPixel()) & 0x1f; cx2 = ((BinaryOperation.bit32Shift(cx2, 1, BinaryOperation.LEFT_SHIFT)) | pixel) & 0x0f; } break; case 1: cxPtr0.setPointer(0, row - 2); cx0 = cxPtr0.nextPixel(); cx0 = (BinaryOperation.bit32Shift(cx0, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr0.nextPixel(); cx0 = (BinaryOperation.bit32Shift(cx0, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr0.nextPixel(); cxPtr1.setPointer(0, row - 1); cx1 = cxPtr1.nextPixel(); cx1 = (BinaryOperation.bit32Shift(cx1, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr1.nextPixel(); cx1 = (BinaryOperation.bit32Shift(cx1, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr1.nextPixel(); cx2 = 0; atPtr0.setPointer(adaptiveTemplateX[0], row + adaptiveTemplateY[0]); for (int col = 0; col < width; col++) { cx = (BinaryOperation.bit32Shift(cx0, 9, BinaryOperation.LEFT_SHIFT)) | (BinaryOperation.bit32Shift(cx1, 4, BinaryOperation.LEFT_SHIFT)) | (BinaryOperation.bit32Shift(cx2, 1, BinaryOperation.LEFT_SHIFT)) | atPtr0.nextPixel(); if (useSkip && skipBitmap.getPixel(col, row) != 0) { pixel = 0; } else { pixel = arithmeticDecoder.decodeBit(cx, arithmeticDecoder.genericRegionStats); if (pixel != 0) { setPixel(col, row, 1); } } cx0 = ((BinaryOperation.bit32Shift(cx0, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr0.nextPixel()) & 0x0f; cx1 = ((BinaryOperation.bit32Shift(cx1, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr1.nextPixel()) & 0x1f; cx2 = ((BinaryOperation.bit32Shift(cx2, 1, BinaryOperation.LEFT_SHIFT)) | pixel) & 0x07; } break; case 2: cxPtr0.setPointer(0, row - 2); cx0 = cxPtr0.nextPixel(); cx0 = (BinaryOperation.bit32Shift(cx0, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr0.nextPixel(); cxPtr1.setPointer(0, row - 1); cx1 = cxPtr1.nextPixel(); cx1 = (BinaryOperation.bit32Shift(cx1, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr1.nextPixel(); cx2 = 0; atPtr0.setPointer(adaptiveTemplateX[0], row + adaptiveTemplateY[0]); for (int col = 0; col < width; col++) { cx = (BinaryOperation.bit32Shift(cx0, 7, BinaryOperation.LEFT_SHIFT)) | (BinaryOperation.bit32Shift(cx1, 3, BinaryOperation.LEFT_SHIFT)) | (BinaryOperation.bit32Shift(cx2, 1, BinaryOperation.LEFT_SHIFT)) | atPtr0.nextPixel(); if (useSkip && skipBitmap.getPixel(col, row) != 0) { pixel = 0; } else { pixel = arithmeticDecoder.decodeBit(cx, arithmeticDecoder.genericRegionStats); if (pixel != 0) { setPixel(col, row, 1); } } cx0 = ((BinaryOperation.bit32Shift(cx0, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr0.nextPixel()) & 0x07; cx1 = ((BinaryOperation.bit32Shift(cx1, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr1.nextPixel()) & 0x0f; cx2 = ((BinaryOperation.bit32Shift(cx2, 1, BinaryOperation.LEFT_SHIFT)) | pixel) & 0x03; } break; case 3: cxPtr1.setPointer(0, row - 1); cx1 = cxPtr1.nextPixel(); cx1 = (BinaryOperation.bit32Shift(cx1, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr1.nextPixel(); cx2 = 0; atPtr0.setPointer(adaptiveTemplateX[0], row + adaptiveTemplateY[0]); for (int col = 0; col < width; col++) { cx = (BinaryOperation.bit32Shift(cx1, 5, BinaryOperation.LEFT_SHIFT)) | (BinaryOperation.bit32Shift(cx2, 1, BinaryOperation.LEFT_SHIFT)) | atPtr0.nextPixel(); if (useSkip && skipBitmap.getPixel(col, row) != 0) { pixel = 0; } else { pixel = arithmeticDecoder.decodeBit(cx, arithmeticDecoder.genericRegionStats); if (pixel != 0) { setPixel(col, row, 1); } } cx1 = ((BinaryOperation.bit32Shift(cx1, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr1.nextPixel()) & 0x1f; cx2 = ((BinaryOperation.bit32Shift(cx2, 1, BinaryOperation.LEFT_SHIFT)) | pixel) & 0x0f; } break; } } } } public void readGenericRefinementRegion(int template, boolean typicalPredictionGenericRefinementOn, JBIG2Bitmap referredToBitmap, int referenceDX, int referenceDY, short[] adaptiveTemplateX, short[] adaptiveTemplateY) throws IOException, JBIG2Exception { //ArithmeticDecoder arithmeticDecoder = ArithmeticDecoder.getInstance(); BitmapPointer cxPtr0, cxPtr1, cxPtr2, cxPtr3, cxPtr4, cxPtr5, cxPtr6, typicalPredictionGenericRefinementCXPtr0, typicalPredictionGenericRefinementCXPtr1, typicalPredictionGenericRefinementCXPtr2; long ltpCX; if (template != 0) { ltpCX = 0x008; cxPtr0 = new BitmapPointer(this); cxPtr1 = new BitmapPointer(this); cxPtr2 = new BitmapPointer(referredToBitmap); cxPtr3 = new BitmapPointer(referredToBitmap); cxPtr4 = new BitmapPointer(referredToBitmap); cxPtr5 = new BitmapPointer(this); cxPtr6 = new BitmapPointer(this); typicalPredictionGenericRefinementCXPtr0 = new BitmapPointer(referredToBitmap); typicalPredictionGenericRefinementCXPtr1 = new BitmapPointer(referredToBitmap); typicalPredictionGenericRefinementCXPtr2 = new BitmapPointer(referredToBitmap); } else { ltpCX = 0x0010; cxPtr0 = new BitmapPointer(this); cxPtr1 = new BitmapPointer(this); cxPtr2 = new BitmapPointer(referredToBitmap); cxPtr3 = new BitmapPointer(referredToBitmap); cxPtr4 = new BitmapPointer(referredToBitmap); cxPtr5 = new BitmapPointer(this); cxPtr6 = new BitmapPointer(referredToBitmap); typicalPredictionGenericRefinementCXPtr0 = new BitmapPointer(referredToBitmap); typicalPredictionGenericRefinementCXPtr1 = new BitmapPointer(referredToBitmap); typicalPredictionGenericRefinementCXPtr2 = new BitmapPointer(referredToBitmap); } long cx, cx0, cx2, cx3, cx4; long typicalPredictionGenericRefinementCX0, typicalPredictionGenericRefinementCX1, typicalPredictionGenericRefinementCX2; boolean ltp = false; for (int row = 0; row < height; row++) { if (template != 0) { cxPtr0.setPointer(0, row - 1); cx0 = cxPtr0.nextPixel(); cxPtr1.setPointer(-1, row); cxPtr2.setPointer(-referenceDX, row - 1 - referenceDY); cxPtr3.setPointer(-1 - referenceDX, row - referenceDY); cx3 = cxPtr3.nextPixel(); cx3 = (BinaryOperation.bit32Shift(cx3, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr3.nextPixel(); cxPtr4.setPointer(-referenceDX, row + 1 - referenceDY); cx4 = cxPtr4.nextPixel(); typicalPredictionGenericRefinementCX0 = typicalPredictionGenericRefinementCX1 = typicalPredictionGenericRefinementCX2 = 0; if (typicalPredictionGenericRefinementOn) { typicalPredictionGenericRefinementCXPtr0.setPointer(-1 - referenceDX, row - 1 - referenceDY); typicalPredictionGenericRefinementCX0 = typicalPredictionGenericRefinementCXPtr0.nextPixel(); typicalPredictionGenericRefinementCX0 = (BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX0, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr0.nextPixel(); typicalPredictionGenericRefinementCX0 = (BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX0, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr0.nextPixel(); typicalPredictionGenericRefinementCXPtr1.setPointer(-1 - referenceDX, row - referenceDY); typicalPredictionGenericRefinementCX1 = typicalPredictionGenericRefinementCXPtr1.nextPixel(); typicalPredictionGenericRefinementCX1 = (BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX1, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr1.nextPixel(); typicalPredictionGenericRefinementCX1 = (BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX1, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr1.nextPixel(); typicalPredictionGenericRefinementCXPtr2.setPointer(-1 - referenceDX, row + 1 - referenceDY); typicalPredictionGenericRefinementCX2 = typicalPredictionGenericRefinementCXPtr2.nextPixel(); typicalPredictionGenericRefinementCX2 = (BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX2, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr2.nextPixel(); typicalPredictionGenericRefinementCX2 = (BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX2, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr2.nextPixel(); } for (int col = 0; col < width; col++) { cx0 = ((BinaryOperation.bit32Shift(cx0, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr0.nextPixel()) & 7; cx3 = ((BinaryOperation.bit32Shift(cx3, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr3.nextPixel()) & 7; cx4 = ((BinaryOperation.bit32Shift(cx4, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr4.nextPixel()) & 3; if (typicalPredictionGenericRefinementOn) { typicalPredictionGenericRefinementCX0 = ((BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX0, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr0.nextPixel()) & 7; typicalPredictionGenericRefinementCX1 = ((BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX1, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr1.nextPixel()) & 7; typicalPredictionGenericRefinementCX2 = ((BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX2, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr2.nextPixel()) & 7; int decodeBit = arithmeticDecoder.decodeBit(ltpCX, arithmeticDecoder.refinementRegionStats); if (decodeBit != 0) { ltp = !ltp; } if (typicalPredictionGenericRefinementCX0 == 0 && typicalPredictionGenericRefinementCX1 == 0 && typicalPredictionGenericRefinementCX2 == 0) { setPixel(col, row, 0); continue; } else if (typicalPredictionGenericRefinementCX0 == 7 && typicalPredictionGenericRefinementCX1 == 7 && typicalPredictionGenericRefinementCX2 == 7) { setPixel(col, row, 1); continue; } } cx = (BinaryOperation.bit32Shift(cx0, 7, BinaryOperation.LEFT_SHIFT)) | (cxPtr1.nextPixel() << 6) | (cxPtr2.nextPixel() << 5) | (BinaryOperation.bit32Shift(cx3, 2, BinaryOperation.LEFT_SHIFT)) | cx4; int pixel = arithmeticDecoder.decodeBit(cx, arithmeticDecoder.refinementRegionStats); if (pixel == 1) { setPixel(col, row, 1); } } } else { cxPtr0.setPointer(0, row - 1); cx0 = cxPtr0.nextPixel(); cxPtr1.setPointer(-1, row); cxPtr2.setPointer(-referenceDX, row - 1 - referenceDY); cx2 = cxPtr2.nextPixel(); cxPtr3.setPointer(-1 - referenceDX, row - referenceDY); cx3 = cxPtr3.nextPixel(); cx3 = (BinaryOperation.bit32Shift(cx3, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr3.nextPixel(); cxPtr4.setPointer(-1 - referenceDX, row + 1 - referenceDY); cx4 = cxPtr4.nextPixel(); cx4 = (BinaryOperation.bit32Shift(cx4, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr4.nextPixel(); cxPtr5.setPointer(adaptiveTemplateX[0], row + adaptiveTemplateY[0]); cxPtr6.setPointer(adaptiveTemplateX[1] - referenceDX, row + adaptiveTemplateY[1] - referenceDY); typicalPredictionGenericRefinementCX0 = typicalPredictionGenericRefinementCX1 = typicalPredictionGenericRefinementCX2 = 0; if (typicalPredictionGenericRefinementOn) { typicalPredictionGenericRefinementCXPtr0.setPointer(-1 - referenceDX, row - 1 - referenceDY); typicalPredictionGenericRefinementCX0 = typicalPredictionGenericRefinementCXPtr0.nextPixel(); typicalPredictionGenericRefinementCX0 = (BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX0, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr0.nextPixel(); typicalPredictionGenericRefinementCX0 = (BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX0, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr0.nextPixel(); typicalPredictionGenericRefinementCXPtr1.setPointer(-1 - referenceDX, row - referenceDY); typicalPredictionGenericRefinementCX1 = typicalPredictionGenericRefinementCXPtr1.nextPixel(); typicalPredictionGenericRefinementCX1 = (BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX1, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr1.nextPixel(); typicalPredictionGenericRefinementCX1 = (BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX1, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr1.nextPixel(); typicalPredictionGenericRefinementCXPtr2.setPointer(-1 - referenceDX, row + 1 - referenceDY); typicalPredictionGenericRefinementCX2 = typicalPredictionGenericRefinementCXPtr2.nextPixel(); typicalPredictionGenericRefinementCX2 = (BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX2, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr2.nextPixel(); typicalPredictionGenericRefinementCX2 = (BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX2, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr2.nextPixel(); } for (int col = 0; col < width; col++) { cx0 = ((BinaryOperation.bit32Shift(cx0, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr0.nextPixel()) & 3; cx2 = ((BinaryOperation.bit32Shift(cx2, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr2.nextPixel()) & 3; cx3 = ((BinaryOperation.bit32Shift(cx3, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr3.nextPixel()) & 7; cx4 = ((BinaryOperation.bit32Shift(cx4, 1, BinaryOperation.LEFT_SHIFT)) | cxPtr4.nextPixel()) & 7; if (typicalPredictionGenericRefinementOn) { typicalPredictionGenericRefinementCX0 = ((BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX0, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr0.nextPixel()) & 7; typicalPredictionGenericRefinementCX1 = ((BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX1, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr1.nextPixel()) & 7; typicalPredictionGenericRefinementCX2 = ((BinaryOperation.bit32Shift(typicalPredictionGenericRefinementCX2, 1, BinaryOperation.LEFT_SHIFT)) | typicalPredictionGenericRefinementCXPtr2.nextPixel()) & 7; int decodeBit = arithmeticDecoder.decodeBit(ltpCX, arithmeticDecoder.refinementRegionStats); if (decodeBit == 1) { ltp = !ltp; } if (typicalPredictionGenericRefinementCX0 == 0 && typicalPredictionGenericRefinementCX1 == 0 && typicalPredictionGenericRefinementCX2 == 0) { setPixel(col, row, 0); continue; } else if (typicalPredictionGenericRefinementCX0 == 7 && typicalPredictionGenericRefinementCX1 == 7 && typicalPredictionGenericRefinementCX2 == 7) { setPixel(col, row, 1); continue; } } cx = (BinaryOperation.bit32Shift(cx0, 11, BinaryOperation.LEFT_SHIFT)) | (cxPtr1.nextPixel() << 10) | (BinaryOperation.bit32Shift(cx2, 8, BinaryOperation.LEFT_SHIFT)) | (BinaryOperation.bit32Shift(cx3, 5, BinaryOperation.LEFT_SHIFT)) | (BinaryOperation.bit32Shift(cx4, 2, BinaryOperation.LEFT_SHIFT)) | (cxPtr5.nextPixel() << 1) | cxPtr6.nextPixel(); int pixel = arithmeticDecoder.decodeBit(cx, arithmeticDecoder.refinementRegionStats); if (pixel == 1) { setPixel(col, row, 1); } } } } } public void readTextRegion(boolean huffman, boolean symbolRefine, int noOfSymbolInstances, int logStrips, int noOfSymbols, int[][] symbolCodeTable, int symbolCodeLength, JBIG2Bitmap[] symbols, int defaultPixel, int combinationOperator, boolean transposed, int referenceCorner, int sOffset, int[][] huffmanFSTable, int[][] huffmanDSTable, int[][] huffmanDTTable, int[][] huffmanRDWTable, int[][] huffmanRDHTable, int[][] huffmanRDXTable, int[][] huffmanRDYTable, int[][] huffmanRSizeTable, int template, short[] symbolRegionAdaptiveTemplateX, short[] symbolRegionAdaptiveTemplateY, JBIG2StreamDecoder decoder) throws JBIG2Exception, IOException { JBIG2Bitmap symbolBitmap; int strips = 1 << logStrips; clear(defaultPixel); //HuffmanDecoder huffDecoder = HuffmanDecoder.getInstance(); //ArithmeticDecoder arithmeticDecoder = ArithmeticDecoder.getInstance(); int t; if (huffman) { t = huffmanDecoder.decodeInt(huffmanDTTable).intResult(); } else { t = arithmeticDecoder.decodeInt(arithmeticDecoder.iadtStats).intResult(); } t *= -strips; int currentInstance = 0; int firstS = 0; int dt, tt, ds, s; while (currentInstance < noOfSymbolInstances) { if (huffman) { dt = huffmanDecoder.decodeInt(huffmanDTTable).intResult(); } else { dt = arithmeticDecoder.decodeInt(arithmeticDecoder.iadtStats).intResult(); } t += dt * strips; if (huffman) { ds = huffmanDecoder.decodeInt(huffmanFSTable).intResult(); } else { ds = arithmeticDecoder.decodeInt(arithmeticDecoder.iafsStats).intResult(); } firstS += ds; s = firstS; while (true) { if (strips == 1) { dt = 0; } else if (huffman) { dt = decoder.readBits(logStrips); } else { dt = arithmeticDecoder.decodeInt(arithmeticDecoder.iaitStats).intResult(); } tt = t + dt; long symbolID; if (huffman) { if (symbolCodeTable != null) { symbolID = huffmanDecoder.decodeInt(symbolCodeTable).intResult(); } else { symbolID = decoder.readBits(symbolCodeLength); } } else { symbolID = arithmeticDecoder.decodeIAID(symbolCodeLength, arithmeticDecoder.iaidStats); } if (symbolID >= noOfSymbols) { if (JBIG2StreamDecoder.debug) System.out.println("Invalid symbol number in JBIG2 text region"); } else { symbolBitmap = null; int ri; if (symbolRefine) { if (huffman) { ri = decoder.readBit(); } else { ri = arithmeticDecoder.decodeInt(arithmeticDecoder.iariStats).intResult(); } } else { ri = 0; } if (ri != 0) { int refinementDeltaWidth, refinementDeltaHeight, refinementDeltaX, refinementDeltaY; if (huffman) { refinementDeltaWidth = huffmanDecoder.decodeInt(huffmanRDWTable).intResult(); refinementDeltaHeight = huffmanDecoder.decodeInt(huffmanRDHTable).intResult(); refinementDeltaX = huffmanDecoder.decodeInt(huffmanRDXTable).intResult(); refinementDeltaY = huffmanDecoder.decodeInt(huffmanRDYTable).intResult(); decoder.consumeRemainingBits(); arithmeticDecoder.start(); } else { refinementDeltaWidth = arithmeticDecoder.decodeInt(arithmeticDecoder.iardwStats).intResult(); refinementDeltaHeight = arithmeticDecoder.decodeInt(arithmeticDecoder.iardhStats).intResult(); refinementDeltaX = arithmeticDecoder.decodeInt(arithmeticDecoder.iardxStats).intResult(); refinementDeltaY = arithmeticDecoder.decodeInt(arithmeticDecoder.iardyStats).intResult(); } refinementDeltaX = ((refinementDeltaWidth >= 0) ? refinementDeltaWidth : refinementDeltaWidth - 1) / 2 + refinementDeltaX; refinementDeltaY = ((refinementDeltaHeight >= 0) ? refinementDeltaHeight : refinementDeltaHeight - 1) / 2 + refinementDeltaY; symbolBitmap = new JBIG2Bitmap(refinementDeltaWidth + symbols[(int) symbolID].width, refinementDeltaHeight + symbols[(int) symbolID].height, arithmeticDecoder, huffmanDecoder, mmrDecoder); symbolBitmap.readGenericRefinementRegion(template, false, symbols[(int) symbolID], refinementDeltaX, refinementDeltaY, symbolRegionAdaptiveTemplateX, symbolRegionAdaptiveTemplateY); } else { symbolBitmap = symbols[(int) symbolID]; } int bitmapWidth = symbolBitmap.width - 1; int bitmapHeight = symbolBitmap.height - 1; if (transposed) { switch (referenceCorner) { case 0: // bottom left combine(symbolBitmap, tt, s, combinationOperator); break; case 1: // top left combine(symbolBitmap, tt, s, combinationOperator); break; case 2: // bottom right combine(symbolBitmap, (int) (tt - bitmapWidth), s, combinationOperator); break; case 3: // top right combine(symbolBitmap, (int) (tt - bitmapWidth), s, combinationOperator); break; } s += bitmapHeight; } else { switch (referenceCorner) { case 0: // bottom left combine(symbolBitmap, s, (int) (tt - bitmapHeight), combinationOperator); break; case 1: // top left combine(symbolBitmap, s, tt, combinationOperator); break; case 2: // bottom right combine(symbolBitmap, s, (int) (tt - bitmapHeight), combinationOperator); break; case 3: // top right combine(symbolBitmap, s, (int) tt, combinationOperator); break; } s += bitmapWidth; } } currentInstance++; DecodeIntResult decodeIntResult; if (huffman) { decodeIntResult = huffmanDecoder.decodeInt(huffmanDSTable); } else { decodeIntResult = arithmeticDecoder.decodeInt(arithmeticDecoder.iadsStats); } if (!decodeIntResult.booleanResult()) { break; } ds = decodeIntResult.intResult(); s += sOffset + ds; } } } public void clear(int defPixel) { data.set(0, data.size(), defPixel == 1); } public void combine(JBIG2Bitmap bitmap, int x, int y, long combOp) { int srcWidth = bitmap.width; int srcHeight = bitmap.height; int srcRow = 0, srcCol = 0; // int maxRow = y + srcHeight; // int maxCol = x + srcWidth; // // for (int row = y; row < maxRow; row++) { // for (int col = x; col < maxCol; srcCol += 8, col += 8) { // // byte srcPixelByte = bitmap.getPixelByte(srcCol, srcRow); // byte dstPixelByte = getPixelByte(col, row); // byte endPixelByte; // // switch ((int) combOp) { // case 0: // or // endPixelByte = (byte) (dstPixelByte | srcPixelByte); // break; // case 1: // and // endPixelByte = (byte) (dstPixelByte & srcPixelByte); // break; // case 2: // xor // endPixelByte = (byte) (dstPixelByte ^ srcPixelByte); // break; // case 3: // xnor // endPixelByte = (byte) ~(dstPixelByte ^ srcPixelByte); // break; // case 4: // replace // default: // endPixelByte = srcPixelByte; // break; // } // int used = maxCol - col; // if (used < 8) { // // mask bits // endPixelByte = (byte) ((endPixelByte & (0xFF >> (8 - used))) | (dstPixelByte & (0xFF << (used)))); // } // setPixelByte(col, row, endPixelByte); // } // // srcCol = 0; // srcRow++; for (int row = y; row < y + srcHeight; row++) { for (int col = x; col < x + srcWidth; col++) { int srcPixel = bitmap.getPixel(srcCol, srcRow); switch ((int) combOp) { case 0: // or setPixel(col, row, getPixel(col, row) | srcPixel); break; case 1: // and setPixel(col, row, getPixel(col, row) & srcPixel); break; case 2: // xor setPixel(col, row, getPixel(col, row) ^ srcPixel); break; case 3: // xnor if ((getPixel(col, row) == 1 && srcPixel == 1) || (getPixel(col, row) == 0 && srcPixel == 0)) setPixel(col, row, 1); else setPixel(col, row, 0); break; case 4: // replace setPixel(col, row, srcPixel); break; } srcCol++; } srcCol = 0; srcRow++; } } /** * set a full byte of pixels */ // private void setPixelByte(int col, int row, byte bits) { //data.setByte(row, col, bits); // } /** * get a byte of pixels */ // public byte getPixelByte(int col, int row) { //return data.getByte(row, col); // } private void duplicateRow(int yDest, int ySrc) { // for (int i = 0; i < width;) { // setPixelByte(i, yDest, getPixelByte(i, ySrc)); // i += 8; // } for (int i = 0; i < width; i++) { setPixel(i, yDest, getPixel(i, ySrc)); } } public int getWidth() { return width; } public int getHeight() { return height; } public byte[] getData(boolean switchPixelColor) { // byte[] bytes = new byte[height * line]; // // for (int i = 0; i < height; i++) { // System.arraycopy(data.bytes[i], 0, bytes, line * i, line); // } // // for (int i = 0; i < bytes.length; i++) { // // reverse bits // // int value = bytes[i]; // value = (value & 0x0f) << 4 | (value & 0xf0) >> 4; // value = (value & 0x33) << 2 | (value & 0xcc) >> 2; // value = (value & 0x55) << 1 | (value & 0xaa) >> 1; // // if (switchPixelColor) { // value ^= 0xff; // } // // bytes[i] = (byte) (value & 0xFF); // } // // return bytes; byte[] bytes = new byte[height * line]; int count = 0, offset = 0; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { if (data.get(count)) { int bite = (count + offset) / 8; int bit = (count + offset) % 8; bytes[bite] |= 1 << (7 - bit); } count++; } offset = (line * 8 * (row + 1)) - count; } if (switchPixelColor) { for (int i = 0; i < bytes.length; i++) { bytes[i] ^= 0xff; } } return bytes; } public JBIG2Bitmap getSlice(int x, int y, int width, int height) { // JBIG2Bitmap slice = new JBIG2Bitmap(width, height); // // int sliceRow = 0, sliceCol = 0; // int maxCol = x + width; // // //ShowGUIMessage.showGUIMessage("x", this.getBufferedImage(), "xx"); // // System.out.println(">>> getSlice x = "+x+" y = "+y+ " width = "+width+ " height = "+height); // System.out.println(">>> baseImage width = "+this.width+ " height = "+this.height); // // System.out.println("counter = "+counter); // if(counter == 17){ // System.out.println(); // //ShowGUIMessage.showGUIMessage("x", this.getBufferedImage(), "xx"); // } // // ShowGUIMessage.showGUIMessage("x", this.getBufferedImage(), "xx"); // // for (int row = y; row < height; row++) { // for (int col = x; col < maxCol; col += 8, sliceCol += 8) { // slice.setPixelByte(sliceCol, sliceRow, getPixelByte(col, row)); // //if(counter > 10) // //ShowGUIMessage.showGUIMessage("new", slice.getBufferedImage(), "new"); // } // sliceCol = 0; // sliceRow++; // } // counter++; // // ShowGUIMessage.showGUIMessage("new", slice.getBufferedImage(), "new"); // // return slice; JBIG2Bitmap slice = new JBIG2Bitmap(width, height, arithmeticDecoder, huffmanDecoder, mmrDecoder); int sliceRow = 0, sliceCol = 0; for (int row = y; row < height; row++) { for (int col = x; col < x + width; col++) { //System.out.println("row = "+row +" column = "+col); slice.setPixel(sliceCol, sliceRow, getPixel(col, row)); sliceCol++; } sliceCol = 0; sliceRow++; } return slice; } private void setPixel(int col, int row, FastBitSet data, int value) { if (value == 1) data.set(row, col); else data.clear(row, col); } // private void setPixelByte(int col, int row, FastBitSet data, byte bits) { // data.setByte(row, col, bits); // } // public void setPixel(int col, int row, int value) { // setPixel(col, row, data, value); // } // public int getPixel(int col, int row) { // return data.get(row, col) ? 1 : 0; // } private void setPixel(int col, int row, BitSet data, int value) { int index = (row * width) + col; data.set(index, value == 1); } public void setPixel(int col, int row, int value) { setPixel(col, row, data, value); } public int getPixel(int col, int row) { return data.get((row * width) + col) ? 1 : 0; } public void expand(int newHeight, int defaultPixel) { // System.out.println("expand FastBitSet"); // FastBitSet newData = new FastBitSet(width, newHeight); // // for (int row = 0; row < height; row++) { // for (int col = 0; col < width; col += 8) { // setPixelByte(col, row, newData, getPixelByte(col, row)); // } // } // // this.height = newHeight; // this.data = newData; BitSet newData = new BitSet(newHeight * width); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { setPixel(col, row, newData, getPixel(col, row)); } } this.height = newHeight; this.data = newData; } public void setBitmapNumber(int segmentNumber) { this.bitmapNumber = segmentNumber; } public int getBitmapNumber() { return bitmapNumber; } public BufferedImage getBufferedImage() { byte[] bytes = getData(true); if (bytes == null) return null; // make a a DEEP copy so we can't alter int len = bytes.length; byte[] copy = new byte[len]; System.arraycopy(bytes, 0, copy, 0, len); /** create an image from the raw data */ DataBuffer db = new DataBufferByte(copy, copy.length); WritableRaster raster = Raster.createPackedRaster(db, width, height, 1, null); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY); image.setData(raster); return image; } static final class FastBitSet { byte[][] bytes; int w, h; public FastBitSet(int width, int height) { bytes = new byte[height][(width + 7) / 8]; this.w = width; this.h = height; //System.out.println("width = "+width+" height = "+height); } // public int getByte(int row, int col) { // // System.out.println("(width + 7) / 8 = " + (width + 7) / 8); // System.out.println("external width = " + width + " external height = " + height); // System.out.println("internal width = " + w + " internal height = " + h); // System.out.println("row = " + row + " column = " + col); // // int offset = col / 8; // int mod = col % 8; // // System.out.println("offset = " + offset + " mod = " + mod+" bytes[row].length = "+bytes[row].length); // // if (mod == 0) // return bytes[row][offset]; // // if(offset == bytes[row].length - 1){ // System.out.println("returning"); // return ((bytes[row][offset] & 0xFF) >> mod); // } // // int left = ((bytes[row][offset] & 0xFF) >> mod); // int right = ((bytes[row][offset + 1] & 0xFF) << (8 - mod)); // // return left | right; // } // public void setByte(int row, int col, int bits) { // int offset = col / 8; // int mod = col % 8; // // System.out.println("setByte offset = " + offset + " mod = " + mod); // // // if (mod == 0) // bytes[row][offset] = (byte) bits; // else { // int mask = 0xFF >> (8 - mod); // System.out.println("setByte mask = " + mask); // bytes[row][offset] = (byte) ((bytes[row][offset] & mask) | ((bits & 0xFF) << mod)); // bytes[row][offset + 1] = (byte) ((bytes[row][offset + 1] & ~mask) | ((bits & 0xFF) >> (8 - mod))); // } // } public byte getByte(int row, int col) { // System.out.println("(width + 7) / 8 = " + (width + 7) / 8); // System.out.println("external width = " + width + " external height = " + height); // System.out.println("internal width = " + w + " internal height = " + h); // System.out.println("row = " + row + " column = " + col); int offset = col / 8; int mod = col % 8; // System.out.println("offset = " + offset + " mod = " + mod+" bytes[row].length = "+bytes[row].length); if (mod == 0) return bytes[row][offset]; // if(offset == bytes[row].length - 1){ // System.out.println("returning"); // return ((bytes[row][offset] & 0xFF) >> mod); // } byte leftMask = (byte) (0xFF >> (8 - mod)); byte rightMask = (byte) (0xFF << mod); byte left = (byte) ((bytes[row][offset] & leftMask) << (8 - mod)); if(offset + 1 >= bytes[row].length){ System.out.println("returning"); return left; } byte right = (byte) ((bytes[row][offset + 1] & rightMask) >> mod); return (byte) (left | right); } public void setByte(int row, int col, byte bits) { int offset = col / 8; int mod = col % 8; //System.out.println("setByte offset = " + offset + " mod = " + mod); if (mod == 0) bytes[row][offset] = (byte) bits; else { byte left = (byte) (bits >> mod); byte leftMask = (byte) (0xFF << (8 - mod)); bytes[row][offset] &= leftMask; bytes[row][offset] |= left; if(offset + 1 >= bytes[row].length) return; byte right = (byte) (bits << (8 - mod)); byte rightMask = (byte) (0xFF >> mod); bytes[row][offset + 1] &= rightMask; bytes[row][offset + 1] |= right; // int mask = 0xFF >> (8 - mod); // System.out.println("setByte mask = " + mask); // bytes[row][offset] = (byte) ((bytes[row][offset] & mask) | ((bits & 0xFF) << mod)); // bytes[row][offset + 1] = (byte) ((bytes[row][offset + 1] & ~mask) | ((bits & 0xFF) >> (8 - mod))); } } public void set(int row, int col) { byte bit = (byte) (1 << (col % 8)); bytes[row][col / 8] |= bit; } public void clear(int row, int col) { byte bit = (byte) (1 << (col % 8)); bytes[row][col / 8] &= ~bit; } public boolean get(int row, int col) { byte bit = (byte) (1 << (col % 8)); return (bytes[row][col / 8] & bit) != 0; } public void reset(boolean set) { for (int i = 0; i < bytes.length; i++) Arrays.fill(bytes[i], set ? (byte) 0xFF : (byte) 0x00); } } } libjpedal-jbig2-20100117/org/jpedal/jbig2/io/0000755000175000017500000000000011213455610020200 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/io/StreamReader.java0000644000175000017500000000755711213455610023437 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * StreamReader.java * --------------- */ package org.jpedal.jbig2.io; import java.io.IOException; import org.jpedal.jbig2.examples.pdf.PDFSegment; public class StreamReader { private byte[] data; private int bitPointer = 7; private int bytePointer = 0; public StreamReader(byte[] data) { this.data = data; } public short readByte(PDFSegment pdfSeg) { short bite = (short) (data[bytePointer++] & 255); if (pdfSeg != null) pdfSeg.writeToHeader(bite); return bite; } public void readByte(short[] buf, PDFSegment pdfSeg) throws IOException { for (int i = 0; i < buf.length; i++) { buf[i] = (short) (data[bytePointer++] & 255); } if (pdfSeg != null) pdfSeg.writeToHeader(buf); } public short readByte() { short bite = (short) (data[bytePointer++] & 255); return bite; } public void readByte(short[] buf) { for (int i = 0; i < buf.length; i++) { buf[i] = (short) (data[bytePointer++] & 255); } } public int readBit() { short buf = readByte(); short mask = (short) (1 << bitPointer); int bit = (buf & mask) >> bitPointer; bitPointer--; if (bitPointer == -1) { bitPointer = 7; } else { movePointer(-1); } return bit; } public int readBits(int num) { int result = 0; for (int i = 0; i < num; i++) { result = (result << 1) | readBit(); } return result; } public void movePointer(int ammount) { bytePointer += ammount; } public void consumeRemainingBits() { if (bitPointer != 7) readBits(bitPointer + 1); } public boolean isFinished() { return bytePointer == data.length; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/jai/0000755000175000017500000000000011213455610020334 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/jai/JBIG2ImageReader.java0000644000175000017500000003707611213455610024077 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * JBIG2ImageReader.java * --------------- */ package org.jpedal.jbig2.jai; import java.awt.Point; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.awt.image.DataBufferByte; import java.awt.image.Raster; import java.awt.image.WritableRaster; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.imageio.ImageReadParam; import javax.imageio.ImageReader; import javax.imageio.ImageTypeSpecifier; import javax.imageio.metadata.IIOMetadata; import javax.imageio.spi.ImageReaderSpi; import javax.imageio.stream.ImageInputStream; import org.jpedal.jbig2.JBIG2Decoder; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.image.JBIG2Bitmap; public class JBIG2ImageReader extends ImageReader { private JBIG2Decoder decoder; private ImageInputStream stream; private boolean readFile; protected JBIG2ImageReader(ImageReaderSpi originatingProvider) { // Save the identity of the ImageReaderSpi subclass that invoked this // constructor. super(originatingProvider); } public void setInput(Object input, boolean seekForwardOnly, boolean ignoreMetadata) { super.setInput(input, seekForwardOnly, ignoreMetadata); if (input == null) { this.stream = null; return; } // The input source must be an ImageInputStream because the originating // provider -- the JBIG2ImageReaderSpi class -- passes // STANDARD_INPUT_TYPE // -- an array consisting only of ImageInputStream -- to its superclass // in its constructor call. if (input instanceof ImageInputStream) this.stream = (ImageInputStream) input; else throw new IllegalArgumentException("ImageInputStream expected!"); } public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException { BufferedImage dst = null; try { // Calculate and return a Rectangle that identifies the region of // the // source image that should be read: // // 1. If param is null, the upper-left corner of the region is (0, // 0), // and the width and height are specified by the width and height // arguments. In other words, the entire image is read. // // 2. If param is not null // // 2.1 If param.getSourceRegion() returns a non-null Rectangle, the // region is calculated as the intersection of param's Rectangle // and the earlier (0, 0, width, height Rectangle). // // 2.2 param.getSubsamplingXOffset() is added to the region's x // coordinate and subtracted from its width. // // 2.3 param.getSubsamplingYOffset() is added to the region's y // coordinate and subtracted from its height. int width = getWidth(imageIndex); int height = getHeight(imageIndex); Rectangle sourceRegion = getSourceRegion(param, width, height); // Source subsampling is used to return a scaled-down source image. // Default 1 values for X and Y subsampling indicate that a // non-scaled // source image will be returned. int sourceXSubsampling = 1; int sourceYSubsampling = 1; // The destination offset determines the starting location in the // destination where decoded pixels are placed. Default (0, 0) // values indicate the upper-left corner. Point destinationOffset = new Point(0, 0); // If param is not null, override the source subsampling, and // destination offset defaults. if (param != null) { sourceXSubsampling = param.getSourceXSubsampling(); sourceYSubsampling = param.getSourceYSubsampling(); destinationOffset = param.getDestinationOffset(); } // Obtain a BufferedImage into which decoded pixels will be placed. // This destination will be returned to the application. // // 1. If param is not null // // 1.1 If param.getDestination() returns a BufferedImage // // 1.1.1 Return this BufferedImage // // Else // // 1.1.2 Invoke param.getDestinationType (). // // 1.1.3 If the returned ImageTypeSpecifier equals // getImageTypes (0) (see below), return its BufferedImage. // // 2. If param is null or a BufferedImage has not been obtained // // 2.1 Return getImageTypes (0)'s BufferedImage. dst = getDestination(param, getImageTypes(0), width, height); // Create a WritableRaster for the destination. WritableRaster wrDst = dst.getRaster(); JBIG2Bitmap bitmap = decoder.getPageAsJBIG2Bitmap(imageIndex).getSlice(sourceRegion.x, sourceRegion.y, sourceRegion.width, sourceRegion.height); BufferedImage image = bitmap.getBufferedImage(); int newWidth = (int) (image.getWidth() * (1 / (double) sourceXSubsampling)); int newHeight = (int) (image.getHeight() * (1 / (double) sourceYSubsampling)); BufferedImage scaledImage = scaleImage(image.getRaster(), newWidth, newHeight, 1, 1); Raster raster = null; if (scaledImage != null) { raster = scaledImage.getRaster(); } else raster = image.getRaster(); wrDst.setRect(destinationOffset.x, destinationOffset.y, raster); } catch (RuntimeException e) { e.printStackTrace(); } return dst; } public IIOMetadata getImageMetadata(int imageIndex) throws IOException { return null; } public IIOMetadata getStreamMetadata() throws IOException { return null; } public Iterator getImageTypes(int imageIndex) throws IOException { readFile(); checkIndex(imageIndex); // Create a List of ImageTypeSpecifiers that identify the possible image // types to which the single JBIG2 image can be decoded. An // ImageTypeSpecifier is used with ImageReader's getDestination() method // to return an appropriate BufferedImage that contains the decoded // image, and is accessed by an application. List l = new ArrayList(); // The JBIG2 reader only uses a single List entry. This entry describes // a // BufferedImage of TYPE_INT_RGB, which is a commonly used image type. l.add(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_BYTE_BINARY)); // Return an iterator that retrieves elements from the list. return l.iterator(); } public int getNumImages(boolean allowSearch) throws IOException { readFile(); return decoder.getNumberOfPages(); } public int getHeight(int imageIndex) throws IOException { readFile(); checkIndex(imageIndex); return decoder.getPageAsJBIG2Bitmap(imageIndex).getHeight(); } public int getWidth(int imageIndex) throws IOException { readFile(); checkIndex(imageIndex); return decoder.getPageAsJBIG2Bitmap(imageIndex).getWidth(); } private void checkIndex(int imageIndex) { int noOfPages = decoder.getNumberOfPages(); if (imageIndex < 0 || imageIndex > noOfPages) throw new IndexOutOfBoundsException("Bad index!"); } private static BufferedImage scaleImage(Raster ras, int pX, int pY, int comp, int d) { int w = ras.getWidth(); int h = ras.getHeight(); byte[] data = ((DataBufferByte) ras.getDataBuffer()).getData(); // see what we could reduce to and still be big enough for page int newW = w, newH = h; int sampling = 1; int smallestH = pY << 2; // double so comparison works int smallestW = pX << 2; // cannot be smaller than page while (newW > smallestW && newH > smallestH) { sampling = sampling << 1; newW = newW >> 1; newH = newH >> 1; } int scaleX = w / pX; if (scaleX < 1) scaleX = 1; int scaleY = h / pY; if (scaleY < 1) scaleY = 1; // choose smaller value so at least size of page sampling = scaleX; if (sampling > scaleY) sampling = scaleY; // switch to 8 bit and reduce bw image size by averaging if (sampling > 1) { newW = w / sampling; newH = h / sampling; if (d == 1) { int size = newW * newH; byte[] newData = new byte[size]; final int[] flag = { 1, 2, 4, 8, 16, 32, 64, 128 }; int origLineLength = (w + 7) >> 3; int bit; byte currentByte; // scan all pixels and down-sample for (int y = 0; y < newH; y++) { for (int x = 0; x < newW; x++) { int bytes = 0, count = 0; // allow for edges in number of pixels left int wCount = sampling, hCount = sampling; int wGapLeft = w - x; int hGapLeft = h - y; if (wCount > wGapLeft) wCount = wGapLeft; if (hCount > hGapLeft) hCount = hGapLeft; // count pixels in sample we will make into a pixel (ie // 2x2 is 4 pixels , 4x4 is 16 pixels) for (int yy = 0; yy < hCount; yy++) { for (int xx = 0; xx < wCount; xx++) { currentByte = data[((yy + (y * sampling)) * origLineLength) + (((x * sampling) + xx) >> 3)]; bit = currentByte & flag[7 - (((x * sampling) + xx) & 7)]; if (bit != 0) bytes++; count++; } } // set value as white or average of pixels int offset = x + (newW * y); if (count > 0) { newData[offset] = (byte) ((255 * bytes) / count); } else { newData[offset] = (byte) 255; } } } data = newData; h = newH; w = newW; d = 8; // imageMask=false; } else if (d == 8) { int x = 0, y = 0, xx = 0, yy = 0, jj = 0, origLineLength = 0; try { // black and white if (w * h == data.length) comp = 1; byte[] newData = new byte[newW * newH * comp]; // System.err.println(w+" "+h+" "+data.length+" // comp="+comp+" scaling="+sampling+" "+decodeColorData); origLineLength = w * comp; // System.err.println("size="+w*h*comp+" filter"+filter+" // scaling="+sampling+" comp="+comp); // System.err.println("w="+w+" h="+h+" data="+data.length+" // origLineLength="+origLineLength+" sampling="+sampling); // scan all pixels and down-sample for (y = 0; y < newH; y++) { for (x = 0; x < newW; x++) { // allow for edges in number of pixels left int wCount = sampling, hCount = sampling; int wGapLeft = w - x; int hGapLeft = h - y; if (wCount > wGapLeft) wCount = wGapLeft; if (hCount > hGapLeft) hCount = hGapLeft; for (jj = 0; jj < comp; jj++) { int byteTotal = 0, count = 0; // count pixels in sample we will make into a // pixel (ie 2x2 is 4 pixels , 4x4 is 16 pixels) for (yy = 0; yy < hCount; yy++) { for (xx = 0; xx < wCount; xx++) { byteTotal = byteTotal + (data[((yy + (y * sampling)) * origLineLength) + (((x * sampling * comp) + (xx * comp) + jj))] & 255); count++; } } // set value as white or average of pixels if (count > 0) // if(index==null) newData[jj + (x * comp) + (newW * y * comp)] = (byte) ((byteTotal) / count); // else // newData[x+(newW*y)]=(byte)(((index[1] & // 255)*byteTotal)/count); else { // if(index==null) // newData[jj+x+(newW*y*comp)]=(byte) 255; // else // newData[x+(newW*y)]=index[0]; } } } } data = newData; h = newH; w = newW; } catch (Exception e) { // System.err.println("xx=" + xx + " yy=" + yy + " jj=" + jj + " ptr=" + ((yy + (y * sampling)) * origLineLength) + (((x * sampling) + (xx * comp) + jj)) + '/' + data.length); // System.err.println("index="+index); System.err.println(((yy + (y * sampling)) * origLineLength) + " " + (((x * sampling) + (xx * comp) + jj))); System.err.println("w=" + w + " h=" + h + " sampling=" + sampling + " x=" + x + " y=" + y); // System.out.println("xx="+xx+" yy="+yy); e.printStackTrace(); // } } } if (sampling > 1) { final int[] bands = { 0 }; // System.out.println("w=" + w + " h=" + h + " size=" + // data.length); // WritableRaster raster =Raster.createPackedRaster(new // DataBufferByte(newData, newData.length), newW, newH, 1, null); Raster raster = Raster.createInterleavedRaster(new DataBufferByte(data, data.length), w, h, w, 1, bands, null); BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY); image.setData(raster); return image; } else { return null; } } private void readFile() { // Do not allow this header to be read more than once. if (readFile) return; // Make sure that the application has set the input source. if (stream == null) throw new IllegalStateException("No input stream!"); // Read the header. decoder = new JBIG2Decoder(); try { byte[] data; int size = (int) stream.length(); if (size == -1) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] temp = new byte[8192]; for (int len = 0; (len = stream.read(temp)) > 0;) { bos.write(temp, 0, len); } bos.close(); data = bos.toByteArray(); } else { data = new byte[size]; stream.readFully(data); } decoder.decodeJBIG2(data); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JBIG2Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } readFile = true; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/jai/JBIG2ImageReaderSpi.java0000644000175000017500000001177011213455610024544 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * JBIG2ImageReaderSpi.java * --------------- */ package org.jpedal.jbig2.jai; import java.io.IOException; import java.util.Arrays; import javax.imageio.ImageReader; import javax.imageio.spi.ImageReaderSpi; import javax.imageio.stream.ImageInputStream; public class JBIG2ImageReaderSpi extends ImageReaderSpi { public JBIG2ImageReaderSpi() { super("JPedal", // vendorName "1.0", // version new String[] { "JBIG2" }, // names new String[] { "jb2", "jbig2" }, // suffixes new String[] { "image/x-jbig2" }, // MIMETypes "org.jpedal.jbig2.jai.JBIG2ImageReader", // readerClassName STANDARD_INPUT_TYPE, // inputTypes null, // writerSpiNames false, // supportsStandardStreamMetadataFormat null, // nativeStreamMetadataFormatName null, // nativeStreamMetadataFormatClassName null, // extraStreamMetadataFormatNames null, // extraStreamMetadataFormatClassNames false, // supportsStandardImageMetadataFormat null, // nativeImageMetadataFormatName null, // nativeImageMetadataFormatClassName null, // extraImageMetadataFormatNames null); // extraImageMetadataFormatClassNames } public boolean canDecodeInput(Object input) throws IOException { // The input source must be an ImageInputStream because the constructor // passes STANDARD_INPUT_TYPE (an array consisting of ImageInputStream) // as the only type of input source that it will deal with to its // superclass. if (!(input instanceof ImageInputStream)) return false; ImageInputStream stream = (ImageInputStream) input; /** Read and validate the input source's header. */ byte[] header = new byte[8]; try { // The input source's current position must be preserved so that // other ImageReaderSpis can determine if they can decode the input // source's format, should this input source be unable to handle the // decoding. Because the input source is an ImageInputStream, its // mark() and reset() methods are called to preserve the current // position. stream.mark(); stream.read(header); stream.reset(); } catch (IOException e) { return false; } byte[] controlHeader = new byte[] { (byte) 151, 74, 66, 50, 13, 10, 26, 10 }; return Arrays.equals(controlHeader, header); } public ImageReader createReaderInstance(Object extension) throws IOException { // Inform the JBIG2 image reader that this JBIG2 image reader SPI is the // originating provider -- the object that creates the JBIG2 image // reader. return new JBIG2ImageReader(this); } public String getDescription(java.util.Locale locale) { return "JPedal JBIG2 Image Decoder provided by IDRsolutions. See http://www.jpedal.org/jbig.php"; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/0000755000175000017500000000000011213455610021233 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/segment/extensions/0000755000175000017500000000000011213455610023432 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/segment/extensions/ExtensionSegment.java0000644000175000017500000000545211213455610027602 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * ExtensionSegment.java * --------------- */ package org.jpedal.jbig2.segment.extensions; import java.io.IOException; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.segment.Segment; public class ExtensionSegment extends Segment { public ExtensionSegment(JBIG2StreamDecoder streamDecoder) { super(streamDecoder); } public void readSegment() throws IOException, JBIG2Exception { for (int i = 0; i < getSegmentHeader().getSegmentDataLength(); i++) { decoder.readByte(); } } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/pageinformation/0000755000175000017500000000000011213455610024415 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/segment/pageinformation/PageInformationFlags.java0000644000175000017500000000577711213455610031337 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * PageInformationFlags.java * --------------- */ package org.jpedal.jbig2.segment.pageinformation; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.segment.Flags; public class PageInformationFlags extends Flags { public static String DEFAULT_PIXEL_VALUE = "DEFAULT_PIXEL_VALUE"; public static String DEFAULT_COMBINATION_OPERATOR = "DEFAULT_COMBINATION_OPERATOR"; public void setFlags(int flagsAsInt) { this.flagsAsInt = flagsAsInt; /** extract DEFAULT_PIXEL_VALUE */ flags.put(DEFAULT_PIXEL_VALUE, new Integer((flagsAsInt >> 2) & 1)); /** extract DEFAULT_COMBINATION_OPERATOR */ flags.put(DEFAULT_COMBINATION_OPERATOR, new Integer((flagsAsInt >> 3) & 3)); if (JBIG2StreamDecoder.debug) System.out.println(flags); } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/pageinformation/PageInformationSegment.java0000644000175000017500000001151411213455610031667 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * PageInformationSegment.java * --------------- */ package org.jpedal.jbig2.segment.pageinformation; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.image.JBIG2Bitmap; import org.jpedal.jbig2.segment.Segment; import org.jpedal.jbig2.util.BinaryOperation; import java.io.IOException; public class PageInformationSegment extends Segment { private int pageBitmapHeight, pageBitmapWidth; private int yResolution, xResolution; PageInformationFlags pageInformationFlags = new PageInformationFlags(); private int pageStriping; private JBIG2Bitmap pageBitmap; public PageInformationSegment(JBIG2StreamDecoder streamDecoder) { super(streamDecoder); } public PageInformationFlags getPageInformationFlags() { return pageInformationFlags; } public JBIG2Bitmap getPageBitmap() { return pageBitmap; } public void readSegment() throws IOException, JBIG2Exception { if (JBIG2StreamDecoder.debug) System.out.println("==== Reading Page Information Dictionary ===="); short[] buff = new short[4]; decoder.readByte(buff); pageBitmapWidth = BinaryOperation.getInt32(buff); buff = new short[4]; decoder.readByte(buff); pageBitmapHeight = BinaryOperation.getInt32(buff); if (JBIG2StreamDecoder.debug) System.out.println("Bitmap size = " + pageBitmapWidth + 'x' + pageBitmapHeight); buff = new short[4]; decoder.readByte(buff); xResolution = BinaryOperation.getInt32(buff); buff = new short[4]; decoder.readByte(buff); yResolution = BinaryOperation.getInt32(buff); if (JBIG2StreamDecoder.debug) System.out.println("Resolution = " + xResolution + 'x' + yResolution); /** extract page information flags */ short pageInformationFlagsField = decoder.readByte(); pageInformationFlags.setFlags(pageInformationFlagsField); if (JBIG2StreamDecoder.debug) System.out.println("symbolDictionaryFlags = " + pageInformationFlagsField); buff = new short[2]; decoder.readByte(buff); pageStriping = BinaryOperation.getInt16(buff); if (JBIG2StreamDecoder.debug) System.out.println("Page Striping = " + pageStriping); int defPix = pageInformationFlags.getFlagValue(PageInformationFlags.DEFAULT_PIXEL_VALUE); int height; if (pageBitmapHeight == -1) { height = pageStriping & 0x7fff; } else { height = pageBitmapHeight; } pageBitmap = new JBIG2Bitmap(pageBitmapWidth, height, arithmeticDecoder, huffmanDecoder, mmrDecoder); pageBitmap.clear(defPix); } public int getPageBitmapHeight() { return pageBitmapHeight; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/pattern/0000755000175000017500000000000011213455610022710 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/segment/pattern/PatternDictionaryFlags.java0000644000175000017500000000557411213455610030206 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * PatternDictionaryFlags.java * --------------- */ package org.jpedal.jbig2.segment.pattern; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.segment.Flags; public class PatternDictionaryFlags extends Flags { public static String HD_MMR = "HD_MMR"; public static String HD_TEMPLATE = "HD_TEMPLATE"; public void setFlags(int flagsAsInt) { this.flagsAsInt = flagsAsInt; /** extract HD_MMR */ flags.put(HD_MMR, new Integer(flagsAsInt & 1)); /** extract HD_TEMPLATE */ flags.put(HD_TEMPLATE, new Integer((flagsAsInt >> 1) & 3)); if (JBIG2StreamDecoder.debug) System.out.println(flags); } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/pattern/PatternDictionarySegment.java0000644000175000017500000001224011213455610030540 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * PatternDictionarySegment.java * --------------- */ package org.jpedal.jbig2.segment.pattern; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.image.JBIG2Bitmap; import org.jpedal.jbig2.segment.Segment; import org.jpedal.jbig2.util.BinaryOperation; import java.io.IOException; public class PatternDictionarySegment extends Segment { PatternDictionaryFlags patternDictionaryFlags = new PatternDictionaryFlags(); private int width; private int height; private int grayMax; private JBIG2Bitmap[] bitmaps; private int size; public PatternDictionarySegment(JBIG2StreamDecoder streamDecoder) { super(streamDecoder); } public void readSegment() throws IOException, JBIG2Exception { /** read text region Segment flags */ readPatternDictionaryFlags(); width = decoder.readByte(); height = decoder.readByte(); if (JBIG2StreamDecoder.debug) System.out.println("pattern dictionary size = " + width + " , " + height); short[] buf = new short[4]; decoder.readByte(buf); grayMax = BinaryOperation.getInt32(buf); if (JBIG2StreamDecoder.debug) System.out.println("grey max = " + grayMax); boolean useMMR = patternDictionaryFlags.getFlagValue(PatternDictionaryFlags.HD_MMR) == 1; int template = patternDictionaryFlags.getFlagValue(PatternDictionaryFlags.HD_TEMPLATE); if (!useMMR) { arithmeticDecoder.resetGenericStats(template, null); arithmeticDecoder.start(); } short[] genericBAdaptiveTemplateX = new short[4], genericBAdaptiveTemplateY = new short[4]; genericBAdaptiveTemplateX[0] = (short) -width; genericBAdaptiveTemplateY[0] = 0; genericBAdaptiveTemplateX[1] = -3; genericBAdaptiveTemplateY[1] = -1; genericBAdaptiveTemplateX[2] = 2; genericBAdaptiveTemplateY[2] = -2; genericBAdaptiveTemplateX[3] = -2; genericBAdaptiveTemplateY[3] = -2; size = grayMax + 1; JBIG2Bitmap bitmap = new JBIG2Bitmap(size * width, height, arithmeticDecoder, huffmanDecoder, mmrDecoder); bitmap.clear(0); bitmap.readBitmap(useMMR, template, false, false, null, genericBAdaptiveTemplateX, genericBAdaptiveTemplateY, segmentHeader.getSegmentDataLength() - 7); JBIG2Bitmap bitmaps[] = new JBIG2Bitmap[size]; int x = 0; for (int i = 0; i < size; i++) { bitmaps[i] = bitmap.getSlice(x, 0, width, height); x += width; } this.bitmaps = bitmaps; } public JBIG2Bitmap[] getBitmaps() { return bitmaps; } private void readPatternDictionaryFlags() throws IOException { short patternDictionaryFlagsField = decoder.readByte(); patternDictionaryFlags.setFlags(patternDictionaryFlagsField); if (JBIG2StreamDecoder.debug) System.out.println("pattern Dictionary flags = " + patternDictionaryFlagsField); } public PatternDictionaryFlags getPatternDictionaryFlags() { return patternDictionaryFlags; } public int getSize() { return size; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/region/0000755000175000017500000000000011213455610022516 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/segment/region/generic/0000755000175000017500000000000011213455610024132 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/segment/region/generic/GenericRegionFlags.java0000644000175000017500000000577111213455610030504 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * GenericRegionFlags.java * --------------- */ package org.jpedal.jbig2.segment.region.generic; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.segment.Flags; public class GenericRegionFlags extends Flags { public static String MMR = "MMR"; public static String GB_TEMPLATE = "GB_TEMPLATE"; public static String TPGDON = "TPGDON"; public void setFlags(int flagsAsInt) { this.flagsAsInt = flagsAsInt; /** extract MMR */ flags.put(MMR, new Integer(flagsAsInt & 1)); /** extract GB_TEMPLATE */ flags.put(GB_TEMPLATE, new Integer((flagsAsInt >> 1) & 3)); /** extract TPGDON */ flags.put(TPGDON, new Integer((flagsAsInt >> 3) & 1)); if(JBIG2StreamDecoder.debug) System.out.println(flags); } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/region/generic/GenericRegionSegment.java0000644000175000017500000001676011213455610031052 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * GenericRegionSegment.java * --------------- */ package org.jpedal.jbig2.segment.region.generic; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.image.JBIG2Bitmap; import org.jpedal.jbig2.segment.pageinformation.PageInformationFlags; import org.jpedal.jbig2.segment.pageinformation.PageInformationSegment; import org.jpedal.jbig2.segment.region.RegionFlags; import org.jpedal.jbig2.segment.region.RegionSegment; import java.io.IOException; public class GenericRegionSegment extends RegionSegment { private GenericRegionFlags genericRegionFlags = new GenericRegionFlags(); private boolean inlineImage; private boolean unknownLength = false; public GenericRegionSegment(JBIG2StreamDecoder streamDecoder, boolean inlineImage) { super(streamDecoder); this.inlineImage = inlineImage; } public void readSegment() throws IOException, JBIG2Exception { if(JBIG2StreamDecoder.debug) System.out.println("==== Reading Immediate Generic Region ===="); super.readSegment(); /** read text region Segment flags */ readGenericRegionFlags(); boolean useMMR = genericRegionFlags.getFlagValue(GenericRegionFlags.MMR) != 0; int template = genericRegionFlags.getFlagValue(GenericRegionFlags.GB_TEMPLATE); short[] genericBAdaptiveTemplateX = new short[4]; short[] genericBAdaptiveTemplateY = new short[4]; if (!useMMR) { if (template == 0) { genericBAdaptiveTemplateX[0] = readATValue(); genericBAdaptiveTemplateY[0] = readATValue(); genericBAdaptiveTemplateX[1] = readATValue(); genericBAdaptiveTemplateY[1] = readATValue(); genericBAdaptiveTemplateX[2] = readATValue(); genericBAdaptiveTemplateY[2] = readATValue(); genericBAdaptiveTemplateX[3] = readATValue(); genericBAdaptiveTemplateY[3] = readATValue(); } else { genericBAdaptiveTemplateX[0] = readATValue(); genericBAdaptiveTemplateY[0] = readATValue(); } arithmeticDecoder.resetGenericStats(template, null); arithmeticDecoder.start(); } boolean typicalPredictionGenericDecodingOn = genericRegionFlags.getFlagValue(GenericRegionFlags.TPGDON) != 0; int length = segmentHeader.getSegmentDataLength(); if(length == -1) { /** * length of data is unknown, so it needs to be determined through examination of the data. * See 7.2.7 - Segment data length of the JBIG2 specification. */ unknownLength = true; short match1; short match2; if(useMMR) { // look for 0x00 0x00 (0, 0) match1 = 0; match2 = 0; } else { // look for 0xFF 0xAC (255, 172) match1 = 255; match2 = 172; } int bytesRead = 0; while(true) { short bite1 = decoder.readByte(); bytesRead++; if(bite1 == match1){ short bite2 = decoder.readByte(); bytesRead++; if(bite2 == match2){ length = bytesRead - 2; break; } } } decoder.movePointer(-bytesRead); } JBIG2Bitmap bitmap = new JBIG2Bitmap(regionBitmapWidth, regionBitmapHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder); bitmap.clear(0); bitmap.readBitmap(useMMR, template, typicalPredictionGenericDecodingOn, false, null, genericBAdaptiveTemplateX, genericBAdaptiveTemplateY, useMMR ? 0 : length - 18); if (inlineImage) { PageInformationSegment pageSegment = decoder.findPageSegement(segmentHeader.getPageAssociation()); JBIG2Bitmap pageBitmap = pageSegment.getPageBitmap(); int extCombOp = regionFlags.getFlagValue(RegionFlags.EXTERNAL_COMBINATION_OPERATOR); if(pageSegment.getPageBitmapHeight() == -1 && regionBitmapYLocation + regionBitmapHeight > pageBitmap.getHeight()) { pageBitmap.expand(regionBitmapYLocation + regionBitmapHeight, pageSegment.getPageInformationFlags().getFlagValue(PageInformationFlags.DEFAULT_PIXEL_VALUE)); } pageBitmap.combine(bitmap, regionBitmapXLocation, regionBitmapYLocation, extCombOp); } else { bitmap.setBitmapNumber(getSegmentHeader().getSegmentNumber()); decoder.appendBitmap(bitmap); } if(unknownLength) { decoder.movePointer(4); } } private void readGenericRegionFlags() throws IOException { /** extract text region Segment flags */ short genericRegionFlagsField = decoder.readByte(); genericRegionFlags.setFlags(genericRegionFlagsField); if(JBIG2StreamDecoder.debug) System.out.println("generic region Segment flags = " + genericRegionFlagsField); } public GenericRegionFlags getGenericRegionFlags() { return genericRegionFlags; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/region/halftone/0000755000175000017500000000000011213455610024316 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/segment/region/halftone/HalftoneRegionFlags.java0000644000175000017500000000653311213455610031051 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * HalftoneRegionFlags.java * --------------- */ package org.jpedal.jbig2.segment.region.halftone; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.segment.Flags; public class HalftoneRegionFlags extends Flags { public static String H_MMR = "H_MMR"; public static String H_TEMPLATE = "H_TEMPLATE"; public static String H_ENABLE_SKIP = "H_ENABLE_SKIP"; public static String H_COMB_OP = "H_COMB_OP"; public static String H_DEF_PIXEL = "H_DEF_PIXEL"; public void setFlags(int flagsAsInt) { this.flagsAsInt = flagsAsInt; /** extract H_MMR */ flags.put(H_MMR, new Integer(flagsAsInt & 1)); /** extract H_TEMPLATE */ flags.put(H_TEMPLATE, new Integer((flagsAsInt >> 1) & 3)); /** extract H_ENABLE_SKIP */ flags.put(H_ENABLE_SKIP, new Integer((flagsAsInt >> 3) & 1)); /** extract H_COMB_OP */ flags.put(H_COMB_OP, new Integer((flagsAsInt >> 4) & 7)); /** extract H_DEF_PIXEL */ flags.put(H_DEF_PIXEL, new Integer((flagsAsInt >> 7) & 1)); if(JBIG2StreamDecoder.debug) System.out.println(flags); } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/region/halftone/HalftoneRegionSegment.java0000644000175000017500000002152211213455610031412 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * HalftoneRegionSegment.java * --------------- */ package org.jpedal.jbig2.segment.region.halftone; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.image.JBIG2Bitmap; import org.jpedal.jbig2.segment.Segment; import org.jpedal.jbig2.segment.pageinformation.PageInformationSegment; import org.jpedal.jbig2.segment.pattern.PatternDictionarySegment; import org.jpedal.jbig2.segment.region.RegionFlags; import org.jpedal.jbig2.segment.region.RegionSegment; import org.jpedal.jbig2.util.BinaryOperation; import java.io.IOException; public class HalftoneRegionSegment extends RegionSegment { private HalftoneRegionFlags halftoneRegionFlags = new HalftoneRegionFlags(); private boolean inlineImage; public HalftoneRegionSegment(JBIG2StreamDecoder streamDecoder, boolean inlineImage) { super(streamDecoder); this.inlineImage = inlineImage; } public void readSegment() throws IOException, JBIG2Exception { super.readSegment(); /** read text region Segment flags */ readHalftoneRegionFlags(); short[] buf = new short[4]; decoder.readByte(buf); int gridWidth = BinaryOperation.getInt32(buf); buf = new short[4]; decoder.readByte(buf); int gridHeight = BinaryOperation.getInt32(buf); buf = new short[4]; decoder.readByte(buf); int gridX = BinaryOperation.getInt32(buf); buf = new short[4]; decoder.readByte(buf); int gridY = BinaryOperation.getInt32(buf); if (JBIG2StreamDecoder.debug) System.out.println("grid pos and size = " + gridX + ',' + gridY + ' ' + gridWidth + ',' + gridHeight); buf = new short[2]; decoder.readByte(buf); int stepX = BinaryOperation.getInt16(buf); buf = new short[2]; decoder.readByte(buf); int stepY = BinaryOperation.getInt16(buf); if (JBIG2StreamDecoder.debug) System.out.println("step size = " + stepX + ',' + stepY); int[] referedToSegments = segmentHeader.getReferredToSegments(); if (referedToSegments.length != 1) { System.out.println("Error in halftone Segment. refSegs should == 1"); } Segment segment = decoder.findSegment(referedToSegments[0]); if (segment.getSegmentHeader().getSegmentType() != Segment.PATTERN_DICTIONARY) { if(JBIG2StreamDecoder.debug) System.out.println("Error in halftone Segment. bad symbol dictionary reference"); } PatternDictionarySegment patternDictionarySegment = (PatternDictionarySegment) segment; int bitsPerValue = 0, i = 1; while (i < patternDictionarySegment.getSize()) { bitsPerValue++; i <<= 1; } JBIG2Bitmap bitmap = patternDictionarySegment.getBitmaps()[0]; int patternWidth = bitmap.getWidth(); int patternHeight = bitmap.getHeight(); if (JBIG2StreamDecoder.debug) System.out.println("pattern size = " + patternWidth + ',' + patternHeight); boolean useMMR = halftoneRegionFlags.getFlagValue(HalftoneRegionFlags.H_MMR) != 0; int template = halftoneRegionFlags.getFlagValue(HalftoneRegionFlags.H_TEMPLATE); if (!useMMR) { arithmeticDecoder.resetGenericStats(template, null); arithmeticDecoder.start(); } int halftoneDefaultPixel = halftoneRegionFlags.getFlagValue(HalftoneRegionFlags.H_DEF_PIXEL); bitmap = new JBIG2Bitmap(regionBitmapWidth, regionBitmapHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder); bitmap.clear(halftoneDefaultPixel); boolean enableSkip = halftoneRegionFlags.getFlagValue(HalftoneRegionFlags.H_ENABLE_SKIP) != 0; JBIG2Bitmap skipBitmap = null; if (enableSkip) { skipBitmap = new JBIG2Bitmap(gridWidth, gridHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder); skipBitmap.clear(0); for (int y = 0; y < gridHeight; y++) { for (int x = 0; x < gridWidth; x++) { int xx = gridX + y * stepY + x * stepX; int yy = gridY + y * stepX - x * stepY; if (((xx + patternWidth) >> 8) <= 0 || (xx >> 8) >= regionBitmapWidth || ((yy + patternHeight) >> 8) <= 0 || (yy >> 8) >= regionBitmapHeight) { skipBitmap.setPixel(y, x, 1); } } } } int[] grayScaleImage = new int[gridWidth * gridHeight]; short[] genericBAdaptiveTemplateX = new short[4], genericBAdaptiveTemplateY = new short[4]; genericBAdaptiveTemplateX[0] = (short) (template <= 1 ? 3 : 2); genericBAdaptiveTemplateY[0] = -1; genericBAdaptiveTemplateX[1] = -3; genericBAdaptiveTemplateY[1] = -1; genericBAdaptiveTemplateX[2] = 2; genericBAdaptiveTemplateY[2] = -2; genericBAdaptiveTemplateX[3] = -2; genericBAdaptiveTemplateY[3] = -2; JBIG2Bitmap grayBitmap ; for (int j = bitsPerValue - 1; j >= 0; --j) { grayBitmap = new JBIG2Bitmap(gridWidth, gridHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder); grayBitmap.readBitmap(useMMR, template, false, enableSkip, skipBitmap, genericBAdaptiveTemplateX, genericBAdaptiveTemplateY, -1); i = 0; for (int row = 0; row < gridHeight; row++) { for (int col = 0; col < gridWidth; col++) { int bit = grayBitmap.getPixel(col, row) ^ (grayScaleImage[i] & 1); grayScaleImage[i] = (grayScaleImage[i] << 1) | bit; i++; } } } int combinationOperator = halftoneRegionFlags.getFlagValue(HalftoneRegionFlags.H_COMB_OP); i = 0; for (int col = 0; col < gridHeight; col++) { int xx = gridX + col * stepY; int yy = gridY + col * stepX; for (int row = 0; row < gridWidth; row++) { if (!(enableSkip && skipBitmap.getPixel(col, row) == 1)) { JBIG2Bitmap patternBitmap = patternDictionarySegment.getBitmaps()[grayScaleImage[i]]; bitmap.combine(patternBitmap, xx >> 8, yy >> 8, combinationOperator); } xx += stepX; yy -= stepY; i++; } } if (inlineImage) { PageInformationSegment pageSegment = decoder.findPageSegement(segmentHeader.getPageAssociation()); JBIG2Bitmap pageBitmap = pageSegment.getPageBitmap(); int externalCombinationOperator = regionFlags.getFlagValue(RegionFlags.EXTERNAL_COMBINATION_OPERATOR); pageBitmap.combine(bitmap, regionBitmapXLocation, regionBitmapYLocation, externalCombinationOperator); } else { bitmap.setBitmapNumber(getSegmentHeader().getSegmentNumber()); decoder.appendBitmap(bitmap); } } private void readHalftoneRegionFlags() throws IOException { /** extract text region Segment flags */ short halftoneRegionFlagsField = decoder.readByte(); halftoneRegionFlags.setFlags(halftoneRegionFlagsField); if (JBIG2StreamDecoder.debug) System.out.println("generic region Segment flags = " + halftoneRegionFlagsField); } public HalftoneRegionFlags getHalftoneRegionFlags() { return halftoneRegionFlags; } }libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/region/refinement/0000755000175000017500000000000011213455610024652 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/segment/region/refinement/RefinementRegionFlags.java0000644000175000017500000000560411213455610031737 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * RefinementRegionFlags.java * --------------- */ package org.jpedal.jbig2.segment.region.refinement; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.segment.Flags; public class RefinementRegionFlags extends Flags { public static String GR_TEMPLATE = "GR_TEMPLATE"; public static String TPGDON = "TPGDON"; public void setFlags(int flagsAsInt) { this.flagsAsInt = flagsAsInt; /** extract GR_TEMPLATE */ flags.put(GR_TEMPLATE, new Integer(flagsAsInt & 1)); /** extract TPGDON */ flags.put(TPGDON, new Integer((flagsAsInt >> 1) & 1)); if (JBIG2StreamDecoder.debug) System.out.println(flags); } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/region/refinement/RefinementRegionSegment.java0000644000175000017500000001503711213455610032306 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * RefinementRegionSegment.java * --------------- */ package org.jpedal.jbig2.segment.region.refinement; import java.io.IOException; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.image.JBIG2Bitmap; import org.jpedal.jbig2.segment.pageinformation.PageInformationFlags; import org.jpedal.jbig2.segment.pageinformation.PageInformationSegment; import org.jpedal.jbig2.segment.region.RegionFlags; import org.jpedal.jbig2.segment.region.RegionSegment; public class RefinementRegionSegment extends RegionSegment { private RefinementRegionFlags refinementRegionFlags = new RefinementRegionFlags(); private boolean inlineImage; private int noOfReferedToSegments; int[] referedToSegments; public RefinementRegionSegment(JBIG2StreamDecoder streamDecoder, boolean inlineImage, int[] referedToSegments, int noOfReferedToSegments) { super(streamDecoder); this.inlineImage = inlineImage; this.referedToSegments = referedToSegments; this.noOfReferedToSegments = noOfReferedToSegments; } public void readSegment() throws IOException, JBIG2Exception { if (JBIG2StreamDecoder.debug) System.out.println("==== Reading Generic Refinement Region ===="); super.readSegment(); /** read text region segment flags */ readGenericRegionFlags(); short[] genericRegionAdaptiveTemplateX = new short[2]; short[] genericRegionAdaptiveTemplateY = new short[2]; int template = refinementRegionFlags.getFlagValue(RefinementRegionFlags.GR_TEMPLATE); if (template == 0) { genericRegionAdaptiveTemplateX[0] = readATValue(); genericRegionAdaptiveTemplateY[0] = readATValue(); genericRegionAdaptiveTemplateX[1] = readATValue(); genericRegionAdaptiveTemplateY[1] = readATValue(); } if (noOfReferedToSegments == 0 || inlineImage) { PageInformationSegment pageSegment = decoder.findPageSegement(segmentHeader.getPageAssociation()); JBIG2Bitmap pageBitmap = pageSegment.getPageBitmap(); if (pageSegment.getPageBitmapHeight() == -1 && regionBitmapYLocation + regionBitmapHeight > pageBitmap.getHeight()) { pageBitmap.expand(regionBitmapYLocation + regionBitmapHeight, pageSegment.getPageInformationFlags().getFlagValue(PageInformationFlags.DEFAULT_PIXEL_VALUE)); } } if (noOfReferedToSegments > 1) { if(JBIG2StreamDecoder.debug) System.out.println("Bad reference in JBIG2 generic refinement Segment"); return; } JBIG2Bitmap referedToBitmap; if (noOfReferedToSegments == 1) { referedToBitmap = decoder.findBitmap(referedToSegments[0]); } else { PageInformationSegment pageSegment = decoder.findPageSegement(segmentHeader.getPageAssociation()); JBIG2Bitmap pageBitmap = pageSegment.getPageBitmap(); referedToBitmap = pageBitmap.getSlice(regionBitmapXLocation, regionBitmapYLocation, regionBitmapWidth, regionBitmapHeight); } arithmeticDecoder.resetRefinementStats(template, null); arithmeticDecoder.start(); boolean typicalPredictionGenericRefinementOn = refinementRegionFlags.getFlagValue(RefinementRegionFlags.TPGDON) != 0; JBIG2Bitmap bitmap = new JBIG2Bitmap(regionBitmapWidth, regionBitmapHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder); bitmap.readGenericRefinementRegion(template, typicalPredictionGenericRefinementOn, referedToBitmap, 0, 0, genericRegionAdaptiveTemplateX, genericRegionAdaptiveTemplateY); if (inlineImage) { PageInformationSegment pageSegment = decoder.findPageSegement(segmentHeader.getPageAssociation()); JBIG2Bitmap pageBitmap = pageSegment.getPageBitmap(); int extCombOp = regionFlags.getFlagValue(RegionFlags.EXTERNAL_COMBINATION_OPERATOR); pageBitmap.combine(bitmap, regionBitmapXLocation, regionBitmapYLocation, extCombOp); } else { bitmap.setBitmapNumber(getSegmentHeader().getSegmentNumber()); decoder.appendBitmap(bitmap); } } private void readGenericRegionFlags() throws IOException { /** extract text region Segment flags */ short refinementRegionFlagsField = decoder.readByte(); refinementRegionFlags.setFlags(refinementRegionFlagsField); if (JBIG2StreamDecoder.debug) System.out.println("generic region Segment flags = " + refinementRegionFlagsField); } public RefinementRegionFlags getGenericRegionFlags() { return refinementRegionFlags; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/region/text/0000755000175000017500000000000011213455610023502 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/segment/region/text/TextRegionFlags.java0000644000175000017500000000770211213455610027420 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * TextRegionFlags.java * --------------- */ package org.jpedal.jbig2.segment.region.text; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.segment.Flags; public class TextRegionFlags extends Flags { public static String SB_HUFF = "SB_HUFF"; public static String SB_REFINE = "SB_REFINE"; public static String LOG_SB_STRIPES = "LOG_SB_STRIPES"; public static String REF_CORNER = "REF_CORNER"; public static String TRANSPOSED = "TRANSPOSED"; public static String SB_COMB_OP = "SB_COMB_OP"; public static String SB_DEF_PIXEL = "SB_DEF_PIXEL"; public static String SB_DS_OFFSET = "SB_DS_OFFSET"; public static String SB_R_TEMPLATE = "SB_R_TEMPLATE"; public void setFlags(int flagsAsInt) { this.flagsAsInt = flagsAsInt; /** extract SB_HUFF */ flags.put(SB_HUFF, new Integer(flagsAsInt & 1)); /** extract SB_REFINE */ flags.put(SB_REFINE, new Integer((flagsAsInt >> 1) & 1)); /** extract LOG_SB_STRIPES */ flags.put(LOG_SB_STRIPES, new Integer((flagsAsInt >> 2) & 3)); /** extract REF_CORNER */ flags.put(REF_CORNER, new Integer((flagsAsInt >> 4) & 3)); /** extract TRANSPOSED */ flags.put(TRANSPOSED, new Integer((flagsAsInt >> 6) & 1)); /** extract SB_COMB_OP */ flags.put(SB_COMB_OP, new Integer((flagsAsInt >> 7) & 3)); /** extract SB_DEF_PIXEL */ flags.put(SB_DEF_PIXEL, new Integer((flagsAsInt >> 9) & 1)); int sOffset = (flagsAsInt >> 10) & 0x1f; if ((sOffset & 0x10) != 0) { sOffset |= -1 - 0x0f; } flags.put(SB_DS_OFFSET, new Integer(sOffset)); /** extract SB_R_TEMPLATE */ flags.put(SB_R_TEMPLATE, new Integer((flagsAsInt >> 15) & 1)); if (JBIG2StreamDecoder.debug) System.out.println(flags); } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/region/text/TextRegionHuffmanFlags.java0000644000175000017500000000740511213455610030725 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * TextRegionHuffmanFlags.java * --------------- */ package org.jpedal.jbig2.segment.region.text; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.segment.Flags; public class TextRegionHuffmanFlags extends Flags { public static String SB_HUFF_FS = "SB_HUFF_FS"; public static String SB_HUFF_DS = "SB_HUFF_DS"; public static String SB_HUFF_DT = "SB_HUFF_DT"; public static String SB_HUFF_RDW = "SB_HUFF_RDW"; public static String SB_HUFF_RDH = "SB_HUFF_RDH"; public static String SB_HUFF_RDX = "SB_HUFF_RDX"; public static String SB_HUFF_RDY = "SB_HUFF_RDY"; public static String SB_HUFF_RSIZE = "SB_HUFF_RSIZE"; public void setFlags(int flagsAsInt) { this.flagsAsInt = flagsAsInt; /** extract SB_HUFF_FS */ flags.put(SB_HUFF_FS, new Integer(flagsAsInt & 3)); /** extract SB_HUFF_DS */ flags.put(SB_HUFF_DS, new Integer((flagsAsInt >> 2) & 3)); /** extract SB_HUFF_DT */ flags.put(SB_HUFF_DT, new Integer((flagsAsInt >> 4) & 3)); /** extract SB_HUFF_RDW */ flags.put(SB_HUFF_RDW, new Integer((flagsAsInt >> 6) & 3)); /** extract SB_HUFF_RDH */ flags.put(SB_HUFF_RDH, new Integer((flagsAsInt >> 8) & 3)); /** extract SB_HUFF_RDX */ flags.put(SB_HUFF_RDX, new Integer((flagsAsInt >> 10) & 3)); /** extract SB_HUFF_RDY */ flags.put(SB_HUFF_RDY, new Integer((flagsAsInt >> 12) & 3)); /** extract SB_HUFF_RSIZE */ flags.put(SB_HUFF_RSIZE, new Integer((flagsAsInt >> 14) & 1)); if (JBIG2StreamDecoder.debug) System.out.println(flags); } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/region/text/TextRegionSegment.java0000644000175000017500000003161611213455610027767 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * TextRegionSegment.java * --------------- */ package org.jpedal.jbig2.segment.region.text; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.decoders.HuffmanDecoder; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.image.JBIG2Bitmap; import org.jpedal.jbig2.segment.Segment; import org.jpedal.jbig2.segment.pageinformation.PageInformationSegment; import org.jpedal.jbig2.segment.region.RegionFlags; import org.jpedal.jbig2.segment.region.RegionSegment; import org.jpedal.jbig2.segment.symboldictionary.SymbolDictionarySegment; import org.jpedal.jbig2.util.BinaryOperation; public class TextRegionSegment extends RegionSegment { private TextRegionFlags textRegionFlags = new TextRegionFlags(); private TextRegionHuffmanFlags textRegionHuffmanFlags = new TextRegionHuffmanFlags(); private int noOfSymbolInstances; private boolean inlineImage; private short[] symbolRegionAdaptiveTemplateX = new short[2], symbolRegionAdaptiveTemplateY = new short[2]; public TextRegionSegment(JBIG2StreamDecoder streamDecoder, boolean inlineImage) { super(streamDecoder); this.inlineImage = inlineImage; } public void readSegment() throws IOException, JBIG2Exception { if (JBIG2StreamDecoder.debug) System.out.println("==== Reading Text Region ===="); super.readSegment(); /** read text region Segment flags */ readTextRegionFlags(); short[] buff = new short[4]; decoder.readByte(buff); noOfSymbolInstances = BinaryOperation.getInt32(buff); if (JBIG2StreamDecoder.debug) System.out.println("noOfSymbolInstances = " + noOfSymbolInstances); int noOfReferredToSegments = segmentHeader.getReferredToSegmentCount(); int[] referredToSegments = segmentHeader.getReferredToSegments(); List codeTables = new ArrayList(); List segmentsReferenced = new ArrayList(); int noOfSymbols = 0; if (JBIG2StreamDecoder.debug) System.out.println("noOfReferredToSegments = " + noOfReferredToSegments); for (int i = 0; i < noOfReferredToSegments; i++) { Segment seg = decoder.findSegment(referredToSegments[i]); int type = seg.getSegmentHeader().getSegmentType(); if (type == Segment.SYMBOL_DICTIONARY) { segmentsReferenced.add(seg); noOfSymbols += ((SymbolDictionarySegment) seg).getNoOfExportedSymbols(); } else if (type == Segment.TABLES) { codeTables.add(seg); } } int symbolCodeLength = 0; int count = 1; while (count < noOfSymbols) { symbolCodeLength++; count <<= 1; } int currentSymbol = 0; JBIG2Bitmap[] symbols = new JBIG2Bitmap[noOfSymbols]; for (Iterator it = segmentsReferenced.iterator(); it.hasNext();) { Segment seg = (Segment) it.next(); if (seg.getSegmentHeader().getSegmentType() == Segment.SYMBOL_DICTIONARY) { JBIG2Bitmap[] bitmaps = ((SymbolDictionarySegment) seg).getBitmaps(); for (int j = 0; j < bitmaps.length; j++) { symbols[currentSymbol] = bitmaps[j]; currentSymbol++; } } } int[][] huffmanFSTable = null; int[][] huffmanDSTable = null; int[][] huffmanDTTable = null; int[][] huffmanRDWTable = null; int[][] huffmanRDHTable = null; int[][] huffmanRDXTable = null; int[][] huffmanRDYTable = null; int[][] huffmanRSizeTable = null; boolean sbHuffman = textRegionFlags.getFlagValue(TextRegionFlags.SB_HUFF) != 0; int i = 0; if (sbHuffman) { int sbHuffFS = textRegionHuffmanFlags.getFlagValue(TextRegionHuffmanFlags.SB_HUFF_FS); if (sbHuffFS == 0) { huffmanFSTable = HuffmanDecoder.huffmanTableF; } else if (sbHuffFS == 1) { huffmanFSTable = HuffmanDecoder.huffmanTableG; } else { } int sbHuffDS = textRegionHuffmanFlags.getFlagValue(TextRegionHuffmanFlags.SB_HUFF_DS); if (sbHuffDS == 0) { huffmanDSTable = HuffmanDecoder.huffmanTableH; } else if (sbHuffDS == 1) { huffmanDSTable = HuffmanDecoder.huffmanTableI; } else if (sbHuffDS == 2) { huffmanDSTable = HuffmanDecoder.huffmanTableJ; } else { } int sbHuffDT = textRegionHuffmanFlags.getFlagValue(TextRegionHuffmanFlags.SB_HUFF_DT); if (sbHuffDT == 0) { huffmanDTTable = HuffmanDecoder.huffmanTableK; } else if (sbHuffDT == 1) { huffmanDTTable = HuffmanDecoder.huffmanTableL; } else if (sbHuffDT == 2) { huffmanDTTable = HuffmanDecoder.huffmanTableM; } else { } int sbHuffRDW = textRegionHuffmanFlags.getFlagValue(TextRegionHuffmanFlags.SB_HUFF_RDW); if (sbHuffRDW == 0) { huffmanRDWTable = HuffmanDecoder.huffmanTableN; } else if (sbHuffRDW == 1) { huffmanRDWTable = HuffmanDecoder.huffmanTableO; } else { } int sbHuffRDH = textRegionHuffmanFlags.getFlagValue(TextRegionHuffmanFlags.SB_HUFF_RDH); if (sbHuffRDH == 0) { huffmanRDHTable = HuffmanDecoder.huffmanTableN; } else if (sbHuffRDH == 1) { huffmanRDHTable = HuffmanDecoder.huffmanTableO; } else { } int sbHuffRDX = textRegionHuffmanFlags.getFlagValue(TextRegionHuffmanFlags.SB_HUFF_RDX); if (sbHuffRDX == 0) { huffmanRDXTable = HuffmanDecoder.huffmanTableN; } else if (sbHuffRDX == 1) { huffmanRDXTable = HuffmanDecoder.huffmanTableO; } else { } int sbHuffRDY = textRegionHuffmanFlags.getFlagValue(TextRegionHuffmanFlags.SB_HUFF_RDY); if (sbHuffRDY == 0) { huffmanRDYTable = HuffmanDecoder.huffmanTableN; } else if (sbHuffRDY == 1) { huffmanRDYTable = HuffmanDecoder.huffmanTableO; } else { } int sbHuffRSize = textRegionHuffmanFlags.getFlagValue(TextRegionHuffmanFlags.SB_HUFF_RSIZE); if (sbHuffRSize == 0) { huffmanRSizeTable = HuffmanDecoder.huffmanTableA; } else { } } int[][] runLengthTable = new int[36][4]; int[][] symbolCodeTable = new int[noOfSymbols + 1][4]; if (sbHuffman) { decoder.consumeRemainingBits(); for (i = 0; i < 32; i++) { runLengthTable[i] = new int[] { i, decoder.readBits(4), 0, 0 }; } runLengthTable[32] = new int[] { 0x103, decoder.readBits(4), 2, 0 }; runLengthTable[33] = new int[] { 0x203, decoder.readBits(4), 3, 0 }; runLengthTable[34] = new int[] { 0x20b, decoder.readBits(4), 7, 0 }; runLengthTable[35] = new int[] { 0, 0, HuffmanDecoder.jbig2HuffmanEOT }; runLengthTable = huffmanDecoder.buildTable(runLengthTable, 35); for (i = 0; i < noOfSymbols; i++) { symbolCodeTable[i] = new int[] { i, 0, 0, 0 }; } i = 0; while (i < noOfSymbols) { int j = huffmanDecoder.decodeInt(runLengthTable).intResult(); if (j > 0x200) { for (j -= 0x200; j != 0 && i < noOfSymbols; j--) { symbolCodeTable[i++][1] = 0; } } else if (j > 0x100) { for (j -= 0x100; j != 0 && i < noOfSymbols; j--) { symbolCodeTable[i][1] = symbolCodeTable[i - 1][1]; i++; } } else { symbolCodeTable[i++][1] = j; } } symbolCodeTable[noOfSymbols][1] = 0; symbolCodeTable[noOfSymbols][2] = HuffmanDecoder.jbig2HuffmanEOT; symbolCodeTable = huffmanDecoder.buildTable(symbolCodeTable, noOfSymbols); decoder.consumeRemainingBits(); } else { symbolCodeTable = null; arithmeticDecoder.resetIntStats(symbolCodeLength); arithmeticDecoder.start(); } boolean symbolRefine = textRegionFlags.getFlagValue(TextRegionFlags.SB_REFINE) != 0; int logStrips = textRegionFlags.getFlagValue(TextRegionFlags.LOG_SB_STRIPES); int defaultPixel = textRegionFlags.getFlagValue(TextRegionFlags.SB_DEF_PIXEL); int combinationOperator = textRegionFlags.getFlagValue(TextRegionFlags.SB_COMB_OP); boolean transposed = textRegionFlags.getFlagValue(TextRegionFlags.TRANSPOSED) != 0; int referenceCorner = textRegionFlags.getFlagValue(TextRegionFlags.REF_CORNER); int sOffset = textRegionFlags.getFlagValue(TextRegionFlags.SB_DS_OFFSET); int template = textRegionFlags.getFlagValue(TextRegionFlags.SB_R_TEMPLATE); if (symbolRefine) { arithmeticDecoder.resetRefinementStats(template, null); } JBIG2Bitmap bitmap = new JBIG2Bitmap(regionBitmapWidth, regionBitmapHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder); bitmap.readTextRegion(sbHuffman, symbolRefine, noOfSymbolInstances, logStrips, noOfSymbols, symbolCodeTable, symbolCodeLength, symbols, defaultPixel, combinationOperator, transposed, referenceCorner, sOffset, huffmanFSTable, huffmanDSTable, huffmanDTTable, huffmanRDWTable, huffmanRDHTable, huffmanRDXTable, huffmanRDYTable, huffmanRSizeTable, template, symbolRegionAdaptiveTemplateX, symbolRegionAdaptiveTemplateY, decoder); if (inlineImage) { PageInformationSegment pageSegment = decoder.findPageSegement(segmentHeader.getPageAssociation()); JBIG2Bitmap pageBitmap = pageSegment.getPageBitmap(); if (JBIG2StreamDecoder.debug) System.out.println(pageBitmap + " " + bitmap); int externalCombinationOperator = regionFlags.getFlagValue(RegionFlags.EXTERNAL_COMBINATION_OPERATOR); pageBitmap.combine(bitmap, regionBitmapXLocation, regionBitmapYLocation, externalCombinationOperator); } else { bitmap.setBitmapNumber(getSegmentHeader().getSegmentNumber()); decoder.appendBitmap(bitmap); } decoder.consumeRemainingBits(); } private void readTextRegionFlags() throws IOException { /** extract text region Segment flags */ short[] textRegionFlagsField = new short[2]; decoder.readByte(textRegionFlagsField); int flags = BinaryOperation.getInt16(textRegionFlagsField); textRegionFlags.setFlags(flags); if (JBIG2StreamDecoder.debug) System.out.println("text region Segment flags = " + flags); boolean sbHuff = textRegionFlags.getFlagValue(TextRegionFlags.SB_HUFF) != 0; if (sbHuff) { /** extract text region Segment Huffman flags */ short[] textRegionHuffmanFlagsField = new short[2]; decoder.readByte(textRegionHuffmanFlagsField); flags = BinaryOperation.getInt16(textRegionHuffmanFlagsField); textRegionHuffmanFlags.setFlags(flags); if (JBIG2StreamDecoder.debug) System.out.println("text region segment Huffman flags = " + flags); } boolean sbRefine = textRegionFlags.getFlagValue(TextRegionFlags.SB_REFINE) != 0; int sbrTemplate = textRegionFlags.getFlagValue(TextRegionFlags.SB_R_TEMPLATE); if (sbRefine && sbrTemplate == 0) { symbolRegionAdaptiveTemplateX[0] = readATValue(); symbolRegionAdaptiveTemplateY[0] = readATValue(); symbolRegionAdaptiveTemplateX[1] = readATValue(); symbolRegionAdaptiveTemplateY[1] = readATValue(); } } public TextRegionFlags getTextRegionFlags() { return textRegionFlags; } public TextRegionHuffmanFlags getTextRegionHuffmanFlags() { return textRegionHuffmanFlags; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/region/RegionFlags.java0000644000175000017500000000545611213455610025573 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * RegionFlags.java * --------------- */ package org.jpedal.jbig2.segment.region; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.segment.Flags; public class RegionFlags extends Flags { public static String EXTERNAL_COMBINATION_OPERATOR = "EXTERNAL_COMBINATION_OPERATOR"; public void setFlags(int flagsAsInt) { this.flagsAsInt = flagsAsInt; /** extract EXTERNAL_COMBINATION_OPERATOR */ flags.put(EXTERNAL_COMBINATION_OPERATOR, new Integer(flagsAsInt & 7)); if (JBIG2StreamDecoder.debug) System.out.println(flags); } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/region/RegionSegment.java0000644000175000017500000000752011213455610026133 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * RegionSegment.java * --------------- */ package org.jpedal.jbig2.segment.region; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.segment.Segment; import org.jpedal.jbig2.util.BinaryOperation; import java.io.IOException; public abstract class RegionSegment extends Segment { protected int regionBitmapWidth, regionBitmapHeight; protected int regionBitmapXLocation, regionBitmapYLocation; protected RegionFlags regionFlags = new RegionFlags(); public RegionSegment(JBIG2StreamDecoder streamDecoder) { super(streamDecoder); } public void readSegment() throws IOException, JBIG2Exception { short[] buff = new short[4]; decoder.readByte(buff); regionBitmapWidth = BinaryOperation.getInt32(buff); buff = new short[4]; decoder.readByte(buff); regionBitmapHeight = BinaryOperation.getInt32(buff); if (JBIG2StreamDecoder.debug) System.out.println("Bitmap size = " + regionBitmapWidth + 'x' + regionBitmapHeight); buff = new short[4]; decoder.readByte(buff); regionBitmapXLocation = BinaryOperation.getInt32(buff); buff = new short[4]; decoder.readByte(buff); regionBitmapYLocation = BinaryOperation.getInt32(buff); if (JBIG2StreamDecoder.debug) System.out.println("Bitmap location = " + regionBitmapXLocation + ',' + regionBitmapYLocation); /** extract region Segment flags */ short regionFlagsField = decoder.readByte(); regionFlags.setFlags(regionFlagsField); if (JBIG2StreamDecoder.debug) System.out.println("region Segment flags = " + regionFlagsField); } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/stripes/0000755000175000017500000000000011213455610022724 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/segment/stripes/EndOfStripeSegment.java0000644000175000017500000000546211213455610027303 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * EndOfStripeSegment.java * --------------- */ package org.jpedal.jbig2.segment.stripes; import java.io.IOException; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.segment.Segment; public class EndOfStripeSegment extends Segment { public EndOfStripeSegment(JBIG2StreamDecoder streamDecoder) { super(streamDecoder); } public void readSegment() throws IOException, JBIG2Exception { for (int i = 0; i < this.getSegmentHeader().getSegmentDataLength(); i++) { decoder.readByte(); } } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/symboldictionary/0000755000175000017500000000000011213455610024626 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/segment/symboldictionary/SymbolDictionaryFlags.java0000644000175000017500000001015111213455610031737 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * SymbolDictionaryFlags.java * --------------- */ package org.jpedal.jbig2.segment.symboldictionary; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.segment.Flags; public class SymbolDictionaryFlags extends Flags { public static String SD_HUFF = "SD_HUFF"; public static String SD_REF_AGG = "SD_REF_AGG"; public static String SD_HUFF_DH = "SD_HUFF_DH"; public static String SD_HUFF_DW = "SD_HUFF_DW"; public static String SD_HUFF_BM_SIZE = "SD_HUFF_BM_SIZE"; public static String SD_HUFF_AGG_INST = "SD_HUFF_AGG_INST"; public static String BITMAP_CC_USED = "BITMAP_CC_USED"; public static String BITMAP_CC_RETAINED = "BITMAP_CC_RETAINED"; public static String SD_TEMPLATE = "SD_TEMPLATE"; public static String SD_R_TEMPLATE = "SD_R_TEMPLATE"; public void setFlags(int flagsAsInt) { this.flagsAsInt = flagsAsInt; /** extract SD_HUFF */ flags.put(SD_HUFF, new Integer(flagsAsInt & 1)); /** extract SD_REF_AGG */ flags.put(SD_REF_AGG, new Integer((flagsAsInt >> 1) & 1)); /** extract SD_HUFF_DH */ flags.put(SD_HUFF_DH, new Integer((flagsAsInt >> 2) & 3)); /** extract SD_HUFF_DW */ flags.put(SD_HUFF_DW, new Integer((flagsAsInt >> 4) & 3)); /** extract SD_HUFF_BM_SIZE */ flags.put(SD_HUFF_BM_SIZE, new Integer((flagsAsInt >> 6) & 1)); /** extract SD_HUFF_AGG_INST */ flags.put(SD_HUFF_AGG_INST, new Integer((flagsAsInt >> 7) & 1)); /** extract BITMAP_CC_USED */ flags.put(BITMAP_CC_USED, new Integer((flagsAsInt >> 8) & 1)); /** extract BITMAP_CC_RETAINED */ flags.put(BITMAP_CC_RETAINED, new Integer((flagsAsInt >> 9) & 1)); /** extract SD_TEMPLATE */ flags.put(SD_TEMPLATE, new Integer((flagsAsInt >> 10) & 3)); /** extract SD_R_TEMPLATE */ flags.put(SD_R_TEMPLATE, new Integer((flagsAsInt >> 12) & 1)); if (JBIG2StreamDecoder.debug) System.out.println(flags); } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/symboldictionary/SymbolDictionarySegment.java0000644000175000017500000004362511213455610032321 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * SymbolDictionarySegment.java * --------------- */ package org.jpedal.jbig2.segment.symboldictionary; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.decoders.ArithmeticDecoderStats; import org.jpedal.jbig2.decoders.DecodeIntResult; import org.jpedal.jbig2.decoders.HuffmanDecoder; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.image.JBIG2Bitmap; import org.jpedal.jbig2.segment.Segment; import org.jpedal.jbig2.segment.tables.JBIG2CodeTable; import org.jpedal.jbig2.util.BinaryOperation; public class SymbolDictionarySegment extends Segment { private int noOfExportedSymbols; private int noOfNewSymbols; short[] symbolDictionaryAdaptiveTemplateX = new short[4], symbolDictionaryAdaptiveTemplateY = new short[4]; short[] symbolDictionaryRAdaptiveTemplateX = new short[2], symbolDictionaryRAdaptiveTemplateY = new short[2]; private JBIG2Bitmap[] bitmaps; private SymbolDictionaryFlags symbolDictionaryFlags = new SymbolDictionaryFlags(); private ArithmeticDecoderStats genericRegionStats; private ArithmeticDecoderStats refinementRegionStats; public SymbolDictionarySegment(JBIG2StreamDecoder streamDecoder) { super(streamDecoder); } public void readSegment() throws IOException, JBIG2Exception { if (JBIG2StreamDecoder.debug) System.out.println("==== Read Segment Symbol Dictionary ===="); /** read symbol dictionary flags */ readSymbolDictionaryFlags(); List codeTables = new ArrayList(); int numberOfInputSymbols = 0; int noOfReferredToSegments = segmentHeader.getReferredToSegmentCount(); int[] referredToSegments = segmentHeader.getReferredToSegments(); for (int i = 0; i < noOfReferredToSegments; i++) { Segment seg = decoder.findSegment(referredToSegments[i]); int type = seg.getSegmentHeader().getSegmentType(); if (type == Segment.SYMBOL_DICTIONARY) { numberOfInputSymbols += ((SymbolDictionarySegment) seg).noOfExportedSymbols; } else if (type == Segment.TABLES) { codeTables.add(seg); } } int symbolCodeLength = 0; int i = 1; while (i < numberOfInputSymbols + noOfNewSymbols) { symbolCodeLength++; i <<= 1; } JBIG2Bitmap[] bitmaps = new JBIG2Bitmap[numberOfInputSymbols + noOfNewSymbols]; int k = 0; SymbolDictionarySegment inputSymbolDictionary = null; for (i = 0; i < noOfReferredToSegments; i++) { Segment seg = decoder.findSegment(referredToSegments[i]); if (seg.getSegmentHeader().getSegmentType() == Segment.SYMBOL_DICTIONARY) { inputSymbolDictionary = (SymbolDictionarySegment) seg; for (int j = 0; j < inputSymbolDictionary.noOfExportedSymbols; j++) { bitmaps[k++] = inputSymbolDictionary.bitmaps[j]; } } } int[][] huffmanDHTable = null; int[][] huffmanDWTable = null; int[][] huffmanBMSizeTable = null; int[][] huffmanAggInstTable = null; boolean sdHuffman = symbolDictionaryFlags.getFlagValue(SymbolDictionaryFlags.SD_HUFF) != 0; int sdHuffmanDifferenceHeight = symbolDictionaryFlags.getFlagValue(SymbolDictionaryFlags.SD_HUFF_DH); int sdHuffmanDiferrenceWidth = symbolDictionaryFlags.getFlagValue(SymbolDictionaryFlags.SD_HUFF_DW); int sdHuffBitmapSize = symbolDictionaryFlags.getFlagValue(SymbolDictionaryFlags.SD_HUFF_BM_SIZE); int sdHuffAggregationInstances = symbolDictionaryFlags.getFlagValue(SymbolDictionaryFlags.SD_HUFF_AGG_INST); i = 0; if (sdHuffman) { if (sdHuffmanDifferenceHeight == 0) { huffmanDHTable = HuffmanDecoder.huffmanTableD; } else if (sdHuffmanDifferenceHeight == 1) { huffmanDHTable = HuffmanDecoder.huffmanTableE; } else { huffmanDHTable = ((JBIG2CodeTable) codeTables.get(i++)).getHuffTable(); } if (sdHuffmanDiferrenceWidth == 0) { huffmanDWTable = HuffmanDecoder.huffmanTableB; } else if (sdHuffmanDiferrenceWidth == 1) { huffmanDWTable = HuffmanDecoder.huffmanTableC; } else { huffmanDWTable = ((JBIG2CodeTable) codeTables.get(i++)).getHuffTable(); } if (sdHuffBitmapSize == 0) { huffmanBMSizeTable = HuffmanDecoder.huffmanTableA; } else { huffmanBMSizeTable = ((JBIG2CodeTable) codeTables.get(i++)).getHuffTable(); } if (sdHuffAggregationInstances == 0) { huffmanAggInstTable = HuffmanDecoder.huffmanTableA; } else { huffmanAggInstTable = ((JBIG2CodeTable) codeTables.get(i++)).getHuffTable(); } } int contextUsed = symbolDictionaryFlags.getFlagValue(SymbolDictionaryFlags.BITMAP_CC_USED); int sdTemplate = symbolDictionaryFlags.getFlagValue(SymbolDictionaryFlags.SD_TEMPLATE); if (!sdHuffman) { if (contextUsed != 0 && inputSymbolDictionary != null) { arithmeticDecoder.resetGenericStats(sdTemplate, inputSymbolDictionary.genericRegionStats); } else { arithmeticDecoder.resetGenericStats(sdTemplate, null); } arithmeticDecoder.resetIntStats(symbolCodeLength); arithmeticDecoder.start(); } int sdRefinementAggregate = symbolDictionaryFlags.getFlagValue(SymbolDictionaryFlags.SD_REF_AGG); int sdRefinementTemplate = symbolDictionaryFlags.getFlagValue(SymbolDictionaryFlags.SD_R_TEMPLATE); if (sdRefinementAggregate != 0) { if (contextUsed != 0 && inputSymbolDictionary != null) { arithmeticDecoder.resetRefinementStats(sdRefinementTemplate, inputSymbolDictionary.refinementRegionStats); } else { arithmeticDecoder.resetRefinementStats(sdRefinementTemplate, null); } } int deltaWidths[] = new int[noOfNewSymbols]; int deltaHeight = 0; i = 0; while (i < noOfNewSymbols) { int instanceDeltaHeight = 0; if (sdHuffman) { instanceDeltaHeight = huffmanDecoder.decodeInt(huffmanDHTable).intResult(); } else { instanceDeltaHeight = arithmeticDecoder.decodeInt(arithmeticDecoder.iadhStats).intResult(); } if (instanceDeltaHeight < 0 && -instanceDeltaHeight >= deltaHeight) { if(JBIG2StreamDecoder.debug) System.out.println("Bad delta-height value in JBIG2 symbol dictionary"); } deltaHeight += instanceDeltaHeight; int symbolWidth = 0; int totalWidth = 0; int j = i; while (true) { int deltaWidth = 0; DecodeIntResult decodeIntResult; if (sdHuffman) { decodeIntResult = huffmanDecoder.decodeInt(huffmanDWTable); } else { decodeIntResult = arithmeticDecoder.decodeInt(arithmeticDecoder.iadwStats); } if (!decodeIntResult.booleanResult()) break; deltaWidth = decodeIntResult.intResult(); if (deltaWidth < 0 && -deltaWidth >= symbolWidth) { if(JBIG2StreamDecoder.debug) System.out.println("Bad delta-width value in JBIG2 symbol dictionary"); } symbolWidth += deltaWidth; if (sdHuffman && sdRefinementAggregate == 0) { deltaWidths[i] = symbolWidth; totalWidth += symbolWidth; } else if (sdRefinementAggregate == 1) { int refAggNum = 0; if (sdHuffman) { refAggNum = huffmanDecoder.decodeInt(huffmanAggInstTable).intResult(); } else { refAggNum = arithmeticDecoder.decodeInt(arithmeticDecoder.iaaiStats).intResult(); } if (refAggNum == 1) { int symbolID = 0, referenceDX = 0, referenceDY = 0; if (sdHuffman) { symbolID = decoder.readBits(symbolCodeLength); referenceDX = huffmanDecoder.decodeInt(HuffmanDecoder.huffmanTableO).intResult(); referenceDY = huffmanDecoder.decodeInt(HuffmanDecoder.huffmanTableO).intResult(); decoder.consumeRemainingBits(); arithmeticDecoder.start(); } else { symbolID = (int) arithmeticDecoder.decodeIAID(symbolCodeLength, arithmeticDecoder.iaidStats); referenceDX = arithmeticDecoder.decodeInt(arithmeticDecoder.iardxStats).intResult(); referenceDY = arithmeticDecoder.decodeInt(arithmeticDecoder.iardyStats).intResult(); } JBIG2Bitmap referredToBitmap = bitmaps[symbolID]; JBIG2Bitmap bitmap = new JBIG2Bitmap(symbolWidth, deltaHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder); bitmap.readGenericRefinementRegion(sdRefinementTemplate, false, referredToBitmap, referenceDX, referenceDY, symbolDictionaryRAdaptiveTemplateX, symbolDictionaryRAdaptiveTemplateY); bitmaps[numberOfInputSymbols + i] = bitmap; } else { JBIG2Bitmap bitmap = new JBIG2Bitmap(symbolWidth, deltaHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder); bitmap.readTextRegion(sdHuffman, true, refAggNum, 0, numberOfInputSymbols + i, null, symbolCodeLength, bitmaps, 0, 0, false, 1, 0, HuffmanDecoder.huffmanTableF, HuffmanDecoder.huffmanTableH, HuffmanDecoder.huffmanTableK, HuffmanDecoder.huffmanTableO, HuffmanDecoder.huffmanTableO, HuffmanDecoder.huffmanTableO, HuffmanDecoder.huffmanTableO, HuffmanDecoder.huffmanTableA, sdRefinementTemplate, symbolDictionaryRAdaptiveTemplateX, symbolDictionaryRAdaptiveTemplateY, decoder); bitmaps[numberOfInputSymbols + i] = bitmap; } } else { JBIG2Bitmap bitmap = new JBIG2Bitmap(symbolWidth, deltaHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder); bitmap.readBitmap(false, sdTemplate, false, false, null, symbolDictionaryAdaptiveTemplateX, symbolDictionaryAdaptiveTemplateY, 0); bitmaps[numberOfInputSymbols + i] = bitmap; } i++; } if (sdHuffman && sdRefinementAggregate == 0) { int bmSize = huffmanDecoder.decodeInt(huffmanBMSizeTable).intResult(); decoder.consumeRemainingBits(); JBIG2Bitmap collectiveBitmap = new JBIG2Bitmap(totalWidth, deltaHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder); if (bmSize == 0) { int padding = totalWidth % 8; int bytesPerRow = (int) Math.ceil(totalWidth / 8d); //short[] bitmap = new short[totalWidth]; //decoder.readByte(bitmap); int size = deltaHeight * ((totalWidth + 7) >> 3); short[] bitmap = new short[size]; decoder.readByte(bitmap); short[][] logicalMap = new short[deltaHeight][bytesPerRow]; int count = 0; for (int row = 0; row < deltaHeight; row++) { for (int col = 0; col < bytesPerRow; col++) { logicalMap[row][col] = bitmap[count]; count++; } } int collectiveBitmapRow = 0, collectiveBitmapCol = 0; for (int row = 0; row < deltaHeight; row++) { for (int col = 0; col < bytesPerRow; col++) { if (col == (bytesPerRow - 1)) { // this is the last // byte in the row short currentByte = logicalMap[row][col]; for (int bitPointer = 7; bitPointer >= padding; bitPointer--) { short mask = (short) (1 << bitPointer); int bit = (currentByte & mask) >> bitPointer; collectiveBitmap.setPixel(collectiveBitmapCol, collectiveBitmapRow, bit); collectiveBitmapCol++; } collectiveBitmapRow++; collectiveBitmapCol = 0; } else { short currentByte = logicalMap[row][col]; for (int bitPointer = 7; bitPointer >= 0; bitPointer--) { short mask = (short) (1 << bitPointer); int bit = (currentByte & mask) >> bitPointer; collectiveBitmap.setPixel(collectiveBitmapCol, collectiveBitmapRow, bit); collectiveBitmapCol++; } } } } } else { collectiveBitmap.readBitmap(true, 0, false, false, null, null, null, bmSize); } int x = 0; while (j < i){ bitmaps[numberOfInputSymbols + j] = collectiveBitmap.getSlice(x, 0, deltaWidths[j], deltaHeight); x += deltaWidths[j]; j++; } } } this.bitmaps = new JBIG2Bitmap[noOfExportedSymbols]; int j = i = 0; boolean export = false; while (i < numberOfInputSymbols + noOfNewSymbols) { int run = 0; if (sdHuffman) { run = huffmanDecoder.decodeInt(HuffmanDecoder.huffmanTableA).intResult(); } else { run = arithmeticDecoder.decodeInt(arithmeticDecoder.iaexStats).intResult(); } if (export) { for (int cnt = 0; cnt < run; cnt++) { this.bitmaps[j++] = bitmaps[i++]; } } else { i += run; } export = !export; } int contextRetained = symbolDictionaryFlags.getFlagValue(SymbolDictionaryFlags.BITMAP_CC_RETAINED); if (!sdHuffman && contextRetained == 1) { genericRegionStats = genericRegionStats.copy(); if (sdRefinementAggregate == 1) { refinementRegionStats = refinementRegionStats.copy(); } } /** consume any remaining bits */ decoder.consumeRemainingBits(); } private void readSymbolDictionaryFlags() throws IOException { /** extract symbol dictionary flags */ short[] symbolDictionaryFlagsField = new short[2]; decoder.readByte(symbolDictionaryFlagsField); int flags = BinaryOperation.getInt16(symbolDictionaryFlagsField); symbolDictionaryFlags.setFlags(flags); if (JBIG2StreamDecoder.debug) System.out.println("symbolDictionaryFlags = " + flags); // symbol dictionary AT flags int sdHuff = symbolDictionaryFlags.getFlagValue(SymbolDictionaryFlags.SD_HUFF); int sdTemplate = symbolDictionaryFlags.getFlagValue(SymbolDictionaryFlags.SD_TEMPLATE); if (sdHuff == 0) { if (sdTemplate == 0) { symbolDictionaryAdaptiveTemplateX[0] = readATValue(); symbolDictionaryAdaptiveTemplateY[0] = readATValue(); symbolDictionaryAdaptiveTemplateX[1] = readATValue(); symbolDictionaryAdaptiveTemplateY[1] = readATValue(); symbolDictionaryAdaptiveTemplateX[2] = readATValue(); symbolDictionaryAdaptiveTemplateY[2] = readATValue(); symbolDictionaryAdaptiveTemplateX[3] = readATValue(); symbolDictionaryAdaptiveTemplateY[3] = readATValue(); } else { symbolDictionaryAdaptiveTemplateX[0] = readATValue(); symbolDictionaryAdaptiveTemplateY[0] = readATValue(); } } // symbol dictionary refinement AT flags int refAgg = symbolDictionaryFlags.getFlagValue(SymbolDictionaryFlags.SD_REF_AGG); int sdrTemplate = symbolDictionaryFlags.getFlagValue(SymbolDictionaryFlags.SD_R_TEMPLATE); if (refAgg != 0 && sdrTemplate == 0) { symbolDictionaryRAdaptiveTemplateX[0] = readATValue(); symbolDictionaryRAdaptiveTemplateY[0] = readATValue(); symbolDictionaryRAdaptiveTemplateX[1] = readATValue(); symbolDictionaryRAdaptiveTemplateY[1] = readATValue(); } /** extract no of exported symbols */ short[] noOfExportedSymbolsField = new short[4]; decoder.readByte(noOfExportedSymbolsField); int noOfExportedSymbols = BinaryOperation.getInt32(noOfExportedSymbolsField); this.noOfExportedSymbols = noOfExportedSymbols; if (JBIG2StreamDecoder.debug) System.out.println("noOfExportedSymbols = " + noOfExportedSymbols); /** extract no of new symbols */ short[] noOfNewSymbolsField = new short[4]; decoder.readByte(noOfNewSymbolsField); int noOfNewSymbols = BinaryOperation.getInt32(noOfNewSymbolsField); this.noOfNewSymbols = noOfNewSymbols; if (JBIG2StreamDecoder.debug) System.out.println("noOfNewSymbols = " + noOfNewSymbols); } public int getNoOfExportedSymbols() { return noOfExportedSymbols; } public void setNoOfExportedSymbols(int noOfExportedSymbols) { this.noOfExportedSymbols = noOfExportedSymbols; } public int getNoOfNewSymbols() { return noOfNewSymbols; } public void setNoOfNewSymbols(int noOfNewSymbols) { this.noOfNewSymbols = noOfNewSymbols; } public JBIG2Bitmap[] getBitmaps() { return bitmaps; } public SymbolDictionaryFlags getSymbolDictionaryFlags() { return symbolDictionaryFlags; } public void setSymbolDictionaryFlags(SymbolDictionaryFlags symbolDictionaryFlags) { this.symbolDictionaryFlags = symbolDictionaryFlags; } private ArithmeticDecoderStats getGenericRegionStats() { return genericRegionStats; } private void setGenericRegionStats(ArithmeticDecoderStats genericRegionStats) { this.genericRegionStats = genericRegionStats; } private void setRefinementRegionStats(ArithmeticDecoderStats refinementRegionStats) { this.refinementRegionStats = refinementRegionStats; } private ArithmeticDecoderStats getRefinementRegionStats() { return refinementRegionStats; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/tables/0000755000175000017500000000000011213455610022505 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/segment/tables/JBIG2CodeTable.java0000644000175000017500000000454411213455610025717 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * JBIG2CodeTable.java * --------------- */ package org.jpedal.jbig2.segment.tables; public class JBIG2CodeTable { public static int[][] getHuffTable() { return null; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/Flags.java0000644000175000017500000000517711213455610023144 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * Flags.java * --------------- */ package org.jpedal.jbig2.segment; import java.util.LinkedHashMap; import java.util.Map; public abstract class Flags { protected int flagsAsInt; protected Map flags = new LinkedHashMap(); public int getFlagValue(String key) { Integer value = (Integer) flags.get(key); return value.intValue(); } public abstract void setFlags(int flagsAsInt); } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/Segment.java0000644000175000017500000001136311213455610023504 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * Segment.java * --------------- */ package org.jpedal.jbig2.segment; import java.io.IOException; import org.jpedal.jbig2.JBIG2Exception; import org.jpedal.jbig2.decoders.ArithmeticDecoder; import org.jpedal.jbig2.decoders.HuffmanDecoder; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.decoders.MMRDecoder; public abstract class Segment { public static final int SYMBOL_DICTIONARY = 0; public static final int INTERMEDIATE_TEXT_REGION = 4; public static final int IMMEDIATE_TEXT_REGION = 6; public static final int IMMEDIATE_LOSSLESS_TEXT_REGION = 7; public static final int PATTERN_DICTIONARY = 16; public static final int INTERMEDIATE_HALFTONE_REGION = 20; public static final int IMMEDIATE_HALFTONE_REGION = 22; public static final int IMMEDIATE_LOSSLESS_HALFTONE_REGION = 23; public static final int INTERMEDIATE_GENERIC_REGION = 36; public static final int IMMEDIATE_GENERIC_REGION = 38; public static final int IMMEDIATE_LOSSLESS_GENERIC_REGION = 39; public static final int INTERMEDIATE_GENERIC_REFINEMENT_REGION = 40; public static final int IMMEDIATE_GENERIC_REFINEMENT_REGION = 42; public static final int IMMEDIATE_LOSSLESS_GENERIC_REFINEMENT_REGION = 43; public static final int PAGE_INFORMATION = 48; public static final int END_OF_PAGE = 49; public static final int END_OF_STRIPE = 50; public static final int END_OF_FILE = 51; public static final int PROFILES = 52; public static final int TABLES = 53; public static final int EXTENSION = 62; public static final int BITMAP = 70; protected SegmentHeader segmentHeader; protected HuffmanDecoder huffmanDecoder; protected ArithmeticDecoder arithmeticDecoder; protected MMRDecoder mmrDecoder; protected JBIG2StreamDecoder decoder; public Segment(JBIG2StreamDecoder streamDecoder) { this.decoder = streamDecoder; // try { //huffDecoder = HuffmanDecoder.getInstance(); // arithmeticDecoder = ArithmeticDecoder.getInstance(); huffmanDecoder = decoder.getHuffmanDecoder(); arithmeticDecoder = decoder.getArithmeticDecoder(); mmrDecoder = decoder.getMMRDecoder(); // } catch (JBIG2Exception e) { // e.printStackTrace(); // } } protected short readATValue() throws IOException { short atValue; short c0 = atValue = decoder.readByte(); if ((c0 & 0x80) != 0) { atValue |= -1 - 0xff; } return atValue; } public SegmentHeader getSegmentHeader() { return segmentHeader; } public void setSegmentHeader(SegmentHeader segmentHeader) { this.segmentHeader = segmentHeader; } public abstract void readSegment() throws IOException, JBIG2Exception; } libjpedal-jbig2-20100117/org/jpedal/jbig2/segment/SegmentHeader.java0000644000175000017500000001110611213455610024610 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * SegmentHeader.java * --------------- */ package org.jpedal.jbig2.segment; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; public class SegmentHeader { private int segmentNumber; private int segmentType; private boolean pageAssociationSizeSet; private boolean deferredNonRetainSet; private int referredToSegmentCount; private short[] rententionFlags; private int[] referredToSegments; private int pageAssociation; private int dataLength; public void setSegmentNumber(int SegmentNumber) { this.segmentNumber = SegmentNumber; } public void setSegmentHeaderFlags(short SegmentHeaderFlags) { segmentType = SegmentHeaderFlags & 63; // 63 = 00111111 pageAssociationSizeSet = (SegmentHeaderFlags & 64) == 64; // 64 = // 01000000 deferredNonRetainSet = (SegmentHeaderFlags & 80) == 80; // 64 = 10000000 if (JBIG2StreamDecoder.debug) { System.out.println("SegmentType = " + segmentType); System.out.println("pageAssociationSizeSet = " + pageAssociationSizeSet); System.out.println("deferredNonRetainSet = " + deferredNonRetainSet); } } public void setReferredToSegmentCount(int referredToSegmentCount) { this.referredToSegmentCount = referredToSegmentCount; } public void setRententionFlags(short[] rententionFlags) { this.rententionFlags = rententionFlags; } public void setReferredToSegments(int[] referredToSegments) { this.referredToSegments = referredToSegments; } public int[] getReferredToSegments() { return referredToSegments; } public int getSegmentType() { return segmentType; } public int getSegmentNumber() { return segmentNumber; } public boolean isPageAssociationSizeSet() { return pageAssociationSizeSet; } public boolean isDeferredNonRetainSet() { return deferredNonRetainSet; } public int getReferredToSegmentCount() { return referredToSegmentCount; } public short[] getRententionFlags() { return rententionFlags; } public int getPageAssociation() { return pageAssociation; } public void setPageAssociation(int pageAssociation) { this.pageAssociation = pageAssociation; } public void setDataLength(int dataLength) { this.dataLength = dataLength; } public void setSegmentType(int type) { this.segmentType = type; } public int getSegmentDataLength() { return dataLength; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/util/0000755000175000017500000000000011213455610020546 5ustar moellermoellerlibjpedal-jbig2-20100117/org/jpedal/jbig2/util/BinaryOperation.java0000644000175000017500000000637011213455610024524 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * BinaryOperation.java * --------------- */ package org.jpedal.jbig2.util; public class BinaryOperation { public static final int LEFT_SHIFT = 0; public static final int RIGHT_SHIFT = 1; public static int getInt32(short[] number) { return (number[0] << 24) | (number[1] << 16) | (number[2] << 8) | number[3]; } public static int getInt16(short[] number) { return (number[0] << 8) | number[1]; } public static long bit32Shift(long number, int shift, int direction) { if (direction == LEFT_SHIFT) number <<= shift; else number >>= shift; long mask = 0xffffffffl; // 1111 1111 1111 1111 1111 1111 1111 1111 return (number & mask); } public static int bit8Shift(int number, int shift, int direction) { if (direction == LEFT_SHIFT) number <<= shift; else number >>= shift; int mask = 0xff; // 1111 1111 return (number & mask); } public static int getInt32(byte[] number) { return (number[0] << 24) | (number[1] << 16) | (number[2] << 8) | number[3]; } } libjpedal-jbig2-20100117/org/jpedal/jbig2/JBIG2Decoder.java0000644000175000017500000001566611213455610022535 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * JBIG2Decoder.java * --------------- */ package org.jpedal.jbig2; import java.awt.image.BufferedImage; import java.awt.image.DataBuffer; import java.awt.image.DataBufferByte; import java.awt.image.Raster; import java.awt.image.WritableRaster; import java.io.DataInput; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import java.util.List; import org.jpedal.jbig2.decoders.JBIG2StreamDecoder; import org.jpedal.jbig2.image.JBIG2Bitmap; import org.jpedal.jbig2.segment.Segment; import org.jpedal.jbig2.segment.pageinformation.PageInformationSegment; public class JBIG2Decoder { private JBIG2StreamDecoder streamDecoder; /** * Constructor */ public JBIG2Decoder() { streamDecoder = new JBIG2StreamDecoder(); } /** * If the data stream is taken from a PDF, there may be some global data. Pass any global data * in here. Call this method before decodeJBIG2(...) * @param data global data * @throws IOException * @throws JBIG2Exception */ public void setGlobalData(byte[] data) throws IOException, JBIG2Exception { streamDecoder.setGlobalData(data); } /** * Decodes a JBIG2 image from a File object * @param file File to decode * @throws IOException * @throws JBIG2Exception */ public void decodeJBIG2(File file) throws IOException, JBIG2Exception { decodeJBIG2(file.getAbsolutePath()); } /** * Decodes a JBIG2 image from a String path * @param file Must be the full path to the image * @throws IOException * @throws JBIG2Exception */ public void decodeJBIG2(String file) throws IOException, JBIG2Exception { decodeJBIG2(new FileInputStream(file)); } /** * Decodes a JBIG2 image from an InputStream * @param inputStream InputStream * @throws IOException * @throws JBIG2Exception */ public void decodeJBIG2(InputStream inputStream) throws IOException, JBIG2Exception { int availiable = inputStream.available(); byte[] bytes = new byte[availiable]; inputStream.read(bytes); decodeJBIG2(bytes); } /** * Decodes a JBIG2 image from a DataInput * @param dataInput DataInput * @throws IOException * @throws JBIG2Exception */ public void decodeJBIG2(DataInput dataInput) throws IOException, JBIG2Exception { // long availiable = inputStream.length(); // // byte[] bytes = new byte[availiable]; // inputStream.read(bytes); // // decodeJBIG2(bytes); } /** * Decodes a JBIG2 image from a byte array * @param data the raw data stream * @throws IOException * @throws JBIG2Exception */ public void decodeJBIG2(byte[] data) throws IOException, JBIG2Exception { streamDecoder.decodeJBIG2(data); } /** * * @param page * @return */ public BufferedImage getPageAsBufferedImage(int page) { page++; JBIG2Bitmap pageBitmap = streamDecoder.findPageSegement(page).getPageBitmap(); byte[] bytes = pageBitmap.getData(true); if (bytes == null) return null; // make a a DEEP copy so we cant alter int len = bytes.length; byte[] copy = new byte[len]; System.arraycopy(bytes, 0, copy, 0, len); // byte[] data = pageBitmap.getData(true).clone(); int width = pageBitmap.getWidth(); int height = pageBitmap.getHeight(); /** create an image from the raw data */ DataBuffer db = new DataBufferByte(copy, copy.length); WritableRaster raster = Raster.createPackedRaster(db, width, height, 1, null); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY); image.setData(raster); return image; } public boolean isNumberOfPagesKnown() { return streamDecoder.isNumberOfPagesKnown(); } public int getNumberOfPages() { int pages = streamDecoder.getNumberOfPages(); if (streamDecoder.isNumberOfPagesKnown() && pages != 0) return pages; int noOfPages = 0; List segments = getAllSegments(); for (Iterator it = segments.iterator(); it.hasNext();) { Segment segment = (Segment) it.next(); if (segment.getSegmentHeader().getSegmentType() == Segment.PAGE_INFORMATION) noOfPages++; } return noOfPages; } public List getAllSegments() { return streamDecoder.getAllSegments(); } public PageInformationSegment findPageSegement(int page) { page++; return streamDecoder.findPageSegement(page); } public Segment findSegment(int segmentNumber) { return streamDecoder.findSegment(segmentNumber); } public JBIG2Bitmap getPageAsJBIG2Bitmap(int page) { page++; return streamDecoder.findPageSegement(page).getPageBitmap(); } public boolean isRandomAccessOrganisationUsed() { return streamDecoder.isRandomAccessOrganisationUsed(); } } libjpedal-jbig2-20100117/org/jpedal/jbig2/JBIG2Exception.java0000644000175000017500000000536311213455610023117 0ustar moellermoeller/** * =========================================== * Java Pdf Extraction Decoding Access Library * =========================================== * * Project Info: http://www.jpedal.org * (C) Copyright 1997-2008, IDRsolutions and Contributors. * Main Developer: Simon Barnett * * This file is part of JPedal * * Copyright (c) 2008, IDRsolutions * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the IDRsolutions nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY IDRsolutions ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL IDRsolutions BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * Other JBIG2 image decoding implementations include * jbig2dec (http://jbig2dec.sourceforge.net/) * xpdf (http://www.foolabs.com/xpdf/) * * The final draft JBIG2 specification can be found at http://www.jpeg.org/public/fcd14492.pdf * * All three of the above resources were used in the writing of this software, with methodologies, * processes and inspiration taken from all three. * * --------------- * JBIG2Exception.java * --------------- */ package org.jpedal.jbig2; public class JBIG2Exception extends Exception { public JBIG2Exception(Exception ex) { super(ex); } /** * Constructs a JBIGException whithout a message. */ public JBIG2Exception() { super(); } /** * Constructs a JBIGException with a message. * * @param message * a message describing the exception */ public JBIG2Exception(String message) { super(message); } }