| 1 |
/** |
|---|
| 2 |
* TGADecoder.as |
|---|
| 3 |
* |
|---|
| 4 |
* @see http://snippets.libspark.org/ |
|---|
| 5 |
* @see http://snippets.libspark.org/trac/wiki/rch850/Metasequoia |
|---|
| 6 |
* |
|---|
| 7 |
* Copyright (c) 2008 rch850 |
|---|
| 8 |
* |
|---|
| 9 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 10 |
* of this software and associated documentation files (the "Software"), to deal |
|---|
| 11 |
* in the Software without restriction, including without limitation the rights |
|---|
| 12 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 13 |
* copies of the Software, and to permit persons to whom the Software is |
|---|
| 14 |
* furnished to do so, subject to the following conditions: |
|---|
| 15 |
* |
|---|
| 16 |
* The above copyright notice and this permission notice shall be included in |
|---|
| 17 |
* all copies or substantial portions of the Software. |
|---|
| 18 |
* |
|---|
| 19 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 20 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 21 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 22 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 23 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 24 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|---|
| 25 |
* THE SOFTWARE. |
|---|
| 26 |
*/ |
|---|
| 27 |
|
|---|
| 28 |
package org.libspark.pv3d.decoders { |
|---|
| 29 |
import flash.display.BitmapData; |
|---|
| 30 |
import flash.utils.ByteArray; |
|---|
| 31 |
import flash.utils.Endian; |
|---|
| 32 |
|
|---|
| 33 |
public class TGADecoder { |
|---|
| 34 |
//___________________________________________________________ const |
|---|
| 35 |
|
|---|
| 36 |
// constant value for _imageType |
|---|
| 37 |
private const TYPE_NONE:uint = 0x00; |
|---|
| 38 |
private const TYPE_INDEX_COLOR:uint = 0x01; |
|---|
| 39 |
private const TYPE_FULL_COLOR:uint = 0x02; |
|---|
| 40 |
private const TYPE_RLE_BIT:uint = 0x08; |
|---|
| 41 |
|
|---|
| 42 |
private const DIR_RIGHT_UP:int = 0; |
|---|
| 43 |
private const DIR_LEFT_UP:int = 1; |
|---|
| 44 |
private const DIR_RIGHT_DOWN:int = 2; |
|---|
| 45 |
private const DIR_LEFT_DOWN:int = 3; |
|---|
| 46 |
|
|---|
| 47 |
//___________________________________________________________ vars |
|---|
| 48 |
|
|---|
| 49 |
private var _bitmap:BitmapData; |
|---|
| 50 |
public function get bitmap():BitmapData { |
|---|
| 51 |
return _bitmap; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
private var _idLength:int; // byte |
|---|
| 55 |
private var _colorMapType:int; // byte |
|---|
| 56 |
private var _imageType:int; // byte |
|---|
| 57 |
private var _colorMapIndex:int; // short |
|---|
| 58 |
private var _colorMapLength:int; // short |
|---|
| 59 |
private var _colorMapSize:int; // byte |
|---|
| 60 |
private var _originX:int; // short |
|---|
| 61 |
private var _originY:int; // short |
|---|
| 62 |
private var _width:int; // short |
|---|
| 63 |
public function get width():int { |
|---|
| 64 |
return _width; |
|---|
| 65 |
} |
|---|
| 66 |
private var _height:int; // short |
|---|
| 67 |
public function get height():int { |
|---|
| 68 |
return _height; |
|---|
| 69 |
} |
|---|
| 70 |
private var _bitDepth:int; // byte |
|---|
| 71 |
private var _descriptor:int; // byte |
|---|
| 72 |
public function get pixelDirection():int { |
|---|
| 73 |
// descriptor: |
|---|
| 74 |
// 4th bit: 0 = left to right, 1 = right to left |
|---|
| 75 |
// 5th bit: 0 = bottom up, 1 = top down |
|---|
| 76 |
return (_descriptor >> 4) & 3; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
/** |
|---|
| 80 |
* Construct TGA file from ByteArray. |
|---|
| 81 |
*/ |
|---|
| 82 |
public function TGADecoder(bytes:ByteArray) { |
|---|
| 83 |
bytes.position = 0; |
|---|
| 84 |
bytes.endian = Endian.LITTLE_ENDIAN; |
|---|
| 85 |
|
|---|
| 86 |
_idLength = bytes.readByte(); |
|---|
| 87 |
_colorMapType = bytes.readByte(); |
|---|
| 88 |
_imageType = bytes.readByte(); |
|---|
| 89 |
_colorMapIndex = bytes.readShort(); |
|---|
| 90 |
_colorMapLength = bytes.readShort(); |
|---|
| 91 |
_colorMapSize = bytes.readByte() |
|---|
| 92 |
_originX = bytes.readShort(); |
|---|
| 93 |
_originY = bytes.readShort(); |
|---|
| 94 |
_width = bytes.readShort(); |
|---|
| 95 |
_height = bytes.readShort(); |
|---|
| 96 |
_bitDepth = bytes.readByte(); |
|---|
| 97 |
_descriptor = bytes.readByte(); |
|---|
| 98 |
|
|---|
| 99 |
_bitmap = new BitmapData(_width, _height); |
|---|
| 100 |
|
|---|
| 101 |
// ignore unsupported formats. |
|---|
| 102 |
if ((_imageType & TYPE_FULL_COLOR) == 0 |
|---|
| 103 |
|| (_imageType & TYPE_RLE_BIT) != 0) { |
|---|
| 104 |
throw new Error("Unsupported tga format."); |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
_bitmap.lock(); |
|---|
| 108 |
try { |
|---|
| 109 |
if (_bitDepth == 32) { |
|---|
| 110 |
loadBitmap32(bytes); |
|---|
| 111 |
} else if (_bitDepth == 24) { |
|---|
| 112 |
loadBitmap24(bytes); |
|---|
| 113 |
} |
|---|
| 114 |
} finally { |
|---|
| 115 |
_bitmap.unlock(); |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
/** |
|---|
| 120 |
* Load 32 bpp bitmap. |
|---|
| 121 |
*/ |
|---|
| 122 |
private function loadBitmap32(bytes:ByteArray):void { |
|---|
| 123 |
var x:int, y:int; |
|---|
| 124 |
switch (pixelDirection) { |
|---|
| 125 |
case DIR_RIGHT_UP: |
|---|
| 126 |
for (y = _bitmap.height - 1; y >= 0; --y) { |
|---|
| 127 |
for (x = 0; x < _bitmap.width; ++x) { |
|---|
| 128 |
_bitmap.setPixel32(x, y, bytes.readUnsignedInt()); |
|---|
| 129 |
} |
|---|
| 130 |
} |
|---|
| 131 |
break; |
|---|
| 132 |
case DIR_LEFT_UP: |
|---|
| 133 |
for (y = _bitmap.height - 1; y >= 0; --y) { |
|---|
| 134 |
for (x = _bitmap.width - 1; x >= 0; --x) { |
|---|
| 135 |
_bitmap.setPixel32(x, y, bytes.readUnsignedInt()); |
|---|
| 136 |
} |
|---|
| 137 |
} |
|---|
| 138 |
break; |
|---|
| 139 |
case DIR_RIGHT_DOWN: |
|---|
| 140 |
for (y = 0; y < _bitmap.height; ++y) { |
|---|
| 141 |
for (x = 0; x < _bitmap.width; ++x) { |
|---|
| 142 |
_bitmap.setPixel32(x, y, bytes.readUnsignedInt()); |
|---|
| 143 |
} |
|---|
| 144 |
} |
|---|
| 145 |
break; |
|---|
| 146 |
case DIR_LEFT_DOWN: |
|---|
| 147 |
for (y = 0; y < _bitmap.height; ++y) { |
|---|
| 148 |
for (x = _bitmap.width - 1; x >= 0; --x) { |
|---|
| 149 |
_bitmap.setPixel32(x, y, bytes.readUnsignedInt()); |
|---|
| 150 |
} |
|---|
| 151 |
} |
|---|
| 152 |
break; |
|---|
| 153 |
} |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
/** |
|---|
| 157 |
* Load 24 bpp bitmap. |
|---|
| 158 |
*/ |
|---|
| 159 |
private function loadBitmap24(bytes:ByteArray):void { |
|---|
| 160 |
var x:int, y:int; |
|---|
| 161 |
var r:uint, g:uint, b:uint; |
|---|
| 162 |
switch (pixelDirection) { |
|---|
| 163 |
case DIR_RIGHT_UP: |
|---|
| 164 |
for (y = _bitmap.height - 1; y >= 0; --y) { |
|---|
| 165 |
for (x = 0; x < _bitmap.width; ++x) { |
|---|
| 166 |
b = bytes.readUnsignedByte(); |
|---|
| 167 |
g = bytes.readUnsignedByte(); |
|---|
| 168 |
r = bytes.readUnsignedByte(); |
|---|
| 169 |
_bitmap.setPixel32(x, y, 0xFF000000 | (r << 16) | (g << 8) | b); |
|---|
| 170 |
} |
|---|
| 171 |
} |
|---|
| 172 |
break; |
|---|
| 173 |
case DIR_LEFT_UP: |
|---|
| 174 |
for (y = _bitmap.height - 1; y >= 0; --y) { |
|---|
| 175 |
for (x = _bitmap.width - 1; x >= 0; --x) { |
|---|
| 176 |
b = bytes.readUnsignedByte(); |
|---|
| 177 |
g = bytes.readUnsignedByte(); |
|---|
| 178 |
r = bytes.readUnsignedByte(); |
|---|
| 179 |
_bitmap.setPixel32(x, y, 0xFF000000 | (r << 16) | (g << 8) | b); |
|---|
| 180 |
} |
|---|
| 181 |
} |
|---|
| 182 |
break; |
|---|
| 183 |
case DIR_RIGHT_DOWN: |
|---|
| 184 |
for (y = 0; y < _bitmap.height; ++y) { |
|---|
| 185 |
for (x = 0; x < _bitmap.width; ++x) { |
|---|
| 186 |
b = bytes.readUnsignedByte(); |
|---|
| 187 |
g = bytes.readUnsignedByte(); |
|---|
| 188 |
r = bytes.readUnsignedByte(); |
|---|
| 189 |
_bitmap.setPixel32(x, y, 0xFF000000 | (r << 16) | (g << 8) | b); |
|---|
| 190 |
} |
|---|
| 191 |
} |
|---|
| 192 |
break; |
|---|
| 193 |
case DIR_LEFT_DOWN: |
|---|
| 194 |
for (y = 0; y < _bitmap.height; ++y) { |
|---|
| 195 |
for (x = _bitmap.width - 1; x >= 0; --x) { |
|---|
| 196 |
b = bytes.readUnsignedByte(); |
|---|
| 197 |
g = bytes.readUnsignedByte(); |
|---|
| 198 |
r = bytes.readUnsignedByte(); |
|---|
| 199 |
_bitmap.setPixel32(x, y, 0xFF000000 | (r << 16) | (g << 8) | b); |
|---|
| 200 |
} |
|---|
| 201 |
} |
|---|
| 202 |
break; |
|---|
| 203 |
} |
|---|
| 204 |
} |
|---|
| 205 |
} |
|---|
| 206 |
} |
|---|