// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package png

// intSize is either 32 or 64.
const intSize = 32 << (^uint(0) >> 63)

func abs( int) int {
	// m := -1 if x < 0. m := 0 otherwise.
	 :=  >> (intSize - 1)

	// In two's complement representation, the negative number
	// of any number (except the smallest one) can be computed
	// by flipping all the bits and add 1. This is faster than
	// code with a branch.
	// See Hacker's Delight, section 2-4.
	return ( ^ ) - 
}

// paeth implements the Paeth filter function, as per the PNG specification.
func paeth(, ,  uint8) uint8 {
	// This is an optimized version of the sample code in the PNG spec.
	// For example, the sample code starts with:
	//	p := int(a) + int(b) - int(c)
	//	pa := abs(p - int(a))
	// but the optimized form uses fewer arithmetic operations:
	//	pa := int(b) - int(c)
	//	pa = abs(pa)
	 := int()
	 := int() - 
	 := int() - 
	 = abs( + )
	 = abs()
	 = abs()
	if  <=  &&  <=  {
		return 
	} else if  <=  {
		return 
	}
	return 
}

// filterPaeth applies the Paeth filter to the cdat slice.
// cdat is the current row's data, pdat is the previous row's data.
func filterPaeth(,  []byte,  int) {
	var , , , , ,  int
	for  := 0;  < ; ++ {
		,  = 0, 0
		for  := ;  < len();  +=  {
			 = int([])
			 =  - 
			 =  - 
			 = abs( + )
			 = abs()
			 = abs()
			if  <=  &&  <=  {
				// No-op.
			} else if  <=  {
				 = 
			} else {
				 = 
			}
			 += int([])
			 &= 0xff
			[] = uint8()
			 = 
		}
	}
}