<commit_before>/*******************************************************************************
 * encoding.cpp
 *
 * ---------------------------------------------------------------------------
 * Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
 * Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
 *
 * POV-Ray is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * POV-Ray is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------------
 * POV-Ray is based on the popular DKB raytracer version 2.12.
 * DKBTrace was originally written by <NAME>.
 * DKBTrace Ver 2.0-2.12 were written by <NAME> & <NAME>.
 * ---------------------------------------------------------------------------
 * $File: //depot/povray/smp/source/base/image/encoding.cpp $
 * $Revision: #6 $
 * $Change: 6096 $
 * $DateTime: 2013/11/18 14:11:26 $
 * $Author: clipka $
 *******************************************************************************/

// configbase.h must always be the first POV file included within base *.cpp files
#include "base/configbase.h"
#include "base/image/encoding.h"
#include "base/image/image.h"
#include "base/povmsgid.h"

// this must be the last file included
#include "base/povdebug.h"

namespace pov_base
{

/*******************************************************************************/

#define ALPHA_EPSILON 1.0e-6 ///< Smallest alpha value we dare to safely use with premultiplied alpha

static const unsigned int MaxBayerMatrixSize = 4;
typedef float BayerMatrix[MaxBayerMatrixSize][MaxBayerMatrixSize];

static const BayerMatrix BayerMatrices[MaxBayerMatrixSize+1] = 
{
	// dummy for 0x0
	{ { 0 } },
	// 1x1 (of little use, but here it is)
	{ { 1/2.0-0.5 } },
	// 2x2
	{ { 1/4.0-0.5, 3/4.0-0.5 },
	  { 4/4.0-0.5, 2/4.0-0.5 } },
	// 3x3
	{ { 3/9.0-0.5, 7/9.0-0.5, 4/9.0-0.5 },
	  { 6/9.0-0.5, 1/9.0-0.5, 9/9.0-0.5 },
	  { 2/9.0-0.5, 8/9.0-0.5, 5/9.0-0.5 } },
	// 4x4
	{ {  1/16.0-0.5,  9/16.0-0.5,  3/16.0-0.5, 11/16.0-0.5 },
	  { 13/16.0-0.5,  5/16.0-0.5, 15/16.0-0.5,  7/16.0-0.5 },
	  {  4/16.0-0.5, 12/16.0-0.5,  2/16.0-0.5, 10/16.0-0.5 },
	  { 16/16.0-0.5,  8/16.0-0.5, 14/16.0-0.5,  6/16.0-0.5 } }
};

/*******************************************************************************/

/// Class representing "no-op" dithering rules.
class NoDither : public DitherHandler
{
	public:
		virtual void getOffset(unsigned int x, unsigned int y, OffsetInfo& offLin, OffsetInfo& offQnt);
};

/// Class representing bayer dithering rules, generating a regular pattern.
class BayerDither : public DitherHandler
{
	public:
		BayerDither(unsigned int mxSize);
		virtual void getOffset(unsigned int x, unsigned int y, OffsetInfo& offLin, OffsetInfo& offQnt);
		static inline float getOffset(unsigned int x, unsigned int y, unsigned int ms) { return BayerMatrices[ms][x%ms][y%ms]; }
	protected:
		OffsetInfo  lastErr;
		int         matrixSize;
};

/// Class representing simple 1D error diffusion dithering rules, carrying over the error from one pixel to the next.
class DiffusionDither1D : public DitherHandler
{
	public:
		virtual void getOffset(unsigned int x, unsigned int y, OffsetInfo& offLin, OffsetInfo& offQnt);
		virtual void setError(unsigned int x, unsigned int y, const OffsetInfo& err);
	protected:
		OffsetInfo lastErr;
};

/// Class representing simple 2D error diffusion dithering rules, carrying over the error from one pixel to the right, as well as the two pixels below.
/// @note   This implementation uses an additional 1-line pixel buffer to avoid manipulating the original image.
class DiffusionDither2D : public DitherHandler
{
	public:
		DiffusionDither2D(unsigned int width);
		virtual ~DiffusionDither2D();
		virtual void getOffset(unsigned int x, unsigned int y, OffsetInfo& offLin, OffsetInfo& offQnt);
		virtual void setError(unsigned int x, unsigned int y, const OffsetInfo& err);
	protected:
		unsigned int imageWidth;
		OffsetInfo* nextRowOffset;
		OffsetInfo* thisRowOffset;
};

/// Class representing Floyd-Steinberg dithering rules, carrying over the error from one pixel to the right, as well as the three pixels below.
/// @note   This implementation uses an additional 1-line pixel buffer to avoid manipulating the original image.
class FloydSteinbergDither : public DitherHandler
{
	public:
		FloydSteinbergDither(unsigned int width);
		virtual ~FloydSteinbergDither();
		virtual void getOffset(unsigned int x, unsigned int y, OffsetInfo& offLin, OffsetInfo& offQnt);
		virtual void setError(unsigned int x, unsigned int y, const OffsetInfo& err);
	protected:
		unsigned int imageWidth;
		OffsetInfo* nextRowOffset;
		OffsetInfo* thisRowOffset;
};

/*******************************************************************************/
<commit_msg>minor fixes in dithering code<commit_after>/*******************************************************************************
 * encoding.cpp
 *
 * ---------------------------------------------------------------------------
 * Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
 * Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
 *
 * POV-Ray is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * POV-Ray is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------------
 * POV-Ray is based on the popular DKB raytracer version 2.12.
 * DKBTrace was originally written by <NAME>.
 * DKBTrace Ver 2.0-2.12 were written by <NAME> & <NAME>.
 * ---------------------------------------------------------------------------
 * $File: //depot/povray/smp/source/base/image/encoding.cpp $
 * $Revision: #6 $
 * $Change: 6096 $
 * $DateTime: 2013/11/18 14:11:26 $
 * $Author: clipka $
 *******************************************************************************/

// configbase.h must always be the first POV file included within base *.cpp files
#include "base/configbase.h"
#include "base/image/encoding.h"
#include "base/image/image.h"
#include "base/povmsgid.h"

// this must be the last file included
#include "base/povdebug.h"

namespace pov_base
{

/*******************************************************************************/

#define ALPHA_EPSILON 1.0e-6 ///< Smallest alpha value we dare to safely use with premultiplied alpha

static const unsigned int MaxBayerMatrixSize = 4;
typedef float BayerMatrix[MaxBayerMatrixSize][MaxBayerMatrixSize];

static const BayerMatrix BayerMatrices[MaxBayerMatrixSize+1] = 
{
	// dummy for 0x0
	{ { 0 } },
	// 1x1 (of little use, but here it is)
	{ { 1/2.0-0.5 } },
	// 2x2
	{ { 1/4.0-0.5, 3/4.0-0.5 },
	  { 4/4.0-0.5, 2/4.0-0.5 } },
	// 3x3
	{ { 3/9.0-0.5, 7/9.0-0.5, 4/9.0-0.5 },
	  { 6/9.0-0.5, 1/9.0-0.5, 9/9.0-0.5 },
	  { 2/9.0-0.5, 8/9.0-0.5, 5/9.0-0.5 } },
	// 4x4
	{ {  1/16.0-0.5,  9/16.0-0.5,  3/16.0-0.5, 11/16.0-0.5 },
	  { 13/16.0-0.5,  5/16.0-0.5, 15/16.0-0.5,  7/16.0-0.5 },
	  {  4/16.0-0.5, 12/16.0-0.5,  2/16.0-0.5, 10/16.0-0.5 },
	  { 16/16.0-0.5,  8/16.0-0.5, 14/16.0-0.5,  6/16.0-0.5 } }
};

/*******************************************************************************/

/// Class representing "no-op" dithering rules.
class NoDither : public DitherHandler
{
	public:
		virtual void getOffset(unsigned int x, unsigned int y, OffsetInfo& offLin, OffsetInfo& offQnt);
};

/// Class representing bayer dithering rules, generating a regular pattern.
class BayerDither : public DitherHandler
{
	public:
		BayerDither(unsigned int mxSize);
		virtual void getOffset(unsigned int x, unsigned int y, OffsetInfo& offLin, OffsetInfo& offQnt);
		static inline float getOffset(unsigned int x, unsigned int y, unsigned int ms) { return BayerMatrices[ms][x%ms][y%ms]; }
	protected:
		unsigned int matrixSize;
};

/// Class representing simple 1D error diffusion dithering rules, carrying over the error from one pixel to the next.
class DiffusionDither1D : public DitherHandler
{
	public:
		virtual void getOffset(unsigned int x, unsigned int y, OffsetInfo& offLin, OffsetInfo& offQnt);
		virtual void setError(unsigned int x, unsigned int y, const OffsetInfo& err);
	protected:
		OffsetInfo lastErr;
};

/// Class representing simple 2D error diffusion dithering rules, carrying over the error from one pixel to the right, as well as the two pixels below.
/// @note   This implementation uses an additional 2-line pixel buffer to avoid manipulating the original image.
class DiffusionDither2D : public DitherHandler
{
	public:
		DiffusionDither2D(unsigned int width);
		virtual ~DiffusionDither2D();
		virtual void getOffset(unsigned int x, unsigned int y, OffsetInfo& offLin, OffsetInfo& offQnt);
		virtual void setError(unsigned int x, unsigned int y, const OffsetInfo& err);
	protected:
		unsigned int imageWidth;
		OffsetInfo* nextRowOffset;
		OffsetInfo* thisRowOffset;
};

/// Class representing Floyd-Steinberg dithering rules, carrying over the error from one pixel to the right, as well as the three pixels below.
/// @note   This implementation uses an additional 2-line pixel buffer to avoid manipulating the original image.
class FloydSteinbergDither : public DitherHandler
{
	public:
		FloydSteinbergDither(unsigned int width);
		virtual ~FloydSteinbergDither();
		virtual void getOffset(unsigned int x, unsigned int y, OffsetInfo& offLin, OffsetInfo& offQnt);
		virtual void setError(unsigned int x, unsigned int y, const OffsetInfo& err);
	protected:
		unsigned int imageWidth;
		OffsetInfo* nextRowOffset;
		OffsetInfo* thisRowOffset;
};

/*******************************************************************************/

{'fraction_non_alphanumeric': 0.21911487758945386, 'fraction_numerical': 0.044256120527306965, 'mean_word_length': 3.246701319472211, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4529390', 'n_tokens_mistral': 3546, 'n_tokens_neox': 3230, 'n_words': 1073}