/*
  Author     : Gary M. Zoppetti
  Course     : CMSC 476
  Description: Show how to use ImageMagick to read/write pixels of an image.
               Students should use a Makefile and pkg-config to determine
                 how to build this.
*/

#include <print>

#include <Magick++.h>

int
main ()
{
  Magick::InitializeMagick (nullptr);

  Magick::Image image;
  try
  {
    image.read ("MillersvilleLogo.png");
  }
  catch (Magick::Exception& error)
  {
    std::println ("Caught exception: [{}]", error.what ());
    return 1;
  }

  size_t width = image.columns ();
  size_t height = image.rows ();
  size_t channels = image.channels ();

  // Get a view into pixel cache.
  // Quantum is ImageMagick's internal data type (a float that uses 16 bits).
  Magick::Quantum* pixels = image.getPixels (0, 0, width, height);

  for (size_t y = 0; y < height; ++y)
  {
    for (size_t x = 0; x < width; ++x)
    {
      // ImageMagick stores pixels contiguously: R, G, B, [ A ]
      // We calculate the offset based on the number of channels
      size_t offset = (y * width + x) * channels;

      // RGB format, with optional alpha
      // pixels[offset + 0] = 0;
      pixels[offset + 1] = 15;
      // pixels[offset + 2] is Blue
    }
  }

  // Wite cache back to image.
  image.syncPixels ();

  // Write result.
  image.write ("Output.png");
}
