C++: Convert text file of integers into a bitmap image file in BMP format

后端 未结 5 2116
我在风中等你
我在风中等你 2020-12-09 13:29

I have a text file being saved by a matrix library containing a 2D matrix as such:

1 0 0 
6 0 4
0 1 1

Where each number is represented with

5条回答
  •  轮回少年
    2020-12-09 13:44

    If you choose the right image format this is very easy. PGM has an ASCII variant that looks almost exactly like your matrix, but with a header.

    P2
    3 3
    6
    1 0 0 
    6 0 4
    0 1 1
    

    Where P2 is the magic for ASCII PGM, the size is 3x3 and 6 is the maxval. I chose 6 because that was the maximum value you presented, which makes 6 white (while 0 is black). In a typical PGM that's 255, which is consistent with an 8-bit grayscale image.

    PPM is almost as simple, it just has 3 color components per pixel instead of 1.

    You can operate on these images with anything that takes PPM (netpbm, ImageMagick, GIMP, etc). You can resave them as binary PPMs which are basically the same size as an equivalent BMP.

提交回复
热议问题