You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2017/05/04 18:49:55 UTC

svn commit: r1793849 - /pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateGradientShadingPDF.java

Author: tilman
Date: Thu May  4 18:49:55 2017
New Revision: 1793849

URL: http://svn.apache.org/viewvc?rev=1793849&view=rev
Log:
PDFBOX-3778: add sample code with type 4 shading creation; reduce dpi of rendered image so that it is faster

Modified:
    pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateGradientShadingPDF.java

Modified: pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateGradientShadingPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateGradientShadingPDF.java?rev=1793849&r1=1793848&r2=1793849&view=diff
==============================================================================
--- pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateGradientShadingPDF.java (original)
+++ pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateGradientShadingPDF.java Thu May  4 18:49:55 2017
@@ -18,12 +18,15 @@ package org.apache.pdfbox.examples.pdmod
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
+import java.io.OutputStream;
 import javax.imageio.ImageIO;
+import javax.imageio.stream.MemoryCacheImageOutputStream;
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSFloat;
 import org.apache.pdfbox.cos.COSInteger;
 import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSStream;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDPage;
 import org.apache.pdfbox.pdmodel.common.function.PDFunctionType2;
@@ -33,11 +36,12 @@ import org.apache.pdfbox.pdmodel.graphic
 import org.apache.pdfbox.pdmodel.graphics.shading.PDShading;
 import org.apache.pdfbox.pdmodel.graphics.shading.PDShadingType2;
 import org.apache.pdfbox.pdmodel.graphics.shading.PDShadingType3;
+import org.apache.pdfbox.pdmodel.graphics.shading.PDShadingType4;
 import org.apache.pdfbox.rendering.PDFRenderer;
 
 /**
- * This example creates a PDF with type 2 (axial) and 3 (radial) shadings with a
- * type 2 (exponential) function.
+ * This example creates a PDF with type 2 (axial) and type 3 (radial) shadings with a type 2
+ * (exponential) function, and a type 4 (gouraud triangle shading) without function.
  *
  * @author Tilman Hausherr
  */
@@ -107,11 +111,85 @@ public class CreateGradientShadingPDF
             radialShading.setCoords(coords2);
             radialShading.setFunction(func);
 
+            // Gouraud shading
+            // See PDF 32000 specification,
+            // 8.7.4.5.5 Type 4 Shadings (Free-Form Gouraud-Shaded Triangle Meshes)
+            PDShadingType4 gouraudShading = new PDShadingType4(new COSStream());
+            gouraudShading.setShadingType(PDShading.SHADING_TYPE4);
+            // we use multiple of 8, so that no padding is needed
+            gouraudShading.setBitsPerFlag(8);
+            gouraudShading.setBitsPerCoordinate(16);
+            gouraudShading.setBitsPerComponent(8);
+            COSArray decodeArray = new COSArray();
+            // coordinates x y map 16 bits 0..FFFF to 0..FFFF to make your life easy
+            // so no calculation is needed, but you can only use integer coordinates
+            // for real numbers, you'll need smaller bounds, e.g. 0xFFFF / 0xA = 0x1999
+            // would allow 1 point decimal result coordinate.
+            // See in PDF specification: 8.9.5.2 Decode Arrays
+            decodeArray.add(COSInteger.ZERO);
+            decodeArray.add(COSInteger.get(0xFFFF));
+            decodeArray.add(COSInteger.ZERO);
+            decodeArray.add(COSInteger.get(0xFFFF));
+            // colors r g b map 8 bits from 0..FF to 0..1
+            decodeArray.add(COSInteger.ZERO);
+            decodeArray.add(COSInteger.ONE);
+            decodeArray.add(COSInteger.ZERO);
+            decodeArray.add(COSInteger.ONE);
+            decodeArray.add(COSInteger.ZERO);
+            decodeArray.add(COSInteger.ONE);
+            gouraudShading.setDecodeValues(decodeArray);
+            gouraudShading.setColorSpace(PDDeviceRGB.INSTANCE);
+            
+            // Function is not required for type 4 shadings and not really useful, 
+            // because if a function would be used, each edge "color" of a triangle would be one value, 
+            // which would then transformed into n color components by the function so it is 
+            // difficult to get 3 "extremes".
+            
+            // fill the vertex stream
+            OutputStream os = ((COSStream) gouraudShading.getCOSObject()).createOutputStream();
+            MemoryCacheImageOutputStream mcos = new MemoryCacheImageOutputStream(os);
+
+            // Vertex 1, starts with flag1
+            // (flags always 0 for vertices of start triangle)
+            mcos.writeByte(0);
+            // x1 y1 (left corner)
+            mcos.writeShort(0);
+            mcos.writeShort(0);
+            // r1 g1 b1 (red)
+            mcos.writeByte(0xFF);
+            mcos.writeByte(0);
+            mcos.writeByte(0);
+
+            // Vertex 2, starts with flag2
+            mcos.writeByte(0);
+            // x2 y2 (top corner)
+            mcos.writeShort(100);
+            mcos.writeShort(100);
+            // r2 g2 b2 (green)
+            mcos.writeByte(0);
+            mcos.writeByte(0xFF);
+            mcos.writeByte(0);
+
+            // Vertex 3, starts with flag3
+            mcos.writeByte(0);
+            // x3 y3 (right corner)
+            mcos.writeShort(200);
+            mcos.writeShort(0);
+            // r3 g3 b3 (blue)
+            mcos.writeByte(0);
+            mcos.writeByte(0);
+            mcos.writeByte(0xFF);
+
+            mcos.close();
+            // outside stream MUST be closed as well, see javadoc of MemoryCacheImageOutputStream
+            os.close();
+
             // invoke shading from content stream
             // compress parameter is set to false so that you can see the stream in a text editor
             PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false);
             contentStream.shadingFill(axialShading);
             contentStream.shadingFill(radialShading);
+            contentStream.shadingFill(gouraudShading);
             contentStream.close();
             
             document.save(file);
@@ -119,7 +197,7 @@ public class CreateGradientShadingPDF
             
             // render the PDF and save it into a PNG file
             document = PDDocument.load(new File(file));
-            BufferedImage bim = new PDFRenderer(document).renderImageWithDPI(0, 300);
+            BufferedImage bim = new PDFRenderer(document).renderImageWithDPI(0, 100);
             ImageIO.write(bim, "png", new File(file + ".png"));
             document.close();
         }