Generating RGBA portable graphic images through C++

PNG images are capable of supporting multiple image properties such as multiple colors, degree of transparency, Gamma correction, Lossless compression, etc. PNG images are widely used and preferred for numerous types of images.

For working with PNG files we will be using the PNGwriter platform-independent, wrapper open-source C++ library for libpng(PNG reference library) one of the most feature-rich library, written in C. PNGwriter library works on Linux, Unix, macOS, and Windows. Its supported features include opening existing PNG images, plotting and reading pixels in the RGB, HSV, and CMYK color spaces, basic shapes, scaling, bilinear interpolation, full TrueType antialiased and rotated text support, and Bezier curves. It requires the FreeType2 library for text support.
For more documentation and libraries visit SourceForge, PNGwriter-github.

How images are generated:
Pixels are the smallest unit that collectively generates an image. Each pixel has some numeric value which represents a color. Below are the steps:

  1. We choose the desired height and width of the image.
  2. In the desired, we iterate and apply colors pixel by pixel.
  3. This collection of pixels is then stored with proper extension and properties, hence generating an image.

Generating Image by applying colors:

Below is the program which generates the RGBA portal graphic images in C++:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to generate PNG images
  
#include <iostream>
#include <pngwriter.h>
#include <string>
  
// Function to generate PNG image
void generate_PNG(int const width,
                  int const height,
                  std::string filepath)
{
  
    // Print the filepath
    cout << endl
         << "Filepath: "
         << filepath.c_str();
  
    // Generate the flag
    pngwriter flag{ width, height, 0,
                    filepath.data() };
  
    // Calculate sizes
    int const size = width / 3;
  
    // Fill the squares
    flag.filledsquare(0, 0, size,
                      2 * size, 0,
                      0, 65535);
  
    flag.filledsquare(size, 0, 2 * size,
                      2 * size, 0,
                      65535, 65535);
  
    flag.filledsquare(2 * size, 0,
                      3 * size, 2 * size,
                      65535, 0, 65535);
  
    // Close the flag
    flag.close();
}
  
// Driver code
int main()
{
    // Given width and height
    int width = 300, height = 200;
  
    // Filepath
    std::string filepath;
  
    // Function call to generate PNG image
    generate_PNG(width, height, filepath);
  
    return 0;
}

chevron_right


Output:

Explanation:
The above program takes width, height, and file address with a file name. The pngwriter class represents a PNG image. The constructor of the PNG image allows us to set the width and height in pixels, a background colour, and the path to the file where the image should be saved. As illustrated in the above code, we just arranged three solid colors side-by-side and for that, we used filledsquare() function which has taken the x-y coordinates value from start to end positions and a colour values (R, G, B). When the image is saved in memory, then call the close() method to save it to a disk file.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.