Blame IlmImf/ImfRgbaYca.h

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