Read java Byte from PHP -


i have java application send compressed image on network. each pixel value converted new byte type. problem don't know how correctly decode thos byte php.

here java compression:

public byte[] compressedimage(bufferedimage img, int color_type) {   if ((img.gettype() != 2) && (img.gettype() != 1)) {     throw new runtimeexception("image format not supported");   }    int sizex = img.getwidth();  // 640px   int sizey = img.getheight(); // 80px    vector compresseddata = new vector(sizey * (1 + 3 * sizex) + 1);   databuffer data = img.getraster().getdatabuffer();    int count = 0;   int nextpix = 0;   int ptr = 0;   (int y = 0; y < sizey; y++) {     compresseddata.add(new byte((byte)-128));     int currpix = data.getelem(ptr++);     int x = 1;     do{       if (x < sizex) {         nextpix = data.getelem(ptr++);          if ((nextpix == currpix) && (count < 63)) {            count++;           continue;         }       }        int color_r = (currpix & 0xff0000) >> 16;       int color_g = (currpix & 0xff00) >> 8;       int color_b = currpix & 0xff;        compresseddata.add(new byte((byte)((color_r & 0xff) >> 1)));       compresseddata.add(new byte((byte)((color_g & 0xff) >> 1)));       compresseddata.add(new byte((byte)((color_b & 0xff) >> 1)));        if (count != 0) {         compresseddata.add(new byte((byte)(0xffffffc0 | count)));       }       count = 0;       currpix = nextpix;     } while (x++ < sizex);   }    byte[] result = new byte[compresseddata.size()];   enumeration e = compresseddata.elements();    int = 0;   while (e.hasmoreelements()) {     result[(i++)] = ((byte)e.nextelement()).bytevalue();   }   return result; } 

using wireshark software have been able capture packet full image stored in file "scalio.bin"

now try read scalio.bin file php in order recreate image. know each -128 value means new line know should able 4 values: r, g, b , count.

i tried play unpack() , sprintf() without real success:

$filename = "scalio.bin"; $handle = fopen($filename, "rb"); $content = fread($handle, filesize($filename));  /* method 1 */ ($i=0; $i<strlen($content); $i++){   $v = sprintf("%d", bin2hex($content[$i]));   if(hexdec($v) == 128) {     $line++;   } else {     $imgarray[$line][] = hexdec($v);   } }  echo "<pre>"; print_r($imgarray); echo "</pre>";  /* method 2 */ $data  = unpack("c*", $content); foreach($data $val){   switch($val){     case -128:       $line++;       break;     default:       $imgarray[$line][] = $val;   } }  for($i=0; $i<sizeof($imgarray); $i++){   $imgarray[$i] = array_chunk($imgarray[$i], 4); }  echo "<pre>"; print_r($imgarray); echo "</pre>"; 

i have read byte type limited -128 127 expect value -128 127 in php code, hexadecimal value, or char, or large number 4294967252

i've found 'count' variable need xor reverted (i.e somethnig $val ^ 0xffffffc0 r, g, b value need shift ($val & 0xff) << 1

finally question is: how decode .bin file plenty of java byte value php ?

finally got ! i'm not happy performance. idea how optimize code ?

<?php $filename = "scalio.bin"; $handle   = fopen($filename, "rb"); $content = fread($handle, filesize($filename));  $line=-1; $imgarray=array();  $arr=array("r", "g", "b"); $color=-1; function getnextcolor(){   global $arr, $color;   if($color > 1)     $color = -1;   $color++;   return $arr[$color]; }  $pix=-1; ($i=0; $i<strlen($content); $i++){   $buf   = bin2hex($content[$i]);   $dec   = hexdec($buf);   $bytes = (($dec+128)%256) -128;    if($bytes == -128){     $line++;     $pix=-1;   }elseif($bytes < 0){     $b = substr(decbin($bytes), -8);     $imgarray[$line][$pix]['count'] = (bindec($b) ^ 0xc0) + 1;   }else{     $col = getnextcolor();     $b = ($bytes & 0xff) << 1;     if($col == "r")       $pix++;     $imgarray[$line][$pix][$col] = $b;   } }  $sizex = 640; $sizey = 80; $gd = imagecreatetruecolor($sizex, $sizey);  $imgarraytrue=array(); foreach($imgarray $lineindex=>$line){   foreach($line $pixelindex=>$pixel){     if( array_key_exists('count', $pixel) ){       for($x=0; $x<$pixel['count']; $x++){         $imgarraytrue[$lineindex][] = array("r"=>$pixel['r'], "g"=>$pixel['g'], "b"=>$pixel['b']);       }     }else{       $imgarraytrue[$lineindex][] = $pixel;     }   } }  foreach($imgarraytrue $lineindex=>$line){   foreach($line $pixelindex=>$pixel){     $color = imagecolorallocate($gd, $pixel['r'], $pixel['g'], $pixel['b']);     imagesetpixel($gd, $pixelindex, $lineindex, $color);   } }  header('content-type: image/gif'); imagegif($gd); ?> 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -