Skip to content

Commit

Permalink
Use Ogre to save images
Browse files Browse the repository at this point in the history
Instead of using libFreeImage directly, use Ogre. Changes exportMap
behavior.
Image is flipped compared to before and exportMap doesn't return false
if image saving fails due to permissions.
  • Loading branch information
latami committed Feb 12, 2017
1 parent 1600940 commit 126eb26
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 37 deletions.
11 changes: 1 addition & 10 deletions Linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,6 @@ include_directories(${Boost_INCLUDE_DIR})
add_definitions(-DBOOST_ALL_NO_LIB)
set(OGRE_LIBRARIES ${OGRE_LIBRARIES} ${Boost_LIBRARIES})

find_path(LIBFREEIMAGE FreeImage.h)

if(${LIBFREEIMAGE} STREQUAL "LIBFREEIMAGE-NOTFOUND")
message(FATAL_ERROR "Libfreeimage-header not found. ")
else()
message(STATUS "Libfreeimage-header found from " ${LIBFREEIMAGE})
set(FreeImageLib freeimage)
endif(${LIBFREEIMAGE} STREQUAL "LIBFREEIMAGE-NOTFOUND")

set(HDRS
../simplexnoise1234.h
../PSphere.h
Expand Down Expand Up @@ -142,7 +133,7 @@ add_custom_command(TARGET PlanetGenerator POST_BUILD COMMAND ${CMAKE_COMMAND} -E
# Add "_d" to debug-binary
set_target_properties(PlanetGenerator PROPERTIES DEBUG_POSTFIX _d)

target_link_libraries(PlanetGenerator ${OGRE_LIBRARIES} ${OIS_LIBRARIES} ${OGRE_Overlay_LIBRARIES} ${Qt5Libs} ${FreeImageLib})
target_link_libraries(PlanetGenerator ${OGRE_LIBRARIES} ${OIS_LIBRARIES} ${OGRE_Overlay_LIBRARIES} ${Qt5Libs})

set(CMAKE_CXX_FLAGS "-Wall")

Expand Down
3 changes: 0 additions & 3 deletions Linux/README
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ cmake >= 2.8.11 (tested: 3.0.2, 3.2.1)
Ogre == 1.9 (tested: 1.9.0)
ois == 1.3 (tested: 1.3.0)
Qt >= 5 (tested: 5.4.1)
FreeImage >= 3? (tested: 3.10.0, 3.15.4)

Tested on:
Ubuntu 15.04 (x86_64)
Expand All @@ -23,7 +22,6 @@ cmake
libogre-1.9-dev
libois-dev
qtbase5-dev
libfreeimage-dev
########################

#### Fedora ############
Expand All @@ -37,7 +35,6 @@ cmake
ogre-devel
ois-devel
qt5-qtbase-devel
freeimage-devel
########################

#### Gentoo ############
Expand Down
48 changes: 24 additions & 24 deletions PSphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@
#include <OgreMeshSerializer.h>
#include <OgreDataStream.h>
#include <OgreException.h>
#include <OgreImageCodec.h>
#include "OgreConfigFile.h"
#include "Common.h"
#include "ResourceParameter.h"

#define FREEIMAGE_LIB
#include "FreeImage.h"
#include <assert.h>

using namespace std;
Expand Down Expand Up @@ -910,44 +909,45 @@ unsigned char *PSphere::exportMap(unsigned short width, unsigned short height, M
}

bool PSphere::exportMap(unsigned short width, unsigned short height, std::string fileName, MapType type) {
RGBQUAD color;
unsigned char *exportImage;

/* Create map to memory location pointed by exportImage. */
exportImage = exportMap(width, height, type);

if (exportImage == NULL)
{
std::cerr << "Map not created!" << std::endl;
std::cerr << "Map not created!" << std::endl;
return false;
}

// Ignore given height with Cubemap
if (type == MAP_CUBE)
height = width/4*3;

// Use freeimage to save the map as a file
FreeImage_Initialise();
FIBITMAP *bitmap = FreeImage_Allocate(width, height, 24);
for(int i=0; i < width; i++) {
for (int j=0; j < height; j++) {
color.rgbRed = exportImage[((width*j)+i)*3];
color.rgbGreen = exportImage[((width*j)+i)*3+1];
color.rgbBlue = exportImage[((width*j)+i)*3+2];
FreeImage_SetPixelColor(bitmap, i, j, &color);
}
}
if ( !FreeImage_Save(FIF_PNG, bitmap, fileName.c_str(), 0) )
{
std::cerr << "Saving image " << fileName << " failed!" << std::endl;
Ogre::ImageCodec::ImageData *imgData = new Ogre::ImageCodec::ImageData();

FreeImage_Unload(bitmap);
FreeImage_DeInitialise();
imgData->width = width;
imgData->height = height;
imgData->depth = 1;
imgData->format = Ogre::PF_BYTE_RGB;

return false;
}
FreeImage_Unload(bitmap);
FreeImage_DeInitialise();
Ogre::MemoryDataStreamPtr imgStreamPtr;
imgStreamPtr = Ogre::MemoryDataStreamPtr(new Ogre::MemoryDataStream(exportImage, width*height*3));
Ogre::Codec::CodecDataPtr codec(imgData);

/* Check if there already is a running root */
if (Ogre::Root::getSingletonPtr() == NULL)
{
Ogre::Root root(""); // Needs a root to use encodeToFile()

Ogre::Codec::getCodec("png")->encodeToFile(imgStreamPtr,
fileName,
codec);
}
else
Ogre::Codec::getCodec("png")->encodeToFile(imgStreamPtr,
fileName,
codec);

delete[] exportImage;

Expand Down

0 comments on commit 126eb26

Please sign in to comment.