diff --git a/.gitignore b/.gitignore index 89626f0..92690bd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,28 @@ -/*.o -/*.d +*.o +.deps +.dirstamp /*.S -/test -/hiptext +/*.a +/*.d +/*.plist +/*.swp +/*.tar.gz +/Makefile +/Makefile.in +/aclocal.m4 +/autom4te.cache +/config.* +/config/compile +/config/config.* +/config/depcomp +/config/install-sh +/config/missing +/config/test-driver +/configure /cpplint.py /gtest/src/*.o -/*.swp -/*.plist +/hiptext +/hiptext_test* +/src/pixel_parse.cc +/stamp-h1 +/test-suite.log diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..38bec7b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,35 @@ +language: cpp + +dist: trusty +addons: + apt: + packages: + - libavcodec-dev + - libavformat-dev + - libfreetype6-dev + - libgflags-dev + - libgoogle-glog-dev + - libjpeg-dev + - libpng12-dev + - libswscale-dev + - ragel + +compiler: + - clang + - gcc + +script: + - ./autogen.sh + - ./configure --disable-silent-rules + - make -j2 hiptext hiptext_test + - ./hiptext_test + - make check + - make dist + - sudo apt-get remove -y ragel + - tar xvzf hiptext-?.?.tar.gz + - cd hiptext-?.? + - ./configure + - make -j2 + - sudo make install + - hiptext --width=80 --nocolor balls.png + - hiptext --width=80 --xterm256 balls.png diff --git a/Makefile b/Makefile deleted file mode 100644 index 89adbcf..0000000 --- a/Makefile +++ /dev/null @@ -1,90 +0,0 @@ -# hiptext - Image to Text Converter -# Copyright (c) 2013 Justine Tunney - -# Example invocations: -# - make # Bring hiptext to life. -# - make check # Run unit tests. -# - sudo make install # Allow hiptext to stay forever :) -# - sudo make uninstall # Kick hiptext out of your house :( -# - make clean # Delete all generated files. -# - make lint # Check for C++ style errors. -# - make -pn | less # View implicit make rules and variables. -# -# Create an optimized build: -# CXXFLAGS="-O3 -flto -DNDEBUG" make - -LINK.o = $(LINK.cc) -PREFIX ?= /usr/local -TARGET_ARCH ?= -march=native -CXXFLAGS ?= -g -O3 -CXXFLAGS += -std=c++11 -Wall -Wextra -CXXFLAGS += -fno-exceptions -fno-rtti -LDLIBS += -lm -lglog -lgflags -lpng -ljpeg -LDLIBS += -lavcodec -lavformat -lavutil -lswscale -LDLIBS += $(shell freetype-config --libs) - -SOURCES = \ - artiste.o \ - charquantizer.o \ - font.o \ - graphic.o \ - jpeg.o \ - macterm.o \ - movie.o \ - pixel.o \ - pixel_parse.o \ - png.o \ - termprinter.o \ - sixelprinter.o \ - unicode.o \ - xterm256.o - -all: hiptext -hiptext: hiptext.o $(SOURCES) - -.PHONY: check clean install uninstall lint - -check: test - ./test --alsologtostderr --gtest_color=yes - -clean: - $(RM) test hiptext $(wildcard *.o *.d *.S $(GTEST_DIR)/*.o) cpplint.py - -install: hiptext - install --mode=0755 hiptext $(PREFIX)/bin - -uninstall: - $(RM) $(PREFIX)/bin/hiptext - -cpplint.py: - wget -O $@ https://google-styleguide.googlecode.com/svn/trunk/cpplint/cpplint.py - -lint: cpplint.py - python cpplint.py $(wildcard *.cc hiptext/*.h) \ - 2>&1 | grep -v 'termprinter\.cc:.*non-const' \ - | grep -v 'readability/streams' \ - | grep -v 'build/include' \ - | grep -v 'build/header_guard' \ - | grep -v 'legal/copyright' \ - | grep -v 'Found C system header after' \ - | grep -v 'runtime/references' \ - | grep -v 'whitespace/parens' - -%.cc: %.rl - ragel -o $@ $< - -# Flag overrides for individual targets. -pixel_parse.o: CXXFLAGS := $(filter-out -MD,$(CXXFLAGS)) -font.%: CXXFLAGS += $(shell freetype-config --cflags) - -# google-test integration magic. -GTEST_DIR ?= gtest -TESTS = $(GTEST_DIR)/src/gtest-all.o $(GTEST_DIR)/src/gtest_main.o \ - $(patsubst %.cc,%.o,$(wildcard *_test.cc)) -$(TESTS): CXXFLAGS += -I$(GTEST_DIR)/include -I$(GTEST_DIR) -pthread -$(filter gtest%,$(TESTS)): CXXFLAGS := $(filter-out -MD -Wall,$(CXXFLAGS)) -test: $(TESTS) $(SOURCES) ; $(LINK.cc) $^ $(LDLIBS) -lpthread -o $@ - -# Recompile sources when headers change. -CXXFLAGS += -MD --include $(wildcard *.d) diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..a2e422f --- /dev/null +++ b/Makefile.am @@ -0,0 +1,120 @@ +AUTOMAKE_OPTIONS = subdir-objects +ACLOCAL_AMFLAGS = -I config + +EXTRA_DIST = \ + DejaVuSansMono.ttf \ + README.md \ + autogen.sh \ + balls.png \ + obama.jpg \ + $(shell find gtest) + +bin_PROGRAMS = hiptext +noinst_LIBRARIES = libhiptext.a libgtest.a + +################################################################################ +## libhiptext.a + +libhiptext_a_SOURCES = \ + src/artiste.cc \ + src/charquantizer.cc \ + src/font.cc \ + src/graphic.cc \ + src/hiptext.cc \ + src/hiptext/artiste.h \ + src/hiptext/charquantizer.h \ + src/hiptext/font.h \ + src/hiptext/graphic.h \ + src/hiptext/jpeg.h \ + src/hiptext/macterm.h \ + src/hiptext/movie.h \ + src/hiptext/pixel.h \ + src/hiptext/png.h \ + src/hiptext/sixelprinter.h \ + src/hiptext/termprinter.h \ + src/hiptext/unicode.h \ + src/hiptext/unused.h \ + src/hiptext/xterm256.h \ + src/jpeg.cc \ + src/macterm.cc \ + src/movie.cc \ + src/pixel.cc \ + src/pixel_parse.cc \ + src/pixel_parse.rl \ + src/png.cc \ + src/sixelprinter.cc \ + src/termprinter.cc \ + src/unicode.cc \ + src/xterm256.cc + +libhiptext_a_CPPFLAGS = \ + -Isrc \ + $(LIBAVCODEC_CFLAGS) \ + $(LIBAVFORMAT_CFLAGS) \ + $(LIBAVUTIL_CFLAGS) \ + $(LIBFREETYPE_CFLAGS) \ + $(LIBGFLAGS_CFLAGS) \ + $(LIBGLOG_CFLAGS) \ + $(LIBPNG_CFLAGS) \ + $(LIBSWSCALE_CFLAGS) + +src/pixel_parse.cc: src/pixel_parse.rl + +################################################################################ +## hiptext + +hiptext_SOURCES = src/hiptext.cc + +hiptext_CPPFLAGS = $(libhiptext_a_CPPFLAGS) + +hiptext_LDADD = \ + libhiptext.a \ + -ljpeg \ + $(LIBAVCODEC_LIBS) \ + $(LIBAVFORMAT_LIBS) \ + $(LIBAVUTIL_LIBS) \ + $(LIBFREETYPE_LIBS) \ + $(LIBGFLAGS_LIBS) \ + $(LIBGLOG_LIBS) \ + $(LIBPNG_LIBS) \ + $(LIBSWSCALE_LIBS) + +################################################################################ +## libgtest + +libgtest_a_SOURCES = \ + gtest/src/gtest-all.cc + +libgtest_a_CPPFLAGS = \ + $(PTHREAD_CFLAGS) \ + -Igtest \ + -Igtest/include + +################################################################################ +## test + +check_PROGRAMS = hiptext_test +TESTS = $(check_PROGRAMS) + +hiptext_test_SOURCES = \ + test/pixel_test.cc \ + test/xterm256_test.cc \ + test/test.cc + +hiptext_test_CPPFLAGS = \ + $(hiptext_CPPFLAGS) \ + $(PTHREAD_CFLAGS) \ + -Igtest/include + +hiptext_test_LDADD = \ + libgtest.a \ + $(hiptext_LDADD) \ + $(PTHREAD_LIBS) \ + $(PTHREAD_CFLAGS) # XXX: Not sure why this is needed. + +################################################################################ +## miscellaneous + +src/%.cc: src/%.rl + @echo " RL " $@; + @$(RAGEL) -G2 -o $@ $< diff --git a/README.md b/README.md index 51abde1..a918282 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# hiptext +# hiptext [![Build Status](https://travis-ci.org/jart/hiptext.svg?branch=master)](https://travis-ci.org/jart/hiptext) hiptext is command line tool for rendering images and videos inside terminals. @@ -13,12 +13,13 @@ hiptext is command line tool for rendering images and videos inside terminals. You need clang or gcc >=4.7. You also need to install: sudo apt-get install build-essential libpng12-dev libjpeg-dev \ - libfreetype6-dev libgif-dev ragel libavformat-dev libavcodec-dev \ + libfreetype6-dev libavformat-dev libavcodec-dev \ libswscale-dev libgflags-dev libgoogle-glog-dev ## Installation - make + ./configure + make -j4 sudo make install ## Usage @@ -106,7 +107,3 @@ not the case, you can specify your background color using a CSS or X11 color string. hiptext --bg=white balls.png - -## Testing - - make test && ./test diff --git a/autogen.sh b/autogen.sh new file mode 100755 index 0000000..f942ab2 --- /dev/null +++ b/autogen.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -ex +aclocal --install --force -I config +autoreconf --install --force --verbose -I config diff --git a/config/ax_cxx_compile_stdcxx.m4 b/config/ax_cxx_compile_stdcxx.m4 new file mode 100644 index 0000000..2c18e49 --- /dev/null +++ b/config/ax_cxx_compile_stdcxx.m4 @@ -0,0 +1,562 @@ +# =========================================================================== +# http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html +# =========================================================================== +# +# SYNOPSIS +# +# AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional]) +# +# DESCRIPTION +# +# Check for baseline language coverage in the compiler for the specified +# version of the C++ standard. If necessary, add switches to CXX and +# CXXCPP to enable support. VERSION may be '11' (for the C++11 standard) +# or '14' (for the C++14 standard). +# +# The second argument, if specified, indicates whether you insist on an +# extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g. +# -std=c++11). If neither is specified, you get whatever works, with +# preference for an extended mode. +# +# The third argument, if specified 'mandatory' or if left unspecified, +# indicates that baseline support for the specified C++ standard is +# required and that the macro should error out if no mode with that +# support is found. If specified 'optional', then configuration proceeds +# regardless, after defining HAVE_CXX${VERSION} if and only if a +# supporting mode is found. +# +# LICENSE +# +# Copyright (c) 2008 Benjamin Kosnik +# Copyright (c) 2012 Zack Weinberg +# Copyright (c) 2013 Roy Stogner +# Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov +# Copyright (c) 2015 Paul Norman +# Copyright (c) 2015 Moritz Klammler +# +# Copying and distribution of this file, with or without modification, are +# permitted in any medium without royalty provided the copyright notice +# and this notice are preserved. This file is offered as-is, without any +# warranty. + +#serial 4 + +dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro +dnl (serial version number 13). + +AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl + m4_if([$1], [11], [], + [$1], [14], [], + [$1], [17], [m4_fatal([support for C++17 not yet implemented in AX_CXX_COMPILE_STDCXX])], + [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl + m4_if([$2], [], [], + [$2], [ext], [], + [$2], [noext], [], + [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl + m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true], + [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true], + [$3], [optional], [ax_cxx_compile_cxx$1_required=false], + [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])]) + AC_LANG_PUSH([C++])dnl + ac_success=no + AC_CACHE_CHECK(whether $CXX supports C++$1 features by default, + ax_cv_cxx_compile_cxx$1, + [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])], + [ax_cv_cxx_compile_cxx$1=yes], + [ax_cv_cxx_compile_cxx$1=no])]) + if test x$ax_cv_cxx_compile_cxx$1 = xyes; then + ac_success=yes + fi + + m4_if([$2], [noext], [], [dnl + if test x$ac_success = xno; then + for switch in -std=gnu++$1 -std=gnu++0x; do + cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch]) + AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch, + $cachevar, + [ac_save_CXX="$CXX" + CXX="$CXX $switch" + AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])], + [eval $cachevar=yes], + [eval $cachevar=no]) + CXX="$ac_save_CXX"]) + if eval test x\$$cachevar = xyes; then + CXX="$CXX $switch" + if test -n "$CXXCPP" ; then + CXXCPP="$CXXCPP $switch" + fi + ac_success=yes + break + fi + done + fi]) + + m4_if([$2], [ext], [], [dnl + if test x$ac_success = xno; then + dnl HP's aCC needs +std=c++11 according to: + dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf + dnl Cray's crayCC needs "-h std=c++11" + for switch in -std=c++$1 -std=c++0x +std=c++$1 "-h std=c++$1"; do + cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch]) + AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch, + $cachevar, + [ac_save_CXX="$CXX" + CXX="$CXX $switch" + AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])], + [eval $cachevar=yes], + [eval $cachevar=no]) + CXX="$ac_save_CXX"]) + if eval test x\$$cachevar = xyes; then + CXX="$CXX $switch" + if test -n "$CXXCPP" ; then + CXXCPP="$CXXCPP $switch" + fi + ac_success=yes + break + fi + done + fi]) + AC_LANG_POP([C++]) + if test x$ax_cxx_compile_cxx$1_required = xtrue; then + if test x$ac_success = xno; then + AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.]) + fi + fi + if test x$ac_success = xno; then + HAVE_CXX$1=0 + AC_MSG_NOTICE([No compiler with C++$1 support was found]) + else + HAVE_CXX$1=1 + AC_DEFINE(HAVE_CXX$1,1, + [define if the compiler supports basic C++$1 syntax]) + fi + AC_SUBST(HAVE_CXX$1) +]) + + +dnl Test body for checking C++11 support + +m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11], + _AX_CXX_COMPILE_STDCXX_testbody_new_in_11 +) + + +dnl Test body for checking C++14 support + +m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14], + _AX_CXX_COMPILE_STDCXX_testbody_new_in_11 + _AX_CXX_COMPILE_STDCXX_testbody_new_in_14 +) + + +dnl Tests for new features in C++11 + +m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[ + +// If the compiler admits that it is not ready for C++11, why torture it? +// Hopefully, this will speed up the test. + +#ifndef __cplusplus + +#error "This is not a C++ compiler" + +#elif __cplusplus < 201103L + +#error "This is not a C++11 compiler" + +#else + +namespace cxx11 +{ + + namespace test_static_assert + { + + template + struct check + { + static_assert(sizeof(int) <= sizeof(T), "not big enough"); + }; + + } + + namespace test_final_override + { + + struct Base + { + virtual void f() {} + }; + + struct Derived : public Base + { + virtual void f() override {} + }; + + } + + namespace test_double_right_angle_brackets + { + + template < typename T > + struct check {}; + + typedef check single_type; + typedef check> double_type; + typedef check>> triple_type; + typedef check>>> quadruple_type; + + } + + namespace test_decltype + { + + int + f() + { + int a = 1; + decltype(a) b = 2; + return a + b; + } + + } + + namespace test_type_deduction + { + + template < typename T1, typename T2 > + struct is_same + { + static const bool value = false; + }; + + template < typename T > + struct is_same + { + static const bool value = true; + }; + + template < typename T1, typename T2 > + auto + add(T1 a1, T2 a2) -> decltype(a1 + a2) + { + return a1 + a2; + } + + int + test(const int c, volatile int v) + { + static_assert(is_same::value == true, ""); + static_assert(is_same::value == false, ""); + static_assert(is_same::value == false, ""); + auto ac = c; + auto av = v; + auto sumi = ac + av + 'x'; + auto sumf = ac + av + 1.0; + static_assert(is_same::value == true, ""); + static_assert(is_same::value == true, ""); + static_assert(is_same::value == true, ""); + static_assert(is_same::value == false, ""); + static_assert(is_same::value == true, ""); + return (sumf > 0.0) ? sumi : add(c, v); + } + + } + + namespace test_noexcept + { + + int f() { return 0; } + int g() noexcept { return 0; } + + static_assert(noexcept(f()) == false, ""); + static_assert(noexcept(g()) == true, ""); + + } + + namespace test_constexpr + { + + template < typename CharT > + unsigned long constexpr + strlen_c_r(const CharT *const s, const unsigned long acc) noexcept + { + return *s ? strlen_c_r(s + 1, acc + 1) : acc; + } + + template < typename CharT > + unsigned long constexpr + strlen_c(const CharT *const s) noexcept + { + return strlen_c_r(s, 0UL); + } + + static_assert(strlen_c("") == 0UL, ""); + static_assert(strlen_c("1") == 1UL, ""); + static_assert(strlen_c("example") == 7UL, ""); + static_assert(strlen_c("another\0example") == 7UL, ""); + + } + + namespace test_rvalue_references + { + + template < int N > + struct answer + { + static constexpr int value = N; + }; + + answer<1> f(int&) { return answer<1>(); } + answer<2> f(const int&) { return answer<2>(); } + answer<3> f(int&&) { return answer<3>(); } + + void + test() + { + int i = 0; + const int c = 0; + static_assert(decltype(f(i))::value == 1, ""); + static_assert(decltype(f(c))::value == 2, ""); + static_assert(decltype(f(0))::value == 3, ""); + } + + } + + namespace test_uniform_initialization + { + + struct test + { + static const int zero {}; + static const int one {1}; + }; + + static_assert(test::zero == 0, ""); + static_assert(test::one == 1, ""); + + } + + namespace test_lambdas + { + + void + test1() + { + auto lambda1 = [](){}; + auto lambda2 = lambda1; + lambda1(); + lambda2(); + } + + int + test2() + { + auto a = [](int i, int j){ return i + j; }(1, 2); + auto b = []() -> int { return '0'; }(); + auto c = [=](){ return a + b; }(); + auto d = [&](){ return c; }(); + auto e = [a, &b](int x) mutable { + const auto identity = [](int y){ return y; }; + for (auto i = 0; i < a; ++i) + a += b--; + return x + identity(a + b); + }(0); + return a + b + c + d + e; + } + + int + test3() + { + const auto nullary = [](){ return 0; }; + const auto unary = [](int x){ return x; }; + using nullary_t = decltype(nullary); + using unary_t = decltype(unary); + const auto higher1st = [](nullary_t f){ return f(); }; + const auto higher2nd = [unary](nullary_t f1){ + return [unary, f1](unary_t f2){ return f2(unary(f1())); }; + }; + return higher1st(nullary) + higher2nd(nullary)(unary); + } + + } + + namespace test_variadic_templates + { + + template + struct sum; + + template + struct sum + { + static constexpr auto value = N0 + sum::value; + }; + + template <> + struct sum<> + { + static constexpr auto value = 0; + }; + + static_assert(sum<>::value == 0, ""); + static_assert(sum<1>::value == 1, ""); + static_assert(sum<23>::value == 23, ""); + static_assert(sum<1, 2>::value == 3, ""); + static_assert(sum<5, 5, 11>::value == 21, ""); + static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, ""); + + } + + // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae + // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function + // because of this. + namespace test_template_alias_sfinae + { + + struct foo {}; + + template + using member = typename T::member_type; + + template + void func(...) {} + + template + void func(member*) {} + + void test(); + + void test() { func(0); } + + } + +} // namespace cxx11 + +#endif // __cplusplus >= 201103L + +]]) + + +dnl Tests for new features in C++14 + +m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[ + +// If the compiler admits that it is not ready for C++14, why torture it? +// Hopefully, this will speed up the test. + +#ifndef __cplusplus + +#error "This is not a C++ compiler" + +#elif __cplusplus < 201402L + +#error "This is not a C++14 compiler" + +#else + +namespace cxx14 +{ + + namespace test_polymorphic_lambdas + { + + int + test() + { + const auto lambda = [](auto&&... args){ + const auto istiny = [](auto x){ + return (sizeof(x) == 1UL) ? 1 : 0; + }; + const int aretiny[] = { istiny(args)... }; + return aretiny[0]; + }; + return lambda(1, 1L, 1.0f, '1'); + } + + } + + namespace test_binary_literals + { + + constexpr auto ivii = 0b0000000000101010; + static_assert(ivii == 42, "wrong value"); + + } + + namespace test_generalized_constexpr + { + + template < typename CharT > + constexpr unsigned long + strlen_c(const CharT *const s) noexcept + { + auto length = 0UL; + for (auto p = s; *p; ++p) + ++length; + return length; + } + + static_assert(strlen_c("") == 0UL, ""); + static_assert(strlen_c("x") == 1UL, ""); + static_assert(strlen_c("test") == 4UL, ""); + static_assert(strlen_c("another\0test") == 7UL, ""); + + } + + namespace test_lambda_init_capture + { + + int + test() + { + auto x = 0; + const auto lambda1 = [a = x](int b){ return a + b; }; + const auto lambda2 = [a = lambda1(x)](){ return a; }; + return lambda2(); + } + + } + + namespace test_digit_seperators + { + + constexpr auto ten_million = 100'000'000; + static_assert(ten_million == 100000000, ""); + + } + + namespace test_return_type_deduction + { + + auto f(int& x) { return x; } + decltype(auto) g(int& x) { return x; } + + template < typename T1, typename T2 > + struct is_same + { + static constexpr auto value = false; + }; + + template < typename T > + struct is_same + { + static constexpr auto value = true; + }; + + int + test() + { + auto x = 0; + static_assert(is_same::value, ""); + static_assert(is_same::value, ""); + return x; + } + + } + +} // namespace cxx14 + +#endif // __cplusplus >= 201402L + +]]) diff --git a/config/ax_pthread.m4 b/config/ax_pthread.m4 new file mode 100644 index 0000000..4c4051e --- /dev/null +++ b/config/ax_pthread.m4 @@ -0,0 +1,485 @@ +# =========================================================================== +# http://www.gnu.org/software/autoconf-archive/ax_pthread.html +# =========================================================================== +# +# SYNOPSIS +# +# AX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]]) +# +# DESCRIPTION +# +# This macro figures out how to build C programs using POSIX threads. It +# sets the PTHREAD_LIBS output variable to the threads library and linker +# flags, and the PTHREAD_CFLAGS output variable to any special C compiler +# flags that are needed. (The user can also force certain compiler +# flags/libs to be tested by setting these environment variables.) +# +# Also sets PTHREAD_CC to any special C compiler that is needed for +# multi-threaded programs (defaults to the value of CC otherwise). (This +# is necessary on AIX to use the special cc_r compiler alias.) +# +# NOTE: You are assumed to not only compile your program with these flags, +# but also to link with them as well. For example, you might link with +# $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS +# +# If you are only building threaded programs, you may wish to use these +# variables in your default LIBS, CFLAGS, and CC: +# +# LIBS="$PTHREAD_LIBS $LIBS" +# CFLAGS="$CFLAGS $PTHREAD_CFLAGS" +# CC="$PTHREAD_CC" +# +# In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant +# has a nonstandard name, this macro defines PTHREAD_CREATE_JOINABLE to +# that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX). +# +# Also HAVE_PTHREAD_PRIO_INHERIT is defined if pthread is found and the +# PTHREAD_PRIO_INHERIT symbol is defined when compiling with +# PTHREAD_CFLAGS. +# +# ACTION-IF-FOUND is a list of shell commands to run if a threads library +# is found, and ACTION-IF-NOT-FOUND is a list of commands to run it if it +# is not found. If ACTION-IF-FOUND is not specified, the default action +# will define HAVE_PTHREAD. +# +# Please let the authors know if this macro fails on any platform, or if +# you have any other suggestions or comments. This macro was based on work +# by SGJ on autoconf scripts for FFTW (http://www.fftw.org/) (with help +# from M. Frigo), as well as ac_pthread and hb_pthread macros posted by +# Alejandro Forero Cuervo to the autoconf macro repository. We are also +# grateful for the helpful feedback of numerous users. +# +# Updated for Autoconf 2.68 by Daniel Richard G. +# +# LICENSE +# +# Copyright (c) 2008 Steven G. Johnson +# Copyright (c) 2011 Daniel Richard G. +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation, either version 3 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General +# Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program. If not, see . +# +# As a special exception, the respective Autoconf Macro's copyright owner +# gives unlimited permission to copy, distribute and modify the configure +# scripts that are the output of Autoconf when processing the Macro. You +# need not follow the terms of the GNU General Public License when using +# or distributing such scripts, even though portions of the text of the +# Macro appear in them. The GNU General Public License (GPL) does govern +# all other use of the material that constitutes the Autoconf Macro. +# +# This special exception to the GPL applies to versions of the Autoconf +# Macro released by the Autoconf Archive. When you make and distribute a +# modified version of the Autoconf Macro, you may extend this special +# exception to the GPL to apply to your modified version as well. + +#serial 23 + +AU_ALIAS([ACX_PTHREAD], [AX_PTHREAD]) +AC_DEFUN([AX_PTHREAD], [ +AC_REQUIRE([AC_CANONICAL_HOST]) +AC_REQUIRE([AC_PROG_CC]) +AC_REQUIRE([AC_PROG_SED]) +AC_LANG_PUSH([C]) +ax_pthread_ok=no + +# We used to check for pthread.h first, but this fails if pthread.h +# requires special compiler flags (e.g. on Tru64 or Sequent). +# It gets checked for in the link test anyway. + +# First of all, check if the user has set any of the PTHREAD_LIBS, +# etcetera environment variables, and if threads linking works using +# them: +if test "x$PTHREAD_CFLAGS$PTHREAD_LIBS" != "x"; then + ax_pthread_save_CC="$CC" + ax_pthread_save_CFLAGS="$CFLAGS" + ax_pthread_save_LIBS="$LIBS" + AS_IF([test "x$PTHREAD_CC" != "x"], [CC="$PTHREAD_CC"]) + CFLAGS="$CFLAGS $PTHREAD_CFLAGS" + LIBS="$PTHREAD_LIBS $LIBS" + AC_MSG_CHECKING([for pthread_join using $CC $PTHREAD_CFLAGS $PTHREAD_LIBS]) + AC_LINK_IFELSE([AC_LANG_CALL([], [pthread_join])], [ax_pthread_ok=yes]) + AC_MSG_RESULT([$ax_pthread_ok]) + if test "x$ax_pthread_ok" = "xno"; then + PTHREAD_LIBS="" + PTHREAD_CFLAGS="" + fi + CC="$ax_pthread_save_CC" + CFLAGS="$ax_pthread_save_CFLAGS" + LIBS="$ax_pthread_save_LIBS" +fi + +# We must check for the threads library under a number of different +# names; the ordering is very important because some systems +# (e.g. DEC) have both -lpthread and -lpthreads, where one of the +# libraries is broken (non-POSIX). + +# Create a list of thread flags to try. Items starting with a "-" are +# C compiler flags, and other items are library names, except for "none" +# which indicates that we try without any flags at all, and "pthread-config" +# which is a program returning the flags for the Pth emulation library. + +ax_pthread_flags="pthreads none -Kthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config" + +# The ordering *is* (sometimes) important. Some notes on the +# individual items follow: + +# pthreads: AIX (must check this before -lpthread) +# none: in case threads are in libc; should be tried before -Kthread and +# other compiler flags to prevent continual compiler warnings +# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h) +# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads), Tru64 +# (Note: HP C rejects this with "bad form for `-t' option") +# -pthreads: Solaris/gcc (Note: HP C also rejects) +# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it +# doesn't hurt to check since this sometimes defines pthreads and +# -D_REENTRANT too), HP C (must be checked before -lpthread, which +# is present but should not be used directly; and before -mthreads, +# because the compiler interprets this as "-mt" + "-hreads") +# -mthreads: Mingw32/gcc, Lynx/gcc +# pthread: Linux, etcetera +# --thread-safe: KAI C++ +# pthread-config: use pthread-config program (for GNU Pth library) + +case $host_os in + + freebsd*) + + # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able) + # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread) + + ax_pthread_flags="-kthread lthread $ax_pthread_flags" + ;; + + hpux*) + + # From the cc(1) man page: "[-mt] Sets various -D flags to enable + # multi-threading and also sets -lpthread." + + ax_pthread_flags="-mt -pthread pthread $ax_pthread_flags" + ;; + + openedition*) + + # IBM z/OS requires a feature-test macro to be defined in order to + # enable POSIX threads at all, so give the user a hint if this is + # not set. (We don't define these ourselves, as they can affect + # other portions of the system API in unpredictable ways.) + + AC_EGREP_CPP([AX_PTHREAD_ZOS_MISSING], + [ +# if !defined(_OPEN_THREADS) && !defined(_UNIX03_THREADS) + AX_PTHREAD_ZOS_MISSING +# endif + ], + [AC_MSG_WARN([IBM z/OS requires -D_OPEN_THREADS or -D_UNIX03_THREADS to enable pthreads support.])]) + ;; + + solaris*) + + # On Solaris (at least, for some versions), libc contains stubbed + # (non-functional) versions of the pthreads routines, so link-based + # tests will erroneously succeed. (N.B.: The stubs are missing + # pthread_cleanup_push, or rather a function called by this macro, + # so we could check for that, but who knows whether they'll stub + # that too in a future libc.) So we'll check first for the + # standard Solaris way of linking pthreads (-mt -lpthread). + + ax_pthread_flags="-mt,pthread pthread $ax_pthread_flags" + ;; +esac + +# GCC generally uses -pthread, or -pthreads on some platforms (e.g. SPARC) + +AS_IF([test "x$GCC" = "xyes"], + [ax_pthread_flags="-pthread -pthreads $ax_pthread_flags"]) + +# The presence of a feature test macro requesting re-entrant function +# definitions is, on some systems, a strong hint that pthreads support is +# correctly enabled + +case $host_os in + darwin* | hpux* | linux* | osf* | solaris*) + ax_pthread_check_macro="_REENTRANT" + ;; + + aix*) + ax_pthread_check_macro="_THREAD_SAFE" + ;; + + *) + ax_pthread_check_macro="--" + ;; +esac +AS_IF([test "x$ax_pthread_check_macro" = "x--"], + [ax_pthread_check_cond=0], + [ax_pthread_check_cond="!defined($ax_pthread_check_macro)"]) + +# Are we compiling with Clang? + +AC_CACHE_CHECK([whether $CC is Clang], + [ax_cv_PTHREAD_CLANG], + [ax_cv_PTHREAD_CLANG=no + # Note that Autoconf sets GCC=yes for Clang as well as GCC + if test "x$GCC" = "xyes"; then + AC_EGREP_CPP([AX_PTHREAD_CC_IS_CLANG], + [/* Note: Clang 2.7 lacks __clang_[a-z]+__ */ +# if defined(__clang__) && defined(__llvm__) + AX_PTHREAD_CC_IS_CLANG +# endif + ], + [ax_cv_PTHREAD_CLANG=yes]) + fi + ]) +ax_pthread_clang="$ax_cv_PTHREAD_CLANG" + +ax_pthread_clang_warning=no + +# Clang needs special handling, because older versions handle the -pthread +# option in a rather... idiosyncratic way + +if test "x$ax_pthread_clang" = "xyes"; then + + # Clang takes -pthread; it has never supported any other flag + + # (Note 1: This will need to be revisited if a system that Clang + # supports has POSIX threads in a separate library. This tends not + # to be the way of modern systems, but it's conceivable.) + + # (Note 2: On some systems, notably Darwin, -pthread is not needed + # to get POSIX threads support; the API is always present and + # active. We could reasonably leave PTHREAD_CFLAGS empty. But + # -pthread does define _REENTRANT, and while the Darwin headers + # ignore this macro, third-party headers might not.) + + PTHREAD_CFLAGS="-pthread" + PTHREAD_LIBS= + + ax_pthread_ok=yes + + # However, older versions of Clang make a point of warning the user + # that, in an invocation where only linking and no compilation is + # taking place, the -pthread option has no effect ("argument unused + # during compilation"). They expect -pthread to be passed in only + # when source code is being compiled. + # + # Problem is, this is at odds with the way Automake and most other + # C build frameworks function, which is that the same flags used in + # compilation (CFLAGS) are also used in linking. Many systems + # supported by AX_PTHREAD require exactly this for POSIX threads + # support, and in fact it is often not straightforward to specify a + # flag that is used only in the compilation phase and not in + # linking. Such a scenario is extremely rare in practice. + # + # Even though use of the -pthread flag in linking would only print + # a warning, this can be a nuisance for well-run software projects + # that build with -Werror. So if the active version of Clang has + # this misfeature, we search for an option to squash it. + + AC_CACHE_CHECK([whether Clang needs flag to prevent "argument unused" warning when linking with -pthread], + [ax_cv_PTHREAD_CLANG_NO_WARN_FLAG], + [ax_cv_PTHREAD_CLANG_NO_WARN_FLAG=unknown + # Create an alternate version of $ac_link that compiles and + # links in two steps (.c -> .o, .o -> exe) instead of one + # (.c -> exe), because the warning occurs only in the second + # step + ax_pthread_save_ac_link="$ac_link" + ax_pthread_sed='s/conftest\.\$ac_ext/conftest.$ac_objext/g' + ax_pthread_link_step=`$as_echo "$ac_link" | sed "$ax_pthread_sed"` + ax_pthread_2step_ac_link="($ac_compile) && (echo ==== >&5) && ($ax_pthread_link_step)" + ax_pthread_save_CFLAGS="$CFLAGS" + for ax_pthread_try in '' -Qunused-arguments -Wno-unused-command-line-argument unknown; do + AS_IF([test "x$ax_pthread_try" = "xunknown"], [break]) + CFLAGS="-Werror -Wunknown-warning-option $ax_pthread_try -pthread $ax_pthread_save_CFLAGS" + ac_link="$ax_pthread_save_ac_link" + AC_LINK_IFELSE([AC_LANG_SOURCE([[int main(void){return 0;}]])], + [ac_link="$ax_pthread_2step_ac_link" + AC_LINK_IFELSE([AC_LANG_SOURCE([[int main(void){return 0;}]])], + [break]) + ]) + done + ac_link="$ax_pthread_save_ac_link" + CFLAGS="$ax_pthread_save_CFLAGS" + AS_IF([test "x$ax_pthread_try" = "x"], [ax_pthread_try=no]) + ax_cv_PTHREAD_CLANG_NO_WARN_FLAG="$ax_pthread_try" + ]) + + case "$ax_cv_PTHREAD_CLANG_NO_WARN_FLAG" in + no | unknown) ;; + *) PTHREAD_CFLAGS="$ax_cv_PTHREAD_CLANG_NO_WARN_FLAG $PTHREAD_CFLAGS" ;; + esac + +fi # $ax_pthread_clang = yes + +if test "x$ax_pthread_ok" = "xno"; then +for ax_pthread_try_flag in $ax_pthread_flags; do + + case $ax_pthread_try_flag in + none) + AC_MSG_CHECKING([whether pthreads work without any flags]) + ;; + + -mt,pthread) + AC_MSG_CHECKING([whether pthreads work with -mt -lpthread]) + PTHREAD_CFLAGS="-mt" + PTHREAD_LIBS="-lpthread" + ;; + + -*) + AC_MSG_CHECKING([whether pthreads work with $ax_pthread_try_flag]) + PTHREAD_CFLAGS="$ax_pthread_try_flag" + ;; + + pthread-config) + AC_CHECK_PROG([ax_pthread_config], [pthread-config], [yes], [no]) + AS_IF([test "x$ax_pthread_config" = "xno"], [continue]) + PTHREAD_CFLAGS="`pthread-config --cflags`" + PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`" + ;; + + *) + AC_MSG_CHECKING([for the pthreads library -l$ax_pthread_try_flag]) + PTHREAD_LIBS="-l$ax_pthread_try_flag" + ;; + esac + + ax_pthread_save_CFLAGS="$CFLAGS" + ax_pthread_save_LIBS="$LIBS" + CFLAGS="$CFLAGS $PTHREAD_CFLAGS" + LIBS="$PTHREAD_LIBS $LIBS" + + # Check for various functions. We must include pthread.h, + # since some functions may be macros. (On the Sequent, we + # need a special flag -Kthread to make this header compile.) + # We check for pthread_join because it is in -lpthread on IRIX + # while pthread_create is in libc. We check for pthread_attr_init + # due to DEC craziness with -lpthreads. We check for + # pthread_cleanup_push because it is one of the few pthread + # functions on Solaris that doesn't have a non-functional libc stub. + # We try pthread_create on general principles. + + AC_LINK_IFELSE([AC_LANG_PROGRAM([#include +# if $ax_pthread_check_cond +# error "$ax_pthread_check_macro must be defined" +# endif + static void routine(void *a) { a = 0; } + static void *start_routine(void *a) { return a; }], + [pthread_t th; pthread_attr_t attr; + pthread_create(&th, 0, start_routine, 0); + pthread_join(th, 0); + pthread_attr_init(&attr); + pthread_cleanup_push(routine, 0); + pthread_cleanup_pop(0) /* ; */])], + [ax_pthread_ok=yes], + []) + + CFLAGS="$ax_pthread_save_CFLAGS" + LIBS="$ax_pthread_save_LIBS" + + AC_MSG_RESULT([$ax_pthread_ok]) + AS_IF([test "x$ax_pthread_ok" = "xyes"], [break]) + + PTHREAD_LIBS="" + PTHREAD_CFLAGS="" +done +fi + +# Various other checks: +if test "x$ax_pthread_ok" = "xyes"; then + ax_pthread_save_CFLAGS="$CFLAGS" + ax_pthread_save_LIBS="$LIBS" + CFLAGS="$CFLAGS $PTHREAD_CFLAGS" + LIBS="$PTHREAD_LIBS $LIBS" + + # Detect AIX lossage: JOINABLE attribute is called UNDETACHED. + AC_CACHE_CHECK([for joinable pthread attribute], + [ax_cv_PTHREAD_JOINABLE_ATTR], + [ax_cv_PTHREAD_JOINABLE_ATTR=unknown + for ax_pthread_attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do + AC_LINK_IFELSE([AC_LANG_PROGRAM([#include ], + [int attr = $ax_pthread_attr; return attr /* ; */])], + [ax_cv_PTHREAD_JOINABLE_ATTR=$ax_pthread_attr; break], + []) + done + ]) + AS_IF([test "x$ax_cv_PTHREAD_JOINABLE_ATTR" != "xunknown" && \ + test "x$ax_cv_PTHREAD_JOINABLE_ATTR" != "xPTHREAD_CREATE_JOINABLE" && \ + test "x$ax_pthread_joinable_attr_defined" != "xyes"], + [AC_DEFINE_UNQUOTED([PTHREAD_CREATE_JOINABLE], + [$ax_cv_PTHREAD_JOINABLE_ATTR], + [Define to necessary symbol if this constant + uses a non-standard name on your system.]) + ax_pthread_joinable_attr_defined=yes + ]) + + AC_CACHE_CHECK([whether more special flags are required for pthreads], + [ax_cv_PTHREAD_SPECIAL_FLAGS], + [ax_cv_PTHREAD_SPECIAL_FLAGS=no + case $host_os in + solaris*) + ax_cv_PTHREAD_SPECIAL_FLAGS="-D_POSIX_PTHREAD_SEMANTICS" + ;; + esac + ]) + AS_IF([test "x$ax_cv_PTHREAD_SPECIAL_FLAGS" != "xno" && \ + test "x$ax_pthread_special_flags_added" != "xyes"], + [PTHREAD_CFLAGS="$ax_cv_PTHREAD_SPECIAL_FLAGS $PTHREAD_CFLAGS" + ax_pthread_special_flags_added=yes]) + + AC_CACHE_CHECK([for PTHREAD_PRIO_INHERIT], + [ax_cv_PTHREAD_PRIO_INHERIT], + [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include ]], + [[int i = PTHREAD_PRIO_INHERIT;]])], + [ax_cv_PTHREAD_PRIO_INHERIT=yes], + [ax_cv_PTHREAD_PRIO_INHERIT=no]) + ]) + AS_IF([test "x$ax_cv_PTHREAD_PRIO_INHERIT" = "xyes" && \ + test "x$ax_pthread_prio_inherit_defined" != "xyes"], + [AC_DEFINE([HAVE_PTHREAD_PRIO_INHERIT], [1], [Have PTHREAD_PRIO_INHERIT.]) + ax_pthread_prio_inherit_defined=yes + ]) + + CFLAGS="$ax_pthread_save_CFLAGS" + LIBS="$ax_pthread_save_LIBS" + + # More AIX lossage: compile with *_r variant + if test "x$GCC" != "xyes"; then + case $host_os in + aix*) + AS_CASE(["x/$CC"], + [x*/c89|x*/c89_128|x*/c99|x*/c99_128|x*/cc|x*/cc128|x*/xlc|x*/xlc_v6|x*/xlc128|x*/xlc128_v6], + [#handle absolute path differently from PATH based program lookup + AS_CASE(["x$CC"], + [x/*], + [AS_IF([AS_EXECUTABLE_P([${CC}_r])],[PTHREAD_CC="${CC}_r"])], + [AC_CHECK_PROGS([PTHREAD_CC],[${CC}_r],[$CC])])]) + ;; + esac + fi +fi + +test -n "$PTHREAD_CC" || PTHREAD_CC="$CC" + +AC_SUBST([PTHREAD_LIBS]) +AC_SUBST([PTHREAD_CFLAGS]) +AC_SUBST([PTHREAD_CC]) + +# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND: +if test "x$ax_pthread_ok" = "xyes"; then + ifelse([$1],,[AC_DEFINE([HAVE_PTHREAD],[1],[Define if you have POSIX threads libraries and header files.])],[$1]) + : +else + ax_pthread_ok=no + $2 +fi +AC_LANG_POP +])dnl AX_PTHREAD diff --git a/config/pkg.m4 b/config/pkg.m4 new file mode 100644 index 0000000..9a71878 --- /dev/null +++ b/config/pkg.m4 @@ -0,0 +1,159 @@ +# pkg.m4 - Macros to locate and utilise pkg-config. -*- Autoconf -*- +# serial 1 (pkg-config-0.24) +# +# Copyright © 2004 Scott James Remnant . +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# As a special exception to the GNU General Public License, if you +# distribute this file as part of a program that contains a +# configuration script generated by Autoconf, you may include it under +# the same distribution terms that you use for the rest of that program. + +# PKG_PROG_PKG_CONFIG([MIN-VERSION]) +# ---------------------------------- +AC_DEFUN([PKG_PROG_PKG_CONFIG], +[m4_pattern_forbid([^_?PKG_[A-Z_]+$]) +m4_pattern_allow([^PKG_CONFIG(_(PATH|LIBDIR|SYSROOT_DIR|ALLOW_SYSTEM_(CFLAGS|LIBS)))?$]) +m4_pattern_allow([^PKG_CONFIG_(DISABLE_UNINSTALLED|TOP_BUILD_DIR|DEBUG_SPEW)$]) +AC_ARG_VAR([PKG_CONFIG], [path to pkg-config utility]) +AC_ARG_VAR([PKG_CONFIG_PATH], [directories to add to pkg-config's search path]) +AC_ARG_VAR([PKG_CONFIG_LIBDIR], [path overriding pkg-config's built-in search path]) + +if test "x$ac_cv_env_PKG_CONFIG_set" != "xset"; then + AC_PATH_TOOL([PKG_CONFIG], [pkg-config]) +fi +if test -n "$PKG_CONFIG"; then + _pkg_min_version=m4_default([$1], [0.9.0]) + AC_MSG_CHECKING([pkg-config is at least version $_pkg_min_version]) + if $PKG_CONFIG --atleast-pkgconfig-version $_pkg_min_version; then + AC_MSG_RESULT([yes]) + else + AC_MSG_RESULT([no]) + PKG_CONFIG="" + fi +fi[]dnl +])# PKG_PROG_PKG_CONFIG + +# PKG_CHECK_EXISTS(MODULES, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND]) +# +# Check to see whether a particular set of modules exists. Similar +# to PKG_CHECK_MODULES(), but does not set variables or print errors. +# +# Please remember that m4 expands AC_REQUIRE([PKG_PROG_PKG_CONFIG]) +# only at the first occurence in configure.ac, so if the first place +# it's called might be skipped (such as if it is within an "if", you +# have to call PKG_CHECK_EXISTS manually +# -------------------------------------------------------------- +AC_DEFUN([PKG_CHECK_EXISTS], +[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl +if test -n "$PKG_CONFIG" && \ + AC_RUN_LOG([$PKG_CONFIG --exists --print-errors "$1"]); then + m4_default([$2], [:]) +m4_ifvaln([$3], [else + $3])dnl +fi]) + +# _PKG_CONFIG([VARIABLE], [COMMAND], [MODULES]) +# --------------------------------------------- +m4_define([_PKG_CONFIG], +[if test -n "$$1"; then + pkg_cv_[]$1="$$1" + elif test -n "$PKG_CONFIG"; then + PKG_CHECK_EXISTS([$3], + [pkg_cv_[]$1=`$PKG_CONFIG --[]$2 "$3" 2>/dev/null` + test "x$?" != "x0" && pkg_failed=yes ], + [pkg_failed=yes]) + else + pkg_failed=untried +fi[]dnl +])# _PKG_CONFIG + +# _PKG_SHORT_ERRORS_SUPPORTED +# ----------------------------- +AC_DEFUN([_PKG_SHORT_ERRORS_SUPPORTED], +[AC_REQUIRE([PKG_PROG_PKG_CONFIG]) +if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then + _pkg_short_errors_supported=yes +else + _pkg_short_errors_supported=no +fi[]dnl +])# _PKG_SHORT_ERRORS_SUPPORTED + + +# PKG_CHECK_MODULES(VARIABLE-PREFIX, MODULES, [ACTION-IF-FOUND], +# [ACTION-IF-NOT-FOUND]) +# +# +# Note that if there is a possibility the first call to +# PKG_CHECK_MODULES might not happen, you should be sure to include an +# explicit call to PKG_PROG_PKG_CONFIG in your configure.ac +# +# +# -------------------------------------------------------------- +AC_DEFUN([PKG_CHECK_MODULES], +[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl +AC_ARG_VAR([$1][_CFLAGS], [C compiler flags for $1, overriding pkg-config])dnl +AC_ARG_VAR([$1][_LIBS], [linker flags for $1, overriding pkg-config])dnl + +pkg_failed=no +AC_MSG_CHECKING([for $1]) + +_PKG_CONFIG([$1][_CFLAGS], [cflags], [$2]) +_PKG_CONFIG([$1][_LIBS], [libs], [$2]) + +m4_define([_PKG_TEXT], [Alternatively, you may set the environment variables $1[]_CFLAGS +and $1[]_LIBS to avoid the need to call pkg-config. +See the pkg-config man page for more details.]) + +if test $pkg_failed = yes; then + AC_MSG_RESULT([no]) + _PKG_SHORT_ERRORS_SUPPORTED + if test $_pkg_short_errors_supported = yes; then + $1[]_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "$2" 2>&1` + else + $1[]_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "$2" 2>&1` + fi + # Put the nasty error message in config.log where it belongs + echo "$$1[]_PKG_ERRORS" >&AS_MESSAGE_LOG_FD + + m4_default([$4], [AC_MSG_ERROR( +[Package requirements ($2) were not met: + +$$1_PKG_ERRORS + +Consider adjusting the PKG_CONFIG_PATH environment variable if you +installed software in a non-standard prefix. + +_PKG_TEXT])[]dnl + ]) +elif test $pkg_failed = untried; then + AC_MSG_RESULT([no]) + m4_default([$4], [AC_MSG_FAILURE( +[The pkg-config script could not be found or is too old. Make sure it +is in your PATH or set the PKG_CONFIG environment variable to the full +path to pkg-config. + +_PKG_TEXT + +To get pkg-config, see .])[]dnl + ]) +else + $1[]_CFLAGS=$pkg_cv_[]$1[]_CFLAGS + $1[]_LIBS=$pkg_cv_[]$1[]_LIBS + AC_MSG_RESULT([yes]) + $3 +fi[]dnl +])# PKG_CHECK_MODULES diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..3b876a2 --- /dev/null +++ b/configure.ac @@ -0,0 +1,45 @@ +# -*- Autoconf -*- + +AC_PREREQ(2.57) +AC_INIT(hiptext, 0.2, jtunney@gmail.com) + +AC_CONFIG_AUX_DIR(config) +AC_CONFIG_MACRO_DIR(config) +AM_INIT_AUTOMAKE(foreign) +AC_CONFIG_SRCDIR(src/hiptext.cc) +ifdef(AM_SILENT_RULES, [AM_SILENT_RULES(yes)]) +AC_CANONICAL_BUILD +AC_CANONICAL_HOST +PKG_PROG_PKG_CONFIG(0.22) + +AC_LANG(C++) +AC_PROG_CXX +AX_CXX_COMPILE_STDCXX(11) +AC_PROG_RANLIB +AX_PTHREAD + +AC_CHECK_LIB(jpeg, jpeg_set_defaults, [], [ + AC_MSG_ERROR([error: libjpeg is required]) +]) + +PKG_CHECK_MODULES(LIBAVCODEC, libavcodec) +PKG_CHECK_MODULES(LIBAVFORMAT, libavformat) +PKG_CHECK_MODULES(LIBAVUTIL, libavutil) +PKG_CHECK_MODULES(LIBGFLAGS, libgflags) +PKG_CHECK_MODULES(LIBGLOG, libglog) +PKG_CHECK_MODULES(LIBPNG, libpng) +PKG_CHECK_MODULES(LIBSWSCALE, libswscale) + +AC_CHECK_PROGS(FREETYPE, freetype-config) +if test -z "$FREETYPE"; then + AC_MSG_ERROR([error: libfreetype-dev is required]) +fi +LIBFREETYPE_CFLAGS="`$FREETYPE --cflags`" +LIBFREETYPE_LIBS="`$FREETYPE --libs`" +AC_SUBST(LIBFREETYPE_CFLAGS) +AC_SUBST(LIBFREETYPE_LIBS) + +AC_CHECK_PROGS(RAGEL, ragel) + +AC_CONFIG_FILES(Makefile) +AC_OUTPUT diff --git a/install-gflags.sh b/install-gflags.sh deleted file mode 100755 index 5b48039..0000000 --- a/install-gflags.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash -set -ex - -VERSION="2.0" - -pushd /tmp -rm -rf gflags-$VERSION gflags-$VERSION.tar.gz -wget http://gflags.googlecode.com/files/gflags-$VERSION.tar.gz || exit $? -tar -xzf gflags-$VERSION.tar.gz || exit $? -pushd gflags-$VERSION -./configure || exit $? -make -j4 || exit $? -sudo make install || exit $? -popd -rm -rf gflags-$VERSION gflags-$VERSION.tar.gz -popd - -echo -e "\033[32msuccess\033[m" - diff --git a/install-glog.sh b/install-glog.sh deleted file mode 100755 index c9fe000..0000000 --- a/install-glog.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash -set -ex - -VERSION="0.3.2" - -pushd /tmp -rm -rf glog-$VERSION glog-$VERSION.tar.gz -wget http://google-glog.googlecode.com/files/glog-$VERSION.tar.gz || exit $? -tar -xzf glog-$VERSION.tar.gz || exit $? -pushd glog-$VERSION -./configure || exit $? -make -j4 || exit $? -sudo make install || exit $? -popd -rm -rf glog-$VERSION glog-$VERSION.tar.gz -popd - -echo -e "\033[32msuccess\033[m" diff --git a/artiste.cc b/src/artiste.cc similarity index 99% rename from artiste.cc rename to src/artiste.cc index 9c0352b..cab54ba 100644 --- a/artiste.cc +++ b/src/artiste.cc @@ -1,7 +1,7 @@ // hiptext - Image to Text Converter // Serene Han -#include "artiste.h" +#include "hiptext/artiste.h" #include #include @@ -12,7 +12,7 @@ #include #include -#include "movie.h" +#include "hiptext/movie.h" #ifdef __APPLE__ using sighandler_t = sig_t; diff --git a/charquantizer.cc b/src/charquantizer.cc similarity index 88% rename from charquantizer.cc rename to src/charquantizer.cc index 5011740..dda6862 100644 --- a/charquantizer.cc +++ b/src/charquantizer.cc @@ -1,7 +1,7 @@ // By Justine Tunney // hiptext - Image to Text Converter -#include "charquantizer.h" +#include "hiptext/charquantizer.h" // For Emacs: // Local Variables: diff --git a/css_color.rl b/src/css_color.rl similarity index 100% rename from css_color.rl rename to src/css_color.rl diff --git a/font.cc b/src/font.cc similarity index 96% rename from font.cc rename to src/font.cc index e3b3218..64ec82a 100644 --- a/font.cc +++ b/src/font.cc @@ -1,15 +1,15 @@ // hiptext - Image to Text Converter // By Justine Tunney -#include "font.h" +#include "hiptext/font.h" #include #include FT_FREETYPE_H #include #include -#include "graphic.h" -#include "pixel.h" +#include "hiptext/graphic.h" +#include "hiptext/pixel.h" DEFINE_string(font, "DejaVuSansMono.ttf", "The path to the font .ttf file to use."); diff --git a/graphic.cc b/src/graphic.cc similarity index 98% rename from graphic.cc rename to src/graphic.cc index 419d7df..2bb13a0 100644 --- a/graphic.cc +++ b/src/graphic.cc @@ -1,12 +1,12 @@ // hiptext - Image to Text Converter // By Justine Tunney -#include "graphic.h" +#include "hiptext/graphic.h" #include #include #include #include -#include "pixel.h" +#include "hiptext/pixel.h" // Calculate number that's percent between p1 and p2. static inline double Lerp(double p1, double p2, double percent) { diff --git a/hiptext.cc b/src/hiptext.cc similarity index 95% rename from hiptext.cc rename to src/hiptext.cc index a23fa02..c106210 100644 --- a/hiptext.cc +++ b/src/hiptext.cc @@ -14,18 +14,18 @@ #include #include -#include "artiste.h" -#include "charquantizer.h" -#include "font.h" -#include "jpeg.h" -#include "pixel.h" -#include "png.h" -#include "macterm.h" -#include "movie.h" -#include "xterm256.h" -#include "termprinter.h" -#include "sixelprinter.h" -#include "unicode.h" +#include "hiptext/artiste.h" +#include "hiptext/charquantizer.h" +#include "hiptext/font.h" +#include "hiptext/jpeg.h" +#include "hiptext/pixel.h" +#include "hiptext/png.h" +#include "hiptext/macterm.h" +#include "hiptext/movie.h" +#include "hiptext/xterm256.h" +#include "hiptext/termprinter.h" +#include "hiptext/sixelprinter.h" +#include "hiptext/unicode.h" using std::cout; using std::string; diff --git a/artiste.h b/src/hiptext/artiste.h similarity index 98% rename from artiste.h rename to src/hiptext/artiste.h index 0091005..8aa2235 100644 --- a/artiste.h +++ b/src/hiptext/artiste.h @@ -7,7 +7,7 @@ #include #include -#include "unicode.h" +#include "hiptext/unicode.h" class Movie; class Graphic; diff --git a/charquantizer.h b/src/hiptext/charquantizer.h similarity index 100% rename from charquantizer.h rename to src/hiptext/charquantizer.h diff --git a/font.h b/src/hiptext/font.h similarity index 100% rename from font.h rename to src/hiptext/font.h diff --git a/graphic.h b/src/hiptext/graphic.h similarity index 98% rename from graphic.h rename to src/hiptext/graphic.h index 37c8ca4..51edd04 100644 --- a/graphic.h +++ b/src/hiptext/graphic.h @@ -9,7 +9,7 @@ #include #include -#include "pixel.h" +#include "hiptext/pixel.h" class Graphic { public: diff --git a/jpeg.h b/src/hiptext/jpeg.h similarity index 100% rename from jpeg.h rename to src/hiptext/jpeg.h diff --git a/macterm.h b/src/hiptext/macterm.h similarity index 100% rename from macterm.h rename to src/hiptext/macterm.h diff --git a/movie.h b/src/hiptext/movie.h similarity index 97% rename from movie.h rename to src/hiptext/movie.h index 6009b51..7f07ab1 100644 --- a/movie.h +++ b/src/hiptext/movie.h @@ -6,7 +6,7 @@ #include -#include "graphic.h" +#include "hiptext/graphic.h" struct AVCodec; struct AVCodecContext; diff --git a/pixel.h b/src/hiptext/pixel.h similarity index 100% rename from pixel.h rename to src/hiptext/pixel.h diff --git a/png.h b/src/hiptext/png.h similarity index 100% rename from png.h rename to src/hiptext/png.h diff --git a/sixelprinter.h b/src/hiptext/sixelprinter.h similarity index 100% rename from sixelprinter.h rename to src/hiptext/sixelprinter.h diff --git a/termprinter.h b/src/hiptext/termprinter.h similarity index 100% rename from termprinter.h rename to src/hiptext/termprinter.h diff --git a/unicode.h b/src/hiptext/unicode.h similarity index 100% rename from unicode.h rename to src/hiptext/unicode.h diff --git a/unused.h b/src/hiptext/unused.h similarity index 100% rename from unused.h rename to src/hiptext/unused.h diff --git a/xterm256.h b/src/hiptext/xterm256.h similarity index 100% rename from xterm256.h rename to src/hiptext/xterm256.h diff --git a/jpeg.cc b/src/jpeg.cc similarity index 94% rename from jpeg.cc rename to src/jpeg.cc index 13ab626..4cde41a 100644 --- a/jpeg.cc +++ b/src/jpeg.cc @@ -1,7 +1,7 @@ // hiptext - Image to Text Converter // By Justine Tunney -#include "jpeg.h" +#include "hiptext/jpeg.h" #include #include @@ -10,8 +10,8 @@ #include #include -#include "pixel.h" -#include "graphic.h" +#include "hiptext/pixel.h" +#include "hiptext/graphic.h" static void OnError(j_common_ptr cinfo) { char buffer[JMSG_LENGTH_MAX]; diff --git a/macterm.cc b/src/macterm.cc similarity index 99% rename from macterm.cc rename to src/macterm.cc index 1042a9a..03a5463 100644 --- a/macterm.cc +++ b/src/macterm.cc @@ -1,7 +1,7 @@ // hiptext - Image to Text Converter // By Justine Tunney -#include "macterm.h" +#include "hiptext/macterm.h" #include #include @@ -11,7 +11,7 @@ #include -#include "pixel.h" +#include "hiptext/pixel.h" using std::array; using std::distance; diff --git a/movie.cc b/src/movie.cc similarity index 98% rename from movie.cc rename to src/movie.cc index 9ad9368..9bf94b3 100644 --- a/movie.cc +++ b/src/movie.cc @@ -1,7 +1,7 @@ // hiptext - Image to Text Converter // By Serene Han & Justine Tunney -#include "movie.h" +#include "hiptext/movie.h" #include @@ -12,8 +12,8 @@ extern "C" { // ffmpeg hates C++ and won't put this in their headers. #include } -#include "graphic.h" -#include "pixel.h" +#include "hiptext/graphic.h" +#include "hiptext/pixel.h" Movie::Movie(const std::string& path) { format_ = avformat_alloc_context(); diff --git a/pixel.cc b/src/pixel.cc similarity index 99% rename from pixel.cc rename to src/pixel.cc index eb52f61..0f611fd 100644 --- a/pixel.cc +++ b/src/pixel.cc @@ -1,7 +1,7 @@ // hiptext - Image to Text Converter // By Justine Tunney -#include "pixel.h" +#include "hiptext/pixel.h" #include #include #include diff --git a/pixel_parse.rl b/src/pixel_parse.rl similarity index 95% rename from pixel_parse.rl rename to src/pixel_parse.rl index e11ddb0..ba6fa99 100644 --- a/pixel_parse.rl +++ b/src/pixel_parse.rl @@ -3,8 +3,8 @@ // Complete CSS3/X11 Color Parser Implementation (and more!) -#include "pixel.h" -#include "unused.h" +#include "hiptext/pixel.h" +#include "hiptext/unused.h" #include #include diff --git a/png.cc b/src/png.cc similarity index 97% rename from png.cc rename to src/png.cc index 9859384..2e3125e 100644 --- a/png.cc +++ b/src/png.cc @@ -1,7 +1,7 @@ // hiptext - Image to Text Converter // By Justine Tunney -#include "png.h" +#include "hiptext/png.h" #include #include @@ -10,8 +10,8 @@ #define PNG_SKIP_SETJMP_CHECK #include -#include "graphic.h" -#include "pixel.h" +#include "hiptext/graphic.h" +#include "hiptext/pixel.h" Graphic LoadPNG(const std::string& path) { FILE* fp = fopen(path.data(), "rb"); diff --git a/sixelprinter.cc b/src/sixelprinter.cc similarity index 97% rename from sixelprinter.cc rename to src/sixelprinter.cc index 2bc4ec4..14f09ae 100644 --- a/sixelprinter.cc +++ b/src/sixelprinter.cc @@ -1,9 +1,9 @@ // hiptext - Image to Text Converter // By Justine Tunney -#include "sixelprinter.h" -#include "xterm256.h" -#include "pixel.h" +#include "hiptext/sixelprinter.h" +#include "hiptext/xterm256.h" +#include "hiptext/pixel.h" #include #include diff --git a/termprinter.cc b/src/termprinter.cc similarity index 99% rename from termprinter.cc rename to src/termprinter.cc index 95e2fd4..07e46e8 100644 --- a/termprinter.cc +++ b/src/termprinter.cc @@ -1,7 +1,7 @@ // hiptext - Image to Text Converter // By Justine Tunney -#include "termprinter.h" +#include "hiptext/termprinter.h" #include #include diff --git a/unicode.cc b/src/unicode.cc similarity index 97% rename from unicode.cc rename to src/unicode.cc index 6871cc7..284d678 100644 --- a/unicode.cc +++ b/src/unicode.cc @@ -1,7 +1,7 @@ // hiptext - Image to Text Converter // By Justine Tunney -#include "unicode.h" +#include "hiptext/unicode.h" #include #include #include diff --git a/xterm256.cc b/src/xterm256.cc similarity index 99% rename from xterm256.cc rename to src/xterm256.cc index 0db765e..0fce3da 100644 --- a/xterm256.cc +++ b/src/xterm256.cc @@ -1,10 +1,10 @@ // hiptext - Image to Text Converter // By Justine Tunney -#include "xterm256.h" +#include "hiptext/xterm256.h" #include #include -#include "pixel.h" +#include "hiptext/pixel.h" DEFINE_bool(fast, false, "Use O(1) xterm256 approximate color quantizer."); diff --git a/pixel_test.cc b/test/pixel_test.cc similarity index 92% rename from pixel_test.cc rename to test/pixel_test.cc index 69b1da3..d224d22 100644 --- a/pixel_test.cc +++ b/test/pixel_test.cc @@ -1,7 +1,7 @@ // hiptext - Image to Text Converter // By Justine Tunney -#include "pixel.h" +#include "hiptext/pixel.h" #include TEST(PixelTest, Basic) { diff --git a/test/test.cc b/test/test.cc new file mode 100644 index 0000000..2c0d487 --- /dev/null +++ b/test/test.cc @@ -0,0 +1,20 @@ +#include +#include + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + testing::FLAGS_gtest_death_test_style = "threadsafe"; + google::InitGoogleLogging(argv[0]); + return RUN_ALL_TESTS(); +} + +// For Emacs: +// Local Variables: +// mode:c++ +// indent-tabs-mode:nil +// tab-width:2 +// c-basic-offset:2 +// c-file-style:nil +// End: +// For VIM: +// vim:set expandtab softtabstop=2 shiftwidth=2 tabstop=2: diff --git a/xterm256_test.cc b/test/xterm256_test.cc similarity index 94% rename from xterm256_test.cc rename to test/xterm256_test.cc index 2974940..2c08a0b 100644 --- a/xterm256_test.cc +++ b/test/xterm256_test.cc @@ -1,9 +1,9 @@ // hiptext - Image to Text Converter // By Justine Tunney -#include "xterm256.h" +#include "hiptext/xterm256.h" #include -#include "pixel.h" +#include "hiptext/pixel.h" TEST(Xterm256Test, Basic) { EXPECT_EQ(16, rgb_to_xterm256({0, 0, 0})); diff --git a/trash.cc b/trash.cc deleted file mode 100644 index df605c6..0000000 --- a/trash.cc +++ /dev/null @@ -1,292 +0,0 @@ -// Makefile: -lavfilter - - avfilter_register_all(); - -#include - - AVFilterContext* buffersink_ctx_; - AVFilterContext* buffersrc_ctx_; - AVFilterGraph* filter_graph_; - - InitFilters("scale=78:24"); - -int Movie::InitFilters(const string& filters_descr) { - char args[512]; - int ret; - AVFilter* buffersrc = avfilter_get_by_name("buffer"); - AVFilter* buffersink = avfilter_get_by_name("ffbuffersink"); - AVFilterInOut* outputs = avfilter_inout_alloc(); - AVFilterInOut* inputs = avfilter_inout_alloc(); - AVPixelFormat pix_fmts[] = {AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE}; - AVBufferSinkParams* buffersink_params; - filter_graph = avfilter_graph_alloc(); - snprintf(args, sizeof(args), - "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d", - dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt, - dec_ctx->time_base.num, dec_ctx->time_base.den, - dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den); - ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in", - args, NULL, filter_graph); - if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n"); - return ret; - } - /* buffer video sink: to terminate the filter chain. */ - buffersink_params = av_buffersink_params_alloc(); - buffersink_params->pixel_fmts = pix_fmts; - ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out", - NULL, buffersink_params, filter_graph); - av_free(buffersink_params); - if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n"); - return ret; - } - /* Endpoints for the filter graph. */ - outputs->name = av_strdup("in"); - outputs->filter_ctx = buffersrc_ctx; - outputs->pad_idx = 0; - outputs->next = NULL; - inputs->name = av_strdup("out"); - inputs->filter_ctx = buffersink_ctx; - inputs->pad_idx = 0; - inputs->next = NULL; - if ((ret = avfilter_graph_parse(filter_graph, filters_descr, - &inputs, &outputs, NULL)) < 0) - return ret; - if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0) - return ret; - return 0; -} - -////////////////////////////////////////////////////////////////////// -// xterm hack #1 - -struct Combo { - Combo(Pixel color, wchar_t ch, uint8_t xterm_fg, uint8_t xterm_bg) - : color(color), ch(ch), xterm_fg(xterm_fg), xterm_bg(xterm_bg) {} - Pixel color; - wchar_t ch; - int xterm_fg; - int xterm_bg; -}; - -// std::vector g_combos_red; -// std::vector g_combos_green; -// std::vector g_combos_blue; -// std::unordered_map g_combos; -std::vector g_combos; - -void InitXterm256Hack1() { - for (int xterm_bg = 17; xterm_bg < 256; ++xterm_bg) { - g_combos.emplace_back(xterm_to_rgb(xterm_bg), L' ', 0, xterm_bg); - } - for (int xterm_fg = 17; xterm_fg < 232; ++xterm_fg) { - for (int xterm_bg = 17; xterm_bg < 232; ++xterm_bg) { - Pixel bg = xterm_to_rgb(xterm_bg); - Pixel fg = xterm_to_rgb(xterm_fg); - bg.MixKubelkaMunk(fg); - g_combos.emplace_back(bg, L'\u2591', xterm_fg, xterm_bg); - } - } -} - -const Combo& QuantizeXterm256Hack1(const Pixel& color) { - const Combo* best = nullptr; - double best_dist = 1e6; - for (const auto& combo : g_combos) { - double dist = color.Distance(combo.color); - if (dist < best_dist) { - best = &combo; - best_dist = dist; - } - } - CHECK_NOTNULL(best); - assert(best != nullptr); - return *best; -} - -void PrintImageXterm256Hack1(std::ostream& os, const Graphic& graphic) { - TermPrinter out(os); - Pixel bg = Pixel(FLAGS_bg); - int bg256 = rgb_to_xterm256(bg); - for (int y = 0; y < graphic.height(); ++y) { - for (int x = 0; x < graphic.width(); ++x) { - const Pixel& color = graphic.Get(x, y).Copy().Opacify(bg); - const Combo& combo = QuantizeXterm256Hack1(color); - if (combo.xterm_fg) - out.SetForeground256(combo.xterm_fg); - if (combo.xterm_bg) - out.SetBackground256(combo.xterm_bg); - if (!FLAGS_bgprint && (combo.xterm_fg == bg256 && - combo.xterm_bg == bg256)) { - out << FLAGS_space; - } else { - out << combo.ch; - } - } - out.Reset(); - out << "\n"; - } -} - - if (FLAGS_xterm256_hack1) - InitXterm256Hack1(); - - if (FLAGS_xterm256_hack1) { - PrintImageXterm256Hack1( - os, graphic.BilinearScale(width, height / 2)); - } - -// hack #1 -////////////////////////////////////////////////////////////////////// - -void HideCursor() { - g_cursor_saved = true; - cout << "\x1b[?25l\x1b[s"; -} - -void ShowCursor() { - g_cursor_saved = false; - cout << "\x1b[u\x1b[?25h"; -} - -void ResetCursor() { - cout << "\x1b[H"; -} - - - signal(SIGINT, OnCtrlC); - struct stat dinfo; - stat(path.data(), &dinfo); - if (S_ISDIR(dinfo.st_mode)) { - // If directory, print a movie using all the frames.. - cout << "Printing a Movie from directory.\n"; - PrintMovie(path, 1000); - exit(0); - } - -// Prints all the frames from a directory. -// Assumes mplayer generated all these from a .jpg -void PrintMovie(const string& dir, const int frames) { - HideCursor(); - for (int frame = 1; frame <= frames; ++frame) { - ResetCursor(); - std::stringstream ss; - char buf[128]; - snprintf(buf, sizeof(buf), "%s/%08d.jpg", dir.data(), frame); - PrintImage(ss, LoadJPEG(buf)); - cout << ss.str(); - if (FLAGS_stepthrough) { - string lol; - std::getline(std::cin, lol); - } - } - ShowCursor(); -} - - - // Graphic lol(256, 3); - // for (int fg = 17; fg < 232; ++fg) { - // Pixel pix = xterm_to_rgb(fg); - // lol.Get(pix.red() * 255, 0) = Pixel::kBlack; - // lol.Get(pix.green() * 255, 1) = Pixel::kBlack; - // lol.Get(pix.blue() * 255, 2) = Pixel::kBlack; - // } - // WritePNG(lol, "/home/jart/www/graphic.png"); - - // Graphic lol(256, 256, Pixel::Parse("grey")); - // for (int fg = 16; fg < 256; ++fg) { - // Pixel pix = xterm_to_rgb(fg); - // cout << fg << " = " << pix << "\n"; - // lol.Get(pix.red() * 255, - // pix.blue() * 255) = Pixel(0, 0, pix.blue()); - // } - // WritePNG(lol, "/home/jart/www/graphic.png"); - - // InitXterm256Hack1(); - // Graphic lol(256, 256, Pixel::Parse("grey")); - // int n = 0; - // for (const auto& combo : g_combos) { - // n++; - // Pixel pix = combo.color; - // CHECK(pix.red() >= 0) << n; - // lol.Get(pix.red() * 255, - // pix.green() * 255) = Pixel(0, 0, pix.blue()); - // } - // WritePNG(lol, "/home/jart/www/graphic.png"); - - // TermPrinter out(cout, Pixel::Parse(FLAGS_bg), FLAGS_bgprint); - // out.SetBold(true); - // out << "hello\n"; - PrintImage(cout, LoadPNG("balls.png")); - PrintImage(cout, LoadJPEG("obama.jpg")); - // PrintImage(cout, LoadLetter(L'@', Pixel::kWhite, Pixel::kClear)); - - // InitXterm256Hack1(); - // Graphic spectrum = GenerateSpectrum(200, 100); - // for (int y = 0; y < spectrum.height(); ++y) { - // for (int x = 0; x < spectrum.width(); ++x) { - // spectrum.Get(x, y) = QuantizeXterm256Hack1(spectrum.Get(x, y)).color; - // spectrum.Get(x, y) = xterm_to_rgb(rgb_to_xterm256(spectrum.Get(x, y))); - // } - // } - // WritePNG(spectrum, "/home/jart/www/graphic.png"); - - // TermPrinter out(cout, Pixel::kBlack, false); - // out.SetBackground256(Pixel::kGreen); - // out.SetForeground256(Pixel::kGreen); - // out << L'\u2580'; - // out << L'\u2580'; - // out << L'\u2580'; - // out << L'\u2580'; - // out << L'\n'; - - // cout << "\x1b[?25l"; // Hide cursor. - // for (int frame = 1; frame <= 1000; ++frame) { - // std::stringstream ss; - // ss << "\x1b[H\n"; - // char buf[128]; - // snprintf(buf, sizeof(buf), "rickroll/%08d.jpg", frame); - // PrintImage(ss, LoadJPEG(buf)); - // cout << ss.str(); - // // timespec req = {0, 50000000}; - // // nanosleep(&req, NULL); - // } - - // for (int code = 40; code < 256; ++code) { - // std::ostringstream out; - // string val; - // Pixel pix; - - // for (int n = 0; n < 10; ++n) { - // cout << "\x1b[38;5;" << code << "m" - // << wstring(80, kFullBlock) - // << "\x1b[0m\n"; - // } - // cout << "What dost thou see? "; - // do { std::cin >> val; } while (val == ""); - // pix = Pixel(val); - // out << "[0][" << code << "] = {" - // << static_cast(pix.red() * 255) << ", " - // << static_cast(pix.green() * 255) << ", " - // << static_cast(pix.blue() * 255) << "}," - // << "\n"; - // cout << "\n"; - - // for (int n = 0; n < 10; ++n) { - // cout << "\x1b[48;5;" << code << "m" - // << string(80, L' ') - // << "\x1b[0m\n"; - // } - // cout << "What dost thou see? "; - // do { std::cin >> val; } while (val == ""); - // pix = Pixel(val); - // out << "[1][" << code << "] = {" - // << static_cast(pix.red() * 255) << ", " - // << static_cast(pix.green() * 255) << ", " - // << static_cast(pix.blue() * 255) << "}," - // << "\n"; - // cout << "\n"; - - // std::ofstream("terminal.app.txt", std::ios_base::app) << out.str(); - // }