Skip to content

Commit 93532f0

Browse files
committed
Add support for TVPaint Delta compression
1 parent 42d1971 commit 93532f0

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

Diff for: ImageFormats/DeepReader.cs

+32-31
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
using MechanikaDesign.ImageFormats;
22
using System;
3-
using System.Collections.Generic;
43
using System.Drawing;
54
using System.IO;
65
using System.Text;
76

87

98
/*
109
11-
Decoder for IFF (FORM-based) images.
10+
Decoder for IFF DEEP (TV Paint) images.
1211
1312
Copyright 2013-2023 by Warren Galyen
1413
https://www.mechanikadesign.com
@@ -72,10 +71,7 @@ public static Bitmap Load(Stream stream)
7271

7372
stream.Read(tempBytes, 0, 4);
7473
string fileType = Encoding.ASCII.GetString(tempBytes, 0, 4);
75-
if (fileType != "DEEP") { throw new ApplicationException("This is not a valid DEEP file."); }
76-
77-
byte[] palette = null;
78-
var rowPalette = new List<byte[]>();
74+
if (fileType != "DEEP" && fileType != "TVPP") { throw new ApplicationException("This is not a valid DEEP file."); }
7975

8076
while (stream.Position < stream.Length)
8177
{
@@ -149,6 +145,7 @@ public static Bitmap Load(Stream stream)
149145
{
150146
for (int x = 0; x < imgWidth; x++)
151147
{
148+
// TODO: handle non-8-bit element lengths?
152149
if (numElements == 3)
153150
{
154151
bmpData[4 * (y * imgWidth + x) + 2] = bodyChunk[ptr++];
@@ -169,42 +166,46 @@ public static Bitmap Load(Stream stream)
169166
else if (compressionType == 5)
170167
{
171168
int scanLineSize = imgWidth;
172-
173-
byte[] uncompressed = new byte[bmpData.Length * 2];
174-
169+
byte[] uncompressed = new byte[scanLineSize * 2];
175170
int pos = 0;
176171

177172
for (int y = 0; y < imgHeight; y++)
178173
{
179-
int d;
180-
int v = 0;
181-
182-
for (int i = 0; i < scanLineSize; i++)
174+
for (int e = 0; e < numElements; e++)
183175
{
184-
d = bodyChunk[pos >> 1];
185-
if ((pos++ & 0x1) != 0) d &= 0xF;
186-
else d >>= 4;
187-
v += tvdcTable[d];
188-
uncompressed[i] = (byte)v;
189-
if (tvdcTable[d] == 0)
176+
int d = 0;
177+
int v = 0;
178+
179+
for (int i = 0; i < scanLineSize; i++)
190180
{
191181
d = bodyChunk[pos >> 1];
192-
if ((pos++ & 0x1) != 0) d &= 0xf;
182+
if ((pos++ & 1) != 0) d &= 0xF;
193183
else d >>= 4;
194-
while (d-- > 0) uncompressed[++i] = (byte)v;
184+
v += tvdcTable[d];
185+
uncompressed[i] = (byte)v;
186+
if (tvdcTable[d] == 0)
187+
{
188+
d = bodyChunk[pos >> 1];
189+
if ((pos++ & 1) != 0) d &= 0xF;
190+
else d >>= 4;
191+
while (d-- != 0) uncompressed[++i] = (byte)v;
192+
}
195193
}
196-
}
197194

198-
pos = (pos + 1) / 2;
195+
if (pos % 2 != 0) pos++;
199196

200-
int xx = 0;
201-
for (int x = 0; x < imgWidth; x++)
202-
{
203-
bmpData[4 * (y * imgWidth + x)] = uncompressed[xx];
204-
bmpData[4 * (y * imgWidth + x) + 1] = uncompressed[xx];
205-
bmpData[4 * (y * imgWidth + x) + 2] = uncompressed[xx];
206-
bmpData[4 * (y * imgWidth + x) + 3] = 0xFF;
207-
xx++;
197+
// TODO: handle non-8-bit element lengths?
198+
for (int x = 0; x < imgWidth; x++)
199+
{
200+
if (e < 3)
201+
{
202+
bmpData[4 * (y * imgWidth + x) + (2 - e)] = uncompressed[x];
203+
}
204+
else
205+
{
206+
bmpData[4 * (y * imgWidth + x) + e] = uncompressed[x];
207+
}
208+
}
208209
}
209210
}
210211
}

Diff for: ImageFormats/Picture.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public static Bitmap Load(Stream stream)
105105
bmp = IlbmReader.Load(stream);
106106
}
107107
else if ((header[0] == 'F') && (header[1] == 'O') && (header[2] == 'R') && (header[3] == 'M')
108-
&& (header[8] == 'D') && (header[9] == 'E') && (header[10] == 'E') && (header[11] == 'P'))
108+
&& (((header[8] == 'D') && (header[9] == 'E') && (header[10] == 'E') && (header[11] == 'P')) || ((header[8] == 'T') && (header[9] == 'V') && (header[10] == 'P') && (header[11] == 'P'))))
109109
{
110110
bmp = DeepReader.Load(stream);
111111
}

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ imageformats
1212
- .MAC (MacPaint)
1313
- .CUT (Dr. Halo)
1414
- .XPM (X Window PixMap)
15+
- .DEEP (TVPaint IFF DEEP images)
1516
- .FITS (experimental support)
1617
- .DICOM (experimental support)
1718
- .DDS (DirectDraw Surface images)

0 commit comments

Comments
 (0)