From 9fd8255123cd0ee928a6ba401b2799b5b3fd16f1 Mon Sep 17 00:00:00 2001 From: Matteo Paonessa Date: Fri, 4 Nov 2016 11:00:04 +0100 Subject: [PATCH] First commit --- .gitignore | 39 ++++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 10 ++++++++++ LICENSE | 21 +++++++++++++++++++++ README.md | 2 ++ caesium/CMakeLists.txt | 5 +++++ caesium/caesium.c | 36 +++++++++++++++++++++++++++++++++++ caesium/caesium.h | 10 ++++++++++ caesium/cstypes.h | 43 ++++++++++++++++++++++++++++++++++++++++++ caesium/error.c | 28 +++++++++++++++++++++++++++ caesium/error.h | 8 ++++++++ caesium/utils.c | 28 +++++++++++++++++++++++++++ caesium/utils.h | 10 ++++++++++ demo/CMakeLists.txt | 3 +++ demo/main.c | 8 ++++++++ 14 files changed, 251 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 LICENSE create mode 100644 README.md create mode 100644 caesium/CMakeLists.txt create mode 100644 caesium/caesium.c create mode 100644 caesium/caesium.h create mode 100644 caesium/cstypes.h create mode 100644 caesium/error.c create mode 100644 caesium/error.h create mode 100644 caesium/utils.c create mode 100644 caesium/utils.h create mode 100644 demo/CMakeLists.txt create mode 100644 demo/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..efb4f5f --- /dev/null +++ b/.gitignore @@ -0,0 +1,39 @@ +# Object files +*.o +*.ko +*.obj +*.elf + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su + +# CLion +.idea/* + +# Build +build \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..b7877ae --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.6) +project(libcaesium) + +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + +#set(SOURCE_FILES demo/main.cpp) +#add_executable(libcaesium ${SOURCE_FILES}) + +add_subdirectory(caesium) +add_subdirectory(demo) \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..051d0bf --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 Matteo Paonessa + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..99a6f93 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# libcaesium +The Caesium compression library written in C diff --git a/caesium/CMakeLists.txt b/caesium/CMakeLists.txt new file mode 100644 index 0000000..d44c87c --- /dev/null +++ b/caesium/CMakeLists.txt @@ -0,0 +1,5 @@ +add_library(caesium caesium.c error.c utils.c) + +# Make sure the compiler can find include files for our Caesium library +# when other libraries or executables link to Caesium +target_include_directories(caesium PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) \ No newline at end of file diff --git a/caesium/caesium.c b/caesium/caesium.c new file mode 100644 index 0000000..bbeed35 --- /dev/null +++ b/caesium/caesium.c @@ -0,0 +1,36 @@ +#include +#include + +#include "cstypes.h" +#include "error.h" +#include "utils.h" +#include "caesium.h" + +bool cs_compress(const char *input, const char *output, cs_image_pars *options) +{ + FILE *pInputFile; + enum image_type type; + bool result = false; + + //Inline... Should I split into 2 lines? + if ((pInputFile = fopen(input, "rb")) == NULL) { + display_error(0, 4); + return result; + } + + type = detect_image_type(pInputFile); + + fclose(pInputFile); + + if (type == UNKN) { + display_error(1, 3); + } else if (type == JPEG) { + //TODO result Compress JPEG + printf("Called JPEG compression\n"); + } else if (type == PNG) { + //TODO result Compress PNG + printf("Called PNG compression\n"); + } + + return result; +} \ No newline at end of file diff --git a/caesium/caesium.h b/caesium/caesium.h new file mode 100644 index 0000000..b6a9cbf --- /dev/null +++ b/caesium/caesium.h @@ -0,0 +1,10 @@ +#ifndef CAESIUM_H +#define CAESIUM_H + +#include + +#include "cstypes.h" + +bool cs_compress(const char *input, const char *output, cs_image_pars *options); + +#endif \ No newline at end of file diff --git a/caesium/cstypes.h b/caesium/cstypes.h new file mode 100644 index 0000000..0fbe4ef --- /dev/null +++ b/caesium/cstypes.h @@ -0,0 +1,43 @@ +#ifndef CS_CCLTYPES +#define CS_CCLTYPES + +#include + +typedef struct cs_jpeg_pars +{ + int quality; + int color_space; + int dct_method; + bool exif_copy; + bool lossless; + //TODO uncomment when linking turbojpeg enum TJSAMP subsample; +} cs_jpeg_pars; + +typedef struct cs_png_pars +{ + int iterations; + int iterations_large; + int block_split_strategy; + bool lossy_8; + bool transparent; + int auto_filter_strategy; +} cs_png_pars; + +typedef struct cs_image_pars +{ + cs_jpeg_pars jpeg; + cs_png_pars png; + + int width; + int height; + char **filepath; +} cs_image_pars; + +enum image_type +{ + JPEG, + PNG, + UNKN, +}; + +#endif diff --git a/caesium/error.c b/caesium/error.c new file mode 100644 index 0000000..01eef2d --- /dev/null +++ b/caesium/error.c @@ -0,0 +1,28 @@ +#include + +#include "error.h" + +void display_error(int level, int code) +{ + 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) { + case 1: + return "NULL file pointer while checking type."; + case 2: + return "Could not read enough file bytes for type checking."; + case 3: + return "File type not supported."; + case 4: + return "Could not open input file."; + default: + return "Unrecognized error."; + } +} \ No newline at end of file diff --git a/caesium/error.h b/caesium/error.h new file mode 100644 index 0000000..4394d93 --- /dev/null +++ b/caesium/error.h @@ -0,0 +1,8 @@ +#ifndef CS_ERROR +#define CS_ERROR + +void display_error(int level, int code); + +const char *get_error_message(int code); + +#endif \ No newline at end of file diff --git a/caesium/utils.c b/caesium/utils.c new file mode 100644 index 0000000..949db0b --- /dev/null +++ b/caesium/utils.c @@ -0,0 +1,28 @@ +#include +#include + +#include "utils.h" +#include "error.h" + +enum image_type detect_image_type(FILE *pFile) +{ + unsigned char buffer[2]; + + if (pFile == NULL) { + display_error(0, 1); + return UNKN; + } + + if (fread(buffer, 1, 2, pFile) < 2) { + display_error(0, 2); + return UNKN; + } + + if (buffer[0] == 0xFF && buffer[1] == 0xD8) { + return JPEG; + } else if (buffer[0] == 0x89 && buffer[1] == 0x50) { + return PNG; + } + + return UNKN; +} diff --git a/caesium/utils.h b/caesium/utils.h new file mode 100644 index 0000000..abb28e9 --- /dev/null +++ b/caesium/utils.h @@ -0,0 +1,10 @@ +#ifndef CCLT_UTILS +#define CCLT_UTILS + +#include + +#include "cstypes.h" + +enum image_type detect_image_type(FILE *pFile); + +#endif \ No newline at end of file diff --git a/demo/CMakeLists.txt b/demo/CMakeLists.txt new file mode 100644 index 0000000..79ca8e2 --- /dev/null +++ b/demo/CMakeLists.txt @@ -0,0 +1,3 @@ +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 new file mode 100644 index 0000000..12bd41a --- /dev/null +++ b/demo/main.c @@ -0,0 +1,8 @@ +#include "caesium.h" + +int main() +{ + cs_image_pars options; + cs_compress("test", "test1", &options); + return 0; +} \ No newline at end of file