[转]解析QQ三国图片

package {
import flash.display.*;
import flash.events.Event;
import flash.net.*;
import flash.utils.*;

public class SGIMG extends Sprite
{
public var load:URLLoader = new URLLoader();
private var _bmp:Bitmap = new Bitmap();
private var _ext:String;
private var rootPath:String = "D:\\WorkSpace\\QQSG\\DATA\\res\\";
private var imgHeight:int,imgWidth:int;

public function SGIMG()
{  
   this.stage.scaleMode = StageScaleMode.NO_SCALE;
   this.stage.align = StageAlign.TOP_LEFT;   
   load.dataFormat = URLLoaderDataFormat.BINARY;
   load.addEventListener(Event.COMPLETE,comHandler);   
   //loadFile("UiRes\\Version04\\Login\\logo2.gsa");
   //loadFile("res\\MapRes\\denglu\\yun00.gso");
   //loadFile("UiRes\\Version04\\Login\\Job\\Image\\dddd.bmp");   
}
private function loadFile(filePath:String):void{
   var url:URLRequest = new URLRequest(rootPath + filePath);
   load.load(url);
   var leng:int = url.url.length;
   _ext= url.url.substr(leng - 3,3);
  
}
private function comHandler(evt:Event):void{
   var b:ByteArray = load.data;
   
   b.endian = Endian.LITTLE_ENDIAN;
   var imgHeader:Object = new Object();
   trace(b.readMultiByte(8,"utf-8"));//图片格式
   //--------------说明图象数据压缩的类型-----------------
    var offest:int = 11;//gso:DIMG为11,gsa:IANI为4
    if(_ext == "gso" || _ext == "gsn"){
     offest = 17;//DIMG  dds格式 RGB 44444
    }else if(_ext == "gsa"){
    offest = 11;//IANI  bmp格式
    }
    var headeData:Array = new Array();
    for(var index:int = 0; index < offest; index++){
          headeData[index] = b.readInt();
          trace(index,"---------",headeData[index]);
    }
    imgWidth = headeData[4];//说明水平分辨率
    imgHeight = headeData[5];//说明垂直分辨率
   //-------------------------------------------------
   //--------------------实际图像数据------------------------
   var imgData:Array = new Array(imgHeight);
   for(var row:int=0; row < imgHeight; row++){
              imgData[row] = new Array(imgWidth);
              for(var col:int = 0; col < imgWidth; col++){
              if(_ext == "gso" || _ext == "gsn"){
                       imgData[row][col] = getPixel(b) ; 
              }else if(_ext == "gsa"){
                      //imgData[row][col] = b.readUnsignedInt(); 
                      imgData[row][col] = getPixel(b)
                    //trace(imgData[row][col].toString(16));
    }
}
   }
           trace(b.bytesAvailable);
   
   if(_ext == "gso" || _ext == "gsn"){
   //数据结尾
   var tempData:Array = new Array();
           for(var x:int = 0; x < 6; x++){
              tempData[x] = b.readInt();
           }
           }
           trace(b.bytesAvailable);
           
   addChild(drawImage(imgData)); 
}
public function getPixel(b:ByteArray):uint{
   var colorA:uint = b.readUnsignedByte();//GB
var colorB:uint = b.readUnsignedByte();//AR
return h4(colorB) << 24 | l4(colorB) << 16 | h4(colorA) << 8 | l4(colorA);//RGB
}
public function h4(n:uint):uint{
    var d:uint = (n & 0xF0) >> 4;//取高4位
    return d << 4 | d;//A转为AA
}
public function l4(n:uint):uint{
    var d:uint = (n & 0xF);//取低4位
    return d << 4 | d;//A转为AA
}
public  function drawImage(imgData:Array):Bitmap{
var bmd:BitmapData = new BitmapData(imgWidth, imgHeight, true, 0xff000000);
for(var row:int=0; row<imgHeight; row++){
for(var col:int=0; col<imgWidth; col++){
bmd.setPixel32(col, row, imgData[row][col] );
}
}
_bmp.bitmapData = bmd;
trace("------sss-----",imgWidth,imgHeight);
return _bmp;
}
}
}

你可能感兴趣的:(.net,qq,ext,Flash)