【发布时间】:2014-11-08 16:32:58
【问题描述】:
我想尝试在 C++ 中制作一个程序,该程序获取图像并逐像素扫描,对于相对较暗的像素,它会打印一个空格,而对于浅色像素,它会打印如下符号:
"@#$%^!M()-~, etc" 将仅使用符号绘制新图片。
我是新来的,我真的很感激一些关于如何解决这个问题的指导,
我想给我的老师一个惊喜。
非常感谢您。
好吧,我已经拥有了:
#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 标签?到目前为止,您还尝试过什么?另外,如果您不自己学习,就不会给老师留下深刻印象!
-
你说得对,我删除了 C 标签,到目前为止我没有尝试任何东西,我不知道如何从图像中获取像素
-
您需要充实更多细节……您打算使用什么图像格式?如果它是压缩的(jpeg、png 等),您还需要在代码中对其进行解压缩。
-
我发现了那个东西:codeproject.com/Questions/467586/…,因为解决方案 3 说用字符更改像素而不关心哪个图片文件,我的意思是所有图片类型都对我有好处
-
你最好先学个bmp库。在您可以使用该库之后,您的任务应该很容易。希望这有助于stackoverflow.com/questions/199403/…
标签: c++