You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2013/08/20 20:06:41 UTC
svn commit: r1515905 - in
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics:
color/PDIndexed.java xobject/PDCcitt.java
Author: lehmi
Date: Tue Aug 20 18:06:40 2013
New Revision: 1515905
URL: http://svn.apache.org/r1515905
Log:
PDFBOX-1638: fixed the usage of an indexed colorspace for a CCITT encoded XObjectImage
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexed.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDCcitt.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexed.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexed.java?rev=1515905&r1=1515904&r2=1515905&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexed.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexed.java Tue Aug 20 18:06:40 2013
@@ -16,6 +16,14 @@
*/
package org.apache.pdfbox.pdmodel.graphics.color;
+import java.awt.color.ColorSpace;
+import java.awt.image.ColorModel;
+import java.awt.image.DataBuffer;
+import java.awt.image.IndexColorModel;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.cos.COSInteger;
@@ -24,20 +32,11 @@ import org.apache.pdfbox.cos.COSNumber;
import org.apache.pdfbox.cos.COSStream;
import org.apache.pdfbox.cos.COSString;
-import java.awt.color.ColorSpace;
-import java.awt.image.ColorModel;
-import java.awt.image.DataBuffer;
-import java.awt.image.IndexColorModel;
-
-import java.io.InputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-
/**
* This class represents an Indexed color space.
- *
+ *
* @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
- * @version $Revision: 1.4 $
+ *
*/
public class PDIndexed extends PDColorSpace
{
@@ -56,7 +55,7 @@ public class PDIndexed extends PDColorSp
private PDColorSpace baseColorspace = null;
private ColorModel baseColorModel = null;
-
+
/**
* The lookup data as byte array.
*/
@@ -65,35 +64,40 @@ public class PDIndexed extends PDColorSp
private byte[] indexedColorValues;
private int indexNumOfComponents;
private int maxIndex;
-
+
+ /**
+ * Indexed color values are always 8bit based.
+ */
+ private static final int INDEXED_BPC = 8;
+
/**
* Constructor, default DeviceRGB, hival 255.
*/
public PDIndexed()
{
array = new COSArray();
- array.add( COSName.INDEXED );
- array.add( COSName.DEVICERGB );
- array.add( COSInteger.get( 255 ) );
- array.add( org.apache.pdfbox.cos.COSNull.NULL );
+ array.add(COSName.INDEXED);
+ array.add(COSName.DEVICERGB);
+ array.add(COSInteger.get(255));
+ array.add(org.apache.pdfbox.cos.COSNull.NULL);
}
/**
* Constructor.
- *
+ *
* @param indexedArray The array containing the indexed parameters
*/
- public PDIndexed( COSArray indexedArray )
+ public PDIndexed(COSArray indexedArray)
{
array = indexedArray;
}
/**
- * This will return the number of color components. This will return the
- * number of color components in the base color.
- *
+ * This will return the number of color components. This will return the number of color components in the base
+ * color.
+ *
* @return The number of components in this color space.
- *
+ *
* @throws IOException If there is an error getting the number of color components.
*/
public int getNumberOfComponents() throws IOException
@@ -103,7 +107,7 @@ public class PDIndexed extends PDColorSp
/**
* This will return the name of the color space.
- *
+ *
* @return The name of the color space.
*/
public String getName()
@@ -113,9 +117,9 @@ public class PDIndexed extends PDColorSp
/**
* Create a Java colorspace for this colorspace.
- *
+ *
* @return A color space that can be used for Java AWT operations.
- *
+ *
* @throws IOException If there is an error creating the color space.
*/
protected ColorSpace createColorSpace() throws IOException
@@ -125,107 +129,106 @@ public class PDIndexed extends PDColorSp
/**
* Create a Java color model for this colorspace.
- *
+ *
* @param bpc The number of bits per component.
- *
+ *
* @return A color model that can be used for Java AWT operations.
- *
+ *
* @throws IOException If there is an error creating the color model.
*/
- public ColorModel createColorModel( int bpc ) throws IOException
+ public ColorModel createColorModel(int bpc) throws IOException
{
return createColorModel(bpc, -1);
}
/**
* Create a Java color model for this colorspace including the given mask value.
- *
- * @param bpc The number of bits per component.
+ *
+ * @param bpc The number of bits per component of the indexed color model.
* @param mask the mask value, -1 indicates no mask
- *
+ *
* @return A color model that can be used for Java AWT operations.
- *
+ *
* @throws IOException If there is an error creating the color model.
*/
- public ColorModel createColorModel( int bpc, int mask ) throws IOException
+ public ColorModel createColorModel(int bpc, int mask) throws IOException
{
- ColorModel colorModel = getBaseColorModel(bpc);
+ ColorModel colorModel = getBaseColorModel(INDEXED_BPC);
calculateIndexedColorValues(colorModel, bpc);
if (mask > -1)
{
- return new IndexColorModel(bpc, maxIndex+1, indexedColorValues, 0, colorModel.hasAlpha(), mask);
+ return new IndexColorModel(bpc, maxIndex + 1, indexedColorValues, 0, colorModel.hasAlpha(), mask);
}
else
{
- return new IndexColorModel(bpc, maxIndex+1, indexedColorValues, 0, colorModel.hasAlpha());
+ return new IndexColorModel(bpc, maxIndex + 1, indexedColorValues, 0, colorModel.hasAlpha());
}
}
/**
* This will get the color space that acts as the index for this color space.
- *
+ *
* @return The base color space.
- *
+ *
* @throws IOException If there is error creating the base color space.
*/
public PDColorSpace getBaseColorSpace() throws IOException
{
if (baseColorspace == null)
{
- COSBase base = array.getObject( 1 );
- baseColorspace = PDColorSpaceFactory.createColorSpace( base );
+ COSBase base = array.getObject(1);
+ baseColorspace = PDColorSpaceFactory.createColorSpace(base);
}
return baseColorspace;
}
/**
* This will set the base color space.
- *
+ *
* @param base The base color space to use as the index.
*/
- public void setBaseColorSpace( PDColorSpace base )
+ public void setBaseColorSpace(PDColorSpace base)
{
- array.set( 1, base.getCOSObject() );
+ array.set(1, base.getCOSObject());
baseColorspace = base;
}
/**
* Get the highest value for the lookup.
- *
+ *
* @return The hival entry.
*/
public int getHighValue()
{
- return ((COSNumber)array.getObject( 2 )).intValue();
+ return ((COSNumber) array.getObject(2)).intValue();
}
/**
- * This will set the highest value that is allowed. This cannot be higher
- * than 255.
- *
+ * This will set the highest value that is allowed. This cannot be higher than 255.
+ *
* @param high The highest value for the lookup table.
*/
- public void setHighValue( int high )
+ public void setHighValue(int high)
{
- array.set( 2, high );
+ array.set(2, high);
}
/**
* This will perform a lookup into the color lookup table.
- *
+ *
* @param lookupIndex The zero-based index into the table, should not exceed the high value.
* @param componentNumber The component number, probably 1,2,3,3.
- *
+ *
* @return The value that was from the lookup table.
- *
+ *
* @throws IOException If there is an error looking up the color.
*/
- public int lookupColor( int lookupIndex, int componentNumber ) throws IOException
+ public int lookupColor(int lookupIndex, int componentNumber) throws IOException
{
PDColorSpace baseColor = getBaseColorSpace();
byte[] data = getLookupData();
int numberOfComponents = baseColor.getNumberOfComponents();
- return (data[lookupIndex*numberOfComponents + componentNumber]+256)%256;
+ return (data[lookupIndex * numberOfComponents + componentNumber] + 256) % 256;
}
/**
@@ -236,35 +239,35 @@ public class PDIndexed extends PDColorSp
*/
public byte[] getLookupData() throws IOException
{
- if ( lookupData == null)
+ if (lookupData == null)
{
- COSBase lookupTable = array.getObject( 3 );
- if( lookupTable instanceof COSString )
+ COSBase lookupTable = array.getObject(3);
+ if (lookupTable instanceof COSString)
{
- lookupData = ((COSString)lookupTable).getBytes();
+ lookupData = ((COSString) lookupTable).getBytes();
}
- else if( lookupTable instanceof COSStream )
+ else if (lookupTable instanceof COSStream)
{
- //Data will be small so just load the whole thing into memory for
- //easier processing
- COSStream lookupStream = (COSStream)lookupTable;
+ // Data will be small so just load the whole thing into memory for
+ // easier processing
+ COSStream lookupStream = (COSStream) lookupTable;
InputStream input = lookupStream.getUnfilteredStream();
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
- byte[] buffer = new byte[ 1024 ];
+ byte[] buffer = new byte[1024];
int amountRead;
- while( (amountRead = input.read(buffer, 0, buffer.length)) != -1 )
+ while ((amountRead = input.read(buffer, 0, buffer.length)) != -1)
{
- output.write( buffer, 0, amountRead );
+ output.write(buffer, 0, amountRead);
}
lookupData = output.toByteArray();
}
- else if( lookupTable == null )
+ else if (lookupTable == null)
{
lookupData = new byte[0];
}
else
{
- throw new IOException( "Error: Unknown type for lookup table " + lookupTable );
+ throw new IOException("Error: Unknown type for lookup table " + lookupTable);
}
}
return lookupData;
@@ -272,25 +275,26 @@ public class PDIndexed extends PDColorSp
/**
* This will set a color in the color lookup table.
- *
+ *
* @param lookupIndex The zero-based index into the table, should not exceed the high value.
* @param componentNumber The component number, probably 1,2,3,3.
* @param color The color that will go into the table.
- *
+ *
* @throws IOException If there is an error looking up the color.
*/
- public void setLookupColor( int lookupIndex, int componentNumber, int color ) throws IOException
+ public void setLookupColor(int lookupIndex, int componentNumber, int color) throws IOException
{
PDColorSpace baseColor = getBaseColorSpace();
int numberOfComponents = baseColor.getNumberOfComponents();
byte[] data = getLookupData();
- data[lookupIndex*numberOfComponents + componentNumber] = (byte)color;
- COSString string = new COSString( data );
- array.set( 3, string );
+ data[lookupIndex * numberOfComponents + componentNumber] = (byte) color;
+ COSString string = new COSString(data);
+ array.set(3, string);
}
/**
* Returns the components of the color for the given index.
+ *
* @param index the index of the color value
* @return COSArray with the color components
* @throws IOException If the tint function is not supported
@@ -298,33 +302,33 @@ public class PDIndexed extends PDColorSp
public float[] calculateColorValues(int index) throws IOException
{
// TODO bpc != 8 ??
- calculateIndexedColorValues(getBaseColorModel(8), 8);
+ calculateIndexedColorValues(getBaseColorModel(INDEXED_BPC), 8);
float[] colorValues = null;
if (index < maxIndex)
{
int bufferIndex = index * indexNumOfComponents;
colorValues = new float[indexNumOfComponents];
- for (int i=0; i < indexNumOfComponents; i++)
+ for (int i = 0; i < indexNumOfComponents; i++)
{
- colorValues[i] = (float)indexedColorValues[bufferIndex+i];
+ colorValues[i] = (float) indexedColorValues[bufferIndex + i];
}
}
return colorValues;
}
-
+
private ColorModel getBaseColorModel(int bpc) throws IOException
{
if (baseColorModel == null)
{
baseColorModel = getBaseColorSpace().createColorModel(bpc);
- if( baseColorModel.getTransferType() != DataBuffer.TYPE_BYTE )
+ if (baseColorModel.getTransferType() != DataBuffer.TYPE_BYTE)
{
- throw new IOException( "Not implemented" );
+ throw new IOException("Not implemented");
}
}
return baseColorModel;
}
-
+
private void calculateIndexedColorValues(ColorModel colorModel, int bpc) throws IOException
{
if (indexedColorValues == null)
@@ -335,28 +339,28 @@ public class PDIndexed extends PDColorSp
int highValue = getHighValue();
// choose the correct size, sometimes there are more indexed values than needed
// and sometimes there are fewer indexed value than possible
- maxIndex = Math.min(numberOfColorValues-1, highValue);
+ maxIndex = Math.min(numberOfColorValues - 1, highValue);
byte[] index = getLookupData();
// despite all definitions there may be less values within the lookup data
- int numberOfColorValuesFromIndex = (index.length / baseColorModel.getNumComponents())-1;
+ int numberOfColorValuesFromIndex = (index.length / baseColorModel.getNumComponents()) - 1;
maxIndex = Math.min(maxIndex, numberOfColorValuesFromIndex);
// does the colorspace have an alpha channel?
boolean hasAlpha = baseColorModel.hasAlpha();
- indexNumOfComponents = 3 + ( hasAlpha ? 1 : 0);
- int buffersize = (maxIndex+1) * indexNumOfComponents;
+ indexNumOfComponents = 3 + (hasAlpha ? 1 : 0);
+ int buffersize = (maxIndex + 1) * indexNumOfComponents;
indexedColorValues = new byte[buffersize];
byte[] inData = new byte[baseColorModel.getNumComponents()];
int bufferIndex = 0;
- for( int i = 0; i <= maxIndex; i++ )
+ for (int i = 0; i <= maxIndex; i++)
{
System.arraycopy(index, i * inData.length, inData, 0, inData.length);
// calculate RGB values
- indexedColorValues[bufferIndex] = (byte)colorModel.getRed(inData);
- indexedColorValues[bufferIndex+1] = (byte)colorModel.getGreen(inData);
- indexedColorValues[bufferIndex+2] = (byte)colorModel.getBlue(inData);
- if(hasAlpha)
+ indexedColorValues[bufferIndex] = (byte) colorModel.getRed(inData);
+ indexedColorValues[bufferIndex + 1] = (byte) colorModel.getGreen(inData);
+ indexedColorValues[bufferIndex + 2] = (byte) colorModel.getBlue(inData);
+ if (hasAlpha)
{
- indexedColorValues[bufferIndex+3] = (byte)colorModel.getAlpha(inData);
+ indexedColorValues[bufferIndex + 3] = (byte) colorModel.getAlpha(inData);
}
bufferIndex += indexNumOfComponents;
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDCcitt.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDCcitt.java?rev=1515905&r1=1515904&r2=1515905&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDCcitt.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/xobject/PDCcitt.java Tue Aug 20 18:06:40 2013
@@ -149,7 +149,6 @@ public class PDCcitt extends PDXObjectIm
rows = Math.max(rows, height);
}
boolean blackIsOne = decodeParms.getBoolean(COSName.BLACK_IS_1, false);
-
byte[] bufferData = null;
ColorModel colorModel = null;
PDColorSpace colorspace = getColorSpace();
@@ -161,16 +160,16 @@ public class PDCcitt extends PDXObjectIm
COSBase maskArray = getMask();
if (maskArray != null && maskArray instanceof COSArray)
{
- colorModel = csIndexed.createColorModel(8, ((COSArray) maskArray).getInt(0));
+ colorModel = csIndexed.createColorModel(1, ((COSArray) maskArray).getInt(0));
}
else
{
- colorModel = csIndexed.createColorModel(8);
+ colorModel = csIndexed.createColorModel(1);
}
}
else
{
- byte[] map = new byte[] { (byte) 0x00, (byte) 0xff };
+ byte[] map = new byte[] { (byte) 0x00, (byte) 0xFF };
colorModel = new IndexColorModel(1, map.length, map, map, map, Transparency.OPAQUE);
}
WritableRaster raster = colorModel.createCompatibleWritableRaster(cols, rows);