快速图像像素操作

快速图像像素操作

提供了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;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

车斗

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

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

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

打赏作者

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

抵扣说明:

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

余额充值