HiSilicon OSD anti-aliasing, background transparency and superimposed watermark

Preface

The use of HiSilicon chips to superimpose OSD has been introduced in the previous article. The basic usage method will not be introduced again. This article introduces the usage of OSD anti-aliasing and background transparency. You need to adapt it according to your own chip model. I hope it will be helpful to everyone. If you have any questions, you can leave a message below to discuss.

1. The background is transparent

(1) When creating RGN, you can set the background color and transparency. Check whether the SDK model supports setting the background color. This does not seem to work when used in conjunction with the SDL TTF library. Students who use dot matrix fonts can try it. I don't know if it will have any effect.

u32BgColor parameter setting, the picture below is an example implemented in the original "HiSilicon 3516a implements OSD superimposed watermark" blog. According to the description in the HiSilicon SDK, just set the background transparency and background color.
Insert image description here

(2) When using the open source libraries SDL and TTF_SDL, set the watermark color interface to avoid using the TTF_RenderUTF8_Shaded interface. This interface will cause a shadow behind the font. Unless this shadow effect is needed, the TTF_RenderUTF8_Solid interface can generate the effect you want. This article 2. The version of the SDL library used in this article is newer than the original one, and the interface has changed. Finally, a link to the SDL library I used will be attached. The background of the generated image stream is transparent and can be saved as a bmp format image to see the effect.

//保存为bmp格式图片
SDL_SaveBMP(temp, "save.bmp");
//设置字体颜色
SDL_Color forecol = {
    
     0x00, 0xff, 0xff, 0xff };
text = TTF_RenderUTF8_Solid(font, pstr, forecol);

2. Use blending modes to remove aliasing


The method of removing aliasing mainly uses the method TTF_RenderUTF8_Blended in the open source library.
This interface uses blending mode to create a 32-bit ARGB surface and render the given text with high quality, using alpha blending to dither the font with a given color.


int main(int argc, const char *argv[])
{
    
    
    char pstr[128] = {
    
    0};
    snprintf(pstr, 128, "%s","我爱中国(wo ai zhong guo)");.

    SDL_PixelFormat *fmt;
    TTF_Font *font;
    SDL_Surface *text, *temp;
    if (TTF_Init() < 0 )
    {
    
    
        fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError());
        SDL_Quit();
    }
    font = TTF_OpenFont(FONT_TTF_PATH, 48);
    if ( font == NULL )
    {
    
    
        fprintf(stderr, "Couldn't load %d pt font from %s: %s\n",18,"ptsize", SDL_GetError());
    }
    SDL_Color forecol = {
    
     0x00, 0xff, 0xff, 0xff };

    text = TTF_RenderUTF8_Blended(font, pstr, forecol);
    printf("h:%d, w:%d, text len:%d\n", text->h, text->w, strlen((char*)text->pixels));
    
    fmt = (SDL_PixelFormat*)malloc(sizeof(SDL_PixelFormat));
    memset(fmt,0,sizeof(SDL_PixelFormat));
    fmt->BitsPerPixel = 32; //每像素位数
    fmt->BytesPerPixel = 4; //每像素字节数
    fmt->format = SDL_PIXELFORMAT_ARGB8888;
    fmt->Amask = 0xFF000000;    //透明度分量
    fmt->Rmask = 0x00FF0000;    //红色分量
    fmt->Gmask = 0x0000FF00;    //绿色分量
    fmt->Bmask = 0x000000FF;    //蓝色分量

    temp = SDL_ConvertSurface(text,fmt,0);
   
    SDL_SaveBMP(temp, "argb8888.bmp");
    free(fmt);
    fmt = NULL;
    SDL_FreeSurface(text);
    SDL_FreeSurface(temp);
    TTF_CloseFont(font);
    TTF_Quit();
    return 0;
}

When using blend mode, you need to set some parameters, the most important thing! Otherwise, a rectangular solid color bar will be generated, and the font will not be visible.
(1) fmt->BitsPerPixel: This parameter is the number of bits per pixel, and ARGB8888 has 32 bits per pixel.
(2) fmt->BytesPerPixel: This parameter is the number of bytes per pixel, set to 4.

Insert image description here
The picture above shows the elements in the SDL_PixelFormatEnum enumeration. The yellow ones are the configuration of the two parameters BitsPerPixel and BytesPerPixel. To find the corresponding parameters according to the format used, you can refer to this place.
(3) fmt->format: The format is SDL_PIXELFORMAT_ARGB8888.
(4) ARGB component settings can be set according to the above code.

The generated pictures are as follows: the above one is generated using the blend mode, and the following one is generated using the solid mode. It can be seen that the surface of the font generated using the blend mode is smooth and full, and the one generated using the solid mode is jagged.
Insert image description here

Using blend mode, you can see that it is a 32-bit ARGB surface in the format of ARGB8888. So if you use a HISI chip, you need to check whether the chip supports this format:
[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-6X00Qox7-1663235448305)(en-resource://database/520:0)]

Otherwise, an error will be reported when creating RGN:
[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-yhNUmzBj-1663235448306)(en-resource://database/522:0)]
If it is not supported, still use soil mode and use SDL_PIXELFORMAT_ARGB1555 format. Basically every chip supports this format, and no better method has been found yet.
Friends who support the ARGB8888 format can try it, thank you.

Data link

Open source packages (freetype, SDL, SDL_TTF) used in the new version of OSD watermark.
Previous article "HiSilicon 3516a implements OSD superimposed watermark"

Guess you like

Origin blog.csdn.net/weixin_37926485/article/details/126876468