Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C

A simple graphics library in C with BMP and WMF export

Rate me:
Please Sign up or sign in to vote.
4.46/5 (7 votes)
15 Jul 2011CPOL5 min read 32.8K   1.6K   14  
A graphics library to export graphical output to BMP or WMF.
/*------------------------------------------------------------------
	Copyright (c) Uisang Hwang 2011
	
	LibTG : Library for Technical Graph v0.01

	Tested on 
	Visual C++ 2005
	Visual C++ 2010
	Open Watcom C/C++ v1.9

	Permission to use, copy, modify, and distribute this software
	for any use is hereby granted provided
	this notice is kept intact within the source file
 ------------------------------------------------------------------*/

#ifndef __libtg_h__
#define __libtg_h__

#ifdef __cplusplus
extern "C" {
#endif

#define __EXPORT_BMP__
#define __EXPORT_WMF__

typedef unsigned char 	ubyte;
typedef unsigned short 	ushort;
typedef unsigned int 	ulong;

typedef short int 		TG_Short;
typedef float 			TG_Real;
typedef int 			TG_Symbol;

#define TG_Fail 					0x0000
#define TG_Success 					0x0001
#define TG_ERROR_INVALID_PAPERNAME 	0x0002

typedef unsigned int 	TG_Status;
typedef unsigned int 	TG_Error;
typedef ulong 			TG_Color;

/*... Default color ...*/

#define TG_BLACK 			0
#define TG_RED				1
#define TG_GREEN			2
#define TG_BLUE				3
#define TG_YELLOW			4
#define TG_CYAN				5
#define TG_MAGENTA			6
#define TG_BROWN			7
#define TG_PURPLE			8
#define TG_LIGHTGRAY		9
#define TG_DARKGRAY			10
#define TG_LIGHTBLUE		11
#define TG_LIGHTGREEN		12
#define TG_LIGHTCYAN		13
#define TG_LIGHTRED			14
#define TG_LIGHTMAGENTA		15
#define TG_WHITE			16
	
/*... Default Symbol ...*/

#define TG_SYM_CIRCLE		0
#define TG_SYM_DELTA		1
#define TG_SYM_GRADIENT		2
#define TG_SYM_RTRIANGLE	3
#define TG_SYM_LTRIANGLE	4
#define TG_SYM_DIAMOND		5
#define TG_SYM_SQUARE		6
#define TG_SYM_CUSTOM       7 /*... not supported ...*/
#define TG_SYM_CHAR         8 /*... not supported ...*/

typedef struct TG_RGB_
{
	TG_Color r,g,b;
}
	TG_RGB,
	*TG_RGB_Ptr;
	
typedef struct TG_Point_ 
{
	TG_Real x, y;
} 
	TG_Point,
	*TG_Point_Ptr;
	
typedef struct TG_Point3_ 
{
	TG_Real x, y, z;
} 
	TG_Point3,
	*TG_Point3_Ptr;
	
typedef struct TG_Sizef_
{
	TG_Real sx, sy, width, height;
}
	TG_Sizef,
	*TG_Sizef_Ptr;

typedef struct TG_Sizei_
{
	int sx, sy, width, height;
}
	TG_Sizei,
	*TG_Sizei_Ptr;

typedef struct TG_Rectangle_
{
	int sx, sy, ex, ey;
}
	TG_Rectangle,
	*TG_Rectangle_Ptr;

typedef TG_Sizef TG_FrameSize;
typedef TG_Sizef TG_PlotAreaSize;
typedef TG_Sizef TG_WorkspaceSize;
typedef TG_Sizei TG_PixelCoordFrame;
typedef TG_Sizei TG_PixelCoordPlotArea;
typedef TG_Sizei TG_PixelCoordWorkspace; /*... Paper ...*/

/*--------------------------------------------------------
	All thickness or size such as line, symbol outline is 
	the percentage of a frame height in logical unit.
	Logical unit is based on paper coordinate system
	in inch unit. 

	Polyline(TG_PlotZone_Ptr p, 
			 TG_Real*		 pointx, 
			 TG_Real*		 pointy, 
			 int			 npoint, 
			 TG_Color		 lc, 
			 TG_Real		 lthk)

	Polygon(TG_PlotZone_Ptr p, 
			TG_Real*		pointx, 
			TG_Real*		pointy, 
			int				npoint, 
			TG_Color		fc)

	Symbol(TG_PlotZone_Ptr	p, 
		   TG_Real			x, 
		   TG_Real			y, 
		   TG_Symbol		sym, 
		   TG_Real			size, 
		   TG_Color			fc, 
		   int				fill, 
		   TG_Color			lc, 
		   int				line, 
		   TG_Real			lthk)

 ------------------------------------------------------*/

typedef void(*LP_SET_COLOR_DEFAULT)(TG_Color);
typedef void(*LP_SET_COLOR_RGB)(TG_RGB_Ptr);
typedef void(*LP_GSAVE)(void);
typedef void(*LP_GRESTORE)(void);
typedef void(*LP_MOVETO)(struct TG_PlotZone_*,TG_Real, TG_Real);
typedef void(*LP_LINETO)(struct TG_PlotZone_*,TG_Real,TG_Real);
typedef void(*LP_MAKEPEN)(struct TG_PlotZone_*,TG_Color,TG_Real);
typedef void(*LP_DELETEPEN)(void);
typedef void(*LP_PUTPIXEL1)(struct TG_PlotZone_*,TG_Real,TG_Real,TG_Color);
typedef void(*LP_PUTPIXEL2)(struct TG_PlotZone_*,int,int,TG_Color);
typedef void(*LP_LINE)(struct TG_PlotZone_*,TG_Real,TG_Real,TG_Real,TG_Real,TG_Color,TG_Real);
typedef void(*LP_POLYLINE)(struct TG_PlotZone_*,TG_Real*,TG_Real*,int,TG_Color,TG_Real);
typedef void(*LP_POLYGON)(struct TG_PlotZone_*,TG_Real*,TG_Real*,int,TG_Color);
typedef void(*LP_SYMBOL)(struct TG_PlotZone_*,TG_Real,TG_Real,TG_Symbol,TG_Real,TG_Color,int,TG_Color,int,TG_Real);

typedef struct TG_Paper_
{
	char series_name[10];
	TG_Real width_inch, height_inch;
	TG_Real width_mm, height_mm;
}
	TG_Paper,
	*TG_Paper_Ptr;

typedef struct TG_Frame_ 
{
	TG_FrameSize frame_size;
	TG_PlotAreaSize plotarea_size;
	
	TG_Real fxl, fyl; /*... scaling factor for logical unit ...*/
	TG_Real fxp, fyp; /*... scaling factor for physical unit ...*/
	TG_Real fx;       /*... the smallest value of fxl, fyl ...*/
	TG_Real minx, maxx, miny, maxy; /*... World coordinate ...*/
} 
	TG_Frame,
	*TG_Frame_Ptr;
	
/*------------------------------------------------------
	World coord system: the origin is lower left corner
	Pixel coord system: the origin is upper left corner
 -----------------------------------------------------*/

typedef struct TG_Plot_ 
{
	LP_MAKEPEN makepen;
	LP_DELETEPEN deletepen;
	LP_PUTPIXEL1 putpixel1; /*... world coord w/ TG_Color ...*/
	LP_PUTPIXEL2 putpixel2; /*... pixel coord w/ TG_Color ...*/
	LP_MOVETO moveto;       /*... world coord ...*/
	LP_LINETO lineto;       /*... world coord ...*/
	LP_LINE line;
	LP_GSAVE gsave;
	LP_GRESTORE grestore;
	LP_POLYLINE polyline;
	LP_POLYGON polygon;
	LP_SYMBOL symbol;
} 
	TG_Plot,
	*TG_Plot_Ptr;

typedef struct TG_PlotZone_
{
	TG_Frame frame;
	TG_Plot plot;
}
	TG_PlotZone,
	*TG_PlotZone_Ptr;
	
typedef struct TG_Workspace_
{
	TG_Paper paper;
	TG_WorkspaceSize size;
	TG_PlotZone zone;
} 
	TG_Workspace, 
	*TG_Workspace_Ptr;

extern TG_Workspace_Ptr TG_InitTGLibrary(const char* paper_name);
extern void TG_CloseTGLibrary(TG_Workspace_Ptr workspace);
extern void TG_SetFrame(TG_Frame_Ptr f, TG_Real sx, TG_Real sy, TG_Real width, TG_Real height, 
	TG_Real minx, TG_Real maxx, TG_Real miny, TG_Real maxy);


#ifdef __EXPORT_BMP__ /*... BITMAP ...*/
extern void TG_BMP_SetReverseWorldCoord(int coord);
extern int TG_BMP_OpenExport(TG_PlotZone* p, const char* file, int wid, int hgt, int memory);
extern void TG_BMP_CloseExport(void);
#endif

#ifdef __EXPORT_WMF__ /*... WINDOWSMETAFILE ...*/
extern void TG_WMF_SetReverseWorldCoord(int coord);
extern int TG_WMF_OpenExport(TG_PlotZone* p, const char* file);
extern void TG_WMF_CloseExport(void);
#endif

extern void TG_GetRGB(TG_Color c, int* r, int* g, int* b);
extern TG_Color TG_GetCustomColor(int r, int g, int b);
extern TG_Color TG_GetColor(int c);

#ifdef __cplusplus
}
#endif

#endif

/*... End of libtg.h ...*/

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
hus
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions