COSC-3P93-Project/Step 3/include/engine/image/image.h

115 lines
3.7 KiB
C
Raw Normal View History

2022-10-20 11:30:15 -04:00
/*
* Created by Brett Terpstra 6920201 on 14/10/22.
* Copyright (c) Brett Terpstra 2022 All Rights Reserved
*/
#ifndef STEP_2_IMAGE_H
#define STEP_2_IMAGE_H
#include "engine/util/std.h"
#include "engine/math/vectors.h"
2022-10-20 11:30:15 -04:00
namespace Raytracing {
2022-12-13 01:33:31 -05:00
2022-10-20 11:30:15 -04:00
class Image {
private:
2022-10-25 01:06:26 -04:00
unsigned long width;
unsigned long height;
unsigned long _width;
unsigned long _height;
2022-12-13 01:33:31 -05:00
Vec4* pixelData;
2022-12-02 23:32:18 -05:00
bool m_modified = false;
2022-10-20 11:30:15 -04:00
public:
2022-10-25 01:06:26 -04:00
Image(unsigned long width, unsigned long height);
2022-12-13 01:33:31 -05:00
Image(const Image& image);
2022-10-20 11:30:15 -04:00
Image(const Image&& image) = delete;
2022-12-13 01:33:31 -05:00
/**
* Converts the image to a array of doubles, the pixels are packed vectors of order (x,y,z,w)
*/
2022-12-02 23:32:18 -05:00
std::vector<double> toArray();
2022-12-13 01:33:31 -05:00
/**
* Loads the pixel data from the double array, ignoring values which where not modified.
* It would make more sense to send only modified data however it is much easier not to.
* @param array array to load from
* @param size size of the array
* @param id unused
*/
2022-12-02 23:32:18 -05:00
void fromArray(double* array, int size, int id);
2022-12-13 01:33:31 -05:00
inline void setPixelColor(unsigned long x, unsigned long y, const Vec4& color) {
2022-12-02 23:32:18 -05:00
m_modified = true;
pixelData[(x * height) + y] = Vec4{color.r(), color.g(), color.b(), 1.0};
2022-10-20 11:30:15 -04:00
}
2022-12-13 01:33:31 -05:00
2022-10-25 01:06:26 -04:00
[[nodiscard]] inline Vec4 getPixelColor(unsigned long x, unsigned long y) const {
2022-10-20 11:30:15 -04:00
return pixelData[(x * height) + y];
}
2022-12-13 01:33:31 -05:00
[[nodiscard]] inline int getPixelR(int x, int y) const {
return int(255.0 * getPixelColor(x, y).r());
};
2022-12-13 01:33:31 -05:00
[[nodiscard]] inline int getPixelG(int x, int y) const {
return int(255.0 * getPixelColor(x, y).g());
};
2022-12-13 01:33:31 -05:00
[[nodiscard]] inline int getPixelB(int x, int y) const {
return int(255.0 * getPixelColor(x, y).b());
}
2022-12-13 01:33:31 -05:00
[[nodiscard]] inline int getPixelA(int x, int y) const {
return int(255.0 * getPixelColor(x, y).a());
}
2022-12-13 01:33:31 -05:00
2022-10-25 01:06:26 -04:00
[[nodiscard]] inline int getWidth() const { return int(width); }
2022-12-13 01:33:31 -05:00
2022-10-25 01:06:26 -04:00
[[nodiscard]] inline int getHeight() const { return int(height); }
2022-12-13 01:33:31 -05:00
2022-12-02 23:32:18 -05:00
[[nodiscard]] inline bool modified() const { return m_modified; }
2022-12-13 01:33:31 -05:00
2022-10-20 11:30:15 -04:00
~Image();
};
2022-12-13 01:33:31 -05:00
class ImageInput {
private:
int width, height, channels;
unsigned char* data;
public:
explicit ImageInput(const std::string& image);
2022-12-13 01:33:31 -05:00
/**
* Loads the image as a buffer for GUI icons
* @return pointer that must be manually freed.
*/
unsigned long* getImageAsIconBuffer();
2022-12-13 01:33:31 -05:00
unsigned char* getImageData() { return data; }
[[nodiscard]] int getImageSize() const { return width; }
~ImageInput();
};
2022-10-20 11:30:15 -04:00
// image writer class used to output the image to a file.
class ImageOutput {
private:
2022-12-13 01:33:31 -05:00
const Image& image;
2022-10-20 11:30:15 -04:00
public:
explicit ImageOutput(const Image& image): image(image) {}
2022-12-13 01:33:31 -05:00
/**
* Writes the image stored in this class
* @param file file to write to
* @param formatExtension .png / .jpg etc
*/
virtual void write(const std::string& file, const std::string& formatExtension);
2022-10-20 11:30:15 -04:00
};
2022-12-13 01:33:31 -05:00
2022-10-20 11:30:15 -04:00
}
#endif //STEP_2_IMAGE_H