| 1: | <?php
|
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: |
|
| 24: |
|
| 25: | namespace WideImage\Operation;
|
| 26: |
|
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: |
|
| 32: | class AddNoise
|
| 33: | {
|
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: |
|
| 43: | public function execute($image, $amount, $type) {
|
| 44: | switch ($type) {
|
| 45: | case 'salt&pepper':
|
| 46: | $fun = 'saltPepperNoise_fun';
|
| 47: | break;
|
| 48: | case 'color':
|
| 49: | $fun = 'colorNoise_fun';
|
| 50: | break;
|
| 51: | default :
|
| 52: | $fun = 'monoNoise_fun';
|
| 53: | break;
|
| 54: | }
|
| 55: |
|
| 56: | return static::filter($image->asTrueColor(), $fun, $amount);
|
| 57: | }
|
| 58: |
|
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: |
|
| 67: | public function filter($image, $function, $value)
|
| 68: | {
|
| 69: | for ($y = 0; $y < $image->getHeight(); $y++) {
|
| 70: | for ($x = 0; $x< $image->getWidth(); $x++) {
|
| 71: | $rgb = imagecolorat($image->getHandle(), $x, $y);
|
| 72: |
|
| 73: | $a = ($rgb >> 24) & 0xFF;
|
| 74: | $r = ($rgb >> 16) & 0xFF;
|
| 75: | $g = ($rgb >> 8) & 0xFF;
|
| 76: | $b = $rgb & 0xFF;
|
| 77: |
|
| 78: | static::$function($r, $g, $b, $value);
|
| 79: |
|
| 80: | $color = imagecolorallocatealpha($image->getHandle(), $r, $g, $b, $a);
|
| 81: | imagesetpixel($image->getHandle(), $x, $y, $color);
|
| 82: | }
|
| 83: | }
|
| 84: |
|
| 85: | return $image;
|
| 86: | }
|
| 87: |
|
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: |
|
| 97: | public function colorNoise_fun(&$r, &$g, &$b, $amount)
|
| 98: | {
|
| 99: | $r = static::byte($r + mt_rand(0, $amount) - ($amount >> 1) );
|
| 100: | $g = static::byte($g + mt_rand(0, $amount) - ($amount >> 1) );
|
| 101: | $b = static::byte($b + mt_rand(0, $amount) - ($amount >> 1) );
|
| 102: | }
|
| 103: |
|
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: |
|
| 113: | public function monoNoise_fun(&$r, &$g, &$b, $amount)
|
| 114: | {
|
| 115: | $rand = mt_rand(0, $amount) - ($amount >> 1);
|
| 116: |
|
| 117: | $r = static::byte($r + $rand);
|
| 118: | $g = static::byte($g + $rand);
|
| 119: | $b = static::byte($b + $rand);
|
| 120: | }
|
| 121: |
|
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: |
|
| 131: | public function saltPepperNoise_fun(&$r, &$g, &$b, $amount)
|
| 132: | {
|
| 133: | if (mt_rand(0, 255 - $amount) != 0) {
|
| 134: | return;
|
| 135: | }
|
| 136: |
|
| 137: | $rand = mt_rand(0, 1);
|
| 138: |
|
| 139: | switch ($rand) {
|
| 140: | case 0:
|
| 141: | $r = $g = $b = 0;
|
| 142: | break;
|
| 143: | case 1:
|
| 144: | $r = $g = $b = 255;
|
| 145: | break;
|
| 146: | }
|
| 147: | }
|
| 148: |
|
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: |
|
| 155: | public function byte($b)
|
| 156: | {
|
| 157: | if ($b > 255) {
|
| 158: | return 255;
|
| 159: | }
|
| 160: |
|
| 161: | if ($b < 0) {
|
| 162: | return 0;
|
| 163: | }
|
| 164: |
|
| 165: | return (int) $b;
|
| 166: | }
|
| 167: | }
|
| 168: | |