1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.tiff.photometricinterpreters;
18
19 import java.io.IOException;
20
21 import org.apache.commons.imaging.ImagingException;
22 import org.apache.commons.imaging.common.ImageBuilder;
23
24 public class PhotometricInterpreterYCbCr extends PhotometricInterpreter {
25
26
27
28
29
30
31
32
33
34
35
36
37 public static int convertYCbCrtoRGB(final int y, final int cb, final int cr) {
38 final double r1 = 1.164 * (y - 16.0) + 1.596 * (cr - 128.0);
39 final double g1 = 1.164 * (y - 16.0) - 0.813 * (cr - 128.0) - 0.392 * (cb - 128.0);
40 final double b1 = 1.164 * (y - 16.0) + 2.017 * (cb - 128.0);
41
42 final int r = limit((int) r1, 0, 255);
43 final int g = limit((int) g1, 0, 255);
44 final int b = limit((int) b1, 0, 255);
45
46 final int alpha = 0xff;
47 return alpha << 24 | r << 16 | g << 8 | b << 0;
48 }
49
50 public static int limit(final int value, final int min, final int max) {
51 return Math.min(max, Math.max(min, value));
52 }
53
54 public PhotometricInterpreterYCbCr(final int samplesPerPixel, final int[] bitsPerSample, final int predictor, final int width, final int height) {
55 super(samplesPerPixel, bitsPerSample, predictor, width, height);
56 }
57
58 @Override
59 public void interpretPixel(final ImageBuilder imageBuilder, final int[] samples, final int x, final int y) throws ImagingException, IOException {
60 final int Y = samples[0];
61 final int cb = samples[1];
62 final int cr = samples[2];
63 final double r = Y + 1.402 * (cr - 128.0);
64 final double g = Y - 0.34414 * (cb - 128.0) - 0.71414 * (cr - 128.0);
65 final double b = Y + 1.772 * (cb - 128.0);
66
67 final int red = limit((int) r, 0, 255);
68 final int green = limit((int) g, 0, 255);
69 final int blue = limit((int) b, 0, 255);
70
71 final int alpha = 0xff;
72 final int rgb = alpha << 24 | red << 16 | green << 8 | blue << 0;
73 imageBuilder.setRgb(x, y, rgb);
74
75 }
76 }