|  | Posted by Bint on 08/10/06 20:30 
How do I use PHP to create a binary array of data?  Specifically, I need to create an image array ( a string of 16 bit integers that is width*height in
 size).  I've got a data array coming in as an outside argument which is
 run-length-encoded image data.  I can read that image data using ord and bit
 shifting to put my 16 bit values together, but I don't know how to plug them
 into a new array (or binary string?).  What I am looking for is a binary
 string, I guess, not a php array.  Can anyone tell me how to do that?
 
 Here's the routine I wrote, which doesn't work.  Particularly I don't think
 I should be creating my binary string elements with output[$indexnumber].
 That is creating a key-value array. I thought about using pack, but here I
 need to pack values into a single array over and over in for loops--I can't
 do it all at once.  Is there a way to pack a single value and then append it
 to a binary string that I slowly "grow"?
 
 Thanks
 B
 
 
 function rle_decode(&$data, $length)
 {
 $forceit = 1;
 $outnum = 0;
 
 for ($pnum = 0; $pnum < $length; ) {
 $cur = (ord($data{$pnum*2}) << 8) + ord($data{$pnum*2+1});
 $pnum++;
 $output[$outnum++] = $cur;
 
 /* check for run */
 if (($forceit == 0)&&($cur == $prev))
 {
 /* we have a run.  write it out. */
 $count = (ord($data{$pnum*2}) << 8) + ord($data{$pnum*2+1});
 $pnum++;
 while ($count > 0)
 {
 $output[$outnum++] = $cur;
 $count--;
 }
 
 $forceit = 1;
 }
 else
 {
 /* no run */
 $prev = $cur;
 $forceit = 0;
 }
 }
 
 return $output;
 }
  Navigation: [Reply to this message] |