gd を c から

  制作 2004/11/21



実装 : lightbox.lzh

  
// LboxGd.cpp: LboxGd クラスのインプリメンテーション
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LboxGd.h"

//////////////////////////////////////////////////////////////////////
// 構築/消滅
//////////////////////////////////////////////////////////////////////

#include "gd.h"
typedef gdImagePtr
(__stdcall *LPFUNC_gdImageCreateFrom)(
	FILE *
);
typedef void
(__stdcall *LPFUNC_gdImageDestroy)(
	gdImagePtr
);
typedef int
(__stdcall *LPFUNC_gdImageColor)(
	gdImagePtr,
	int,
	int,
	int
);
typedef void
(__stdcall *LPFUNC_gdImageSetThickness)(
	gdImagePtr,
	int
);
typedef void
(__stdcall *LPFUNC_gdImageSetStyle)(
	gdImagePtr,
	int *,
	int
);
typedef void
(__stdcall *LPFUNC_gdImageLine)(
	gdImagePtr,
	int,
	int,
	int,
	int,
	int
);
typedef void
(__stdcall *LPFUNC_gdImageSave)(
	gdImagePtr,
	FILE *
);
typedef void
(__stdcall *LPFUNC_gdImageJpeg)(
	gdImagePtr,
	FILE *,
	int
);
typedef void
(__stdcall *LPFUNC_gdImageRectangle)(
	gdImagePtr,
	int,int,
	int,int,
	int
);
typedef void
(__stdcall *LPFUNC_gdImageArc)(
	gdImagePtr,
	int,int,
	int,int,
	int,int,
	int
);
typedef void
(__stdcall *LPFUNC_gdImageCopyResampled)(
	gdImagePtr,gdImagePtr,
	int,int,
	int,int,
	int,int,
	int,int
);
typedef gdImagePtr
(__stdcall *LPFUNC_gdImageCreateTrueColor)(
	int, int
);

// *********************************************************
// コンストラクタ
// *********************************************************
LboxGd::LboxGd()
{
	this->Type.SetChar( 0, 0 );
	this->ErrMessage.SetChar( 0, 0 );
	this->lib = LoadLibrary( "bgd.dll" );
	this->gd = NULL;
}

// *********************************************************
// デストラクタ
// *********************************************************
LboxGd::~LboxGd()
{
	if ( this->gd != NULL ) {
		this->Destroy();
	}
	if ( this->lib != NULL ) {
		FreeLibrary( this->lib );
	}
}

// *********************************************************
// エラーメッセージのセット
// 戻り値 : 無し
// *********************************************************
void LboxGd::SetLibLoadErrorMessage( void )
{
	this->ErrMessage.operator = (
		"bgd.dll がインストールされていません"
	);
}
void LboxGd::SetFuncLoadErrorMessage( void )
{
	this->ErrMessage.operator = (
		"関数をロードできませんでした"
	);
}

// *********************************************************
// イメージの破棄
// 戻り値 : true 成功, false 失敗
// *********************************************************
void LboxGd::Destroy( void )
{
	if ( this->gd == NULL ) {
		return;
	}

	LPFUNC_gdImageDestroy Dll_gdImageDestroy;

	Dll_gdImageDestroy =
		(LPFUNC_gdImageDestroy)GetProcAddress(
			this->lib, "gdImageDestroy@4" );
	if ( Dll_gdImageDestroy == NULL ) {
		this->SetFuncLoadErrorMessage();
		return;
	}

	gdImagePtr im;
	
	im = (gdImagePtr)(this->gd);
	Dll_gdImageDestroy( im );
	this->gd = NULL;
		
	return;

}

// *********************************************************
// PNG のロード
// 戻り値 : true 成功, false 失敗
// *********************************************************
BOOL LboxGd::LoadPng( LboxString *LPath )
{
	return LboxGd::LoadPng(
		LPath->szLboxString
	);
}
BOOL LboxGd::LoadPng( LPTSTR lpPath )
{
	if ( this->lib == NULL ) {
		this->SetLibLoadErrorMessage();
		return false;
	}

	if ( this->gd != NULL ) {
		this->Destroy();
	}

	LPFUNC_gdImageCreateFrom Dll_gdImageCreateFromPng;

	Dll_gdImageCreateFromPng =
		(LPFUNC_gdImageCreateFrom)GetProcAddress(
			lib, "gdImageCreateFromPng@4"
		);
	if ( Dll_gdImageCreateFromPng == NULL ) {
		this->SetFuncLoadErrorMessage();
		return false;
	}

	gdImagePtr im;

	FILE *in;
	in = fopen( lpPath, "rb" );
	if ( in == NULL ) {
		this->ErrMessage.operator = (
			"ファイルをオープンできませんでした"
		);
		return false;
	}

	im = Dll_gdImageCreateFromPng( in );
	fclose(in);

	this->gd = (void *)im;
	this->Type.operator = ("PNG");

	return true;
}

