mirror of https://github.com/weiju/amiga-stuff
png2image: options added
png2image added the option to specify the variable name for the images and whether to generate for Intuition or pure data. Also generates code for the color palette now
This commit is contained in:
parent
44fff9e796
commit
f545f5dfa5
|
@ -7,6 +7,7 @@ from PIL import Image
|
|||
import argparse
|
||||
import math
|
||||
import sys
|
||||
import os
|
||||
|
||||
def chunks(l, n):
|
||||
for i in range(0, len(list(l)), n):
|
||||
|
@ -20,9 +21,11 @@ def color_to_plane_bits(color, depth):
|
|||
result[bit] = 1
|
||||
return result
|
||||
|
||||
def write_amiga_image(image, outfile):
|
||||
def write_amiga_image(image, outfile, img_name, use_intuition):
|
||||
imdata = im.getdata()
|
||||
width, height = im.size
|
||||
|
||||
# colors is a list of 3-integer lists ([[r1, g1, b1], ...])
|
||||
colors = [i for i in chunks([b for b in im.palette.tobytes()], 3)]
|
||||
depth = int(math.log(len(colors), 2))
|
||||
|
||||
|
@ -48,10 +51,11 @@ def write_amiga_image(image, outfile):
|
|||
planes[planeidx][pos] |= (1 << (15 - (x + i) % 16)) # 1 << ((x + i) % 16)
|
||||
x += 16
|
||||
|
||||
outfile.write('#include <intuition/intuition.h>\n\n')
|
||||
|
||||
if use_intuition:
|
||||
outfile.write('#include <intuition/intuition.h>\n\n')
|
||||
imgdata_varname = '%s_data' % img_name
|
||||
outfile.write("/* Ensure that this data is within chip memory or you'll see nothing !!! */\n");
|
||||
outfile.write('UWORD __chip imdata[] = {\n')
|
||||
outfile.write('UWORD __chip %s[] = {\n' % imgdata_varname)
|
||||
indent = 4
|
||||
for i, plane in enumerate(planes):
|
||||
outfile.write(' ' * indent + '// plane %d\n' % i)
|
||||
|
@ -63,16 +67,40 @@ def write_amiga_image(image, outfile):
|
|||
|
||||
|
||||
planepick = 2 ** depth - 1 # currently we always use all of the planes
|
||||
outfile.write('struct Image image = {\n')
|
||||
outfile.write(' ' * indent + '0, 0, %d, %d, %d, imdata,\n' % (width, height, depth));
|
||||
outfile.write(' ' * indent + '%d, 0, NULL\n' % planepick) # PlanePick, PlaneOnOff
|
||||
outfile.write('};\n')
|
||||
if use_intuition:
|
||||
outfile.write('struct Image %s = {\n' % img_name)
|
||||
outfile.write(' ' * indent + '0, 0, %d, %d, %d, %s,\n' % (width, height, depth, imgdata_varname));
|
||||
outfile.write(' ' * indent + '%d, 0, NULL\n' % planepick) # PlanePick, PlaneOnOff
|
||||
outfile.write('};\n\n')
|
||||
|
||||
outfile.write('UWORD %s_colors[%d] = {\n' % (img_name, len(colors)))
|
||||
colors4 = []
|
||||
for i, color in enumerate(colors):
|
||||
r, g, b = color
|
||||
r4 = r >> 4
|
||||
g4 = g >> 4
|
||||
b4 = b >> 4
|
||||
colors4.append('0x%x%x%x' % (r4, g4, b4))
|
||||
outfile.write((' ' * indent) + ','.join(colors4) + '\n')
|
||||
outfile.write('};\n\n')
|
||||
|
||||
DESCRIPTION = """png2image - Amiga Image Converter
|
||||
|
||||
This tool converts a PNG image to a C header file containing image
|
||||
information in planar format. Optionally it generates the data
|
||||
structures needed for usage as Intuition image."""
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description='Amiga Image converter')
|
||||
parser.add_argument('pngfile')
|
||||
parser.add_argument('--plain', default=False)
|
||||
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
description=DESCRIPTION)
|
||||
parser.add_argument('pngfile', help="input PNG file")
|
||||
parser.add_argument('headerfile', help="output header file")
|
||||
parser.add_argument('--img_name', default='image', help="variable name of the image")
|
||||
parser.add_argument('--use_intuition', action='store_true', help="generate data for Intuition")
|
||||
|
||||
args = parser.parse_args()
|
||||
im = Image.open(args.pngfile)
|
||||
write_amiga_image(im, sys.stdout)
|
||||
if not os.path.exists(args.headerfile):
|
||||
with open(args.headerfile, 'w') as outfile:
|
||||
write_amiga_image(im, outfile, img_name=args.img_name, use_intuition=args.use_intuition)
|
||||
else:
|
||||
print("file '%s' already exists." % args.headerfile)
|
||||
|
|
Loading…
Reference in New Issue