2016-11-04 11:00:04 +01:00
# libcaesium
2016-11-13 14:43:54 +01:00
[](https://travis-ci.org/Lymphatus/libcaesium)
2016-11-14 23:43:52 +01:00
Libcaesium is a simple library performing JPEG and PNG compression/optimization using [mozjpeg ](https://github.com/mozilla/mozjpeg ) and [zopfli ](https://github.com/google/zopfli ).
2016-11-16 09:41:46 +01:00
## Download
Binaries not available yet. Please refer to the compilation section below.
2016-11-14 23:43:52 +01:00
## Basic usage
2017-02-21 12:54:52 +01:00
Libcaesium exposes one single function to compress, auto-detecting the input file type:
2016-11-14 23:43:52 +01:00
```C
bool cs_compress(const char *input,
2019-12-23 18:52:55 +01:00
const char *output,
cs_image_pars *options,
int* err_n);
2016-11-14 23:43:52 +01:00
```
#### Parameters
**input** - input file path
**output** - output file path
**options** - pointer to the options struct, containing compression parameters (see below)
2019-12-23 18:52:55 +01:00
**err_n** - pointer to an integer that will contain the error code if something went wrong during compression
2016-11-14 23:43:52 +01:00
#### Return value
2019-12-23 18:52:55 +01:00
**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.
2016-11-14 23:43:52 +01:00
## Compression options
Libcaesium supports a few compression parameters for each JPEG and PNG.
2017-02-21 12:54:52 +01:00
You need to initialize the default values before compressing by calling `initialize_parameters()` .
2016-11-14 23:43:52 +01:00
They are defined into a top level struct containing each supported file parameters, as follows:
```C
typedef struct cs_image_pars
{
cs_jpeg_pars jpeg;
cs_png_pars png;
} cs_image_pars;
```
### JPEG
```C
typedef struct cs_jpeg_pars
{
int quality;
bool exif_copy;
int dct_method;
2017-12-29 23:09:13 +01:00
double scale_factor;
2016-11-14 23:43:52 +01:00
} cs_jpeg_pars;
```
2017-12-29 23:09:13 +01:00
The first 4 parameters matters, in term of compression, while the others will be set by the compressor/decompressor
2016-11-14 23:43:52 +01:00
during the compression progress and thus they will be overwritten.
2016-12-15 21:47:35 +01:00
- **quality**: in a range from 0 to 100, the quality of the resulting image. **Note** that 0 means _optimization_ (see below). Default: 0.
2017-06-03 12:22:23 +02:00
- **exif_copy**: set it to _true_ to copy EXIF tag info after compression. Default: false.
2016-11-14 23:43:52 +01:00
- **dct_method**: one of the turbojpeg DCT flags. Default: TJFLAG_FASTDCT.
2017-12-29 23:09:13 +01:00
- **scale_factor**: the image scaling factor, expressed as double precision number. Default: 1.0.
2016-11-14 23:43:52 +01:00
### PNG
```C
typedef struct cs_png_pars
{
int iterations;
int iterations_large;
int block_split_strategy;
bool lossy_8;
bool transparent;
int auto_filter_strategy;
2017-12-29 23:09:13 +01:00
double scale_factor;
2016-11-14 23:43:52 +01:00
} cs_png_pars;
```
2017-12-29 23:09:13 +01:00
Those are the zopflipng compression parameters, except for the last one.
2016-11-14 23:43:52 +01:00
- **iterations**: number of iterations (more means more compression). Default: 10.
- **iteration_large**: number of iterations for large files. Default: 5.
- **block_split_strategy**: filter strategy. Default: 4;
- **lossy_8**: convert 16-bit per channel image to 8-bit per channel. Default: true.
- **transparent**: remove colors behind alpha channel 0. Default: true.
- **auto_filter_strategy**: legacy.
2017-12-29 23:09:13 +01:00
- **scale_factor**: the image scaling factor, expressed as double precision number. Note that PNG cannot be upscaled. Default: 1.0.
2016-11-14 23:43:52 +01:00
2016-11-16 09:41:46 +01:00
## Compilation and Installation
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.
2019-12-23 18:52:55 +01:00
You can also enable the verbose output, which will print on stderr if anything goes wrong, by using the `-DVERBOSE=1` flag during compilation.
2016-11-14 23:43:52 +01:00
2019-12-10 23:29:34 +01:00
### OS X/Linux
2017-03-21 18:13:27 +01:00
##### Requirements
2019-12-10 23:29:34 +01:00
Be sure you have the build tools
###### Linux
`$ sudo apt-get install libtool autoconf git nasm pkg-config cmake libpng-dev`
2017-03-21 18:13:27 +01:00
2019-12-10 23:29:34 +01:00
###### OSX
`$ brew install nasm cmake`
Get the code with
`$ git clone https://github.com/Lymphatus/libcaesium.git`
If you don't have `mozjpeg` and `zopfli` you should run
2017-03-21 18:13:27 +01:00
```bash
$ cd libcaesium
2019-12-10 23:29:34 +01:00
$ ./install.sh
2016-11-16 09:41:46 +01:00
```
2020-01-30 15:02:50 +01:00
which will install the requirements.
2017-03-21 18:13:27 +01:00
##### Compile
Provided you have all the requirements, building and installing from git is as simple as typing
```bash
2016-11-16 09:41:46 +01:00
$ mkdir build
$ cd build
$ cmake ..
$ make
$ sudo make install
```
2019-12-10 23:29:34 +01:00
This will compile the Caesium library, the required header and a small demo application named _caesiumd_ .
2016-11-16 09:41:46 +01:00
### Windows
2017-03-21 18:13:27 +01:00
Compiling on Windows is somehow tricky. You can achieve it with MinGW (tested) or Cygwin (not tested), but it's better to stick with the binaries provided.
2016-11-14 23:43:52 +01:00
## Compression vs Optimization
JPEG is a lossy format: that means you will always lose some information after each compression. So, compressing a file with
100 quality for 10 times will result in a always different image, even though you can't really see the difference.
2016-11-14 23:45:20 +01:00
Libcaesium also supports optimization, by setting the _quality_ to 0. This performs a lossless process, resulting in the same image,
2016-11-14 23:43:52 +01:00
but with a smaller size (10-15% usually).
2017-12-29 23:09:13 +01:00
PNG is lossless, so libcaesium will always perform optimization rather than compression.
## Resizing
Resizing is partially supported. It is handy but it's almost completely out of the scope of this library.
2020-01-30 15:02:50 +01:00
If you really feel the need to do it within libcaesium you can do so, but I advise you should opt for a different toolset for the best results.