Skip to content

Commit

Permalink
Change representation of AlternativeColorSpace.
Browse files Browse the repository at this point in the history
Add ICC profile to CMYKColorSpace and use it for color conversion. This
makes the conversion to rgb a lot better but also slows it down.
  • Loading branch information
Borisvl committed Apr 18, 2012
1 parent acd5df4 commit f9d2af9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/com/sun/pdfview/PDFImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,8 @@ private ColorModel getColorModel() {
0, true);
}
} else if (cs instanceof AlternateColorSpace){
ColorSpace altCS = new AltColorSpace(((AlternateColorSpace) cs).getFunktion(), cs.getColorSpace());
//ColorSpace altCS = new AltColorSpace(((AlternateColorSpace) cs).getFunktion(), cs.getColorSpace());
ColorSpace altCS = cs.getColorSpace();
int[] bits = new int[altCS.getNumComponents()];
for (int i = 0; i <
bits.length; i++) {
Expand Down
8 changes: 4 additions & 4 deletions src/com/sun/pdfview/colorspace/AltColorSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public float[] fromRGB(float[] p_rgbvalue) {
*/
@Override
public float[] toCIEXYZ(float[] p_colorvalue) {
p_colorvalue = this.fkt.calculate(p_colorvalue);
return this.origCs.toCIEXYZ(p_colorvalue);
float[] colorvalue = this.fkt.calculate(p_colorvalue);
return this.origCs.toCIEXYZ(colorvalue);
}

/**
Expand All @@ -67,7 +67,7 @@ public float[] toCIEXYZ(float[] p_colorvalue) {
*/
@Override
public float[] toRGB(float[] p_colorvalue) {
p_colorvalue = this.fkt.calculate(p_colorvalue);
return this.origCs.toRGB(p_colorvalue);
float[] colorvalue = this.fkt.calculate(p_colorvalue);
return this.origCs.toRGB(colorvalue);
}
}
7 changes: 6 additions & 1 deletion src/com/sun/pdfview/colorspace/AlternateColorSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class AlternateColorSpace extends PDFColorSpace {
/** The function */
private PDFFunction function;

private AltColorSpace altcolorspace;

/** Creates a new instance of AlternateColorSpace */
public AlternateColorSpace(PDFColorSpace alternate, PDFFunction function) {
super(null);
Expand Down Expand Up @@ -76,7 +78,9 @@ public AlternateColorSpace(PDFColorSpace alternate, PDFFunction function) {
* get the original Java ColorSpace.
*/
@Override public ColorSpace getColorSpace() {
return this.alternate.getColorSpace();
if (altcolorspace == null) altcolorspace = new AltColorSpace(function, alternate.getColorSpace());
return altcolorspace;
//return this.alternate.getColorSpace();
}

/*************************************************************************
Expand All @@ -87,4 +91,5 @@ public PDFFunction getFunktion() {
return this.function;
}


}
29 changes: 28 additions & 1 deletion src/com/sun/pdfview/colorspace/CMYKColorSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
package com.sun.pdfview.colorspace;

import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import java.awt.color.ICC_Profile;
import java.io.IOException;

/**
* A ColorSpace for the CMYK color space.
Expand All @@ -31,11 +34,21 @@
*/
public class CMYKColorSpace extends ColorSpace {

private ICC_Profile icc;
private ICC_ColorSpace icc_cs;

/**
* Create a new CMYKColorSpace Instance.
*/
public CMYKColorSpace() {
super(ColorSpace.TYPE_CMYK, 4);
try {
icc = ICC_Profile.getInstance(getClass().getResourceAsStream("/ch/randelshofer/media/jpeg/Generic_CMYK_Profile.icc"));
icc_cs = new ICC_ColorSpace(icc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
Expand All @@ -48,6 +61,9 @@ public CMYKColorSpace() {
*/
@Override
public float[] fromCIEXYZ(float[] p_colorvalue) {
if (icc_cs != null) {
return icc_cs.fromCIEXYZ(p_colorvalue);
}
ColorSpace l_cs = ColorSpace.getInstance(ColorSpace.TYPE_RGB);
float[] l_rgb = l_cs.toCIEXYZ(p_colorvalue);
return fromRGB(l_rgb);
Expand All @@ -69,6 +85,10 @@ public float[] fromCIEXYZ(float[] p_colorvalue) {
*/
@Override
public float[] fromRGB(float[] p_rgbvalue) {
if (icc_cs != null) {
return icc_cs.fromRGB(p_rgbvalue);
}

/* TODO: Maybe we should do a better job to determine when black should
* be used and pulled out? -- At this time, it's not necessary for our
* (Scantegrity's) uses.
Expand Down Expand Up @@ -107,6 +127,10 @@ public float[] fromRGB(float[] p_rgbvalue) {
*/
@Override
public float[] toCIEXYZ(float[] p_colorvalue) {
if (icc_cs != null) {
return icc_cs.toCIEXYZ(p_colorvalue);
}

float[] l_rgb = toRGB(p_colorvalue);
ColorSpace l_cs = ColorSpace.getInstance(ColorSpace.TYPE_RGB);
return l_cs.toCIEXYZ(l_rgb);
Expand All @@ -126,6 +150,9 @@ public float[] toCIEXYZ(float[] p_colorvalue) {
*/
@Override
public float[] toRGB(float[] p_colorvalue) {
if (icc_cs != null) {
return icc_cs.toRGB(p_colorvalue);
}
float[] l_res = {0,0,0};
if (p_colorvalue.length >= 4)
{
Expand All @@ -144,7 +171,7 @@ public float[] toRGB(float[] p_colorvalue) {
* @return p_colors, with any values greater than 1 set to 1, and less than
* 0 set to 0.
*/
public float[] normalize(float[] p_colors) {
private float[] normalize(float[] p_colors) {
for (int l_i = 0; l_i < p_colors.length; l_i++) {
if (p_colors[l_i] > (float)1.0) p_colors[l_i] = (float)1.0;
else if (p_colors[l_i] < (float)0.0) p_colors[l_i] = (float)0.0;
Expand Down

0 comments on commit f9d2af9

Please sign in to comment.