Type4ShadingContext.java (pdfbox-2.0.23-src) | : | Type4ShadingContext.java (pdfbox-2.0.24-src) | ||
---|---|---|---|---|
skipping to change at line 21 | skipping to change at line 21 | |||
* Unless required by applicable law or agreed to in writing, software | * Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | * distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | * See the License for the specific language governing permissions and | |||
* limitations under the License. | * limitations under the License. | |||
*/ | */ | |||
package org.apache.pdfbox.pdmodel.graphics.shading; | package org.apache.pdfbox.pdmodel.graphics.shading; | |||
import java.awt.Rectangle; | import java.awt.Rectangle; | |||
import java.awt.geom.AffineTransform; | import java.awt.geom.AffineTransform; | |||
import java.awt.geom.Point2D; | ||||
import java.awt.image.ColorModel; | import java.awt.image.ColorModel; | |||
import java.io.EOFException; | ||||
import java.io.IOException; | import java.io.IOException; | |||
import java.util.ArrayList; | ||||
import java.util.Collections; | ||||
import java.util.List; | ||||
import javax.imageio.stream.ImageInputStream; | ||||
import javax.imageio.stream.MemoryCacheImageInputStream; | ||||
import org.apache.commons.logging.Log; | import org.apache.commons.logging.Log; | |||
import org.apache.commons.logging.LogFactory; | import org.apache.commons.logging.LogFactory; | |||
import org.apache.pdfbox.cos.COSDictionary; | ||||
import org.apache.pdfbox.cos.COSStream; | ||||
import org.apache.pdfbox.pdmodel.common.PDRange; | ||||
import org.apache.pdfbox.util.Matrix; | import org.apache.pdfbox.util.Matrix; | |||
/** | /** | |||
* AWT PaintContext for Gouraud Triangle Mesh (Type 4) shading. | * AWT PaintContext for Gouraud Triangle Mesh (Type 4) shading. | |||
* | * | |||
* @author Tilman Hausherr | * @author Tilman Hausherr | |||
* @author Shaola Ren | * @author Shaola Ren | |||
*/ | */ | |||
class Type4ShadingContext extends GouraudShadingContext | class Type4ShadingContext extends GouraudShadingContext | |||
{ | { | |||
skipping to change at line 65 | skipping to change at line 56 | |||
*/ | */ | |||
Type4ShadingContext(PDShadingType4 shading, ColorModel cm, AffineTransform x form, | Type4ShadingContext(PDShadingType4 shading, ColorModel cm, AffineTransform x form, | |||
Matrix matrix, Rectangle deviceBounds) throws IOE xception | Matrix matrix, Rectangle deviceBounds) throws IOE xception | |||
{ | { | |||
super(shading, cm, xform, matrix); | super(shading, cm, xform, matrix); | |||
LOG.debug("Type4ShadingContext"); | LOG.debug("Type4ShadingContext"); | |||
bitsPerFlag = shading.getBitsPerFlag(); | bitsPerFlag = shading.getBitsPerFlag(); | |||
//TODO handle cases where bitperflag isn't 8 | //TODO handle cases where bitperflag isn't 8 | |||
LOG.debug("bitsPerFlag: " + bitsPerFlag); | LOG.debug("bitsPerFlag: " + bitsPerFlag); | |||
setTriangleList(collectTriangles(shading, xform, matrix)); | setTriangleList(shading.collectTriangles(xform, matrix)); | |||
createPixelTable(deviceBounds); | createPixelTable(deviceBounds); | |||
} | } | |||
private List<ShadedTriangle> collectTriangles(PDShadingType4 freeTriangleSha | ||||
dingType, AffineTransform xform, Matrix matrix) | ||||
throws IOException | ||||
{ | ||||
COSDictionary dict = freeTriangleShadingType.getCOSObject(); | ||||
if (!(dict instanceof COSStream)) | ||||
{ | ||||
return Collections.emptyList(); | ||||
} | ||||
PDRange rangeX = freeTriangleShadingType.getDecodeForParameter(0); | ||||
PDRange rangeY = freeTriangleShadingType.getDecodeForParameter(1); | ||||
if (Float.compare(rangeX.getMin(), rangeX.getMax()) == 0 || | ||||
Float.compare(rangeY.getMin(), rangeY.getMax()) == 0) | ||||
{ | ||||
return Collections.emptyList(); | ||||
} | ||||
PDRange[] colRange = new PDRange[numberOfColorComponents]; | ||||
for (int i = 0; i < numberOfColorComponents; ++i) | ||||
{ | ||||
colRange[i] = freeTriangleShadingType.getDecodeForParameter(2 + i); | ||||
} | ||||
List<ShadedTriangle> list = new ArrayList<ShadedTriangle>(); | ||||
long maxSrcCoord = (long) Math.pow(2, bitsPerCoordinate) - 1; | ||||
long maxSrcColor = (long) Math.pow(2, bitsPerColorComponent) - 1; | ||||
COSStream stream = (COSStream) dict; | ||||
ImageInputStream mciis = new MemoryCacheImageInputStream(stream.createIn | ||||
putStream()); | ||||
try | ||||
{ | ||||
byte flag = (byte) 0; | ||||
try | ||||
{ | ||||
flag = (byte) (mciis.readBits(bitsPerFlag) & 3); | ||||
} | ||||
catch (EOFException ex) | ||||
{ | ||||
LOG.error(ex); | ||||
} | ||||
boolean eof = false; | ||||
while (!eof) | ||||
{ | ||||
Vertex p0, p1, p2; | ||||
Point2D[] ps; | ||||
float[][] cs; | ||||
int lastIndex; | ||||
try | ||||
{ | ||||
switch (flag) | ||||
{ | ||||
case 0: | ||||
p0 = readVertex(mciis, maxSrcCoord, maxSrcColor, ran | ||||
geX, rangeY, colRange, | ||||
matrix, xform); | ||||
flag = (byte) (mciis.readBits(bitsPerFlag) & 3); | ||||
if (flag != 0) | ||||
{ | ||||
LOG.error("bad triangle: " + flag); | ||||
} | ||||
p1 = readVertex(mciis, maxSrcCoord, maxSrcColor, ran | ||||
geX, rangeY, colRange, | ||||
matrix, xform); | ||||
mciis.readBits(bitsPerFlag); | ||||
if (flag != 0) | ||||
{ | ||||
LOG.error("bad triangle: " + flag); | ||||
} | ||||
p2 = readVertex(mciis, maxSrcCoord, maxSrcColor, ran | ||||
geX, rangeY, colRange, | ||||
matrix, xform); | ||||
ps = new Point2D[] { p0.point, p1.point, p2.point }; | ||||
cs = new float[][] { p0.color, p1.color, p2.color }; | ||||
list.add(new ShadedTriangle(ps, cs)); | ||||
flag = (byte) (mciis.readBits(bitsPerFlag) & 3); | ||||
break; | ||||
case 1: | ||||
case 2: | ||||
lastIndex = list.size() - 1; | ||||
if (lastIndex < 0) | ||||
{ | ||||
LOG.error("broken data stream: " + list.size()); | ||||
} | ||||
else | ||||
{ | ||||
ShadedTriangle preTri = list.get(lastIndex); | ||||
p2 = readVertex(mciis, maxSrcCoord, maxSrcColor, | ||||
rangeX, rangeY, | ||||
colRange, matrix, xform); | ||||
ps = new Point2D[] { flag == 1 ? preTri.corner[1 | ||||
] : preTri.corner[0], | ||||
preTri.corner[2], | ||||
p2.point }; | ||||
cs = new float[][] { flag == 1 ? preTri.color[1] | ||||
: preTri.color[0], | ||||
preTri.color[2], | ||||
p2.color }; | ||||
list.add(new ShadedTriangle(ps, cs)); | ||||
flag = (byte) (mciis.readBits(bitsPerFlag) & 3); | ||||
} | ||||
break; | ||||
default: | ||||
LOG.warn("bad flag: " + flag); | ||||
break; | ||||
} | ||||
} | ||||
catch (EOFException ex) | ||||
{ | ||||
eof = true; | ||||
} | ||||
} | ||||
} | ||||
finally | ||||
{ | ||||
mciis.close(); | ||||
} | ||||
return list; | ||||
} | ||||
} | } | |||
End of changes. 6 change blocks. | ||||
130 lines changed or deleted | 2 lines changed or added |