resource_manager.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:8k
- /*
- * ===========================================================================
- * PRODUCTION $Log: resource_manager.cpp,v $
- * PRODUCTION Revision 1000.0 2004/06/01 21:29:26 gouriano
- * PRODUCTION PRODUCTION: IMPORTED [GCC34_MSVC7] Dev-tree R1.5
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: resource_manager.cpp,v 1000.0 2004/06/01 21:29:26 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Andrey Yazhuk
- *
- * File Description:
- */
-
- #include <ncbi_pch.hpp>
- #include <corelib/ncbifile.hpp>
- #include <gui/widgets/fl/resource_manager.hpp>
- #include <gui/utils/system_path.hpp>
- #include <FL/Fl.H>
- #include <FL/fl_draw.H>
- #include <FL/Fl_BMP_Image.H>
- #include <FL/Fl_GIF_Image.H>
- #include <util/image/image_io.hpp>
- BEGIN_NCBI_SCOPE
- ////////////////////////////////////////////////////////////////////////////////
- ///
- CPNGImageExt::CPNGImageExt(const char* filename)
- : Fl_PNG_Image(filename)
- {
- }
- void CPNGImageExt::DrawTransparent(Fl_Color back_color, int X, int Y, int W, int H,
- bool disabled, int cx, int cy)
- {
- if(d() == 4) {
- int n = w() * h() * d();
- TByte* new_data = new TByte[n];
- TByte** p_data = (TByte**) data();
- TByte* d = *p_data;
- TByte bk_red = back_color >> 24;
- TByte bk_green = back_color >> 16;
- TByte bk_blue = back_color >> 8;
-
- TByte dis_red, dis_green, dis_blue;
- const static double dis_K = 0.66;
- const double K = 1.0 - dis_K;
-
- if(disabled) {
- Fl::get_color(FL_GRAY, dis_red, dis_green, dis_blue);
- dis_red = (TByte) (dis_red * dis_K);
- dis_green = (TByte) (dis_green * dis_K);
- dis_blue = (TByte) (dis_blue * dis_K);
- }
-
- for( int i = 0; i < n; i += 4 ) { // iterate by pixels
- TByte kDst = d[i + 3];
- TByte kSrc = 255 - kDst;
-
- TByte red = d[i];
- TByte green = d[i + 1];
- TByte blue = d[i + 2];
-
- if(disabled) { // average with gray
- red = (TByte) (red * K + dis_red);
- green = (TByte) (red * K + dis_green);
- blue = (TByte) (red * K + dis_blue);
- }
- // alpha-blending with background
- new_data[i] = (red * kDst + bk_red * kSrc) / 255;
- new_data[i + 1] = (green * kDst + bk_green * kSrc) / 255;
- new_data[i + 2] = (blue * kDst + bk_blue * kSrc) / 255;
- new_data[i + 3] = 255;
- }
-
- uncache();
- array = new_data;
- draw(X, Y, W, H, cx, cy);
- array = d;
- delete new_data;
- } else {
- draw(X, Y, W, H, cx, cy);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////
- /// CResourceManager
- const char* kListDelim = ";";
- const char* kWhiteSpace = " /t/n";
- CResourceManager::CResourceManager(const string& dir_list)
- {
- m_DirList.clear();
- list<string> toks;
- NStr::Split(dir_list, kListDelim, toks);
- ITERATE( list<string>, it, toks) { // iterating through directories
- string dir = NStr::TruncateSpaces(*it);
- dir = CSystemPath::ResolvePath(dir);
-
- if(dir.size()) { // not empty
- CDirEntry entry(dir);
- if (entry.Exists()) { // and does exist
- m_DirList.push_back(dir);
- }
- }
- }
- }
- void CResourceManager::RegisterAlias(const string& alias,
- const string& local_path)
- {
- m_AliasToPath[alias] = local_path;
- }
- CFLTKImageHandle CResourceManager::GetImage(const string& alias)
- {
- TAliasToImageMap::iterator it = m_AliasToImage.find(alias);
- if(it == m_AliasToImage.end()) {
- string error;
- TAliasToPathMap::const_iterator itP = m_AliasToPath.find(alias);
- if(itP == m_AliasToPath.end()) {
- error = "Alias not registered";
- } else { // loading
- try {
- string full_path;
- ITERATE(TDirList, itDir, m_DirList) {
- full_path = *itDir;
- full_path += CDirEntry::GetPathSeparator();
- full_path += itP->second;
- CDirEntry entry(full_path);
- if(entry.Exists())
- break;
- }
- if(full_path.size() == 0) {
- error = "File "";
- error = itP->second + "" is not found";
- } else { // try to load file
- CImageIO::EType type = CImageIO::GetTypeFromFileName(full_path);
- TImage* pImage = NULL;
- switch(type) {
- case CImageIO::ePng: pImage = new CPNGImageExt(full_path.c_str()); break;
- case CImageIO::eBmp: pImage = new Fl_BMP_Image(full_path.c_str()); break;
- case CImageIO::eGif: pImage = new Fl_GIF_Image(full_path.c_str()); break;
- default: break;
- };
- if(pImage) {
- if(*pImage->data()) {
- CFLTKImageHandle handle(pImage);
- m_AliasToImage[alias] = handle;
- return handle;
- } else {
- error = "Invalid image format";
- }
- } else {
- error = "Unsupported image type "";
- error += full_path;
- error += '"';
- }
- }
-
- } catch(std::exception& e) {
- error = e.what();
- }
- catch(...) {
- error = "Unknown error";
- }
- }
- ERR_POST("CResourceManager: Error loading image for alias "" << alias << "", " << error << '.');
- return CFLTKImageHandle(NULL);
-
- } else {
- return it->second;
- }
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: resource_manager.cpp,v $
- * Revision 1000.0 2004/06/01 21:29:26 gouriano
- * PRODUCTION: IMPORTED [GCC34_MSVC7] Dev-tree R1.5
- *
- * Revision 1.5 2004/05/21 22:27:53 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.4 2004/05/13 17:25:01 yazhuk
- * Extended CPNGImageExt::DrawTransparent() to support grayed-out images
- *
- * Revision 1.3 2004/05/10 16:25:40 yazhuk
- * Addressed GCC warnings
- *
- * Revision 1.2 2004/05/03 19:48:53 yazhuk
- * Added Fl_PNG_Image class, implemented support for "standard" directories
- *
- * Revision 1.1 2004/04/22 16:56:41 yazhuk
- * Initial revision
- *
- *
- * ===========================================================================
- */