/**********************************************************************
 
	Copyright (C) 2003 Hirohisa MORI <joshua@nichibun.ac.jp>
 
	This program is free software; you can redistribute it 
	and/or modify it under the terms of the GLOBALBASE 
	Library General Public License (G-LGPL) as published by 

	http://www.globalbase.org/
 
	This program is distributed in the hope that it will be 
	useful, but WITHOUT ANY WARRANTY; without even the 
	implied warranty of MERCHANTABILITY or FITNESS FOR A 
	PARTICULAR PURPOSE.

**********************************************************************/



#include	<stdio.h>
#include	<string.h>
#include	"ppm_error.h"
#include	"ppm_types.h"
#include	"u_file.h"

int
write_ppm(PPM_INFO * p,unsigned char * data)
{
char buffer[100];
int i,len;
	if ( u_lseek(p->pi_fd,SEEK_SET,0) < 0 )
		return E_UNIX;
	sprintf(buffer,"P6\n%i %i\n%i\n",
		p->pi_width,
		p->pi_height,
		p->pi_max);
	len = strlen(buffer);
	if ( u_write(p->pi_fd,buffer,len) < len )
		return E_UNIX;
	for ( i = p->pi_width*p->pi_height ; i ; i -- ) {
		if ( u_write(p->pi_fd,data,3) < 3 )
			return E_UNIX;
		data += 3;
	}
	return E_OK;
}
