    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.imaging.formats.tiff.photometricinterpreters;
    018
    019import java.io.IOException;
    020
    021import org.apache.commons.imaging.ImageReadException;
    022import org.apache.commons.imaging.common.ImageBuilder;
    023
    024public class PhotometricInterpreterYCbCr extends PhotometricInterpreter {
    025
    026    public PhotometricInterpreterYCbCr(final int samplesPerPixel,
    027            final int[] bitsPerSample, final int predictor,
    028            final int width, final int height) {
    029        super(samplesPerPixel, bitsPerSample, predictor, width, height);
    030    }
    031
    032    public static int limit(final int value, final int min, final int max) {
    033        return Math.min(max, Math.max(min, value));
    034    }
    035
    036    /**
    037     * This method converts a YUV (aka YCbCr) colorspace to a RGB colorspace.
    038     * This is handy when trying to reconstruct an image in Java from YCbCr
    039     * transmitted data. This routine expects the data to fall in the standard
    040     * PC 0..255 range per pixel, with the array dimensions corresponding to the
    041     * imageWidth and imageHeight. These variables are either set manually in
    042     * the case of a null constructor, or they are automatically calculated from
    043     * the image parameter constructor.
    044     *
    045     * @param Y
    046     *            The Y component set.
    047     * @param Cb
    048     *            The Cb component set.
    049     * @param Cr
    050     *            The Cr component set.
    051     * @return R The R component.
    052     */
    053    public static int convertYCbCrtoRGB(final int Y, final int Cb, final int Cr) {
    054        final double r1 = (((1.164 * (Y - 16.0))) + (1.596 * (Cr - 128.0)));
    055        final double g1 = (((1.164 * (Y - 16.0))) - (0.813 * (Cr - 128.0)) - (0.392 * (Cb - 128.0)));
    056        final double b1 = (((1.164 * (Y - 16.0))) + (2.017 * (Cb - 128.0)));
    057
    058        final int r = limit((int) r1, 0, 255);
    059        final int g = limit((int) g1, 0, 255);
    060        final int b = limit((int) b1, 0, 255);
    061
    062        final int alpha = 0xff;
    063        return (alpha << 24) | (r << 16) | (g << 8) | (b << 0);
    064    }
    065
    066    @Override
    067    public void interpretPixel(final ImageBuilder imageBuilder, final int[] samples, final int x,
    068            final int y) throws ImageReadException, IOException {
    069        final int Y = samples[0];
    070        final int Cb = samples[1];
    071        final int Cr = samples[2];
    072        final double R = Y + 1.402 * (Cr - 128.0);
    073        final double G = Y - 0.34414 * (Cb - 128.0) - 0.71414 * (Cr - 128.0);
    074        final double B = Y + 1.772 * (Cb - 128.0);
    075
    076        final int red = limit((int) R, 0, 255);
    077        final int green = limit((int) G, 0, 255);
    078        final int blue = limit((int) B, 0, 255);
    079
    080        final int alpha = 0xff;
    081        final int rgb = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
    082        imageBuilder.setRGB(x, y, rgb);
    083
    084    }
    085}