File: gd_io_stream.h

package info (click to toggle)
libgd2 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 10,360 kB
  • sloc: ansic: 48,194; sh: 5,603; cpp: 1,298; makefile: 323; perl: 225; tcl: 45
file content (125 lines) | stat: -rw-r--r-- 3,714 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* *****************************************************************************
** Initial file written and documented by:
** Kevin Shepherd <kshepherd@php.net> December 2007
** of Scarlet Line http://www.scarletline.com/
*******************************************************************************/
/** \file gd_io_stream.h
	\brief C++ standard library iostream specializations of gdIOCtx.

	Note that all of the methods defined in this header are internal to the
	libgd library, except for the constructors.
	Only the constructors are needed by a user of the libgd API.
	This file does not use or need gdpp.h, but if GD::Image is
	used, then C++ coding becomes even simpler, and the classes below
	become entirely hidden implementation details.
	Example usage, convert png to gif:
	#include <fstream>
	#include "gd_io_stream.h"
	std::ifstream in("image.png", std::ios_base::in | std::ios_base::binary );
	if (in.good())
		{
		istreamIOCtx _in_ctx(in);
		gdImagePtr im_in = gdImageCreateFromPngCtx ( & _in_ctx);
		std::ofstream out("image.gif", std::ios_base::out | std::ios_base::binary );
		ostreamIOCtx _out_ctx(out);
		gdImageGifCtx(im_in, & _out_ctx);
		}
	gdImageDestroy(im_in);
*/
#ifndef _gd_io_stream_h
#define _gd_io_stream_h
#ifdef __cplusplus

#include "gd.h"
#include <iostream>

/** Standard library input stream specialization of gdIOCtx
*/
class BGD_EXPORT_DATA_IMPL istreamIOCtx : public gdIOCtx
{
public:
	typedef std::istream	stream_type;
	/** Construct an instance of this input stream specialization,
		given an input stream.
		For example:
		std::ifstream in("image.png", std::ios_base::in | std::ios_base::binary );
		istreamIOCtx in_ctx(in);
	*/
	istreamIOCtx(stream_type & __stream) {
		init( & __stream);
	}

	static int	Getbuf (struct gdIOCtx * ctx, void * buf, int size);
	static int	Putbuf (struct gdIOCtx * , const void * , int );
	static void	Putchar (struct gdIOCtx * , int );
	static int	Getchar (struct gdIOCtx * ctx);
	static int	Seek (struct gdIOCtx * ctx, const int pos);
	static long	Tell (struct gdIOCtx * ctx);
	static void	FreeCtx (struct gdIOCtx * ctx);

	void init(stream_type * __stream) {
		getC = Getchar;
		putC = Putchar;
		getBuf = Getbuf;
		putBuf = Putbuf;
		tell = Tell;
		seek = Seek;
		gd_free = FreeCtx;
		_M_stream = __stream;
	}
private:
	stream_type *	_M_stream;
};
/** Allocate a new instance of the class
*/
inline gdIOCtx * gdNewIstreamCtx (std::istream * __stream)
{
	return new istreamIOCtx(* __stream);
}

/** Standard library output stream specialization of gdIOCtx
*/
class BGD_EXPORT_DATA_IMPL ostreamIOCtx : public gdIOCtx
{
public:
	typedef std::ostream	stream_type;
	/** Construct an instance of this output stream specialization,
		given an output stream.
		For example:
		std::ofstream out("image.gif", std::ios_base::out | std::ios_base::binary );
		ostreamIOCtx out_ctx(out);
	*/
	ostreamIOCtx(stream_type & __stream) {
		init( & __stream);
	}

	static int	Getbuf (struct gdIOCtx * , void * , int );
	static int	Putbuf (struct gdIOCtx * ctx, const void * buf, int size);
	static int	Getchar (struct gdIOCtx * );
	static void	Putchar (struct gdIOCtx * ctx, int a);
	static int	Seek (struct gdIOCtx * ctx, const int pos);
	static long	Tell (struct gdIOCtx * ctx);
	static void	FreeCtx (struct gdIOCtx * ctx);

	void init(stream_type * __stream) {
		getC = Getchar;
		putC = Putchar;
		getBuf = Getbuf;
		putBuf = Putbuf;
		tell = Tell;
		seek = Seek;
		gd_free = FreeCtx;
		_M_stream = __stream;
	}
private:
	stream_type *	_M_stream;
};
/** Allocate a new instance of the class
*/
inline gdIOCtx * gdNewOstreamCtx (std::ostream * __stream)
{
	return new ostreamIOCtx(* __stream);
}

#endif /* __cplusplus */
#endif /* _gd_io_stream_h */