【问题标题】:Change pixel to Symbols/Chars将像素更改为符号/字符
【发布时间】: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++


【解决方案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(查看:循环遍历内部区域和边界部分)

希望这会为您指明正确的方向。
附:如果您还有其他问题,请随时提问。

【讨论】:

  • 我必须在不下载任何库或软件的情况下完成它,而我拥有 Misson 只需要 C++ 库就可以做到这一点
  • 好吧,那会很困难。你有多少时间?因为,这将需要很长时间。诚然,我很欣赏你的目标。
  • 因为,就像我说的,制作自己的解析器非常耗时。有时间我会把你链接到几个网站。
猜你喜欢
  • 2017-09-08
  • 1970-01-01
  • 2018-10-15
  • 2020-07-23
  • 2018-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多