接下來就是對每個圖層進行解析: ui
這裏講到前兩個字節讀出來若是小於0,那麼絕對值就是圖層的個數。 spa
layerCount = Math.abs(layerData.readShort()); code
下面是每一個圖層所包含的內容信息表格: ip
這樣,就能夠根據每一個圖層的字節長度循環去處理每個圖層: ci
layerVect = new Vector.<LayerVo>(); var layerVo:LayerVo; for (var i:int = 0; i < layerCount; i++) { layerVo = new LayerVo(); layerVo.parse(layerData); layerVect.push(layerVo); }
圖層的前4個值(top,Left,Bottom,Right)分別是左上角跟右下角在psd裏面的座標位置。 string
Channel是圖層的通道數,Channel length info是通道信息,而且每種通道佔6位字節長度。 it
從上面表格能夠看出,通道類型分別是RGB跟Alpha以及他們分別長度。 io
Extra data size:是擴展信息的長度,這裏面保存了圖層名字以及一些額外的信息(例如文本內容,圖層類型(圖層組或是圖層)等)。 function
Layer name的描述說圖層名字長度是4的倍數,若是讀出來的不是就要補齊,若是恰好是4的倍數,直接加上4。 class
Adjustment layer info裏面就是保存了上面所說的額外信息。
private const ChANNEL_TYPE:Object = {"0":"Red","1":"Green", "2":"Blue", "-1":"Transparency mask", "-2":"User supplied layer mask"}; public function parse(ba:ByteArray):void { top = ba.readUnsignedInt(); //左上角 left = ba.readUnsignedInt(); bottom = ba.readUnsignedInt(); //右下角 right = ba.readUnsignedInt(); width = right - left; height = bottom - top; var pos0:int; channels = ba.readShort(); for (var i:int = 0; i < channels; i++) { var channelId:int = ba.readShort(); var channelLen:int = ba.readUnsignedInt(); channelDataArr.push({"channelId":channelId, "channelType":ChANNEL_TYPE[channelId+""], "channelLen":channelLen}); } blendModeSignature = ba.readUTFBytes(4); blendModeKey = ba.readUTFBytes(4); opacity = ba.readUnsignedByte(); clipping = ba.readUnsignedByte(); flags = ba.readUnsignedByte(); fillter = ba.readUnsignedByte(); extraLen = ba.readUnsignedInt(); pos0 = ba.position; //當前位置 var layerMaskLen:int = ba.readInt(); if(0 != layerMaskLen) { layerMaskVo = new LayerMaskVo(); layerMaskVo.parse(ba); } var layerBlendingRanges:int = ba.readInt(); if(0 != layerBlendingRanges) { layerBlendingRangesVo = new LayerBlendingRangesVo(); layerBlendingRangesVo.parse(ba, layerBlendingRanges); } var pos:int = ba.position; //pascal string只有255長度 var sizeOfName : uint = ba.readUnsignedByte(); layerName = ba.readMultiByte(sizeOfName, "gb-2312"); if(0 == sizeOfName%4) { sizeOfName += 4; } else { //填充到4的倍數 while(sizeOfName%4 != 0 ) { sizeOfName++; } } ba.position = pos + sizeOfName; while(ba.position < pos0 + extraLen) { analyzeAdjuetmentData(ba); } ba.position = pos0 + extraLen; //圖層的終點位置 = 當前位置 + 擴展長度 }