This repository was archived by the owner on Jul 5, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathGraphicsMagickNET.cpp
More file actions
151 lines (136 loc) · 5.72 KB
/
Copy pathGraphicsMagickNET.cpp
File metadata and controls
151 lines (136 loc) · 5.72 KB
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
//=================================================================================================
// Copyright 2014-2015 Dirk Lemstra <https://graphicsmagick.codeplex.com/>
//
// Licensed under the ImageMagick License (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
//
// http://www.imagemagick.org/script/license.php
//
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.
//=================================================================================================
#include "Stdafx.h"
#include "GraphicsMagickNET.h"
#include "Helpers\EnumHelper.h"
#include "Helpers\FileHelper.h"
using namespace System::IO;
using namespace System::Security;
namespace GraphicsMagick
{
//==============================================================================================
String^ GraphicsMagickNET::CheckDirectory(String^ path)
{
Throw::IfNullOrEmpty("path", path);
path = FileHelper::CheckForBaseDirectory(path);
path = Path::GetFullPath(path);
Throw::IfFalse("path", Directory::Exists(path), "Unable to find directory: {0}", path);
return path;
}
//==============================================================================================
void GraphicsMagickNET::CheckImageMagickFiles(String^ path)
{
for each (String^ graphicsMagickFile in _GraphicsMagickFiles)
{
String^ fileName = path + "\\" + graphicsMagickFile;
Throw::IfFalse("path", File::Exists(fileName), "Unable to find file: {0}", fileName);
}
}
//==============================================================================================
void GraphicsMagickNET::OnLog(const Magick::ExceptionType type, const char* text)
{
if (text == NULL)
return;
if (_LogEvent == nullptr)
return;
std::string message=std::string(text);
_LogEvent->Invoke(nullptr, gcnew LogEventArgs((ExceptionTypes)type, Marshaller::Marshal(message)));
}
//==============================================================================================
void GraphicsMagickNET::SetEnv(const char* name, String^ value)
{
std::string val;
_putenv_s(name, Marshaller::Marshal(value, val).c_str());
}
//==============================================================================================
void GraphicsMagickNET::Log::add(EventHandler<LogEventArgs^>^ handler)
{
_LogEvent += handler;
if (_LogDelegate != nullptr)
return;
_LogDelegate = gcnew MagickLogFuncDelegate(&OnLog);
MagickLib::SetLogMethod((MagickLib::LogMethod)Marshal::GetFunctionPointerForDelegate(_LogDelegate).ToPointer());
}
//==============================================================================================
void GraphicsMagickNET::Log::remove(EventHandler<LogEventArgs^>^ handler)
{
_LogEvent -= handler;
if (_LogEvent != nullptr)
return;
MagickLib::SetLogMethod((MagickLib::LogMethod)NULL);
_LogDelegate = nullptr;
}
//==============================================================================================
IEnumerable<MagickFormatInfo^>^ GraphicsMagickNET::SupportedFormats::get()
{
return MagickFormatInfo::All;
}
//==============================================================================================
String^ GraphicsMagickNET::Version::get()
{
Object^ title = (GraphicsMagickNET::typeid)->Assembly->GetCustomAttributes(AssemblyTitleAttribute::typeid, false)[0];
Object^ version = (GraphicsMagickNET::typeid)->Assembly->GetCustomAttributes(AssemblyFileVersionAttribute::typeid, false)[0];
return ((AssemblyTitleAttribute^)title)->Title + " " + ((AssemblyFileVersionAttribute^)version)->Version;
}
//==============================================================================================
MagickFormatInfo^ GraphicsMagickNET::GetFormatInformation(MagickFormat format)
{
for each (MagickFormatInfo^ formatInfo in SupportedFormats)
{
if (formatInfo->Format == format)
return formatInfo;
}
return nullptr;
}
//==============================================================================================
void GraphicsMagickNET::Initialize(String^ path)
{
path = CheckDirectory(path);
CheckImageMagickFiles(path);
SetEnv("MAGICK_CONFIGURE_PATH", path);
}
//==============================================================================================
void GraphicsMagickNET::SetLogEvents(ExceptionTypes events)
{
String^ eventFlags = nullptr;
if (events == ExceptionTypes::All)
{
eventFlags = "All";
}
else
{
List<String^>^ flags = gcnew List<String^>();
for each(ExceptionTypes flag in EnumHelper::GetFlags(events))
{
if (flag != ExceptionTypes::All)
flags->Add(Enum::GetName(ExceptionTypes::typeid, flag));
}
eventFlags = String::Join(",", flags->ToArray());
}
std::string logEvents;
Marshaller::Marshal(eventFlags, logEvents);
MagickLib::SetLogEventMask(logEvents.c_str());
}
//==============================================================================================
void GraphicsMagickNET::SetTempDirectory(String^ path)
{
SetEnv("MAGICK_TMPDIR", CheckDirectory(path));
}
//==============================================================================================
void GraphicsMagickNET::SetGhostscriptDirectory(String^ path)
{
SetEnv("MAGICK_GHOSTSCRIPT_PATH", CheckDirectory(path));
}
//==============================================================================================
}