C ++ output image file format PPM

About PPM

In order to visually observe the results, you need to learn when output graphics images, and PPM is one of the most simple picture format, ideal for novices to use.
PPM contents of the file something like this:

The first row is fixed P3, the representative write PPM format is an RGB image, in addition to PGM and PBM format corresponding to P1 and P2, respectively, representing monochrome and gray scale image.
The second line of pictures on behalf of two integers 宽度and 高度.
The third row represents 像素值范围generally written as 255, the representative value of each color channel 0-255.

The next three integers each row represents a pixel value of each color channel, a total 宽度*高度row.
From the order of the upper left corner, each row from left to right, then down row by row from the filling.

The sample code image output by the C ++ PPM

int main()
{
    ofstream OutImage;
    OutImage.open("Image.ppm");
    int nx = 200;
    int ny = 100;
    OutImage << "P3\n" << nx << ' ' << ny << "\n255\n";
    for(int i = ny-1; i >= 0; i--)
    {
        for (int j = 0; j < nx; j++)
        {
            float r = (float)j / nx;
            float g = (float)i / ny;
            float b = 0.2;
            int ir = int(255.99 * r);
            int ig = int(255.99 * g);
            int ib = int(255.99 * b);
            OutImage << ir << ' ' << ig << ' ' << ib << '\n';
        }
    }
    return 0;
}

To view the last PPM format images need to support the format picture viewer, I'm using XnView.
result:

PPM format is very simple and easy for beginners, if you want to output more general png format, you can use the power of God milo png function: https://zhuanlan.zhihu.com/p/26525083

Guess you like

Origin www.cnblogs.com/LiveForGame/p/11768925.html