Skip to content

Commit

Permalink
Merge pull request jcbritobr#2 from jcbritobr/massa-patch-1-ppmimage-…
Browse files Browse the repository at this point in the history
…reader-writer

Massa patch 1 ppmimage reader writer
  • Loading branch information
jgardona authored May 23, 2022
2 parents adb84e6 + 16e3ac2 commit ea98ba2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
61 changes: 37 additions & 24 deletions ppmimage.cpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,53 @@
#include <iostream>
#include "ppmimage.hpp"

int PPMImage::getHeigth() const
{
return heigth;
}

int PPMImage::getWidth() const
{
return width;
}

std::vector<Color> PPMImage::getData() const
{
return data;
}
//TODO:: write tests -- this code compiles :-)

PPMImage::PPMImage(int width, int heigth):width(width), heigth(heigth)
{
for (int i = 0; i < width * heigth; ++i) {
data.push_back(Color(0.0f, 0.0f, 0.0f));
}
}
constexpr auto P3MARKER = "P3";
constexpr auto PPMNL = '\n';
constexpr auto PPMSPACE = ' ';
constexpr auto PPMMAGIC = 255;

std::ostream &operator <<(std::ostream &stream, const PPMImage &image)
{
stream << "P3\n" << image.getWidth() << " " << image.getHeigth() << "\n255\n";
stream << P3MARKER << PPMNL << image.width << PPMSPACE << image.height << PPMNL << PPMMAGIC << PPMNL;
if( !stream ) return stream;

for (auto color: image.getData()) {
stream << color.r() << " " << color.g() << " " << color.b() << std::endl;
for ( const auto& color: image.data ) {
stream << color.r() << PPMSPACE << color.g() << PPMSPACE << color.b() << PPMNL;
if( !stream ) return stream;
}

return stream;
}

std::istream &operator >>(std::istream &stream, PPMImage &image)
{
if( !stream ) return stream;
std::string p3;
stream >> p3;
if( !stream ) return stream;
if( p3 != P3MARKER ) {
stream.setstate(std::ios_base::failbit);
return stream;
}
size_t width, height, magic;
stream >> width >> height >> magic;
if( !stream ) return stream;
if( magic != PPMMAGIC ) {
stream.setstate(std::ios_base::failbit);
return stream;
}
std::vector<Color> data;
data.reserve(width*height); // avoid reallocations and copies

for( auto count = width*height; count --> 0; ) {
float r, g, b;
stream >> r >> g >> b;
if( !stream ) return stream;
data.emplace_back(r, g, b);
}

image = PPMImage { width, height, data };

return stream;
}
21 changes: 11 additions & 10 deletions ppmimage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
#include <vector>
#include "color.hpp"

class PPMImage
{
private:
// PPMImage is semi-immutable: one can manipulate in-place the individual pixels but you can't
// resize the image. The array of pixels will be moved whenever possible.

struct PPMImage {
const size_t width;
const size_t height;
std::vector<Color> data;
int width;
int heigth;
public:
PPMImage(int width, int heigth);
int getHeigth() const;
int getWidth() const;
std::vector<Color> getData() const;
PPMImage(size_t width_, size_t height_) : width {width_}, height {height_}, data {width*height, Color{.0f, .0f, .0f}} {}
PPMImage(size_t width_, size_t height_, std::vector<Color> const& data_) : width {width_}, height {height_}, data {data_} {}
PPMImage(size_t width_, size_t height_, std::vector<Color> && data_) : width {width_}, height {height_}, data {std::move(data_)} {}
PPMImage& operator=(PPMImage const& image) { new(this) PPMImage { image.width, image.height, image.data }; return *this; }
PPMImage& operator=(PPMImage && image) { new(this) PPMImage { image.width, image.height, std::move(image.data) }; return *this; }
};

std::ostream& operator <<(std::ostream &stream, PPMImage const &image);
Expand Down

0 comments on commit ea98ba2

Please sign in to comment.