Browse Source

[PPM] Comments are being read (almost) correctly now

master
Unbewohnte 3 years ago
parent
commit
8d00820c8c
  1. 21
      README.txt
  2. 44
      src/pnm.cpp
  3. 19
      tests/test.cpp

21
README.txt

@ -1,5 +1,9 @@
RWPNM - a one-file drop-in C++ library for working with PPM, PGM and PBM image formats.
--------------------------------------------------------------------------------------------
How to use:
Either:
1) Copy src/pnm.cpp to your project, #include and you`re done ! No
@ -9,16 +13,27 @@ or
2) Create a dynamic library and use it instead.
Version:
v0.2
--------------------------------------------------------------------------------------------
Current version:
v0.3
Status:
v0.3
Comments are now read correctly; remove all comments at once
v0.2
PPM comments writing/reading support
v0.1.0
v0.1
PPM reading, writing
--------------------------------------------------------------------------------------------
License:
MIT Licese, you are free to do anything, just don`t forget
to include the notice.

44
src/pnm.cpp

@ -15,7 +15,7 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
#define RWPNM
/*
RWPNM v0.2
RWPNM v0.3
A drop-in library to work with PNM images
*/
@ -23,17 +23,9 @@ A drop-in library to work with PNM images
#include <vector>
#include <fstream>
namespace pnm {
namespace pnm {;
class Color {
public:
Color() {}
~Color() {}
bool operator==(const Color& other);
};
class RGB : public Color {
class RGB {
public:
RGB(uint8_t r = 0, uint8_t g = 0, uint8_t b = 0) {
R = r;
@ -48,15 +40,15 @@ public:
bool operator==(const RGB& other) {
if (R != other.R || G != other.G || B != other.B) {
return false;
if (R == other.R || G == other.G || B == other.B) {
return true;
}
return true;
return false;
}
};
class Grayscale : public Color {
class Grayscale {
public:
Grayscale(uint8_t value = 0) {
intensity = value;
@ -64,6 +56,14 @@ public:
~Grayscale() {}
uint8_t intensity;
bool operator==(const Grayscale& other) {
if (intensity == other.intensity) {
return true;
}
return false;
}
};
@ -126,6 +126,14 @@ public:
m_comments.push_back(comment);
}
void remove_last_comment() {
m_comments.pop_back();
}
void remove_all_comments() {
m_comments.clear();
}
// Return all captured comments of an image
std::vector<std::string> get_comments() {
return m_comments;
@ -152,9 +160,11 @@ public:
while(captured < 3) {
ppm_image_file >> entity;
if (entity[0] == m_COMMENT_CHAR) {
// this is a comment, skip this line
// this is a comment
std::string comment = entity;
getline(ppm_image_file, entity);
m_comments.push_back(entity);
comment += entity;
m_comments.push_back(comment);
continue;
}

19
tests/test.cpp

@ -69,7 +69,7 @@ void write_comment() {
image.add_comment("comment 1");
image.add_comment("comment 2");
image.add_comment("comment \n\n\n");
image.add_comment("comment 3\n\n\n");
image.save("result_image.ppm");
@ -79,6 +79,22 @@ void write_comment() {
}
}
void remove_comments() {
try {
pnm::PPM image;
image.read("result_image.ppm");
image.remove_last_comment();
image.remove_all_comments();
image.save("result_image.ppm");
} catch(const std::exception& e) {
std::string error_message("remove_comment: ", e.what());
throw std::runtime_error(error_message);
}
}
void read_comment() {
try {
write_comment();
@ -105,6 +121,7 @@ int main() {
no_pixel_assign();
write_comment();
read_comment();
remove_comments();
} catch(const std::exception& e) {
std::cout << "[ERROR] " << e.what() << "\n";

Loading…
Cancel
Save