Skip to content

Instantly share code, notes, and snippets.

@coord-e
Last active December 19, 2017 12:41
Show Gist options
  • Save coord-e/119e3cf97520eb4c49758cbf803d3573 to your computer and use it in GitHub Desktop.
Save coord-e/119e3cf97520eb4c49758cbf803d3573 to your computer and use it in GitHub Desktop.
cart cart.cpp Dman.png -r 3 -c 1 -d > Dman.cpp
#include<iostream>
#include<numeric>
#include<algorithm>
#include<sstream>
#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<args.hxx>
#include<clang-c/Index.h>
#define ch ArgumentParser
#define bj CXFile
#define m CXGlobalOpt_None
#define ba CXToken
#define bv CXToken_Comment
#define bw CXToken_Punctuation
#define di Exception
#define cj HelpFlag
#define cx ParseCLI
#define cz ParseError
#define ck Positional
#define dh THRESH_BINARY
#define ce Traits
#define co ValueFlag
#define cq arg_cols
#define ct arg_define_shorten
#define cu arg_desth
#define cm arg_path
#define cp arg_rows
#define cl arg_source
#define cs arg_threshold
#define cw arg_verbose
#define ci argparser
#define dk assert
#define cf basic_ostream
#define o c_str
#define cy catch
#define bp cbegin
#define l clang_CXIndex_setGlobalOptions
#define k clang_createIndex
#define n clang_createTranslationUnitFromSourceFile
#define ca clang_disposeIndex
#define bx clang_disposeString
#define by clang_disposeTokens
#define bz clang_disposeTranslationUnit
#define bo clang_getCString
#define z clang_getCursorExtent
#define bm clang_getSpellingLocation
#define bu clang_getTokenKind
#define bn clang_getTokenLocation
#define bi clang_getTokenSpelling
#define u clang_getTranslationUnitCursor
#define be clang_tokenize
#define cd class
#define bk column
#define g const
#define s cursor
#define db define_shorten
#define da desth
#define cr double
#define h filepath
#define bt first
#define dj floor
#define de imread
#define dl imshow
#define j index
#define bq length
#define bf macro_holder
#define r nullptr
#define bd num_tokens
#define bl offset
#define cg operator
#define bs push_back
#define w range
#define dd raw_image
#define dp resize
#define cb return
#define br second
#define q sizeof
#define bh spelling
#define dn src_image
#define d string
#define cc template
#define df th_image
#define dg threshold
#define bg token
#define f tokenize
#define bb tokens
#define bc unsigned
#define a vector
#define dc verbose
#define dm waitKey
std ::a <std
::d >f (g std :: d &h ){std
::a <std ::d > dest ;auto
g j =k (1 ,0 );l (j ,m );g char
*args [ ]={ "-""X""c"
"l""a""n" "g","-"
"c""c""1" };auto
unit =n (j ,h .o
(),q ( args )/
q (char *),args
,0 ,r ) ;if (
unit != NULL )
{auto g s =u (unit
);auto g w =z (s
);ba *bb = r ;bc int
bd =0 ;be (unit ,w ,&bb ,&bd );std ::pair
<std :: d ,int >bf ={"" ,-1 };for (int i =0 ;i <bd ; ++i )
{auto g &bg =bb [i ];auto g bh =bi (unit ,bg );bj file ;bc line ,bk ,bl ;bm (bn (unit
,bg ), &file ,&line ,&bk ,&bl );std ::d thes =bo (bh );if (*std ::bp (thes )==
'"' && *(std ::cend (thes )-1 )=='"' &&thes .bq ()!=2 &&bf .br ==-1 ) {auto
it =std ::bp (thes )+1 ;auto end =std ::cend (thes )-1 ;for ( ;it
!=end ;it ++){if (*it =='\\' ){it ++;dest .bs (std :: d ("\""
"\\")+ *it +"\"");}else dest .bs (std ::d ( "\"")+*it + "\"")
;}}else if (*std ::bp (thes )=='#' &&bf .br ==-1 ){bf .bt ="#" ;bf .br
=line ;}else if (* std ::bp (thes )=='#' &&bf .br !=-1 &&bf .br
!=line ){dest .bs (bf . bt +'\n' );bf .bt ="#";bf . br =line ;}else {if
(bf .br != -1 ){if (bf . br ==line ){bf .bt +=thes ; thes ="" ;}else {dest
.bs (bf . bt );bf .bt ="" ; bf .br =-1 ;}}auto g kind =bu (bg );if (kind !=bv ){ if (kind
!=bw )dest .bs (thes +" " );else dest .bs (thes ) ;}}bx (bh );}by (unit ,bb ,bd ); bz (unit
);}else {std ::cerr <<"F""a""i""l""e" "d" " ""t""o"" ""t""o""k""e""n" "i""z"
"e"":"" ""\""<<h <<"\""<<std ::endl ;} ca (j );cb dest ;}cc <cd Char ,cd ce ,cd T >std
::cf <Char ,ce >&cg <<(std ::cf <Char ,ce >&os ,g std ::a <T >&vec ){os
<<'[' ;for (auto g &v :vec )os << '"' <<v <<"\""","" ";os <<']'
;cb os ;}int main (int argc ,char ** argv ){args ::ch ci ("c""a""r""t"":"
" ""C""o""n""v""e""r""t"" ""a" " ""c""/""c""+""+"" ""c""o""d""e"" "
"t""o"" ""a""s""c""i""i"" ""a""r" "t");args ::cj help (ci ,"h""e""l""p"
,"P""r""i""n""t"" ""t""h""i""s" " ""h""e""l""p",{'h' ,"h"
"e""l""p"});args ::ck <std ::d > cl (ci ,"s""o""u""r""c""e","P"
"a""t""h"" ""t""o"" ""c"" ""s""o" "u""r""c""e"" ""f""i""l""e"
);args ::ck <std ::d >cm (ci ,"i""m" "a""g""e","P""a""t""h"" "
"t""o"" ""i""m""a""g""e"" ""f""i" "l""e");args ::co <int >cp (
ci ,"r""o""w""s","N""u""m""b""e""r" " ""o""f"" ""r""o""w""s"" "
"(""=""5"")",{'r' ,"r""o""w""s" });args ::co <int >cq (ci ,"c"
"o""l""s","N""u""m""b""e""r"" ""o" "f"" ""c""o""l""s"" ""(""="
"5"")",{'c' ,"c""o""l""s"});args ::co <cr >cs (ci ,"t""h""r""e""s"
"h""o""l""d","t""h""r""e""s""h""o" "l""d"" ""(""=""1""5""0"")"
,{"t""h"});args ::Flag ct (ci ,"S""h" "o""r""t""e""n"" ""u""s""i"
"n""g"" ""#""d""e""f""i""n""e","U" "s""e"" ""#""d""e""f""i""n"
"e"" ""t""o"" ""s""h""o""r""t""e" "n"" ""t""o""k""e""n""s",{
'd' });args ::co <int >cu (ci ,"d""e" "f""i""n""e"" ""t""h""r""e"
"s""h""o""l""d","M""i""n""i""m""u" "m"" ""l""e""n""g""t""h"" ""o"
"f"" ""t""o""k""e""n"" ""(""=""5" ")",{"d""e""f""t""h"});args
::Flag cw (ci ,"v""e""r""b""o""s""e" ,"P""r""i""n""t"" ""v""e""r""b"
"o""s""e"" ""o""u""t""p""u""t"" " "a""n""d"" ""s""h""o""w"" ""i""m"
"a""g""e""s"" ""i""n"" ""p""r""o" "c""e""s""s",{'v' ,"v""e""r""b""o"
"s""e"});try {ci .cx (argc ,argv ) ;}cy (args ::Help ){std ::cout <<ci ;cb 0
;}cy (args ::cz e ){std ::cerr <<e .what ()<<std ::endl ;std ::cerr <<ci ;
cb -1 ;}if (!cm ){std ::cerr <<"S" "p""e""c""i""f""y"" ""a"" ""p""a""t""h"" ""t"
"o"" ""i""m""a""g""e"" ""f""i""l""e""."<<std ::endl ;std ::cerr <<ci ;cb -1 ;}if
(!cl ){std ::cerr <<"S""p""e""c""i""f""y"" ""a"" ""p""a""t""h"" ""t""o"" ""i""m"
"a""g""e"" ""f""i""l""e""."<<std ::endl ;std ::cerr <<ci ;cb -1 ;}auto g rows =cp ?
args ::get (cp ):5 ;auto g cols =cq ?args ::get (cq ):5 ;auto g path =args ::get (cm
);auto g th =cs ?args ::get (cs ):150 ;auto g da =cu ?args ::get (cu ):5 ;bool g db =ct ;bool
g dc =cw ;auto dd =cv ::de (path ,0 );if (!dd .data ){std ::cerr <<"F""a""i""l""e"
"d"" ""t""o"" ""l""o""a""d"" ""s""u""p""p""l""i""e""d"" ""i""m""a""g""e"":"
" "<<path <<std ::endl ;cb -1 ;}cv ::Mat df ;try {cv ::dg (dd ,df ,th ,255 ,
cv ::dh );}cy (cv ::di &ex ){std ::cerr <<"F""a""i""l""e""d"" ""t""o"" "
"a""p""p""l""y"" ""t""h""r""e""s""h""o""l""d""."<<std ::endl ;std
::cerr <<ex .what ()<<std ::endl ;cb -1 ;}int cn =std ::dj (df .
cols /cols )*cols ;int rn =std ::dj (df .rows /rows )*rows
;dk (! (rn %
rows ||cn
%cols ));if
(dc ){ std ::
cerr <<"R"
"e""s" "i""z"
"e""d" " ""i"
"m""a" "g""e"
":"" " <<rn
<<'x' <<cn
<<std ::endl
;cv :: dl ("t"
"h""r" "e""s"
"h""o" "l""d"
,df );cv ::dm (0 );}cv ::Mat dn ;try {cv
::dp (df ,dn ,cv ::Size (cn ,rn ));}cy ( cv
:: di &ex ){std ::cerr <<
"F" "a" "i" "l"
"e" "d" " " "t""o"" "
"r""e" "s""i" "z""e"" ""i""m""a"
"g""e""."<<std ::endl ;std ::
cerr <<ex .what ()<<std ::endl ;return -1 ;}auto tokens =tokenize (args ::get (arg_source ));if (tokens .empty ()){std ::cerr <<"F""a""i""l""e""d"" ""t""o"" ""t""o""k""e""n""i""z""e""."<<std ::endl ;return -1 ;}if (verbose )std ::cerr <<tokens <<std ::endl ;std ::vector <std ::string >prep {};std ::map <std ::string ,std ::string >defmap {};auto it =std ::begin (tokens );auto last =std ::remove_if (tokens .begin (),tokens .end (),[&prep ](auto &&x ){bool c =*std ::cbegin (x )=='#' ;if (c )prep .push_back (x );return c ;});int itr =0 ;std ::stringstream result ;for (int y =0 ;y <src_image .rows ;y +=rows ){for (int x =0 ;x <src_image .cols ;x +=cols ){auto part_image =src_image (cv ::Rect (x ,y ,cols ,rows ));int b =std ::count_if (part_image .begin <unsigned char >(),part_image .end <unsigned char >(),[](auto x )->bool {return x ;});if (b >part_image .total ()/2 ){if (verbose )std ::cerr <<"#";if (it ==last ){result <<"/""/";x +=cols *2 ;}else {if (*std ::cbegin (*it )=='#' ){result <<'\n' <<*it <<'\n' ;}else if (it ->length ()>desth &&*it ->begin ()!='"' &&*it ->end ()!='"' &&define_shorten ){if (!defmap .count (*it )){std ::string key ;do {int p =itr ++;if (!p )key ="a";else {int i ;char t [64 ]={};for (i =62 ;p ;--i ){t [i ]=p %26 +'a' ;p /=26 ;}key =std ::string (t +i +1 );}if (*(std ::cend (*it )-1 )==' ' )key +=" ";}while (std ::find (tokens .begin (),tokens .end (),key )!=tokens .end ());result <<key ;defmap .insert ({*it ,key });}else {result <<defmap [*it ];}}else {result <<*it ;x +=it ->length ()*cols ;}it ++;}}else {result <<" ";if (verbose )std ::cerr <<" ";}}result <<std ::endl ;if (verbose )std ::cerr <<std ::endl ;}if (it !=last ){std ::copy (it ,last ,std ::ostream_iterator <std ::string >(result ));}std ::copy (prep .begin (),prep .end (),std ::ostream_iterator <std ::string >(std ::cout ));std ::cout <<std ::endl ;for (auto const &p :defmap ){std ::cout <<"#""d""e""f""i""n""e"" "<<p .second <<" "<<p .first <<std ::endl ;}std ::copy (std ::istreambuf_iterator <char >(result ),std ::istreambuf_iterator <char >(),std ::ostreambuf_iterator <char >(std ::cout ));return 0 ;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment