1 Star 0 Fork 3

jeremy/hi3516dv300-rtsp-h264

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
loadbmp.c 15.44 KB
一键复制 编辑 原始数据 按行查看 历史
rinetd 提交于 2019-12-03 18:18 . imx335
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "loadbmp.h"
OSD_COMP_INFO s_OSDCompInfo[OSD_COLOR_FMT_BUTT] = {{0, 4, 4, 4}, /*RGB444*/
{4, 4, 4, 4}, /*ARGB4444*/
{0, 5, 5, 5}, /*RGB555*/
{0, 5, 6, 5}, /*RGB565*/
{1, 5, 5, 5}, /*ARGB1555*/
{0, 0, 0, 0}, /*RESERVED*/
{0, 8, 8, 8}, /*RGB888*/
{8, 8, 8, 8} /*ARGB8888*/
};
HI_U16 OSD_MAKECOLOR_U16(HI_U8 r, HI_U8 g, HI_U8 b, OSD_COMP_INFO compinfo)
{
HI_U8 r1, g1, b1;
HI_U16 pixel = 0;
HI_U32 tmp = 15;
r1 = g1 = b1 = 0;
r1 = r >> (8 - compinfo.rlen);
g1 = g >> (8 - compinfo.glen);
b1 = b >> (8 - compinfo.blen);
while (compinfo.alen)
{
pixel |= (1 << tmp);
tmp --;
compinfo.alen--;
}
pixel |= (r1 | (g1 << compinfo.blen) | (b1 << (compinfo.blen + compinfo.glen)));
return pixel;
}
HI_S32 GetBmpInfo(const char* filename, OSD_BITMAPFILEHEADER* pBmpFileHeader
, OSD_BITMAPINFO* pBmpInfo)
{
FILE* pFile;
HI_U16 bfType;
if (NULL == filename)
{
printf("OSD_LoadBMP: filename=NULL\n");
return -1;
}
if ((pFile = fopen((char*)filename, "rb")) == NULL)
{
printf("Open file faild:%s!\n", filename);
return -1;
}
(void)fread(&bfType, 1, sizeof(bfType), pFile);
if (bfType != 0x4d42)
{
printf("not bitmap file\n");
fclose(pFile);
return -1;
}
(void)fread(pBmpFileHeader, 1, sizeof(OSD_BITMAPFILEHEADER), pFile);
(void)fread(pBmpInfo, 1, sizeof(OSD_BITMAPINFO), pFile);
fclose(pFile);
return 0;
}
int LoadBMP(const char* filename, OSD_LOGO_T* pVideoLogo)
{
FILE* pFile;
HI_U16 i, j;
HI_U32 w, h;
HI_U16 Bpp;
HI_U16 dstBpp;
OSD_BITMAPFILEHEADER bmpFileHeader;
OSD_BITMAPINFO bmpInfo;
HI_U8* pOrigBMPBuf;
HI_U8* pRGBBuf;
HI_U32 stride;
if (NULL == filename)
{
printf("OSD_LoadBMP: filename=NULL\n");
return -1;
}
if (GetBmpInfo(filename, &bmpFileHeader, &bmpInfo) < 0)
{
return -1;
}
Bpp = bmpInfo.bmiHeader.biBitCount / 8;
if (Bpp < 2)
{
/* only support 1555 8888 888 bitmap */
printf("bitmap format not supported!\n");
return -1;
}
if (bmpInfo.bmiHeader.biCompression != 0)
{
printf("not support compressed bitmap file!\n");
return -1;
}
if (bmpInfo.bmiHeader.biHeight < 0)
{
printf("bmpInfo.bmiHeader.biHeight < 0\n");
return -1;
}
if ( (pFile = fopen((char*)filename, "rb")) == NULL)
{
printf("Open file faild:%s!\n", filename);
return -1;
}
pVideoLogo->width = (HI_U16)bmpInfo.bmiHeader.biWidth;
pVideoLogo->height = (HI_U16)((bmpInfo.bmiHeader.biHeight > 0) ? bmpInfo.bmiHeader.biHeight : (-bmpInfo.bmiHeader.biHeight));
w = pVideoLogo->width;
h = pVideoLogo->height;
stride = w * Bpp;
#if 1
if (stride % 4)
{
stride = (stride & 0xfffc) + 4;
}
#endif
/* RGB8888 or RGB1555 */
pOrigBMPBuf = (HI_U8*)malloc(h * stride);
if (NULL == pOrigBMPBuf)
{
printf("not enough memory to malloc!\n");
fclose(pFile);
return -1;
}
pRGBBuf = pVideoLogo->pRGBBuffer;
fseek(pFile, bmpFileHeader.bfOffBits, 0);
if (fread(pOrigBMPBuf, 1, h * stride, pFile) != (h * stride) )
{
printf("fread error!line:%d\n", __LINE__);
perror("fread:");
}
if (Bpp > 2)
{
dstBpp = 4;
}
else
{
dstBpp = 2;
}
if (0 == pVideoLogo->stride)
{
pVideoLogo->stride = pVideoLogo->width * dstBpp;
}
for (i = 0; i < h; i++)
{
for (j = 0; j < w; j++)
{
memcpy(pRGBBuf + i * pVideoLogo->stride + j * dstBpp, pOrigBMPBuf + ((h - 1) - i)*stride + j * Bpp, Bpp);
if (dstBpp == 4)
{
//*(pRGBBuf + i*pVideoLogo->stride + j*dstbpp + 3) = random()&0xff; /*alpha*/
*(pRGBBuf + i * pVideoLogo->stride + j * dstBpp + 3) = 0x80; /*alpha*/
}
}
}
free(pOrigBMPBuf);
pOrigBMPBuf = NULL;
fclose(pFile);
return 0;
}
int LoadBMPEx(const char* filename, OSD_LOGO_T* pVideoLogo, OSD_COLOR_FMT_E enFmt)
{
FILE* pFile;
HI_U16 i, j;
HI_U32 w, h;
HI_U16 Bpp;
OSD_BITMAPFILEHEADER bmpFileHeader;
OSD_BITMAPINFO bmpInfo;
HI_U8* pOrigBMPBuf;
HI_U8* pRGBBuf;
HI_U32 stride;
HI_U8 r, g, b;
HI_U8* pStart;
HI_U16* pDst;
if (NULL == filename)
{
printf("OSD_LoadBMP: filename=NULL\n");
return -1;
}
if (GetBmpInfo(filename, &bmpFileHeader, &bmpInfo) < 0)
{
return -1;
}
Bpp = bmpInfo.bmiHeader.biBitCount / 8;
if (Bpp < 2)
{
/* only support 1555.8888 888 bitmap */
printf("bitmap format not supported!\n");
return -1;
}
if (bmpInfo.bmiHeader.biCompression != 0)
{
printf("not support compressed bitmap file!\n");
return -1;
}
if (bmpInfo.bmiHeader.biHeight < 0)
{
printf("bmpInfo.bmiHeader.biHeight < 0\n");
return -1;
}
if ( (pFile = fopen((char*)filename, "rb")) == NULL)
{
printf("Open file faild:%s!\n", filename);
return -1;
}
pVideoLogo->width = (HI_U16)bmpInfo.bmiHeader.biWidth;
pVideoLogo->height = (HI_U16)((bmpInfo.bmiHeader.biHeight > 0) ? bmpInfo.bmiHeader.biHeight : (-bmpInfo.bmiHeader.biHeight));
w = pVideoLogo->width;
h = pVideoLogo->height;
stride = w * Bpp;
#if 1
if (stride % 4)
{
stride = (stride & 0xfffc) + 4;
}
#endif
/* RGB8888 or RGB1555 */
pOrigBMPBuf = (HI_U8*)malloc(h * stride);
if (NULL == pOrigBMPBuf)
{
printf("not enough memory to malloc!\n");
fclose(pFile);
return -1;
}
pRGBBuf = pVideoLogo->pRGBBuffer;
fseek(pFile, bmpFileHeader.bfOffBits, 0);
if (fread(pOrigBMPBuf, 1, h * stride, pFile) != (h * stride) )
{
printf("fread (%d*%d)error!line:%d\n", h, stride, __LINE__);
perror("fread:");
}
if (enFmt >= OSD_COLOR_FMT_RGB888)
{
pVideoLogo->stride = pVideoLogo->width * 4;
}
else
{
pVideoLogo->stride = pVideoLogo->width * 2;
}
for (i = 0; i < h; i++)
{
for (j = 0; j < w; j++)
{
if (Bpp == 3)
{
switch (enFmt)
{
case OSD_COLOR_FMT_RGB444:
case OSD_COLOR_FMT_RGB555:
case OSD_COLOR_FMT_RGB565:
case OSD_COLOR_FMT_RGB1555:
case OSD_COLOR_FMT_RGB4444:
/* start color convert */
pStart = pOrigBMPBuf + ((h - 1) - i) * stride + j * Bpp;
pDst = (HI_U16*)(pRGBBuf + i * pVideoLogo->stride + j * 2);
r = *(pStart);
g = *(pStart + 1);
b = *(pStart + 2);
*pDst = OSD_MAKECOLOR_U16(r, g, b, s_OSDCompInfo[enFmt]);
break;
case OSD_COLOR_FMT_RGB888:
case OSD_COLOR_FMT_RGB8888:
memcpy(pRGBBuf + i * pVideoLogo->stride + j * 4, pOrigBMPBuf + ((h - 1) - i)*stride + j * Bpp, Bpp);
*(pRGBBuf + i * pVideoLogo->stride + j * 4 + 3) = 0xff; /*alpha*/
break;
default:
printf("file(%s), line(%d), no such format!\n", __FILE__, __LINE__);
break;
}
}
else if ((Bpp == 2) || (Bpp == 4)) /*..............*/
{
memcpy(pRGBBuf + i * pVideoLogo->stride + j * Bpp, pOrigBMPBuf + ((h - 1) - i)*stride + j * Bpp, Bpp);
}
}
}
free(pOrigBMPBuf);
pOrigBMPBuf = NULL;
fclose(pFile);
return 0;
}
int LoadBMPCanvas(const char* filename, OSD_LOGO_T* pVideoLogo, OSD_COLOR_FMT_E enFmt)
{
FILE* pFile;
HI_U16 i, j;
HI_U32 w, h;
HI_U16 Bpp;
OSD_BITMAPFILEHEADER bmpFileHeader;
OSD_BITMAPINFO bmpInfo;
HI_U8* pOrigBMPBuf;
HI_U8* pRGBBuf;
HI_U32 stride;
HI_U8 r, g, b;
HI_U8* pStart;
HI_U16* pDst;
if (NULL == filename)
{
printf("OSD_LoadBMP: filename=NULL\n");
return -1;
}
if (GetBmpInfo(filename, &bmpFileHeader, &bmpInfo) < 0)
{
return -1;
}
Bpp = bmpInfo.bmiHeader.biBitCount / 8;
if (Bpp < 2)
{
/* only support 1555.8888 888 bitmap */
printf("bitmap format not supported!\n");
return -1;
}
if (bmpInfo.bmiHeader.biCompression != 0)
{
printf("not support compressed bitmap file!\n");
return -1;
}
if (bmpInfo.bmiHeader.biHeight < 0)
{
printf("bmpInfo.bmiHeader.biHeight < 0\n");
return -1;
}
if ( (pFile = fopen((char*)filename, "rb")) == NULL)
{
printf("Open file faild:%s!\n", filename);
return -1;
}
w = (HI_U16)bmpInfo.bmiHeader.biWidth;
h = (HI_U16)((bmpInfo.bmiHeader.biHeight > 0) ? bmpInfo.bmiHeader.biHeight : (-bmpInfo.bmiHeader.biHeight));
stride = w * Bpp;
#if 1
if (stride % 4)
{
stride = (stride & 0xfffc) + 4;
}
#endif
/* RGB8888 or RGB1555 */
pOrigBMPBuf = (HI_U8*)malloc(h * stride);
if (NULL == pOrigBMPBuf)
{
printf("not enough memory to malloc!\n");
fclose(pFile);
return -1;
}
pRGBBuf = pVideoLogo->pRGBBuffer;
if (stride > pVideoLogo->stride)
{
printf("Bitmap's stride(%d) is bigger than canvas's stide(%d). Load bitmap error!\n", stride, pVideoLogo->stride);
free(pOrigBMPBuf);
fclose(pFile);
return -1;
}
if (h > pVideoLogo->height)
{
printf("Bitmap's height(%d) is bigger than canvas's height(%d). Load bitmap error!\n", h, pVideoLogo->height);
free(pOrigBMPBuf);
fclose(pFile);
return -1;
}
if (w > pVideoLogo->width)
{
printf("Bitmap's width(%d) is bigger than canvas's width(%d). Load bitmap error!\n", w, pVideoLogo->width);
free(pOrigBMPBuf);
fclose(pFile);
return -1;
}
fseek(pFile, bmpFileHeader.bfOffBits, 0);
if (fread(pOrigBMPBuf, 1, h * stride, pFile) != (h * stride) )
{
printf("fread (%d*%d)error!line:%d\n", h, stride, __LINE__);
perror("fread:");
}
for (i = 0; i < h; i++)
{
for (j = 0; j < w; j++)
{
if (Bpp == 3) /*.....*/
{
switch (enFmt)
{
case OSD_COLOR_FMT_RGB444:
case OSD_COLOR_FMT_RGB555:
case OSD_COLOR_FMT_RGB565:
case OSD_COLOR_FMT_RGB1555:
case OSD_COLOR_FMT_RGB4444:
/* start color convert */
pStart = pOrigBMPBuf + ((h - 1) - i) * stride + j * Bpp;
pDst = (HI_U16*)(pRGBBuf + i * pVideoLogo->stride + j * 2);
r = *(pStart);
g = *(pStart + 1);
b = *(pStart + 2);
//printf("Func: %s, line:%d, Bpp: %d, bmp stride: %d, Canvas stride: %d, h:%d, w:%d.\n",
// __FUNCTION__, __LINE__, Bpp, stride, pVideoLogo->stride, i, j);
*pDst = OSD_MAKECOLOR_U16(r, g, b, s_OSDCompInfo[enFmt]);
break;
case OSD_COLOR_FMT_RGB888:
case OSD_COLOR_FMT_RGB8888:
memcpy(pRGBBuf + i * pVideoLogo->stride + j * 4, pOrigBMPBuf + ((h - 1) - i)*stride + j * Bpp, Bpp);
*(pRGBBuf + i * pVideoLogo->stride + j * 4 + 3) = 0xff; /*alpha*/
break;
default:
printf("file(%s), line(%d), no such format!\n", __FILE__, __LINE__);
break;
}
}
else if ((Bpp == 2) || (Bpp == 4)) /*..............*/
{
memcpy(pRGBBuf + i * pVideoLogo->stride + j * Bpp, pOrigBMPBuf + ((h - 1) - i)*stride + j * Bpp, Bpp);
}
}
}
free(pOrigBMPBuf);
pOrigBMPBuf = NULL;
fclose(pFile);
return 0;
}
char* GetExtName(char* filename)
{
char* pret = NULL;
HI_U32 fnLen;
if (NULL == filename)
{
printf("filename can't be null!");
return NULL;
}
fnLen = strlen(filename);
while (fnLen)
{
pret = filename + fnLen;
if (*pret == '.')
{ return (pret + 1); }
fnLen--;
}
return pret;
}
int LoadImage(const char* filename, OSD_LOGO_T* pVideoLogo)
{
char* ext = GetExtName((char*)filename);
if(HI_NULL == ext)
{
printf("GetExtName error!\n");
return -1;
}
if (strcmp(ext, "bmp") == 0)
{
if (0 != LoadBMP(filename, pVideoLogo))
{
printf("OSD_LoadBMP error!\n");
return -1;
}
}
else
{
printf("not supported image file!\n");
return -1;
}
return 0;
}
int LoadImageEx(const char* filename, OSD_LOGO_T* pVideoLogo, OSD_COLOR_FMT_E enFmt)
{
char* ext = GetExtName((char*)filename);
if(HI_NULL == ext)
{
printf("LoadImageEx error!\n");
return -1;
}
if (strcmp(ext, "bmp") == 0)
{
if (0 != LoadBMPEx(filename, pVideoLogo, enFmt))
{
printf("OSD_LoadBMP error!\n");
return -1;
}
}
else
{
printf("not supported image file!\n");
return -1;
}
return 0;
}
int LoadCanvasEx(const char* filename, OSD_LOGO_T* pVideoLogo, OSD_COLOR_FMT_E enFmt)
{
char* ext = GetExtName((char*)filename);
if(HI_NULL == ext)
{
printf("LoadCanvasEx error!\n");
return -1;
}
if (strcmp(ext, "bmp") == 0)
{
if (0 != LoadBMPCanvas(filename, pVideoLogo, enFmt))
{
printf("OSD_LoadBMP error!\n");
return -1;
}
}
else
{
printf("not supported image file!\n");
return -1;
}
return 0;
}
HI_S32 LoadBitMap2Surface(const HI_CHAR* pszFileName, const OSD_SURFACE_S* pstSurface, HI_U8* pu8Virt)
{
OSD_LOGO_T stLogo;
stLogo.stride = pstSurface->u16Stride;
stLogo.pRGBBuffer = pu8Virt;
return LoadImage(pszFileName, &stLogo);
}
HI_S32 CreateSurfaceByBitMap(const HI_CHAR* pszFileName, OSD_SURFACE_S* pstSurface, HI_U8* pu8Virt)
{
OSD_LOGO_T stLogo;
stLogo.pRGBBuffer = pu8Virt;
if (LoadImageEx(pszFileName, &stLogo, pstSurface->enColorFmt) < 0)
{
printf("load bmp error!\n");
return -1;
}
pstSurface->u16Height = stLogo.height;
pstSurface->u16Width = stLogo.width;
pstSurface->u16Stride = stLogo.stride;
return 0;
}
HI_S32 CreateSurfaceByCanvas(const HI_CHAR* pszFileName, OSD_SURFACE_S* pstSurface, HI_U8* pu8Virt, HI_U32 u32Width, HI_U32 u32Height, HI_U32 u32Stride)
{
OSD_LOGO_T stLogo;
stLogo.pRGBBuffer = pu8Virt;
stLogo.width = u32Width;
stLogo.height = u32Height;
stLogo.stride = u32Stride;
if (LoadCanvasEx(pszFileName, &stLogo, pstSurface->enColorFmt) < 0)
{
printf("load bmp error!\n");
return -1;
}
pstSurface->u16Height = u32Height;
pstSurface->u16Width = u32Width;
pstSurface->u16Stride = u32Stride;
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jeremy111hz/hi3516dv300-rtsp-h264.git
[email protected]:jeremy111hz/hi3516dv300-rtsp-h264.git
jeremy111hz
hi3516dv300-rtsp-h264
hi3516dv300-rtsp-h264
master

搜索帮助