add: if (src_p1[mipmap->w] == ck1) a1[2] = 0;
context: TFB_DrawCanvas_Rescale_Trilinear (TFB_Canvas src_canvas, TFB_Canvas dest_canvas, TFB_Canvas src_mipmap, EXTENT size){	SDL_Surface *src = (SDL_Surface *)src_canvas;	SDL_Surface *dst = (SDL_Surface *)dest_canvas;	SDL_Surface *mipmap = (SDL_Surface *)src_mipmap;	SDL_PixelFormat *dstfmt = dst->format;	const int dstopaque = 0xff << dstfmt->Ashift;	const int w = size.width, h = size.height;	const int ALPHA_THRESHOLD = 128;		int ratio = (int)(256.0f * (((float)w / src->w) * 2.0f - 1.0f));	if (ratio < 0)		ratio = 0;	if (ratio > 256)		ratio = 256;	if (size.width > dst->w || size.height > dst->h) 	{		fprintf (stderr, "TFB_DrawCanvas_Rescale_Trilinear: Tried to scale image to size %d %d when dest_canvas has only dimensions of %d %d!  Failing.\n",			size.width, size.height, dst->w, dst->h);		return;	}	if (src->format->BytesPerPixel == 1 && dst->format->BytesPerPixel == 4 &&		mipmap->format->BytesPerPixel == 1)	{		SDL_Color *srcpal = src->format->palette->colors;		SDL_Color *mmpal = mipmap->format->palette->colors;		int fsx0 = (int)(65536.0f * (float)src->w / w);		int fsy0 = (int)(65536.0f * (float)src->h / h);		int fsx1 = (int)(65536.0f * (float)mipmap->w / w);		int fsy1 = (int)(65536.0f * (float)mipmap->h / h);		int sx0 = 0, sy0 = 0, sx1 = 0, sy1 = 0;		int x, y;		Uint32 ck0 = src->format->colorkey;		Uint32 ck1 = mipmap->format->colorkey;		SDL_LockSurface(src);		SDL_LockSurface(dst);		SDL_LockSurface(mipmap);				for (y = 0; y < h; ++y)		{			Uint32 *dst_p = (Uint32 *)dst->pixels + y * dst->w;			Uint8 *src_a0 = (Uint8 *)src->pixels + (sy0 >> 16) * src->pitch;			Uint8 *src_a1 = (Uint8 *)mipmap->pixels + (sy1 >> 16) * mipmap->pitch;			for (x = 0; x < w; ++x)			{				Uint8 u0 = (sx0 >> 8) & 0xff;				Uint8 u1 = (sx1 >> 8) & 0xff;				Uint8 v0 = (sy0 >> 8) & 0xff;				Uint8 v1 = (sy1 >> 8) & 0xff;				Uint8 ul0 = btable[255 - u0][255 - v0];				Uint8 ul1 = btable[255 - u1][255 - v1];				Uint8 ur0 = btable[u0][255 - v0];				Uint8 ur1 = btable[u1][255 - v1];				Uint8 ll0 = btable[255 - u0][v0];				Uint8 ll1 = btable[255 - u1][v1];				Uint8 lr0 = btable[u0][v0];				Uint8 lr1 = btable[u1][v1];				Uint8 r0[5], r1[5], g0[5], g1[5], b0[5], b1[5], a0[5], a1[5];				Uint8 c;								Uint8 *src_p0 = src_a0 + (sx0 >> 16);				Uint8 *src_p1 = src_a1 + (sx1 >> 16);				if ((sx0 >> 16) <= src->w - 2)				{											a0[0] = ((c = *src_p0) == ck0) ? 0 : 255;					r0[0] = srcpal[c].r;					g0[0] = srcpal[c].g;					b0[0] = srcpal[c].b;					a0[1] = ((c = *(src_p0 + 1)) == ck0) ? 0 : 255;					r0[1] = srcpal[c].r;					g0[1] = srcpal[c].g;					b0[1] = srcpal[c].b;					if ((sy0 >> 16) <= src->h - 2)					{						a0[2] = ((c = *(src_p0 + src->pitch)) == ck0) ? 0 : 255;						r0[2] = srcpal[c].r;						g0[2] = srcpal[c].g;						b0[2] = srcpal[c].b;						a0[3] = ((c = *(src_p0 + src->pitch + 1)) == ck0) ? 0 : 255;						r0[3] = srcpal[c].r;						g0[3] = srcpal[c].g;						b0[3] = srcpal[c].b;					}					else					{						a0[2] = a0[0];						r0[2] = r0[0];						g0[2] = g0[0];						b0[2] = b0[0];						a0[3] = a0[1];						r0[3] = r0[1];						g0[3] = g0[1];						b0[3] = b0[1];					}				}				else				{					a0[0] = a0[1] = ((c = *src_p0) == ck0) ? 0 : 255;					r0[0] = r0[1] = srcpal[c].r;					g0[0] = g0[1] = srcpal[c].g;					b0[0] = b0[1] = srcpal[c].b;					if ((sy0 >> 16) <= src->h - 2)					{						a0[2] = a0[3] = ((c = *(src_p0 + src->pitch)) == ck0) ? 0 : 255;						r0[2] = r0[3] = srcpal[c].r;						g0[2] = g0[3] = srcpal[c].g;						b0[2] = b0[3] = srcpal[c].b;					}					else					{						a0[2] = a0[3] = a0[0];						r0[2] = r0[3] = r0[0];						g0[2] = g0[3] = g0[0];						b0[2] = b0[3] = b0[0];					}				}				if ((sx1 >> 16) <= mipmap->w - 2)				{											a1[0] = ((c = *src_p1) == ck1) ? 0 : 255;					r1[0] = mmpal[c].r;					g1[0] = mmpal[c].g;					b1[0] = mmpal[c].b;					a1[1] = ((c = *(src_p1 + 1)) == ck1) ? 0 : 255;					r1[1] = mmpal[c].r;					g1[1] = mmpal[c].g;					b1[1] = mmpal[c].b;					if ((sy1 >> 16) <= mipmap->h - 2)					{						a1[2] = ((c = *(src_p1 + mipmap->pitch)) == ck1) ? 0 : 255;						r1[2] = mmpal[c].r;						g1[2] = mmpal[c].g;						b1[2] = mmpal[c].b;						a1[3] = ((c = *(src_p1 + mipmap->pitch + 1)) == ck1) ? 0 : 255;						r1[3] = mmpal[c].r;						g1[3] = mmpal[c].g;						b1[3] = mmpal[c].b;					}					else					{						a1[2] = a1[0];						r1[2] = r1[0];						g1[2] = g1[0];						b1[2] = b1[0];						a1[3] = a1[1];						r1[3] = r1[1];						g1[3] = g1[1];						b1[3] = b1[1];					}				}				else				{					a1[0] = a1[1] = ((c = *src_p1) == ck1) ? 0 : 255;					r1[0] = r1[1] = mmpal[c].r;					g1[0] = g1[1] = mmpal[c].g;					b1[0] = b1[1] = mmpal[c].b;					if ((sy1 >> 16) <= mipmap->h - 2)					{						a1[2] = a1[3] = ((c = *(src_p1 + mipmap->pitch)) == ck1) ? 0 : 255;						r1[2] = r1[3] = mmpal[c].r;						g1[2] = g1[3] = mmpal[c].g;						b1[2] = b1[3] = mmpal[c].b;					}					else					{						a1[2] = a1[3] = a1[0];						r1[2] = r1[3] = r1[0];						g1[2] = g1[3] = g1[0];						b1[2] = b1[3] = b1[0];					}				}				a0[4] = ((ul0 * a0[0]) >> 8) + ((ur0 * a0[1]) >> 8) + ((ll0 * a0[2]) >> 8) + ((lr0 * a0[3]) >> 8);				a1[4] = ((ul1 * a1[0]) >> 8) + ((ur1 * a1[1]) >> 8) + ((ll1 * a1[2]) >> 8) + ((lr1 * a1[3]) >> 8);								if ((((a0[4] - a1[4]) * ratio) >> 8) + a1[4] > ALPHA_THRESHOLD)				{					r0[4] = ((ul0 * r0[0]) >> 8) + ((ur0 * r0[1]) >> 8) + ((ll0 * r0[2]) >> 8) + ((lr0 * r0[3]) >> 8);					g0[4] = ((ul0 * g0[0]) >> 8) + ((ur0 * g0[1]) >> 8) + ((ll0 * g0[2]) >> 8) + ((lr0 * g0[3]) >> 8);					b0[4] = ((ul0 * b0[0]) >> 8) + ((ur0 * b0[1]) >> 8) + ((ll0 * b0[2]) >> 8) + ((lr0 * b0[3]) >> 8);					r1[4] = ((ul1 * r1[0]) >> 8) + ((ur1 * r1[1]) >> 8) + ((ll1 * r1[2]) >> 8) + ((lr1 * r1[3]) >> 8);					g1[4] = ((ul1 * g1[0]) >> 8) + ((ur1 * g1[1]) >> 8) + ((ll1 * g1[2]) >> 8) + ((lr1 * g1[3]) >> 8);					b1[4] = ((ul1 * b1[0]) >> 8) + ((ur1 * b1[1]) >> 8) + ((ll1 * b1[2]) >> 8) + ((lr1 * b1[3]) >> 8);					*dst_p++ =						(((((r0[4] - r1[4]) * ratio) >> 8) + r1[4]) << dstfmt->Rshift) |						(((((g0[4] - g1[4]) * ratio) >> 8) + g1[4]) << dstfmt->Gshift) |						(((((b0[4] - b1[4]) * ratio) >> 8) + b1[4]) << dstfmt->Bshift) |						dstopaque;				}				else				{					*dst_p++ = 0;				}				sx0 += fsx0;				sx1 += fsx1;			}			sx0 = sx1 = 0;			sy0 += fsy0;			sy1 += fsy1;		}		SDL_UnlockSurface(mipmap);		SDL_UnlockSurface(dst);		SDL_UnlockSurface(src);	}	else if (src->format->BytesPerPixel == 4 && dst->format->BytesPerPixel == 4 &&		mipmap->format->BytesPerPixel == 4)	{		int fsx0 = (int)(65536.0f * (float)src->w / w);		int fsy0 = (int)(65536.0f * (float)src->h / h);		int fsx1 = (int)(65536.0f * (float)mipmap->w / w);		int fsy1 = (int)(65536.0f * (float)mipmap->h / h);		int sx0 = 0, sy0 = 0, sx1 = 0, sy1 = 0;		int x, y;		SDL_LockSurface(src);		SDL_LockSurface(dst);		SDL_LockSurface(mipmap);				for (y = 0; y < h; ++y)		{			Uint32 *dst_p = (Uint32 *)dst->pixels + y * dst->w;			Uint32 *src_a0 = (Uint32 *)src->pixels + (sy0 >> 16) * src->w;			Uint32 *src_a1 = (Uint32 *)mipmap->pixels + (sy1 >> 16) * mipmap->w;			for (x = 0; x < w; ++x)			{				Uint8 u0 = (sx0 >> 8) & 0xff;				Uint8 u1 = (sx1 >> 8) & 0xff;				Uint8 v0 = (sy0 >> 8) & 0xff;				Uint8 v1 = (sy1 >> 8) & 0xff;				Uint8 ul0 = btable[255 - u0][255 - v0];				Uint8 ul1 = btable[255 - u1][255 - v1];				Uint8 ur0 = btable[u0][255 - v0];				Uint8 ur1 = btable[u1][255 - v1];				Uint8 ll0 = btable[255 - u0][v0];				Uint8 ll1 = btable[255 - u1][v1];				Uint8 lr0 = btable[u0][v0];				Uint8 lr1 = btable[u1][v1];				Uint8 r0[5], r1[5], g0[5], g1[5], b0[5], b1[5], a0[5], a1[5];								Uint32 *src_p0 = src_a0 + (sx0 >> 16);				Uint32 *src_p1 = src_a1 + (sx1 >> 16);				if ((sx0 >> 16) <= src->w - 2)				{											SDL_GetRGBA (*src_p0, src->format, &r0[0], &g0[0], &b0[0], &a0[0]);					SDL_GetRGBA (*(src_p0 + 1), src->format, &r0[1], &g0[1], &b0[1], &a0[1]);					if ((sy0 >> 16) <= src->h - 2)					{						SDL_GetRGBA (*(src_p0 + src->w), src->format, &r0[2], &g0[2], &b0[2], &a0[2]);						SDL_GetRGBA (*(src_p0 + src->w + 1), src->format, &r0[3], &g0[3], &b0[3], &a0[3]);					}					else					{						r0[2] = r0[0];						g0[2] = g0[0];						b0[2] = b0[0];						a0[2] = a0[0];						r0[3] = r0[1];						g0[3] = g0[1];						b0[3] = b0[1];						a0[3] = a0[1];					}				}				else				{					SDL_GetRGBA (*src_p0, src->format, &r0[0], &g0[0], &b0[0], &a0[0]);					r0[1] = r0[0];					g0[1] = g0[0];					b0[1] = b0[0];					a0[1] = a0[0];					if ((sy0 >> 16) <= src->h - 2)					{						SDL_GetRGBA (*(src_p0 + src->w), src->format, &r0[2], &g0[2], &b0[2], &a0[2]);						r0[3] = r0[2];						g0[3] = g0[2];						b0[3] = b0[2];						a0[3] = a0[2];					}					else					{						r0[2] = r0[3] = r0[0];						g0[2] = g0[3] = g0[0];						b0[2] = b0[3] = b0[0];						a0[2] = a0[3] = a0[0];					}				}				if ((sx1 >> 16) <= mipmap->w - 2)				{											SDL_GetRGBA (*src_p1, mipmap->format, &r1[0], &g1[0], &b1[0], &a1[0]);					SDL_GetRGBA (*(src_p1 + 1), mipmap->format, &r1[1], &g1[1], &b1[1], &a1[1]);					if ((sy1 >> 16) <= mipmap->h - 2)					{						SDL_GetRGBA (*(src_p1 + mipmap->w) , mipmap->format, &r1[2], &g1[2], &b1[2], &a1[2]);						SDL_GetRGBA (*(src_p1 + mipmap->w + 1), mipmap->format, &r1[3], &g1[3], &b1[3], &a1[3]);					}					else					{						r1[2] = r1[0];						g1[2] = g1[0];						b1[2] = b1[0];						a1[2] = a1[0];						r1[3] = r1[1];						g1[3] = g1[1];						b1[3] = b1[1];						a1[3] = a1[1];					}				}				else				{					SDL_GetRGBA (*src_p1, mipmap->format, &r1[0], &g1[0], &b1[0], &a1[0]);					r1[1] = r1[0];					g1[1] = g1[0];					b1[1] = b1[0];					a1[1] = a1[0];					if ((sy1 >> 16) <= mipmap->h - 2)					{						SDL_GetRGBA (*(src_p1 + mipmap->w), mipmap->format, &r1[2], &g1[2], &b1[2], &a1[2]);						r1[3] = r1[2];						g1[3] = g1[2];						b1[3] = b1[2];						a1[3] = a1[2];					}					else					{						r1[2] = r1[3] = r1[0];						g1[2] = g1[3] = g1[0];						b1[2] = b1[3] = b1[0];						a1[2] = a1[3] = a1[0];					}				}				a0[4] = ((ul0 * a0[0]) >> 8) + ((ur0 * a0[1]) >> 8) + ((ll0 * a0[2]) >> 8) + ((lr0 * a0[3]) >> 8);				a1[4] = ((ul1 * a1[0]) >> 8) + ((ur1 * a1[1]) >> 8) + ((ll1 * a1[2]) >> 8) + ((lr1 * a1[3]) >> 8);								if ((((a0[4] - a1[4]) * ratio) >> 8) + a1[4] > ALPHA_THRESHOLD)				{					r0[4] = ((ul0 * r0[0]) >> 8) + ((ur0 * r0[1]) >> 8) + ((ll0 * r0[2]) >> 8) + ((lr0 * r0[3]) >> 8);					g0[4] = ((ul0 * g0[0]) >> 8) + ((ur0 * g0[1]) >> 8) + ((ll0 * g0[2]) >> 8) + ((lr0 * g0[3]) >> 8);					b0[4] = ((ul0 * b0[0]) >> 8) + ((ur0 * b0[1]) >> 8) + ((ll0 * b0[2]) >> 8) + ((lr0 * b0[3]) >> 8);					r1[4] = ((ul1 * r1[0]) >> 8) + ((ur1 * r1[1]) >> 8) + ((ll1 * r1[2]) >> 8) + ((lr1 * r1[3]) >> 8);					g1[4] = ((ul1 * g1[0]) >> 8) + ((ur1 * g1[1]) >> 8) + ((ll1 * g1[2]) >> 8) + ((lr1 * g1[3]) >> 8);					b1[4] = ((ul1 * b1[0]) >> 8) + ((ur1 * b1[1]) >> 8) + ((ll1 * b1[2]) >> 8) + ((lr1 * b1[3]) >> 8);					*dst_p++ =						(((((r0[4] - r1[4]) * ratio) >> 8) + r1[4]) << dstfmt->Rshift) |						(((((g0[4] - g1[4]) * ratio) >> 8) + g1[4]) << dstfmt->Gshift) |						(((((b0[4] - b1[4]) * ratio) >> 8) + b1[4]) << dstfmt->Bshift) |						dstopaque;				}				else				{					*dst_p++ = 0;				}				sx0 += fsx0;				sx1 += fsx1;			}			sx0 = sx1 = 0;			sy0 += fsy0;			sy1 += fsy1;		}		SDL_UnlockSurface(mipmap);		SDL_UnlockSurface(dst);		SDL_UnlockSurface(src);	}	else	{		fprintf (stderr, "Tried to deal with unknown BPP: %d -> %d, mipmap %d\n",			src->format->BitsPerPixel, dst->format->BitsPerPixel, mipmap->format->BitsPerPixel);	}}