?? plugin.cpp
字號:
// =====================================================================
// FreeImage Plugin Interface
//
// Design and implementation by
// - Floris van den Berg (flvdberg@wxs.nl)
// - Rui Lopes (ruiglopes@yahoo.com)
// - Detlev Vendt (detlev.vendt@brillit.de)
// - Petr Pytelka (pyta@lightcomp.com)
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// =====================================================================
#ifdef WIN32
#pragma warning (disable : 4786) // identifier was truncated to 'number' characters
#include <windows.h>
#include <io.h>
#else
#include <ctype.h>
#endif // WIN32
#include "FreeImage.h"
#include "Utilities.h"
#include "FreeImageIO.h"
#include "Plugin.h"
// =====================================================================
using namespace std;
// =====================================================================
// Plugin search list
// =====================================================================
const char *
s_search_list[] = {
"",
"plugins\\",
};
static int s_search_list_size = sizeof(s_search_list) / sizeof(char *);
static PluginList *s_plugins = NULL;
static int s_plugin_reference_count = 0;
// =====================================================================
// Reimplementation of stricmp (it is not supported on some systems)
// =====================================================================
int
FreeImage_stricmp(const char *s1, const char *s2) {
int c1, c2;
do {
c1 = tolower(*s1++);
c2 = tolower(*s2++);
} while (c1 && c1 == c2);
return c1 - c2;
}
// =====================================================================
// Implementation of PluginList
// =====================================================================
PluginList::PluginList() :
m_plugin_map(),
m_node_count(0) {
}
FREE_IMAGE_FORMAT
PluginList::AddNode(FI_InitProc init_proc, void *instance, const char *format, const char *description, const char *extension, const char *regexpr) {
if (init_proc != NULL) {
PluginNode *node = new PluginNode;
Plugin *plugin = new Plugin;
memset(plugin, 0, sizeof(Plugin));
// fill-in the plugin structure
init_proc(plugin, m_plugin_map.size());
// get the format string (two possible ways)
const char *the_format = NULL;
if (format != NULL)
the_format = format;
else if (plugin->format_proc != NULL)
the_format = plugin->format_proc();
// add the node if it wasn't there already
if (the_format != NULL) {
if (FindNodeFromFormat(the_format) == NULL) {
node->m_id = m_plugin_map.size();
node->m_instance = instance;
node->m_plugin = plugin;
node->m_format = format;
node->m_description = description;
node->m_extension = extension;
node->m_regexpr = regexpr;
node->m_next = NULL;
node->m_enabled = TRUE;
m_plugin_map[m_plugin_map.size()] = node;
return (FREE_IMAGE_FORMAT)node->m_id;
}
}
// something went wrong while allocating the plugin... cleanup
delete plugin;
delete node;
}
return FIF_UNKNOWN;
}
PluginNode *
PluginList::FindNodeFromFormat(const char *format) {
int count = 0;
for (map<int, PluginNode *>::iterator i = m_plugin_map.begin(); i != m_plugin_map.end(); ++i) {
const char *the_format = ((*i).second->m_format != NULL) ? (*i).second->m_format : (*i).second->m_plugin->format_proc();
if (FreeImage_stricmp(the_format, format) == 0)
return (*i).second;
count++;
}
return NULL;
}
PluginNode *
PluginList::FindNodeFromMime(const char *mime) {
int count = 0;
for (map<int, PluginNode *>::iterator i = m_plugin_map.begin(); i != m_plugin_map.end(); ++i) {
const char *the_mime = ((*i).second->m_plugin->mime_proc != NULL) ? (*i).second->m_plugin->mime_proc() : "";
if ((the_mime != NULL) && (strcmp(the_mime, mime) == 0))
return (*i).second;
count++;
}
return NULL;
}
PluginNode *
PluginList::FindNodeFromFIF(int node_id) {
map<int, PluginNode *>::iterator i = m_plugin_map.find(node_id);
if (i != m_plugin_map.end())
return (*i).second;
return NULL;
}
int
PluginList::Size() const {
return m_plugin_map.size();
}
BOOL
PluginList::IsEmpty() const {
return m_plugin_map.empty();
}
PluginList::~PluginList() {
for (map<int, PluginNode *>::iterator i = m_plugin_map.begin(); i != m_plugin_map.end(); ++i) {
#ifdef WIN32
if ((*i).second->m_instance != NULL)
FreeLibrary((HINSTANCE)(*i).second->m_instance);
#endif
delete (*i).second->m_plugin;
delete ((*i).second);
}
}
// =====================================================================
// Retrieve a pointer to the plugin list container
// =====================================================================
PluginList * DLL_CALLCONV
FreeImage_GetPluginList() {
return s_plugins;
}
// =====================================================================
// Plugin System Initialization
// =====================================================================
void DLL_CALLCONV
FreeImage_Initialise(BOOL load_local_plugins_only) {
if (s_plugin_reference_count++ == 0) {
// internal plugin initialization
s_plugins = new PluginList;
if (s_plugins) {
s_plugins->AddNode(InitBMP);
s_plugins->AddNode(InitICO);
s_plugins->AddNode(InitJPEG);
s_plugins->AddNode(InitMNG, NULL, "JNG", "JPEG Network Graphics", "jng", "");
s_plugins->AddNode(InitKOALA);
s_plugins->AddNode(InitIFF);
s_plugins->AddNode(InitMNG);
s_plugins->AddNode(InitPNM, NULL, "PBM", "Portable Bitmap (ASCII)", "pbm", "^P1");
s_plugins->AddNode(InitPNM, NULL, "PBMRAW", "Portable Bitmap (RAW)", "pbm", "^P4");
s_plugins->AddNode(InitPCD);
s_plugins->AddNode(InitPCX);
s_plugins->AddNode(InitPNM, NULL, "PGM", "Portable Greymap (ASCII)", "pgm", "^P2");
s_plugins->AddNode(InitPNM, NULL, "PGMRAW", "Portable Greymap (RAW)", "pgm", "^P5");
s_plugins->AddNode(InitPNG);
s_plugins->AddNode(InitPNM, NULL, "PPM", "Portable Pixelmap (ASCII)", "ppm", "^P3");
s_plugins->AddNode(InitPNM, NULL, "PPMRAW", "Portable Pixelmap (RAW)", "ppm", "^P6");
s_plugins->AddNode(InitRAS);
s_plugins->AddNode(InitTARGA);
s_plugins->AddNode(InitTIFF);
s_plugins->AddNode(InitWBMP);
s_plugins->AddNode(InitPSD);
s_plugins->AddNode(InitCUT);
s_plugins->AddNode(InitXBM);
s_plugins->AddNode(InitXPM);
s_plugins->AddNode(InitDDS);
s_plugins->AddNode(InitGIF);
// external plugin initialization
#ifdef WIN32
if (!load_local_plugins_only) {
int count = 0;
char buffer[MAX_PATH + 200];
char current_dir[2 * _MAX_PATH], module[2 * _MAX_PATH];
BOOL bOk = FALSE;
// store the current directory. then set the directory to the application location
if (GetCurrentDirectoryA(2 * _MAX_PATH, current_dir) != 0) {
if (GetModuleFileNameA(NULL, module, 2 * _MAX_PATH) != 0) {
char *last_point = strrchr(module, '\\');
if (last_point) {
*last_point = '\0';
bOk = SetCurrentDirectoryA(module);
}
}
}
// search for plugins
while (count < s_search_list_size) {
_finddata_t find_data;
long find_handle;
strcpy(buffer, s_search_list[count]);
strcat(buffer, "*.fip");
if ((find_handle= _findfirst(buffer, &find_data)) != -1L) {
do {
strcpy(buffer, s_search_list[count]);
strncat(buffer, find_data.name, MAX_PATH + 200);
HINSTANCE instance = LoadLibraryA(buffer);
if (instance != NULL) {
FARPROC proc_address = GetProcAddress(instance, "_Init@8");
if (proc_address != NULL)
s_plugins->AddNode((FI_InitProc)proc_address, (void *)instance);
else
FreeLibrary(instance);
}
} while (_findnext(find_handle, &find_data) != -1L);
_findclose(find_handle);
}
count++;
}
// restore the current directory
if (bOk)
SetCurrentDirectoryA(current_dir);
}
#endif // WIN32
}
}
}
void DLL_CALLCONV
FreeImage_DeInitialise() {
--s_plugin_reference_count;
if (s_plugin_reference_count == 0) {
delete s_plugins;
}
}
// =====================================================================
// Open and close a bitmap
// =====================================================================
void * DLL_CALLCONV
FreeImage_Open(PluginNode *node, FreeImageIO *io, fi_handle handle, BOOL open_for_reading) {
if (node->m_plugin->open_proc != NULL)
return node->m_plugin->open_proc(io, handle, open_for_reading);
return NULL;
};
void DLL_CALLCONV
FreeImage_Close(PluginNode *node, FreeImageIO *io, fi_handle handle, void *data) {
if (node->m_plugin->close_proc != NULL)
node->m_plugin->close_proc(io, handle, data);
};
// =====================================================================
// Plugin System Load/Save Functions
// =====================================================================
FIBITMAP * DLL_CALLCONV
FreeImage_LoadFromHandle(FREE_IMAGE_FORMAT fif, FreeImageIO *io, fi_handle handle, int flags) {
if ((fif >= 0) && (fif < FreeImage_GetFIFCount())) {
PluginNode *node = s_plugins->FindNodeFromFIF(fif);
if (node != NULL) {
if (node->m_enabled) {
if(node->m_plugin->load_proc != NULL) {
FIBITMAP *bitmap = NULL;
void *data = FreeImage_Open(node, io, handle, TRUE);
bitmap = node->m_plugin->load_proc(io, handle, -1, flags, data);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -