Ignore:
Timestamp:
May 13, 2010, 10:21:05 PM (14 years ago)
Author:
grum
Message:

Improve algorythm for colors analysis + use GPCRequestBuilder interface instead of a local interface

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/ColorStat/cstat_root.class.inc.php

    r6107 r6176  
    2424  class CStat_root extends CommonPlugin
    2525  {
     26    static public $pluginName='ColorStat';
     27    static public $pluginNameFile='cstat';
     28    static public $pluginTables=array('images', 'color_table', 'images_colors');
     29    static public $colorTableSize=Array('small' => Array(30,20), 'large' => Array(10,10));
     30
    2631    protected $css;   //the css object
    27     protected $colorTableSize=Array('small' => Array(30,20), 'large' => Array(10,10));
    2832
    2933    public function __construct($prefixeTable, $filelocation)
    3034    {
    31       $this->setPluginName('ColorStat');
    32       $this->setPluginNameFiles("cstat");
     35      $this->setPluginName(self::$pluginName);
     36      $this->setPluginNameFiles(self::$pluginNameFile);
    3337      parent::__construct($prefixeTable, $filelocation);
    3438      $this->section_name=$this->getPluginNameFiles();
    3539
    36       $this->setTablesList(array('images', 'color_table', 'images_colors'));
     40      $this->setTablesList(self::$pluginTables);
    3741
    3842
     
    6771      $this->config=array(
    6872        'newInstall' => 'n',
    69         'analyze_maxTime' => 0.1,
     73        'analyze_maxTime' => 1,
    7074        'analyze_colorTable' => 'small',
    7175        'analyze_pps' => 0,
     
    7377        'display_gallery_showColors' => 'n',
    7478        'display_stat_orderType' => 'img',
    75         'stat_minPct' => 3.5,
    76       );
     79        'stat_minPct' => 1.5,
     80      );
     81    }
     82
     83    /* ---------------------------------------------------------------------------
     84      ajax functions
     85    --------------------------------------------------------------------------- */
     86
     87
     88  } //class
     89
     90
     91  class CStat_functions
     92  {
     93    static private $tables = Array();
     94    static private $config = Array();
     95
     96    /**
     97     * initialise the class
     98     *
     99     * @param String $prefixeTable : the piwigo prefixe used on tables name
     100     * @param String $pluginNameFile : the plugin name used for tables name
     101     */
     102    static public function init($prefixeTable)
     103    {
     104      GPCCore::loadConfig(CStat_root::$pluginNameFile, self::$config);
     105      $list=CStat_root::$pluginTables;
     106
     107      for($i=0;$i<count($list);$i++)
     108      {
     109        self::$tables[$list[$i]]=$prefixeTable.CStat_root::$pluginNameFile.'_'.$list[$i];
     110      }
     111    }
     112
     113    /**
     114     * return a color table with stat on color
     115     *
     116     * @return Array : a color table with statistics on colors
     117     */
     118    static public function getColorTableWithStat()
     119    {
     120      $generalStats=self::getGeneralStats();
     121
     122      $colors=Array();
     123      $sql="SELECT color_id, num_images, num_pixels
     124            FROM ".self::$tables['color_table']."
     125            WHERE num_images > 0
     126            ORDER BY color_id ";
     127      $result=pwg_query($sql);
     128      if($result)
     129      {
     130        while($row=pwg_db_fetch_assoc($result))
     131        {
     132          $colors[$row['color_id']]=Array('num_images' => $row['num_images'], 'num_pixels' => $row['num_pixels']);
     133        }
     134      }
     135
     136      $colorTable=ColorStat::getColorTable(
     137       CStat_root::$colorTableSize[self::$config['analyze_colorTable']][0],
     138       CStat_root::$colorTableSize[self::$config['analyze_colorTable']][1]
     139      );
     140
     141      foreach($colorTable as $key=>$hue)
     142      {
     143        foreach($hue as $key2=>$saturation)
     144        {
     145          foreach($saturation as $key3=>$value)
     146          {
     147            $rgb=$value->getRGB()->getHexString();
     148            $colorTable[$key][$key2][$key3]=Array(
     149              'color' => $rgb,
     150              'pct'   => '', //(array_key_exists($rgb, $colors))?sprintf("%.2f", round(100*$colors[$rgb]['num_pixels']/$generalStats['pixelsAnalyzedSum'],2)):"",
     151              'num'   => (array_key_exists($rgb, $colors))?$colors[$rgb]['num_images']:"",
     152            );
     153          }
     154        }
     155      }
     156
     157      unset($colors);
     158      return($colorTable);
    77159    }
    78160
     
    86168     * @return String : HTML code
    87169     */
    88     public function htmlColorTable($colorTable, $size=5, $id="", $class="")
     170    static public function htmlColorTable($colorTable, $size=5, $id="", $class="", $br='<br>')
    89171    {
    90172      global $template;
    91173
    92174      $template->set_filename('color_table_page',
    93                     dirname($this->getFileLocation()).'/templates/cstat_color_table.tpl');
    94 
     175                    dirname(__FILE__).'/templates/cstat_color_table.tpl');
     176
     177      switch(count($colorTable))
     178      {
     179        case 12;
     180          $break=4;
     181          $size=10;
     182          break;
     183        case 15;
     184          $break=5;
     185          $size=10;
     186          break;
     187        case 18;
     188          $break=6;
     189          $size=5;
     190          break;
     191        case 20;
     192          $break=5;
     193          $size=10;
     194          break;
     195        case 24;
     196          $break=6;
     197          $size=5;
     198          break;
     199        case 30;
     200          $break=6;
     201          $size=5;
     202          break;
     203        case 36;
     204          $break=6;
     205          $size=5;
     206          break;
     207        case 45;
     208          $break=9;
     209          $size=3;
     210          break;
     211      }
     212
     213      $colors=Array();
    95214      $nbHue=count($colorTable);
    96215      $nbStep=count($colorTable[0]);
    97       $colors=Array();
    98       for($step=0;$step<$nbStep;$step++)
    99       {
    100         $row=Array();
    101         for($hue=0;$hue<$nbHue;$hue++)
    102         {
    103 
    104           if($colorTable[$hue][$step] instanceof HSV)
     216      $col=0;
     217      $row=0;
     218      foreach($colorTable as $key => $hue)
     219      {
     220        $lrow=0;
     221        foreach($hue as $key2 => $saturation)
     222        {
     223          $lcol=0;
     224          foreach($saturation as $key3 => $value)
    105225          {
    106             $row[]=Array('color' => $colorTable[$hue][$step]->getRGB()->getHexString(), 'pct' => "", 'num' => "");
     226            //if(!($col!=0 and $hsv['V']==0) and !($row!=0 and $hsv['S']==0))
     227            $trow=$lcol+$col*$nbStep;
     228            $tcol=$lrow+$row*$nbStep;
     229
     230            if($colorTable[$key][$key2][$key3] instanceof HSV)
     231            {
     232              $colors[$tcol][$trow]=Array('color' => $colorTable[$key][$key2][$key3]->getRGB()->getHexString(), 'pct' => "", 'num' => "");
     233            }
     234            elseif(is_array($colorTable[$key][$key2][$key3]))
     235            {
     236              $colors[$tcol][$trow]=Array(
     237                'color' => (array_key_exists('color', $colorTable[$key][$key2][$key3]))?$colorTable[$key][$key2][$key3]['color']:"",
     238                'pct' => (array_key_exists('pct', $colorTable[$key][$key2][$key3]))?$colorTable[$key][$key2][$key3]['pct']:"",
     239                'num' => (array_key_exists('num', $colorTable[$key][$key2][$key3]))?$colorTable[$key][$key2][$key3]['num']:"",
     240              );
     241            }
     242            else
     243            {
     244              $colors[$tcol][$trow]=Array('color' => $colorTable[$key][$key2][$key3], 'pct' => "", 'num' => "");
     245            }
     246            $lcol++;
    107247          }
    108           elseif(is_array($colorTable[$hue][$step]))
    109           {
    110             $row[]=Array(
    111               'color' => (array_key_exists('color', $colorTable[$hue][$step]))?$colorTable[$hue][$step]['color']:"",
    112               'pct' => (array_key_exists('pct', $colorTable[$hue][$step]))?$colorTable[$hue][$step]['pct']:"",
    113               'num' => (array_key_exists('num', $colorTable[$hue][$step]))?$colorTable[$hue][$step]['num']:"",
    114             );
    115           }
    116           else
    117           {
    118             $row[]=Array('color' => $colorTable[$hue][$step], 'pct' => "", 'num' => "");
    119           }
    120         }
    121         $colors[]=$row;
    122         unset($row);
    123       }
    124 
    125       $data=array(
    126         'colorTable' => $colors,
    127         'size' => $size,
    128         'id' => $id,
    129         'class' => $class
    130       );
    131 
    132       $template->assign('data', $data);
    133       unset($data);
    134 
    135       return($template->parse('color_table_page', true));
    136     }
    137 
    138     /**
    139      * return HTML code for a given colors list
    140      *
    141      * @param Array $colors : list of colors
    142      * @param Int $size     : size for colorbox in the HTML table
    143      * @return String : HTML code
    144      */
    145     public function htmlColorList($colorList, $split=8, $size=5, $id="", $class="", $br='<br>')
    146     {
    147       global $template;
    148 
    149       $template->set_filename('color_table_page',
    150                     dirname($this->getFileLocation()).'/templates/cstat_color_table.tpl');
    151 
    152       $colors=Array();
    153 
    154       $row=0;
    155       $num=0;
    156       foreach($colorList as $key => $val)
    157       {
    158         $colors[$row][]=Array('color' => $key, 'pct' => $val['pct'], 'num' => "");
    159         $num++;
    160         if($num==$split)
    161         {
    162           $num=0;
     248          $lrow++;
     249        }
     250
     251        $col++;
     252        if($col==$break)
     253        {
     254          $col=0;
    163255          $row++;
    164256        }
     
    180272
    181273    /**
     274     * return HTML code for a given colors list
     275     *
     276     * @param Array $colors : list of colors
     277     * @param Int $size     : size for colorbox in the HTML table
     278     * @return String : HTML code
     279     */
     280    static public function htmlColorList($colorList, $split=8, $size=5, $id="", $class="", $br='<br>')
     281    {
     282      global $template;
     283
     284      $template->set_filename('color_table_page',
     285                    dirname(__FILE__).'/templates/cstat_color_table.tpl');
     286
     287      $colors=Array();
     288
     289      $row=0;
     290      $num=0;
     291      foreach($colorList as $key => $val)
     292      {
     293        $colors[$row][]=Array('color' => $key, 'pct' => $val['pct'], 'num' => "");
     294        $num++;
     295        if($num==$split)
     296        {
     297          $num=0;
     298          $row++;
     299        }
     300      }
     301
     302      $data=array(
     303        'colorTable' => $colors,
     304        'size' => $size,
     305        'id' => $id,
     306        'class' => $class,
     307        'br' => $br,
     308      );
     309
     310      $template->assign('data', $data);
     311      unset($data);
     312
     313      return($template->parse('color_table_page', true));
     314    }
     315
     316    /**
    182317     * returns an array of colors & colors percent of an image
    183318     *
     
    185320     * @return Array('colors' => Array(), 'colors_pct' => Array())
    186321     */
    187     public function getImageColors($imageId)
     322    static public function getImageColors($imageId)
    188323    {
    189324      $returned=Array(
     
    193328
    194329      $sql="SELECT colors, colors_pct
    195             FROM ".$this->tables['images']."
     330            FROM ".self::$tables['images']."
    196331            WHERE image_id='".$imageId."'";
    197332      $result=pwg_query($sql);
     
    208343
    209344
    210 
    211345    /**
    212346     *  return all HTML&JS code necessary to display a dialogbox to choose
    213347     *  colors
    214348     */
    215     protected function dialogBoxColor()
     349    static public function dialogBoxColor()
    216350    {
    217351      global $template;
    218352
    219353      $template->set_filename('colors_choose',
    220                     dirname($this->getFileLocation()).'/templates/cstat_dialog_colors_choose.tpl');
    221 
    222       $colorTable=$this->getColorTableWithStat();
     354                    dirname(__FILE__).'/templates/cstat_dialog_colors_choose.tpl');
     355
     356      $colorTable=CStat_functions::getColorTableWithStat();
    223357
    224358      $datas=Array(
    225         'colorTable' => $this->htmlColorTable(
     359        'colorTable' => CStat_functions::htmlColorTable(
    226360                          $colorTable,
    227                           ($this->config['analyze_colorTable']=='small')?16:8,
     361                          (self::$config['analyze_colorTable']=='small')?16:8,
    228362                          "",
    229363                          "color0px"
     
    240374
    241375    /**
    242      * return a color table with stat on color
    243      *
    244      * @return Array : a color table with statistics on colors
    245      */
    246     protected function getColorTableWithStat()
    247     {
    248       $generalStats=$this->getGeneralStats();
    249 
    250       $colors=Array();
    251       $sql="SELECT color_id, num_images, num_pixels
    252             FROM ".$this->tables['color_table']."
    253             WHERE num_images > 0
    254             ORDER BY color_id ";
     376     * returns general stats on the analyzed colors process
     377     *
     378     * @return Array : array with keys
     379     *                  'nbImages',  'timeMax',  'timeMin', 'timeSum',
     380     *                  'pixelsAnalyzedMax', 'pixelsAnalyzedMin',
     381     *                  'pixelsAnalyzedAvg', 'pixelsAnalyzedSum', 'totalPixels',
     382     *                  'ppsMax', 'ppsMin', 'ppsAvg', 'qualityMax',
     383     *                  'qualityMin', 'qualityAvg'
     384     */
     385    static public function getGeneralStats()
     386    {
     387      $returned=Array(
     388        'nbImages' => 0,
     389        'timeMax' => 0,
     390        'timeMin' => 0,
     391        'timeSum' => 0,
     392        'pixelsAnalyzedMax' => 0,
     393        'pixelsAnalyzedMin' => 0,
     394        'pixelsAnalyzedAvg' => 0,
     395        'pixelsAnalyzedSum' => 0,
     396        'totalPixels' => 0,
     397        'ppsMax' => 0,
     398        'ppsMin' => 0,
     399        'ppsAvg' => 0,
     400        'qualityMax' => 0,
     401        'qualityMin' => 0,
     402        'qualityAvg' => 0,
     403      );
     404      $sql="SELECT COUNT(image_id) AS nbImages,
     405                   MAX(time) AS timeMax,
     406                   MIN(time) AS timeMin,
     407                   SUM(time) AS timeSum,
     408                   MAX(analyzed_pixels) AS pixelsAnalyzedMax,
     409                   MIN(analyzed_pixels) AS pixelsAnalyzedMin,
     410                   AVG(analyzed_pixels) AS pixelsAnalyzedAvg,
     411                   SUM(analyzed_pixels) AS pixelsAnalyzedSum,
     412                   SUM(num_pixels) AS totalPixels,
     413                   MAX(pps) AS ppsMax,
     414                   MIN(pps) AS ppsMin,
     415                   AVG(pps) AS ppsAvg,
     416                   MAX(quality) AS qualityMax,
     417                   MIN(quality) AS qualityMin,
     418                   AVG(quality) AS qualityAvg
     419            FROM ".self::$tables['images']."
     420            WHERE analyzed='y';";
    255421      $result=pwg_query($sql);
    256422      if($result)
     
    258424        while($row=pwg_db_fetch_assoc($result))
    259425        {
    260           $colors[$row['color_id']]=Array('num_images' => $row['num_images'], 'num_pixels' => $row['num_pixels']);
    261         }
    262       }
    263 
    264       $colorTable=ColorStat::getColorTable(
    265         $this->colorTableSize[$this->config['analyze_colorTable']][0],
    266         $this->colorTableSize[$this->config['analyze_colorTable']][1]
    267       );
    268       foreach($colorTable as $key=>$val)
    269       {
    270         foreach($val as $key2=>$val2)
    271         {
    272           $rgb=$val2->getRGB()->getHexString();
    273           $colorTable[$key][$key2]=Array(
    274             'color' => $rgb,
    275             'pct'   => '', //(array_key_exists($rgb, $colors))?sprintf("%.2f", round(100*$colors[$rgb]['num_pixels']/$generalStats['pixelsAnalyzedSum'],2)):"",
    276             'num'   => (array_key_exists($rgb, $colors))?$colors[$rgb]['num_images']:"",
    277           );
    278         }
    279       }
    280 
    281       unset($colors);
    282       return($colorTable);
    283     }
    284 
    285 
    286 
    287 
    288 
    289     /* ---------------------------------------------------------------------------
    290       ajax functions
    291     --------------------------------------------------------------------------- */
    292 
    293 
    294   } //class
     426          $returned=$row;
     427        }
     428      }
     429      return($returned);
     430    }
     431
     432  } //CStat_functions
    295433
    296434?>
Note: See TracChangeset for help on using the changeset viewer.