Skip to content

Instantly share code, notes, and snippets.

@mshafae
Created April 28, 2025 07:07
Show Gist options
  • Select an option

  • Save mshafae/f3405bece8f6293036cbd94f2ae555c1 to your computer and use it in GitHub Desktop.

Select an option

Save mshafae/f3405bece8f6293036cbd94f2ae555c1 to your computer and use it in GitHub Desktop.
CPSC 120 Create a simple PNG image
// Gist https://gist.github.com/mshafae/f3405bece8f6293036cbd94f2ae555c1
// Filename cpsc120_simple_image.cc
// CompileCommand clang++ -std=c++20 -I /usr/include/GraphicsMagick -lGraphicsMagick++ cpsc120_simple_image.cc
// Create a simple PNG image.
#include <Magick++.h>
#include <iostream>
int main(int argc, char* argv[]) {
const int kImageWidthAndHeight{512};
std::string output_file_name{"white_dot.png"};
// This initializes the image library we are using.
Magick::InitializeMagick(*argv);
// Colors are defined with three channels: Red, Green, Blue.
// The value for each channel is a floating point number between 0 and 1.
// Black is all 0 values and white is all 1 values.
Magick::ColorRGB white_color{1.0, 1.0, 1.0};
Magick::ColorRGB blue_color{0.0, 0.0, 1.0};
Magick::Geometry image_size(kImageWidthAndHeight, kImageWidthAndHeight);
Magick::Image image(image_size, blue_color);
// Set image's pixel located at x, y to white.
// The x direction is left-right, thus column
// the y direction is up-down, thus row
image.pixelColor(kImageWidthAndHeight / 2, kImageWidthAndHeight / 2, white_color);
image.write(output_file_name);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment