我想在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緩衝區什麼我可以下一個辦?檢查有關緩衝區,如果它的黑暗或光線像素
首先,如果你在談論C++爲什麼使用C標籤?你到目前爲止還嘗試過什麼?如果你不自己學習,你也不會打動你的老師! – Rizier123 2014-11-08 03:45:42
你對我刪除C標籤,我沒有嘗試任何東西到目前爲止,我不知道如何從圖像得到像素 – GuruGada 2014-11-08 03:46:59
你需要充實更多的細節...你打算使用什麼圖像格式?如果它被壓縮(jpeg,png等),則需要在代碼中對其進行解壓縮。 – Julian 2014-11-08 03:48:53