DanBloomberg / leptonica

Leptonica is an open source library containing software that is broadly useful for image processing and image analysis applications. The official github repository for Leptonica is: danbloomberg/leptonica. See leptonica.org for more documentation.
Other
1.74k stars 387 forks source link

Disable all image I/O except for PNM files? #678

Open fschutt opened 1 year ago

fschutt commented 1 year ago

Hi,

I use leptonica with tesseract and I'd like to link everything statically. I use the tesseract library from the API, so I can pass in all files as .pnm (very simple file format). To make compilation simpler and to disable possible "DLL not found" errors, I'd like to disable every dependency such as dependencies on libjpeg, libpng, libgif, etc.

Currently I build leptonica.a with:

mkdir build && cd build

cmake ..
cmake --build . --config Release --target install 

... which links to libjpeg, etc. I try to supress this, but I don't know which flags to use. I'd like to only have PNM support (which I suppose is built-in, so no extra libs necessary for encoding / decoding images).

$ cmake .. -DHAVE_LIBJPEG=0 -DHAVE_LIBPNG=0 -DHAVE_LIBTIFF=0 -DHAVE_LIBWEBP=0 -DHAVE_LIBWEBP_ANIM=0 -DHAVE_LIBZ=0 -DHAVE_LIBJP2K=0 

CMAKE_BUILD_TYPE = Release
-- 
-- General configuration for Leptonica 1.83.0
-- --------------------------------------------------------
-- Build type: Release
-- Compiler: AppleClang
-- C compiler options:  
-- Linker options:  
-- Install directory: /usr/local
-- 
-- Build with sw [SW_BUILD]: OFF
-- Build utility programs [BUILD_PROG]: OFF
-- Used ZLIB library: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/lib/libz.tbd
-- Used PNG library:  /opt/homebrew/lib/libpng.dylib;/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/usr/lib/libz.tbd
-- Used JPEG library: /opt/homebrew/lib/libjpeg.dylib
-- Used JP2K library: openjp2
-- Used TIFF library: /opt/homebrew/lib/libtiff.dylib
-- Used GIF library:  /opt/homebrew/lib/libgif.dylib
-- Used WEBP library: /opt/homebrew/lib/libwebp.dylib;/opt/homebrew/lib/libwebpmux.dylib
-- --------------------------------------------------------
-- 
-- Configuring done
-- Generating done
[100%] Built target leptonica
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/lib/pkgconfig/lept.pc
-- Installing: /usr/local/lib/cmake/leptonica/LeptonicaConfig.cmake
-- Installing: /usr/local/lib/cmake/leptonica/LeptonicaConfig-version.cmake
-- Installing: /usr/local/lib/libleptonica.a
-- Installing: /usr/local/lib/cmake/leptonica/LeptonicaTargets.cmake
-- Installing: /usr/local/lib/cmake/leptonica/LeptonicaTargets-release.cmake
-- Installing: /usr/local/include/leptonica/allheaders.h
-- Installing: /usr/local/include/leptonica/alltypes.h
-- Installing: /usr/local/include/leptonica/array.h
-- Installing: /usr/local/include/leptonica/array_internal.h
-- Installing: /usr/local/include/leptonica/arrayaccess.h
-- Installing: /usr/local/include/leptonica/bbuffer.h
-- Installing: /usr/local/include/leptonica/bilateral.h
-- Installing: /usr/local/include/leptonica/bmf.h
-- Installing: /usr/local/include/leptonica/bmfdata.h
-- Installing: /usr/local/include/leptonica/bmp.h
-- Installing: /usr/local/include/leptonica/ccbord.h
-- Installing: /usr/local/include/leptonica/ccbord_internal.h
-- Installing: /usr/local/include/leptonica/colorfill.h
-- Installing: /usr/local/include/leptonica/dewarp.h
-- Installing: /usr/local/include/leptonica/environ.h
-- Installing: /usr/local/include/leptonica/gplot.h
-- Installing: /usr/local/include/leptonica/hashmap.h
-- Installing: /usr/local/include/leptonica/heap.h
-- Installing: /usr/local/include/leptonica/imageio.h
-- Installing: /usr/local/include/leptonica/jbclass.h
-- Installing: /usr/local/include/leptonica/leptwin.h
-- Installing: /usr/local/include/leptonica/list.h
-- Installing: /usr/local/include/leptonica/morph.h
-- Installing: /usr/local/include/leptonica/pix.h
-- Installing: /usr/local/include/leptonica/pix_internal.h
-- Installing: /usr/local/include/leptonica/ptra.h
-- Installing: /usr/local/include/leptonica/queue.h
-- Installing: /usr/local/include/leptonica/rbtree.h
-- Installing: /usr/local/include/leptonica/readbarcode.h
-- Installing: /usr/local/include/leptonica/recog.h
-- Installing: /usr/local/include/leptonica/regutils.h
-- Installing: /usr/local/include/leptonica/stack.h
-- Installing: /usr/local/include/leptonica/stringcode.h
-- Installing: /usr/local/include/leptonica/sudoku.h
-- Installing: /usr/local/include/leptonica/watershed.h
-- Installing: /usr/local/include/leptonica/endianness.h

However, when I then build tesseract, it complains about libjpeg not being found. Is there a flag for disabling all these unnecessary image formats?

zdenop commented 1 year ago

At the moment you can disable only webp (with -DLIBWEBP_SUPPORT=OFF) and openjpeg (-DOPENJPEG_SUPPORT=OFF).

Just for notice: jpeg is (optional) dependency for tiff, so error could come also from this source especially if you are making static build.

fschutt commented 1 year ago

I eventually managed to disable everything by editing the source code, especially environ.h and the Makefile (to get rid of all mentions of libjpeg, etc.). The resulting library was about 4MB for leptonica and 3MB for tesseract. It would be great if the Makefile could be edited so that it's easier to create a "headless" build.

DanBloomberg commented 1 year ago

For leptonica, a simple way to make your "headless" build is to edit only environ.h and use the static makefile, as described in the README.html.

fschutt commented 1 year ago

@DanBloomberg Yeah I found it, but I'm just suggesting making those environ.h flags configurable from the Makefile, so that it's easier to enable / disable without using sed (when using automated scripts / CI, etc.).

zdenop commented 1 year ago

I plan to adjust cmake configuration, so you can choose which dependencies should be compiled.

fschutt commented 1 year ago

@zdenop I also had to modify cmake/Configure.cmake and prog/makefile.static to completely disable linking to libpng, libtiff, libjpeg and libz: https://github.com/fschutt/tesseract-static-rs/blob/e5fffdf9fc1f5592d304ceab1237f8051535994b/build.rs#L123-L199

DanBloomberg commented 1 year ago

As you demonstrate, in addition to editing environ.h, you also need to modify the ALL_LIBS variable in prog/makefile.static. Nice use of rust!

zdenop commented 1 year ago

please try with current master: cmake -Bbuild -DBUILD_PROG=OFF -DSW_BUILD=OFF -DBUILD_SHARED_LIBS=ON -DENABLE_ZLIB=OFF -DENABLE_PNG=OFF -DENABLE_GIF=OFF -DENABLE_JPEG=OFF -DENABLE_TIFF=OFF -DENABLE_WEBP=OFF -DENABLE_OPENJPEG=OFF