sfImageUnsharpMaskGD.class.php 7.23 KB
Newer Older
Яков's avatar
first  
Яков committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/*
 * This file is part of the sfImageTransform package.
 * (c) 2007 Stuart Lowes <stuart.lowes@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 *
 * sfImageUnsharpMaskGD class.
 *
 * Applies an unsharp mask to the image.
 *
 * Based on http://vikjavev.no/computing/ump.php
 *
 * @package sfImageTransform
 * @subpackage transforms
 * @author Stuart Lowes <stuart.lowes@gmail.com>
 * @version SVN: $Id$
 */
class sfImageUnsharpMaskGD extends sfImageTransformAbstract
{
  /**
   *
   * @var integer
   */
  protected $amount = 500;

  /**
   *
   * @var integer
   */
  protected $radius = 50;

  /**
   *
   * @var float
   */
  protected $threshold = 255;

  public function __construct($radius, $threshold, $amount)
  {
    $this->setRadius($radius);
    $this->setThreshold($threshold);
    $this->setAmount($amount);
  }

  /**
   * The radius of the blurring circle of the mask
   *
   * @param integer
   */
  public function setRadius($radius)
  {
    if(is_numeric($radius) && $radius > 0 && $radius <= 50)
    {

      $this->radius = (float)$radius;
      
      return true;
    }

    return false;
  }

  /**
   * Returns the radius of the blurring circle of the mask
   *
   * @return integer
   */
  public function getRadius()
  {
    return $this->radius;
  }

  /**
   * Threshold the least
   * difference in colour values that is allowed between the original and the mask. In practice
   * this means that low-contrast areas of the picture are left unrendered whereas edges
   * are treated normally. This is good for pictures of e.g. skin or blue skies.
   *
   * @param float
   */
  public function setThreshold($threshold)
  {
    if(is_numeric($threshold) && $threshold > 0 && $threshold <= 500)
    {
      $this->threshold = (float)$threshold;
      
      return true;
    }

    return false;
  }

  /**
   *
   *
   * @return integer
   */
  public function getThreshold()
  {
    return $this->threshold;
  }

  /**
   * Amount of unsharp to be applied. 100 is normal
   *
   * @param integer
   */
  public function setAmount($amount)
  {
    if(is_numeric($amount) && $amount > 0 && $amount <= 500)
    {
      $this->amount = (int)$amount;

      return true;
    }

    return false;
  }

  /**
   * Returns the amount of unsharp mask to be applied
   *
   * @return integer
   */
  public function getAmount()
  {
    return $this->amount;
  }
  
