Skip to content

Commit 0508bdd

Browse files
authored
Merge pull request #141 from saxbophone/develop
v0.22.0
2 parents 7dffe00 + 8fbb3e3 commit 0508bdd

File tree

6 files changed

+99
-10
lines changed

6 files changed

+99
-10
lines changed

CMakeLists.txt

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# begin basic metadata
2424
cmake_minimum_required(VERSION 3.0)
2525

26-
project(libsaxbospiral VERSION 0.21.2 LANGUAGES C)
26+
project(libsaxbospiral VERSION 0.22.0 LANGUAGES C)
2727

2828
# set default C standard to use (C99) if not already set
2929
if(NOT DEFINED SAXBOSPIRAL_C_STANDARD)
@@ -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,16 @@ 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 C math library
149+
target_link_libraries(saxbospiral m)
150+
# Link libsaxbospiral with libpng so we get libpng symbols (if support enabled)
151+
if(SAXBOSPIRAL_PNG_SUPPORT)
152+
target_link_libraries(saxbospiral ${PNG_LIBRARY})
153+
endif()
119154

120155
add_executable(sxp_test tests.c)
121156

122-
target_link_libraries(sxp_test saxbospiral ${PNG_LIBRARY})
157+
target_link_libraries(sxp_test saxbospiral)
123158

124159
install(
125160
TARGETS saxbospiral

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ You will need:
4444

4545
- A compiler that can compile ISO C99 or C11 code
4646
- [Cmake](https://cmake.org/) - v3.0 or newer
47+
48+
*If you also want to be able to produce images in PNG format with the library, you will need:*
4749
- [libpng](http://www.libpng.org/pub/png/libpng.html) - (this often comes preinstalled with many modern unix-like systems)
4850

4951
> ### Note:
@@ -75,6 +77,20 @@ cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DSAXBOSPIRAL_C_STANDARD
7577
LIBSAXBOSPIRAL_C_STANDARD=11 cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON .
7678
```
7779

80+
Also, by default the CMake build script will look for libpng. If it cannot find it then it will disable support for PNG output.
81+
82+
You may choose to explicitly disable or enable PNG support with the `SAXBOSPIRAL_PNG_SUPPORT` CMake variable, which can be passed on the command-line like so:
83+
84+
```sh
85+
# PNG support is required, build will fail if libpng can't be found
86+
cmake -DSAXBOSPIRAL_PNG_SUPPORT=ON .
87+
```
88+
89+
```sh
90+
# PNG support is not included, even if libpng can be found
91+
cmake -DSAXBOSPIRAL_PNG_SUPPORT=OFF .
92+
```
93+
7894
> ### Note:
7995
8096
> Building as a shared library is recommended as then binaries compiled from [sxbp](https://github.com/saxbophone/sxbp) or your own programs that are linked against the shared version can immediately use any installed upgraded versions of libsaxbospiral with compatible ABIs without needing re-compiling.

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)