Skip to content

Commit

Permalink
Added support for write PNG images
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Jorquera committed Mar 22, 2024
1 parent 400f448 commit 0dadcb9
Show file tree
Hide file tree
Showing 5 changed files with 1,738 additions and 16 deletions.
16 changes: 2 additions & 14 deletions src/framebuffer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "framebuffer.h"
#include "interval.h"

#include <fstream>
#include "image.h"

using namespace std;

Expand All @@ -22,16 +21,5 @@ void FrameBuffer::draw(int x, int y, Color color) {
}

void FrameBuffer::save(const std::string& filename) const {
ofstream outputFile(filename);
outputFile << "P3\n" << _width << ' ' << _height << "\n255\n";
for (int y = 0; y < _height; ++y) {
for (int x = 0; x < _width; ++x) {
auto pixelOffset = 3 * (x + y * _width);
outputFile << int(_buffer[pixelOffset + 0]) << ' '
<< int(_buffer[pixelOffset + 1]) << ' '
<< int(_buffer[pixelOffset + 2])
<< '\n';
}
}
outputFile.close();
Image::save(_width, _height, _buffer, filename);
}
7 changes: 7 additions & 0 deletions src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#define STBI_FAILURE_USERMSG
#include "stb_image.h"

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#include <iostream>

using namespace std;
Expand Down Expand Up @@ -35,6 +38,10 @@ bool Image::load(const string& filename) {
return _data != nullptr;
}

bool Image::save(int width, int height, const unsigned char* data, const std::string& filename) {
return stbi_write_png(filename.c_str(), width, height, 3, data, width * 3);
}

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 };
Expand Down
3 changes: 3 additions & 0 deletions src/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ class Image {
public:

Image():_data(nullptr) {}
Image(int width, int height, unsigned char* data):
_width(width),_height(height),_data(data),_bytesPerScanline(_width * _bytesPerPixel) {}
~Image();
Image(const std::string& filename);
bool load(const std::string& filename);
static bool save(int width, int height, const unsigned char* data, const std::string& filename);

inline auto width() const { return _width; }
inline auto height() const { return _height; }
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ void twoPerlinSpheres() {
scene->add(make_shared<Sphere>(Point(0.0, -1000.0, 0.0), 1000, make_shared<Lambertian>(checkerTexture)));
scene->add(make_shared<Sphere>(Point(0.0, 1.2, 0.0), 1.2, make_shared<Lambertian>(perlinTexture)));

Camera camera(16.0 / 9.0, 500, 50, 1920, 20.0, Vector(10.0, 2.0, 3.0), Vector(0.0, 1.0, 0.0), Vector(0.0, 1.0, 0.0));
Camera camera(16.0 / 9.0, 20, 50, 400, 20.0, Vector(10.0, 2.0, 3.0), Vector(0.0, 1.0, 0.0), Vector(0.0, 1.0, 0.0));

camera.render(scene, "image.ppm");
camera.render(scene, "image.png");
}

int main() {
Expand Down
Loading

0 comments on commit 0dadcb9

Please sign in to comment.