summaryrefslogtreecommitdiff
path: root/src/gdpp.cxx
blob: 2a4bf58f15a646f23e0da830f0d9c548cab27ece (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#ifdef HAVE_CONFIG_H
#	include "config.h"
#endif

#ifdef ENABLE_CPP_API

/* *****************************************************************************
** Initial file written and documented by:
** Kevin Shepherd <kshepherd@php.net> December 2007
** of Scarlet Line http://www.scarletline.com/
*******************************************************************************/
/** \file gdpp.cxx
	\brief Implements the non-trivial methods of GD::Image.

	Implementation of the more complex methods defined
	in gdpp.h.
	Notably includes the methods which determine the image file
	format of a file before reading it into memory.
*/
#ifdef __cplusplus
#include "gdpp.h"

namespace GD
	{
	/**
		Load an image from a file, after attempting to
		determine it's image file format.
		Invoke CreateFrom with an already opened
		pointer to a file containing the desired image.
		CreateFrom does not close the file.
		\param[in] in An opened FILE * pointer.
		\return true for success, or false if unable to load the image (most often because the
		file is corrupt or does not contain a recognized image format).
		You can call Width() and Height() member functions of the image to determine its size.
	*/
	bool Image::CreateFrom(FILE * in)
		{
		bool rtn;
		int c = fgetc(in);
		ungetc(c, in);
		switch (c)
			{
		/* PNG
		The first eight bytes of a PNG file always contain the following (decimal) values:
		   0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A
		   == .PNG\r\n.\n
		*/
		case 0x89: // PNG
			rtn = CreateFromPng(in);
			break;

		/* GIF
			0x47 0x49 0x46
		*/
		case 0x47: // GIF
			rtn = CreateFromGif(in);
			break;
		/* JPEG
		A JFIF-standard file will start with the four bytes (hex) FF D8 FF E0,
		    followed by two variable bytes (often hex 00 10), followed by 'JFIF'.
		*/
		case 0xFF: // JPEG
			rtn = CreateFromJpeg(in);
			break;
		/* WBMP
		WBMP Type 0: B/W, Uncompressed bitmap is the only gd supported type
		*/
		case 0x00: // WBMP
			rtn = CreateFromWBMP(in);
			break;
		/* GD2
			0x67 0x64 0x32 0x00
			== GD2\0
		Starts with gd2
		*/
		case 0x67: // GD2
			rtn = CreateFromGd2(in);
			break;
		/* GD
			0xFF 0xFE
			or
			0xFF 0xFF
			Conflicts with Jpeg
		*/
		/* XBM
			#define test_width 16
			#define test_height 7
		*/
		case 0x23: // XBM
			rtn = CreateFromXbm(in);
			break;
		default:
			rtn = false;
			break;
			}
		return rtn;
		}

	/**
		Load an image from a standard input stream, after attempting to
		determine it's image file format.
		Invoke CreateFrom with an already opened stream
		containing the desired image.
		CreateFrom does not close the stream.
		\param[in] in An opened standard library input stream.
		\return true for success, or false if unable to load the image (most often because the
		file is corrupt or does not contain a recognized image format).
		You can call Width() and Height() member functions of the image to determine its size.
		Example usage, convert anything to gif:
		#include <fstream>
		#include <gdpp.h>

		std::ifstream in("image.xxx", std::ios_base::in | std::ios_base::binary );
		GD::Image im;
		im.CreateFrom(in);
		if (im.good())
			{
			std::ofstream out("image.gif", std::ios_base::out | std::ios_base::binary );
			im.Gif(out);
			}
	*/
	bool Image::CreateFrom(std::istream & in)
		{
		bool rtn;
		switch (in.peek())
			{
		/* PNG
		The first eight bytes of a PNG file always contain the following (decimal) values:
		   0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A
		   == .PNG\r\n.\n
		*/
		case 0x89: // PNG
			rtn = CreateFromPng(in);
			break;

		/* GIF
			0x47 0x49 0x46
		*/
		case 0x47: // GIF
			rtn = CreateFromGif(in);
			break;

		/* JPEG
		A JFIF-standard file will start with the four bytes (hex) FF D8 FF E0,
		    followed by two variable bytes (often hex 00 10), followed by 'JFIF'.
		*/
		case 0xFF: // JPEG
			rtn = CreateFromJpeg(in);
			break;

		/* WBMP
		WBMP Type 0: B/W, Uncompressed bitmap is the only gd supported type
		*/
		case 0x00: // WBMP
			rtn = CreateFromWBMP(in);
			break;
		/* GD2
			0x67 0x64 0x32 0x00
			== GD2\0
		Starts with gd2
		*/
		case 0x67: // GD2
			rtn = CreateFromGd2(in);
			break;
		/* GD
			0xFF 0xFE
			or
			0xFF 0xFF
			Conflicts with Jpeg
		*/
		default:
			rtn = false;
			break;
			}
		return rtn;
		}

	/**
		Load an image from an in-RAM memory block, after attempting to
		determine it's image format.
		CreateFrom does not de-allocate the memory.
		\param[in] size The byte count of the memory block.
		\param[in] data A pointer to the memory block.
		\return true for success, or false if unable to load the image (most often because the
		formatting is corrupt or does not contain a recognized image format).
		You can call Width() and Height() member functions of the image to determine its size.
	*/
	bool Image::CreateFrom(int size, void * data)
		{
		bool rtn;
		switch (((unsigned char * )data)[0])
			{

		/* PNG
		The first eight bytes of a PNG file always contain the following (decimal) values:
		   0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A
		   == .PNG\r\n.\n
		*/
		case 0x89: // PNG
			rtn = CreateFromPng(size, data);
			break;

		/* GIF
			0x47 0x49 0x46
		*/
		case 0x47: // GIF
			rtn = CreateFromGif(size, data);
			break;

		/* JPEG
		A JFIF-standard file will start with the four bytes (hex) FF D8 FF E0,
		    followed by two variable bytes (often hex 00 10), followed by 'JFIF'.
		*/
		case 0xFF: // JPEG
			rtn = CreateFromJpeg(size, data);
			break;

		/* WBMP
		WBMP Type 0: B/W, Uncompressed bitmap is the only gd supported type
		*/
		case 0x00: // WBMP
			rtn = CreateFromWBMP(size, data);
			break;
		/* GD2
			0x67 0x64 0x32 0x00
			== GD2\0
		Starts with gd2
		*/
		case 0x67: // GD2
			rtn = CreateFromGd2(size, data);
			break;
		/* GD
			0xFF 0xFE
			or
			0xFF 0xFF
			Conflicts with Jpeg
		*/
		default:
			rtn = false;
			break;
			}
		return rtn;
		}
	} // namespace GD
/**
	Load an image from a standard input stream, regardless of it's image file format.
	You can call Width() and Height() member functions of the image to determine its size.
	Example usage, convert anything to gif:
	#include <fstream>
	#include <gdpp.h>

	std::ifstream in("image.xxx", std::ios_base::in | std::ios_base::binary );
	GD::Image im;
	in >> im;
	if (im.good())
		{
		std::ofstream out("image.gif", std::ios_base::out | std::ios_base::binary );
		im.Gif(out);
		}
*/
std::istream & operator>> (std::istream & in, GD::Image & img)
	{
	img.CreateFrom(in);
	return in;
	}

#endif /* __cplusplus */
#endif //ENABLE_CPP_API