Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
887ed03
feat IMAGING-306, fixed RGBtoHSV and HSVtoRGB to have S and V be 0 to…
Mar 3, 2022
a9a3eff
feat IMAGING-306: added another test for RGBtoHSV and HSVtoRGB
oskaredv Mar 4, 2022
7aa22a5
Merge pull request #1 from DD2480-group4-22/IMAGING-306-HSV
oskaredv Mar 4, 2022
4891ff8
feat IMAGING-306-NEWTESTS: New tests for ColorConversions added
oskaredv Mar 5, 2022
b50c0c5
Merge pull request #2 from DD2480-group4-22/IMAGING-306-NEWTESTS
Jackohass Mar 5, 2022
2a1ed9b
feat IMAGING-306-NEWTESTS: Add tests for HunterLab, DINo99 & DINb99
Jackohass Mar 6, 2022
1c5eb68
Merge pull request #3 from DD2480-group4-22/IMAGING-306-TESTS-HUNTER-…
oskaredv Mar 6, 2022
acce8c4
methods expecting CMY and CMYK now expects them in the range 0-100, a…
nellyak Mar 7, 2022
c372325
fix IMAGING-306-HSL testing issues
Mar 7, 2022
469b38e
Merge branch 'master' into IMAGING-306-HSL
Mar 7, 2022
84cc1b3
fix: var_K in convertCMYtoCMYK now are the correct values.
nellyak Mar 7, 2022
10440ad
feat IMAGING-306-HSL fixed so that ColorConversions expects S and L i…
Mar 8, 2022
d7a6a94
fix: now convertCMYKtoRGB expects doubles and not ints. In convertCMY…
nellyak Mar 8, 2022
65e5d58
added new test testCMYtoCMYKtoCMY() to test 5 different values for CM…
nellyak Mar 8, 2022
34c5e8b
from int:s 0-255 to doubles in scale 0-100 in getRGB() for method Col…
nellyak Mar 8, 2022
6c4343a
from int:s 0-255 to doubles in scale 0-100 in interpretPixel() for me…
nellyak Mar 8, 2022
c305100
from int:s 0-255 to doubles in scale 0-100 in visitSOS() for method C…
nellyak Mar 8, 2022
25b1244
Merge pull request #5 from DD2480-group4-22/IMAGING-306-CMY-CMYK
oskaredv Mar 9, 2022
d2d0f3e
Merge branch 'master' into IMAGING-306-HSL
oskaredv Mar 9, 2022
da83fd7
Merge pull request #4 from DD2480-group4-22/IMAGING-306-HSL
oskaredv Mar 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 95 additions & 129 deletions src/main/java/org/apache/commons/imaging/color/ColorConversions.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,16 @@ public static ColorXyz convertRGBtoXYZ(final int rgb) {
}

public static ColorCmy convertRGBtoCMY(final int rgb) {
// CMY values = 0 ÷ 100
// RGB values = 0 ÷ 255

final int R = 0xff & (rgb >> 16);
final int G = 0xff & (rgb >> 8);
final int B = 0xff & (rgb >> 0);

// RGB values = 0 ÷ 255
// CMY values = 0 ÷ 1

final double C = 1 - (R / 255.0);
final double M = 1 - (G / 255.0);
final double Y = 1 - (B / 255.0);
final double C = (1 - (R / 255.0))*100.0;
final double M = (1 - (G / 255.0))*100.0;
final double Y = (1 - (B / 255.0))*100.0;

return new ColorCmy(C, M, Y);
}
Expand All @@ -198,24 +198,23 @@ public static int convertCMYtoRGB(final ColorCmy cmy) {
// and similarly for G and B.
// This is Ghostscript's formula with K = 0.

// CMY values = 0 ÷ 1
// CMY values = 0 ÷ 100
// RGB values = 0 ÷ 255

final double R = (1 - cmy.C) * 255.0;
final double G = (1 - cmy.M) * 255.0;
final double B = (1 - cmy.Y) * 255.0;

final double R = (1 - cmy.C / 100) * 255.0;
final double G = (1 - cmy.M / 100) * 255.0;
final double B = (1 - cmy.Y / 100) * 255.0;
return convertRGBtoRGB(R, G, B);
}

public static ColorCmyk convertCMYtoCMYK(final ColorCmy cmy) {
// Where CMYK and CMY values = 0 ÷ 1
// Where CMYK and CMY values = 0 ÷ 100

double C = cmy.C;
double M = cmy.M;
double Y = cmy.Y;

double var_K = 1.0;
double var_K = 100.0;

if (C < var_K) {
var_K = C;
Expand All @@ -226,15 +225,16 @@ public static ColorCmyk convertCMYtoCMYK(final ColorCmy cmy) {
if (Y < var_K) {
var_K = Y;
}
if (var_K == 1) { // Black
if (var_K == 100.0) { // Black
C = 0;
M = 0;
Y = 0;
} else {
C = (C - var_K) / (1 - var_K);
M = (M - var_K) / (1 - var_K);
Y = (Y - var_K) / (1 - var_K);
C = (C - var_K) / (1 - var_K / 100.0);
M = (M - var_K) / (1 - var_K / 100.0);
Y = (Y - var_K) / (1 - var_K / 100.0);
}

return new ColorCmyk(C, M, Y, var_K);
}

Expand All @@ -244,21 +244,15 @@ public static ColorCmy convertCMYKtoCMY(final ColorCmyk cmyk) {

public static ColorCmy convertCMYKtoCMY(double C, double M, double Y,
final double K) {
// Where CMYK and CMY values = 0 ÷ 1

C = (C * (1 - K) + K);
M = (M * (1 - K) + K);
Y = (Y * (1 - K) + K);
// Where CMYK and CMY values = 0 ÷ 100

C = C * (1 - K / 100.0) + K;
M = M * (1 - K / 100.0) + K;
Y = Y * (1 - K / 100.0) + K;
return new ColorCmy(C, M, Y);
}

public static int convertCMYKtoRGB(final int c, final int m, final int y, final int k) {
final double C = c / 255.0;
final double M = m / 255.0;
final double Y = y / 255.0;
final double K = k / 255.0;

public static int convertCMYKtoRGB(final double C, final double M, final double Y, final double K) {
return convertCMYtoRGB(convertCMYKtoCMY(C, M, Y, K));
}

Expand All @@ -272,6 +266,7 @@ public static ColorHsl convertRGBtoHSL(final int rgb) {
final double var_G = (G / 255.0);
final double var_B = (B / 255.0);


final double var_Min = Math.min(var_R, Math.min(var_G, var_B)); // Min. value
// of RGB
double var_Max;
Expand Down Expand Up @@ -334,7 +329,7 @@ public static ColorHsl convertRGBtoHSL(final int rgb) {
// Debug.debug("H2", H);
}

return new ColorHsl(H, S, L);
return new ColorHsl(H, S*100, L*100);
}

public static int convertHSLtoRGB(final ColorHsl hsl) {
Expand All @@ -346,19 +341,19 @@ public static int convertHSLtoRGB(final double H, final double S, final double L

if (S == 0) {
// HSL values = 0 ÷ 1
R = L * 255; // RGB results = 0 ÷ 255
G = L * 255;
B = L * 255;
R = L * 2.55; // RGB results = 0 ÷ 255
G = L * 2.55;
B = L * 2.55;
} else {
double var_2;

if (L < 0.5) {
var_2 = L * (1 + S);
var_2 = L/100 * (1 + S/100);
} else {
var_2 = (L + S) - (S * L);
var_2 = (L/100 + S/100) - (S/100 * L/100);
}

final double var_1 = 2 * L - var_2;
final double var_1 = 2 * L/100 - var_2;

R = 255 * convertHuetoRGB(var_1, var_2, H + (1 / 3.0));
G = 255 * convertHuetoRGB(var_1, var_2, H);
Expand Down Expand Up @@ -388,61 +383,40 @@ private static double convertHuetoRGB(final double v1, final double v2, double v
}

public static ColorHsv convertRGBtoHSV(final int rgb) {
double H = 0;
double S = 0;
double V = 0;

final int R = 0xff & (rgb >> 16);
final int G = 0xff & (rgb >> 8);
final int B = 0xff & (rgb >> 0);

final double var_R = (R / 255.0); // RGB values = 0 ÷ 255
final double var_G = (G / 255.0);
final double var_B = (B / 255.0);

final double var_Min = Math.min(var_R, Math.min(var_G, var_B)); // Min. value
// of RGB
boolean maxIsR = false;
boolean maxIsG = false;
double var_Max;
if (var_R >= var_G && var_R >= var_B) {
var_Max = var_R;
maxIsR = true;
} else if (var_G > var_B) {
var_Max = var_G;
maxIsG = true;
} else {
var_Max = var_B;
final double R1 = (R / 255.0); // RGB values = 0 ÷ 255
final double G1 = (G / 255.0);
final double B1 = (B / 255.0);

double Cmax = Math.max(R1, Math.max(G1, B1));
double Cmin = Math.min(R1, Math.min(G1, B1));
double delta = Cmax - Cmin;

if(delta == 0){
H = 0;
}else if(Cmax == B1){
H = Math.abs(60 * (((R1-G1)/delta) + 4));
}else if(Cmax == G1){
H = Math.abs(60 * (((B1-R1)/delta) + 2));
}else if(Cmax == R1){
H = Math.abs(60 * (((G1-B1)/delta) % 6));
}
final double del_Max = var_Max - var_Min; // Delta RGB value

final double V = var_Max;

double H, S;
if (del_Max == 0) {
// This is a gray, no chroma...
H = 0; // HSV results = 0 ÷ 1
S = 0;
} else {
// Chromatic data...
S = del_Max / var_Max;

final double del_R = (((var_Max - var_R) / 6) + (del_Max / 2)) / del_Max;
final double del_G = (((var_Max - var_G) / 6) + (del_Max / 2)) / del_Max;
final double del_B = (((var_Max - var_B) / 6) + (del_Max / 2)) / del_Max;

if (maxIsR) {
H = del_B - del_G;
} else if (maxIsG) {
H = (1 / 3.0) + del_R - del_B;
} else {
H = (2 / 3.0) + del_G - del_R;
}

if (H < 0) {
H += 1;
}
if (H > 1) {
H -= 1;
}
if(Cmax == 0){
S = 0;
} else{
S = (delta / Cmax)*100;
}

V = Cmax*100;

return new ColorHsv(H, S, V);
}

Expand All @@ -453,54 +427,46 @@ public static int convertHSVtoRGB(final ColorHsv HSV) {
public static int convertHSVtoRGB(final double H, final double S, final double V) {
double R, G, B;

if (S == 0) {
// HSV values = 0 ÷ 1
R = V * 255;
G = V * 255;
B = V * 255;
} else {
double var_h = H * 6;
if (var_h == 6) {
var_h = 0; // H must be < 1
}
final double var_i = Math.floor(var_h); // Or ... var_i = floor( var_h )
final double var_1 = V * (1 - S);
final double var_2 = V * (1 - S * (var_h - var_i));
final double var_3 = V * (1 - S * (1 - (var_h - var_i)));

double var_r, var_g, var_b;

if (var_i == 0) {
var_r = V;
var_g = var_3;
var_b = var_1;
} else if (var_i == 1) {
var_r = var_2;
var_g = V;
var_b = var_1;
} else if (var_i == 2) {
var_r = var_1;
var_g = V;
var_b = var_3;
} else if (var_i == 3) {
var_r = var_1;
var_g = var_2;
var_b = V;
} else if (var_i == 4) {
var_r = var_3;
var_g = var_1;
var_b = V;
} else {
var_r = V;
var_g = var_1;
var_b = var_2;
}

R = var_r * 255; // RGB results = 0 ÷ 255
G = var_g * 255;
B = var_b * 255;
double s = S/100;
double v = V/100;

double C = (v * s);
double X = C * (1 - Math.abs((H/60) % 2 - 1));
double m = v - C;

double R1 = 0;
double G1 = 0;
double B1 = 0;

if(0 <= H && H < 60){
R1 = C;
G1 = X;
B1 = 0;
}else if(60 <= H && H < 120){
R1 = X;
G1 = C;
B1 = 0;
}else if(120 <= H && H < 180){
R1 = 0;
G1 = C;
B1 = X;
}else if(180 <= H && H < 240){
R1 = 0;
G1 = X;
B1 = C;
}else if(240 <= H && H < 300){
R1 = X;
G1 = 0;
B1 = C;
}else if(300 <= H && H < 360){
R1 = C;
G1 = 0;
B1 = X;
}

R = (R1+m)*255;
G = (G1+m)*255;
B = (B1+m)*255;
return convertRGBtoRGB(R, G, B);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ public void visitSOS(final int marker, final byte[] markerBytes, final byte[] im
for (int x2 = 0; x2 < hSize
&& x1 + x2 < sofnSegment.width; x2++) {
if (scaledMCU.length == 4) {
final int C = scaledMCU[0].samples[srcRowOffset + x2];
final int M = scaledMCU[1].samples[srcRowOffset + x2];
final int Y = scaledMCU[2].samples[srcRowOffset + x2];
final int K = scaledMCU[3].samples[srcRowOffset + x2];
final double C = scaledMCU[0].samples[srcRowOffset + x2] / 2.550;
final double M = scaledMCU[1].samples[srcRowOffset + x2] / 2.550;
final double Y = scaledMCU[2].samples[srcRowOffset + x2] / 2.550;
final double K = scaledMCU[3].samples[srcRowOffset + x2] / 2.550;
final int rgb = ColorConversions.convertCMYKtoRGB(C, M, Y, K);
dataBuffer.setElem(dstRowOffset + x2, rgb);
} else if (scaledMCU.length == 3) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ protected int getRGB(final int[][][] data, final int x, final int y,
int sk = 0xff & data[3][y][x];

// CRAZY adobe has to store the bytes in reverse form.
sc = 255 - sc;
sm = 255 - sm;
sy = 255 - sy;
sk = 255 - sk;
final double SC = 100 - sc/2.550;
final double SM = 100 - sm/2.550;
final double SY = 100 - sy/2.550;
final double SK = 100 - sk/2.550;

return ColorConversions.convertCMYKtoRGB(sc, sm, sy, sk);
return ColorConversions.convertCMYKtoRGB(SC, SM, SY, SK);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public PhotometricInterpreterCmyk(final int samplesPerPixel,
public void interpretPixel(final ImageBuilder imageBuilder, final int[] samples, final int x,
final int y) throws ImageReadException, IOException {

final int sc = samples[0];
final int sm = samples[1];
final int sy = samples[2];
final int sk = samples[3];
final double sc = samples[0]/2.550;
final double sm = samples[1]/2.550;
final double sy = samples[2]/2.550;
final double sk = samples[3]/2.550;

final int rgb = ColorConversions.convertCMYKtoRGB(sc, sm, sy, sk);
imageBuilder.setRGB(x, y, rgb);
Expand Down
Loading