tjko / jpegoptim

jpegoptim - utility to optimize/compress JPEG files
http://www.iki.fi/tjko/projects.html
GNU General Public License v3.0
1.57k stars 116 forks source link

Compile with MozJPEG #41

Closed adrianmihalko closed 4 years ago

adrianmihalko commented 7 years ago

I am not a CLI master, can you please provide me more details about how to compile with MozJPEG support?

I tried:

$ ls /opt/mozjpeg
bin  include  lib64  share
@ubuntu:~/scripts/jpegoptim$ ./configure CPPFLAGS=-I/opt/mozjpeg/bin/ LDFLAGS=-L/opt/mozjpeg/include/
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for a BSD-compatible install... /usr/bin/install -c
checking whether make sets $(MAKE)... yes
checking for jpeg_read_header in -ljpeg... yes
checking for floor in -lm... yes
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for unistd.h... (cached) yes
checking getopt.h usability... yes
checking getopt.h presence... yes
checking for getopt.h... yes
checking for string.h... (cached) yes
checking libgen.h usability... yes
checking libgen.h presence... yes
checking for libgen.h... yes
checking math.h usability... yes
checking math.h presence... yes
checking for math.h... yes
checking jpeglib.h usability... yes
checking jpeglib.h presence... yes
checking for jpeglib.h... yes
checking size of long... 8
checking size of int... 4
checking for getopt_long... yes
checking for mkstemps... yes
checking for labs... yes
checking for broken jmorecfg.h (METHODDEF)... no
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: config.h is unchanged

$ ./jpegoptim --help
Wrong JPEG library version: library is 80, caller expects 62

https://github.com/mozilla/mozjpeg/issues/232#issuecomment-267703663

adrianmihalko commented 7 years ago

I researched more and I tried:

n@ubuntu:~/scripts/jpegoptim$ ./configure CPPFLAGS=-I/opt/mozjpeg/include LDFLAGS=-L/opt/mozjpeg/lib64
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for a BSD-compatible install... /usr/bin/install -c
checking whether make sets $(MAKE)... yes
checking for jpeg_read_header in -ljpeg... yes
checking for floor in -lm... yes
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for unistd.h... (cached) yes
checking getopt.h usability... yes
checking getopt.h presence... yes
checking for getopt.h... yes
checking for string.h... (cached) yes
checking libgen.h usability... yes
checking libgen.h presence... yes
checking for libgen.h... yes
checking math.h usability... yes
checking math.h presence... yes
checking for math.h... yes
checking jpeglib.h usability... yes
checking jpeglib.h presence... yes
checking for jpeglib.h... yes
checking size of long... 8
checking size of int... 4
checking for getopt_long... yes
checking for mkstemps... yes
checking for labs... yes
checking for broken jmorecfg.h (METHODDEF)... no
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: config.h is unchanged
madrian@ubuntu:~/scripts/jpegoptim$ ls /opt/mozjpeg/include
jconfig.h  jerror.h  jmorecfg.h  jpeglib.h  turbojpeg.h
ubuntu:~/scripts/jpegoptim$ make
gcc  -g -O2 -I/opt/mozjpeg/include -DHAVE_CONFIG_H   -c -o jpegoptim.o jpegoptim.c
gcc  -g -O2 -I/opt/mozjpeg/include -DHAVE_CONFIG_H   -c -o jpegdest.o jpegdest.c
gcc  -g -O2 -I/opt/mozjpeg/include -DHAVE_CONFIG_H   -c -o misc.o misc.c
gcc  -g -O2 -I/opt/mozjpeg/include -DHAVE_CONFIG_H -o jpegoptim jpegoptim.o jpegdest.o misc.o  -L/opt/mozjpeg/lib64 -lm -ljpeg  
ubuntu:~/scripts/jpegoptim$ ./jpegoptim --version
jpegoptim v1.4.4  x86_64-unknown-linux-gnu
Copyright (c) 1996-2016, Timo Kokkonen

libjpeg version: 6b  27-Mar-1998
Copyright (C) 1991-2014 The libjpeg-turbo Project and many others

Now it runs fine, but I am not sure if it is using the mozjpeg library.

 jpegoptim -m 91 120,902.jpg -d ./hello/
 cjpeg -quality 91 120,902.jpg > ./hello/comp91.jpg

-rw-rw-r-- 1 madrian madrian 91073 Dec 17 15:28 120,902.jpg
-rw-rw-r-- 1 madrian madrian 91089 Dec 17 15:30 comp91.jpg

So the 2 files has different file sizes.

HeadstrongCatgirl commented 7 years ago

Had to add /opt/mozjpeg/lib64 to ldconfig manually, but now jpegoptim creates files 3-4% smaller than before. echo '/opt/mozjpeg/lib64/' > /etc/ld.so.conf.d/mozjpeg.conf && ldconfig

aolin480 commented 7 years ago

a buddy and I were just talking about this yesterday and he came up with these instructions, which also work on Mac OS if you have homebrew installed (for pkg tools). Just for reference if anyone wants the steps to do this. Props to @wseemann :

# get the depdendencies (view brew)
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew install pkg-config autoconf automake libtool nasm

