<commit_before>#ifndef SRC_UTILS_IMAGE_PERSISTER_H_

#define SRC_UTILS_IMAGE_PERSISTER_H_

#include <Eigen/Core>
#include <QImage>
#include <boost/filesystem.hpp>
#include <string>
#include <vector>
#include <Magick++.h>

/**
 * \brief Provides static functions to load and save images
 *
 */
class ImagePersister
{
 public:
  template <class T>
  static void saveRGBA32F(T *data, int width, int height, std::string filename)
  {
    Magick::InitializeMagick("");
    Magick::Image image(width, height, "RGBA", Magick::StorageType::FloatPixel,
                        data);
    image.write(filename);
  }

  static void saveR32F(float *data, int width, int height, std::string filename)
  {
    Magick::InitializeMagick("");
    Magick::Image image(width, height, "R", Magick::StorageType::FloatPixel,
                        data);
    image.write(filename);
  }

  static void saveR8I(unsigned char *data, int width, int height,
                      std::string filename)
  {
    QImage image(data, width, height, QImage::Format_Grayscale8);
    image.save(QString(filename.c_str()));
  }

  static std::vector<Eigen::Vector4f> loadRGBA32F(std::string filename)
  {
    if (!boost::filesystem::exists(filename))
      throw std::invalid_argument("Image '" + filename + "' doesn't exist.");

    Magick::Image image(filename);
    std::vector<Eigen::Vector4f> result(image.columns() * image.rows());

    image.write(0, 0, image.columns(), image.rows(), "RGBA",
                Magick::StorageType::FloatPixel, result.data());

    return result;
  }

  static std::vector<float> loadR32F(std::string filename)
  {
    if (!boost::filesystem::exists(filename))
      throw std::invalid_argument("Image '" + filename + "' doesn't exist.");

    Magick::Image image(filename);
    std::vector<float> result(image.columns() * image.rows());<commit_msg>Add functions to save RGBA unsigned char images.<commit_after>#ifndef SRC_UTILS_IMAGE_PERSISTER_H_

#define SRC_UTILS_IMAGE_PERSISTER_H_

#include <Eigen/Core>
#include <QImage>
#include <boost/filesystem.hpp>
#include <string>
#include <vector>
#include <Magick++.h>

/**
 * \brief Provides static functions to load and save images
 *
 */
class ImagePersister
{
 public:
  template <class T>
  static void saveRGBA32F(T *data, int width, int height, std::string filename)
  {
    Magick::InitializeMagick("");
    Magick::Image image(width, height, "RGBA", Magick::StorageType::FloatPixel,
                        data);
    image.write(filename);
  }

  static void saveR32F(float *data, int width, int height, std::string filename)
  {
    Magick::InitializeMagick("");
    Magick::Image image(width, height, "R", Magick::StorageType::FloatPixel,
                        data);
    image.write(filename);
  }

  static void saveRGBA8I(unsigned char *data, int width, int height,
                      std::string filename)
  {
    QImage image(data, width, height, QImage::Format_RGBA8888);
    image.save(QString(filename.c_str()));
  }

  static void flipAndSaveRGBA8I(unsigned char *data, int width, int height,
                      std::string filename)
  {
    QImage image(data, width, height, QImage::Format_RGBA8888);
    image.mirrored(false, true).save(QString(filename.c_str()));
  }

  static void saveR8I(unsigned char *data, int width, int height,
                      std::string filename)
  {
    QImage image(data, width, height, QImage::Format_Grayscale8);
    image.save(QString(filename.c_str()));
  }

  static std::vector<Eigen::Vector4f> loadRGBA32F(std::string filename)
  {
    if (!boost::filesystem::exists(filename))
      throw std::invalid_argument("Image '" + filename + "' doesn't exist.");

    Magick::Image image(filename);
    std::vector<Eigen::Vector4f> result(image.columns() * image.rows());

    image.write(0, 0, image.columns(), image.rows(), "RGBA",
                Magick::StorageType::FloatPixel, result.data());

    return result;
  }

  static std::vector<float> loadR32F(std::string filename)
  {
    if (!boost::filesystem::exists(filename))
      throw std::invalid_argument("Image '" + filename + "' doesn't exist.");

    Magick::Image image(filename);
    std::vector<float> result(image.columns() * image.rows());
{'fraction_non_alphanumeric': 0.1441079034548036, 'fraction_numerical': 0.008991954566966399, 'mean_word_length': 3.8088737201365186, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '787371', 'n_tokens_mistral': 1360, 'n_tokens_neox': 1317, 'n_words': 291}