打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
一组rgb转换函数,支持rgb565/rgb888/xrgb8888之间的数据转换
一组rgb转换函数,支持rgb565/rgb888/xrgb8888之间的数据转换
mybmp.h
Cpp代码
/********************************************************************
created:    2012/04/07
filename:   mybmp.h
author:
purpose:
*********************************************************************/
#ifndef _mybmp_h__
#define _mybmp_h__
//-------------------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif
#define UpAlign4(n) (((n) + 3) & ~3)
#define UpAlign8(n) (((n) + 7) & ~7)
//拷贝数据
void rgb_copy(const void * psrc, void * pdst, int sw, int sh, int dw, int dh, int bpp);
void rbg565_copy(const void * psrc, void * pdst, int sw, int sh, int dw, int dh);
void rbg888_copy(const void * psrc, void * pdst, int sw, int sh, int dw, int dh);
void rbgx8888_copy(const void * psrc, void * pdst, int sw, int sh, int dw, int dh);
//行数据翻转
void line_reversal(void * pdata, int w, int h, int bpp);
void rgb565_line_reversal(void * p565, int w, int h);
void rgb888_line_reversal(void * p888, int w, int h);
void rgbx8888_line_reversal(void * p8888, int w, int h);
//转换
typedef void * (* RGB_CONVERT_FUN)(const void * psrc, int w, int h);
void * rgb565_to_rgb888_buffer(const void * psrc, int w, int h);
void * rgb888_to_rgb565_buffer(const void * psrc, int w, int h);
void * rgb565_to_rgbx8888_buffer(const void * psrc, int w, int h);
void * rgbx8888_to_rgb565_buffer(const void * psrc, int w, int h);
void * rgb888_to_rgbx8888_buffer(const void * psrc, int w, int h);
void * rgbx8888_to_rgb888_buffer(const void * psrc, int w, int h);
RGB_CONVERT_FUN get_convert_func(int frombpp, int tobpp);
#ifdef __cplusplus
};
#endif
//-------------------------------------------------------------------
#endif // #ifndef _mybmp_h__
rgb_convert.c
Cpp代码
/********************************************************************
created:    2012/05/19
filename:   rgb_convert.c
author:
purpose:
*********************************************************************/
//-------------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include "mybmp.h"
//-------------------------------------------------------------------
//拷贝
void rgb_copy(const void * psrc, void * pdst, int sw, int sh, int dw, int dh, int bpp)
{
int bytes = UpAlign8(bpp) >> 3; // bpp / 8
int srclinesize = UpAlign4(sw * bytes);
int dstlinesize = UpAlign4(dw * bytes);
int copylinesize = srclinesize < dstlinesize ? srclinesize : dstlinesize;
int copylines = sh < dh ? sh : dh;
const unsigned char * psrcline = (const unsigned char *)psrc;
const unsigned char * pend = psrcline + copylines * srclinesize;
unsigned char * pdstline = (unsigned char *)pdst;
while (psrcline < pend) {
memcpy(pdstline, psrcline, copylinesize);
psrcline += srclinesize;
pdstline += dstlinesize;
}
}
void rbg565_copy(const void * psrc, void * pdst, int sw, int sh, int dw, int dh)
{
rgb_copy(psrc, pdst, sw, sh, dw, dh, 16);
}
void rbg888_copy(const void * psrc, void * pdst, int sw, int sh, int dw, int dh)
{
rgb_copy(psrc, pdst, sw, sh, dw, dh, 24);
}
void rbgx8888_copy(const void * psrc, void * pdst, int sw, int sh, int dw, int dh)
{
rgb_copy(psrc, pdst, sw, sh, dw, dh, 32);
}
//-------------------------------------------------------------------
//行数据翻转
void line_reversal(void * pdata, int w, int h, int bpp)
{
int bytes     = UpAlign8(bpp) >> 3; // bpp / 8
int linesize  = UpAlign4(w * bytes);//4的整数倍
int copylines = h >> 1;
int i;
unsigned char * pline = NULL;
unsigned char * pline1 = NULL;
unsigned char * linebuffer = NULL;
if (pdata && w > 0 && h > 1) {//至少两行才需要翻转
linebuffer = (unsigned char *)malloc(linesize);
if (linebuffer) {
pline  = (unsigned char *)pdata;
pline1 = (unsigned char *)pdata + linesize * (h - 1);
for (i = 0; i < copylines; i++) {
memcpy(linebuffer, pline, linesize);
memcpy(pline, pline1, linesize);
memcpy(pline1, linebuffer, linesize);
pline  += linesize;
pline1 -= linesize;
}
free(linebuffer);
}
}
}
void rgb565_line_reversal(void * p565, int w, int h)
{
line_reversal(p565, w, h, 16);
}
void rgb888_line_reversal(void * p888, int w, int h)
{
line_reversal(p888, w, h, 24);
}
void rgbx8888_line_reversal(void * p8888, int w, int h)
{
line_reversal(p8888, w, h, 32);
}
//-------------------------------------------------------------------
//转换
static int rgb565_to_rgb888(const void * psrc, int w, int h, void * pdst)
{
int srclinesize = UpAlign4(w * 2);
int dstlinesize = UpAlign4(w * 3);
const unsigned char  * psrcline;
const unsigned short * psrcdot;
unsigned char  * pdstline;
unsigned char  * pdstdot;
int i,j;
if (!psrc || !pdst || w <= 0 || h <= 0) {
printf("rgb565_to_rgb888 : parameter error\n");
return -1;
}
psrcline = (const unsigned char *)psrc;
pdstline = (unsigned char *)pdst;
for (i=0; i<h; i++) {
psrcdot = (const unsigned short *)psrcline;
pdstdot = pdstline;
for (j=0; j<w; j++) {
//565 b|g|r -> 888 r|g|b
*pdstdot++ = (unsigned char)(((*psrcdot) >> 0 ) << 3);
*pdstdot++ = (unsigned char)(((*psrcdot) >> 5 ) << 2);
*pdstdot++ = (unsigned char)(((*psrcdot) >> 11) << 3);
psrcdot++;
}
psrcline += srclinesize;
pdstline += dstlinesize;
}
return 0;
}
static int rgb888_to_rgb565(const void * psrc, int w, int h, void * pdst)
{
int srclinesize = UpAlign4(w * 3);
int dstlinesize = UpAlign4(w * 2);
const unsigned char * psrcline;
const unsigned char * psrcdot;
unsigned char  * pdstline;
unsigned short * pdstdot;
int i,j;
if (!psrc || !pdst || w <= 0 || h <= 0) {
printf("rgb888_to_rgb565 : parameter error\n");
return -1;
}
psrcline = (const unsigned char *)psrc;
pdstline = (unsigned char *)pdst;
for (i=0; i<h; i++) {
psrcdot = psrcline;
pdstdot = (unsigned short *)pdstline;
for (j=0; j<w; j++) {
//888 r|g|b -> 565 b|g|r
*pdstdot =  (((psrcdot[0] >> 3) & 0x1F) << 0)//r
|(((psrcdot[1] >> 2) & 0x3F) << 5)//g
|(((psrcdot[2] >> 3) & 0x1F) << 11);//b
psrcdot += 3;
pdstdot++;
}
psrcline += srclinesize;
pdstline += dstlinesize;
}
return 0;
}
static int rgb565_to_rgbx8888(const void * psrc, int w, int h, void * pdst)
{
int srclinesize = UpAlign4(w * 2);
int dstlinesize = UpAlign4(w * 4);
const unsigned char  * psrcline;
const unsigned short * psrcdot;
unsigned char  * pdstline;
unsigned char  * pdstdot;
int i,j;
if (!psrc || !pdst || w <= 0 || h <= 0) {
printf("rgb565_to_rgbx8888 : parameter error\n");
return -1;
}
psrcline = (const unsigned char *)psrc;
pdstline = (unsigned char *)pdst;
for (i=0; i<h; i++) {
psrcdot = (const unsigned short *)psrcline;
pdstdot = pdstline;
for (j=0; j<w; j++) {
pdstdot++;
*pdstdot++ = (unsigned char)(((*psrcdot) >> 0 ) << 3);
*pdstdot++ = (unsigned char)(((*psrcdot) >> 5 ) << 2);
*pdstdot++ = (unsigned char)(((*psrcdot) >> 11) << 3);
psrcdot++;
}
psrcline += srclinesize;
pdstline += dstlinesize;
}
return 0;
}
static int rgbx8888_to_rgb565(const void * psrc, int w, int h, void * pdst)
{
int srclinesize = UpAlign4(w * 4);
int dstlinesize = UpAlign4(w * 2);
const unsigned char * psrcline;
const unsigned char * psrcdot;
unsigned char  * pdstline;
unsigned short * pdstdot;
int i,j;
if (!psrc || !pdst || w <= 0 || h <= 0) {
printf("rgbx8888_to_rgb565 : parameter error\n");
return -1;
}
psrcline = (const unsigned char *)psrc;
pdstline = (unsigned char *)pdst;
for (i=0; i<h; i++) {
psrcdot = psrcline;
pdstdot = (unsigned short *)pdstline;
for (j=0; j<w; j++) {
//888 r|g|b -> 565 b|g|r
*pdstdot =  (((psrcdot[1] >> 3) & 0x1F) << 0)//r
|(((psrcdot[2] >> 2) & 0x3F) << 5)//g
|(((psrcdot[3] >> 3) & 0x1F) << 11);//b
psrcdot += 4;
pdstdot++;
}
psrcline += srclinesize;
pdstline += dstlinesize;
}
return 0;
}
static int rgb888_to_rgbx8888(const void * psrc, int w, int h, void * pdst)
{
int srclinesize = UpAlign4(w * 3);
int dstlinesize = UpAlign4(w * 4);
const unsigned char * psrcline;
const unsigned char * psrcdot;
unsigned char  * pdstline;
unsigned char  * pdstdot;
int i,j;
if (!psrc || !pdst || w <= 0 || h <= 0) {
printf("rgb888_to_rgbx8888 : parameter error\n");
return -1;
}
psrcline = (const unsigned char *)psrc;
pdstline = (unsigned char *)pdst;
for (i=0; i<h; i++) {
psrcdot = psrcline;
pdstdot = pdstline;
for (j=0; j<w; j++) {
*pdstdot++ = 0;
*pdstdot++ = *psrcdot++;
*pdstdot++ = *psrcdot++;
*pdstdot++ = *psrcdot++;
}
psrcline += srclinesize;
pdstline += dstlinesize;
}
return 0;
}
static int rgbx8888_to_rgb888(const void * psrc, int w, int h, void * pdst)
{
int srclinesize = UpAlign4(w * 4);
int dstlinesize = UpAlign4(w * 3);
const unsigned char * psrcline;
const unsigned char * psrcdot;
unsigned char  * pdstline;
unsigned char  * pdstdot;
int i,j;
if (!psrc || !pdst || w <= 0 || h <= 0) {
printf("rgbx8888_to_rgb888 : parameter error\n");
return -1;
}
psrcline = (const unsigned char *)psrc;
pdstline = (unsigned char *)pdst;
for (i=0; i<h; i++) {
psrcdot = psrcline;
pdstdot = pdstline;
for (j=0; j<w; j++) {
psrcdot++;
*pdstdot++ = *psrcdot++;
*pdstdot++ = *psrcdot++;
*pdstdot++ = *psrcdot++;
}
psrcline += srclinesize;
pdstline += dstlinesize;
}
return 0;
}
void * rgb565_to_rgb888_buffer(const void * psrc, int w, int h)
{
int size = h * UpAlign4(w * 3);
void * pdst = NULL;
if (psrc && w > 0 && h > 0) {
pdst = malloc(size);
if (pdst) {
if (rgb565_to_rgb888(psrc, w, h, pdst)) {
free(pdst);
pdst = NULL;
}
}
}
return pdst;
}
void * rgb888_to_rgb565_buffer(const void * psrc, int w, int h)
{
int size = h * UpAlign4(w * 2);
void * pdst = NULL;
if (psrc && w > 0 && h > 0) {
pdst = malloc(size);
if (pdst) {
if (rgb888_to_rgb565(psrc, w, h, pdst)) {
free(pdst);
pdst = NULL;
}
}
}
return pdst;
}
void * rgb565_to_rgbx8888_buffer(const void * psrc, int w, int h)
{
int size = h * UpAlign4(w * 4);
void * pdst = NULL;
if (psrc && w > 0 && h > 0) {
pdst = malloc(size);
if (pdst) {
if (rgb565_to_rgbx8888(psrc, w, h, pdst)) {
free(pdst);
pdst = NULL;
}
}
}
return pdst;
}
void * rgbx8888_to_rgb565_buffer(const void * psrc, int w, int h)
{
int size = h * UpAlign4(w * 2);
void * pdst = NULL;
if (psrc && w > 0 && h > 0) {
pdst = malloc(size);
if (pdst) {
if (rgbx8888_to_rgb565(psrc, w, h, pdst)) {
free(pdst);
pdst = NULL;
}
}
}
return pdst;
}
void * rgb888_to_rgbx8888_buffer(const void * psrc, int w, int h)
{
int size = h * UpAlign4(w * 4);
void * pdst = NULL;
if (psrc && w > 0 && h > 0) {
pdst = malloc(size);
if (pdst) {
if (rgb888_to_rgbx8888(psrc, w, h, pdst)) {
free(pdst);
pdst = NULL;
}
}
}
return pdst;
}
void * rgbx8888_to_rgb888_buffer(const void * psrc, int w, int h)
{
int size = h * UpAlign4(w * 3);
void * pdst = NULL;
if (psrc && w > 0 && h > 0) {
pdst = malloc(size);
if (pdst) {
if (rgbx8888_to_rgb888(psrc, w, h, pdst)) {
free(pdst);
pdst = NULL;
}
}
}
return pdst;
}
static const RGB_CONVERT_FUN g_convert_func[3][3] =
{
{NULL, rgb565_to_rgb888_buffer, rgb565_to_rgbx8888_buffer},
{rgb888_to_rgb565_buffer, NULL, rgb888_to_rgbx8888_buffer},
{rgbx8888_to_rgb565_buffer, rgbx8888_to_rgb888_buffer, NULL}
};
RGB_CONVERT_FUN get_convert_func(int frombpp, int tobpp)
{
RGB_CONVERT_FUN func_ptr = NULL;
frombpp = UpAlign8(frombpp) / 8 - 2;
tobpp = UpAlign8(tobpp) / 8 - 2;
if ((frombpp >= 0 && frombpp <= 2)
&& (tobpp >= 0 && tobpp <= 2)) {
func_ptr = g_convert_func[frombpp][tobpp];
}
return func_ptr;
}
//-------------------------------------------------------------------
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Base64编码及解码程序源代码
如何使用jpeglib库压缩yuv422?
memcpy
Linux 下 mem类函数 的实现
在iOS AudioQueue中用Speex进行编码和解码
各种小函数——C/C++ 源码 (转)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服