5 /* Number of numbers per scanline. Each streak has 2 numbers (start, length) */
6 #define RLE_ELEMENTS_PER_SCANLINE RLE_STREAKS_PER_SCANLINE * 2
8 /* Two RLE_TYPE elements per streak (= start,length) */
9 #define RLE_BYTES_PER_SCANLINE RLE_ELEMENTS_PER_SCANLINE * sizeof(RLE_TYPE)
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;
18 /* Byte count of the 'scans' buffer */
19 static int rleScansByteCount(RleBitmap *rle) {
20 return rleWorstCaseElementCount(rle->w, rle->h) * sizeof(RLE_TYPE);
23 RleBitmap *rleCreate(unsigned int w, unsigned int h) {
24 RleBitmap *ret = malloc(sizeof(RleBitmap));
29 ret->scans = calloc(rleWorstCaseElementCount(w, h), sizeof(RLE_TYPE));
34 void rleDestroy(RleBitmap *b) {
41 void rleClear(RleBitmap *rle) { memset(rle->scans, 0, rleScansByteCount(rle)); }
43 RleBitmap *rleEncode(RleBitmap *rle, unsigned char *pixels, unsigned int pixelsW,
44 unsigned int pixelsH) {
48 int currentStreakLength = 0;
50 unsigned char *currentInputPixel = pixels;
52 /* https://www.youtube.com/watch?v=RKMR02o1I88&feature=youtu.be&t=55 */
54 rle = rleCreate(pixelsW, pixelsH);
56 rleClear(rle); /* The following code assumes cleared array */
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;
62 for (x = 0; x < pixelsW; x++) {
63 if (*currentInputPixel++) {
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;
70 /* Begin new streak at current x */
71 *output++ = (RLE_TYPE)x;
72 currentStreakLength = 0;
75 /* Begin new streak */
76 *output++ = (RLE_TYPE)x;
77 currentStreakLength = 0;
80 currentStreakLength++;
83 /* Close current streak */
84 *output++ = (RLE_TYPE)currentStreakLength;
85 currentStreakLength = 0;
88 } /* End if (current pixel on) */
89 } /* End for (all x) */
91 /* We reached the end of the scan - close any active streak */
93 *output++ = (RLE_TYPE)currentStreakLength;
96 currentStreakLength = 0;
97 } /* End for (all scans */
102 void rleDistributeStreaks(RleBitmap *rle) {
104 int halfW = rle->w >> 1;
108 #define LAST_STREAK RLE_STREAKS_PER_SCANLINE
111 for (scanline = 0; scanline < rle->h; scanline++) {
112 if (ptr[0] >= halfW) {
113 /* Exchange first with last streak */
115 ptr[0] = ptr[LAST_STREAK * 2 - 2];
116 ptr[LAST_STREAK * 2 - 2] = tmp;
118 ptr[1] = ptr[LAST_STREAK * 2 - 1];
119 ptr[LAST_STREAK * 2 - 1] = tmp;
126 void rleBlit(RleBitmap *rle, unsigned short *dst, int dstW, int dstH, int dstStride, int blitX,
130 int streakLength = 0;
132 RLE_TYPE *input = rle->scans;
133 unsigned short *output;
134 unsigned int *output32;
136 dst += blitX + blitY * dstStride;
138 for (scanline = blitY; scanline < blitY + rle->h; scanline++) {
139 if (scanline < 0 || scanline >= dstH)
141 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
142 streakPos = (int)*input++;
143 streakLength = (int)*input++;
145 if ((streakPos + blitX) <= 0)
148 output = dst + streakPos;
150 /* Check if we need to write the first pixel as 16bit */
151 if (streakLength % 2) {
152 *output++ = RLE_FILL_COLOR;
155 /* Then, write 2 pixels at a time */
157 output32 = (unsigned int *)output;
158 while (streakLength--) {
159 *output32++ = RLE_FILL_COLOR_32;
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;
174 ti = (*((unsigned int *)&t)) & 0x7FFFFF;
176 for (i = 0; i < RLE_ELEMENTS_PER_SCANLINE; i++) {
185 *output++ = ((*b++ * ti) + (*a++ * (div - ti))) >> 23;
191 void rleBlitScale(RleBitmap *rle, unsigned short *dst, int dstW, int dstH, int dstStride, int blitX,
192 int blitY, float scaleX, float scaleY) {
195 int streakLength = 0;
197 unsigned short *output;
198 unsigned int *output32;
199 unsigned char *input;
200 int scanlineCounter = 0;
202 static unsigned char scan[512];
204 /*int blitW = (int)(rle->w * scaleX + 0.5f);*/
205 int blitH = (int)(rle->h * scaleY + 0.5f);
207 /* From this point on, scaleY will be inverted */
208 scaleY = 1.0f / scaleY;
210 scaleXFixed = (int)(scaleX * (float)(1 << RLE_FIXED_BITS) + 0.5f);
212 dst += blitX + blitY * dstStride;
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);
223 if (scanline < 0 || scanline >= dstH)
225 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
226 streakPos = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
227 streakLength = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
229 if ((streakPos + blitX) <= 0)
232 output = dst + streakPos;
234 /* Check if we need to write the first pixel as 16bit */
235 if (streakLength % 2) {
236 *output++ = RLE_FILL_COLOR;
239 /* Then, write 2 pixels at a time */
241 output32 = (unsigned int *)output;
242 while (streakLength--) {
243 *output32++ = RLE_FILL_COLOR_32;
251 void rleBlitScaleInv(RleBitmap *rle, unsigned short *dst, int dstW, int dstH, int dstStride,
252 int blitX, int blitY, float scaleX, float scaleY) {
255 int streakLength = 0;
257 unsigned short *output;
258 unsigned int *output32;
259 unsigned char *input;
260 int scanlineCounter = 0;
262 static unsigned char scan[512];
264 /*int blitW = (int)(rle->w * scaleX + 0.5f);*/
265 int blitH = (int)(rle->h * scaleY + 0.5f);
267 /* From this point on, scaleY will be inverted */
268 scaleY = 1.0f / scaleY;
270 scaleXFixed = (int)(scaleX * (float)(1 << RLE_FIXED_BITS) + 0.5f);
272 dst += blitX + blitY * dstStride;
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);
283 if (scanline < 0 || scanline >= dstH)
285 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
286 streakPos = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
287 streakLength = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
289 if ((streakPos + blitX) <= 0)
292 output = dst + streakPos;
294 /* Check if we need to write the first pixel as 16bit */
295 if (streakLength % 2) {
296 *output++ = RLE_FILL_COLOR;
299 /* Then, write 2 pixels at a time */
301 output32 = (unsigned int *)output;
302 while (streakLength--) {
303 *output32++ = RLE_FILL_COLOR_32;