X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?p=xscreensaver;a=blobdiff_plain;f=utils%2Fbin2c;fp=utils%2Fbin2c;h=7ed5e4c1670fe5526f27d8fdffeebea620889a8e;hp=0000000000000000000000000000000000000000;hb=78add6e627ee5f10e1fa6f3852602ea5066eee5a;hpb=39809ded547bdbb08207d3e514950425215b4410 diff --git a/utils/bin2c b/utils/bin2c new file mode 100755 index 00000000..7ed5e4c1 --- /dev/null +++ b/utils/bin2c @@ -0,0 +1,46 @@ +#!/bin/sh +# Copyright © 2018 Jamie Zawinski +# +# Permission to use, copy, modify, distribute, and sell this software and its +# documentation for any purpose is hereby granted without fee, provided that +# the above copyright notice appear in all copies and that both that +# copyright notice and this permission notice appear in supporting +# documentation. No representations are made about the suitability of this +# software for any purpose. It is provided "as is" without express or +# implied warranty. +# +# Converts a binary file to a C source code string, e.g., to embed the +# contents of a PNG file by including a .h file. +# +# Created: 7-Feb-2018. + +usage () { + echo "usage: $0 in.png out_png.h" >&2 + exit 1 +} + +if [ $# != 2 ]; then usage; fi + +IN="$1" +OUT="$2" + +NAME=`echo "$OUT" | sed \ + -e 's@^.*/@@' \ + -e 's/\.[^.]*$//' \ + -e 's/[-.]/_/g' \ + -e 's/^\([^a-z]\)/_\1/'`; + +if [ x"$PERL" = "x" ]; then PERL=perl ; fi + +# On Linux, we could do this and put the raw image into a .o data segment: +# $(LD) -r -b binary $< -o $@ +# but that doesn't work on MacOS. + +exec $PERL -0 -pe " + BEGIN { print \"#ifdef __GNUC__\\n\"; + print \"__extension__\\n\"; + print \"#endif\\n\"; + print \"static const unsigned char ${NAME}[] =\\n \\\"\"; } + END { print \"\\\";\\n\"; } + s/([^ -\041\043-\076\100-\133\135-\176])/sprintf(\"\\\\%03o\",ord(\$1))/gse;"\ + < "$IN" > "$OUT"