1
1
using MechanikaDesign . ImageFormats ;
2
2
using System ;
3
- using System . Collections . Generic ;
4
3
using System . Drawing ;
5
4
using System . IO ;
6
5
using System . Text ;
7
6
8
7
9
8
/*
10
9
11
- Decoder for IFF (FORM-based ) images.
10
+ Decoder for IFF DEEP (TV Paint ) images.
12
11
13
12
Copyright 2013-2023 by Warren Galyen
14
13
https://www.mechanikadesign.com
@@ -72,10 +71,7 @@ public static Bitmap Load(Stream stream)
72
71
73
72
stream . Read ( tempBytes , 0 , 4 ) ;
74
73
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." ) ; }
79
75
80
76
while ( stream . Position < stream . Length )
81
77
{
@@ -149,6 +145,7 @@ public static Bitmap Load(Stream stream)
149
145
{
150
146
for ( int x = 0 ; x < imgWidth ; x ++ )
151
147
{
148
+ // TODO: handle non-8-bit element lengths?
152
149
if ( numElements == 3 )
153
150
{
154
151
bmpData [ 4 * ( y * imgWidth + x ) + 2 ] = bodyChunk [ ptr ++ ] ;
@@ -169,42 +166,46 @@ public static Bitmap Load(Stream stream)
169
166
else if ( compressionType == 5 )
170
167
{
171
168
int scanLineSize = imgWidth ;
172
-
173
- byte [ ] uncompressed = new byte [ bmpData . Length * 2 ] ;
174
-
169
+ byte [ ] uncompressed = new byte [ scanLineSize * 2 ] ;
175
170
int pos = 0 ;
176
171
177
172
for ( int y = 0 ; y < imgHeight ; y ++ )
178
173
{
179
- int d ;
180
- int v = 0 ;
181
-
182
- for ( int i = 0 ; i < scanLineSize ; i ++ )
174
+ for ( int e = 0 ; e < numElements ; e ++ )
183
175
{
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 ++ )
190
180
{
191
181
d = bodyChunk [ pos >> 1 ] ;
192
- if ( ( pos ++ & 0x1 ) != 0 ) d &= 0xf ;
182
+ if ( ( pos ++ & 1 ) != 0 ) d &= 0xF ;
193
183
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
+ }
195
193
}
196
- }
197
194
198
- pos = ( pos + 1 ) / 2 ;
195
+ if ( pos % 2 != 0 ) pos ++ ;
199
196
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
+ }
208
209
}
209
210
}
210
211
}
0 commit comments