From 3a0b25923a9800947a0ecb61246043caca95e191 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 26 Jul 2016 22:56:27 +0200 Subject: [PATCH] [DEBUG] correct save on bmp file (better select output format) --- egami/Image.cpp | 2 +- egami/Image.h | 2 +- egami/egami.cpp | 6 ++-- egami/wrapperBMP.cpp | 75 +++++++++++++++++++++++++++----------------- 4 files changed, 51 insertions(+), 34 deletions(-) diff --git a/egami/Image.cpp b/egami/Image.cpp index 83887ea..da2c50a 100644 --- a/egami/Image.cpp +++ b/egami/Image.cpp @@ -89,7 +89,7 @@ void egami::Image::configure(const ivec2& _size, enum colorType _type) { } } -enum egami::colorType egami::Image::getType() { +enum egami::colorType egami::Image::getType() const { if (m_data == nullptr) { EGAMI_DEBUG("No internal data for image (nullptr)"); return egami::colorType::undefined; diff --git a/egami/Image.h b/egami/Image.h index 0a7ba98..383081c 100644 --- a/egami/Image.h +++ b/egami/Image.h @@ -81,7 +81,7 @@ namespace egami { void configure(const ivec2& _size=ivec2(32,32), enum colorType _type=egami::colorType::RGBA8); void* getTextureDataPointer(); - enum colorType getType(); + enum colorType getType() const; bool exist() { return m_data != nullptr; } diff --git a/egami/egami.cpp b/egami/egami.cpp index 6b5256d..d97b468 100644 --- a/egami/egami.cpp +++ b/egami/egami.cpp @@ -58,12 +58,12 @@ bool egami::store(const egami::Image& _input, const std::string& _fileName) { // select the corect Loader : if (etk::end_with(tmpName, ".edf") == true) { if (egami::storeEDF(_fileName, _input) == false) { - EGAMI_ERROR("Error to load EDF file '" << _fileName << "'"); + EGAMI_ERROR("Error to store EDF file '" << _fileName << "'"); return false; } } else if (etk::end_with(tmpName, ".bmp") == true) { if (egami::storeBMP(_fileName, _input) == false) { - EGAMI_ERROR("Error to load BMP file '" << _fileName << "'"); + EGAMI_ERROR("Error to store BMP file '" << _fileName << "'"); return false; } } else if (etk::end_with(tmpName, ".svg") == true) { @@ -73,7 +73,7 @@ bool egami::store(const egami::Image& _input, const std::string& _fileName) { EGAMI_ERROR("Can not store in PNG file '" << _fileName << "'"); return false; } else { - EGAMI_ERROR("Extention not managed '" << _fileName << "' Sopported extention : .bmp / .svg / .png"); + EGAMI_ERROR("Extention not managed '" << _fileName << "' Sopported extention: .edf / .bmp / .svg / .png"); return false; } return true; diff --git a/egami/wrapperBMP.cpp b/egami/wrapperBMP.cpp index d2d8cd6..bb3c7a4 100644 --- a/egami/wrapperBMP.cpp +++ b/egami/wrapperBMP.cpp @@ -89,38 +89,35 @@ egami::Image egami::loadBMP(const std::string& _inputFile) { fileName.fileClose(); return out; } + m_width = m_InfoHeader.biWidth; + m_height = m_InfoHeader.biHeight; if( m_InfoHeader.biBitCount == 16 - && m_InfoHeader.biCompression == 0) - { + && m_InfoHeader.biCompression == 0) { m_dataMode = BITS_16_X1R5G5B5; + out.configure(ivec2(m_width,m_height), egami::colorType::RGB8); } else if( m_InfoHeader.biBitCount == 16 - && m_InfoHeader.biCompression == 3) - { + && m_InfoHeader.biCompression == 3) { m_dataMode = BITS_16_R5G6B5; + out.configure(ivec2(m_width,m_height), egami::colorType::RGB8); } else if( m_InfoHeader.biBitCount == 24 - && m_InfoHeader.biCompression == 0) - { + && m_InfoHeader.biCompression == 0) { m_dataMode = BITS_24_R8G8B8; + out.configure(ivec2(m_width,m_height), egami::colorType::RGB8); } else if( m_InfoHeader.biBitCount == 32 - && m_InfoHeader.biCompression == 3) - { + && m_InfoHeader.biCompression == 3) { m_dataMode = BITS_32_X8R8G8B8; + out.configure(ivec2(m_width,m_height), egami::colorType::RGB8); } else if( m_InfoHeader.biBitCount == 32 - && m_InfoHeader.biCompression == 0) - { + && m_InfoHeader.biCompression == 0) { m_dataMode = BITS_32_A8R8G8B8; + out.configure(ivec2(m_width,m_height), egami::colorType::RGBA8); } else { EGAMI_ERROR("the biBitCount & biCompression fealds are unknow == > not supported format ..."); fileName.fileClose();; return out; } - m_width = m_InfoHeader.biWidth; - m_height = m_InfoHeader.biHeight; - // reallocate the image - out.configure(ivec2(m_width,m_height), egami::colorType::RGBA8); - std::vector m_data; - if(0 != m_InfoHeader.biSizeImage) { + if(m_InfoHeader.biSizeImage != 0) { m_data.resize(m_InfoHeader.biSizeImage, 0); if (fileName.fileRead(&m_data[0],m_InfoHeader.biSizeImage,1) != 1){ EGAMI_CRITICAL("Can not read the file with the good size..."); @@ -220,9 +217,15 @@ bool egami::storeBMP(const std::string& _fileName, const egami::Image& _inputIma m_InfoHeader.biWidth = _inputImage.getSize().x(); m_InfoHeader.biHeight = _inputImage.getSize().y(); m_InfoHeader.biPlanes = 1; - m_InfoHeader.biBitCount = 32; - m_InfoHeader.biCompression = 0; - m_InfoHeader.biSizeImage = _inputImage.getSize().x()*_inputImage.getSize().y()*4; + if (_inputImage.getType() == egami::colorType::RGBA8) { + m_InfoHeader.biBitCount = 32; + m_InfoHeader.biCompression = 0; + m_InfoHeader.biSizeImage = _inputImage.getSize().x()*_inputImage.getSize().y()*4; + } else { + m_InfoHeader.biBitCount = 24; + m_InfoHeader.biCompression = 0; + m_InfoHeader.biSizeImage = _inputImage.getSize().x()*_inputImage.getSize().y()*3; + } m_InfoHeader.biXPelsPerMeter = 75; m_InfoHeader.biYPelsPerMeter = 75; m_InfoHeader.biClrUsed = 0; @@ -251,16 +254,30 @@ bool egami::storeBMP(const std::string& _fileName, const egami::Image& _inputIma return false; } */ - uint8_t data[16]; - for(int32_t yyy=0; yyy<_inputImage.getSize().y(); ++yyy) { - for(int32_t xxx=0; xxx<_inputImage.getSize().x(); ++xxx) { - const etk::Color<>& tmpColor = _inputImage.get(ivec2(xxx,yyy)); - uint8_t* pointer = data; - *pointer++ = tmpColor.r(); - *pointer++ = tmpColor.g(); - *pointer++ = tmpColor.b(); - *pointer++ = tmpColor.a(); - fileName.fileWrite(data,4,1); + if (_inputImage.getType() == egami::colorType::RGBA8) { + uint8_t data[16]; + for(int32_t yyy=0; yyy<_inputImage.getSize().y(); ++yyy) { + for(int32_t xxx=0; xxx<_inputImage.getSize().x(); ++xxx) { + const etk::Color<>& tmpColor = _inputImage.get(ivec2(xxx,yyy)); + uint8_t* pointer = data; + *pointer++ = tmpColor.r(); + *pointer++ = tmpColor.g(); + *pointer++ = tmpColor.b(); + *pointer++ = tmpColor.a(); + fileName.fileWrite(data,4,1); + } + } + } else { + uint8_t data[16]; + for(int32_t yyy=0; yyy<_inputImage.getSize().y(); ++yyy) { + for(int32_t xxx=0; xxx<_inputImage.getSize().x(); ++xxx) { + const etk::Color<>& tmpColor = _inputImage.get(ivec2(xxx,yyy)); + uint8_t* pointer = data; + *pointer++ = tmpColor.r(); + *pointer++ = tmpColor.g(); + *pointer++ = tmpColor.b(); + fileName.fileWrite(data,3,1); + } } } fileName.fileClose();