Exemplo n.º 1
0
 size_t operator()(const fs::path& key) const
 {
     size_t ret = 5381;
     const fs::path::value_type* str = key.c_str();
     size_t c;
     while ( (c = *str++) )
     {
         ret = ((ret << 5) + ret) + c; /* hash * 33 + c */
     }
     return ret;
 }
Exemplo n.º 2
0
		std::shared_ptr<image::image_impl_interface> create_image(const fs::path & p)
		{
			std::shared_ptr<image::image_impl_interface> ptr;

			auto ext = p.extension().native();
			if (ext.empty())
				return ptr;

			std::transform(ext.begin(), ext.end(), ext.begin(), [](int ch)
			{
				if ('A' <= ch && ch <= 'Z')
					ch -= ('A' - 'a');
				return ch;
			});

#if defined(NANA_WINDOWS)
			const wchar_t* ext_ico = L".ico";
			const wchar_t* ext_png = L".png";
			const wchar_t* ext_jpg = L".jpg";
			const wchar_t* ext_jpeg = L".jpeg";
#else
			const char* ext_ico = ".ico";
			const char* ext_png = ".png";
			const char* ext_jpg = ".jpg";
			const char* ext_jpeg = ".jpeg";
#endif
			do
			{
				if (ext_ico == ext)
				{
					ptr = std::make_shared<detail::image_ico>();
					break;
				}

				if (ext_png == ext)
				{
#if defined(NANA_ENABLE_PNG)
					ptr = std::make_shared<detail::image_png>();
#else
					return ptr;
#endif
					break;
				}

				if (ext_jpg == ext || ext_jpeg == ext)
				{
#if defined(NANA_ENABLE_JPEG)
					ptr = std::make_shared<detail::image_jpeg>();
#else
					return ptr;
#endif
					break;
				}
			} while (false);

			//Check for BMP
			if (!ptr)
			{
#ifndef NANA_MINGW
				std::ifstream ifs(p.c_str(), std::ios::binary);
#else
				std::ifstream ifs(to_osmbstr(to_utf8(p.native())).c_str(), std::ios::binary);
#endif
				if (ifs)
				{
					unsigned short meta = 0;
					ifs.read(reinterpret_cast<char*>(&meta), 2);
					if (*reinterpret_cast<const short*>("BM") == meta)
						ptr = std::make_shared<detail::image_bmp>();
					else if (*reinterpret_cast<const short*>("MZ") == meta)
						ptr = std::make_shared<detail::image_ico_resource>();
				}
			}

			return ptr;
		}