<commit_before><?php

define('__GRAY_LVL',127); //http://doc.exyks.org/wiki/Php:Classes_constants_are_crap
define('__COLOR_GRAY',  imgs::coloralpha(__GRAY_LVL, __GRAY_LVL, __GRAY_LVL));
define('__COLOR_WHITE', imgs::coloralpha(255, 255, 255));


class imgs {
  const GRAY_LVL    = __GRAY_LVL;
  const COLOR_GRAY  = __COLOR_GRAY;
  const COLOR_WHITE = __COLOR_WHITE;

  static function imageoutstring($img, $func ) { //= "imagepng", doit
    ob_start();
    $func($img);
    $str = ob_get_contents();
    ob_end_clean();
    return $str;
  }

  static function dechex24b($str){
    return substr("000000".dechex($str),-6);
  }

  static function imagetruecolortopalette($img, $palette = array(), $bg = 0){
    list($dx, $dy) = array(imagesx($img), imagesy($img));
    list($img_out, $img_palette) = self::imagecreatewithpalette($dx, $dy, $palette, $bg);

    for($x=0;$x<$dx;$x++)
      for($y=0;$y<$dy;$y++) {
        $color        = imagecolorat($img, $x, $y);
        $color_index  = array_search($color, $img_palette);
        if(!$color_index) continue;
        imagesetpixel($img_out, $x, $y, $color_index);
    }

    return $img_out;
  }

  static function colorswap($img, $colors){
    $palette = self::retrievepalette($img);
    if(!$palette)
      return self::colorswaptruecolor($img, $colors);

    foreach($palette as $i=>$color) {
      if(!isset($colors[$color])) continue;
      $color_new = self::colordec($colors[$color]);
      imagecolorset($img, $i, $color_new['red'], $color_new['green'], $color_new['blue']);
    }
  }

  static function colorswaptruecolor($img, $colors) {
    list($dx, $dy) = array(imagesx($img), imagesy($img));
    for($x=0;$x<$dx;$x++)
      for($y=0;$y<$dy;$y++) {
        $color        = imagecolorat($img, $x, $y);
        if(!($color_new = $colors[$color])) continue;
        imagesetpixel($img, $x, $y, $color_new);    
    }
  }

  static function imagecreatewithpalette($dx, $dy, $palette, $bg = 0){
    $img     = imagecreate($dx, $dy);
    $img_pal = array();
    $img_pal[self::imagecoloradd($img, $bg)] = $bg;
    foreach($palette as $color)
      $img_pal[self::imagecoloradd($img, $color)] = $color;
    return array($img, $img_pal);
  }

  static function imagecoloradd($img, $color){
    $color  = self::colordec($color);
    $index = imagecolorallocate($img, $color['red'], $color['green'], $color['blue']);
    return $index;
  }


  static function retrievepalette($img){
    $colors  = imagecolorstotal($img);
    $palette = array();
    for($i=0;$i<$colors;$i++)
      $palette[$i] = self::colorget(imagecolorsforindex($img, $i));
    return $palette;
  }

  static function coloralpha($r=255,$g=255,$b=255,$alpha=0){
    return ($alpha<<24)+($r<<16)+($g<<8)+$b;
  }

  static function colorget($c){
    return self::coloralpha((int)$c['red'],(int)$c['green'],(int)$c['blue'],(int)$c['alpha']);
  }

  static function colordec($c){
    return array(
      'alpha'=>($c>>24)&0x7F,
      'red'=>($c>>16)&0xFF,
      'green'=>($c>>8)&0xFF,
      'blue'=>$c&0xFF
    );
  }

  static function gray($c){
    return self::alphablend(self::colordec($c));
    return  floor(((($c>>16)&0xFF) + (($c>>8)&0xFF) + ($c&0xFF))/3);
  }

  static function alphablend($color, $bg=255){
    return min(floor((($color['alpha'])/127)*$bg+((127-$color['alpha'])/127)*$color['red']),255);
  }

/*
    Calcule la couleur resultante de la superposition de deux autres, en supportant leur canal alpha
    Ca rouske.
*/
  static function colorfusion($dest,$mask){
    //en.wikipedia.org/wiki/Alpha_transparency
    $aa=(127-$mask['alpha'])/127;$ab=(127-$dest['alpha'])/127;
    $na=($aa+$ab*(1-$aa));

    return array(
        'alpha'=> 127-$na*127,
        'red'=> (int)($na?($aa*$mask['red']+(1-$aa)*$ab*$dest['red'])/$na:0),
        'green'=> (int)($na?($aa*$mask['green']+(1-$aa)*$ab*$dest['green'])/$na:0),
        'blue'=> (int)($na?($aa*$mask['blue']+(1-$aa)*$ab*$dest['blue'])/$na:0),
    );
  }

  static function colorsblend($colors_set){
    $shares = 0;
    $red = $green = $blue = 0;

    foreach($colors_set as $color_set) {
        list($color, $share) = array(self::colordec($color_set[0]), $color_set[1]);
        if($share <= 0) continue;

        $red   += $share * $color['red'];
        $green += $share * $color['green'];
        $blue  += $share * $color['blue'];
        $shares += $share;
    }
    
    $red   = round($red / $shares);
    $green = round($green / $shares);
    $blue  = round($blue / $shares);
    return self::colorget(compact('red', 'green', 'blue'));
  }

 /* return the color 'value' (from 0 to 1), based on gray level & considering alpha
    use this for setting alpha level :   (1-self::colorvalue($color))*127;
    or for setting a gray level :        self::colorvalue($color) * 255
 */
  static function colorvalue($color){
    $color = self::colordec($color);
    $color_level = (255-self::colorgray($color))/255;
    return ((127-$color['alpha'])/127)*$color_level;
  }


/*
    There is no one "correct" conversion from RGB to grayscale, since it depends on the sensitivity response curve of your detector to light as a function of wavelength. A common one in use is:
    Y = 0.3*R + 0.59*G + 0.11*B
*/

  static function colorgray($color){
    return (int)$color['red']*0.3+$color['green']*0.59+$color['blue']*0.11;
  }


  static function imagecross($img, $cx,$cy, $ray = 10, $color = 0) {
    imageline($img, $cx-$ray, $cy, $cx+$ray, $cy, $color);
    imageline($img, $cx, $cy-$ray, $cx, $cy+$ray, $color);
    imageellipse ($img, $cx , $cy , $ray, $ray, $color );
  }


/** Create a new image, respecting alpha channels */
  static function imagecreatetruealpha($w,$h){
    $img = imagecreatetruecolor($w,$h);
    imagealphablending($img,false);
    imagesavealpha($img,true);
    imagefill($img, 0, 0, self::coloralpha(255,255,255,127));
    return $img;
  }

/** Create a new image from a file, regarless its extension */
  static function imagecreatefromfile($file){
    $img_supported_ext=array('png'=>'png','jpg'=>'jpeg','gif'=>'gif','jpeg'=>'jpeg');
    if(!$ext=$img_supported_ext[trim(strtolower(strrchr($file,'.')),'.')]) return false;
    $func="imagecreatefrom$ext";
    if(!($img=@$func($file))) return false;
    imagealphablending($img,false);
    imagesavealpha($img,true);
    return $img;
  }

