读取字库文件显示OSD(HI3536平台VPSS)

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "sample_comm.h"

int utf8togb2312(char *utf8str, int utf8len, char *gb2312str, int gb2312len);
static int zk_init_flag = 0;

struct ZIKU
{
    int no;//序号
    int horc;//2=hz,1=ascii
    char name[256];//字库名称
    int h;//汉字高bit
    int w;//汉字宽bit
    int len;//字库长度
    char *addr; //汉字内容
};

struct ZIKU ziku[2]=
{
    //{0, 1,"/app/songtiascii_24x16.bin",24,16,0,NULL},//宋体12
    //{1, 2,"/app/songti_24x24.bin",24,24,0,NULL},//宋体24
    {0, 1,"/app/songtiascii_48x24.bin",48,24,0,NULL},//宋体12
    {1, 2,"/app/songti_48x48.bin",48,48,0,NULL},//宋体24
};


int osd_init_zk()
{
    int fd = -1;
    struct stat statbuf;
    int i=0;
    int readlen = 0;

    for(i=0; i<2; i++)
    {
        memset(&statbuf, 0, sizeof(statbuf));
        stat(ziku[i].name, &statbuf);
        ziku[i].len = statbuf.st_size;
        if(ziku[i].len <= 0)
        {
            printf("no find file %s\n", ziku[i].name);
            return -1;
        }

        ziku[i].addr = malloc(ziku[i].len);

        if ((fd = open (ziku[i].name, O_RDONLY, 0)) == -1)
        {
            printf ("Error: can't open file %s\n", ziku[i].name);
            return -2;
        }
        readlen = read(fd, ziku[i].addr, ziku[i].len);
        close (fd);

        if(readlen != ziku[i].len)
        {
            printf("read file %s len error\n", ziku[i].name);
        }
    }

    zk_init_flag = 1;

    return 0;
}

int get_zk_from_file(int zk_no, char *hz, char* buf)
{
    int size = 0;
    int temp = 0;
    if(1 == ziku[zk_no].horc)
    {
        size = ziku[zk_no].w*ziku[zk_no].h/8;
        temp = ((unsigned char)hz[0]-0x20) * size;
        memcpy(buf, &ziku[zk_no].addr[temp], size);
    }else if(2 == ziku[zk_no].horc)
    {
        size = ziku[zk_no].w*ziku[zk_no].h/8;
        temp = (((unsigned char)hz[0]-1-0xa0)*94 + ((unsigned char)hz[1]-1-0xa0)) * size ;
        memcpy(buf, &ziku[zk_no].addr[temp], size);
    }else
    {
        return -1;
    }

    return 0;
}

int put2lcd(char *data, int width, int x0, int y0, int zk_no, int font_color, char *buf)
{
    int j,k,x,y,xx;

    for(y = 0; y < ziku[zk_no].h; y++)
    {
        for (j=0; j<ziku[zk_no].w/8; j++)
        {
            for(x = 0; x < 8; x++)
            {
                k = x % 8;
                xx = x0 + x+8*j;
                if (buf[ziku[zk_no].w/8*y + j]  & (0x80 >> k))
                {
                    memcpy(data + (xx + (y+y0)*width)*4, &font_color, 4);
                }
                else
                {
                    int color = 0x30000000;
                    memcpy(data + (xx + (y+y0)*width)*4, &color, 4);
                }
            }
        }
    }

    return 0;
}

int print_hz_char(char *data, int width, int x, int y, int zk_no, int font_color, char *hz)
{
    char buf[1024];
    int rd;

    rd = get_zk_from_file(zk_no, hz, buf);
        if(rd < 0)
        {
        printf("no find :%s\r\n", hz);
                return -1;
        }

    //printf("get_zk_from_file w:%d, x:%d, y:%d, no:%d, hz:0x%02x\n", width, x, y, zk_no, hz[0]);

#if 0
    int i=0, j=0, k=0, kk=0;
    for(i=0; i<ziku[zk_no].h;i++)
    {
        for(j=0; j<ziku[zk_no].w/8;j++)
        {
            for(k=0; k<8;k++)
            {
                if(buf[kk]>>(8-k) & 0x1)
                {
                    printf("*");
                }else
                {
                    printf(" ");
                }
            }
            kk++;
        }
        printf("\n");
    }
#endif

    put2lcd(data, width, x, y, zk_no, font_color, buf);

    return 0;
}

//width:叠加OSD区域宽度
int fill_osd_str(char *data, int width, int font_type, int font_color, char *utfhz)
{
    int i;
    int len;
    int n = 2;
    int offset = 0;
    int zk_no = 0;
    int x=0, y=0;
    char gbhz[64]= "";

    if(!zk_init_flag)
    {
        printf("zk init fail\n");
        return 0;
    }

    len = strlen(utfhz);
    if(len > 64)
    {
        printf("osd len too len, %d\n",  len);
        return 0;
    }

    //printf("print_hz_str : <%s>\r\n", utfhz);

    utf8togb2312(utfhz, len, gbhz, 0);

    for(i=0; i<len; i+=n)
    {
        if(gbhz[i] > 127)
        {
            zk_no = 1;
            n = ziku[zk_no].horc;
        }
        else if (gbhz[i]>=32)
        {
            zk_no = 0;
            n = ziku[zk_no].horc;
        }
        else if(gbhz[i] ==13)
        {
            //y = y + ziku_on.h;
            //offset =0;
            //printf("ziku_on.h = %d\r\n",ziku_on.h);
            continue;
        }
        else
        {
            //printf("gbhz = %d\r\n",gbhz[i]);
            continue;
        }

        print_hz_char(data, width, x+offset, y, zk_no, font_color, &gbhz[i]);

        offset += ziku[zk_no].w;
    }

    return 0;
}


int yh_dec_init_osd()
{
    HI_S32 ch=0;
    HI_S32 s32Ret;
    MPP_CHN_S stChn;
    RGN_ATTR_S stRgnAttrSet;
    RGN_CHN_ATTR_S stChnAttr;

    osd_init_zk();

    for(ch=0; ch<6; ch++)
    {
        /*attach the OSD to the vpss*/
        stChn.enModId  = HI_ID_VPSS;
        stChn.s32DevId = ch;
        stChn.s32ChnId = 0;

        stRgnAttrSet.enType = OVERLAY_RGN;
        stRgnAttrSet.unAttr.stOverlay.enPixelFmt       = PIXEL_FORMAT_RGB_8888;
        stRgnAttrSet.unAttr.stOverlay.stSize.u32Width  = 48*8;
        stRgnAttrSet.unAttr.stOverlay.stSize.u32Height = 48;
        stRgnAttrSet.unAttr.stOverlay.u32BgColor       = 0x00;

        s32Ret = HI_MPI_RGN_Create(ch, &stRgnAttrSet);
        if(s32Ret != HI_SUCCESS)
        {
            printf("HI_MPI_RGN_Create failed! s32Ret: 0x%x.\n", s32Ret);
            return s32Ret;
        }

        stChnAttr.bShow  = HI_TRUE;
        stChnAttr.enType = OVERLAY_RGN;
        stChnAttr.unChnAttr.stOverlayChn.stPoint.s32X = 0;
        stChnAttr.unChnAttr.stOverlayChn.stPoint.s32Y = 0;
        stChnAttr.unChnAttr.stOverlayChn.u32BgAlpha   = 128;
        stChnAttr.unChnAttr.stOverlayChn.u32FgAlpha   = 128;
        stChnAttr.unChnAttr.stOverlayChn.u32Layer     = 0;

        s32Ret = HI_MPI_RGN_AttachToChn(ch, &stChn, &stChnAttr);
        if(s32Ret != HI_SUCCESS)
        {
            printf("HI_MPI_RGN_AttachToChn failed! s32Ret: 0x%x.\n", s32Ret);
            return s32Ret;
        }

    }

    return s32Ret;
}


int yh_dec_osd_set_attr(int ch, int enable, int startx, int starty, char *str, int len, int color)
{
    HI_S32 s32Ret;
    RGN_CHN_ATTR_S stChnAttr;
    RGN_ATTR_S stRgnAttrSet;
    RGN_CANVAS_INFO_S stCanvasInfo;
    SIZE_S stSize;
    BITMAP_S stBitmap;
    MPP_CHN_S stChn;

    //保证起始坐标点为偶数
    if(startx%2) startx -= 1;
    if(starty%2) starty -= 1;

    s32Ret = HI_MPI_RGN_GetAttr(ch, &stRgnAttrSet);
    if(HI_SUCCESS != s32Ret)
    {
        printf("HI_MPI_RGN_GetAttr failed! s32Ret: 0x%x.\n", s32Ret);
        return s32Ret;
    }

    s32Ret = HI_MPI_RGN_GetCanvasInfo(ch, &stCanvasInfo);
    if(HI_SUCCESS != s32Ret)
    {
        printf("HI_MPI_RGN_GetCanvasInfo failed! s32Ret: 0x%x.\n", s32Ret);
        return s32Ret;
    }

    //printf("osd width:%d, height:%d\n", stCanvasInfo.stSize.u32Width, stCanvasInfo.stSize.u32Height);
    stBitmap.pData   = (HI_VOID *)stCanvasInfo.u32VirtAddr;
    stSize.u32Width  = stCanvasInfo.stSize.u32Width;
    stSize.u32Height = stCanvasInfo.stSize.u32Height;
    memset(stBitmap.pData, 0, stSize.u32Width*stSize.u32Height*4);
    //s32Ret = SAMPLE_RGN_UpdateCanvas("mm2.bmp", &stBitmap, HI_FALSE, 0, &stSize, stCanvasInfo.u32Stride,stRgnAttrSet.unAttr.stOverlay.enPixelFmt);
    s32Ret = fill_osd_str(stBitmap.pData, stSize.u32Width, 0, color, str);
    if(HI_SUCCESS != s32Ret)
    {
        printf("SAMPLE_RGN_UpdateCanvas failed! s32Ret: 0x%x.\n", s32Ret);
        return s32Ret;
    }

    s32Ret = HI_MPI_RGN_UpdateCanvas(ch);
    if(HI_SUCCESS != s32Ret)
    {
        printf("HI_MPI_RGN_UpdateCanvas failed! s32Ret: 0x%x.\n", s32Ret);
        return s32Ret;
    }

    stChnAttr.bShow  = enable;//HI_TRUE;
    stChnAttr.enType = OVERLAY_RGN;
    stChnAttr.unChnAttr.stOverlayChn.stPoint.s32X = startx;
    stChnAttr.unChnAttr.stOverlayChn.stPoint.s32Y = starty;
    stChnAttr.unChnAttr.stOverlayChn.u32BgAlpha   = 128;
    stChnAttr.unChnAttr.stOverlayChn.u32FgAlpha   = 128;
    stChnAttr.unChnAttr.stOverlayChn.u32Layer     = 0;

    stChn.enModId  = HI_ID_VPSS;
    stChn.s32DevId = ch;
    stChn.s32ChnId = 0;

    s32Ret = HI_MPI_RGN_SetDisplayAttr(ch, &stChn, &stChnAttr);
    if (HI_SUCCESS != s32Ret)
    {
        printf("HI_MPI_RGN_SetDisplayAttr failed! s32Ret: 0x%x.\n", s32Ret);
    }

    return s32Ret;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悠哉无忧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值