Last active
December 13, 2023 23:18
-
-
Save mshafae/1e50f814b084f19af6a702ba8f1b9d6d to your computer and use it in GitHub Desktop.
CPSC 120 Example of creating an image using nested for loops and GraphicsMagick.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Gist https://gist.github.com/mshafae/1e50f814b084f19af6a702ba8f1b9d6d | |
// Filename cpsc120_image.cc | |
// CompileCommand clang++ -std=c++17 -I /usr/include/GraphicsMagick -lGraphicsMagick++ cpsc120_image.cc | |
// Create a simple, small image from a given | |
// filename. GraphicsMagick will figure out how to output to the given file | |
// format. | |
// Supported formats: http://www.graphicsmagick.org/formats.html | |
// Example: | |
// clang++ -std=c++17 -I /usr/include/GraphicsMagick -lGraphicsMagick++ | |
// cpsc120_image.cc | |
// ./a.out my_image.jpg | |
// ./a.out my_image.png | |
#include <Magick++.h> | |
#include <cmath> | |
#include <iostream> | |
#include <sstream> | |
#include <string> | |
#include <vector> | |
int main(int argc, char* argv[]) { | |
// Set these to whatever dimensions you want the output image to be. | |
// Warning: don't make this too big or your program will take a long time | |
// to execute. | |
const int kImageWidth{512}; | |
const int kImageHeight{512}; | |
// This initializes the image library we are using. | |
Magick::InitializeMagick(*argv); | |
std::vector<std::string> args{argv, argv + argc}; | |
if (args.size() < 2) { | |
std::cout << "Please provide a path to a file.\n"; | |
std::cout << "For example: " << args.at(0) << " my_image.jpg\n"; | |
return 1; | |
} | |
std::string output_file_name; | |
output_file_name = args.at(1); | |
// 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(1, 1, 1); | |
Magick::Image image(Magick::Geometry(kImageWidth, kImageHeight), white); | |
// We have a white image that is kImageWidth x kImageHeight. Let's | |
// change the pixels in the image to a different color. | |
// For every row, go through each pixel from left to right. | |
// Think of the upper left hand corner of the image as the origin. | |
// The rows go up and down (like a y-axis) and the columns go | |
// left and right (like an x-axis). | |
for (int row = 0; row < image.rows(); row++) { | |
for (int column = 0; column < image.columns(); column++) { | |
// Let's make red go from 0 to 1, from left to right | |
double red{double(column) / double(image.columns() - 1)}; | |
// Let's make the green stay 0.0 | |
double green{0.0}; | |
// Let's make the blue stay 0.0 | |
double blue{0.0}; | |
// If you want to see what the image looks one color at a time | |
// just use one of the colors instead of all three. | |
// Just red | |
// Magick::ColorRGB color{red, 0, 0}; | |
// Just green | |
// Magick::ColorRGB color{0, green, 0}; | |
// Just blue | |
// Magick::ColorRGB color{0, 0, blue}; | |
// We create a new color given the values of red, green, and blue. | |
Magick::ColorRGB color{red, green, blue}; | |
// We set the pixel to the color we just created. The pixel | |
// is located at (column, row) | |
image.pixelColor(column, row, 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