forked from shns/TgaLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added my old Java code from 2004, ported to C# but needs more testing and polishing
- Loading branch information
Showing
10 changed files
with
417 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,22 @@ | ||
MIT License | ||
|
||
Copyright (c) 2016 shns | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
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 OR COPYRIGHT HOLDERS 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. | ||
MIT License | ||
|
||
Copyright (c) 2016 shns | ||
Copyright (c) 2004-2024 Peter Truchly | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
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 OR COPYRIGHT HOLDERS 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.IO; | ||
using NUnit.Framework; | ||
|
||
namespace tgalib_core.Tests; | ||
|
||
[TestFixture] | ||
public class TgaFileFormatTests | ||
{ | ||
[Test] | ||
[TestCase("resources/CBW8.TGA")] | ||
public void Test01_LoadSaveRgb24Rle(string filename) | ||
{ | ||
using FileStream ts = new(filename, FileMode.Open, FileAccess.Read, FileShare.Read); | ||
using BinaryReader r = new(ts); | ||
TgaImage tga = new(r); | ||
Image actualImage = tga.GetImage(); | ||
|
||
TgaFileFormat tff = new(); | ||
using FileStream fs = File.OpenWrite("test01.tga"); | ||
tff.CommonSave(TgaMode.Rgb24Rle, fs, actualImage); | ||
fs.Close(); | ||
// Assert.Pass(); | ||
} | ||
|
||
[Test] | ||
[TestCase("resources/UBW8.TGA", false)] | ||
[TestCase("resources/UCM8.TGA", false)] | ||
[TestCase("resources/UTC16.TGA", false)] | ||
[TestCase("resources/UTC24.TGA", false)] | ||
[TestCase("resources/UTC32.TGA", false)] | ||
[TestCase("resources/UBW8.TGA", true)] | ||
[TestCase("resources/CBW8.TGA", false)] | ||
[TestCase("resources/CCM8.TGA", false)] | ||
[TestCase("resources/CTC16.TGA", false)] | ||
[TestCase("resources/CTC24.TGA", false)] | ||
[TestCase("resources/CTC32.TGA", false)] | ||
[TestCase("resources/rgb32rle.tga", true)] | ||
public void Test02_RoundTripRgb24Rle(string filename, bool forceAlpha) | ||
{ | ||
using FileStream ts = new(filename, FileMode.Open, FileAccess.Read, FileShare.Read); | ||
using BinaryReader br = new(ts); | ||
TgaImage tga = new(br, forceAlpha); | ||
Image actualImage = tga.GetImage(); | ||
|
||
TgaFileFormat tff = new(); | ||
using FileStream fs = File.OpenWrite("test02.tga"); | ||
tff.CommonSave(TgaMode.Rgb24Rle, fs, actualImage); | ||
fs.Close(); | ||
|
||
using FileStream ts2 = new("test02.tga", FileMode.Open, FileAccess.Read, FileShare.Read); | ||
using BinaryReader br2 = new(ts2); | ||
TgaImage tga2 = new(br2, forceAlpha); | ||
Image actualImage2 = tga2.GetImage(); | ||
|
||
int width = actualImage.Width; | ||
int height = actualImage.Height; | ||
|
||
Assert.That(width, Is.EqualTo(actualImage2.Width)); | ||
Assert.That(height, Is.EqualTo(actualImage2.Height)); | ||
|
||
for (int x = 0; x < width; x++) | ||
{ | ||
for (int y = 0; y < height; y++) | ||
{ | ||
actualImage.GetPixelRgba(x, y, out int r1, out int g1, out int b1, out int a1); | ||
actualImage2.GetPixelRgba(x, y, out int r2, out int g2, out int b2, out int a2); | ||
Assert.That(r1, Is.EqualTo(r2)); | ||
Assert.That(g1, Is.EqualTo(g2)); | ||
Assert.That(b1, Is.EqualTo(b2)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
namespace tgalib_core; | ||
|
||
//auxiliary class for RLE compression | ||
public class RleCompressor(Stream stream) | ||
{ | ||
private const int buflen = 65536; | ||
private const int bufcritical = 65532; | ||
private RlePixel[] buf = new RlePixel[buflen]; | ||
private int bufindex = 0;//index of first free place | ||
|
||
private bool match(RlePixel arg1, RlePixel arg2) | ||
{ | ||
if (arg1.c.Length != arg2.c.Length) return(false); | ||
for (int i = 0; i < arg1.c.Length; i++) if (arg1.c[i] != arg2.c[i]) return(false); | ||
return(true); | ||
} | ||
|
||
public void write(byte arg) { write([arg]); } | ||
|
||
//add arg to FIFO | ||
public void write(byte[] arg) | ||
{ | ||
RlePixel argpix = new RlePixel(arg); | ||
if ((bufindex > 0)&&match(buf[bufindex-1],argpix)){ | ||
if (buf[bufindex-1].rep < 128){ | ||
buf[bufindex-1].rep++; | ||
return; | ||
} | ||
} | ||
buf[bufindex]=argpix; | ||
bufindex++; | ||
if (bufindex > bufcritical) forceWrite(); | ||
} | ||
|
||
//write one copy or run packet | ||
private void writePacket(int from, int to) | ||
{ | ||
byte[] header = new byte[1]; | ||
if (to - from > 0) //create copy packet | ||
{ | ||
header[0] = (byte)(to - from);//number of copied pixels - 1 | ||
} else { //write run packet | ||
header[0] = (byte)(128 | (buf[from].rep-1));//number of repetition -1 and highest bit is 1 | ||
} | ||
try | ||
{ | ||
stream.Write(header); | ||
for (int rpc = from; rpc <= to; rpc++) // this should run only once for run packet | ||
{ | ||
stream.Write(buf[rpc].c); | ||
} | ||
} catch (IOException e) { Console.WriteLine(e); } | ||
} | ||
|
||
//forced write of entire buffer to output (end of image or full buffer) | ||
public void forceWrite() | ||
{ | ||
int from = 0; | ||
while (from < bufindex) | ||
{ | ||
int to = from; | ||
while ((to < bufindex-1)&&(buf[to].rep == 1)&&(to-from < 127)) to++; | ||
if ((to - from +1 > 1)&&(buf[to].rep > 1)) to--; | ||
writePacket(from, to); | ||
from = to+1; | ||
//writePacket(from, from); | ||
//from++; | ||
} | ||
bufindex = 0;//empty buffer | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace tgalib_core; | ||
|
||
// Pixel value with repetition count. | ||
public class RlePixel(byte[] arg) | ||
{ | ||
public byte[] c = arg; | ||
public int rep = 1; | ||
} |
Oops, something went wrong.