上面已經把圖層的信息都已經拿到了,剩下的byteArray就是圖片的通道數據,這裏就根據它的通道長度信息來獲取對應的值,從而組合成對應的像素生成圖片。數組
Channel Image data一共包含兩個部分:ui
Compression是圖片的壓縮格式,0是原始數據,未通過任何處理的,1是使用了RLE壓縮格式。url
這裏只處理RLE壓縮格式的狀況:code
for (i = 0; i < layerCount; i++) { layerVo = layerVect[i]; layerVo.parsePixel(layerData); }
public function parsePixel(ba:ByteArray):void { var tempObj:Object; //RGBA通道數據數組,前面已經獲取過每種通道的長度信息 for (var i:int = 0; i < channelDataArr.length; i++) { tempObj = channelDataArr[i]; tempObj.compressedType = ba.readUnsignedShort(); var pixelDataByte:ByteArray = new ByteArray(); //If the Layer’s Size, and therefore the data, is odd, a pad byte will be inserted at the end of the row. //放棄最後兩個字節 if (tempObj.channelLen - 2 > 1) { ba.readBytes(pixelDataByte, 0, tempObj.channelLen - 2); } tempObj.pixelData = getRealPixelData(tempObj.compressedType, pixelDataByte); } }
private function getRealPixelData(compress:int, data:ByteArray):ByteArray { if(1 == compress) { //使用height是由於它是按照行來壓縮的 return decodeRLE(data, height); } return data; } //解壓縮 public function decodeRLE(source : ByteArray, height : int) : ByteArray { source.position = 0; var lines : Array = new Array(height); var i : int = 0; var total : int; var size : int; for ( i = 0; i < height; ++i ) { size = source.readUnsignedShort(); lines[i] = size; total += lines[i]; } if (source.position + total != source.length) { throw new Error("rle decode error"); } var target : ByteArray = new ByteArray(); var length : int = 0; var j : int; var k : int; var n : int; var count : int; for ( i = 0; i < height; ++i ) { length = lines[i]; j = 0; count = 0; while (j < length) { n = source.readByte(); j++; if (n >= 0) { count = n + 1; for (k = 0;k < count;++k) { target.writeByte(source.readByte()); } j += count; } else { count = 1 - n; var byte : int = source.readByte(); for (k = 0;k < count;++k) { target.writeByte(byte); } j++; } } } return target; }
根據獲得的每種顏色的值進行計算獲得對應的像素值:圖片
private static function createPic(layerVo:LayerVo):void { if(true == layerVo.isFolder || true == layerVo.isText) return; var color:uint; var alphaChannel:ByteArray = (layerVo.channelDataArr[0].pixelData as ByteArray); var redChannel:ByteArray = (layerVo.channelDataArr[1].pixelData as ByteArray); var greenChannel:ByteArray = (layerVo.channelDataArr[2].pixelData as ByteArray); var blueChannel:ByteArray = (layerVo.channelDataArr[3].pixelData as ByteArray); alphaChannel.position = redChannel.position = greenChannel.position = blueChannel.position = 0; var bmd:BitmapData = new BitmapData(layerVo.width, layerVo.height); bmd.lock(); //逐個像素計算顏色值 for (var h:int = 0; h < layerVo.height; h++) { for (var w:int = 0; w < layerVo.width; w++) { var alpha:uint = alphaChannel.readUnsignedByte(); var red:uint = redChannel.readUnsignedByte(); var green:uint = greenChannel.readUnsignedByte(); var blue:uint = blueChannel.readUnsignedByte(); color = alpha << 24 |red << 16 |green << 8 | blue; bmd.setPixel32(w, h, color); } } bmd.unlock(); var url:String = File.desktopDirectory.nativePath + "/image/" + layerVo.layerName+".png"; //生成圖片 FileUtil.createPng(url, bmd); }
這個東西講的有點亂,直接發個整理過的xmind給須要的同窗,本身跟着去研究吧:get
http://pan.baidu.com/s/1ge8Dnnd it
代碼的路徑,只是作了簡單的psd解析成vo,剩下的根據本身的實際需求改唄:io
http://pan.baidu.com/s/1i513hHzfunction