| 1 |
/** |
|---|
| 2 |
* com.voidelement.images.psd.PSDParser Class for ActionScript 3.0 |
|---|
| 3 |
* |
|---|
| 4 |
* @author Copyright (c) 2007 munegon |
|---|
| 5 |
* @version 0.2 |
|---|
| 6 |
* |
|---|
| 7 |
* @link http://www.voidelement.com/ |
|---|
| 8 |
* @link http://void.heteml.jp/blog/ |
|---|
| 9 |
* |
|---|
| 10 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 11 |
* you may not use this file except in compliance with the License. |
|---|
| 12 |
* You may obtain a copy of the License at |
|---|
| 13 |
* |
|---|
| 14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 15 |
* |
|---|
| 16 |
* Unless required by applicable law or agreed to in writing, software |
|---|
| 17 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 18 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
|---|
| 19 |
* either express or implied. See the License for the specific language |
|---|
| 20 |
* governing permissions and limitations under the License. |
|---|
| 21 |
*/ |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
package com.voidelement.images.psd.section { |
|---|
| 25 |
import com.voidelement.images.psd.PSDParser; |
|---|
| 26 |
import com.voidelement.images.psd.compression.ImageRAWParser; |
|---|
| 27 |
import com.voidelement.images.psd.compression.ImageRLEParser; |
|---|
| 28 |
|
|---|
| 29 |
import flash.display.BitmapData; |
|---|
| 30 |
import flash.utils.ByteArray; |
|---|
| 31 |
|
|---|
| 32 |
public class PSDImageData { |
|---|
| 33 |
private const COMP_RAW:int = 0; |
|---|
| 34 |
private const COMP_RLE:int = 1; |
|---|
| 35 |
|
|---|
| 36 |
private var _compression:uint; |
|---|
| 37 |
public function get compression():uint { return _compression; } |
|---|
| 38 |
|
|---|
| 39 |
private var _channels:Array; |
|---|
| 40 |
public function get channels():Array { return _channels; } |
|---|
| 41 |
|
|---|
| 42 |
private var _image:BitmapData; |
|---|
| 43 |
public function get image():BitmapData { return _image; } |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
public function PSDImageData( header:PSDFileHeader, stream:ByteArray ) { |
|---|
| 47 |
var width:int = header.columns; |
|---|
| 48 |
var height:int = header.rows; |
|---|
| 49 |
var numChannels:int = header.channel; |
|---|
| 50 |
|
|---|
| 51 |
_compression = stream.readUnsignedShort(); |
|---|
| 52 |
|
|---|
| 53 |
PSDParser.log("\n\n---- PSD Image Data ----"); |
|---|
| 54 |
PSDParser.log("compression: " + compression ); |
|---|
| 55 |
|
|---|
| 56 |
switch ( compression ) { |
|---|
| 57 |
case COMP_RAW: |
|---|
| 58 |
var raw:ImageRAWParser = new ImageRAWParser( numChannels, width, height, stream ); |
|---|
| 59 |
_channels = raw.channelsData; |
|---|
| 60 |
break; |
|---|
| 61 |
case COMP_RLE: |
|---|
| 62 |
var rle:ImageRLEParser = new ImageRLEParser( numChannels, width, height, stream ); |
|---|
| 63 |
_channels = rle.channelsData; |
|---|
| 64 |
break; |
|---|
| 65 |
default: |
|---|
| 66 |
throw new Error("invalid compression: " + compression ); |
|---|
| 67 |
break; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
setImage( width, height ); |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
private function setImage( width:int, height:int ):void { |
|---|
| 74 |
_image = new BitmapData( width, height, false, 0x000000 ); |
|---|
| 75 |
|
|---|
| 76 |
var r:ByteArray = channels[0]; |
|---|
| 77 |
var g:ByteArray = channels[1]; |
|---|
| 78 |
var b:ByteArray = channels[2]; |
|---|
| 79 |
|
|---|
| 80 |
r.position = 0; |
|---|
| 81 |
g.position = 0; |
|---|
| 82 |
b.position = 0; |
|---|
| 83 |
|
|---|
| 84 |
for ( var y:int = 0; y < height; ++y ) { |
|---|
| 85 |
for ( var x:int = 0; x < width; ++x ) { |
|---|
| 86 |
var rgb:uint = r.readUnsignedByte() << 16 | g.readUnsignedByte() << 8 | b.readUnsignedByte(); |
|---|
| 87 |
image.setPixel( x, y, rgb ); |
|---|
| 88 |
} |
|---|
| 89 |
} |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
public function getImage():BitmapData { |
|---|
| 93 |
return image; |
|---|
| 94 |
} |
|---|
| 95 |
} |
|---|
| 96 |
} |
|---|