Digital Images in C++

Icon for links to CS50.dev.

C++ itself doesn't have any built-in support for images, graphics or user-interfaces. All of those capabilities are added using libraries. Click on the little Running-Man in the right to open the VSCode and use these commands to upload the starter code.

$ wget https://occ-cs.com/stb-starter.zip
$ unzip stb-starter.zip
$ cd stb-starter
stb-starter/ $ code main.cpp

We're going to use the stb_image and stb_image_write libraries, written by Sean T. Barret (stb) and placed into the public domain. These libraries provide the ability to read and write several different image formats, in several different ways. Both are C libraries instead of C++ libraries.

The STB libraries are header only libraries. That means you only need to include the header file; there is no separate library to compile and link to. I've already added the header files to the project you are working with in this lesson.

In your own programs, https://github.com/nothings/stb download the latest version of the stb_image.h and stb_image_write.h from the GitHub site and place both files in your project folder. To make sure that the implementation is included, in one file, you need the following preprocessor directives before you include them:

#define STB_IMAGE_IMPLEMENTATION        // REQUIRED (loading)
#define STB_IMAGE_WRITE_IMPLEMENTATION  // writing
#include "stb_image.h"                  // "header-only" C libraries
#include "stb_image_write.h"

I've already added these to main.cpp in the stb-starter.