function image_decal_couleur_127($coul, $val)
{
    include_spip('filtres/images_lib');
    return _image_decale_composante_127($coul, $val);
}
function image_sepia($im, $rgb = "896f5e")
{
    include_spip('filtres/images_lib');
    if (!function_exists("imagecreatetruecolor")) {
        return $im;
    }
    $couleurs = _couleur_hex_to_dec($rgb);
    $dr = $couleurs["red"];
    $dv = $couleurs["green"];
    $db = $couleurs["blue"];
    $fonction = array('image_sepia', func_get_args());
    $image = _image_valeurs_trans($im, "sepia-{$dr}-{$dv}-{$db}", false, $fonction);
    if (!$image) {
        return "";
    }
    $x_i = $image["largeur"];
    $y_i = $image["hauteur"];
    $im = $image["fichier"];
    $dest = $image["fichier_dest"];
    $creer = $image["creer"];
    if ($creer) {
        // Creation de l'image en deux temps
        // de facon a conserver les GIF transparents
        $im = $image["fonction_imagecreatefrom"]($im);
        imagepalettetotruecolor($im);
        $im_ = imagecreatetruecolor($x_i, $y_i);
        @imagealphablending($im_, false);
        @imagesavealpha($im_, true);
        $color_t = ImageColorAllocateAlpha($im_, 255, 255, 255, 127);
        imagefill($im_, 0, 0, $color_t);
        imagecopy($im_, $im, 0, 0, 0, 0, $x_i, $y_i);
        for ($x = 0; $x < $x_i; $x++) {
            for ($y = 0; $y < $y_i; $y++) {
                $rgb = ImageColorAt($im_, $x, $y);
                $a = $rgb >> 24 & 0xff;
                $r = $rgb >> 16 & 0xff;
                $g = $rgb >> 8 & 0xff;
                $b = $rgb & 0xff;
                $r = round(0.299 * $r + 0.587 * $g + 0.114 * $b);
                $g = $r;
                $b = $r;
                $r = _image_decale_composante_127($r, $dr);
                $g = _image_decale_composante_127($g, $dv);
                $b = _image_decale_composante_127($b, $db);
                $color = ImageColorAllocateAlpha($im_, $r, $g, $b, $a);
                imagesetpixel($im_, $x, $y, $color);
            }
        }
        _image_gd_output($im_, $image);
        imagedestroy($im_);
        imagedestroy($im);
    }
    return _image_ecrire_tag($image, array('src' => $dest));
}