Skip to content

Commit

Permalink
Switch to std::filesystem, enable directory support by default
Browse files Browse the repository at this point in the history
  • Loading branch information
fhanau committed Aug 2, 2022
1 parent 89d5622 commit 4d15aa8
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 77 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ make

In addition, you can add the following arguments to the cmake call to turn various features on and off:
- `-DECT_MULTITHREADING=OFF`: Turn off multithreading support
- `-DECT_FOLDER_SUPPORT=ON`: Turn on the ability to recursively search folders (requires [boost::filesystem](https://www.boostcpp.org/))

### With Xcode
You can use cmake to generate an Xcode project. Just add `-G Xcode` to the end of the cmake command:
Expand Down
4 changes: 0 additions & 4 deletions doc/Building.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@ cd src/
make deps
Then, open the Xcode Project and compile the program.

If you want Support for optimizing all files in a folder, you need to link the Boost.Filesystem and Boost.System libraries (boost.org) and define BOOST_SUPPORTED in main.cpp.
This is already done in the Xcode project. In both cases you need to have Boost installed.

(For MP3 Support (Images in MP3 files are recompressed), define MP3_SUPPORTED in main.c and link id3lib(id3lib.sourceforge.net).) This code has been disabled as metadata might get corrupted.

Dependencies:
CMake
Make
NASM
Boost (optional)

Tested Operating Systems and Compilers:
Debian Wheezy (GCC)
Expand Down
23 changes: 0 additions & 23 deletions licenses/LICENSE_1_0.txt

This file was deleted.

3 changes: 0 additions & 3 deletions licenses/Licenses.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
Boost (boost.org) is (C) Beman Dawes, David Abrahams, 1998-2005 and (C) Rene Rivera 2004-2007 and licensed under the Boost Software License, see licenses/LICENSE_1_0.txt.
Boost Usage is optional and Boost code is not included in this project. Binaries using Boost will print „Folder Support enabled“.

Mozjpeg (https://github.com/mozilla/mozjpeg/) is copyrighted by the Mozilla Corporation and licensed under the Mozjpeg license (see licenses/LICENCE.txt).
Zlib (http://www.zlib.net/) is (C) 1995-2013 Jean-loup Gailly and Mark Adler and licensed under the Zlib license, see src/zlib/zlib.h and includes changes by cloudflare (https://github.com/cloudflare/zlib).
Libpng (www.libpng.org/pub/png/libpng.html) is licensed under the PNG license in src/libpng/png.h and (C) Glenn Randers-Pehrson.
Expand Down
14 changes: 3 additions & 11 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(ect VERSION 0.9.1 LANGUAGES CXX C)

include(GNUInstallDirs)
set (CMAKE_CXX_STANDARD 11)

#Note that C++11 is still supported, but using it will cause the directory support to be disabled.
set (CMAKE_CXX_STANDARD 17)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
Expand Down Expand Up @@ -115,7 +117,6 @@ add_definitions(-DZLIB_CONST)

option(ECT_MULTITHREADING "Enable multithreaded processing support" ON)
option(ECT_MP3_SUPPORT "Enable MP3 support (not currently working)" OFF)
option(ECT_FOLDER_SUPPORT "Enable folder support (requires Boost)" OFF)

add_subdirectory(leanify EXCLUDE_FROM_ALL)
add_subdirectory(lodepng EXCLUDE_FROM_ALL)
Expand Down Expand Up @@ -158,13 +159,4 @@ if(ECT_MP3_SUPPORT)
MP3_SUPPORTED=1)
endif()

if(ECT_FOLDER_SUPPORT)
find_package(Boost 1.56 REQUIRED COMPONENTS filesystem)
target_link_libraries(ect
Boost::filesystem)
target_compile_definitions(ect
PRIVATE
BOOST_SUPPORTED=1)
endif()

install(TARGETS ect RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
12 changes: 6 additions & 6 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CC = gcc
CXX = g++
UCFLAGS = -Ofast -std=gnu11 -fsigned-char $(CFLAGS)
UCXXFLAGS = -pthread -Ofast -std=gnu++11 -fsigned-char $(CXXFLAGS)
UCXXFLAGS = -pthread -Ofast -std=gnu++17 -fsigned-char $(CXXFLAGS)
PREFIX ?= /usr/local
BINPREFIX ?= ..
BINDIR ?= $(PREFIX)/bin
Expand All @@ -13,7 +13,7 @@ ifeq ($(OS),Windows_NT)
CXXFLAGS += -mno-ms-bitfields
CMAKE += -G "MSYS Makefiles"
endif
OBJECTS = blocksplitter.o image.o lz77.o opngreduc.o squeeze.o util.o LzFind.o miniz.o
OBJECTS = blocksplitter.o image.o lz77.o opngreduc.o squeeze.o util.o LzFind.o miniz.o transupp.o
CXXSRC = support.cpp zopflipng.cpp zopfli/deflate.cpp zopfli/zopfli_gzip.cpp zopfli/katajainen.cpp \
lodepng/lodepng.cpp lodepng/lodepng_util.cpp optipng/codec.cpp optipng/optipng.cpp jpegtran.cpp gztools.cpp \
leanify/zip.cpp leanify/leanify.cpp
Expand All @@ -23,16 +23,16 @@ all: deps bin

bin: deps
$(CC) -c $(UCFLAGS) optipng/image.c zopfli//util.c zopfli/squeeze.c zopfli/lz77.c \
zopfli/blocksplitter.c optipng/opngreduc/opngreduc.c LzFind.c miniz/miniz.c
$(CXX) $(UCXXFLAGS) main.cpp $(OBJECTS) $(CXXSRC) mozjpeg/libjpeg.a libpng/libpng.a libz.a -o ${BINPREFIX}/ect $(LDFLAGS)
zopfli/blocksplitter.c optipng/opngreduc/opngreduc.c LzFind.c miniz/miniz.c mozjpeg/transupp.c
$(CXX) $(UCXXFLAGS) main.cpp libz.a $(OBJECTS) $(CXXSRC) mozjpeg/libjpeg.a libpng/libpng.a -o ${BINPREFIX}/ect $(LDFLAGS)
clean:
rm -f *.o *.a zlib/*.o libpng/*.o libpng/*.a libpng/pngusr.h libpng/pnglibconf.h
make -C mozjpeg clean
deps: libz.a libpng mozjpeg
libz.a:
cd zlib/; \
$(CC) $(UCFLAGS) -c adler32.c crc32.c deflate.c inffast.c inflate.c inftrees.c trees.c zutil.c gzlib.c gzread.c; \
ar rcs ../libz.a adler32.o crc32.o deflate.o inffast.o inflate.o inftrees.o trees.o zutil.o gzlib.o gzread.o
$(CC) $(UCFLAGS) -c adler32.c crc32.c deflate.c inffast.c inflate.c inftrees.c trees.c zutil.c gzlib.c gzread.c inffast_chunk.c adler32_simd.c crc32_simd.c; \
ar rcs ../libz.a adler32.o crc32.o deflate.o inffast.o inflate.o inftrees.o trees.o zutil.o gzlib.o gzread.o inffast_chunk.o adler32_simd.o crc32_simd.o
libpng:
cp pngusr.h libpng/pngusr.h
make -C libpng/ -f scripts/makefile.linux-opt CC="$(CC)" CFLAGS="$(UCFLAGS) -DPNG_USER_CONFIG -Wno-macro-redefined" libpng.a
Expand Down
40 changes: 20 additions & 20 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ static void Usage() {
" compiled on %s\n"
#endif
"Folder support "
#ifdef BOOST_SUPPORTED
#ifdef FS_SUPPORTED
"enabled\n"
#else
"disabled\n"
#endif

"Losslessly optimizes GZIP, ZIP, JPEG and PNG images\n"
"Usage: ECT [Options] Files"
#ifdef BOOST_SUPPORTED
#ifdef FS_SUPPORTED
"/Folders"
#endif
"...\n"
Expand All @@ -54,7 +54,7 @@ static void Usage() {
" -progressive Use progressive encoding for JPEGs\n"
" -autorotate Automatically rotate JPEGs, when perfectly transformable\n"
" -autorotate=force Automatically rotate JPEGs, dropping non-transformable edge blocks\n"
#ifdef BOOST_SUPPORTED
#ifdef FS_SUPPORTED
" -recurse Recursively search directories\n"
#endif
" -zip Compress file(s) with ZIP algorithm\n"
Expand Down Expand Up @@ -258,7 +258,7 @@ static unsigned char OptimizeJPEG(const char * Infile, const ECTOptions& Options
}

#ifdef MP3_SUPPORTED
#error MP3 code may corrupt metadata.
#error MP3 code may corrupt metadata and has been disabled.
static void OptimizeMP3(const char * Infile, const ECTOptions& Options){
ID3_Tag orig (Infile);
size_t start = orig.Size();
Expand Down Expand Up @@ -368,8 +368,8 @@ unsigned zipHandler(std::vector<int> args, const char * argv[], int files, const
else{
//Construct name
if(!isDirectory(argv[args[0]])
#ifdef BOOST_SUPPORTED
&& boost::filesystem::is_regular_file(argv[args[0]])
#ifdef FS_SUPPORTED
&& std::filesystem::is_regular_file(argv[args[0]])
#endif
){
if(zipfilename.find_last_of(".") > zipfilename.find_last_of("/\\")) {
Expand All @@ -390,12 +390,12 @@ unsigned zipHandler(std::vector<int> args, const char * argv[], int files, const
int error = 0;
for(; error == 0 && i < files; i++){
if(isDirectory(argv[args[i]])){
#ifdef BOOST_SUPPORTED
std::string fold = boost::filesystem::canonical(argv[args[i]]).string();
int substr = boost::filesystem::path(fold).has_parent_path() ? boost::filesystem::path(fold).parent_path().string().length() + 1 : 0;
#ifdef FS_SUPPORTED
std::string fold = std::filesystem::canonical(argv[args[i]]).string();
int substr = std::filesystem::path(fold).has_parent_path() ? std::filesystem::path(fold).parent_path().string().length() + 1 : 0;

boost::filesystem::recursive_directory_iterator a(fold), b;
std::vector<boost::filesystem::path> paths(a, b);
std::filesystem::recursive_directory_iterator a(fold), b;
std::vector<std::filesystem::path> paths(a, b);
for(unsigned j = 0; j < paths.size(); j++){
std::string newfile = paths[j].string();
const char* name = newfile.erase(0, substr).c_str();
Expand Down Expand Up @@ -515,7 +515,7 @@ int main(int argc, const char * argv[]) {
Options.Progressive = false;
Options.Autorotate = 0;
Options.Mode = 3;
#ifdef BOOST_SUPPORTED
#ifdef FS_SUPPORTED
Options.Recurse = false;
#endif
Options.PNG_ACTIVE = true;
Expand Down Expand Up @@ -560,7 +560,7 @@ int main(int argc, const char * argv[]) {
else if (strncmp(argv[i], "-keep", strlen) == 0) {Options.keep = true;}
else if (strcmp(argv[i], "--disable-jpeg") == 0 || strcmp(argv[i], "--disable-jpg") == 0 ){Options.JPEG_ACTIVE = false;}
else if (strcmp(argv[i], "--disable-png") == 0){Options.PNG_ACTIVE = false;}
#ifdef BOOST_SUPPORTED
#ifdef FS_SUPPORTED
else if (strncmp(argv[i], "-recurse", strlen) == 0) {Options.Recurse = 1;}
#endif
else if (strcmp(argv[i], "--strict") == 0) {Options.Strict = true;}
Expand Down Expand Up @@ -609,20 +609,20 @@ int main(int argc, const char * argv[]) {
else {
std::vector<std::string> fileList;
for (int j = 0; j < files; j++){
#ifdef BOOST_SUPPORTED
if (boost::filesystem::is_regular_file(argv[args[j]])){
#ifdef FS_SUPPORTED
if (std::filesystem::is_regular_file(argv[args[j]])){
fileList.push_back(argv[args[j]]);
}
else if (boost::filesystem::is_directory(argv[args[j]])){
if(Options.Recurse){boost::filesystem::recursive_directory_iterator a(argv[args[j]]), b;
std::vector<boost::filesystem::path> paths(a, b);
else if (std::filesystem::is_directory(argv[args[j]])){
if(Options.Recurse){std::filesystem::recursive_directory_iterator a(argv[args[j]]), b;
std::vector<std::filesystem::path> paths(a, b);
for(unsigned i = 0; i < paths.size(); i++){
fileList.push_back(paths[i].string());
}
}
else{
boost::filesystem::directory_iterator a(argv[args[j]]), b;
std::vector<boost::filesystem::path> paths(a, b);
std::filesystem::directory_iterator a(argv[args[j]]), b;
std::vector<std::filesystem::path> paths(a, b);
for(unsigned i = 0; i < paths.size(); i++){
fileList.push_back(paths[i].string());
}
Expand Down
12 changes: 3 additions & 9 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@

#include "gztools.h"

//Compile support for folder input. Requires linking of Boost::filesystem and Boost::system.
//TODO: Native filesystem support is missing from e.g. Xcode, but this will be the preferable solution eventually
/*#if __cplusplus >= 201703L
//Compile support for folder input. Requires std::filesystem introduced in C++17.
#if __cplusplus >= 201703L
#define FS_SUPPORTED
#include <filesystem>
//#include <experimental/filesystem>
using std::fs;
#else*/
#ifdef BOOST_SUPPORTED
#include <boost/filesystem.hpp>
#endif

struct ECTOptions{
Expand All @@ -42,7 +36,7 @@ struct ECTOptions{
bool Allfilters;
bool Allfiltersbrute;
bool Allfilterscheap;
#ifdef BOOST_SUPPORTED
#ifdef FS_SUPPORTED
bool Recurse;
#endif
unsigned DeflateMultithreading;
Expand Down

0 comments on commit 4d15aa8

Please sign in to comment.