summaryrefslogtreecommitdiff
path: root/src/image_data/png_to_c_header.sh
blob: 65f7c266b0ef3f92272a8e819ece336b1aa0da4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/sh
#
#/****  BEGIN - Self compiling script ****\

##
## This script is meant to generate a header file from
## png image data.  The process is to take a png file,
## pass it through gdk-pixbuf-csource and then to
## reformat the generated header by generating c code
## that will include the data array part of the
## cdk-pixbuf-csource header and convert/output a non-
## string version of the same data.
##
## NOTE: This file will be duplicated to generate the
##       c code and the first three lines will be
##       modified to include a starting (c-style)
##       comment block to comment out all the
##       scripting, leaving the c-code at the bottom.
##       This is done to make sure that compilation
##       errors/warnings are tied to the right line
##       number.


##
## Make sure we were given a png file.
##

if [ "$1" = "" ]; then
    echo "Usage: $0 <FILE.PNG>"
    exit 1
fi



##
## Figure out what to name the new header file
##

PNG=$1
NAME=`echo $PNG | sed 's/.png/_pixdata/i'`
HEADER=${NAME}.h
TMP_HEADER="$0.tmp.header.h"

echo "PNG    : $PNG"
echo "NAME   : $NAME"
echo "HEADER : $HEADER"
echo ""



##
## Convert the PNG data to a temporary header
##

echo "Converting '$PNG' to temp header '$TMP_HEADER' ..."
gdk-pixbuf-csource --struct --name=${NAME} $PNG > $TMP_HEADER || exit 1



##
## Generate header to include for compilation
##

INC_HEADER="$0.inc.header.h"

echo "Generating include header '$INC_HEADER' ..."

echo "static char tmp_pixel_data[] = "              >  $INC_HEADER
sed -n '/[ \t]*\".*$/ p' $TMP_HEADER | sed 's/,$//' >> $INC_HEADER
echo ";"                                            >> $INC_HEADER



##
## Generate the code that will include the header above
##

TMP_CODE="$0.code.c"

echo "Generating conversion code '$TMP_CODE' ..."

let L="`wc -l < $0` - 3" # Skip the first three lines!
echo "#include \"$INC_HEADER\"" >   $TMP_CODE
echo ""                         >>  $TMP_CODE
echo "/""**"                    >>  $TMP_CODE
echo ""                         >>  $TMP_CODE
tail -n $L $0                   >>  $TMP_CODE



##
## Compile
##

TMP_BIN="$0.binary"

echo "Compiling conversion app '$TMP_BIN' ..."

# Compile Flags
CFLAGS="-g -Wall"

gcc $CFLAGS -o $TMP_BIN $TMP_CODE

RET="$?" > /dev/null
if [ "$RET" != "0" ]; then
    echo "Compilation error"
    exit 1
fi



##
## Execute code to do conversion.
##

echo "Converting $TMP_HEADER => ${HEADER}_MID ..."

./$TMP_BIN > ${HEADER}_MID



##
## Generate final header
##

echo "Generating final header '$HEADER' ..."

head -n 2 $TMP_HEADER                          >  $HEADER
echo "static guint8 ${NAME}_pixel_data[] = {"  >> $HEADER
cat ${HEADER}_MID                              >> $HEADER
echo ""                                        >> $HEADER
echo "};"                                      >> $HEADER

REGEXP="s/^  \(.*\/\* pixel_data.*\)/  ${NAME}_pixel_data \/\* pixel_data \*\//"

# gdk-pixbuf-csource adds 2 extra empty lines so get rid of one of them.
L=`wc -l < $TMP_HEADER`
let L="$L - `tail -n 2 $TMP_HEADER | grep '^[ \t]*$' | wc -l`"

head -n $L $TMP_HEADER | \
sed -n '/^  \"/ !p' | \
sed -n '/^\// !p' | \
sed -e "$REGEXP"                               >> $HEADER



##
## Cleanup
##

rm -rf $TMP_HEADER $INC_HEADER $TMP_CODE ${HEADER}_MID $TMP_BIN

echo ""
echo "Created '${HEADER}'"

exit

\***** END - Self compiling script *******/

#include <stdio.h>

int main(int argc, char **argv)
{
    char *data = tmp_pixel_data;
    size_t len = sizeof(tmp_pixel_data) -1; // -1 for string terminator '\0'
    size_t i;
    int output = 0;

    printf("  ");
    for ( i = 0; i < len; i++ ) {

        printf("0x%.2x", (0xFF & *data));
        output++;

        if ( i+1 < len ) {
            printf(",");
        }
        if ( output >= 15 ) {
            output = 0;
            printf("\n  ");
        }        

        data++;
    }

    return 0;
}