admin 管理员组文章数量: 1086019
快速图像像素操作
快速图像像素操作
提供了2个API,SetDIBPixelColor和GetDIBPixelColor,
对DIB图像像素快速设置和取得, 以替代系统提供的SetPixel和GetPixel.
SetPixel和GetPixel是基于DC的操作,速度太慢.
// ImageSetPixelFast.cpp : 定义控制台应用程序的入口点。
// cheungmine
#include "stdafx.h"
#include <atlstr.h>
#include <atlimage.h>
// 24位色和16位色转换宏
// by cheungmine
#define RGB888toRGB565(r,g,b) ((WORD)(((WORD(r)<<8)&0xF800)|((WORD(g)<<3)&0x7E0)|((WORD(b) >> 3))))
#define RGBtoRGB565(rgb) ((WORD)(((((WORD)((rgb)>>3))&(0x1F))<<11)|((((WORD)((rgb)>>10))&(0x3F))<<5)|(((WORD)((rgb)>>19))&(0x1F))))
#define RGB888toRGB555(r,g,b) ((WORD)(((WORD(r)<<7)&0x7C00)|((WORD(g)<<2)&0x3E0)|((WORD(b)>>3))))
#define RGBtoRGB555(rgb) ((WORD)(((((WORD)((rgb)>>3))&(0x1F))<<10)|((((WORD)((rgb)>>11))&(0x1F))<<5)|(((WORD)((rgb)>>19))&(0x1F))))
#define RGB555toRGB(rgb555) ((DWORD)(((BYTE)(((rgb555)>>7)&0xF8)|((WORD)((BYTE)(((rgb555)>>2)&0xF8))<<8))|(((DWORD)(BYTE)(((rgb555)<<3)&0xF8))<<16)))
#define RGB565toRGB(rgb565) ((DWORD)(((BYTE)((((rgb565)&0xF800)>>11)<<3)|((WORD)((BYTE)((((rgb565)&0x07E0)>>5)<<2))<<8))|(((DWORD)(BYTE)(((rgb565)&0x001F)<<3))<<16)))
typedef struct
{
void *lpBits;
int width;
int height;
int nBpp;
int nPitch;
int row;
int col;
DWORD dwColor;
}DIBitsDesc;
void SetDIBPixelColor(DIBitsDesc *pDib)
{
if (pDib->nPitch>0){
// top-down DIB
if(pDib->nBpp==16){
int at = -pDib->nPitch*(pDib->row-pDib->height+1)+pDib->col*2;
*((WORD*)(&((BYTE*)pDib->lpBits)[at])) = (WORD) pDib->dwColor;
}
if (pDib->nBpp==24){
int at = -pDib->nPitch*(pDib->row-pDib->height+1)+pDib->col*3;
BYTE* gbr = &((BYTE*)pDib->lpBits)[at];
*gbr++ = GetBValue(pDib->dwColor);
*gbr++ = GetGValue(pDib->dwColor);
*gbr = GetRValue(pDib->dwColor);
}
else if(pDib->nBpp==32){
int at = -pDib->nPitch*(pDib->row-pDib->height+1)+pDib->col*4;
BYTE* gbra = &((BYTE*)pDib->lpBits)[at];
*gbra++ = 0;
*gbra++ = GetBValue(pDib->dwColor);
*gbra++ = GetGValue(pDib->dwColor);
*gbra = GetRValue(pDib->dwColor);
}
}
else{
// bottom-up DIB
if(pDib->nBpp==16){
int at = pDib->nPitch*pDib->row+pDib->col*2;
*((WORD*)(&((BYTE*)pDib->lpBits)[at])) = (WORD) pDib->dwColor;
}
if (pDib->nBpp==24){
int at = pDib->nPitch*pDib->row+pDib->col*3;
BYTE* gbr = &((BYTE*)pDib->lpBits)[at];
*gbr++ = GetBValue(pDib->dwColor);
*gbr++ = GetGValue(pDib->dwColor);
*gbr = GetRValue(pDib->dwColor);
}
else if(pDib->nBpp==32){
int at = pDib->nPitch*pDib->row+pDib->col*4;
BYTE* agbr = &((BYTE*)pDib->lpBits)[at];
*agbr++ = 0;
*agbr++ = GetBValue(pDib->dwColor);
*agbr++ = GetGValue(pDib->dwColor);
*agbr = GetRValue(pDib->dwColor);
}
}
}
void GetDIBPixelColor(DIBitsDesc *pDib)
{
if (pDib->nPitch>0){
// top-down DIB
if(pDib->nBpp==16){
int at = -pDib->nPitch*(pDib->row-pDib->height+1)+pDib->col*2;
pDib->dwColor = *((WORD*)(&((BYTE*)pDib->lpBits)[at]));
}
if (pDib->nBpp==24){
int at = -pDib->nPitch*(pDib->row-pDib->height+1)+pDib->col*3;
BYTE* gbr = &((BYTE*)pDib->lpBits)[at];
pDib->dwColor = RGB(gbr[2], gbr[1], gbr[0]);
}
else if(pDib->nBpp==32){
int at = -pDib->nPitch*(pDib->row-pDib->height+1)+pDib->col*4;
BYTE* agbr = &((BYTE*)pDib->lpBits)[at];
pDib->dwColor = RGB(agbr[2], agbr[1], agbr[0]);
}
}
else{
// bottom-up DIB
if(pDib->nBpp==16){
int at = pDib->nPitch*pDib->row+pDib->col*2;
pDib->dwColor = *((WORD*)(&((BYTE*)pDib->lpBits)[at]));
}
if (pDib->nBpp==24){
int at = pDib->nPitch*pDib->row+pDib->col*3;
const BYTE* gbr = &((BYTE*)pDib->lpBits)[at];
pDib->dwColor = RGB(gbr[2], gbr[1], gbr[0]);
}
else if(pDib->nBpp==32){
int at = pDib->nPitch*pDib->row+pDib->col*4;
const BYTE* agbr = &((BYTE*)pDib->lpBits)[at];
pDib->dwColor = RGB(agbr[2], agbr[1], agbr[0]);
}
}
}
int main(int argc, char* argv[])
{
CImage img;
img.Load("c://greatwall.jpg");
DIBitsDesc dib;
dib.width = img.GetWidth();
dib.height = img.GetHeight();
dib.nBpp = img.GetBPP();
dib.nPitch = img.GetPitch();
dib.lpBits = img.GetBits();
if (img.IsDIBSection())
{
for(int r=0; r<dib.height; r++){
for(int c=0; c<dib.width; c++){
// too slow to use: img.SetPixel(c, r, RGB(0,255,0));
dib.row = r;
dib.col = c;
dib.dwColor = RGB(0,255,0);
SetDIBPixelColor(&dib);
}
}
}
img.Save("c://test2.bmp");
return 0;
}
本文标签: 快速图像像素操作
版权声明:本文标题:快速图像像素操作 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1693588123a230899.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论