Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented ppm reader by following the default methods #1161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build/Templates/ConfigLocal.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
// TIFF codec
#cmakedefine _USE_TIFF

// PPM codec
#cmakedefine _USE_PPM

// OpenGL support
#cmakedefine _USE_OPENGL

Expand Down
2 changes: 2 additions & 0 deletions libs/IO/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ if(TIFF_FOUND)
else()
SET(TIFF_LIBRARIES "")
endif()
# Since we always have the OpenCV library
SET(_USE_PPM TRUE CACHE INTERNAL "")

# List sources files
FILE(GLOB LIBRARY_FILES_C "*.cpp")
Expand Down
6 changes: 6 additions & 0 deletions libs/IO/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
#ifdef _USE_TIFF
#define _IMAGE_TIFF // add TIFF support
#endif
#ifdef _USE_PPM
#define _IMAGE_PPM // add PPM support
#endif

#include "ImageSCI.h"
#ifdef _IMAGE_BMP
Expand All @@ -56,6 +59,9 @@
#ifdef _IMAGE_TIFF
#include "ImageTIFF.h"
#endif
#ifdef _IMAGE_PPM
#include "ImagePPM.h"
#endif
#include "PLY.h"
#include "OBJ.h"
/*----------------------------------------------------------------*/
Expand Down
4 changes: 4 additions & 0 deletions libs/IO/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,10 @@ CImage* CImage::Create(LPCTSTR szName, IMCREATE mode)
else if (_tcsncicmp(fext, _T(".tif"), 4) == 0 || _tcsncicmp(fext, _T(".tiff"), 5) == 0)
pImage = new CImageTIFF();
#endif
#ifdef _IMAGE_PPM
else if (_tcsncicmp(fext, _T(".ppm"), 4) == 0)
pImage = new CImagePPM();
#endif
else
goto UNKNOWN_FORMAT;

Expand Down
133 changes: 133 additions & 0 deletions libs/IO/ImagePPM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
////////////////////////////////////////////////////////////////////
// ImageJPG.cpp
//
// Copyright 2007 cDc@seacave
// Distributed under the Boost Software License, Version 1.0
// (See http://www.boost.org/LICENSE_1_0.txt)

#include "Common.h"

#ifdef _IMAGE_PPM
#include "ImagePPM.h"

#ifdef _MSC_VER
#define XMD_H // prevent redefinition of INT32
#undef FAR // prevent FAR redefinition
#endif

using namespace SEACAVE;


// D E F I N E S ///////////////////////////////////////////////////

// F U N C T I O N S ///////////////////////////////////////////////

// S T R U C T S ///////////////////////////////////////////////////

CImagePPM::CImagePPM()
{

} // Constructor

CImagePPM::~CImagePPM()
{
if (!m_data)
delete m_data;
} // Destructor
/*----------------------------------------------------------------*/

void CImagePPM::Close()
{

}
/*----------------------------------------------------------------*/

HRESULT CImagePPM::ReadHeader()
{
const size_t nSize = ((ISTREAM*)m_pStream)->getSize();

((ISTREAM*)m_pStream)->setPos(0);
CAutoPtrArr<uint8_t> pData(new uint8_t[nSize]);
m_pStream->read(pData, nSize);

std::ifstream file((char *) &pData, nSize);

// Read the magic number (P6).
char magic_number[3];
file.read(magic_number, 2);
if (std::string(magic_number) != "P6") {
std::cerr << "Error: Invalid image format." << std::endl;
return _FAIL;
}

// Read the whitespace character.
char whitespace;
file.get(whitespace);
if (whitespace != ' ') {
std::cerr << "Error: Invalid image format." << std::endl;
return _FAIL;
}

// Read the width.
int width;
file >> width;

// Read the whitespace character.
file.get(whitespace);
if (whitespace != ' ') {
std::cerr << "Error: Invalid image format." << std::endl;
return _FAIL;
}

// Read the height.
int height;
file >> height;

// Read the whitespace character.
file.get(whitespace);
if (whitespace != ' ') {
std::cerr << "Error: Invalid image format." << std::endl;
return _FAIL;
}

// Read the maximum value.
int max_value;
file >> max_value;

m_dataWidth = m_width = width;
m_dataHeight= m_height = height;
m_numLevels = 0;
m_level = 0;
m_stride = 3;
m_format = PF_R8G8B8;
m_lineWidth = m_dataWidth * m_stride;

size_t offset = nSize - (m_dataHeight * m_lineWidth);
memcpy(m_data, pData + offset, m_dataHeight * m_lineWidth);

return _OK;
} // ReadHeader
/*----------------------------------------------------------------*/

HRESULT CImagePPM::ReadData(void* pData, PIXELFORMAT dataFormat, Size nStride, Size lineWidth)
{
memcpy(pData, m_data, m_height * lineWidth);
return _OK;
} // Read
/*----------------------------------------------------------------*/

HRESULT CImagePPM::WriteHeader(PIXELFORMAT imageFormat, Size width, Size height, BYTE numLevels)
{
//TODO: to implement the PPM encoder
return _FAIL;
} // WriteHeader
/*----------------------------------------------------------------*/

HRESULT CImagePPM::WriteData(void* pData, PIXELFORMAT dataFormat, Size nStride, Size lineWidth)
{
//TODO: to implement the PPM encoder
return _FAIL;
} // WriteData
/*----------------------------------------------------------------*/

#endif // _IMAGE_PPM
44 changes: 44 additions & 0 deletions libs/IO/ImagePPM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
////////////////////////////////////////////////////////////////////
// ImagePPM.h
//
// Copyright 2007 cDc@seacave
// Distributed under the Boost Software License, Version 1.0
// (See http://www.boost.org/LICENSE_1_0.txt)

#ifndef __SEACAVE_IMAGEPPM_H__
#define __SEACAVE_IMAGEPPM_H__


// D E F I N E S ///////////////////////////////////////////////////


// I N C L U D E S /////////////////////////////////////////////////

#include "Image.h"

namespace SEACAVE {

// S T R U C T S ///////////////////////////////////////////////////

class IO_API CImagePPM : public CImage
{
public:
CImagePPM();
virtual ~CImagePPM();

void Close();

HRESULT ReadHeader();
HRESULT ReadData(void*, PIXELFORMAT, Size nStride, Size lineWidth);
HRESULT WriteHeader(PIXELFORMAT, Size width, Size height, BYTE numLevels);
HRESULT WriteData(void*, PIXELFORMAT, Size nStride, Size lineWidth);

protected:
// cv::Mat m_cvimg;
BYTE* m_data = NULL;
}; // class CImagePPM
/*----------------------------------------------------------------*/

} // namespace SEACAVE

#endif // __SEACAVE_IMAGEPPM_H__