001: /**
002: * Copyright (c) 2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.graphics.predictor;
031:
032: /**
033: *
034: *
035: * In an Uptimum encoded image, each line takes up width*bpp+1 bytes. The first
036: * byte holds a number that signifies which algorithm encoded the line.
037: *
038: * @author xylifyx@yahoo.co.uk
039: * @version $Revision: 1.3 $
040: */
041: public class Uptimum extends PredictorAlgorithm {
042: /**
043: * {@inheritDoc}
044: */
045: public void checkBufsiz(byte[] filtered, byte[] raw) {
046: if (filtered.length != (getWidth() * getBpp() + 1)
047: * getHeight()) {
048:
049: throw new IllegalArgumentException(
050: "filtered.length != (width*bpp + 1) * height, "
051: + filtered.length + " "
052: + (getWidth() * getBpp() + 1) * getHeight()
053: + "w,h,bpp=" + getWidth() + ","
054: + getHeight() + "," + getBpp());
055: }
056: if (raw.length != getWidth() * getHeight() * getBpp()) {
057: throw new IllegalArgumentException(
058: "raw.length != width * height * bpp, raw.length="
059: + raw.length + " w,h,bpp=" + getWidth()
060: + "," + getHeight() + "," + getBpp());
061: }
062: }
063:
064: /**
065: * {@inheritDoc}
066: */
067: public void encodeLine(byte[] src, byte[] dest, int srcDy,
068: int srcOffset, int destDy, int destOffset) {
069: throw new UnsupportedOperationException("encodeLine");
070: }
071:
072: /**
073: * {@inheritDoc}
074: */
075: public void decodeLine(byte[] src, byte[] dest, int srcDy,
076: int srcOffset, int destDy, int destOffset) {
077: throw new UnsupportedOperationException("decodeLine");
078: }
079:
080: /**
081: * {@inheritDoc}
082: */
083: public void encode(byte[] src, byte[] dest) {
084: checkBufsiz(dest, src);
085: throw new UnsupportedOperationException("encode");
086: }
087:
088: /**
089: * Filter indexed by byte code.
090: */
091: PredictorAlgorithm[] filter = { new None(), new Sub(), new Up(),
092: new Average(), new Paeth() };
093:
094: /**
095: * {@inheritDoc}
096: */
097: public void setBpp(int bpp) {
098: super .setBpp(bpp);
099: for (int i = 0; i < filter.length; i++) {
100: filter[i].setBpp(bpp);
101: }
102: }
103:
104: /**
105: * {@inheritDoc}
106: */
107: public void setHeight(int height) {
108: super .setHeight(height);
109: for (int i = 0; i < filter.length; i++) {
110: filter[i].setHeight(height);
111: }
112: }
113:
114: /**
115: * {@inheritDoc}
116: */
117: public void setWidth(int width) {
118: super .setWidth(width);
119: for (int i = 0; i < filter.length; i++) {
120: filter[i].setWidth(width);
121: }
122: }
123:
124: /**
125: * {@inheritDoc}
126: */
127: public void decode(byte[] src, byte[] dest) {
128: checkBufsiz(src, dest);
129: int bpl = getWidth() * getBpp();
130: int srcDy = bpl + 1;
131: for (int y = 0; y < getHeight(); y++) {
132: PredictorAlgorithm f = filter[src[y * srcDy]];
133: int srcOffset = y * srcDy + 1;
134: f.decodeLine(src, dest, srcDy, srcOffset, bpl, y * bpl);
135: }
136: }
137: }
|