-
Notifications
You must be signed in to change notification settings - Fork 59
/
Writer.hx
221 lines (197 loc) · 5.38 KB
/
Writer.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package format.tga;
import format.tga.Data;
import haxe.ds.Vector;
import haxe.io.Output;
/**
* ...
* @author Yanrishatum
*/
class Writer
{
private var o:Output;
public function new(o:Output)
{
this.o = o;
}
public function write(data:Data):Void
{
writeHeader(data);
if (data.imageId != null) o.writeString(data.imageId);
switch (data.header.imageType)
{
case ImageType.NoImage:
writeColorMap(data);
case ImageType.UncompressedColorMapped:
writeColorMap(data);
writeIndexes(data, false);
case ImageType.UncompressedTrueColor:
writeTrueColor(data, false);
case ImageType.UncompressedBlackAndWhite:
writeMono(data, false);
case ImageType.RunLengthColorMapped:
writeColorMap(data);
writeIndexes(data, true);
case ImageType.RunLengthTrueColor:
writeTrueColor(data, true);
case ImageType.RunLengthBlackAndWhite:
writeMono(data, true);
default:
throw "Unsupported image type";
}
}
private function writeTrueColor(data:Data, rle:Bool):Void
{
writePixels(data.imageData, data.header.bitsPerPixel, data.header.alphaChannelBits != 0, rle);
}
private function writeColorMap(data:Data):Void
{
writePixels(data.colorMapData, data.header.colorMapEntrySize, data.header.alphaChannelBits != 0, false);
}
private function writeIndexes(data:Data, rle:Bool):Void
{
var pal:Vector<Int> = data.colorMapData;
var pixels:Vector<Int> = data.imageData;
if (rle)
{
throw "Run-Length encoding does not supported yet";
}
else
{
var writeFunc:Int->Void;
switch (data.header.bitsPerPixel)
{
case 8:
writeFunc = o.writeByte;
case 24:
writeFunc = o.writeUInt24;
case 32:
writeFunc = o.writeInt32;
default:
throw "Unsupported bits per pixels amount";
}
var i:Int = 0;
inline function indexOf(val:Int):Int
{
var off:Int = 0;
while (off < pal.length)
{
if (pal[off] == val) break;
off++;
}
return off;
}
while (i < pixels.length)
{
writeFunc(indexOf(pixels[i++]));
}
}
}
private function writeMono(data:Data, rle:Bool):Void
{
var pixels:Vector<Int> = data.imageData;
if (rle)
{
throw "Run-Length encoding does not supported yet";
}
else
{
var alhpa:Bool = data.header.bitsPerPixel == 16;
var i:Int = 0;
while (i < pixels.length)
{
var pix:Int = pixels[i++];
o.writeByte(pix & 0xff);
if (alhpa) o.writeByte((pix >> 24) & 0xff);
}
}
}
private function writePixels(pixels:Vector<Int>, bitsPerPixel:Int, alpha:Bool, rle:Bool):Void
{
if (rle) throw "Run-Length encoding does not supported yet";
else
{
var writeFunc:Int->Void;
var encodeFunc:Int->Bool->Int;
switch (bitsPerPixel)
{
case 16:
writeFunc = o.writeUInt16;
encodeFunc = encode16;
case 24:
writeFunc = o.writeUInt24;
encodeFunc = encode24;
case 32:
writeFunc = o.writeInt32;
encodeFunc = encode32;
default:
throw "Unsupported bits per pixels amount";
}
var i:Int = 0;
while (i < pixels.length)
{
writeFunc(encodeFunc(pixels[i++], alpha));
}
}
}
private function encode16(color:Int, alpha:Bool):Int
{
return (alpha ? ((color & 0xff000000) != 0 ? 0x8000 : 0) : 0) | // A
Std.int(((color & 0xff0000) >> 16) / 0xff * 0x1f) << 10 | // R
Std.int(((color & 0xff00) >> 8) / 0xff * 0x1f) << 5 | // G
Std.int((color & 0xff) / 0xff * 0x1f); // B
}
private function encode24(color:Int, alpha:Bool):Int
{
return color & 0xffffff;
}
private function encode32(color:Int, alpha:Bool):Int
{
return alpha ? color : color & 0xffffff;
}
private function writeHeader(data:Data):Void
{
var h:Header = data.header;
o.writeByte(data.imageId != null ? data.imageId.length : 0);
o.writeByte(h.colorMapType);
switch (h.imageType)
{
case ImageType.NoImage:
o.writeByte(0);
case ImageType.UncompressedColorMapped:
o.writeByte(1);
case ImageType.UncompressedTrueColor:
o.writeByte(2);
case ImageType.UncompressedBlackAndWhite:
o.writeByte(3);
case ImageType.RunLengthColorMapped:
o.writeByte(9);
case ImageType.RunLengthTrueColor:
o.writeByte(10);
case ImageType.RunLengthBlackAndWhite:
o.writeByte(11);
case ImageType.Unknown(type):
o.writeByte(type);
}
o.writeInt16(h.colorMapFirstIndex);
o.writeInt16(h.colorMapLength);
o.writeByte(h.colorMapEntrySize);
o.writeInt16(h.xOrigin);
o.writeInt16(h.yOrigin);
o.writeInt16(h.width);
o.writeInt16(h.height);
o.writeByte(h.bitsPerPixel);
var descriptor:Int = h.alphaChannelBits;
switch (h.imageOrigin)
{
case ImageOrigin.BottomLeft:
// None
case ImageOrigin.BottomRight:
descriptor |= 0x10;
case ImageOrigin.TopLeft:
descriptor |= 0x20;
case ImageOrigin.TopRight:
descriptor |= 0x30;
}
o.writeByte(descriptor);
}
}