// *********************************************************
// JPEG のロード
// 戻り値 : true 成功, false 失敗
// *********************************************************
BOOL LboxGd::LoadJpeg( LboxString *LPath )
{
	return LboxGd::LoadJpeg(
		LPath->szLboxString
	);
}
BOOL LboxGd::LoadJpeg( LPTSTR lpPath )
{
	if ( this->lib == NULL ) {
		this->SetLibLoadErrorMessage();
		return false;
	}

	if ( this->gd != NULL ) {
		this->Destroy();
	}

	LPFUNC_gdImageCreateFrom Dll_gdImageCreateFromJpeg;

	Dll_gdImageCreateFromJpeg =
		(LPFUNC_gdImageCreateFrom)GetProcAddress(
			lib, "gdImageCreateFromJpeg@4"
		);
	if ( Dll_gdImageCreateFromJpeg == NULL ) {
		this->SetFuncLoadErrorMessage();
		return false;
	}

	gdImagePtr im;

	FILE *in;
	in = fopen( lpPath, "rb" );
	if ( in == NULL ) {
		this->ErrMessage.operator = (
			"ファイルをオープンできませんでした"
		);
		return false;
	}

	im = Dll_gdImageCreateFromJpeg( in );
	fclose(in);

	this->gd = (void *)im;
	this->Type.operator = ("JPEG");

	return true;
}

// *********************************************************
// GIF のロード
// 戻り値 : true 成功, false 失敗
// *********************************************************
BOOL LboxGd::LoadGif( LboxString *LPath )
{
	return LboxGd::LoadGif(
		LPath->szLboxString
	);
}
BOOL LboxGd::LoadGif( LPTSTR lpPath )
{
	if ( this->lib == NULL ) {
		this->SetLibLoadErrorMessage();
		return false;
	}

	if ( this->gd != NULL ) {
		this->Destroy();
	}

	LPFUNC_gdImageCreateFrom Dll_gdImageCreateFromGif;

	Dll_gdImageCreateFromGif =
		(LPFUNC_gdImageCreateFrom)GetProcAddress(
			lib, "gdImageCreateFromJpeg@4"
		);
	if ( Dll_gdImageCreateFromGif == NULL ) {
		this->SetFuncLoadErrorMessage();
		return false;
	}

	gdImagePtr im;

	FILE *in;
	in = fopen( lpPath, "rb" );
	if ( in == NULL ) {
		this->ErrMessage.operator = (
			"ファイルをオープンできませんでした"
		);
		return false;
	}

	im = Dll_gdImageCreateFromGif( in );
	fclose(in);

	this->gd = (void *)im;
	this->Type.operator = ("GIF");

	return true;
}

// *********************************************************
// 拡張子によるロード
// 戻り値 : true 成功, false 失敗
// *********************************************************
BOOL LboxGd::Load( LboxString *LPath )
{
	return LboxGd::Load(
		LPath->szLboxString
	);
}
BOOL LboxGd::Load( LPTSTR lpPath )
{
	LboxString LString;
	LPTSTR ptr;
	
	LString.operator = (lpPath);
	LString.Upper();
	ptr = LString.FindExtension();
	if ( lstrcmp( ptr, ".PNG" ) == 0 ) {
		return LboxGd::LoadPng( lpPath );
	}
	if ( lstrcmp( ptr, ".JPEG" ) == 0 ) {
		return LboxGd::LoadJpeg( lpPath );
	}
	if ( lstrcmp( ptr, ".JPG" ) == 0 ) {
		return LboxGd::LoadJpeg( lpPath );
	}
	if ( lstrcmp( ptr, ".GIF" ) == 0 ) {
		return LboxGd::LoadGif( lpPath );
	}

	return false;
}

