Removed dependency to libjpeg, stb_image_write now supports writing JPEG files
This commit is contained in:
parent
44944989e8
commit
2aa70def6f
26 changed files with 8 additions and 1986 deletions
|
@ -91,17 +91,13 @@ include_directories("${PROJECT_SOURCE_DIR}/extlibs/headers/stb_image")
|
|||
|
||||
# let CMake know about our additional graphics libraries paths
|
||||
if(SFML_OS_WINDOWS)
|
||||
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${PROJECT_SOURCE_DIR}/extlibs/headers/jpeg")
|
||||
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${PROJECT_SOURCE_DIR}/extlibs/headers/freetype2")
|
||||
elseif(SFML_OS_MACOSX)
|
||||
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${PROJECT_SOURCE_DIR}/extlibs/headers/jpeg")
|
||||
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${PROJECT_SOURCE_DIR}/extlibs/headers/freetype2")
|
||||
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "${PROJECT_SOURCE_DIR}/extlibs/libs-osx/Frameworks")
|
||||
elseif(SFML_OS_IOS)
|
||||
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${PROJECT_SOURCE_DIR}/extlibs/headers/jpeg")
|
||||
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${PROJECT_SOURCE_DIR}/extlibs/headers/freetype2")
|
||||
elseif(SFML_OS_ANDROID)
|
||||
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${PROJECT_SOURCE_DIR}/extlibs/headers/jpeg")
|
||||
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${PROJECT_SOURCE_DIR}/extlibs/headers/freetype2")
|
||||
endif()
|
||||
|
||||
|
@ -111,7 +107,7 @@ if(NOT SFML_OPENGL_ES)
|
|||
if(SFML_OS_LINUX)
|
||||
find_package(X11 REQUIRED)
|
||||
endif()
|
||||
include_directories(${FREETYPE_INCLUDE_DIRS} ${JPEG_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR})
|
||||
include_directories(${FREETYPE_INCLUDE_DIRS} ${OPENGL_INCLUDE_DIR})
|
||||
endif()
|
||||
if(SFML_OPENGL_ES AND SFML_OS_LINUX)
|
||||
find_package(EGL REQUIRED)
|
||||
|
@ -119,13 +115,11 @@ if(SFML_OPENGL_ES AND SFML_OS_LINUX)
|
|||
include_directories(${EGL_INCLUDE_DIR} ${GLES_INCLUDE_DIR})
|
||||
endif()
|
||||
if(SFML_OS_ANDROID)
|
||||
find_host_package(JPEG REQUIRED)
|
||||
find_host_package(Freetype REQUIRED)
|
||||
else()
|
||||
find_package(JPEG REQUIRED)
|
||||
find_package(Freetype REQUIRED)
|
||||
endif()
|
||||
include_directories(${FREETYPE_INCLUDE_DIRS} ${JPEG_INCLUDE_DIR})
|
||||
include_directories(${FREETYPE_INCLUDE_DIRS})
|
||||
|
||||
# build the list of external libraries to link
|
||||
if(NOT SFML_OPENGL_ES)
|
||||
|
@ -142,7 +136,7 @@ if(SFML_OS_IOS)
|
|||
elseif(SFML_OS_ANDROID)
|
||||
list(APPEND GRAPHICS_EXT_LIBS z)
|
||||
endif()
|
||||
list(APPEND GRAPHICS_EXT_LIBS ${FREETYPE_LIBRARY} ${JPEG_LIBRARY})
|
||||
list(APPEND GRAPHICS_EXT_LIBS ${FREETYPE_LIBRARY})
|
||||
|
||||
# add preprocessor symbols
|
||||
add_definitions(-DSTBI_FAILURE_USERMSG)
|
||||
|
|
|
@ -32,11 +32,6 @@
|
|||
#include <stb_image.h>
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include <stb_image_write.h>
|
||||
extern "C"
|
||||
{
|
||||
#include <jpeglib.h>
|
||||
#include <jerror.h>
|
||||
}
|
||||
#include <cctype>
|
||||
|
||||
|
||||
|
@ -268,7 +263,7 @@ bool ImageLoader::saveImageToFile(const std::string& filename, const std::vector
|
|||
else if (extension == "jpg" || extension == "jpeg")
|
||||
{
|
||||
// JPG format
|
||||
if (writeJpg(filename, pixels, size.x, size.y))
|
||||
if (stbi_write_jpg(filename.c_str(), size.x, size.y, 4, &pixels[0], 90))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -277,60 +272,6 @@ bool ImageLoader::saveImageToFile(const std::string& filename, const std::vector
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
bool ImageLoader::writeJpg(const std::string& filename, const std::vector<Uint8>& pixels, unsigned int width, unsigned int height)
|
||||
{
|
||||
// Open the file to write in
|
||||
FILE* file = fopen(filename.c_str(), "wb");
|
||||
if (!file)
|
||||
return false;
|
||||
|
||||
// Initialize the error handler
|
||||
jpeg_compress_struct compressInfos;
|
||||
jpeg_error_mgr errorManager;
|
||||
compressInfos.err = jpeg_std_error(&errorManager);
|
||||
|
||||
// Initialize all the writing and compression infos
|
||||
jpeg_create_compress(&compressInfos);
|
||||
compressInfos.image_width = width;
|
||||
compressInfos.image_height = height;
|
||||
compressInfos.input_components = 3;
|
||||
compressInfos.in_color_space = JCS_RGB;
|
||||
jpeg_stdio_dest(&compressInfos, file);
|
||||
jpeg_set_defaults(&compressInfos);
|
||||
jpeg_set_quality(&compressInfos, 90, TRUE);
|
||||
|
||||
// Get rid of the alpha channel
|
||||
std::vector<Uint8> buffer(width * height * 3);
|
||||
for (std::size_t i = 0; i < width * height; ++i)
|
||||
{
|
||||
buffer[i * 3 + 0] = pixels[i * 4 + 0];
|
||||
buffer[i * 3 + 1] = pixels[i * 4 + 1];
|
||||
buffer[i * 3 + 2] = pixels[i * 4 + 2];
|
||||
}
|
||||
Uint8* ptr = &buffer[0];
|
||||
|
||||
// Start compression
|
||||
jpeg_start_compress(&compressInfos, TRUE);
|
||||
|
||||
// Write each row of the image
|
||||
while (compressInfos.next_scanline < compressInfos.image_height)
|
||||
{
|
||||
JSAMPROW rawPointer = ptr + (compressInfos.next_scanline * width * 3);
|
||||
jpeg_write_scanlines(&compressInfos, &rawPointer, 1);
|
||||
}
|
||||
|
||||
// Finish compression
|
||||
jpeg_finish_compress(&compressInfos);
|
||||
jpeg_destroy_compress(&compressInfos);
|
||||
|
||||
// Close the file
|
||||
fclose(file);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace priv
|
||||
|
||||
} // namespace sf
|
||||
|
|
|
@ -118,19 +118,6 @@ private:
|
|||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
~ImageLoader();
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Save an image file in JPEG format
|
||||
///
|
||||
/// \param filename Path of image file to save
|
||||
/// \param pixels Array of pixels to save to image
|
||||
/// \param width Width of image to save, in pixels
|
||||
/// \param height Height of image to save, in pixels
|
||||
///
|
||||
/// \return True if saving was successful
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
bool writeJpg(const std::string& filename, const std::vector<Uint8>& pixels, unsigned int width, unsigned int height);
|
||||
};
|
||||
|
||||
} // namespace priv
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue