lol, all this trouble, while it was just converting to octal. of course, now that I know it, it makes sense.
For those that want to convert their image to a const unsigned char, like gimp does it. Here is my python code (it uses PIL).
"""
Cuban-Pete 2011
- free as beer code snippet
- this code snippet creates from images in the "images" folder one images.c file
- the created file can be loaded in your C(++) program.
- so colors go from decimal to octal format
"""
import os, sys, Image
lol = ''
f = open('images.c', 'w') #global set file. overwriting is 'on'
def run_colors_and_print(image_location, image_name ):
image = Image.open(image_location) #open image
pix = image.load() #put info in pix
width = image.size[0]
height = image.size[1]
lol = '' #reset global! string, important
ten_count = 0
startbool = True #remove unneeded extra double quotes at start
for y in range(0,height):
for x in range(0,width):
if ten_count == 0:
if startbool == True:
lol = lol + "\n\t\""
startbool = False
else:
lol = lol + "\"\n\t\""
# putting in the RGBA.
temp = "\%o\%o\%o\%o" % (pix[x,y][0], pix[x,y][1], pix[x,y][2], pix[x,y][-1]);
lol = lol + temp
ten_count += 1
if ten_count == 10: #make lines as long as this value...
ten_count = 0
f.write("static const struct {")
f.write("\n\t unsigned int \t width;")
f.write("\n\t unsigned int \t height;")
f.write("\n\t unsigned int \t bytes_per_pixel;")
f.write("\n\t unsigned char \t pixel_data[%i * %i * 4 + 1];" % (width, height) )
#the image name is made here (currently set on "normal_" and followed by image file name
f.write("\n} normal_%s = {" % (image_name[ 0: len(image_name)-4 ]) ) #remove the ".png" tag and backslash
f.write("\n\t%i, %i, 4," % (width, height) )
f.write("%s\"," % (lol) ) #the pixel data written
f.write("\n};")
print "done %s" % (image_name)
## loop in the "images" folder to get all images
image_folder = "images/"
for subdir, dirs, files in os.walk(image_folder):
for file in files:
run_colors_and_print(image_folder+file, file)
f.write("\n\n")
f.close() #important!
##Ouput example below for a 2x2 image in all white:
##static const struct {
## unsigned int width;
## unsigned int height;
## unsigned int bytes_per_pixel;
## unsigned char pixel_data[2 * 2 * 4 + 1];
##} gimp_image = {
## 2, 2, 4,
## "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377",
##};