diff --git a/CMakeLists.txt b/CMakeLists.txt index b77ccbc..e0717a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,8 +7,14 @@ set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) # The version number. set(VERSION_MAJOR 0) -set(VERSION_MINOR 4) -set(VERSION_PATCH 4) +set(VERSION_MINOR 5) +set(VERSION_PATCH 0) + +if (DEFINED VERBOSE) + set(VERBOSE ${VERBOSE}) +else () + set(VERBOSE 0) +endif () configure_file( "caesium/config.h.in" @@ -22,19 +28,19 @@ if (APPLE) include_directories(/opt/mozjpeg/include) include_directories(/usr/local/include) -elseif(WIN32) +elseif (WIN32) include_directories(C:\\mozjpeg\\include) include_directories(C:\\zopfli\\src) -else() +else () include_directories(/opt/mozjpeg/include) include_directories(/usr/include/zopflipng) - if(EXISTS /opt/mozjpeg/lib64) + if (EXISTS /opt/mozjpeg/lib64) link_directories(/opt/mozjpeg/lib64) - else() + else () link_directories(/opt/mozjpeg/lib) - endif() -endif() + endif () +endif () link_directories(/usr/local/lib) diff --git a/README.md b/README.md index 0bba028..8f0abbe 100644 --- a/README.md +++ b/README.md @@ -11,16 +11,19 @@ Binaries not available yet. Please refer to the compilation section below. Libcaesium exposes one single function to compress, auto-detecting the input file type: ```C bool cs_compress(const char *input, - char *output, - cs_image_pars *options); + const char *output, + cs_image_pars *options, + int* err_n); ``` #### Parameters **input** - input file path **output** - output file path **options** - pointer to the options struct, containing compression parameters (see below) +**err_n** - pointer to an integer that will contain the error code if something went wrong during compression #### Return value -**true** if the compression has successfully ended, or **false** if any error occurs. +**true** if the compression has successfully ended, or **false** if any error occurs. If any error occurred, the **err_n** +variable will contain the error code. See `error.h` for further info. ## Compression options Libcaesium supports a few compression parameters for each JPEG and PNG. @@ -78,6 +81,7 @@ Those are the zopflipng compression parameters, except for the last one. Libcaesium uses cmake to build and install the library. Before compiling, be sure to have all the requisites. Libcaesium requires [mozjpeg](https://github.com/mozilla/mozjpeg) and [zopfli](https://github.com/google/zopfli) installed as shared/static libraries. Please refer to their own documentation for detailed instructions. +You can also enable the verbose output, which will print on stderr if anything goes wrong, by using the `-DVERBOSE=1` flag during compilation. ### OS X/Linux ##### Requirements diff --git a/caesium/caesium.c b/caesium/caesium.c index e68c0fe..f48e8bc 100644 --- a/caesium/caesium.c +++ b/caesium/caesium.c @@ -1,4 +1,3 @@ -#include #include #include "error.h" @@ -6,7 +5,7 @@ #include "png.h" #include "jpeg.h" -bool cs_compress(const char *input_path, const char *output_path, cs_image_pars *options) +bool cs_compress(const char *input_path, const char *output_path, cs_image_pars *options, int* err_n) { FILE *pInputFile; image_type type; @@ -15,6 +14,7 @@ bool cs_compress(const char *input_path, const char *output_path, cs_image_pars if ((pInputFile = fopen(input_path, "rb")) == NULL) { display_error(ERROR, 104); + *err_n = error_code; return result; } @@ -39,6 +39,8 @@ bool cs_compress(const char *input_path, const char *output_path, cs_image_pars result = cs_png_optimize(input_path, output_path, &options->png); } + *err_n = error_code; + return result; } diff --git a/caesium/caesium.h b/caesium/caesium.h index 85c416a..d7d5a25 100644 --- a/caesium/caesium.h +++ b/caesium/caesium.h @@ -54,7 +54,7 @@ typedef enum error_level WARNING = 1 } error_level; -bool cs_compress(const char *input_path, const char *output_path, cs_image_pars *options); +bool cs_compress(const char *input_path, const char *output_path, cs_image_pars *options, int* err_n); cs_image_pars initialize_parameters(); #ifdef __cplusplus diff --git a/caesium/config.h.in b/caesium/config.h.in index 30f46c8..5bb7d65 100644 --- a/caesium/config.h.in +++ b/caesium/config.h.in @@ -1,3 +1,5 @@ #define VERSION_MAJOR @VERSION_MAJOR@ #define VERSION_MINOR @VERSION_MINOR@ -#define VERSION_PATCH @VERSION_PATCH@ \ No newline at end of file +#define VERSION_PATCH @VERSION_PATCH@ + +#define VERBOSE @VERBOSE@ \ No newline at end of file diff --git a/caesium/error.c b/caesium/error.c index baa5278..b365eb1 100644 --- a/caesium/error.c +++ b/caesium/error.c @@ -1,62 +1,67 @@ #include #include "error.h" +#include "config.h" -void display_error(error_level level, int code) -{ - char *error_level = ((level) ? "[WARNING]" : "[ERROR]"); - fprintf(stderr, "%s %d: %s\n", - error_level, - code, - get_error_message(code)); +int error_code = 0; + +void display_error(error_level level, int code) { + error_code = code; + if (VERBOSE) { + char *error_level = ((level) ? "[WARNING]" : "[ERROR]"); + + fprintf(stderr, "%s %d: %s\n", + error_level, + code, + get_error_message(code)); + } } -const char *get_error_message(int code) -{ - switch (code) { - //Generic errors - case 101: - return "NULL file pointer while checking type."; - case 103: - return "File type not supported."; - case 104: - return "Could not open input file."; +const char *get_error_message(int code) { + switch (code) { + //Generic errors + case 101: + return "NULL file pointer while checking type."; + case 103: + return "File type not supported."; + case 104: + return "Could not open input file."; - //JPEG related errors - case 200: - return "Failed to open JPEG file while trying to get markers"; - case 201: - return "Failed to open input JPEG file while optimizing"; - case 202: - return "Failed to open output JPEG file while optimizing"; - case 203: - return "Failed to open JPEG file while compressing"; - case 204: - return "Failed to open JPEG file while decompressing"; - case 205: - return "Failed to retrieve input JPEG file size"; - case 206: - return "Input JPEG file is too big"; - case 207: - return "Compressor failed"; - case 208: - return "Decompressor failed"; - case 209: - return "CMYK images are not fully supported and can only be optimized."; + //JPEG related errors + case 200: + return "Failed to open JPEG file while trying to get markers"; + case 201: + return "Failed to open input JPEG file while optimizing"; + case 202: + return "Failed to open output JPEG file while optimizing"; + case 203: + return "Failed to open JPEG file while compressing"; + case 204: + return "Failed to open JPEG file while decompressing"; + case 205: + return "Failed to retrieve input JPEG file size"; + case 206: + return "Input JPEG file is too big"; + case 207: + return "Compressor failed"; + case 208: + return "Decompressor failed"; + case 209: + return "CMYK images are not fully supported and can only be optimized."; - //PNG related errors - case 300: - return "Failed to load PNG file."; - case 301: - return "Error while optimizing PNG."; - case 303: - return "Error while writing output PNG file."; - case 304: - return "Error while resizing PNG file."; - case 305: - return "PNG scaling factor must be a number greater than 0 and equal or minor to 1."; + //PNG related errors + case 300: + return "Failed to load PNG file."; + case 301: + return "Error while optimizing PNG."; + case 303: + return "Error while writing output PNG file."; + case 304: + return "Error while resizing PNG file."; + case 305: + return "PNG scaling factor must be a number greater than 0 and equal or minor to 1."; - default: - return "Unrecognized error."; - } + default: + return "Unrecognized error."; + } } \ No newline at end of file diff --git a/caesium/error.h b/caesium/error.h index 189c23b..c45f1b2 100644 --- a/caesium/error.h +++ b/caesium/error.h @@ -3,6 +3,8 @@ #include "caesium.h" +extern int error_code; + void display_error(error_level level, int code); const char *get_error_message(int code); diff --git a/config.h b/config.h deleted file mode 100644 index 62da0e8..0000000 --- a/config.h +++ /dev/null @@ -1,3 +0,0 @@ -#define VERSION_MAJOR 0 -#define VERSION_MINOR 4 -#define VERSION_PATCH 1 diff --git a/demo/CMakeLists.txt b/demo/CMakeLists.txt index 79ca8e2..f2db30f 100644 --- a/demo/CMakeLists.txt +++ b/demo/CMakeLists.txt @@ -1,3 +1,9 @@ +if (NOT WIN32) + set(CMAKE_C_FLAGS "--std=gnu99 -fPIC -Wno-nullability-completeness ${CMAKE_C_FLAGS}") +else () + set(CMAKE_C_FLAGS "--std=gnu99 ${CMAKE_C_FLAGS}") +endif () + add_executable(caesiumd main.c) target_link_libraries(caesiumd LINK_PUBLIC caesium) \ No newline at end of file diff --git a/demo/main.c b/demo/main.c index 9f1a05c..1c7eb79 100644 --- a/demo/main.c +++ b/demo/main.c @@ -1,6 +1,6 @@ #include #include -#include "caesium.h" +#include "../caesium/caesium.h" #include "config.h" int main(int argc, char *argv[]) @@ -16,7 +16,8 @@ int main(int argc, char *argv[]) } cs_image_pars options = initialize_parameters(); - bool result = cs_compress(argv[1], argv[2], &options); + int error_code = 0; + cs_compress(argv[1], argv[2], &options, &error_code); - exit(!result); + exit(error_code); } \ No newline at end of file