First commit

This commit is contained in:
Matteo Paonessa 2016-11-04 11:00:04 +01:00
commit 9fd8255123
14 changed files with 251 additions and 0 deletions

39
.gitignore vendored Normal file
View File

@ -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

10
CMakeLists.txt Normal file
View File

@ -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)

21
LICENSE Normal file
View File

@ -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.

2
README.md Normal file
View File

@ -0,0 +1,2 @@
# libcaesium
The Caesium compression library written in C

5
caesium/CMakeLists.txt Normal file
View File

@ -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})

36
caesium/caesium.c Normal file
View File

@ -0,0 +1,36 @@
#include <stdlib.h>
#include <stdio.h>
#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;
}

10
caesium/caesium.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef CAESIUM_H
#define CAESIUM_H
#include <stdbool.h>
#include "cstypes.h"
bool cs_compress(const char *input, const char *output, cs_image_pars *options);
#endif

43
caesium/cstypes.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef CS_CCLTYPES
#define CS_CCLTYPES
#include <stdbool.h>
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

28
caesium/error.c Normal file
View File

@ -0,0 +1,28 @@
#include <stdio.h>
#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.";
}
}

8
caesium/error.h Normal file
View File

@ -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

28
caesium/utils.c Normal file
View File

@ -0,0 +1,28 @@
#include <stdlib.h>
#include <stdio.h>
#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;
}

10
caesium/utils.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef CCLT_UTILS
#define CCLT_UTILS
#include <stdio.h>
#include "cstypes.h"
enum image_type detect_image_type(FILE *pFile);
#endif

3
demo/CMakeLists.txt Normal file
View File

@ -0,0 +1,3 @@
add_executable(caesiumd main.c)
target_link_libraries(caesiumd LINK_PUBLIC caesium)

8
demo/main.c Normal file
View File

@ -0,0 +1,8 @@
#include "caesium.h"
int main()
{
cs_image_pars options;
cs_compress("test", "test1", &options);
return 0;
}