Commit b04a53c1 authored by kraus's avatar kraus
Browse files

Adding reader for Portable Bitmap files

parent 34b8cafa
......@@ -15,6 +15,7 @@ src/OPALrevision.h
src/opal
tests/tools/gtest
tests/opal_unit_tests
tests/OpalSourcePath.h
doc/user_guide/opal_user_guide.bcf
doc/user_guide/opal_user_guide.idx
doc/user_guide/opal_user_guide.lof
......@@ -24,4 +25,4 @@ doc/user_guide/opal_user_guide.out
doc/user_guide/opal_user_guide.pdf
doc/user_guide/opal_user_guide.run.xml
doc/user_guide/opal_user_guide.synctex.gz
doc/user_guide/opal_user_guide.toc
\ No newline at end of file
doc/user_guide/opal_user_guide.toc
......@@ -2,6 +2,7 @@ set (_SRCS
OpalException.cpp
OpalFilter.cpp
OpalSection.cpp
PortableBitmapReader.cpp
RegularExpression.cpp
Round.cpp
Timer.cpp
......@@ -20,6 +21,7 @@ set (HDRS
OpalException.h
OpalFilter.h
OpalSection.h
PortableBitmapReader.h
RegularExpression.h
Round.h
Timer.h
......
#include "Utilities/PortableBitmapReader.h"
#include <iostream>
#include <fstream>
#include <sstream>
PortableBitmapReader::PortableBitmapReader(const std::string &input) {
std::ifstream in(input);
readHeader(in);
pixels_m.resize(width_m * height_m);
if (type_m == ASCII) {
readImageAscii(in);
} else {
readImageBinary(in);
}
}
std::string PortableBitmapReader::getNextPart(std::istream &in) {
char c;
do {
c = in.get();
if (c == '#') {
do {
c = in.get();
} while (c != '\n');
} else if (!(c == ' ' ||
c == '\t' ||
c == '\n' ||
c == '\r')) {
in.putback(c);
break;
}
} while (true);
std::string nextPart;
in >> nextPart;
return nextPart;
}
void PortableBitmapReader::readHeader(std::istream &in) {
std::string magicValue = getNextPart(in);
if (magicValue == "P1") {
type_m = ASCII;
} else if (magicValue == "P4") {
type_m = BINARY;
} else {
throw OpalException("PortableBitmapReader::readHeader",
"Unknown magic value: '" + magicValue + "'");
}
{
std::string tmp = getNextPart(in);
std::istringstream conv;
conv.str(tmp);
conv >> width_m;
}
{
std::string tmp = getNextPart(in);
std::istringstream conv;
conv.str(tmp);
conv >> height_m;
}
}
void PortableBitmapReader::readImageAscii(std::istream &in) {
unsigned int size = height_m * width_m;
unsigned int i = 0;
while (i < size) {
char c;
in >> c;
if (!(c == ' ' ||
c == '\n' ||
c == '\t' ||
c == '\r')) {
pixels_m[i] = (c == '1');
++ i;
}
}
}
void PortableBitmapReader::readImageBinary(std::istream &in) {
static const unsigned int sizeChar = sizeof(char) * 8;
unsigned int trueSize = (height_m * width_m);
unsigned int size = trueSize / sizeChar;
if (size * sizeChar != trueSize)
++ size;
unsigned int numPixels = 0;
for (unsigned int i = 0; i < size; ++ i) {
char c;
in >> c;
unsigned int numBits = std::min(sizeChar, trueSize - numPixels);
for (unsigned int j = numBits; j > 0 ; -- j, ++ numPixels) {
pixels_m[numPixels] = ((c >> (numBits - 1)) & 1);
}
}
}
\ No newline at end of file
#ifndef PORTABLEBITMAPRREADER_H
#define PORTABLEBITMAPRREADER_H
#include "Utilities/OpalException.h"
#include <string>
#include <vector>
#include <istream>
class PortableBitmapReader {
public:
PortableBitmapReader(const std::string & input);
unsigned int getWidth() const;
unsigned int getHeight() const;
bool isBlack(unsigned int i, unsigned int j) const;
private:
void readHeader(std::istream &in);
void readImageAscii(std::istream &in);
void readImageBinary(std::istream &in);
std::string getNextPart(std::istream &in);
unsigned int getIdx(unsigned int h, unsigned int w) const;
unsigned int width_m;
unsigned int height_m;
enum FileType {
ASCII,
BINARY
};
FileType type_m;
// static unsigned int sizeChar_s;
std::vector<bool> pixels_m;
};
inline
unsigned int PortableBitmapReader::getWidth() const {
return width_m;
}
inline
unsigned int PortableBitmapReader::getHeight() const {
return height_m;
}
inline
bool PortableBitmapReader::isBlack(unsigned int i, unsigned int j) const {
return pixels_m[getIdx(i, j)];
}
inline
unsigned int PortableBitmapReader::getIdx(unsigned int h, unsigned int w) const {
if (h >= height_m || w >= width_m) throw OpalException("PortableBitmapReader::getIdx",
"Pixel number out of bounds");
return h * width_m + w;
}
#endif
\ No newline at end of file
......@@ -53,6 +53,8 @@ include_directories (${GTEST_INCLUDE_DIRS}
${GTEST_INCLUDE_DIRS}
)
configure_file(SourcePath.h.in ${CMAKE_CURRENT_SOURCE_DIR}/OpalSourcePath.h)
# Check to see if cmake finds the test files
# MESSAGE(STATUS "unit test src files: ${TEST_SRCS}")
......@@ -68,4 +70,4 @@ target_link_libraries (
${OTHER_CMAKE_EXE_LINKER_FLAGS}
${GTEST_BOTH_LIBRARIES}
-lpthread
)
)
\ No newline at end of file
#define OPAL_SOURCE_DIR "${CMAKE_SOURCE_DIR}"
\ No newline at end of file
set (_SRCS
PortableBitmapReaderTest.cpp
)
include_directories (
......
#include "gtest/gtest.h"
#include "OpalSourcePath.h"
#include "Utilities/PortableBitmapReader.h"
#include "opal_test_utilities/SilenceTest.h"
#include <iostream>
TEST(PBMReaderTest, SimpleAsciiTest) {
OpalTestUtilities::SilenceTest silencer;
std::string opalSourcePath = OPAL_SOURCE_DIR;
std::string pathToBitmapFile = opalSourcePath + "/tests/opal_src/Utilities/Untitled.pbm";
PortableBitmapReader reader(pathToBitmapFile);
bool pixel = reader.isBlack(111, 300);
ASSERT_TRUE(pixel);
pixel = reader.isBlack(111, 319);
ASSERT_TRUE(pixel);
pixel = reader.isBlack(111, 320);
ASSERT_FALSE(pixel);
pixel = reader.isBlack(199, 300);
ASSERT_TRUE(pixel);
pixel = reader.isBlack(200, 300);
ASSERT_FALSE(pixel);
}
TEST(PBMReaderTest, SimpleBinaryTest) {
OpalTestUtilities::SilenceTest silencer;
std::string opalSourcePath = OPAL_SOURCE_DIR;
std::string pathToBitmapFile = opalSourcePath + "/tests/opal_src/Utilities/Untitled_binary.pbm";
PortableBitmapReader reader(pathToBitmapFile);
bool pixel = reader.isBlack(111, 300);
ASSERT_TRUE(pixel);
pixel = reader.isBlack(111, 319);
ASSERT_TRUE(pixel);
pixel = reader.isBlack(111, 320);
ASSERT_FALSE(pixel);
pixel = reader.isBlack(199, 300);
ASSERT_TRUE(pixel);
pixel = reader.isBlack(200, 300);
ASSERT_FALSE(pixel);
}
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment