logo

English

이곳의 프로그래밍관련 정보와 소스는 마음대로 활용하셔도 좋습니다. 다만 쓰시기 전에 통보 정도는 해주시는 것이 예의 일것 같습니다. 질문이나 오류 수정은 siseong@gmail.com 으로 주세요. 감사합니다.

[Win32] HBITMAP Contrast 조절하는 코드 - RGB 이미지 보정

by 엉뚱도마뱀 posted May 04, 2018
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

GDI+를 사용하면 보다 편리한 방법이 있습니다만

전통적인 GDI를 사용하는 코드에서 HBITMAP으로 Contrast를 조절하는 방법입니다.

이 코드를 응용하면 RGB 형식으로 변환하여 여러가지 이미지 처리를 하는데 응용할 수 있습니다.

 

 

#include <windows.h>

#include <complex>

 

static unsigned char TransLut[256];

 

void build_lookup_table ( int contr )

{

    float step,step_value;

 

    for (int i=0; i < 256; i++)

        TransLut[i] = i;

 

    if (contr > 0)

    {

        unsigned int MinBin = contr;

        unsigned int MaxBin = 255 - contr;

 

        step = sqrt((double)contr)/contr;

        step_value = 0.0;

 

        for ( i = 0; i < MinBin; i++)

        {

            TransLut[i] = (unsigned char)step_value;

            step_value += step;

        }

 

        step = 256.0f / (float)(MaxBin-MinBin);

 

        for ( i = MinBin; i <= MaxBin; i++)

        {

 

        if (step_value > 255.0f)

        {

            step_value = 255.0f;

            step = 0.0f;

        }

 

        TransLut[i] = (unsigned char)step_value;

        step_value += step;

        }

 

        for ( i = MaxBin + 1; i < 256; i++)

            TransLut[i] = 255;

 

    }

    else

    {

        if (contr<0)

        {

            step = (256.0+(float)(contr*2))/256.0;

            step_value = (float)contr * -1.0;

 

            for (i = 0;i < 256; i++)

            {

                TransLut[i] = (unsigned char)step_value;

                step_value += step;

            }

        }

    }

}

 

BOOL ChangeBitmapContrast(HBITMAP * phBitmap, int iCount)

{

    BITMAP    bm;

    BYTE    * pBits;

    RGBQUAD * pRgb;

    WORD      wByteCount;

    int       i, iPixels, gray;

 

    build_lookup_table(iCount);

 

    // Take BITMAP structure from HBITMAP

 

    GetObject(*phBitmap, sizeof(BITMAP), &bm);

 

    // Calculate bytes to read

 

    wByteCount = bm.bmHeight * (2 * ((bm.bmWidth * bm.bmBitsPixel + 15) / 16));

 

    // Alocate momory for bits od pixels and get pointers

 

    pBits = (BYTE *) malloc(wByteCount);

    GetBitmapBits(*phBitmap, wByteCount, pBits);

 

    // Convert pointer to byte to pointer to RGBQUAD

 

    pRgb = (RGBQUAD *) pBits;

 

    // Operate on pixel's colors

 

    iPixels = wByteCount / (bm.bmBitsPixel / 8);

 

    for(i = 0; i < iPixels; i++, pRgb++)

    {

        gray = (pRgb->rgbRed + pRgb->rgbGreen + pRgb->rgbBlue) / 3;

        int k = TransLut[gray]-gray;

 

        pRgb->rgbRed   = min(pRgb->rgbRed   + k, 255);

        pRgb->rgbGreen = min(pRgb->rgbGreen + k, 255);

        pRgb->rgbBlue  = min(pRgb->rgbBlue  + k, 255);

    }

 

    SetBitmapBits(*phBitmap, wByteCount, pBits);

 

    return TRUE;

}

TAG •

List of Articles
No. Subject Author Date Views
45 [C#] UI Update from Thread, Thread에서 UI 업데이트 하기 샘플 코드 lizard2019 2019.01.23 1034
44 [C#] 프로그램 종료 방법 lizard2019 2019.01.23 6761
43 [C#] 코드 실행 시간 측정 및 DateTime 스트링으로 변환 포맷 lizard2019 2019.01.23 22947
» [Win32] HBITMAP Contrast 조절하는 코드 - RGB 이미지 보정 엉뚱도마뱀 2018.05.04 734
41 [Windows] DOS 명령어 실행하고 결과 스트링 가져오는 샘플 코드 digipine 2017.11.02 2687
40 VC++ UTF8 변환 관련 매크로 digipine 2017.11.02 8778
39 C# 으로 구현한 화면 캡춰 클래스 1 digipine 2017.11.02 33962
38 C# - 한글로된 폰트명 처리 방법 개선 (Font Name Localization) digipine 2017.11.02 1388
37 [C#] StreamReader 에서의 한글 Encoding 문제 digipine 2017.10.29 999
36 [API Hooking] Dll Injection 하는 방법 digipine 2017.10.29 5210
35 [WIN32] 파일 핸들로 파일 명 구하기 digipine 2017.10.29 1060
34 [WIN32] Process ID로 HWND 구하기 digipine 2017.10.29 5011
33 MS의 Hot Fix API의 유형 연구 digipine 2017.10.29 311
32 [WIN32] 실행 중인 프로세스를 외부에서 강제로 종료, 안전한 TerminateProcess digipine 2017.10.29 3455
31 [WIN32] API Hook 정리 문서 digipine 2017.10.29 1963
30 [WIN32, WINCE] 디스크 용량 구하는 방법 API GetDiskFreeSpaceEx digipine 2017.10.29 1564
29 [WINCE] MulDiv 함수 구현 digipine 2017.10.29 699
28 [DirectShow] 화면 원본 비율유지 digipine 2017.10.29 956
27 [VC++, WInAPI] 폴더를 통채로 지우기, 서브 폴더 포함, DeleteAllFiles digipine 2017.10.29 1761
26 [Win API]프로세스 아이디와 윈도우 핸들을 이용 파일명 구하기 digipine 2017.10.29 1390
Board Pagination Prev 1 2 3 Next
/ 3