//-----------------------------------------------------------------------------
// Copyright © 2006 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/arith
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	pix_ftoi_base
//
// purpose	Convert a floating point (double) pixel intensity value in
//		the range 0.0 .. 1.0 to a gamma corrected integer (int) pixel
//		intensity value in the range 0 .. N-1.
//
// arguments	1 (double) gamma correction factor to use
//		2 (double) pixel intensity from 0.0 to 1.0
//		3 (int) integer scale N
//
// returns	(int) pixel intensity in the range 0 to N-1
//-----------------------------------------------------------------------------
#define pix_ftoi_base(g,v,s) ({							\
	double libh__v;								\
	double libh__g;								\
	int libh__s;								\
	libh__g = (g);								\
	libh__s = (s);								\
	( libh__s <= 1 ) ? 0 : ({						\
		libh__v = (v);							\
		( libh__v == 0.0 ) ? 0 : ({					\
			libh__v=(int)((exp(log(libh__v)/libh__g)*((double)(libh__s-1)))+0.5);	\
			( libh__v < 0 ) ? 0 : ( libh__v >= libh__s ) ? libh__s - 1 : libh__v;	\
		})								\
	})									\
})

//-----------------------------------------------------------------------------
// macro	pix_ftoi
//
// purpose	Convert a floating point (double) pixel intensity value in
//		the range 0.0 .. 1.0 to a gamma corrected integer (int) pixel
//		intensity value in the range 0 .. N-1.
//
// arguments	1 (double) gamma correction factor to use
//		2 (double) pixel intensity from 0.0 to 1.0
//		3 (int) integer scale N
//
// returns	(int) pixel intensity in the range 0 to N-1
//-----------------------------------------------------------------------------
#define pix_ftoi(g,v,s) ({							\
	double libh__g;								\
	libh__g = (g);								\
	if ( libh__g <= 0.0 ) libh__g = ( PIX_DEFAULT_GAMMA );			\
	pix_ftoi_base(libh__g,(v),(s));						\
})

__FMACRO_END__

