5/6/09

Calculando el número de apariciones de un color en una imagen paletizada usando GD.

$i = imagecreate(1000, 1000);
$c1 = imagecolorallocate($i, 0x00, 0x00, 0x00);
 $c2 = imagecolorallocate($i, 0xFF, 0xFF, 0xFF);
 
 imagefill($i, 0, 0, $c2);
 
 for ($n = 0; $n < 100; $n++) imagesetpixel($i, $n, $n, $c1);

 printf("Method1 (iterating using php) {\n");
 {
  $t0 = microtime(true);
  ob_start(); imagegd($i); $data = substr(ob_get_clean(), 0x40D);
  printf("  Output image: %.4f seconds\n", microtime(true) - $t0);

  $t0 = microtime(true);
  $count = array();
  list($w, $h) = array(imageSX($i), imageSY($i));
  for ($y = 0; $y < $h; $y++) {
   for ($x = 0; $x < $w; $x++) {
    @$count[imagecolorat($i, $x, $y)]++;
   }
  }
  $count = count_chars($data, 1);
  arsort($count);
  printf("  Counting: %.4f seconds\n", microtime(true) - $t0);
  print_r($count);
 }
 printf("}\n");
 
 printf("Method2 (hack) {\n");
 {
  $t0 = microtime(true);
  ob_start(); imagegd($i); $data = substr(ob_get_clean(), 0x40D);
  printf("  Output image: %.4f seconds\n", microtime(true) - $t0);

  $t0 = microtime(true);
  $count = count_chars($data, 1);
  arsort($count);
  printf("  Counting: %.4f seconds\n", microtime(true) - $t0);
  print_r($count);
 }
 printf("}\n");

1 comentario:

  1. phpElegant is a term used to describe clean, efficient, and well-organized PHP code that adheres to best practices, emphasizing readability and maintainability. abogado inmobiliario phpElegant may represent a PHP development framework or library that promotes elegant coding solutions, reducing complexity and redundancy in web development projects. Truck Accident Lawyer VA
    phpElegant might be the name of a community or resource dedicated to sharing elegant coding techniques, tips, and examples for PHP programmers, fostering excellence in PHP development.

    ResponderEliminar