From d3a98cf6cbc3bd0b9efc570f58e8812c03931c18 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 16 Oct 2018 10:08:48 +0200 Subject: Original 5.40 --- utils/bin2c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 utils/bin2c (limited to 'utils/bin2c') diff --git a/utils/bin2c b/utils/bin2c new file mode 100755 index 0000000..7ed5e4c --- /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" -- cgit v1.2.3-55-g7522