快速图像像素操作
2009-12-19 21:39
344 查看
快速图像像素操作
提供了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;
}
提供了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;
}
相关文章推荐
- 快速对图像的像素进行操作 opencv 实战
- opencv学习之快速对图像的像素进行操作 opencv 实战
- 快速图像像素操作
- 快速对图像的像素进行操作 opencv 实战
- (转载)快速对图像的像素进行操作 opencv 实战
- OpenCv 如何对图像的像素进行操作
- opencv像素基本操作及图像遍历at
- opencv像素基本操作及图像遍历at
- opencv-彩色RGB图像的像素值操作
- Ex1:图像读取和显示以及像素操作
- opencv像素基本操作及图像遍历at
- Android游戏Graphics绘图之图像像素操作
- BGRABitmap图像操作4:直接操作像素
- python3-opencv库(2)--遍历图像像素,利用Numpy创建图像,Numpy基本操作
- 对图像的单个像素操作
- opencv像素基本操作及图像遍历at
- BGRABitmap图像操作13:标准坐标系统下像素级别例子
- 我的OpenCV学习笔记(三):利用操作像素完成简单的图像处理:加入椒盐噪声、图像翻转、改变对比度、图像锐化
- 图像像素操作
- OpenGL: 图像像素操作