├── .gitattributes
├── EncodeToTGAExtension.cs
├── Readme.md
└── UNLICENSE
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 |
--------------------------------------------------------------------------------
/EncodeToTGAExtension.cs:
--------------------------------------------------------------------------------
1 | //
2 | // This is free and unencumbered software released into the public domain.
3 | //
4 | // Anyone is free to copy, modify, publish, use, compile, sell, or
5 | // distribute this software, either in source code form or as a compiled
6 | // binary, for any purpose, commercial or non-commercial, and by any
7 | // means.
8 | //
9 | // In jurisdictions that recognize copyright laws, the author or authors
10 | // of this software dedicate any and all copyright interest in the
11 | // software to the public domain. We make this dedication for the benefit
12 | // of the public at large and to the detriment of our heirs and
13 | // successors. We intend this dedication to be an overt act of
14 | // relinquishment in perpetuity of all present and future rights to this
15 | // software under copyright law.
16 | //
17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 | // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 | // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 | // OTHER DEALINGS IN THE SOFTWARE.
24 | //
25 | // For more information, please refer to
26 | //
27 |
28 | using UnityEngine;
29 | using System.IO;
30 |
31 | public static class EncodeToTGAExtension
32 | {
33 | // == "TRUEVISION-XFile.\0" (ASCII)
34 | static readonly byte[] c_arV2Signature = { 0x54, 0x52, 0x55, 0x45, 0x56, 0x49, 0x53, 0x49, 0x4F, 0x4E, 0x2D, 0x58, 0x46, 0x49, 0x4C, 0x45, 0x2E, 0x00 };
35 |
36 | public enum Compression
37 | {
38 | None, RLE
39 | }
40 |
41 | public static byte[] EncodeToTGA(this Texture2D _texture2D, Compression _compression = Compression.RLE)
42 | {
43 | const int iTgaHeaderSize = 18;
44 | const int iBytesPerPixelRGB24 = 3; // 1 byte per channel (rgb)
45 | const int iBytesPerPixelARGB32 = 4; // ~ (rgba)
46 |
47 | int iBytesPerPixel = _texture2D.format == TextureFormat.ARGB32 || _texture2D.format == TextureFormat.RGBA32
48 | ? iBytesPerPixelARGB32
49 | : iBytesPerPixelRGB24;
50 |
51 | //
52 |
53 | using (MemoryStream memoryStream = new MemoryStream(iTgaHeaderSize + _texture2D.width * _texture2D.height * iBytesPerPixel))
54 | {
55 | using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
56 | {
57 | // Write TGA Header
58 |
59 | binaryWriter.Write((byte)0); // IDLength (not in use)
60 | binaryWriter.Write((byte)0); // ColorMapType (not in use)
61 | binaryWriter.Write((byte)(_compression == Compression.None ? 2 : 10)); // DataTypeCode (10 == Runlength encoded RGB images)
62 | binaryWriter.Write((short)0); // ColorMapOrigin (not in use)
63 | binaryWriter.Write((short)0); // ColorMapLength (not in use)
64 | binaryWriter.Write((byte)0); // ColorMapDepth (not in use)
65 | binaryWriter.Write((short)0); // Origin X
66 | binaryWriter.Write((short)0); // Origin Y
67 | binaryWriter.Write((short)_texture2D.width); // Width
68 | binaryWriter.Write((short)_texture2D.height); // Height
69 | binaryWriter.Write((byte)(iBytesPerPixel * 8)); // Bits Per Pixel
70 | binaryWriter.Write((byte)0); // ImageDescriptor (not in use)
71 |
72 | Color32[] arPixels = _texture2D.GetPixels32();
73 |
74 | if (_compression == Compression.None)
75 | {
76 | // Write all Pixels one after the other
77 | for (int iPixel = 0; iPixel < arPixels.Length; iPixel++)
78 | {
79 | Color32 c32Pixel = arPixels[iPixel];
80 | binaryWriter.Write(c32Pixel.b);
81 | binaryWriter.Write(c32Pixel.g);
82 | binaryWriter.Write(c32Pixel.r);
83 |
84 | if (iBytesPerPixel == iBytesPerPixelARGB32)
85 | binaryWriter.Write(c32Pixel.a);
86 | }
87 | }
88 | else
89 | {
90 | // Write RLE Encoded Pixels
91 |
92 | const int iMaxPacketLength = 128;
93 | int iPacketStart = 0;
94 | int iPacketEnd = 0;
95 |
96 | while (iPacketStart < arPixels.Length)
97 | {
98 | Color32 c32PreviousPixel = arPixels[iPacketStart];
99 |
100 | // Get current Packet Type
101 | RLEPacketType packetType = EncodeToTGAExtension.PacketType(arPixels, iPacketStart);
102 |
103 | // Find Packet End
104 | int iReadEnd = Mathf.Min(iPacketStart + iMaxPacketLength, arPixels.Length);
105 | for (iPacketEnd = iPacketStart + 1; iPacketEnd < iReadEnd; ++iPacketEnd)
106 | {
107 | bool bPreviousEqualsCurrent = EncodeToTGAExtension.Equals(arPixels[iPacketEnd - 1], arPixels[iPacketEnd]);
108 |
109 | // Packet End if change in Packet Type or if max Packet-Size reached
110 | if (packetType == RLEPacketType.RAW && bPreviousEqualsCurrent ||
111 | packetType == RLEPacketType.RLE && !bPreviousEqualsCurrent)
112 | {
113 | break;
114 | }
115 | }
116 |
117 | // Write Packet
118 |
119 | int iPacketLength = iPacketEnd - iPacketStart;
120 |
121 | switch (packetType)
122 | {
123 | case RLEPacketType.RLE:
124 |
125 | // Add RLE-Bit to PacketLength
126 | binaryWriter.Write((byte)((iPacketLength - 1) | (1 << 7)));
127 |
128 | binaryWriter.Write(c32PreviousPixel.b);
129 | binaryWriter.Write(c32PreviousPixel.g);
130 | binaryWriter.Write(c32PreviousPixel.r);
131 |
132 | if (iBytesPerPixel == iBytesPerPixelARGB32)
133 | binaryWriter.Write(c32PreviousPixel.a);
134 |
135 | break;
136 | case RLEPacketType.RAW:
137 |
138 | binaryWriter.Write((byte)(iPacketLength - 1));
139 |
140 | for (int iPacketPosition = iPacketStart; iPacketPosition < iPacketEnd; ++iPacketPosition)
141 | {
142 | Color32 c32Pixel = arPixels[iPacketPosition];
143 | binaryWriter.Write(c32Pixel.b);
144 | binaryWriter.Write(c32Pixel.g);
145 | binaryWriter.Write(c32Pixel.r);
146 |
147 | if (iBytesPerPixel == iBytesPerPixelARGB32)
148 | binaryWriter.Write(c32Pixel.a);
149 | }
150 |
151 | break;
152 | }
153 |
154 | iPacketStart = iPacketEnd;
155 | }
156 | }
157 |
158 | binaryWriter.Write(0); // Offset of meta-information (not in use)
159 | binaryWriter.Write(0); // Offset of Developer-Area (not in use)
160 | binaryWriter.Write(c_arV2Signature); // ImageDescriptor (not in use)
161 | }
162 |
163 | return memoryStream.ToArray();
164 | }
165 | }
166 |
167 | //
168 |
169 | // RLE Helper
170 |
171 | private enum RLEPacketType { RLE, RAW }
172 |
173 | private static bool Equals(Color32 _first, Color32 _second)
174 | {
175 | return _first.r == _second.r && _first.g == _second.g && _first.b == _second.b && _first.a == _second.a;
176 | }
177 |
178 | private static RLEPacketType PacketType(Color32[] _arData, int _iPacketPosition)
179 | {
180 | if ((_iPacketPosition != _arData.Length - 1) && EncodeToTGAExtension.Equals(_arData[_iPacketPosition], _arData[_iPacketPosition + 1]))
181 | {
182 | return RLEPacketType.RLE;
183 | }
184 | else
185 | {
186 | return RLEPacketType.RAW;
187 | }
188 | }
189 | }
190 |
--------------------------------------------------------------------------------
/Readme.md:
--------------------------------------------------------------------------------
1 | > Since Unity2018.3 this feature is officially supported by the ImageConversion-Class and part of Unity. Use that, if you have no definite reason not to (i.e. Unity does not use Run-Length-Encoding -- If you need that or have certain disc-space requirements, you might still want to use this implementation).
2 |
3 | # Texture2D.EncodeToTGA (Unity)
4 |
5 | Adds a EncodeToTGA Extension-Method to Texture2D. Uses Run-Length-Encoding and can Encode RGB24 and ARGB32 Formats (equally to EncodeToPNG and EncodeToJPG).
6 |
7 | As some software doesn't support RLE-Encoded TGA-Files, also supports uncompressed Encoding. The Default is RLE.
8 |
9 | ## Usage
10 |
11 | ```csharp
12 |
13 | Texture2D texture;
14 |
15 | // Default usage -- write tga-data to file
16 | File.WriteAllBytes("file.tga", texture.EncodeToTGA());
17 |
18 | // Encode w/o compression -- in most cases you won't need that.
19 | // Only use, if you work with software that doesn't support Compressed TGAs.
20 | File.WriteAllBytes("file.tga", texture.EncodeToTGA(EncodeToTGAExtension.Compression.None));
21 |
22 | ```
23 |
24 | ## (Un)License
25 |
26 | This is free and unencumbered software released into the public domain.
27 |
28 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
29 |
30 | In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
31 |
32 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 |
34 | For more information, please refer to
35 |
--------------------------------------------------------------------------------
/UNLICENSE:
--------------------------------------------------------------------------------
1 |
2 | This is free and unencumbered software released into the public domain.
3 |
4 | Anyone is free to copy, modify, publish, use, compile, sell, or
5 | distribute this software, either in source code form or as a compiled
6 | binary, for any purpose, commercial or non-commercial, and by any
7 | means.
8 |
9 | In jurisdictions that recognize copyright laws, the author or authors
10 | of this software dedicate any and all copyright interest in the
11 | software to the public domain. We make this dedication for the benefit
12 | of the public at large and to the detriment of our heirs and
13 | successors. We intend this dedication to be an overt act of
14 | relinquishment in perpetuity of all present and future rights to this
15 | software under copyright law.
16 |
17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 | OTHER DEALINGS IN THE SOFTWARE.
24 |
25 | For more information, please refer to
26 |
--------------------------------------------------------------------------------