rle.c
4.31 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* img_getrowsize, img_setrowsize, img_rle_compact, img_rle_expand -
*
* Paul Haeberli - 1984
*
*/
#include <stdio.h>
#include "image.h"
int img_getrowsize(IMAGE *image)
{
switch(image->dim) {
case 1:
return image->rowsize[0];
case 2:
return image->rowsize[image->y];
case 3:
return image->rowsize[image->y+image->z*image->ysize];
}
return 0;
}
void img_setrowsize(IMAGE *image, int cnt, int y, int z)
{
int *sizeptr;
if(img_badrow(image,y,z))
return;
switch(image->dim) {
case 1:
sizeptr = (int *)&image->rowsize[0];
image->rowstart[0] = image->rleend;
break;
case 2:
sizeptr = (int *)&image->rowsize[y];
image->rowstart[y] = image->rleend;
break;
case 3:
sizeptr = (int *)&image->rowsize[y+z*image->ysize];
image->rowstart[y+z*image->ysize] = image->rleend;
}
if(*sizeptr != -1)
image->wastebytes += *sizeptr;
*sizeptr = cnt;
image->rleend += cnt;
}
#define docompact \
while(iptr<ibufend) { \
sptr = iptr; \
iptr += 2; \
while((iptr<ibufend)&&((iptr[-2]!=iptr[-1])||(iptr[-1]!=iptr[0])))\
iptr++; \
iptr -= 2; \
count = iptr-sptr; \
while(count) { \
todo = count>126 ? 126:count; \
count -= todo; \
*optr++ = 0x80|todo; \
while(todo--) \
*optr++ = *sptr++; \
} \
sptr = iptr; \
cc = *iptr++; \
while( (iptr<ibufend) && (*iptr == cc) ) \
iptr++; \
count = iptr-sptr; \
while(count) { \
todo = count>126 ? 126:count; \
count -= todo; \
*optr++ = todo; \
*optr++ = cc; \
} \
} \
*optr++ = 0;
int img_rle_compact(unsigned short *expbuf, int ibpp,
unsigned short *rlebuf, int obpp, int cnt)
{
if(ibpp == 1 && obpp == 1) {
register unsigned char *iptr = (unsigned char *)expbuf;
register unsigned char *ibufend = iptr+cnt;
register unsigned char *sptr;
register unsigned char *optr = (unsigned char *)rlebuf;
register unsigned short todo, cc;
register int count;
docompact;
return optr - (unsigned char *)rlebuf;
} else if(ibpp == 1 && obpp == 2) {
register unsigned char *iptr = (unsigned char *)expbuf;
register unsigned char *ibufend = iptr+cnt;
register unsigned char *sptr;
register unsigned short *optr = rlebuf;
register unsigned short todo, cc;
register int count;
docompact;
return optr - rlebuf;
} else if(ibpp == 2 && obpp == 1) {
register unsigned short *iptr = expbuf;
register unsigned short *ibufend = iptr+cnt;
register unsigned short *sptr;
register unsigned char *optr = (unsigned char *)rlebuf;
register unsigned short todo, cc;
register int count;
docompact;
return optr - (unsigned char *)rlebuf;
} else if(ibpp == 2 && obpp == 2) {
register unsigned short *iptr = expbuf;
register unsigned short *ibufend = iptr+cnt;
register unsigned short *sptr;
register unsigned short *optr = rlebuf;
register unsigned short todo, cc;
register int count;
docompact;
return optr - rlebuf;
} else {
i_errhdlr("rle_compact: bad bpp: %d %d\n",ibpp,obpp);
return 0;
}
}
#define doexpand \
while(1) { \
pixel = *iptr++; \
if ( !(count = (pixel & 0x7f)) ) \
return; \
if(pixel & 0x80) { \
while(count--) \
*optr++ = *iptr++; \
} else { \
pixel = *iptr++; \
while(count--) \
*optr++ = pixel; \
} \
}
void img_rle_expand(unsigned short *rlebuf, int ibpp,
unsigned short *expbuf, int obpp)
{
if(ibpp == 1 && obpp == 1) {
register unsigned char *iptr = (unsigned char *)rlebuf;
register unsigned char *optr = (unsigned char *)expbuf;
register unsigned short pixel,count;
doexpand;
} else if(ibpp == 1 && obpp == 2) {
register unsigned char *iptr = (unsigned char *)rlebuf;
register unsigned short *optr = expbuf;
register unsigned short pixel,count;
doexpand;
} else if(ibpp == 2 && obpp == 1) {
register unsigned short *iptr = rlebuf;
register unsigned char *optr = (unsigned char *)expbuf;
register unsigned short pixel,count;
doexpand;
} else if(ibpp == 2 && obpp == 2) {
register unsigned short *iptr = rlebuf;
register unsigned short *optr = expbuf;
register unsigned short pixel,count;
doexpand;
} else
i_errhdlr("rle_expand: bad bpp: %d %d\n",ibpp,obpp);
}