Commit 4e4acf79 authored by Davis King's avatar Davis King

Make CMake build a test program to see if libjpeg is really available and not broken.

parent 7611b347
......@@ -493,16 +493,8 @@ if (NOT TARGET dlib)
endif()
if (DLIB_JPEG_SUPPORT)
# try to find libjpeg
find_package(JPEG QUIET)
# Make sure there isn't something wrong with the version of libjpeg
# installed on this system. Also don't use the installed libjpeg
# if this is an APPLE system because apparently it's broken (as of 2015/01/01).
if (JPEG_FOUND AND NOT ("${JPEG_INCLUDE_DIR}" MATCHES "(.*)(Ana|ana|mini)conda(.*)") AND NOT BUILDING_PYTHON_IN_MSVC)
set(CMAKE_REQUIRED_LIBRARIES ${JPEG_LIBRARY})
CHECK_FUNCTION_EXISTS(jpeg_read_header LIBJPEG_IS_GOOD)
endif()
if (JPEG_FOUND AND LIBJPEG_IS_GOOD AND NOT APPLE)
include(cmake_utils/find_libjpeg.cmake)
if (JPEG_FOUND)
include_directories(${JPEG_INCLUDE_DIR})
set (dlib_needed_libraries ${dlib_needed_libraries} ${JPEG_LIBRARY})
else()
......
#This script just runs CMake's built in JPEG finding tool. But it also checks that the
#copy of libjpeg that cmake finds actually builds and links.
cmake_minimum_required(VERSION 2.8.12)
# Don't rerun this script if its already been executed.
if (DEFINED JPEG_FOUND)
return()
endif()
find_package(JPEG QUIET)
if(JPEG_FOUND)
try_compile(test_for_libjpeg_worked
${PROJECT_BINARY_DIR}/test_for_libjpeg_build
${CMAKE_CURRENT_LIST_DIR}/test_for_libjpeg
test_if_libjpeg_is_broken)
message (STATUS "Found system copy of libjpeg: ${JPEG_LIBRARY}")
if(NOT test_for_libjpeg_worked)
set(JPEG_FOUND 0)
message (STATUS "System copy of libjpeg is broken. Will build our own libjpeg and use that instead.")
endif()
endif()
cmake_minimum_required(VERSION 2.8.12)
project(test_if_libjpeg_is_broken)
find_package(JPEG)
include_directories(${JPEG_INCLUDE_DIR})
add_executable(libjpeg_test libjpeg_test.cpp)
target_link_libraries(libjpeg_test ${JPEG_LIBRARY})
// Copyright (C) 2019 Davis E. King (davis@dlib.net), Nils Labugt
// License: Boost Software License See LICENSE.txt for the full license.
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
#include <iostream>
struct jpeg_loader_error_mgr
{
jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};
void jpeg_loader_error_exit (j_common_ptr cinfo)
{
jpeg_loader_error_mgr* myerr = (jpeg_loader_error_mgr*) cinfo->err;
longjmp(myerr->setjmp_buffer, 1);
}
// This code doesn't really make a lot of sense. It's just calling all the libjpeg functions to make
// sure they can be compiled and linked.
int main()
{
std::cerr << "This program is just for build system testing. Don't actually run it." << std::endl;
abort();
FILE *fp = fopen("whatever.jpg", "rb" );
jpeg_decompress_struct cinfo;
jpeg_loader_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = jpeg_loader_error_exit;
setjmp(jerr.setjmp_buffer);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, fp);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
unsigned long height_ = cinfo.output_height;
unsigned long width_ = cinfo.output_width;
unsigned long output_components_ = cinfo.output_components;
unsigned char* rows[123];
while (cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo, &rows[cinfo.output_scanline], 100);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose( fp );
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment