Source for file Blend.php
Documentation is available at Blend.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker */
* Copyright (c) 2008 Firman Wandayandi <firman@php.net>
* This source file is subject to the BSD License license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://www.opensource.org/licenses/bsd-license.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to pear-dev@list.php.net so we can send you a copy immediately.
* @author Firman Wandayandi <firman@php.net>
* @copyright Copyright (c) 2008 Firman Wandayandi <firman@php.net>
* @license http://www.opensource.org/licenses/bsd-license.php
* @version $Id: Blend.php,v 1.3 2008/05/26 09:31:23 firman Exp $
* @since File available since Release 1.0.0RC1
require_once 'Image/Tools.php';
require_once 'Image/Tools/Utils.php';
* Class provide image blending functions.
* Algorithms base on article http://www.pegtop.net/delphi/articles/blendmodes
* written by Jens Gruschel.
* @author Firman Wandayandi <firman@php.net>
* @copyright Copyright (c) 2008 Firman Wandayandi <firman@php.net>
* @license http://www.opensource.org/licenses/bsd-license.php
* @version Release: 1.0.0RC1
* @since Class available since Release 1.0.0RC1
* Image_Tools_Thumbnail API version.
* image1 mixed First image or destination or background
* image2 mixed Second image or sample or foreground
* mode string Blend mode, may one of normal, multiply,
* screen, darken, lighten, difference, exclusion,
* negation, interpolation, stamp, softlight, hardlight,
* overlay, colordodge, colorburn, softdodge, softburn,
* additive, subtractive, reflect, glow, freeze, heat,
* logicXOR, logicAND or logicOR
* x int X position of the second image on the first image
* y int Y position of the second image on the first image
* @see Image_Tools_Utils::$options
* Cosine table for interpolation mode.
* @see Image_Tools_Utils::interpolation()
var $_cosineTab = array ();
* Function which called before render.
* @return bool|PEAR_ErrorTRUE on success or PEAR_Error on failure.
* @see Image_Tools::createImage()
if (PEAR ::isError ($res)) {
if (PEAR ::isError ($res)) {
// initialize an array if only its interpolation mode
if ($this->options['mode'] == 'interpolation') {
for ($i = 0; $i < 256; $i++ ) {
$this->cosineTab[] = (int) (round(64 - cos($i * M_PI / 255 ) * 64 ));
* @return true|PEAR_Error
$method = array ($this, " _{$this->options['mode']}" );
return PEAR ::raiseError ('Invalid mode or not supported');
$height1 = imagesy($this->_image1);
// sets out of bound flags
$x_outbound = $y_outbound = false;
// walking through pixels
for ($x2 = 0 , $x1 = $this->options['x']; $x2 < $width; $x2++ , $x1++ ) {
// x and y already out of bound, nothing to process
if ($x_outbound && $y_outbound) {
$x_outbound = $y_outbound = false;
for ($y2 = 0 , $y1 = $this->options['y']; $y2 < $height; $y2++ , $y1++ ) {
if ($color2['a'] == 127 ) {
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _multiply ($a, $b)
return (int) ($a * $b / 255 );
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
return (int) (255 - ((255 - $a) * (255 - $b) / 255 ));
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _lighten ($a, $b)
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _difference ($a, $b)
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _exclusion ($a, $b)
return (int) ($a + $b - ($a * $b / 127 ));
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _negation ($a, $b)
return 255 - abs(255 - $a - $b);
* Interpolation blend mode.
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _interpolation ($a, $b)
$c = $this->cosineTab[$b] + $this->cosineTab[$a];
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _softlight ($a, $b)
$c = (int) ($a * $b / 255 );
return (int) ($c + $a * (255 - ((255- $a) * (255- $b) / 255 ) - $c) / 255 );
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _hardlight ($a, $b)
$result = 255 - ((255- $b) * (255- $a) / 127 );
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _overlay ($a, $b)
$result = 255 - ((255 - $a) * (255 - $b) / 127 );
* Color Dodge blend mode.
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _colordodge ($a, $b)
$c = (int) (($a << 8 ) / (255- $b));
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _colorburn ($a, $b)
$c = (int) (255 - (((255- $a) << 8 ) / $b));
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _softdodge ($a, $b)
$c = (int) (($a << 7 ) / (255- $b));
$c = (int) (255 - (((255- $b) << 7 ) / $a));
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _softburn ($a, $b)
$c = (int) (($b << 7 ) / (255- $a));
$c = (int) (255 - (((255- $a) << 7 ) / $b));
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _additive ($a, $b)
* Subtractive blend mode.
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _subtractive ($a, $b)
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _reflect ($a, $b)
$c = (int) ($a * $a / (255- $b));
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
$c = (int) ($b* $b / (255- $a));
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
$c = (int) (255 - sqrt(255- $a) / $b);
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
$c = (int) (255 - sqrt(255- $b) / $a);
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _logicXOR ($a, $b)
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _logicAND ($a, $b)
* @param integer $a Background color (0 to 255)
* @param integer $b Foreground color (0 to 255)
function _logicOR ($a, $b)
Documentation generated on Mon, 26 May 2008 06:30:06 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|