From eb323f4b83d280d6c966715432d78d6ab2db98ab Mon Sep 17 00:00:00 2001 From: kunchuntsai Date: Wed, 24 Jul 2024 01:48:33 +0900 Subject: [PATCH] Implemented ppm reader by following the default methods --- build/Templates/ConfigLocal.h.in | 3 + libs/IO/CMakeLists.txt | 2 + libs/IO/Common.h | 6 ++ libs/IO/Image.cpp | 4 + libs/IO/ImagePPM.cpp | 133 +++++++++++++++++++++++++++++++ libs/IO/ImagePPM.h | 44 ++++++++++ 6 files changed, 192 insertions(+) create mode 100644 libs/IO/ImagePPM.cpp create mode 100644 libs/IO/ImagePPM.h diff --git a/build/Templates/ConfigLocal.h.in b/build/Templates/ConfigLocal.h.in index 39ac2925a..af4709298 100644 --- a/build/Templates/ConfigLocal.h.in +++ b/build/Templates/ConfigLocal.h.in @@ -42,6 +42,9 @@ // TIFF codec #cmakedefine _USE_TIFF +// PPM codec +#cmakedefine _USE_PPM + // OpenGL support #cmakedefine _USE_OPENGL diff --git a/libs/IO/CMakeLists.txt b/libs/IO/CMakeLists.txt index 0f0595194..7ae0b8bc6 100644 --- a/libs/IO/CMakeLists.txt +++ b/libs/IO/CMakeLists.txt @@ -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") diff --git a/libs/IO/Common.h b/libs/IO/Common.h index e43aaac44..3f2a62477 100644 --- a/libs/IO/Common.h +++ b/libs/IO/Common.h @@ -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 @@ -56,6 +59,9 @@ #ifdef _IMAGE_TIFF #include "ImageTIFF.h" #endif +#ifdef _IMAGE_PPM +#include "ImagePPM.h" +#endif #include "PLY.h" #include "OBJ.h" /*----------------------------------------------------------------*/ diff --git a/libs/IO/Image.cpp b/libs/IO/Image.cpp index 3d3e1cecb..02353fe33 100644 --- a/libs/IO/Image.cpp +++ b/libs/IO/Image.cpp @@ -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; diff --git a/libs/IO/ImagePPM.cpp b/libs/IO/ImagePPM.cpp new file mode 100644 index 000000000..898c01026 --- /dev/null +++ b/libs/IO/ImagePPM.cpp @@ -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 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 diff --git a/libs/IO/ImagePPM.h b/libs/IO/ImagePPM.h new file mode 100644 index 000000000..b9ea3049f --- /dev/null +++ b/libs/IO/ImagePPM.h @@ -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__