[ create a new paste ] login | about

Link: http://codepad.org/xCQpBPC3    [ raw code | fork ]

PHP, pasted on Feb 6:
<?php

function filterSub($bytes) {
  $out = [1];
  for($i = 0; $i < count($bytes); $i++) {
    $out[] = ($bytes[$i] - $bytes[$i-1]) & 0xFF;
  }
  return $out;
}

function filterUp($bytes, $prior) {
  $out = [2];
  for($i = 0; $i < count($bytes); $i++) {
    $out[] = ($bytes[$i] - $prior[$i]) & 0xFF;
  }
  return $out;
}

function filterAvg($bytes, $prior) {
  $out = [3];
  for($i = 0; $i < count($bytes); $i++) {
    $out[] = ceil($bytes[$i] - ($bytes[$i-1] + $prior[$i])/2) & 0xFF;
  }
  return $out;
}

function paethPredictor($a, $b, $c) {
  // $a => left; $b => above; $c => above left
  $p = $a + $b - $c;
  $pa = abs($p - $a);
  $pb = abs($p - $b);
  $pc = abs($p - $c);
  if($pa <= $pb && $pa <= $pc) {
    return $a;
  } else if($pb <= $pc) {
    return $b;
  } else {
    return $c;
  }
}

function filterPaeth($bytes, $prior) {
  $out = [4];
  for($i = 0; $i < count($bytes); $i++) {
    $out[] = ($bytes[$i] - paethPredictor($bytes[$i-1], $prior[$i], $prior[$i-1])) & 0xFF;
  }
  return $out;
}


Create a new paste based on this one


Comments: