Blame IlmImf/ImfRgbaYca.h

Packit 0d464f
#ifndef INCLUDED_IMF_RGBA_YCA_H
Packit 0d464f
#define INCLUDED_IMF_RGBA_YCA_H
Packit 0d464f
Packit 0d464f
//////////////////////////////////////////////////////////////////////////////
Packit 0d464f
//
Packit 0d464f
// Copyright (c) 2004, Industrial Light & Magic, a division of Lucasfilm
Packit 0d464f
// Entertainment Company Ltd.  Portions contributed and copyright held by
Packit 0d464f
// others as indicated.  All rights reserved.
Packit 0d464f
//
Packit 0d464f
// Redistribution and use in source and binary forms, with or without
Packit 0d464f
// modification, are permitted provided that the following conditions are
Packit 0d464f
// met:
Packit 0d464f
//
Packit 0d464f
//     * Redistributions of source code must retain the above
Packit 0d464f
//       copyright notice, this list of conditions and the following
Packit 0d464f
//       disclaimer.
Packit 0d464f
//
Packit 0d464f
//     * Redistributions in binary form must reproduce the above
Packit 0d464f
//       copyright notice, this list of conditions and the following
Packit 0d464f
//       disclaimer in the documentation and/or other materials provided with
Packit 0d464f
//       the distribution.
Packit 0d464f
//
Packit 0d464f
//     * Neither the name of Industrial Light & Magic nor the names of
Packit 0d464f
//       any other contributors to this software may be used to endorse or
Packit 0d464f
//       promote products derived from this software without specific prior
Packit 0d464f
//       written permission.
Packit 0d464f
//
Packit 0d464f
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
Packit 0d464f
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
Packit 0d464f
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Packit 0d464f
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
Packit 0d464f
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Packit 0d464f
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Packit 0d464f
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Packit 0d464f
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Packit 0d464f
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit 0d464f
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Packit 0d464f
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 0d464f
//
Packit 0d464f
//////////////////////////////////////////////////////////////////////////////
Packit 0d464f
Packit 0d464f
//-----------------------------------------------------------------------------
Packit 0d464f
//
Packit 0d464f
//	Conversion between RGBA (red, green, blue alpha)
Packit 0d464f
//	and YCA (luminance, subsampled chroma, alpha) data:
Packit 0d464f
//
Packit 0d464f
//	Luminance, Y, is computed as a weighted sum of R, G, and B:
Packit 0d464f
//
Packit 0d464f
//		Y = yw.x * R + yw.y * G + yw.z * B
Packit 0d464f
//
Packit 0d464f
//	Function computeYw() computes a set of RGB-to-Y weights, yw,
Packit 0d464f
//	from a set of primary and white point chromaticities.
Packit 0d464f
//
Packit 0d464f
//	Chroma, C, consists of two components, RY and BY:
Packit 0d464f
//
Packit 0d464f
//		RY = (R - Y) / Y
Packit 0d464f
//		BY = (B - Y) / Y
Packit 0d464f
//
Packit 0d464f
//	For efficiency, the x and y subsampling rates for chroma are
Packit 0d464f
//	hardwired to 2, and the chroma subsampling and reconstruction
Packit 0d464f
//	filters are fixed 27-pixel wide windowed sinc functions.
Packit 0d464f
//
Packit 0d464f
//	Starting with an image that has RGBA data for all pixels,
Packit 0d464f
//
Packit 0d464f
//		RGBA RGBA RGBA RGBA ... RGBA RGBA
Packit 0d464f
//		RGBA RGBA RGBA RGBA ... RGBA RGBA
Packit 0d464f
//		RGBA RGBA RGBA RGBA ... RGBA RGBA
Packit 0d464f
//		RGBA RGBA RGBA RGBA ... RGBA RGBA
Packit 0d464f
//		...
Packit 0d464f
//		RGBA RGBA RGBA RGBA ... RGBA RGBA
Packit 0d464f
//		RGBA RGBA RGBA RGBA ... RGBA RGBA
Packit 0d464f
//
Packit 0d464f
//	function RGBAtoYCA() converts the pixels to YCA format:
Packit 0d464f
//
Packit 0d464f
//		YCA  YCA  YCA  YCA  ... YCA  YCA
Packit 0d464f
//		YCA  YCA  YCA  YCA  ... YCA  YCA
Packit 0d464f
//		YCA  YCA  YCA  YCA  ... YCA  YCA
Packit 0d464f
//		YCA  YCA  YCA  YCA  ... YCA  YCA
Packit 0d464f
//		...
Packit 0d464f
//		YCA  YCA  YCA  YCA  ... YCA  YCA
Packit 0d464f
//		YCA  YCA  YCA  YCA  ... YCA  YCA
Packit 0d464f
//
Packit 0d464f
//	Next, decimateChomaHoriz() eliminates the chroma values from
Packit 0d464f
//	the odd-numbered pixels in every scan line:
Packit 0d464f
//
Packit 0d464f
//		YCA  YA   YCA  YA   ... YCA  YA  
Packit 0d464f
//		YCA  YA   YCA  YA   ... YCA  YA  
Packit 0d464f
//		YCA  YA   YCA  YA   ... YCA  YA  
Packit 0d464f
//		YCA  YA   YCA  YA   ... YCA  YA  
Packit 0d464f
//		...
Packit 0d464f
//		YCA  YA   YCA  YA   ... YCA  YA  
Packit 0d464f
//		YCA  YA   YCA  YA   ... YCA  YA  
Packit 0d464f
//
Packit 0d464f
//	decimateChromaVert() eliminates all chroma values from the
Packit 0d464f
//	odd-numbered scan lines:
Packit 0d464f
//
Packit 0d464f
//		YCA  YA   YCA  YA   ... YCA  YA  
Packit 0d464f
//		YA   YA   YA   YA   ... YA   YA  
Packit 0d464f
//		YCA  YA   YCA  YA   ... YCA  YA  
Packit 0d464f
//		YA   YA   YA   YA   ... YA   YA  
Packit 0d464f
//		...
Packit 0d464f
//		YCA  YA   YCA  YA   ... YCA  YA  
Packit 0d464f
//		YA   YA   YA   YA   ... YA   YA  
Packit 0d464f
//
Packit 0d464f
//	Finally, roundYCA() reduces the precision of the luminance
Packit 0d464f
//	and chroma values so that the pixel data shrink more when
Packit 0d464f
//	they are saved in a compressed file.
Packit 0d464f
//
Packit 0d464f
//	The output of roundYCA() can be converted back to a set
Packit 0d464f
//	of RGBA pixel data that is visually very similar to the
Packit 0d464f
//	original RGBA image, by calling reconstructChromaHoriz(),
Packit 0d464f
//	reconstructChromaVert(), YCAtoRGBA(), and finally
Packit 0d464f
//	fixSaturation().
Packit 0d464f
//
Packit 0d464f
//-----------------------------------------------------------------------------
Packit 0d464f
Packit 0d464f
#include "ImfRgba.h"
Packit 0d464f
#include "ImfChromaticities.h"
Packit 0d464f
#include "ImfNamespace.h"
Packit 0d464f
Packit 0d464f
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
Packit 0d464f
Packit 0d464f
namespace RgbaYca {
Packit 0d464f
Packit 0d464f
Packit 0d464f
//
Packit 0d464f
// Width of the chroma subsampling and reconstruction filters
Packit 0d464f
//
Packit 0d464f
Packit 0d464f
static const int N = 27;
Packit 0d464f
static const int N2 = N / 2;
Packit 0d464f
Packit 0d464f
Packit 0d464f
//
Packit 0d464f
// Convert a set of primary chromaticities into a set of weighting
Packit 0d464f
// factors for computing a pixels's luminance, Y, from R, G and B
Packit 0d464f
//
Packit 0d464f
 
Packit 0d464f
IMF_EXPORT
Packit 0d464f
IMATH_NAMESPACE::V3f computeYw (const Chromaticities &cr);
Packit 0d464f
Packit 0d464f
Packit 0d464f
//
Packit 0d464f
// Convert an array of n RGBA pixels, rgbaIn, to YCA (luminance/chroma/alpha):
Packit 0d464f
//
Packit 0d464f
//	ycaOut[i].g = Y (rgbaIn[i]);
Packit 0d464f
//	ycaOut[i].r = RY (rgbaIn[i]);
Packit 0d464f
//	ycaOut[i].b = BY (rgbaIn[i]);
Packit 0d464f
//	ycaOut[i].a = aIsValid? rgbaIn[i].a: 1
Packit 0d464f
//
Packit 0d464f
// yw is a set of RGB-to-Y weighting factors, as computed by computeYw().
Packit 0d464f
//
Packit 0d464f
Packit 0d464f
IMF_EXPORT
Packit 0d464f
void RGBAtoYCA (const IMATH_NAMESPACE::V3f &yw,
Packit 0d464f
		int n,
Packit 0d464f
	        bool aIsValid,
Packit 0d464f
		const Rgba rgbaIn[/*n*/],
Packit 0d464f
		Rgba ycaOut[/*n*/]);
Packit 0d464f
Packit 0d464f
//
Packit 0d464f
// Perform horizontal low-pass filtering and subsampling of
Packit 0d464f
// the chroma channels of an array of n pixels.  In order
Packit 0d464f
// to avoid indexing off the ends of the input array during
Packit 0d464f
// low-pass filtering, ycaIn must have N2 extra pixels at
Packit 0d464f
// both ends.  Before calling decimateChromaHoriz(), the extra
Packit 0d464f
// pixels should be filled with copies of the first and last
Packit 0d464f
// "real" input pixel.
Packit 0d464f
//
Packit 0d464f
Packit 0d464f
IMF_EXPORT
Packit 0d464f
void decimateChromaHoriz (int n,
Packit 0d464f
			  const Rgba ycaIn[/*n+N-1*/],
Packit 0d464f
			  Rgba ycaOut[/*n*/]);
Packit 0d464f
Packit 0d464f
//
Packit 0d464f
// Perform vertical chroma channel low-pass filtering and subsampling.
Packit 0d464f
// N scan lines of input pixels are combined into a single scan line
Packit 0d464f
// of output pixels.
Packit 0d464f
//
Packit 0d464f
Packit 0d464f
IMF_EXPORT
Packit 0d464f
void decimateChromaVert (int n,
Packit 0d464f
			 const Rgba * const ycaIn[N],
Packit 0d464f
			 Rgba ycaOut[/*n*/]);
Packit 0d464f
Packit 0d464f
//
Packit 0d464f
// Round the luminance and chroma channels of an array of YCA
Packit 0d464f
// pixels that has already been filtered and subsampled.
Packit 0d464f
// The signifcands of the pixels' luminance and chroma values
Packit 0d464f
// are rounded to roundY and roundC bits respectively.
Packit 0d464f
//
Packit 0d464f
Packit 0d464f
IMF_EXPORT
Packit 0d464f
void roundYCA (int n,
Packit 0d464f
	       unsigned int roundY,
Packit 0d464f
	       unsigned int roundC,
Packit 0d464f
	       const Rgba ycaIn[/*n*/],
Packit 0d464f
	       Rgba ycaOut[/*n*/]);
Packit 0d464f
Packit 0d464f
//
Packit 0d464f
// For a scan line that has valid chroma data only for every other pixel,
Packit 0d464f
// reconstruct the missing chroma values.
Packit 0d464f
//
Packit 0d464f
Packit 0d464f
IMF_EXPORT
Packit 0d464f
void reconstructChromaHoriz (int n,
Packit 0d464f
			     const Rgba ycaIn[/*n+N-1*/],
Packit 0d464f
			     Rgba ycaOut[/*n*/]);
Packit 0d464f
Packit 0d464f
//
Packit 0d464f
// For a scan line that has only luminance and no valid chroma data,
Packit 0d464f
// reconstruct chroma from the surronding N scan lines.
Packit 0d464f
//
Packit 0d464f
Packit 0d464f
IMF_EXPORT
Packit 0d464f
void reconstructChromaVert (int n,
Packit 0d464f
			    const Rgba * const ycaIn[N],
Packit 0d464f
			    Rgba ycaOut[/*n*/]);
Packit 0d464f
			 
Packit 0d464f
//
Packit 0d464f
// Convert an array of n YCA (luminance/chroma/alpha) pixels to RGBA.
Packit 0d464f
// This function is the inverse of RGBAtoYCA().
Packit 0d464f
// yw is a set of RGB-to-Y weighting factors, as computed by computeYw().
Packit 0d464f
//
Packit 0d464f
Packit 0d464f
IMF_EXPORT
Packit 0d464f
void YCAtoRGBA (const IMATH_NAMESPACE::V3f &yw,
Packit 0d464f
		int n,
Packit 0d464f
		const Rgba ycaIn[/*n*/],
Packit 0d464f
		Rgba rgbaOut[/*n*/]);
Packit 0d464f
			 
Packit 0d464f
//
Packit 0d464f
// Eliminate super-saturated pixels:
Packit 0d464f
//
Packit 0d464f
// Converting an image from RGBA to YCA, low-pass filtering chroma,
Packit 0d464f
// and converting the result back to RGBA can produce pixels with
Packit 0d464f
// super-saturated colors, where one or two of the RGB components
Packit 0d464f
// become zero or negative.  (The low-pass and reconstruction filters
Packit 0d464f
// introduce some amount of ringing into the chroma components.
Packit 0d464f
// This can lead to negative RGB values near high-contrast edges.)
Packit 0d464f
//
Packit 0d464f
// The fixSaturation() function finds super-saturated pixels and
Packit 0d464f
// corrects them by desaturating their colors while maintaining
Packit 0d464f
// their luminance.  fixSaturation() takes three adjacent input
Packit 0d464f
// scan lines, rgbaIn[0], rgbaIn[1], rgbaIn[2], adjusts the
Packit 0d464f
// saturation of rgbaIn[1], and stores the result in rgbaOut.
Packit 0d464f
//
Packit 0d464f
Packit 0d464f
IMF_EXPORT
Packit 0d464f
void fixSaturation (const IMATH_NAMESPACE::V3f &yw,
Packit 0d464f
		    int n,
Packit 0d464f
		    const Rgba * const rgbaIn[3],
Packit 0d464f
		    Rgba rgbaOut[/*n*/]);
Packit 0d464f
Packit 0d464f
} // namespace RgbaYca
Packit 0d464f
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Packit 0d464f
Packit 0d464f
#endif