Blame src/gdpp.cxx

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 gdpp.cxx
Packit ed3af9
	\brief Implements the non-trivial methods of GD::Image.
Packit ed3af9
	
Packit ed3af9
	Implementation of the more complex methods defined
Packit ed3af9
	in gdpp.h.
Packit ed3af9
	Notably includes the methods which determine the image file
Packit ed3af9
	format of a file before reading it into memory.
Packit ed3af9
*/
Packit ed3af9
#ifdef __cplusplus
Packit ed3af9
#include "gdpp.h"
Packit ed3af9
Packit ed3af9
namespace GD
Packit ed3af9
	{
Packit ed3af9
	/**
Packit ed3af9
		Load an image from a file, after attempting to
Packit ed3af9
		determine it's image file format. 
Packit ed3af9
		Invoke CreateFrom with an already opened
Packit ed3af9
		pointer to a file containing the desired image. 
Packit ed3af9
		CreateFrom does not close the file.
Packit ed3af9
		\param[in] in An opened FILE * pointer.
Packit ed3af9
		\return true for success, or false if unable to load the image (most often because the 
Packit ed3af9
		file is corrupt or does not contain a recognized image format). 
Packit ed3af9
		You can call Width() and Height() member functions of the image to determine its size.
Packit ed3af9
	*/
Packit ed3af9
	bool Image::CreateFrom(FILE * in)
Packit ed3af9
		{
Packit ed3af9
		bool rtn;
Packit ed3af9
		int c = fgetc(in);
Packit ed3af9
		ungetc(c, in);
Packit ed3af9
		switch (c)
Packit ed3af9
			{
Packit ed3af9
		/* PNG
Packit ed3af9
		The first eight bytes of a PNG file always contain the following (decimal) values:
Packit ed3af9
		   0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A
Packit ed3af9
		   == .PNG\r\n.\n
Packit ed3af9
		*/
Packit ed3af9
#ifdef HAVE_LIBPNG
Packit ed3af9
		case 0x89: // PNG
Packit ed3af9
			rtn = CreateFromPng(in);
Packit ed3af9
			break;
Packit ed3af9
#endif
Packit ed3af9
		/* GIF
Packit ed3af9
			0x47 0x49 0x46
Packit ed3af9
		*/
Packit ed3af9
		case 0x47: // GIF
Packit ed3af9
			rtn = CreateFromGif(in);
Packit ed3af9
			break;
Packit ed3af9
#ifdef HAVE_LIBJPEG
Packit ed3af9
		/* JPEG
Packit ed3af9
		A JFIF-standard file will start with the four bytes (hex) FF D8 FF E0,
Packit ed3af9
		    followed by two variable bytes (often hex 00 10), followed by 'JFIF'.		
Packit ed3af9
		*/
Packit ed3af9
		case 0xFF: // JPEG
Packit ed3af9
			rtn = CreateFromJpeg(in);
Packit ed3af9
			break;
Packit ed3af9
#endif
Packit ed3af9
		/* WBMP
Packit ed3af9
		WBMP Type 0: B/W, Uncompressed bitmap is the only gd supported type		
Packit ed3af9
		*/
Packit ed3af9
		case 0x00: // WBMP
Packit ed3af9
			rtn = CreateFromWBMP(in);
Packit ed3af9
			break;
Packit ed3af9
		/* GD2
Packit ed3af9
			0x67 0x64 0x32 0x00 
Packit ed3af9
			== GD2\0
Packit ed3af9
		Starts with gd2 
Packit ed3af9
		*/
Packit ed3af9
		case 0x67: // GD2
Packit ed3af9
			rtn = CreateFromGd2(in);
Packit ed3af9
			break;
Packit ed3af9
		/* GD
Packit ed3af9
			0xFF 0xFE
Packit ed3af9
			or 
Packit ed3af9
			0xFF 0xFF 
Packit ed3af9
			Conflicts with Jpeg
Packit ed3af9
		*/
Packit ed3af9
		/* XBM
Packit ed3af9
			#define test_width 16
Packit ed3af9
			#define test_height 7
Packit ed3af9
		*/	
Packit ed3af9
		case 0x23: // XBM
Packit ed3af9
			rtn = CreateFromXbm(in);
Packit ed3af9
			break;
Packit ed3af9
		default:
Packit ed3af9
			rtn = false;
Packit ed3af9
			break;
Packit ed3af9
			}
Packit ed3af9
		return rtn; 
Packit ed3af9
		}
Packit ed3af9
Packit ed3af9
	/**
Packit ed3af9
		Load an image from a standard input stream, after attempting to
Packit ed3af9
		determine it's image file format. 
Packit ed3af9
		Invoke CreateFrom with an already opened stream
Packit ed3af9
		containing the desired image. 
Packit ed3af9
		CreateFrom does not close the stream.
Packit ed3af9
		\param[in] in An opened standard library input stream.
Packit ed3af9
		\return true for success, or false if unable to load the image (most often because the 
Packit ed3af9
		file is corrupt or does not contain a recognized image format). 
Packit ed3af9
		You can call Width() and Height() member functions of the image to determine its size.
Packit ed3af9
		Example usage, convert anything to gif:
Packit ed3af9
		#include <fstream>
Packit ed3af9
		#include <gdpp.h>
Packit ed3af9
Packit ed3af9
		std::ifstream in("image.xxx", std::ios_base::in | std::ios_base::binary );
Packit ed3af9
		GD::Image im;
Packit ed3af9
		im.CreateFrom(in);
Packit ed3af9
		if (im.good())
Packit ed3af9
			{
Packit ed3af9
			std::ofstream out("image.gif", std::ios_base::out | std::ios_base::binary );
Packit ed3af9
			im.Gif(out);
Packit ed3af9
			}
Packit ed3af9
	*/
Packit ed3af9
	bool Image::CreateFrom(std::istream & in)
Packit ed3af9
		{
Packit ed3af9
		bool rtn;
Packit ed3af9
		switch (in.peek())
Packit ed3af9
			{
Packit ed3af9
#ifdef HAVE_LIBPNG
Packit ed3af9
		/* PNG
Packit ed3af9
		The first eight bytes of a PNG file always contain the following (decimal) values:
Packit ed3af9
		   0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A
Packit ed3af9
		   == .PNG\r\n.\n
Packit ed3af9
		*/
Packit ed3af9
		case 0x89: // PNG
Packit ed3af9
			rtn = CreateFromPng(in);
Packit ed3af9
			break;
Packit ed3af9
#endif
Packit ed3af9
Packit ed3af9
		/* GIF
Packit ed3af9
			0x47 0x49 0x46
Packit ed3af9
		*/
Packit ed3af9
		case 0x47: // GIF
Packit ed3af9
			rtn = CreateFromGif(in);
Packit ed3af9
			break;
Packit ed3af9
Packit ed3af9
#ifdef HAVE_LIBJPEG
Packit ed3af9
		/* JPEG
Packit ed3af9
		A JFIF-standard file will start with the four bytes (hex) FF D8 FF E0,
Packit ed3af9
		    followed by two variable bytes (often hex 00 10), followed by 'JFIF'.		
Packit ed3af9
		*/
Packit ed3af9
		case 0xFF: // JPEG
Packit ed3af9
			rtn = CreateFromJpeg(in);
Packit ed3af9
			break;
Packit ed3af9
#endif
Packit ed3af9
Packit ed3af9
		/* WBMP
Packit ed3af9
		WBMP Type 0: B/W, Uncompressed bitmap is the only gd supported type		
Packit ed3af9
		*/
Packit ed3af9
		case 0x00: // WBMP
Packit ed3af9
			rtn = CreateFromWBMP(in);
Packit ed3af9
			break;
Packit ed3af9
		/* GD2
Packit ed3af9
			0x67 0x64 0x32 0x00 
Packit ed3af9
			== GD2\0
Packit ed3af9
		Starts with gd2 
Packit ed3af9
		*/
Packit ed3af9
		case 0x67: // GD2
Packit ed3af9
			rtn = CreateFromGd2(in);
Packit ed3af9
			break;
Packit ed3af9
		/* GD
Packit ed3af9
			0xFF 0xFE
Packit ed3af9
			or 
Packit ed3af9
			0xFF 0xFF 
Packit ed3af9
			Conflicts with Jpeg
Packit ed3af9
		*/
Packit ed3af9
		default:
Packit ed3af9
			rtn = false;
Packit ed3af9
			break;
Packit ed3af9
			}
Packit ed3af9
		return rtn; 
Packit ed3af9
		}
Packit ed3af9
Packit ed3af9
	/**
Packit ed3af9
		Load an image from an in-RAM memory block, after attempting to
Packit ed3af9
		determine it's image format. 
Packit ed3af9
		CreateFrom does not de-allocate the memory.
Packit ed3af9
		\param[in] size The byte count of the memory block.
Packit ed3af9
		\param[in] data A pointer to the memory block.
Packit ed3af9
		\return true for success, or false if unable to load the image (most often because the 
Packit ed3af9
		formatting is corrupt or does not contain a recognized image format). 
Packit ed3af9
		You can call Width() and Height() member functions of the image to determine its size.
Packit ed3af9
	*/
Packit ed3af9
	bool Image::CreateFrom(int size, void * data)
Packit ed3af9
		{
Packit ed3af9
		bool rtn;
Packit ed3af9
		switch (((unsigned char * )data)[0])
Packit ed3af9
			{
Packit ed3af9
Packit ed3af9
#ifdef HAVE_LIBPNG
Packit ed3af9
		/* PNG
Packit ed3af9
		The first eight bytes of a PNG file always contain the following (decimal) values:
Packit ed3af9
		   0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A
Packit ed3af9
		   == .PNG\r\n.\n
Packit ed3af9
		*/
Packit ed3af9
		case 0x89: // PNG
Packit ed3af9
			rtn = CreateFromPng(size, data);
Packit ed3af9
			break;
Packit ed3af9
#endif
Packit ed3af9
		/* GIF
Packit ed3af9
			0x47 0x49 0x46
Packit ed3af9
		*/
Packit ed3af9
		case 0x47: // GIF
Packit ed3af9
			rtn = CreateFromGif(size, data);
Packit ed3af9
			break;
Packit ed3af9
Packit ed3af9
#ifdef HAVE_LIBJPEG
Packit ed3af9
		/* JPEG
Packit ed3af9
		A JFIF-standard file will start with the four bytes (hex) FF D8 FF E0,
Packit ed3af9
		    followed by two variable bytes (often hex 00 10), followed by 'JFIF'.		
Packit ed3af9
		*/
Packit ed3af9
		case 0xFF: // JPEG
Packit ed3af9
			rtn = CreateFromJpeg(size, data);
Packit ed3af9
			break;
Packit ed3af9
#endif
Packit ed3af9
Packit ed3af9
		/* WBMP
Packit ed3af9
		WBMP Type 0: B/W, Uncompressed bitmap is the only gd supported type		
Packit ed3af9
		*/
Packit ed3af9
		case 0x00: // WBMP
Packit ed3af9
			rtn = CreateFromWBMP(size, data);
Packit ed3af9
			break;
Packit ed3af9
		/* GD2
Packit ed3af9
			0x67 0x64 0x32 0x00 
Packit ed3af9
			== GD2\0
Packit ed3af9
		Starts with gd2 
Packit ed3af9
		*/
Packit ed3af9
		case 0x67: // GD2
Packit ed3af9
			rtn = CreateFromGd2(size, data);
Packit ed3af9
			break;
Packit ed3af9
		/* GD
Packit ed3af9
			0xFF 0xFE
Packit ed3af9
			or 
Packit ed3af9
			0xFF 0xFF 
Packit ed3af9
			Conflicts with Jpeg
Packit ed3af9
		*/
Packit ed3af9
		default:
Packit ed3af9
			rtn = false;
Packit ed3af9
			break;
Packit ed3af9
			}
Packit ed3af9
		return rtn; 
Packit ed3af9
		}
Packit ed3af9
	} // namespace GD
Packit ed3af9
/**
Packit ed3af9
	Load an image from a standard input stream, regardless of it's image file format. 
Packit ed3af9
	You can call Width() and Height() member functions of the image to determine its size.
Packit ed3af9
	Example usage, convert anything to gif:
Packit ed3af9
	#include <fstream>
Packit ed3af9
	#include <gdpp.h>
Packit ed3af9
Packit ed3af9
	std::ifstream in("image.xxx", std::ios_base::in | std::ios_base::binary );
Packit ed3af9
	GD::Image im;
Packit ed3af9
	in >> im;
Packit ed3af9
	if (im.good())
Packit ed3af9
		{
Packit ed3af9
		std::ofstream out("image.gif", std::ios_base::out | std::ios_base::binary );
Packit ed3af9
		im.Gif(out);
Packit ed3af9
		}
Packit ed3af9
*/
Packit ed3af9
std::istream & operator>> (std::istream & in, GD::Image & img)
Packit ed3af9
	{
Packit ed3af9
	img.CreateFrom(in);
Packit ed3af9
	return in;
Packit ed3af9
	}
Packit ed3af9
Packit ed3af9
#endif /* __cplusplus */