Skip to content

Commit d2df9c8

Browse files
committed
Implement conditional compilation of PNG renderer
A feature test is done for this by default, but it can optionally be explicitly enabled or disabled (and the build will fail if it is set to enabled but libpng cannot be found). Run cmake with -DSAXBOSPIRAL_PNG_SUPPORT=<ON|OFF> to specify your build preference. Fixes #131
1 parent 7dffe00 commit d2df9c8

File tree

5 files changed

+80
-9
lines changed

5 files changed

+80
-9
lines changed

CMakeLists.txt

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,43 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "")
8989
enable_c_compiler_flag_if_supported("-Werror")
9090
endif()
9191

92-
# dependencies
92+
# begin dependencies
9393
# add custom dependencies directory
9494
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
9595
# libpng
96-
find_package(PNG 1 EXACT REQUIRED)
97-
include_directories(${PNG_INCLUDE_DIR})
96+
# work out whether we have or have not requested PNG support, or don't care (default)
97+
if(NOT DEFINED SAXBOSPIRAL_PNG_SUPPORT)
98+
# try and find libpng v1.x, but don't fail if we can't
99+
message(STATUS "PNG output support will be enabled if possible")
100+
find_package(PNG 1 EXACT)
101+
# set SAXBOSPIRAL_PNG_SUPPORT based on value of PNG_FOUND
102+
if(PNG_FOUND)
103+
set(SAXBOSPIRAL_PNG_SUPPORT ON)
104+
else()
105+
set(SAXBOSPIRAL_PNG_SUPPORT OFF)
106+
endif()
107+
elseif(SAXBOSPIRAL_PNG_SUPPORT)
108+
# find libpng v1.x and fail the build if we can't
109+
message(STATUS "PNG output support explicitly enabled")
110+
find_package(PNG 1 EXACT REQUIRED)
111+
else()
112+
# we've explicitly disabled PNG support, so don't include libpng
113+
# issue a message saying so
114+
message(STATUS "PNG output support explicitly disabled")
115+
endif()
116+
117+
# include libpng directories and add feature test macro if support is enabled
118+
if(SAXBOSPIRAL_PNG_SUPPORT)
119+
include_directories(${PNG_INCLUDE_DIR})
120+
# feature test macro
121+
add_definitions(-DSAXBOSPIRAL_PNG_SUPPORT)
122+
# issue message
123+
message(STATUS "PNG output support enabled")
124+
else()
125+
# issue message
126+
message(STATUS "PNG output support disabled")
127+
endif()
128+
# end dependencies
98129

99130
# C source files
100131
file(
@@ -114,12 +145,14 @@ set_target_properties(
114145
saxbospiral PROPERTIES VERSION ${SAXBOSPIRAL_VERSION_STRING}
115146
SOVERSION ${PROJECT_VERSION_MAJOR}
116147
)
117-
# Link libsaxbospiral with libpng so we get libpng symbols
118-
target_link_libraries(saxbospiral ${PNG_LIBRARY})
148+
# Link libsaxbospiral with libpng so we get libpng symbols (if support enabled)
149+
if(SAXBOSPIRAL_PNG_SUPPORT)
150+
target_link_libraries(saxbospiral ${PNG_LIBRARY})
151+
endif()
119152

120153
add_executable(sxp_test tests.c)
121154

122-
target_link_libraries(sxp_test saxbospiral ${PNG_LIBRARY})
155+
target_link_libraries(sxp_test saxbospiral)
123156

124157
install(
125158
TARGETS saxbospiral

saxbospiral/render_backends/backend_png.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
* This compilation unit provides functionality to render a bitmap struct to a
66
* PNG image (stored in a buffer).
77
*
8+
* NOTE: PNG output support may have not been enabled in the compiled version
9+
* of libsaxbospiral that you have. If support is not enabled, the library
10+
* boolean constant SXBP_PNG_SUPPORT will be set to false and the one public
11+
* function defined in this library will return SXBP_NOT_IMPLEMENTED.
12+
*
813
*
914
*
1015
* Copyright (C) 2016, Joshua Saxby [email protected]
@@ -22,10 +27,16 @@
2227
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2328
*/
2429
#include <assert.h>
30+
// only include these extra dependencies if support for PNG output was enabled
31+
#ifdef SAXBOSPIRAL_PNG_SUPPORT
2532
#include <stdlib.h>
2633
#include <string.h>
34+
#endif
2735

36+
// only include libpng if support for it was enabled
37+
#ifdef SAXBOSPIRAL_PNG_SUPPORT
2838
#include <png.h>
39+
#endif
2940

3041
#include "../saxbospiral.h"
3142
#include "../render.h"
@@ -36,6 +47,8 @@
3647
extern "C"{
3748
#endif
3849

50+
// only define the following private functions if libpng support was enabled
51+
#ifdef SAXBOSPIRAL_PNG_SUPPORT
3952
// private custom libPNG buffer write function
4053
static void buffer_write_data(
4154
png_structp png_ptr, png_bytep data, png_size_t length
@@ -80,11 +93,13 @@ static void cleanup_png_lib(
8093
free(row);
8194
}
8295
}
96+
#endif // SAXBOSPIRAL_PNG_SUPPORT
8397

8498
/*
8599
* given a bitmap_t struct and a pointer to a blank buffer_t, write the bitmap
86100
* data as a PNG image to the buffer, using libpng.
87-
* returns a status struct containing error information, if any
101+
* returns a status struct containing error information
102+
* returns SXBP_NOT_IMPLEMENTED if PNG support was not enabled
88103
*
89104
* Asserts:
90105
* - That bitmap.pixels is not NULL
@@ -96,6 +111,11 @@ sxbp_status_t sxbp_render_backend_png(
96111
// preconditional assertsions
97112
assert(bitmap.pixels != NULL);
98113
assert(buffer->bytes == NULL);
114+
// only do PNG operations if support is enabled
115+
#ifndef SAXBOSPIRAL_PNG_SUPPORT
116+
// return SXBP_NOT_IMPLEMENTED
117+
return SXBP_NOT_IMPLEMENTED;
118+
#else
99119
// result status
100120
sxbp_status_t result;
101121
// init buffer
@@ -186,6 +206,7 @@ sxbp_status_t sxbp_render_backend_png(
186206
// status ok
187207
result = SXBP_OPERATION_OK;
188208
return result;
209+
#endif // SAXBOSPIRAL_PNG_SUPPORT
189210
}
190211

191212
#ifdef __cplusplus

saxbospiral/render_backends/backend_png.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
* This compilation unit provides functionality to render a bitmap struct to a
66
* PNG image (stored in a buffer).
77
*
8+
* NOTE: PNG output support may have not been enabled in the compiled version
9+
* of libsaxbospiral that you have. If support is not enabled, the library
10+
* boolean constant SXBP_PNG_SUPPORT will be set to false and the one public
11+
* function defined in this library will return SXBP_NOT_IMPLEMENTED.
12+
*
813
*
914
*
1015
* Copyright (C) 2016, Joshua Saxby [email protected]
@@ -35,7 +40,8 @@ extern "C"{
3540
/*
3641
* given a bitmap_t struct and a pointer to a blank buffer_t, write the bitmap
3742
* data as a PNG image to the buffer, using libpng.
38-
* returns a status struct containing error information, if any
43+
* returns a status struct containing error information
44+
* returns SXBP_NOT_IMPLEMENTED if PNG support was not enabled
3945
*
4046
* Asserts:
4147
* - That bitmap.pixels is not NULL

saxbospiral/saxbospiral.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* You should have received a copy of the GNU Affero General Public License
2222
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2323
*/
24+
#include <stdbool.h>
25+
2426
#include "saxbospiral.h"
2527

2628

@@ -35,6 +37,12 @@ const sxbp_version_t LIB_SXBP_VERSION = {
3537
.patch = SAXBOSPIRAL_VERSION_PATCH,
3638
.string = SAXBOSPIRAL_VERSION_STRING,
3739
};
40+
// flag for whether PNG output support has been compiled in based, on macro
41+
#ifdef SAXBOSPIRAL_PNG_SUPPORT
42+
const bool SXBP_PNG_SUPPORT = true;
43+
#else
44+
const bool SXBP_PNG_SUPPORT = false;
45+
#endif
3846

3947
/*
4048
* computes a version_hash_t for a given version_t,

saxbospiral/saxbospiral.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ typedef struct sxbp_version_t {
4141
} sxbp_version_t;
4242

4343
extern const sxbp_version_t LIB_SXBP_VERSION;
44+
// flag for whether PNG output support has been compiled in based, on macro
45+
extern const bool SXBP_PNG_SUPPORT;
4446

4547
// used for indexing and comparing different versions in order
4648
typedef uint32_t sxbp_version_hash_t;
@@ -54,10 +56,11 @@ sxbp_version_hash_t sxbp_version_hash(sxbp_version_t version);
5456
// enum for function error information
5557
typedef enum sxbp_status_t {
5658
SXBP_STATE_UNKNOWN = 0, // unknown, the default state
59+
SXBP_OPERATION_OK, // no problem
5760
SXBP_OPERATION_FAIL, // generic failure state
5861
SXBP_MALLOC_REFUSED, // memory allocation or re-allocation was refused
5962
SXBP_IMPOSSIBLE_CONDITION, // condition thought to be impossible detected
60-
SXBP_OPERATION_OK, // no problem
63+
SXBP_NOT_IMPLEMENTED, // function is not implemented / enabled
6164
} sxbp_status_t;
6265

6366
// type for representing a cartesian direction

0 commit comments

Comments
 (0)