bool open(const std::experimental::filesystem::path& jpeg_file) override { auto fp = ::fopen(to_osmbstr(to_utf8(jpeg_file.native())).c_str(), "rb"); if(nullptr == fp) return false; bool is_opened = false; struct ::jpeg_decompress_struct jdstru; error_mgr jerr; jdstru.err = ::jpeg_std_error(&jerr.pub); jerr.pub.error_exit = _m_error_handler; if (!setjmp(jerr.setjmp_buf)) { ::jpeg_create_decompress(&jdstru); ::jpeg_stdio_src(&jdstru, fp); _m_read_jpg(jdstru); jpeg_finish_decompress(&jdstru); is_opened = true; } ::jpeg_destroy_decompress(&jdstru); ::fclose(fp); return is_opened; }
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; }