VOID png_write(struct RecorderData *data, APTR fh, APTR argb, ULONG modulo, ULONG width, ULONG height, ULONG dupcount)
{
	struct png_header ihdr;
	ULONG length, length2;
	UBYTE *buffer;

	buffer = data->writebuffer;

	ihdr.width = width;
	ihdr.height = height;
	ihdr.depth = 8;
	ihdr.colortype = 2; /* RGB */
	ihdr.compression = 0;
	ihdr.filter = 0;
	ihdr.interlaced = 0;

	length = write_chunk(buffer, sizeof(ihdr), LE_SWAPLONG_C(MAKE_ID('I','H','D','R')), &ihdr);
	length2 = png_encode(&buffer[length], data->writebuffersize - length - 8, argb, modulo, width, height);

	if (length2)
	{
		static const struct png_iend iend = { { 0, LE_SWAPLONG_C(MAKE_ID('I','E','N','D')) }, 0xae426082 };
		ULONG i;

		length += length2;

		qcopy((APTR)&iend, &buffer[length], sizeof(iend));

		for (i = 0; i < dupcount; i++)
		{
			dowrite(fh, buffer, length);
		}
	}
}
static void bg_update_proc(Layer *layer, GContext *ctx) {
	if(!is_encoding) {
		is_encoding = true;
		GBitmap *fb = graphics_capture_frame_buffer(ctx); 
		APP_LOG(APP_LOG_LEVEL_DEBUG, "Captured framebuffer");
		bmpData(fb);	
		APP_LOG(APP_LOG_LEVEL_DEBUG, "Converted bitmap");
		png_encode((const unsigned char*)imagedata);	
		APP_LOG(APP_LOG_LEVEL_DEBUG, "Encoded PNG");
		graphics_release_frame_buffer(ctx, fb); 
		APP_LOG(APP_LOG_LEVEL_DEBUG, "Released framebuffer");	
		//is_encoding = false;
	}

}