# get mozjpeg
wget https://github.com/mozilla/mozjpeg/releases/download/v3.2/mozjpeg-3.2-release-source.tar.gz
tar -xvf mozjpeg-3.2-release-source.tar.gz
cd mozjpeg-3.2-release-source

# build mozjpeg
autoreconf -fiv
./configure
make
sudo make install

# get jpegoptim
git clone https://github.com/tjko/jpegoptim.git
cd jpegoptim.git

# build jpegoptim
./configure CPPFLAGS=-I/opt/mozjpeg/include LDFLAGS=-L/opt/mozjpeg/lib
make
sudo make install

# run it (from terminal)
jpegoptim -V
ianamk commented 6 years ago

a bit late but it might be helpful to somebody else I compiled it with mozjpeg on windows with gcc just by using the switch ./configure --with-libjpeg=/d/mozjpeg-3.3.1/.libs I had to manually copy the mozjpeg include files (jconfig.h, jerror.h, jmorecfg.h, jpeglib.h, turbojpeg.h) to C:\msys64\mingw64\include\ or the ./configure script couldn't find them but this is doable on linux too if you don't want to install the mozjpeg binaries just pass the path to where libjpeg.a is and copy the above mentioned include files to /usr/local/include (you can delete them after compile)

dvershinin commented 5 years ago

Users of CentOS / RedHat 7 may want to install jpegoptim compiled with MozJPEG using these instructions (no compilation required, ready to install package).

ozcoder commented 5 years ago

If you are OK with using ABI 8 of libjpeg then this works. Build mozjpeg and install to prefix of /usr/local

mkdir build
cd build
cmake -GUnix Makefiles ../ -DCMAKE_INSTALL_PREFIX=/usr/local -DWITH_JPEG8=1
make
sudo make install/strip

Now build jpegoptim

./configure --with-libjpeg=/usr/local
make
make strip
sudo make install

Now as root, tell jpegoptim where the library is echo '/usr/local/lib/' > /etc/ld.so.conf.d/jpegoptim.conf

adrianmihalko commented 5 years ago

Guys, can you verify what should be the output when mozjpeg is enabled?

I am compiling with

@ubuntu:~/scripts/jpegoptim$ ./configure CPPFLAGS=-I/opt/mozjpeg/include LDFLAGS=-L/opt/mozjpeg/lib64 --with-libjpeg=/opt/mozjpeg/lib64
@ubuntu:~/scripts/jpegoptim$ make
gcc  -g -O2 -I/opt/mozjpeg/include -I/opt/mozjpeg/lib64 -DHAVE_CONFIG_H   -c -o jpegoptim.o jpegoptim.c

gcc  -g -O2 -I/opt/mozjpeg/include -I/opt/mozjpeg/lib64 -DHAVE_CONFIG_H   -c -o jpegdest.o jpegdest.c
gcc  -g -O2 -I/opt/mozjpeg/include -I/opt/mozjpeg/lib64 -DHAVE_CONFIG_H   -c -o misc.o misc.c
gcc  -g -O2 -I/opt/mozjpeg/include -I/opt/mozjpeg/lib64 -DHAVE_CONFIG_H -o jpegoptim jpegoptim.o jpegdest.o misc.o  -L/opt/mozjpeg/lib64 -L/opt/mozjpeg/lib64 -lm -ljpeg

Output is:

ubuntu:~/scripts/jpegoptim$  /usr/local/bin/jpegoptim -V
jpegoptim v1.4.6  x86_64-unknown-linux-gnu
Copyright (C) 1996-2018, Timo Kokkonen

This program comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistirbute it under certain conditions.
See the GNU General Public License for more details.

libjpeg version: 6b  27-Mar-1998
Copyright (C) 1991-2018 The libjpeg-turbo Project and many others
dvershinin commented 4 years ago

@adrianmihalko the output of jpegoptim -V won't be illustrative, because libjpeg version: 6b 27-Mar-1998 will be there irrespective of using the standard library or MozJPEG.

The best way to check if you have successfully compiled MozJPEG in is with ldd /path/to/jpegoptim | grep jpeg. If the output is empty, it means the library was linked in statically.

adrianmihalko commented 4 years ago

@adrianmihalko the output of jpegoptim -V won't be illustrative, because libjpeg version: 6b 27-Mar-1998 will be there irrespective of using the standard library or MozJPEG.

The best way to check if you have successfully compiled MozJPEG in is with ldd /path/to/jpegoptim | grep jpeg. If the output is empty, it means the library was linked in statically.

Thank you, based on this I think mozjpeg is enabled:

$ ldd /usr/local/bin/jpegoptim | grep jpeg libjpeg.so.62 => /opt/mozjpeg/lib64/libjpeg.so.62 (0x00007f7ee9b70000)

ozcoder commented 4 years ago

If you want some more detail

$ ldd /usr/local/bin/jpegoptim | grep -i jpeg | tr -d "\n" | cut -d">" -f2 | cut -d' ' -f2 | xargs -i{} strings {} | grep mozjpeg

mozjpeg version 4.0.0 (build 20191127)
adrianmihalko commented 4 years ago

I think after 3 years I can close this issue. Thanks for tips.