快速映像像素操作

來源:互聯網
上載者:User

快速映像像素操作

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.