-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pedro Jorquera
committed
Mar 13, 2024
1 parent
1d283b5
commit 316f4d3
Showing
6 changed files
with
8,089 additions
and
1 deletion.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include "image.h" | ||
|
||
#define STB_IMAGE_IMPLEMENTATION | ||
#define STBI_FAILURE_USERMSG | ||
#include "stb_image.h" | ||
|
||
inline int clamp(int x, int low, int high) { | ||
return (x < low) ? low : ((x < high) ? x : high - 1); | ||
} | ||
|
||
Image::Image(const std::string& filename) { | ||
load(filename); | ||
} | ||
|
||
Image::~Image() { | ||
STBI_FREE(_data); | ||
} | ||
|
||
bool Image::load(const std::string& filename) { | ||
auto n = _bytesPerPixel; | ||
_data = stbi_load(filename.c_str(), &_width, &_height, &n, _bytesPerPixel); | ||
_bytesPerScanline = _width * _bytesPerPixel; | ||
return _data != nullptr; | ||
} | ||
|
||
const unsigned char* Image::pixelData(int x, int y) const { | ||
// Return the address of the three bytes of the pixel at x,y (or magenta if no data). | ||
static unsigned char magenta[] = { 255, 0, 255 }; | ||
if (_data == nullptr) return magenta; | ||
|
||
x = clamp(x, 0, _width); | ||
y = clamp(y, 0, _height); | ||
|
||
return _data + y * _bytesPerScanline + x * _bytesPerPixel; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
class Image { | ||
|
||
private: | ||
|
||
const int _bytesPerPixel = 3; | ||
unsigned char* _data; | ||
int _width; | ||
int _height; | ||
int _bytesPerScanline; | ||
|
||
public: | ||
|
||
Image():_data(nullptr) {} | ||
~Image(); | ||
Image(const std::string& filename); | ||
bool load(const std::string& filename); | ||
|
||
inline auto width() const { return _width; } | ||
inline auto height() const { return _height; } | ||
|
||
const unsigned char* pixelData(int x, int y) const; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.