Blame src/gd_io_stream.h

Packit ed3af9
/* *****************************************************************************
Packit ed3af9
** $Id$
Packit ed3af9
** Initial file written and documented by:
Packit ed3af9
** Kevin Shepherd <kshepherd@php.net> December 2007
Packit ed3af9
** of Scarlet Line http://www.scarletline.com/
Packit ed3af9
*******************************************************************************/
Packit ed3af9
/** \file gd_io_stream.h
Packit ed3af9
	\brief C++ standard library iostream specializations of gdIOCtx.
Packit ed3af9
Packit ed3af9
	Note that all of the methods defined in this header are internal to the
Packit ed3af9
	libgd library, except for the constructors.
Packit ed3af9
	Only the constructors are needed by a user of the libgd API.
Packit ed3af9
	This file does not use or need gdpp.h, but if GD::Image is
Packit ed3af9
	used, then C++ coding becomes even simpler, and the classes below
Packit ed3af9
	become entirely hidden implementation details.
Packit ed3af9
	Example usage, convert png to gif:
Packit ed3af9
	#include <fstream>
Packit ed3af9
	#include "gd_io_stream.h"
Packit ed3af9
	std::ifstream in("image.png", std::ios_base::in | std::ios_base::binary );
Packit ed3af9
	if (in.good())
Packit ed3af9
		{
Packit ed3af9
		istreamIOCtx _in_ctx(in);
Packit ed3af9
		gdImagePtr im_in = gdImageCreateFromPngCtx ( & _in_ctx);
Packit ed3af9
		std::ofstream out("image.gif", std::ios_base::out | std::ios_base::binary );
Packit ed3af9
		ostreamIOCtx _out_ctx(out);
Packit ed3af9
		gdImageGifCtx(im_in, & _out_ctx);
Packit ed3af9
		}
Packit ed3af9
	gdImageDestroy(im_in);
Packit ed3af9
*/
Packit ed3af9
#ifdef __cplusplus
Packit ed3af9
#ifndef _gd_io_stream_h
Packit ed3af9
#define _gd_io_stream_h
Packit ed3af9
Packit ed3af9
#include "gd.h"
Packit ed3af9
#include <iostream>
Packit ed3af9
Packit ed3af9
/** Standard library input stream specialization of gdIOCtx
Packit ed3af9
*/
Packit ed3af9
class BGD_EXPORT_DATA_IMPL istreamIOCtx : public gdIOCtx
Packit ed3af9
{
Packit ed3af9
public:
Packit ed3af9
	typedef std::istream	stream_type;
Packit ed3af9
	/** Construct an instance of this input stream specialization,
Packit ed3af9
		given an input stream.
Packit ed3af9
		For example:
Packit ed3af9
		std::ifstream in("image.png", std::ios_base::in | std::ios_base::binary );
Packit ed3af9
		istreamIOCtx in_ctx(in);
Packit ed3af9
	*/
Packit ed3af9
	istreamIOCtx(stream_type & __stream) {
Packit ed3af9
		init( & __stream);
Packit ed3af9
	}
Packit ed3af9
Packit ed3af9
	static int	Getbuf (struct gdIOCtx * ctx, void * buf, int size);
Packit ed3af9
	static int	Putbuf (struct gdIOCtx * , const void * , int );
Packit ed3af9
	static void	Putchar (struct gdIOCtx * , int );
Packit ed3af9
	static int	Getchar (struct gdIOCtx * ctx);
Packit ed3af9
	static int	Seek (struct gdIOCtx * ctx, const int pos);
Packit ed3af9
	static long	Tell (struct gdIOCtx * ctx);
Packit ed3af9
	static void	FreeCtx (struct gdIOCtx * ctx);
Packit ed3af9
Packit ed3af9
	void init(stream_type * __stream) {
Packit ed3af9
		getC = Getchar;
Packit ed3af9
		putC = Putchar;
Packit ed3af9
		getBuf = Getbuf;
Packit ed3af9
		putBuf = Putbuf;
Packit ed3af9
		tell = Tell;
Packit ed3af9
		seek = Seek;
Packit ed3af9
		gd_free = FreeCtx;
Packit ed3af9
		_M_stream = __stream;
Packit ed3af9
	}
Packit ed3af9
private:
Packit ed3af9
	stream_type *	_M_stream;
Packit ed3af9
};
Packit ed3af9
/** Allocate a new instance of the class
Packit ed3af9
*/
Packit ed3af9
inline gdIOCtx * gdNewIstreamCtx (std::istream * __stream)
Packit ed3af9
{
Packit ed3af9
	return new istreamIOCtx(* __stream);
Packit ed3af9
}
Packit ed3af9
Packit ed3af9
/** Standard library output stream specialization of gdIOCtx
Packit ed3af9
*/
Packit ed3af9
class BGD_EXPORT_DATA_IMPL ostreamIOCtx : public gdIOCtx
Packit ed3af9
{
Packit ed3af9
public:
Packit ed3af9
	typedef std::ostream	stream_type;
Packit ed3af9
	/** Construct an instance of this output stream specialization,
Packit ed3af9
		given an output stream.
Packit ed3af9
		For example:
Packit ed3af9
		std::ofstream out("image.gif", std::ios_base::out | std::ios_base::binary );
Packit ed3af9
		ostreamIOCtx out_ctx(out);
Packit ed3af9
	*/
Packit ed3af9
	ostreamIOCtx(stream_type & __stream) {
Packit ed3af9
		init( & __stream);
Packit ed3af9
	}
Packit ed3af9
Packit ed3af9
	static int	Getbuf (struct gdIOCtx * , void * , int );
Packit ed3af9
	static int	Putbuf (struct gdIOCtx * ctx, const void * buf, int size);
Packit ed3af9
	static int	Getchar (struct gdIOCtx * );
Packit ed3af9
	static void	Putchar (struct gdIOCtx * ctx, int a);
Packit ed3af9
	static int	Seek (struct gdIOCtx * ctx, const int pos);
Packit ed3af9
	static long	Tell (struct gdIOCtx * ctx);
Packit ed3af9
	static void	FreeCtx (struct gdIOCtx * ctx);
Packit ed3af9
Packit ed3af9
	void init(stream_type * __stream) {
Packit ed3af9
		getC = Getchar;
Packit ed3af9
		putC = Putchar;
Packit ed3af9
		getBuf = Getbuf;
Packit ed3af9
		putBuf = Putbuf;
Packit ed3af9
		tell = Tell;
Packit ed3af9
		seek = Seek;
Packit ed3af9
		gd_free = FreeCtx;
Packit ed3af9
		_M_stream = __stream;
Packit ed3af9
	}
Packit ed3af9
private:
Packit ed3af9
	stream_type *	_M_stream;
Packit ed3af9
};
Packit ed3af9
/** Allocate a new instance of the class
Packit ed3af9
*/
Packit ed3af9
inline gdIOCtx * gdNewOstreamCtx (std::ostream * __stream)
Packit ed3af9
{
Packit ed3af9
	return new ostreamIOCtx(* __stream);
Packit ed3af9
}
Packit ed3af9
Packit ed3af9
#endif /* _gd_io_stream_h */
Packit ed3af9
#endif /* __cplusplus */