forked from jcbritobr/sray
-
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.
Merge pull request jcbritobr#2 from jcbritobr/massa-patch-1-ppmimage-…
…reader-writer Massa patch 1 ppmimage reader writer
- Loading branch information
Showing
2 changed files
with
48 additions
and
34 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -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; | ||
} |
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