  static function imagecolorize($img, $color){
    $w = imagesx($img); $h = imagesy($img);<commit_msg>Force ext<commit_after><?php

define('__GRAY_LVL',127); //http://doc.exyks.org/wiki/Php:Classes_constants_are_crap
define('__COLOR_GRAY',  imgs::coloralpha(__GRAY_LVL, __GRAY_LVL, __GRAY_LVL));
define('__COLOR_WHITE', imgs::coloralpha(255, 255, 255));


class imgs {
  const GRAY_LVL    = __GRAY_LVL;
  const COLOR_GRAY  = __COLOR_GRAY;
  const COLOR_WHITE = __COLOR_WHITE;

  static function imageoutstring($img, $func ) { //= "imagepng", doit
    ob_start();
    $func($img);
    $str = ob_get_contents();
    ob_end_clean();
    return $str;
  }

  static function dechex24b($str){
    return substr("000000".dechex($str),-6);
  }

  static function imagetruecolortopalette($img, $palette = array(), $bg = 0){
    list($dx, $dy) = array(imagesx($img), imagesy($img));
    list($img_out, $img_palette) = self::imagecreatewithpalette($dx, $dy, $palette, $bg);

    for($x=0;$x<$dx;$x++)
      for($y=0;$y<$dy;$y++) {
        $color        = imagecolorat($img, $x, $y);
        $color_index  = array_search($color, $img_palette);
        if(!$color_index) continue;
        imagesetpixel($img_out, $x, $y, $color_index);
    }

    return $img_out;
  }

  static function colorswap($img, $colors){
    $palette = self::retrievepalette($img);
    if(!$palette)
      return self::colorswaptruecolor($img, $colors);

    foreach($palette as $i=>$color) {
      if(!isset($colors[$color])) continue;
      $color_new = self::colordec($colors[$color]);
      imagecolorset($img, $i, $color_new['red'], $color_new['green'], $color_new['blue']);
    }
  }

  static function colorswaptruecolor($img, $colors) {
    list($dx, $dy) = array(imagesx($img), imagesy($img));
    for($x=0;$x<$dx;$x++)
      for($y=0;$y<$dy;$y++) {
        $color        = imagecolorat($img, $x, $y);
        if(!($color_new = $colors[$color])) continue;
        imagesetpixel($img, $x, $y, $color_new);    
    }
  }

  static function imagecreatewithpalette($dx, $dy, $palette, $bg = 0){
    $img     = imagecreate($dx, $dy);
    $img_pal = array();
    $img_pal[self::imagecoloradd($img, $bg)] = $bg;
    foreach($palette as $color)
      $img_pal[self::imagecoloradd($img, $color)] = $color;
    return array($img, $img_pal);
  }

  static function imagecoloradd($img, $color){
    $color  = self::colordec($color);
    $index = imagecolorallocate($img, $color['red'], $color['green'], $color['blue']);
    return $index;
  }


  static function retrievepalette($img){
    $colors  = imagecolorstotal($img);
    $palette = array();
    for($i=0;$i<$colors;$i++)
      $palette[$i] = self::colorget(imagecolorsforindex($img, $i));
    return $palette;
  }

  static function coloralpha($r=255,$g=255,$b=255,$alpha=0){
    return ($alpha<<24)+($r<<16)+($g<<8)+$b;
  }

  static function colorget($c){
    return self::coloralpha((int)$c['red'],(int)$c['green'],(int)$c['blue'],(int)$c['alpha']);
  }

  static function colordec($c){
    return array(
      'alpha'=>($c>>24)&0x7F,
      'red'=>($c>>16)&0xFF,
      'green'=>($c>>8)&0xFF,
      'blue'=>$c&0xFF
    );
  }

  static function gray($c){
    return self::alphablend(self::colordec($c));
    return  floor(((($c>>16)&0xFF) + (($c>>8)&0xFF) + ($c&0xFF))/3);
  }

  static function alphablend($color, $bg=255){
    return min(floor((($color['alpha'])/127)*$bg+((127-$color['alpha'])/127)*$color['red']),255);
  }

/*
    Calcule la couleur resultante de la superposition de deux autres, en supportant leur canal alpha
    Ca rouske.
*/
  static function colorfusion($dest,$mask){
    //en.wikipedia.org/wiki/Alpha_transparency
    $aa=(127-$mask['alpha'])/127;$ab=(127-$dest['alpha'])/127;
    $na=($aa+$ab*(1-$aa));

    return array(
        'alpha'=> 127-$na*127,
        'red'=> (int)($na?($aa*$mask['red']+(1-$aa)*$ab*$dest['red'])/$na:0),
        'green'=> (int)($na?($aa*$mask['green']+(1-$aa)*$ab*$dest['green'])/$na:0),
        'blue'=> (int)($na?($aa*$mask['blue']+(1-$aa)*$ab*$dest['blue'])/$na:0),
    );
  }

  static function colorsblend($colors_set){
    $shares = 0;
    $red = $green = $blue = 0;

    foreach($colors_set as $color_set) {
        list($color, $share) = array(self::colordec($color_set[0]), $color_set[1]);
        if($share <= 0) continue;

        $red   += $share * $color['red'];
        $green += $share * $color['green'];
        $blue  += $share * $color['blue'];
        $shares += $share;
    }
    
    $red   = round($red / $shares);
    $green = round($green / $shares);
    $blue  = round($blue / $shares);
    return self::colorget(compact('red', 'green', 'blue'));
  }

 /* return the color 'value' (from 0 to 1), based on gray level & considering alpha
    use this for setting alpha level :   (1-self::colorvalue($color))*127;
    or for setting a gray level :        self::colorvalue($color) * 255
 */
  static function colorvalue($color){
    $color = self::colordec($color);
    $color_level = (255-self::colorgray($color))/255;
    return ((127-$color['alpha'])/127)*$color_level;
  }


/*
    There is no one "correct" conversion from RGB to grayscale, since it depends on the sensitivity response curve of your detector to light as a function of wavelength. A common one in use is:
    Y = 0.3*R + 0.59*G + 0.11*B
*/

  static function colorgray($color){
    return (int)$color['red']*0.3+$color['green']*0.59+$color['blue']*0.11;
  }


  static function imagecross($img, $cx,$cy, $ray = 10, $color = 0) {
    imageline($img, $cx-$ray, $cy, $cx+$ray, $cy, $color);
    imageline($img, $cx, $cy-$ray, $cx, $cy+$ray, $color);
    imageellipse ($img, $cx , $cy , $ray, $ray, $color );
  }


/** Create a new image, respecting alpha channels */
  static function imagecreatetruealpha($w,$h){
    $img = imagecreatetruecolor($w,$h);
    imagealphablending($img,false);
    imagesavealpha($img,true);
    imagefill($img, 0, 0, self::coloralpha(255,255,255,127));
    return $img;
  }

/** Create a new image from a file, regarless its extension */
  static function imagecreatefromfile($file, $force_ext = false){
    $img_supported_ext=array('png'=>'png','jpg'=>'jpeg','gif'=>'gif','jpeg'=>'jpeg');
    $ext = pick($force_ext, trim(strtolower(strrchr($file,'.')),'.'));
    if(!$ext=$img_supported_ext[$ext]) return false;
    $func="imagecreatefrom$ext";
    if(!($img=@$func($file))) return false;
    imagealphablending($img,false);
    imagesavealpha($img,true);
    return $img;
  }

  static function imagecolorize($img, $color){
    $w = imagesx($img); $h = imagesy($img);
{'fraction_non_alphanumeric': 0.21358091834356305, 'fraction_numerical': 0.024706704995726827, 'mean_word_length': 3.9336910693752394, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1634804', 'n_tokens_mistral': 4916, 'n_tokens_neox': 4652, 'n_words': 881}