123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637 |
- #include "SDL_gfxBlitFunc.h"
- const unsigned int GFX_ALPHA_ADJUST_ARRAY[256] = {
- 0,
- 15,
- 22,
- 27,
- 31,
- 35,
- 39,
- 42,
- 45,
- 47,
- 50,
- 52,
- 55,
- 57,
- 59,
- 61,
- 63,
- 65,
- 67,
- 69,
- 71,
- 73,
- 74,
- 76,
- 78,
- 79,
- 81,
- 82,
- 84,
- 85,
- 87,
- 88,
- 90,
- 91,
- 93,
- 94,
- 95,
- 97,
- 98,
- 99,
- 100,
- 102,
- 103,
- 104,
- 105,
- 107,
- 108,
- 109,
- 110,
- 111,
- 112,
- 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,
- 141,
- 142,
- 143,
- 144,
- 145,
- 146,
- 147,
- 148,
- 148,
- 149,
- 150,
- 151,
- 152,
- 153,
- 153,
- 154,
- 155,
- 156,
- 157,
- 158,
- 158,
- 159,
- 160,
- 161,
- 162,
- 162,
- 163,
- 164,
- 165,
- 165,
- 166,
- 167,
- 168,
- 168,
- 169,
- 170,
- 171,
- 171,
- 172,
- 173,
- 174,
- 174,
- 175,
- 176,
- 177,
- 177,
- 178,
- 179,
- 179,
- 180,
- 181,
- 182,
- 182,
- 183,
- 184,
- 184,
- 185,
- 186,
- 186,
- 187,
- 188,
- 188,
- 189,
- 190,
- 190,
- 191,
- 192,
- 192,
- 193,
- 194,
- 194,
- 195,
- 196,
- 196,
- 197,
- 198,
- 198,
- 199,
- 200,
- 200,
- 201,
- 201,
- 202,
- 203,
- 203,
- 204,
- 205,
- 205,
- 206,
- 206,
- 207,
- 208,
- 208,
- 209,
- 210,
- 210,
- 211,
- 211,
- 212,
- 213,
- 213,
- 214,
- 214,
- 215,
- 216,
- 216,
- 217,
- 217,
- 218,
- 218,
- 219,
- 220,
- 220,
- 221,
- 221,
- 222,
- 222,
- 223,
- 224,
- 224,
- 225,
- 225,
- 226,
- 226,
- 227,
- 228,
- 228,
- 229,
- 229,
- 230,
- 230,
- 231,
- 231,
- 232,
- 233,
- 233,
- 234,
- 234,
- 235,
- 235,
- 236,
- 236,
- 237,
- 237,
- 238,
- 238,
- 239,
- 240,
- 240,
- 241,
- 241,
- 242,
- 242,
- 243,
- 243,
- 244,
- 244,
- 245,
- 245,
- 246,
- 246,
- 247,
- 247,
- 248,
- 248,
- 249,
- 249,
- 250,
- 250,
- 251,
- 251,
- 252,
- 252,
- 253,
- 253,
- 254,
- 255
- };
- void _SDL_gfxBlitBlitterRGBA(SDL_gfxBlitInfo *info) {
- int width = info->d_width;
- int height = info->d_height;
- Uint8 *src = info->s_pixels;
- int srcskip = info->s_skip;
- Uint8 *dst = info->d_pixels;
- int dstskip = info->d_skip;
- SDL_PixelFormat *srcfmt = info->src;
- SDL_PixelFormat *dstfmt = info->dst;
- Uint8 srcbpp = srcfmt->BytesPerPixel;
- Uint8 dstbpp = dstfmt->BytesPerPixel;
- while (height--) {
- GFX_DUFFS_LOOP4({
- Uint32 pixel;
- unsigned sR;
- unsigned sG;
- unsigned sB;
- unsigned sA;
- unsigned dR;
- unsigned dG;
- unsigned dB;
- unsigned dA;
- unsigned sAA;
- GFX_DISASSEMBLE_RGBA(src, srcbpp, srcfmt, pixel, sR, sG, sB, sA);
- GFX_DISASSEMBLE_RGBA(dst, dstbpp, dstfmt, pixel, dR, dG, dB, dA);
- sAA = GFX_ALPHA_ADJUST_ARRAY[sA & 255];
- GFX_ALPHA_BLEND(sR, sG, sB, sAA, dR, dG, dB);
- dA |= sAA;
- GFX_ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
- src += srcbpp;
- dst += dstbpp;
- }, width);
- src += srcskip;
- dst += dstskip;
- }
- }
- int _SDL_gfxBlitRGBACall(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect) {
-
- if (srcrect->w && srcrect->h) {
- SDL_gfxBlitInfo info;
-
- #if (SDL_MINOR_VERSION == 3)
- info.s_pixels = (Uint8 *) src->pixels + (Uint16) srcrect->y * src->pitch + (Uint16) srcrect->x * src->format->BytesPerPixel;
- #else
- info.s_pixels = (Uint8 *) src->pixels + src->offset + (Uint16) srcrect->y * src->pitch +
- (Uint16) srcrect->x * src->format->BytesPerPixel;
- #endif
- info.s_width = srcrect->w;
- info.s_height = srcrect->h;
- info.s_skip = (int) (src->pitch - info.s_width * src->format->BytesPerPixel);
- #if (SDL_MINOR_VERSION == 3)
- info.d_pixels = (Uint8 *) dst->pixels + (Uint16) dstrect->y * dst->pitch + (Uint16) dstrect->x * dst->format->BytesPerPixel;
- #else
- info.d_pixels = (Uint8 *) dst->pixels + dst->offset + (Uint16) dstrect->y * dst->pitch +
- (Uint16) dstrect->x * dst->format->BytesPerPixel;
- #endif
- info.d_width = dstrect->w;
- info.d_height = dstrect->h;
- info.d_skip = (int) (dst->pitch - info.d_width * dst->format->BytesPerPixel);
- info.aux_data = NULL;
- info.src = src->format;
- info.table = NULL;
- info.dst = dst->format;
-
- _SDL_gfxBlitBlitterRGBA(&info);
- return 1;
- }
- return (0);
- }
- int SDL_gfxBlitRGBA(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect) {
- SDL_Rect sr, dr;
- int srcx, srcy, w, h;
-
- if (!src || !dst) {
- SDL_SetError("SDL_UpperBlit: passed a NULL surface");
- return (-1);
- }
- if ((src->locked) || (dst->locked)) {
- SDL_SetError("Surfaces must not be locked during blit");
- return (-1);
- }
-
- if (dstrect == NULL) {
- dr.x = dr.y = 0;
- dr.w = dst->w;
- dr.h = dst->h;
- } else {
- dr = *dstrect;
- }
-
- if (srcrect) {
- int maxw, maxh;
- srcx = srcrect->x;
- w = srcrect->w;
- if (srcx < 0) {
- w += srcx;
- dr.x -= srcx;
- srcx = 0;
- }
- maxw = src->w - srcx;
- if (maxw < w)
- w = maxw;
- srcy = srcrect->y;
- h = srcrect->h;
- if (srcy < 0) {
- h += srcy;
- dr.y -= srcy;
- srcy = 0;
- }
- maxh = src->h - srcy;
- if (maxh < h)
- h = maxh;
- } else {
- srcx = srcy = 0;
- w = src->w;
- h = src->h;
- }
-
- {
- SDL_Rect *clip = &dst->clip_rect;
- int dx, dy;
- dx = clip->x - dr.x;
- if (dx > 0) {
- w -= dx;
- dr.x += dx;
- srcx += dx;
- }
- dx = dr.x + w - clip->x - clip->w;
- if (dx > 0)
- w -= dx;
- dy = clip->y - dr.y;
- if (dy > 0) {
- h -= dy;
- dr.y += dy;
- srcy += dy;
- }
- dy = dr.y + h - clip->y - clip->h;
- if (dy > 0)
- h -= dy;
- }
- if (w > 0 && h > 0) {
- sr.x = srcx;
- sr.y = srcy;
- sr.w = dr.w = w;
- sr.h = dr.h = h;
- return (_SDL_gfxBlitRGBACall(src, &sr, dst, &dr));
- }
- return 0;
- }
- int SDL_gfxSetAlpha(SDL_Surface *src, Uint8 a) {
- #if SDL_BYTEORDER == SDL_BIG_ENDIAN
- const int alpha_offset = 0;
- #else
- const int alpha_offset = 3;
- #endif
- int i, j, row_skip;
- Uint8 *pixels;
-
- if ((src == NULL) ||
- (src->format == NULL) ||
- (src->format->BytesPerPixel != 4)) {
- SDL_SetError("SDL_gfxSetAlpha: Invalid input surface.");
- return -1;
- }
-
- if (SDL_MUSTLOCK(src)) {
- if (SDL_LockSurface(src) < 0) {
- return (-1);
- }
- }
-
- pixels = (Uint8 *) src->pixels;
- row_skip = (src->pitch - (4 * src->w));
- pixels += alpha_offset;
- for (i = 0; i < src->h; i++) {
- for (j = 0; j < src->w; j++) {
- *pixels = a;
- pixels += 4;
- }
- pixels += row_skip;
- }
-
- if (SDL_MUSTLOCK(src)) {
- SDL_UnlockSurface(src);
- }
- return 1;
- }
- int SDL_gfxMultiplyAlpha(SDL_Surface *src, Uint8 a) {
- #if SDL_BYTEORDER == SDL_BIG_ENDIAN
- const int alpha_offset = 0;
- #else
- const int alpha_offset = 3;
- #endif
- int i, j, row_skip;
- Uint8 *pixels;
-
- if ((src == NULL) ||
- (src->format == NULL) ||
- (src->format->BytesPerPixel != 4)) {
- SDL_SetError("SDL_gfxMultiplyAlpha: Invalid input surface.");
- return -1;
- }
-
- if (a == 255) {
- return 0;
- }
-
- if (SDL_MUSTLOCK(src)) {
- if (SDL_LockSurface(src) < 0) {
- return (-1);
- }
- }
-
- pixels = (Uint8 *) src->pixels;
- row_skip = (src->pitch - (4 * src->w));
- pixels += alpha_offset;
- for (i = 0; i < src->h; i++) {
- for (j = 0; j < src->w; j++) {
- *pixels = (Uint8) (((int) (*pixels) * a) >> 8);
- pixels += 4;
- }
- pixels += row_skip;
- }
-
- if (SDL_MUSTLOCK(src)) {
- SDL_UnlockSurface(src);
- }
- return 1;
- }
|