forked from unwind/gimpilbm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyterun1.c
155 lines (144 loc) · 2.79 KB
/
byterun1.c
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
#define MINRUN 3
#define MAXRUN 128
#define MAXDAT 128
#include "byterun1.h"
/**** ByteRun1 encoding/decoding ****/
static guint8 * packPutDump(guint8 *dest, guint8 nn, const guint8 *src)
{
*dest++ = ((gint8) nn) - 1;
memcpy(dest, src, nn);
dest += nn;
return dest;
}
static guint8 * packPutRun(guint8 *dest, guint8 nn, guint8 cc)
{
*dest++ = -(((gint8) nn) - 1);
*dest++ = cc;
return dest;
}
gint32 packRow(guint8 *odest, const guint8 *src, guint32 width)
{
guint8 *dest = odest;
guint8 ch, lastch;
enum {
run, dump
} mode = dump;
gint16 nbuf = 1;
gint16 rstart = 0;
guint8 buf[256]; /* ? 128 ? */
*buf = lastch = ch = *src++;
--width;
for(; width; --width)
{
buf[nbuf++] = ch = *src++;
switch(mode)
{
case dump:
if(nbuf > MAXDAT)
{
dest = packPutDump(dest, nbuf - 1, buf);
*buf = ch;
nbuf = 1;
rstart = 0;
}
else if(ch == lastch)
{
if(nbuf - rstart >= MINRUN)
{
if(rstart > 0)
dest = packPutDump(dest, rstart, buf);
mode = run;
}
else if(!rstart)
mode = run;
}
else
rstart = nbuf - 1;
break;
case run:
if((ch != lastch) || (nbuf - rstart > MAXRUN))
{
dest = packPutRun(dest, nbuf - 1 - rstart, lastch);
*buf = ch;
nbuf = 1;
rstart = 0;
mode = dump;
}
break;
}
lastch = ch;
}
if(mode == run)
dest = packPutRun(dest, nbuf - rstart, lastch);
else
dest = packPutDump(dest, nbuf, buf);
return dest - odest;
}
gboolean unpackRow(FILE *file, gint8 *bitlinebuf, gint32 bytesNeeded)
{
gboolean success = TRUE;
while(bytesNeeded > 0)
{
gint ch = fgetc(file);
if(EOF == ch)
{
fputs("Got EOF while decrunching!\n", stderr);
return 0;
}
ch = (gint8) ch;
if(ch >= 0)
{
gint skip = 0;
++ch;
if(ch > bytesNeeded)
{
skip = ch - bytesNeeded;
fprintf(stderr, "Bad compr.: %u byte(s) too much in row. Skipping.\n", skip);
ch = bytesNeeded;
}
if(fread(bitlinebuf, ch, 1, file) != 1)
{
fputs("Error reading compressed data, image might look confused.\n", stderr);
}
if(skip)
{
if(fseek(file, skip, SEEK_CUR))
{
fputs("Error skipping extraneous bytes in stream.\n", stderr);
}
}
bitlinebuf += ch;
bytesNeeded -= ch;
}
else if(ch != -128)
{
gint val;
ch = -ch + 1;
val = fgetc(file);
if(EOF == val)
{
fputs("Got EOF while decrunching!\n", stderr);
return FALSE;
}
else
{
if(ch > bytesNeeded)
{
fprintf(stderr, "Bad compr.: %u byte(s) too much in row.\n", ch - bytesNeeded);
ch = bytesNeeded;
}
memset(bitlinebuf, val, ch);
bitlinebuf += ch;
}
bytesNeeded -= ch;
}
else
{
/*fputs("unpackRow: Got -128!\n", stderr); */
/* Amiga inc. says that -128 is "NOP". */
}
}
if(bytesNeeded < 0)
success = 0;
return success;
}