// *********************************************************
// カラー作成
// 戻り値 : true 成功, false 失敗
// *********************************************************
int LboxGd::CreateColor( int r, int g, int b )
{
	if ( this->lib == NULL ) {
		this->SetLibLoadErrorMessage();
		return false;
	}

	LPFUNC_gdImageColor Dll_gdImageColorAllocate;

	Dll_gdImageColorAllocate =
		(LPFUNC_gdImageColor)GetProcAddress(
			lib, "gdImageColorAllocate@16"
		);
	if ( Dll_gdImageColorAllocate == NULL ) {
		this->SetFuncLoadErrorMessage();
		return false;
	}

	gdImagePtr im;

	im = (gdImagePtr)(this->gd);
	int nRet;

	nRet = Dll_gdImageColorAllocate(im, r, g, b );

	return nRet;
}

// *********************************************************
// 線幅決定
// 戻り値 : true 成功, false 失敗
// *********************************************************
int LboxGd::SetLineWidth( int nWidth )
{
	if ( this->lib == NULL ) {
		this->SetLibLoadErrorMessage();
		return false;
	}

	LPFUNC_gdImageSetThickness Dll_gdImageSetThickness;

	Dll_gdImageSetThickness =
		(LPFUNC_gdImageSetThickness)GetProcAddress(
			lib, "gdImageSetThickness@8"
		);
	if ( Dll_gdImageSetThickness == NULL ) {
		this->SetFuncLoadErrorMessage();
		return false;
	}

	gdImagePtr im;

	im = (gdImagePtr)(this->gd);

	Dll_gdImageSetThickness(im, nWidth);

	return true;
}

// *********************************************************
// 線スタイル作成
// 戻り値 : true 成功, false 失敗
// *********************************************************
int LboxGd::SetStyle( int *Array, int nArray )
{
	if ( this->lib == NULL ) {
		this->SetLibLoadErrorMessage();
		return false;
	}

	LPFUNC_gdImageSetStyle Dll_gdImageSetStyle;

	Dll_gdImageSetStyle =
		(LPFUNC_gdImageSetStyle)GetProcAddress(
			lib, "gdImageSetStyle@12"
		);
	if ( Dll_gdImageSetStyle == NULL ) {
		this->SetFuncLoadErrorMessage();
		return false;
	}

	gdImagePtr im;

	im = (gdImagePtr)(this->gd);

	Dll_gdImageSetStyle(im, Array, nArray);

	return true;
}

// *********************************************************
// 直線の描画
// 戻り値 : true 成功, false 失敗
// *********************************************************
// スタイル使用
int LboxGd::Line( int x1, int y1, int x2, int y2 )
{
	if ( this->lib == NULL ) {
		this->SetLibLoadErrorMessage();
		return false;
	}

	LPFUNC_gdImageLine Dll_gdImageLine;

	Dll_gdImageLine =
		(LPFUNC_gdImageLine)GetProcAddress(
			lib, "gdImageLine@24"
		);
	if ( Dll_gdImageLine == NULL ) {
		this->SetFuncLoadErrorMessage();
		return false;
	}

	gdImagePtr im;

	im = (gdImagePtr)(this->gd);

	Dll_gdImageLine(im, x1, y1, x2, y2, gdStyled );

	return true;
}
// 色使用
int LboxGd::Line( int x1, int y1, int x2, int y2, int color )
{
	if ( this->lib == NULL ) {
		this->SetLibLoadErrorMessage();
		return false;
	}

	LPFUNC_gdImageLine Dll_gdImageLine;

	Dll_gdImageLine =
		(LPFUNC_gdImageLine)GetProcAddress(
			lib, "gdImageLine@24"
		);
	if ( Dll_gdImageLine == NULL ) {
		this->SetFuncLoadErrorMessage();
		return false;
	}

	gdImagePtr im;

	im = (gdImagePtr)(this->gd);

	Dll_gdImageLine(im, x1, y1, x2, y2, color );

	return true;
}

// *********************************************************
// 矩形の描画
// 戻り値 : true 成功, false 失敗
// *********************************************************
int LboxGd::Box(
	int x1, int y1, int x2, int y2, int color, int fill )
{
	if ( this->lib == NULL ) {
		this->SetLibLoadErrorMessage();
		return false;
	}

	LPFUNC_gdImageRectangle Dll_gdImageRectangle;

	if ( fill ) {
		Dll_gdImageRectangle =
			(LPFUNC_gdImageRectangle)GetProcAddress(
				lib, "imageFilledRectAngle@24"
			);
	}
	else {
		Dll_gdImageRectangle =
			(LPFUNC_gdImageRectangle)GetProcAddress(
				lib, "gdImageRectangle@24"
			);
	}
	if ( Dll_gdImageRectangle == NULL ) {
		this->SetFuncLoadErrorMessage();
		return false;
	}

	gdImagePtr im;

	im = (gdImagePtr)(this->gd);

	Dll_gdImageRectangle(im, x1, y1, x2, y2, color );

	return true;
}

