Using Rust libcaesium

This commit is contained in:
Matteo Paonessa 2021-10-23 21:49:43 +02:00
parent 8be97e4201
commit 91ac6ff742
10 changed files with 111 additions and 94 deletions

View File

@ -23,6 +23,6 @@ before_install:
before_script:
- mkdir build
- cd build
- cmake ..
- cmake .. -DLIBCAESIUM_PATH=libcaesium/target/release
script: make

View File

@ -11,16 +11,13 @@ You will also need cmake if you want to compile it from source.
For Windows you just need to download the latest release package from [here](https://github.com/Lymphatus/caesium-clt/releases).
Unzip the package using your favorite software, open it and run `caesiumclt.exe` from the Command Prompt or PowerShell.
### MacOS X
**NOTE:** Homebrew installation will come soon. Use Linux instructions below for now.
### Linux
### MacOS X and Linux
Download the latest release package from [here](https://github.com/Lymphatus/caesium-clt/releases) or clone from git.
Then run:
$ cd caesium-clt
$ mkdir build && cd build
$ cmake ..
$ cmake .. -DLIBCAESIUM_PATH=/your/libcaesium/path/dir
$ make
$ sudo make install

View File

@ -3,10 +3,4 @@
#libcaesium
git clone https://github.com/Lymphatus/libcaesium.git
cd libcaesium || exit
sudo chmod +x install.sh
./install.sh
mkdir build
cd build || exit
cmake ..
make
sudo make install
cargo build --release

View File

@ -7,7 +7,6 @@ FILE(GLOB CVendorHeaders vendor/*.h)
add_executable(caesiumclt ${CVendorSources} ${CVendorHeaders} ${CSources} ${CHeaders})
target_link_libraries(caesiumclt LINK_PUBLIC caesium)
target_link_libraries(caesiumclt LINK_PUBLIC m)
target_link_libraries(caesiumclt LINK_PUBLIC caesium m)
install(TARGETS caesiumclt DESTINATION bin)

View File

@ -16,7 +16,6 @@
*/
#include <stdio.h>
#include <caesium.h>
#include <stdlib.h>
#include "error.h"

View File

@ -18,6 +18,12 @@
#ifndef CAESIUMCLT_ERROR_H
#define CAESIUMCLT_ERROR_H
typedef enum error_level
{
ERROR = 0,
WARNING = 1
} error_level;
void display_error(error_level level, int code);
const char *get_error_message(int code);

View File

@ -31,7 +31,7 @@
#include "error.h"
#include "shared.h"
cclt_options parse_arguments(char **argv, cs_image_pars *options) {
cclt_options parse_arguments(char **argv, C_CSParameters *options) {
struct optparse opts;
//Initialize application options
cclt_options parameters = {NULL, "", "", false, false, 0, 0, 0, false, all};
@ -53,16 +53,25 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options) {
};
optparse_init(&opts, argv);
int option;
int quality = 0;
while ((option = optparse_long(&opts, longopts, NULL)) != -1) {
switch (option) {
case 'q':
options->jpeg.quality = (int) strtol(opts.optarg, (char **) NULL, 10);
if (options->jpeg.quality < 0 || options->jpeg.quality > 100) {
quality = (int) strtol(opts.optarg, (char **) NULL, 10);
if (quality < 0 || quality > 100) {
display_error(ERROR, 1);
}
if (quality == 0) {
options->optimize = true;
} else {
options->jpeg_quality = quality;
options->png_level = parse_png_quality(quality);
options->webp_quality = quality;
options->gif_quality = quality;
}
break;
case 'e':
options->jpeg.exif_copy = true;
options->keep_metadata = true;
break;
case 'o':
if (opts.optarg[0] == '~') {
@ -101,9 +110,6 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options) {
#endif
}
break;
case 's':
options->jpeg.scale_factor = options->png.scale_factor = parse_scale_factor(opts.optarg);
break;
case 'R':
parameters.recursive = true;
break;
@ -221,11 +227,11 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options) {
return parameters;
}
int start_compression(cclt_options *options, cs_image_pars *parameters) {
int start_compression(cclt_options *options, struct C_CSParameters parameters) {
int compressed_files = 0;
off_t input_file_size = 0;
off_t output_file_size = 0;
//Create the output folder if does not exists
off_t input_file_size;
off_t output_file_size;
//Create the output folder if it does not exist
if (mkpath(options->output_folder) == -1) {
display_error(ERROR, 5);
}
@ -235,8 +241,7 @@ int start_compression(cclt_options *options, cs_image_pars *parameters) {
char *output_full_path = NULL;
char *original_output_full_path = NULL;
bool overwriting = false;
off_t file_size = 0;
int compression_error_code = 0;
off_t file_size;
//If we don't need to keep the structure, we put all the files in one folder by just the filename
if (!options->keep_structure) {
output_full_path = malloc((strlen(filename) + strlen(options->output_folder) + 1) * sizeof(char));
@ -307,7 +312,7 @@ int start_compression(cclt_options *options, cs_image_pars *parameters) {
fflush(stdout);
//Prevent compression if running in dry mode
if (!options->dry_run) {
if (cs_compress(options->input_files[i], output_full_path, parameters, &compression_error_code)) {
if (c_compress(options->input_files[i], output_full_path, parameters)) {
compressed_files++;
output_file_size = get_file_size(output_full_path);
@ -324,10 +329,9 @@ int start_compression(cclt_options *options, cs_image_pars *parameters) {
print_to_console(stdout, verbose, "\r%s -> %s [%.2f%%]\n",
human_input_size,
human_output_size,
((float) output_file_size - input_file_size) * 100 / input_file_size);
((float) output_file_size - (float) input_file_size) * 100 / (float) input_file_size);
} else {
print_to_console(stdout, verbose, "\n");
print_to_console(stderr, verbose, "Compression failed with error %d\n", compression_error_code);
print_to_console(stderr, verbose, "\nCompression failed\n");
options->input_total_size -= get_file_size(options->input_files[i]);
}
}

View File

@ -18,12 +18,12 @@
#ifndef CAESIUM_CLT_HELPER_H
#define CAESIUM_CLT_HELPER_H
#include <caesium.h>
#ifdef _WIN32
#define MAX_PATH_SIZE _MAX_PATH
#else
#include <limits.h>
#include <stdbool.h>
#define MAX_PATH_SIZE PATH_MAX
#endif
@ -34,6 +34,18 @@ typedef enum overwrite_policy {
all
} overwrite_policy;
typedef struct C_CSParameters {
bool keep_metadata;
unsigned int jpeg_quality;
unsigned int png_level;
bool png_force_zopfli;
unsigned int gif_quality;
unsigned int webp_quality;
bool optimize;
} C_CSParameters;
extern bool c_compress(const char *i, const char *o, struct C_CSParameters params);
typedef struct cclt_options
{
char **input_files;
@ -50,8 +62,8 @@ typedef struct cclt_options
cclt_options parse_arguments(char *argv[], cs_image_pars *options);
cclt_options parse_arguments(char *argv[], C_CSParameters *options);
int start_compression(cclt_options *options, cs_image_pars *parameters);
int start_compression(cclt_options *options, struct C_CSParameters parameters);
#endif //CAESIUM_CLT_HELPER_H

View File

@ -18,7 +18,6 @@
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <caesium.h>
#include "utils.h"
#include "shared.h"
@ -31,18 +30,25 @@ int main(int argc, char *argv[])
}
long compression_time = 0;
cs_image_pars compress_options;
cclt_options options;
//Initialize the default parameters
compress_options = initialize_parameters();
C_CSParameters compress_options = {
false,
80,
3,
false,
20,
60,
false
};
//Set them according to command line parameters
options = parse_arguments(argv, &compress_options);
//Start a timer before calling the compression
clock_t start = clock(), diff;
start_compression(&options, &compress_options);
start_compression(&options, compress_options);
//Cleanup the memory allocated objects
free(options.input_files);

View File

@ -38,12 +38,12 @@ bool file_exists(const char* file_path);
int strndx(const char* string, char search);
double parse_scale_factor(const char* factor_string);
overwrite_policy parse_overwrite_policy(const char* overwrite_string);
void print_to_console(FILE* buffer, int verbose, const char* format, ...);
int parse_png_quality(int quality);
#ifdef _WIN32
char *str_replace(char *orig, char *rep, char *with);
char *strsep(char **stringp, const char *delim);