removed clang-format and clang_complete files from the repo
[dosdemo] / src / rlebmap.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include "rlebmap.h"
4
5 /* Number of numbers per scanline. Each streak has 2 numbers (start, length) */
6 #define RLE_ELEMENTS_PER_SCANLINE RLE_STREAKS_PER_SCANLINE * 2
7
8 /* Two RLE_TYPE elements per streak (= start,length) */
9 #define RLE_BYTES_PER_SCANLINE RLE_ELEMENTS_PER_SCANLINE * sizeof(RLE_TYPE)
10
11 /* RLE_TYPE count required for storing an RLE of w,h */
12 static int rleWorstCaseElementCount(int w, int h) {
13         /* Allocate an extra worst case for one scanline, which is w/2 streaks = w
14          * (start,length) elements */
15         return h * RLE_ELEMENTS_PER_SCANLINE + w;
16 }
17
18 /* Byte count of the 'scans' buffer */
19 static int rleScansByteCount(RleBitmap *rle) {
20         return rleWorstCaseElementCount(rle->w, rle->h) * sizeof(RLE_TYPE);
21 }
22
23 RleBitmap *rleCreate(unsigned int w, unsigned int h) {
24         RleBitmap *ret = malloc(sizeof(RleBitmap));
25         ret->w = w;
26         ret->h = h;
27
28         /* Allocate scans */
29         ret->scans = calloc(rleWorstCaseElementCount(w, h), sizeof(RLE_TYPE));
30
31         return ret;
32 }
33
34 void rleDestroy(RleBitmap *b) {
35         if (!b)
36                 return;
37         free(b->scans);
38         free(b);
39 }
40
41 void rleClear(RleBitmap *rle) { memset(rle->scans, 0, rleScansByteCount(rle)); }
42
43 RleBitmap *rleEncode(RleBitmap *rle, unsigned char *pixels, unsigned int pixelsW,
44                      unsigned int pixelsH) {
45         int x = 0;
46         int y = 0;
47         int streakActive = 0;
48         int currentStreakLength = 0;
49         RLE_TYPE *output = 0;
50         unsigned char *currentInputPixel = pixels;
51
52         /* https://www.youtube.com/watch?v=RKMR02o1I88&feature=youtu.be&t=55 */
53         if (!rle)
54                 rle = rleCreate(pixelsW, pixelsH);
55         else
56                 rleClear(rle); /* The following code assumes cleared array */
57
58         for (y = 0; y < pixelsH; y++) {
59                 /* Go to the beginning of the RLE scan */
60                 output = rle->scans + y * RLE_ELEMENTS_PER_SCANLINE;
61
62                 for (x = 0; x < pixelsW; x++) {
63                         if (*currentInputPixel++) {
64                                 if (streakActive) {
65                                         if (currentStreakLength >= RLE_MAX_STREAK_LENGTH) {
66                                                 /* Do not allow streaks of more than max length -
67                                                  * close current streak */
68                                                 *output++ = (RLE_TYPE)currentStreakLength;
69
70                                                 /* Begin new streak at current x */
71                                                 *output++ = (RLE_TYPE)x;
72                                                 currentStreakLength = 0;
73                                         }
74                                 } else {
75                                         /* Begin new streak */
76                                         *output++ = (RLE_TYPE)x;
77                                         currentStreakLength = 0;
78                                         streakActive = 1;
79                                 }
80                                 currentStreakLength++;
81                         } else {
82                                 if (streakActive) {
83                                         /* Close current streak */
84                                         *output++ = (RLE_TYPE)currentStreakLength;
85                                         currentStreakLength = 0;
86                                         streakActive = 0;
87                                 }
88                         } /* End if (current pixel on) */
89                 }         /* End for (all x) */
90
91                 /* We reached the end of the scan - close any active streak */
92                 if (streakActive) {
93                         *output++ = (RLE_TYPE)currentStreakLength;
94                 }
95                 streakActive = 0;
96                 currentStreakLength = 0;
97         } /* End for (all scans */
98
99         return rle;
100 }
101
102 void rleDistributeStreaks(RleBitmap *rle) {
103         int scanline = 0;
104         int halfW = rle->w >> 1;
105         RLE_TYPE *ptr = 0;
106         RLE_TYPE tmp = 0;
107
108 #define LAST_STREAK RLE_STREAKS_PER_SCANLINE
109
110         ptr = rle->scans;
111         for (scanline = 0; scanline < rle->h; scanline++) {
112                 if (ptr[0] >= halfW) {
113                         /* Exchange first with last streak */
114                         tmp = ptr[0];
115                         ptr[0] = ptr[LAST_STREAK * 2 - 2];
116                         ptr[LAST_STREAK * 2 - 2] = tmp;
117                         tmp = ptr[1];
118                         ptr[1] = ptr[LAST_STREAK * 2 - 1];
119                         ptr[LAST_STREAK * 2 - 1] = tmp;
120                 }
121
122                 ptr += 8;
123         }
124 }
125
126 void rleBlit(RleBitmap *rle, unsigned short *dst, int dstW, int dstH, int dstStride, int blitX,
127              int blitY) {
128         int scanline = 0;
129         int streakPos = 0;
130         int streakLength = 0;
131         int streak = 0;
132         RLE_TYPE *input = rle->scans;
133         unsigned short *output;
134         unsigned int *output32;
135
136         dst += blitX + blitY * dstStride;
137
138         for (scanline = blitY; scanline < blitY + rle->h; scanline++) {
139                 if (scanline < 0 || scanline >= dstH)
140                         continue;
141                 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
142                         streakPos = (int)*input++;
143                         streakLength = (int)*input++;
144
145                         if ((streakPos + blitX) <= 0)
146                                 continue;
147
148                         output = dst + streakPos;
149
150                         /* Check if we need to write the first pixel as 16bit */
151                         if (streakLength % 2) {
152                                 *output++ = RLE_FILL_COLOR;
153                         }
154
155                         /* Then, write 2 pixels at a time */
156                         streakLength >>= 1;
157                         output32 = (unsigned int *)output;
158                         while (streakLength--) {
159                                 *output32++ = RLE_FILL_COLOR_32;
160                         }
161                 }
162
163                 dst += dstStride;
164         }
165 }
166
167 /* This is madness. We could at least check that we are not interpolating from 0 -> something
168  * (length). This could remove the need for 'distributeScans' */
169 void interpolateScan(RLE_TYPE *output, RLE_TYPE *a, RLE_TYPE *b, float t) {
170         static int div = 1 << 23;
171         int ti, i;
172
173         t += 1.0f;
174         ti = (*((unsigned int *)&t)) & 0x7FFFFF;
175
176         for (i = 0; i < RLE_ELEMENTS_PER_SCANLINE; i++) {
177                 if (*a == 0) {
178                         *output++ = *b++;
179                         a++;
180                 } else {
181                         if (*b == 0) {
182                                 *output++ = *a++;
183                                 b++;
184                         } else {
185                                 *output++ = ((*b++ * ti) + (*a++ * (div - ti))) >> 23;
186                         }
187                 }
188         }
189 }
190
191 void rleBlitScale(RleBitmap *rle, unsigned short *dst, int dstW, int dstH, int dstStride, int blitX,
192                   int blitY, float scaleX, float scaleY) {
193         int scanline = 0;
194         int streakPos = 0;
195         int streakLength = 0;
196         int streak = 0;
197         unsigned short *output;
198         unsigned int *output32;
199         unsigned char *input;
200         int scanlineCounter = 0;
201         int scaleXFixed;
202         static unsigned char scan[512];
203
204         /*int blitW = (int)(rle->w * scaleX + 0.5f);*/
205         int blitH = (int)(rle->h * scaleY + 0.5f);
206
207         /* From this point on, scaleY will be inverted */
208         scaleY = 1.0f / scaleY;
209
210         scaleXFixed = (int)(scaleX * (float)(1 << RLE_FIXED_BITS) + 0.5f);
211
212         dst += blitX + blitY * dstStride;
213
214         for (scanline = blitY; scanline < blitY + blitH; scanline++) {
215                 float normalScan = scanlineCounter * scaleY; /* ScaleY  is inverted */
216                 unsigned char *scan0 = rle->scans + RLE_BYTES_PER_SCANLINE * (int)normalScan;
217                 unsigned char *scan1 = scan0 + RLE_BYTES_PER_SCANLINE;
218                 normalScan -= (int)normalScan;
219                 interpolateScan(scan, scan0, scan1, normalScan);
220                 input = scan;
221                 scanlineCounter++;
222
223                 if (scanline < 0 || scanline >= dstH)
224                         continue;
225                 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
226                         streakPos = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
227                         streakLength = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
228
229                         if ((streakPos + blitX) <= 0)
230                                 continue;
231
232                         output = dst + streakPos;
233
234                         /* Check if we need to write the first pixel as 16bit */
235                         if (streakLength % 2) {
236                                 *output++ = RLE_FILL_COLOR;
237                         }
238
239                         /* Then, write 2 pixels at a time */
240                         streakLength >>= 1;
241                         output32 = (unsigned int *)output;
242                         while (streakLength--) {
243                                 *output32++ = RLE_FILL_COLOR_32;
244                         }
245                 }
246
247                 dst += dstStride;
248         }
249 }
250
251 void rleBlitScaleInv(RleBitmap *rle, unsigned short *dst, int dstW, int dstH, int dstStride,
252                      int blitX, int blitY, float scaleX, float scaleY) {
253         int scanline = 0;
254         int streakPos = 0;
255         int streakLength = 0;
256         int streak = 0;
257         unsigned short *output;
258         unsigned int *output32;
259         unsigned char *input;
260         int scanlineCounter = 0;
261         int scaleXFixed;
262         static unsigned char scan[512];
263
264         /*int blitW = (int)(rle->w * scaleX + 0.5f);*/
265         int blitH = (int)(rle->h * scaleY + 0.5f);
266
267         /* From this point on, scaleY will be inverted */
268         scaleY = 1.0f / scaleY;
269
270         scaleXFixed = (int)(scaleX * (float)(1 << RLE_FIXED_BITS) + 0.5f);
271
272         dst += blitX + blitY * dstStride;
273
274         for (scanline = blitY; scanline > blitY - blitH; scanline--) {
275                 float normalScan = scanlineCounter * scaleY; /* ScaleY is inverted */
276                 unsigned char *scan0 = rle->scans + RLE_BYTES_PER_SCANLINE * (int)normalScan;
277                 unsigned char *scan1 = scan0 + RLE_BYTES_PER_SCANLINE;
278                 normalScan -= (int)normalScan;
279                 interpolateScan(scan, scan0, scan1, normalScan);
280                 input = scan;
281                 scanlineCounter++;
282
283                 if (scanline < 0 || scanline >= dstH)
284                         continue;
285                 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
286                         streakPos = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
287                         streakLength = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
288
289                         if ((streakPos + blitX) <= 0)
290                                 continue;
291
292                         output = dst + streakPos;
293
294                         /* Check if we need to write the first pixel as 16bit */
295                         if (streakLength % 2) {
296                                 *output++ = RLE_FILL_COLOR;
297                         }
298
299                         /* Then, write 2 pixels at a time */
300                         streakLength >>= 1;
301                         output32 = (unsigned int *)output;
302                         while (streakLength--) {
303                                 *output32++ = RLE_FILL_COLOR_32;
304                         }
305                 }
306
307                 dst -= dstStride;
308         }
309 }