  protected function transform(sfImage $image) 
  {
    $resource = $image->getAdapter()->getHolder();

    // Attempt to calibrate the parameters to Photoshop:
    $amount = $this->getAmount() * 0.016;
    $radius = abs(round($this->getRadius() * 2));

    $threshold = $this->getThreshold();

    $w = $image->getWidth();
    $h = $image->getHeight();

    if ($radius > 0)
    {
      $imgCanvas = imagecreatetruecolor($w, $h);
      $imgBlur = imagecreatetruecolor($w, $h);
      
      if (function_exists('imageconvolution')) 
      {
        $matrix = array(   
          array( 1, 2, 1 ),
          array( 2, 4, 2 ),
          array( 1, 2, 1 )
        );
        
        imagecopy ($imgBlur, $resource, 0, 0, 0, 0, $w, $h);
        imageconvolution($imgBlur, $matrix, 16, 0);   
      } 
      
      else
      {   

        // Move copies of the image around one pixel at the time and merge them with weight  
        // according to the matrix. The same matrix is simply repeated for higher radii.  
        for ($i = 0; $i < $radius; $i++)
        {  
          imagecopy ($imgBlur, $resource, 0, 0, 1, 0, $w - 1, $h); // left
          imagecopymerge ($imgBlur, $resource, 1, 0, 0, 0, $w, $h, 50); // right
          imagecopymerge ($imgBlur, $resource, 0, 0, 0, 0, $w, $h, 50); // center
          imagecopy ($imgCanvas, $imgBlur, 0, 0, 0, 0, $w, $h);  

          imagecopymerge ($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 33.33333 ); // up  
          imagecopymerge ($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 25); // down  
        }  
      }  

      if ($threshold > 0)
      {
        // Calculate the difference between the blurred pixels and the original  
        // and set the pixels  
        
        // Each row
        for ($x = 0; $x < $w - 1; $x++)
        { 
          // Each pixel
          for ($y = 0; $y < $h; $y++)
          {

            $rgbOrig = ImageColorAt($resource, $x, $y);
            $rOrig = (($rgbOrig >> 16) & 0xFF);  
            $gOrig = (($rgbOrig >> 8) & 0xFF);  
            $bOrig = ($rgbOrig & 0xFF);  

            $rgbBlur = ImageColorAt($imgBlur, $x, $y);  

            $rBlur = (($rgbBlur >> 16) & 0xFF);  
            $gBlur = (($rgbBlur >> 8) & 0xFF);  
            $bBlur = ($rgbBlur & 0xFF);  

            // When the masked pixels differ less from the original  
            // than the threshold specifies, they are set to their original value.  
            $rNew = $rOrig;
            if (abs($rOrig - $rBlur) >= $threshold)
            {
              $rNew = max(0, min(255, ($amount * ($rOrig - $rBlur)) + $rOrig));
            }
            
            $gNew = $gOrig;
            if (abs($gOrig - $gBlur) >= $threshold)
            {
              $gNew = max(0, min(255, ($amount * ($gOrig - $gBlur)) + $gOrig));
            }
            
            
            $bNew = $bOrig;
            if (abs($bOrig - $bBlur) >= $threshold)   
            {
              $bNew = max(0, min(255, ($amount * ($bOrig - $bBlur)) + $bOrig));
            }

            if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) 
            {  
              $pixCol = ImageColorAllocate($resource, $rNew, $gNew, $bNew);
              ImageSetPixel($resource, $x, $y, $pixCol);
            }  
          }  
        }  
      }  
     
      else
      {  
        // each row
        for ($x = 0; $x < $w; $x++)
        {   
          // each pixel 
          for ($y = 0; $y < $h; $y++)
          {  
            $rgbOrig = ImageColorAt($resource, $x, $y);
            $rOrig = (($rgbOrig >> 16) & 0xFF);  
            $gOrig = (($rgbOrig >> 8) & 0xFF);  
            $bOrig = ($rgbOrig & 0xFF);  

            $rgbBlur = ImageColorAt($imgBlur, $x, $y);  

            $rBlur = (($rgbBlur >> 16) & 0xFF);  
            $gBlur = (($rgbBlur >> 8) & 0xFF);  
            $bBlur = ($rgbBlur & 0xFF);  

            $rNew = ($amount * ($rOrig - $rBlur)) + $rOrig;  
            
            if($rNew > 255)
            {
              $rNew = 255;
            }  
            elseif ($rNew < 0)
            {
              $rNew = 0;
            }
            
            $gNew = ($amount * ($gOrig - $gBlur)) + $gOrig;
            
            if($gNew > 255)
            {
              $gNew = 255;
            }  
            elseif ($gNew < 0)
            {
              $gNew = 0;
            }
            
            $bNew = ($amount * ($bOrig - $bBlur)) + $bOrig;  
            
            if ($bNew > 255)
            {
              $bNew = 255;
            }  
            elseif ($bNew < 0)
            {
              $bNew = 0;
            }
            
            $rgbNew = ($rNew << 16) + ($gNew <<8) + $bNew;  
            ImageSetPixel($resource, $x, $y, $rgbNew);
          }  
        }  
      }
      
      imagedestroy($imgCanvas);
      imagedestroy($imgBlur);
    
    }
    
    return $image;
  }

}