2014-11-08 131 views
1

我想在C++中嘗試製作一個圖像並逐個像素地進行掃描的程序,對於相對較暗的像素,它會打印一個空間,對於亮像素,它會打印如下符號: 「 @#$%^!M() - 〜等「,這將只用符號繪製新圖片。
我是新的,我真的很感激一些指導,我是如何處理這個的, 我想驚喜我的老師。
非常感謝您提前。將像素更改爲符號/字符

確定這是我已經有:

#include <windows.h> 
#include <stdio.h>  

BYTE* ConvertBMPToRGBBuffer(BYTE* Buffer, int width, int height) 
{ 
    // first make sure the parameters are valid 
    if ((NULL == Buffer) || (width == 0) || (height == 0)) 
     return NULL; 

    // find the number of padding bytes 

    int padding = 0; 
    int scanlinebytes = width * 3; 
    while ((scanlinebytes + padding) % 4 != 0)  // DWORD = 4 bytes 
     padding++; 
    // get the padded scanline width 
    int psw = scanlinebytes + padding; 

    // create new buffer 
    BYTE* newbuf = new BYTE[width*height * 3]; 

    // now we loop trough all bytes of the original buffer, 
    // swap the R and B bytes and the scanlines 
    long bufpos = 0; 
    long newpos = 0; 
    for (int y = 0; y < height; y++) 
    for (int x = 0; x < 3 * width; x += 3) 
    { 
     newpos = y * 3 * width + x; 
     bufpos = (height - y - 1) * psw + x; 

     newbuf[newpos] = Buffer[bufpos + 2]; 
     newbuf[newpos + 1] = Buffer[bufpos + 1]; 
     newbuf[newpos + 2] = Buffer[bufpos]; 
    } 

    return newbuf; 
} 


BYTE* LoadBMP(int* width, int* height, long* size, LPCTSTR bmpfile) 
{ 
    // declare bitmap structures 
    BITMAPFILEHEADER bmpheader; 
    BITMAPINFOHEADER bmpinfo; 
    // value to be used in ReadFile funcs 
    DWORD bytesread; 
    // open file to read from 
    HANDLE file = CreateFile(bmpfile, GENERIC_READ, FILE_SHARE_READ, 
     NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL); 
    if (NULL == file) 
     return NULL; // coudn't open file 


    // read file header 
    if (ReadFile(file, &bmpheader, sizeof (BITMAPFILEHEADER), &bytesread, NULL) == false) 
    { 
     CloseHandle(file); 
     return NULL; 
    } 

    //read bitmap info 

    if (ReadFile(file, &bmpinfo, sizeof (BITMAPINFOHEADER), &bytesread, NULL) == false) 
    { 
     CloseHandle(file); 
     return NULL; 
    } 

    // check if file is actually a bmp 
    if (bmpheader.bfType != 'MB') 
    { 
     CloseHandle(file); 
     return NULL; 
    } 

    // get image measurements 
    *width = bmpinfo.biWidth; 
    *height = abs(bmpinfo.biHeight); 

    // check if bmp is uncompressed 
    if (bmpinfo.biCompression != BI_RGB) 
    { 
     CloseHandle(file); 
     return NULL; 
    } 

    // check if we have 24 bit bmp 
    if (bmpinfo.biBitCount != 24) 
    { 
     CloseHandle(file); 
     return NULL; 
    } 


    // create buffer to hold the data 
    *size = bmpheader.bfSize - bmpheader.bfOffBits; 
    BYTE* Buffer = new BYTE[*size]; 
    // move file pointer to start of bitmap data 
    SetFilePointer(file, bmpheader.bfOffBits, NULL, FILE_BEGIN); 
    // read bmp data 
    if (ReadFile(file, Buffer, *size, &bytesread, NULL) == false) 
    { 
     delete[] Buffer; 
     CloseHandle(file); 
     return NULL; 
    } 

    // everything successful here: close file and return buffer 

    CloseHandle(file); 

    return Buffer; 
} 



void main() 
{ 


    int x, y; 
    long s, s2; 
    BYTE* a = LoadBMP(&x, &y, &s, L"20140626_143101.bmp); 
    BYTE* b = ConvertBMPToRGBBuffer(a, x, y); 
    // what now?? 

    delete[] a; 
    delete[] b; 
} 

後,我轉換BMP至RGB緩衝區什麼我可以下一個辦?檢查有關緩衝區,如果它的黑暗或光線像素

+0

首先,如果你在談論C++爲什麼使用C標籤?你到目前爲止還嘗試過什麼?如果你不自己學習,你也不會打動你的老師! – Rizier123 2014-11-08 03:45:42

+0

你對我刪除C標籤,我沒有嘗試任何東西到目前爲止,我不知道如何從圖像得到像素 – GuruGada 2014-11-08 03:46:59

+0

你需要充實更多的細節...你打算使用什麼圖像格式?如果它被壓縮(jpeg,png等),則需要在代碼中對其進行解壓縮。 – Julian 2014-11-08 03:48:53

回答

1

我想爲您設置正確的路徑,即沒有給你整個答案。我找到了一些參考資料,可以幫助您瞭解您想要了解的內容。

首先,必須解碼圖像。那就是將圖像以另一種可讀格式複製到變量中。寫這其中的一個,特別是當知道C++時,這不是一個好主意。但有很多圖書館都做這件事情。我建議:
http://cimg.sourceforge.net/,然後爲簡短的教程:http://www.math.ucla.edu/~wittman/hyper/vick/Cimg_tutorial.pdf和程序,即編輯單個像素:http://cimg.sourceforge.net/reference/group__cimg__loops.html(請看:循環在內部區域和邊界部分)

這有望將指向您在正確的方向。
P.S.隨意問你是否還有其他問題。

+0

我必須這樣做無需下載任何庫或軟件 我有那個Misson,只用C++庫做 – GuruGada 2014-11-08 08:33:38

+0

好吧,這會讓它變得非常困難。你有多少時間?因爲,這將需要很長時間。無可否認,我很佩服你的目標。 – Cartier 2014-11-08 14:42:35

+0

爲什麼?也許你可以給我一個推動之後我能做些什麼 – GuruGada 2014-11-08 16:01:07