// *********************************************************
// 楕円の描画
// 戻り値 : true 成功, false 失敗
// *********************************************************
int LboxGd::Arc( int x, int y, int w, int h, int color )
{
	if ( this->lib == NULL ) {
		this->SetLibLoadErrorMessage();
		return false;
	}

	return LboxGd::Arc(
		x, y,
		w, h,
		0, 359,
		color
	);
}
int LboxGd::Arc(
	int x, int y, int w, int h, int s, int e, int color )
{
	if ( this->lib == NULL ) {
		this->SetLibLoadErrorMessage();
		return false;
	}

	LPFUNC_gdImageArc Dll_gdImageArc;

	Dll_gdImageArc =
		(LPFUNC_gdImageArc)GetProcAddress(
			lib, "gdImageArc@32"
		);
	if ( Dll_gdImageArc == NULL ) {
		this->SetFuncLoadErrorMessage();
		return false;
	}

	gdImagePtr im;

	im = (gdImagePtr)(this->gd);

	Dll_gdImageArc(im, x, y, w, h, s, e, color );

	return true;
}

// *********************************************************
// PNG 保存
// 戻り値 : true 成功, false 失敗
// *********************************************************
BOOL LboxGd::SavePng( LboxString *LPath )
{
	return LboxGd::SavePng(
		LPath->szLboxString
	);
}
BOOL LboxGd::SavePng( LPTSTR lpPath )
{
	if ( this->lib == NULL ) {
		this->SetLibLoadErrorMessage();
		return false;
	}

	LPFUNC_gdImageSave Dll_gdImagePng;

	Dll_gdImagePng =
		(LPFUNC_gdImageSave)GetProcAddress(
			lib, "gdImagePng@8"
		);
	if ( Dll_gdImagePng == NULL ) {
		this->SetFuncLoadErrorMessage();
		return false;
	}

	FILE *out;
	out = fopen( lpPath, "wb" );
	if ( out == NULL ) {
		this->ErrMessage.operator = (
			"ファイルをオープンできませんでした"
		);
		return false;
	}

	gdImagePtr im;

	im = (gdImagePtr)(this->gd);

	Dll_gdImagePng(im, out);
	fclose( out );

	return true;
}

// *********************************************************
// JPEG 保存
// 戻り値 : true 成功, false 失敗
// *********************************************************
BOOL LboxGd::SaveJpeg( LboxString *LPath, int Quality )
{
	return LboxGd::SaveJpeg(
		LPath->szLboxString,
		Quality
	);
}
BOOL LboxGd::SaveJpeg( LPTSTR lpPath, int Quality )
{
	if ( this->lib == NULL ) {
		this->SetLibLoadErrorMessage();
		return false;
	}

	LPFUNC_gdImageJpeg Dll_gdImageJpeg;

	Dll_gdImageJpeg =
		(LPFUNC_gdImageJpeg)GetProcAddress(
			lib, "gdImageJpeg@12"
		);
	if ( Dll_gdImageJpeg == NULL ) {
		this->SetFuncLoadErrorMessage();
		return false;
	}

	FILE *out;
	out = fopen( lpPath, "wb" );
	if ( out == NULL ) {
		this->ErrMessage.operator = (
			"ファイルをオープンできませんでした"
		);
		return false;
	}

	gdImagePtr im;

	im = (gdImagePtr)(this->gd);

	Dll_gdImageJpeg(im, out, Quality);
	fclose( out );

	return true;
}

// *********************************************************
// GIF 保存
// 戻り値 : true 成功, false 失敗
// *********************************************************
BOOL LboxGd::SaveGif( LboxString *LPath )
{
	return LboxGd::SaveGif(
		LPath->szLboxString
	);
}
BOOL LboxGd::SaveGif( LPTSTR lpPath )
{
	if ( this->lib == NULL ) {
		this->SetLibLoadErrorMessage();
		return false;
	}

	LPFUNC_gdImageSave Dll_gdImageGif;

	Dll_gdImageGif =
		(LPFUNC_gdImageSave)GetProcAddress(
			lib, "gdImageGif@8"
		);
	if ( Dll_gdImageGif == NULL ) {
		this->SetFuncLoadErrorMessage();
		return false;
	}

	FILE *out;
	out = fopen( lpPath, "wb" );
	if ( out == NULL ) {
		this->ErrMessage.operator = (
			"ファイルをオープンできませんでした"
		);
		return false;
	}

	gdImagePtr im;

	im = (gdImagePtr)(this->gd);

	Dll_gdImageGif(im, out);
	fclose( out );

	return true;
}

