source-class-Com.Tecnick.Barcode.Type.Square.Datamatrix.ErrorCorrection

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
Overview

Classes

Exceptions

  1: <?php
  2: /**
  3:  * ErrorCorrection.php
  4:  *
  5:  * @since       2015-02-21
  6:  * @category    Library
  7:  * @package     Barcode
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2010-2016 Nicola Asuni - Tecnick.com LTD
 10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 11:  * @link        https://github.com/tecnickcom/tc-lib-barcode
 12:  *
 13:  * This file is part of tc-lib-barcode software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\Barcode\Type\Square\Datamatrix;
 17: 
 18: /**
 19:  * Com\Tecnick\Barcode\Type\Square\Datamatrix\ErrorCorrection
 20:  *
 21:  * Error correction methods and other utilities for Datamatrix Barcode type class
 22:  *
 23:  * @since       2015-02-21
 24:  * @category    Library
 25:  * @package     Barcode
 26:  * @author      Nicola Asuni <info@tecnick.com>
 27:  * @copyright   2010-2016 Nicola Asuni - Tecnick.com LTD
 28:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 29:  * @link        https://github.com/tecnickcom/tc-lib-barcode
 30:  */
 31: class ErrorCorrection
 32: {
 33:     /**
 34:      * Product of two numbers in a Power-of-Two Galois Field
 35:      *
 36:      * @param int   $numa First number to multiply.
 37:      * @param int   $numb Second number to multiply.
 38:      * @param array $log  Log table.
 39:      * @param array $alog Anti-Log table.
 40:      * @param array $ngf  Number of Factors of the Reed-Solomon polynomial.
 41:      *
 42:      * @return int product
 43:      */
 44:     protected function getGFProduct($numa, $numb, $log, $alog, $ngf)
 45:     {
 46:         if (($numa == 0) || ($numb == 0)) {
 47:             return 0;
 48:         }
 49:         return ($alog[($log[$numa] + $log[$numb]) % ($ngf - 1)]);
 50:     }
 51: 
 52:     /**
 53:      * Add error correction codewords to data codewords array (ANNEX E).
 54:      *
 55:      * @param array $wdc Array of datacodewords.
 56:      * @param int   $nbk Number of blocks.
 57:      * @param int   $ncw Number of data codewords per block.
 58:      * @param int   $ncc Number of correction codewords per block.
 59:      * @param int   $ngf Number of fields on log/antilog table (power of 2).
 60:      * @param int   $vpp The value of its prime modulus polynomial (301 for ECC200).
 61:      *
 62:      * @return array data codewords + error codewords
 63:      */
 64:     public function getErrorCorrection($wdc, $nbk, $ncw, $ncc, $ngf = 256, $vpp = 301)
 65:     {
 66:         // generate the log ($log) and antilog ($alog) tables
 67:         $log = array(0);
 68:         $alog = array(1);
 69:         $this->genLogs($log, $alog, $ngf, $vpp);
 70:         
 71:         // generate the polynomial coefficients (c)
 72:         $plc = array_fill(0, ($ncc + 1), 0);
 73:         $plc[0] = 1;
 74:         for ($i = 1; $i <= $ncc; ++$i) {
 75:             $plc[$i] = $plc[($i-1)];
 76:             for ($j = ($i - 1); $j >= 1; --$j) {
 77:                 $plc[$j] = $plc[($j - 1)] ^ $this->getGFProduct($plc[$j], $alog[$i], $log, $alog, $ngf);
 78:             }
 79:             $plc[0] = $this->getGFProduct($plc[0], $alog[$i], $log, $alog, $ngf);
 80:         }
 81:         ksort($plc);
 82:         
 83:         // total number of data codewords
 84:         $num_wd = ($nbk * $ncw);
 85:         // total number of error codewords
 86:         $num_we = ($nbk * $ncc);
 87:         // for each block
 88:         for ($b = 0; $b < $nbk; ++$b) {
 89:             // create interleaved data block
 90:             $block = array();
 91:             for ($n = $b; $n < $num_wd; $n += $nbk) {
 92:                 $block[] = $wdc[$n];
 93:             }
 94:             // initialize error codewords
 95:             $wec = array_fill(0, ($ncc + 1), 0);
 96:             // calculate error correction codewords for this block
 97:             for ($i = 0; $i < $ncw; ++$i) {
 98:                 $ker = ($wec[0] ^ $block[$i]);
 99:                 for ($j = 0; $j < $ncc; ++$j) {
100:                     $wec[$j] = ($wec[($j + 1)] ^ $this->getGFProduct($ker, $plc[($ncc - $j - 1)], $log, $alog, $ngf));
101:                 }
102:             }
103:             // add error codewords at the end of data codewords
104:             $j = 0;
105:             for ($i = $b; $i < $num_we; $i += $nbk) {
106:                 $wdc[($num_wd + $i)] = $wec[$j];
107:                 ++$j;
108:             }
109:         }
110:         // reorder codewords
111:         ksort($wdc);
112:         return $wdc;
113:     }
114: 
115:     /**
116:      * Generate the log ($log) and antilog ($alog) tables
117:      *
118:      * @param array $log  Log table
119:      * @param arrya $alog Anti-Log table
120:      * @param int   $ngf  Number of fields on log/antilog table (power of 2).
121:      * @param int   $vpp  The value of its prime modulus polynomial (301 for ECC200).
122:      */
123:     protected function genLogs(&$log, &$alog, $ngf, $vpp)
124:     {
125:         for ($i = 1; $i < $ngf; ++$i) {
126:             $alog[$i] = ($alog[($i - 1)] * 2);
127:             if ($alog[$i] >= $ngf) {
128:                 $alog[$i] ^= $vpp;
129:             }
130:             $log[$alog[$i]] = $i;
131:         }
132:         ksort($log);
133:     }
134: }
135: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy