#!/usr/bin/python

import sys
import gd

__author__ = 'Neil Harvey <neilharvey@gmail.com>'



def drawStripe( im, color, x1, y1, xsize, ysize, horizontal ):
	# Create a tile.
	tile = gd.image( (4,4) )
	# Set a transparent background color.
	bgColor = tile.colorAllocate( (0,0,0) )
	tile.colorTransparent( bgColor )
	# Copy the drawing color from the image to the tile.
	fgColor = tile.colorAllocate( im.colorComponents( color ) )
	
	if horizontal:
		# Draw the horizontal pattern on the tile:
		# OOXX
		# OXXO
		# XXOO
		# XOOX
		tile.line( (2,0), (3,0), fgColor )
		tile.line( (1,1), (2,1), fgColor )
		tile.line( (0,2), (1,2), fgColor )
		tile.setPixel( (0,3), fgColor )
		tile.setPixel( (3,3), fgColor )
	else:
		# Draw the vertical pattern on the tile:
		# XXOO
		# XOOX
		# OOXX
		# OXXO
		tile.line( (0,0), (1,0), fgColor )
		tile.setPixel( (0,1), fgColor )
		tile.setPixel( (3,1), fgColor )
		tile.line( (2,2), (3,2), fgColor )
		tile.line( (1,3), (2,3), fgColor )
	im.setTile( tile )
	maxx = x1 + xsize
	maxy = y1 + ysize
	im.filledRectangle( (x1, y1), (maxx-1, maxy-1), gd.gdTiled)

	# As a convenience, return the starting position for the next stripe.
	if horizontal:
		return maxy
	else:
		return maxx



def main():
	# Read the stripes, one per line from stdin: r,g,b,pixels. r,g,b are
	# integers from 0-255.
	stripes = []
	width = 0
	for line in sys.stdin:
		r,g,b,pixels = map( int, line.strip().split(',') )
		width += pixels
		stripes.append( (r,g,b,pixels) )

	# Draw the stripes.
	height = width
	im = gd.image( (width, height) )
	bgColor = im.colorAllocate( (16,16,16) ) # background color
	y = 0
	for r,g,b,pixels in stripes:
		color = im.colorExact( (r,g,b) )
		if color == -1:
			color = im.colorAllocate( (r,g,b) )
			if color == -1:
				raise Exception, 'ran out of color indexes'
		y = drawStripe( im, color, 0, y, width, pixels, horizontal=True )
	x = 0
	for r,g,b,pixels in stripes:
		color = im.colorExact( (r,g,b) )
		if color == -1:
			color = im.colorAllocate( (r,g,b) )
			if color == -1:
				raise Exception, 'ran out of color indexes'
		x = drawStripe( im, color, x, 0, pixels, height, horizontal=False )
	# Output the finished tartan to stdout.
	im.writePng( sys.stdout )



if __name__ == '__main__':
	main()
