Tip: ARGB alpha channel A + original YUV surface y0 + to write in Y1 = calculate a new y2.
The calculation formula is
(y1 * A + y0 * (255-a))/255
void Rgb2yuv (int r, int g, int b, int *y, int *u, int *v) {int y0, u0, v0;y0 = 66*r + 129*g + 25*b;u0 = -38*r + -74*g + 112*b;v0 = 112*r + -94*g + -18*b;y0 = (y0+128) >>8;u0 = (u0+128) >>8;v0 = (v0+128) >>8;*y = y0 + 16;*u = u0 + 128;*v = v0 + 128;} void Yuv2rgb (int y, int u, int v, int *r, int *g, int *b) {int r0,g0,b0;v = V-128;u = U-128;r0 = y + V + (V>>2) + (v>>3) + (v>>5), G0 = y-((u>>2) + (u>>4) + (u>>5))-((v>>1) + (v>>3) + (V> ; >4) + (v>>5)) B0 = y + U + (u>>1) + (U>>2) + (u>>6); *r = r0 > 255? 255:r0;*g = g0 > 255? 255:g0;*b = b0 > 255? 255:b0;} int Blitsurface2yuv (Sdl_surface *src, Sdl_overlay *dst, sdl_rect *dstrect, int isblended) {Uint8 R, G, B,a;int Y0,u0,v0;int Y1,u1,v1;int y2,u2,v2;int Y,x;int height = src->h < dstrect->h? Src->h:dstrect->h;int width = src->w < dstrect->w? Src->w:dstrect->w;int Uv_off = 0; Uint32 pixel;printf ("Src->format->bitsperpixel%d Src->format%08x\r\n ", Src->format->bitsperpixel,src->format); Dst->format! = Sdl_yv12_overlay) return 1;for (y = 0; y < height; ++y) {for (x = 0; x < width; ++x) {switch (SRC->FO Rmat->bitsperpixel) {Case 8:pixel = * ((uint8*) Src->pixels + y*src->pitch + x); Break;case 16:pixel = * ((Uint16*) Src->pixels + Y*SRC->PITCH/2 + x) break;case 32:pixel = * ((uint32*) Src->pixels + Y*SRC->PITCH/4 + x); break;de fault:return-1;} Sdl_getrgb (Pixel, Src->format, &r, &g, &b); Sdl_getrgba (Pixel, Src->format, &r, &g, &b,&a); Rgb2yuv (R, G, B, &y1, &U1, &V1); if (isblended) {y0 = (dst->pixels[0][(dstrect->y + y) * Dst->pitches[0] + (dstrect->x + x)]); V0 = (Dst->pixe ls[1][(Uv_off + DSTRECT->Y/2) * Dst->pitches[1] + (DSTRECT->X/2 + X/2)]); u0 = (dst->pixels[2][(Uv_off + DST RECT->Y/2) * Dst->pitches[2] + (DSTRECT->X/2 + X/2)]); y2 = (Uint8) ((y1 * A + y0 * ( 255-A)/255) U2 = (Uint8) ((U1 * A + u0 * (255-a))/255); v2 = (Uint8) ((v1 * A + v0 * (255-a))/255); Y1 =y2;u1=u2;v1=v2;} memset (Dst->pixels[0] + (dstrect->y + y) * Dst->pitches[0] + (dstrect->x + x), (Uint8) y1, 1); if ((x%2 = = 0) && (y%2 = = 0)) {memset (dst->pixels[1] + (Uv_off + dstrect->y/2) * Dst->pitches[1] + (DSTRECT->X/2 + x /2), (Uint8) v1, 1); memset (dst->pixels[2] + (Uv_off + dstrect->y/2) * Dst->pitches[2] + (DSTRECT->X/2 + X/2), (Uint8) U1, 1);}} if (y%2 = = 0) ++uv_off;} return 0;}
ARGB32 to YUV12 using SDL1.2 Sdl_ttf to output text on the video surface