// *********************************************************
// 拡張子による保存
// 戻り値 : true 成功, false 失敗
// *********************************************************
BOOL LboxGd::Save( LboxString *LPath, int Quality )
{
	return LboxGd::Save(
		LPath->szLboxString,
		Quality
	);
}
BOOL LboxGd::Save( LPTSTR lpPath, int Quality )
{
	LboxString LString;
	LPTSTR ptr;
	
	LString.operator = (lpPath);
	LString.Upper();
	ptr = LString.FindExtension();
	if ( lstrcmp( ptr, ".PNG" ) == 0 ) {
		return LboxGd::SavePng( lpPath );
	}
	if ( lstrcmp( ptr, ".JPEG" ) == 0 ) {
		return LboxGd::SaveJpeg( lpPath, Quality );
	}
	if ( lstrcmp( ptr, ".JPG" ) == 0 ) {
		return LboxGd::SaveJpeg( lpPath, Quality );
	}
	if ( lstrcmp( ptr, ".GIF" ) == 0 ) {
		return LboxGd::SaveGif( lpPath );
	}

	return false;
}

// *********************************************************
// 幅取得
// 戻り値 : 幅
// *********************************************************
int LboxGd::GetWidth( void )
{
	gdImagePtr im;

	im = (gdImagePtr)(this->gd);

	return im->sx;
}
// *********************************************************
// 高さ取得
// 戻り値 : 高さ
// *********************************************************
int LboxGd::GetHeight( void )
{
	gdImagePtr im;

	im = (gdImagePtr)(this->gd);

	return im->sy;
}
// *********************************************************
// 幅・高さ取得
// 戻り値 : 高さ
// *********************************************************
void LboxGd::GetImageSize( int *w, int *h )
{
	(*w) = this->GetWidth();
	(*h) = this->GetHeight();
}

// *********************************************************
// 伸縮コピー
// 戻り値 : true 成功, false 失敗
// *********************************************************
BOOL LboxGd::Copy( LboxGd *objGD, int nRate )
{
	if ( this->lib == NULL ) {
		this->SetLibLoadErrorMessage();
		return false;
	}

	LPFUNC_gdImageCopyResampled Dll_gdImageCopyResampled;

	Dll_gdImageCopyResampled =
		(LPFUNC_gdImageCopyResampled)GetProcAddress(
			lib, "gdImageCopyResampled@40"
		);
	if ( Dll_gdImageCopyResampled == NULL ) {
		this->SetFuncLoadErrorMessage();
		return false;
	}

	int w,w2;
	int h,h2;

	this->GetImageSize( &w, &h );
	w2 = (double)w * ( (double)nRate / (double)100 );
	h2 = (double)h * ( (double)nRate / (double)100 );

	gdImagePtr im_in;
	gdImagePtr im_out;

	if ( !(objGD->Create( w2, h2 )) ) {
		return false;
	}

	im_in = (gdImagePtr)(this->gd);
	im_out = (gdImagePtr)(objGD->gd);

	Dll_gdImageCopyResampled(
		im_out,
		im_in,
		0, 0, 0, 0,
		w2, h2,
		w, h
	);

	return true;
}

// *********************************************************
// 新規イメージ作成
// 戻り値 : true 成功, false 失敗
// *********************************************************
BOOL LboxGd::CreateJpeg( int w, int h )
{
	this->Type = "JPEG";
	return LboxGd::Create( w, h );
}
BOOL LboxGd::CreatePng( int w, int h )
{
	this->Type = "PNG";
	return LboxGd::Create( w, h );
}
BOOL LboxGd::Create( int w, int h )
{
	if ( this->lib == NULL ) {
		this->SetLibLoadErrorMessage();
		return false;
	}

	LPFUNC_gdImageCreateTrueColor Dll_gdImageCreateTrueColor;

	Dll_gdImageCreateTrueColor =
		(LPFUNC_gdImageCreateTrueColor)GetProcAddress(
			lib, "gdImageCreateTrueColor@8"
		);
	if ( Dll_gdImageCreateTrueColor == NULL ) {
		this->SetFuncLoadErrorMessage();
		return false;
	}

	if ( this->gd != NULL ) {
		this->Destroy();
	}

	gdImagePtr im;

	im = Dll_gdImageCreateTrueColor(
		w, h
	);
	this->gd = (void *)im;

	return true;
}
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