📄 rleaccel.c
字号:
run = x - runstart; while(skip > max_transl_run) { ADD_TRANSL_COUNTS(max_transl_run, 0); skip -= max_transl_run; } len = MIN(run, max_transl_run); ADD_TRANSL_COUNTS(skip, len); dst += copy_transl(dst, src + runstart, len, sf, df); runstart += len; run -= len; while(run) { len = MIN(run, max_transl_run); ADD_TRANSL_COUNTS(0, len); dst += copy_transl(dst, src + runstart, len, sf, df); runstart += len; run -= len; } if(!blankline) lastline = dst; } while(x < w); src += surface->pitch >> 2; } dst = lastline; /* back up past trailing blank lines */ ADD_OPAQUE_COUNTS(0, 0); }#undef ADD_OPAQUE_COUNTS#undef ADD_TRANSL_COUNTS /* Now that we have it encoded, release the original pixels */ if((surface->flags & GAL_PREALLOC) != GAL_PREALLOC && (surface->flags & GAL_HWSURFACE) != GAL_HWSURFACE) { free( surface->pixels ); surface->pixels = NULL; } /* realloc the buffer to release unused memory */ { Uint8 *p = realloc(rlebuf, dst - rlebuf); if(!p) p = rlebuf; surface->map->sw_data->aux_data = p; } return 0;}static Uint32 getpix_8(Uint8 *srcbuf){ return *srcbuf;}static Uint32 getpix_16(Uint8 *srcbuf){ return *(Uint16 *)srcbuf;}static Uint32 getpix_24(Uint8 *srcbuf){ if(GAL_BYTEORDER == GAL_LIL_ENDIAN) return srcbuf[0] + (srcbuf[1] << 8) + (srcbuf[2] << 16); else return (srcbuf[0] << 16) + (srcbuf[1] << 8) + srcbuf[2];}static Uint32 getpix_32(Uint8 *srcbuf){ return *(Uint32 *)srcbuf;}typedef Uint32 (*getpix_func)(Uint8 *);static getpix_func getpixes[4] = { getpix_8, getpix_16, getpix_24, getpix_32};static int RLEColorkeySurface(GAL_Surface *surface){ Uint8 *rlebuf, *dst; int maxn; int y; Uint8 *srcbuf, *curbuf, *lastline; int maxsize = 0; int skip, run; int bpp = surface->format->BytesPerPixel; getpix_func getpix; Uint32 ckey, rgbmask; int w, h; /* calculate the worst case size for the compressed surface */ switch(bpp) { case 1: /* worst case is alternating opaque and transparent pixels, starting with an opaque pixel */ maxsize = surface->h * 3 * (surface->w / 2 + 1) + 2; break; case 2: case 3: /* worst case is solid runs, at most 255 pixels wide */ maxsize = surface->h * (2 * (surface->w / 255 + 1) + surface->w * bpp) + 2; break; case 4: /* worst case is solid runs, at most 65535 pixels wide */ maxsize = surface->h * (4 * (surface->w / 65535 + 1) + surface->w * 4) + 4; break; } rlebuf = (Uint8 *)malloc(maxsize); if ( rlebuf == NULL ) { GAL_OutOfMemory(); return(-1); } /* Set up the conversion */ srcbuf = (Uint8 *)surface->pixels+surface->offset; curbuf = srcbuf; maxn = bpp == 4 ? 65535 : 255; skip = run = 0; dst = rlebuf; rgbmask = ~surface->format->Amask; ckey = surface->format->colorkey & rgbmask; lastline = dst; getpix = getpixes[bpp - 1]; w = surface->w; h = surface->h;#define ADD_COUNTS(n, m) \ if(bpp == 4) { \ ((Uint16 *)dst)[0] = n; \ ((Uint16 *)dst)[1] = m; \ dst += 4; \ } else { \ dst[0] = n; \ dst[1] = m; \ dst += 2; \ } for(y = 0; y < h; y++) { int x = 0; int blankline = 0; do { int run, skip, len; int runstart; int skipstart = x; /* find run of transparent, then opaque pixels */ while(x < w && (getpix(srcbuf + x * bpp) & rgbmask) == ckey) x++; runstart = x; while(x < w && (getpix(srcbuf + x * bpp) & rgbmask) != ckey) x++; skip = runstart - skipstart; if(skip == w) blankline = 1; run = x - runstart; /* encode segment */ while(skip > maxn) { ADD_COUNTS(maxn, 0); skip -= maxn; } len = MIN(run, maxn); ADD_COUNTS(skip, len); memcpy(dst, srcbuf + runstart * bpp, len * bpp); dst += len * bpp; run -= len; runstart += len; while(run) { len = MIN(run, maxn); ADD_COUNTS(0, len); memcpy(dst, srcbuf + runstart * bpp, len * bpp); dst += len * bpp; runstart += len; run -= len; } if(!blankline) lastline = dst; } while(x < w); srcbuf += surface->pitch; } dst = lastline; /* back up bast trailing blank lines */ ADD_COUNTS(0, 0);#undef ADD_COUNTS /* Now that we have it encoded, release the original pixels */ if((surface->flags & GAL_PREALLOC) != GAL_PREALLOC && (surface->flags & GAL_HWSURFACE) != GAL_HWSURFACE) { free( surface->pixels ); surface->pixels = NULL; } /* realloc the buffer to release unused memory */ { /* If realloc returns NULL, the original block is left intact */ Uint8 *p = realloc(rlebuf, dst - rlebuf); if(!p) p = rlebuf; surface->map->sw_data->aux_data = p; } return(0);}int GAL_RLESurface(GAL_Surface *surface){ int retcode; /* Clear any previous RLE conversion */ if ( (surface->flags & GAL_RLEACCEL) == GAL_RLEACCEL ) { GAL_UnRLESurface(surface, 1); } /* We don't support RLE encoding of bitmaps */ if ( surface->format->BitsPerPixel < 8 ) { return(-1); }#if 0 /* Lock the surface if it's in hardware */ if ( surface->flags & (GAL_HWSURFACE|GAL_ASYNCBLIT) ) { GAL_VideoDevice *video = current_video; GAL_VideoDevice *this = current_video; if ( video->LockHWSurface(this, surface) < 0 ) { return(-1); } }#endif /* Encode */ if((surface->flags & GAL_SRCCOLORKEY) == GAL_SRCCOLORKEY) { retcode = RLEColorkeySurface(surface); } else { if((surface->flags & GAL_SRCALPHA) == GAL_SRCALPHA && surface->format->Amask != 0) retcode = RLEAlphaSurface(surface); else retcode = -1; /* no RLE for per-surface alpha sans ckey */ }#if 0 /* Unlock the surface if it's in hardware */ if ( surface->flags & (GAL_HWSURFACE|GAL_ASYNCBLIT) ) { GAL_VideoDevice *video = current_video; GAL_VideoDevice *this = current_video; video->UnlockHWSurface(this, surface); }#endif if(retcode < 0) return -1; /* The surface is now accelerated */ surface->flags |= GAL_RLEACCEL; return(0);}/* * Un-RLE a surface with pixel alpha * This may not give back exactly the image before RLE-encoding; all * completely transparent pixels will be lost, and colour and alpha depth * may have been reduced (when encoding for 16bpp targets). */static void UnRLEAlpha(GAL_Surface *surface){ Uint8 *srcbuf; Uint32 *dst; GAL_PixelFormat *sf = surface->format; RLEDestFormat *df = surface->map->sw_data->aux_data; int (*uncopy_opaque)(Uint32 *, void *, int, RLEDestFormat *, GAL_PixelFormat *); int (*uncopy_transl)(Uint32 *, void *, int, RLEDestFormat *, GAL_PixelFormat *); int w = surface->w; int bpp = df->BytesPerPixel; if(bpp == 2) { uncopy_opaque = uncopy_opaque_16; uncopy_transl = uncopy_transl_16; } else { uncopy_opaque = uncopy_transl = uncopy_32; } surface->pixels = malloc(surface->h * surface->pitch); /* fill background with transparent pixels */ memset(surface->pixels, 0, surface->h * surface->pitch); dst = surface-
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -