Example #1
0
 function Enc($aData, $aDebug = false)
 {
     if ($this->iTilde) {
         $r = tilde_process($aData);
         if ($r === false) {
             $this->iError = -10;
             return false;
         }
         $aData = $r;
     }
     $this->iConv = null;
     $this->iConv = ECC_Factory::Create($this->iErrLevel);
     // Start by splitting the input string to an array
     $data = str_split($aData);
     $ndata = count($data);
     // Automatically select the smallest encodation schema
     $this->iEncodation->AutoSelect($data);
     // Create the output bit array
     $bits = array();
     // Get 5 Prefix bits that specified format id
     $bits = $this->iEncodation->GetPrefix();
     $bidx = 5;
     // Calculate the CRC-CCITT (16 bits) for the original data and add it master bit stream.
     $crc_prefix = array(chr($this->iEncodation->GetCRCPrefix()), chr(0));
     $crc_data = array_merge($crc_prefix, $data);
     $crc = $this->iCRC_CCITT->Get($crc_data);
     $crcbits = array();
     Word2Bits($crc, $crcbits, 16);
     for ($i = 0; $i < 16; ++$i) {
         $bits[$bidx++] = $crcbits[$i];
     }
     // Get data length as a 9 bit sequence bit reversed
     $lenbits = array();
     Word2Bits($ndata, $lenbits, 9);
     $lenbits = array_reverse($lenbits);
     for ($i = 0; $i < 9; ++$i) {
         $bits[$bidx++] = $lenbits[$i];
     }
     // Encode data and copy to master bit stream.
     $databits = array();
     $this->iEncodation->Encode($data, $databits);
     // Number of code words. Each codeword is represented as an array of bits
     $m = count($databits);
     // In preparation to adding to the master bit stream each symbol
     // must first be bit reversed according to the standard
     for ($i = 0; $i < $m; ++$i) {
         $databits[$i] = array_reverse($databits[$i]);
     }
     // Add each code word in its bit-reversed form to the master bit stream
     for ($i = 0; $i < $m; ++$i) {
         $k = count($databits[$i]);
         for ($j = 0; $j < $k; ++$j) {
             $bits[$bidx++] = $databits[$i][$j];
         }
     }
     // Now do the convolutional coding to create the protected bit stream
     $protectedbits = array();
     $this->iConv->_Get($bits, $protectedbits);
     // Now get the header (depends on the ECC chosen)
     $headerbits = $this->iConv->GetHeader();
     // Find out how many trailer bit (set to zero) we need to either
     // a) Make it the smallest possible size of matrix or
     // b) Fill it out to the user specified size of matrix
     $totBits = count($headerbits) + count($protectedbits);
     if ($this->iSize == -1) {
         // Find the smallest possible size to use
         $mat_size = 7;
         $mat_idx = 0;
         while ($mat_size <= 47 && $mat_size * $mat_size < $totBits) {
             $mat_idx++;
             $mat_size += 2;
         }
         if ($mat_size > 47) {
             $this->iError = -31;
             return false;
         }
         $this->iSize = $mat_size;
         $ntrailerbits = $mat_size * $mat_size - $totBits;
     } else {
         // User specified size
         $mat_size = $this->iSize;
         if ($mat_size * $mat_size < $totBits) {
             $this->iError = -31;
             return false;
         }
         $ntrailerbits = $mat_size * $mat_size - $totBits;
         $mat_idx = ($mat_size - 7) / 2;
     }
     $trailerbits = array_fill(0, $ntrailerbits, 0);
     // We now have the final bit stream by concatenating
     // header + protected bit stream + trailer bits
     $bits = array_merge($headerbits, $protectedbits, $trailerbits);
     $ret = $this->iMasterRand->Randomize($bits);
     if ($ret === false) {
         $this->iError = -33;
         return false;
     }
     // Place the bits in the matrice according to the bit placement and
     // add alignment edges to the output matrix
     $outputMatrix = array(array(), array());
     $this->iBitPlacement->Set($mat_idx, $bits, $outputMatrix);
     $pspec = new PrintSpecification(DM_TYPE_140, $data, $outputMatrix, $this->iEncodation->iSelectSchema, $this->iErrLevel);
     return $pspec;
 }
 function Enc($aData, $aDebug = false)
 {
     if ($this->iTilde) {
         $r = tilde_process($aData);
         if ($r === false) {
             $this->iError = -10;
             return false;
         }
         $aData = $r;
     }
     $this->iConv = null;
     $this->iConv = ECC_Factory::Create($this->iErrLevel);
     $data = str_split($aData);
     $ndata = count($data);
     $this->iEncodation->AutoSelect($data);
     $bits = array();
     $bits = $this->iEncodation->GetPrefix();
     $bidx = 5;
     $crc_prefix = array(chr($this->iEncodation->GetCRCPrefix()), chr(0));
     $crc_data = array_merge($crc_prefix, $data);
     $crc = $this->iCRC_CCITT->Get($crc_data);
     $crcbits = array();
     Word2Bits($crc, $crcbits, 16);
     for ($i = 0; $i < 16; ++$i) {
         $bits[$bidx++] = $crcbits[$i];
     }
     $lenbits = array();
     Word2Bits($ndata, $lenbits, 9);
     $lenbits = array_reverse($lenbits);
     for ($i = 0; $i < 9; ++$i) {
         $bits[$bidx++] = $lenbits[$i];
     }
     $databits = array();
     $this->iEncodation->Encode($data, $databits);
     $m = count($databits);
     for ($i = 0; $i < $m; ++$i) {
         $databits[$i] = array_reverse($databits[$i]);
     }
     for ($i = 0; $i < $m; ++$i) {
         $k = count($databits[$i]);
         for ($j = 0; $j < $k; ++$j) {
             $bits[$bidx++] = $databits[$i][$j];
         }
     }
     $protectedbits = array();
     $this->iConv->_Get($bits, $protectedbits);
     $headerbits = $this->iConv->GetHeader();
     $totBits = count($headerbits) + count($protectedbits);
     if ($this->iSize == -1) {
         $mat_size = 7;
         $mat_idx = 0;
         while ($mat_size <= 47 && $mat_size * $mat_size < $totBits) {
             $mat_idx++;
             $mat_size += 2;
         }
         if ($mat_size > 47) {
             $this->iError = -31;
             return false;
         }
         $this->iSize = $mat_size;
         $ntrailerbits = $mat_size * $mat_size - $totBits;
     } else {
         $mat_size = $this->iSize;
         if ($mat_size * $mat_size < $totBits) {
             $this->iError = -31;
             return false;
         }
         $ntrailerbits = $mat_size * $mat_size - $totBits;
         $mat_idx = ($mat_size - 7) / 2;
     }
     $trailerbits = array_fill(0, $ntrailerbits, 0);
     $bits = array_merge($headerbits, $protectedbits, $trailerbits);
     $ret = $this->iMasterRand->Randomize($bits);
     if ($ret === false) {
         $this->iError = -33;
         return false;
     }
     $outputMatrix = array(array(), array());
     $this->iBitPlacement->Set($mat_idx, $bits, $outputMatrix);
     $pspec = new PrintSpecification(DM_TYPE_140, $data, $outputMatrix, $this->iEncodation->iSelectSchema, $this->iErrLevel);
     return $pspec;
 }