2022-10-28 09:31:13 +02:00
|
|
|
#include "common/texture.h"
|
|
|
|
#include <cmath>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
Texture::Texture(int width, int height) { this->resize(width, height); }
|
|
|
|
|
|
|
|
Texture::Texture(char const *fileName) { this->load(fileName); }
|
|
|
|
|
|
|
|
bool Texture::load(char const *fileName) {
|
2022-12-13 00:16:19 +01:00
|
|
|
CImg<unsigned char> img_char(fileName);
|
|
|
|
if (img_char.is_empty()) {
|
|
|
|
std::cerr << "(Image): Could not open file " << fileName << std::endl;
|
|
|
|
return false;
|
2022-10-28 09:31:13 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 00:16:19 +01:00
|
|
|
this->image_ = CImg<float>(img_char.width(), img_char.height(), img_char.depth(), 3, 0);
|
|
|
|
for (int c = 0; c < 3; c++)
|
|
|
|
cimg_forXYZ(img_char, x, y, z) {
|
|
|
|
this->image_(x, y, z, c) = img_char(x, y, z, std::min(c, img_char.spectrum() - 1)) / 255.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-10-28 09:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Texture::save(char const *fileName) const {
|
2022-12-13 00:16:19 +01:00
|
|
|
CImg<unsigned char> img_char;
|
|
|
|
img_char = 255 * this->image_;
|
2022-10-28 09:31:13 +02:00
|
|
|
|
2022-12-13 00:16:19 +01:00
|
|
|
img_char.save(fileName);
|
|
|
|
return true;
|
2022-10-28 09:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Color Texture::getPixelAt(int x, int y) const {
|
2022-12-13 00:16:19 +01:00
|
|
|
if ((x %= this->width()) < 0)
|
|
|
|
x += this->width();
|
|
|
|
if ((y %= this->height()) < 0)
|
|
|
|
y += this->height();
|
|
|
|
return Color(this->image_.atXY(x, y, 0), this->image_.atXY(x, y, 1), this->image_.atXY(x, y, 2));
|
2022-10-28 09:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Texture::setPixelAt(int x, int y, Color const &color) {
|
2022-12-13 00:16:19 +01:00
|
|
|
if ((x %= this->width()) < 0)
|
|
|
|
x += this->width();
|
|
|
|
if ((y %= this->height()) < 0)
|
|
|
|
y += this->height();
|
|
|
|
this->image_(x, y, 0) = color.r;
|
|
|
|
this->image_(x, y, 1) = color.g;
|
|
|
|
this->image_(x, y, 2) = color.b;
|
2022-10-28 09:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Color Texture::color(float u, float v, bool interpolate) const {
|
2022-12-13 00:16:19 +01:00
|
|
|
Color color;
|
|
|
|
if (!interpolate) {
|
|
|
|
color = this->getPixelAt(int(roundf(u * this->width())), int(roundf(v * this->height())));
|
|
|
|
} else {
|
|
|
|
// IMPLEMENT bilinear interpolation
|
|
|
|
float x1 = std::floor(u * this->width());
|
|
|
|
float x2 = std::ceil(u * this->width());
|
|
|
|
float y1 = std::floor(v * this->height());
|
|
|
|
float y2 = std::ceil(v * this->height());
|
|
|
|
float x = u * this->width();
|
|
|
|
float y = v * this->height();
|
2022-12-13 00:18:46 +01:00
|
|
|
Color x1y1 = this->getPixelAt(static_cast<int>(x1), static_cast<int>(y1));
|
|
|
|
Color x2y1 = this->getPixelAt(static_cast<int>(x2), static_cast<int>(y1));
|
|
|
|
Color x1y2 = this->getPixelAt(static_cast<int>(x1), static_cast<int>(y2));
|
|
|
|
Color x2y2 = this->getPixelAt(static_cast<int>(x2), static_cast<int>(y2));
|
2022-12-13 00:16:19 +01:00
|
|
|
Color fxy1 = ((x2 - x) / (x2 - x1)) * x1y1 + ((x - x1) / (x2 - x1)) * x2y1;
|
|
|
|
Color fxy2 = ((x2 - x) / (x2 - x1)) * x1y2 + ((x - x1) / (x2 - x1)) * x2y2;
|
|
|
|
color = ((y2 - y) / (y2 - y1)) * fxy1 + ((y - y1) / (y2 - y1)) * fxy2;
|
|
|
|
}
|
|
|
|
return color;
|
2022-10-28 09:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Color Texture::color(Vector2d const &surfacePosition, bool interpolate) const {
|
2022-12-13 00:16:19 +01:00
|
|
|
return color(surfacePosition.u, surfacePosition.v, interpolate);
|
2022-10-28 09:31:13 +02:00
|
|
|
}
|