|
| 1 | +#pragma once |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +#include <algorithm> |
| 8 | +#include <fstream> |
| 9 | +#include <iostream> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +#include "color.h" |
| 13 | +// Assuming 'color' is a class or struct already defined |
| 14 | +// with overloaded operator+ and a method to output the color data |
| 15 | + |
| 16 | +class PPMImageFile { |
| 17 | +private: |
| 18 | + std::string filename; |
| 19 | + int image_width, image_height; |
| 20 | + std::vector<color> data; |
| 21 | + bool IsNormalized; |
| 22 | + |
| 23 | +public: |
| 24 | + PPMImageFile(const std::string &file_name, int width = 0, int height = 0) |
| 25 | + : filename(file_name), image_width(width), image_height(height), |
| 26 | + IsNormalized(false) { |
| 27 | + data.resize(width * height); |
| 28 | + } |
| 29 | + |
| 30 | + color *getHostPtr() { return data.data(); } |
| 31 | + |
| 32 | + void setData(color *C) { |
| 33 | + for (int i = 0, e = image_width * image_height; i != e; ++i) |
| 34 | + data[i] = C[i]; |
| 35 | + } |
| 36 | + |
| 37 | + void normalize() { |
| 38 | + for (auto &pixel_color : data) { |
| 39 | + auto r = pixel_color.x(); |
| 40 | + auto g = pixel_color.y(); |
| 41 | + auto b = pixel_color.z(); |
| 42 | + |
| 43 | + // Apply a linear to gamma transform for gamma 2 |
| 44 | + r = linear_to_gamma(r); |
| 45 | + g = linear_to_gamma(g); |
| 46 | + b = linear_to_gamma(b); |
| 47 | + |
| 48 | + // Write the translated [0,255] value of each color component. |
| 49 | + static const interval intensity(0.000, 0.999); |
| 50 | + pixel_color = color(static_cast<int>(256 * intensity.clamp(r)), |
| 51 | + static_cast<int>(256 * intensity.clamp(g)), |
| 52 | + static_cast<int>(256 * intensity.clamp(b))); |
| 53 | + } |
| 54 | + IsNormalized = true; |
| 55 | + } |
| 56 | + |
| 57 | + bool save() const { |
| 58 | + if (!IsNormalized) { |
| 59 | + std::cerr |
| 60 | + << "Error: Image is not normalized. Saving the unnormalized image." |
| 61 | + << std::endl; |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + std::ofstream file(filename); |
| 66 | + |
| 67 | + if (!file) { |
| 68 | + std::cerr << "File could not be opened for writing." << std::endl; |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + // PPM header |
| 73 | + file << "P3\n" << image_width << ' ' << image_height << "\n255\n"; |
| 74 | + |
| 75 | + // Write each pixel to the file |
| 76 | + for (const auto &pixel_color : data) { |
| 77 | + file << pixel_color.x() << ' ' << pixel_color.y() << ' ' |
| 78 | + << pixel_color.z() << '\n'; |
| 79 | + } |
| 80 | + |
| 81 | + file.close(); |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + bool load() { |
| 86 | + std::ifstream file(filename); |
| 87 | + |
| 88 | + if (!file) { |
| 89 | + std::cerr << "File could not be opened for reading." << std::endl; |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + std::string header; |
| 94 | + int max_val; |
| 95 | + |
| 96 | + // Read the header and check format |
| 97 | + file >> header; |
| 98 | + if (header != "P3") { |
| 99 | + std::cerr << "Unsupported file format." << std::endl; |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + // Read image dimensions and maximum value |
| 104 | + file >> image_width >> image_height >> max_val; |
| 105 | + |
| 106 | + // Resize the data vector to hold the image data |
| 107 | + data.resize(image_width * image_height); |
| 108 | + |
| 109 | + // Read pixel data |
| 110 | + for (auto &pixel_color : data) { |
| 111 | + int r, g, b; |
| 112 | + file >> r >> g >> b; |
| 113 | + pixel_color = color(r, g, b); |
| 114 | + } |
| 115 | + |
| 116 | + IsNormalized = true; // Assuming the loaded image is already normalized |
| 117 | + file.close(); |
| 118 | + return true; |
| 119 | + } |
| 120 | + bool compare(const PPMImageFile &img, double threshold = 1e-3) const { |
| 121 | + if (IsNormalized != img.IsNormalized) { |
| 122 | + std::cerr << "Cannot compare " << filename << " and " << img.filename |
| 123 | + << " because one is normalized and the other is not." |
| 124 | + << std::endl; |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + if (image_width != img.image_width || image_height != img.image_height) { |
| 129 | + std::cerr << "Images dimensions do not match." << std::endl; |
| 130 | + return false; |
| 131 | + } |
| 132 | + if (IsNormalized) { |
| 133 | + threshold *= 255.0; |
| 134 | + } |
| 135 | + |
| 136 | + struct Difference { |
| 137 | + double value; |
| 138 | + int x, y; |
| 139 | + }; |
| 140 | + |
| 141 | + std::vector<Difference> topDifferences; |
| 142 | + bool anySignificantDifference = false; |
| 143 | + |
| 144 | + for (int y = 0; y < image_height; ++y) { |
| 145 | + for (int x = 0; x < image_width; ++x) { |
| 146 | + const color &c1 = data[y * image_width + x]; |
| 147 | + const color &c2 = img.data[y * image_width + x]; |
| 148 | + |
| 149 | + double diff = |
| 150 | + std::max({std::abs(c1.x() - c2.x()), std::abs(c1.y() - c2.y()), |
| 151 | + std::abs(c1.z() - c2.z())}); |
| 152 | + |
| 153 | + if (diff > threshold) { |
| 154 | + anySignificantDifference = true; |
| 155 | + |
| 156 | + if (topDifferences.size() < 10) { |
| 157 | + topDifferences.push_back({diff, x, y}); |
| 158 | + std::sort(topDifferences.begin(), topDifferences.end(), |
| 159 | + [](const Difference &a, const Difference &b) { |
| 160 | + return a.value > b.value; |
| 161 | + }); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + if (anySignificantDifference) { |
| 168 | + // Output top differences |
| 169 | + std::cout << "Top Differences between " << filename << " and " |
| 170 | + << img.filename << ":\n"; |
| 171 | + for (const auto &diff : topDifferences) { |
| 172 | + std::cout << "Location (" << diff.x << ", " << diff.y << "), " |
| 173 | + << "Difference: " << diff.value << ", " << filename << ": " |
| 174 | + << data[diff.y * image_width + diff.x].toString() << ", " |
| 175 | + << img.filename << ": " |
| 176 | + << img.data[diff.y * image_width + diff.x].toString() << "\n"; |
| 177 | + } |
| 178 | + return false; |
| 179 | + } else { |
| 180 | + std::cout << filename << " and " << img.filename << " are the same.\n"; |
| 181 | + return true; |
| 182 | + } |
| 183 | + } |
| 184 | +}; |
0 commit comments