std::unique_ptr<Image<OpenCVColorType>> OpenCVImageDenoiser::denoise(const ImageType& image) const { cv::Mat img(image.height(), image.width(), CV_8UC3); for (int i = 0; i < img.rows; ++i) for (int j = 0; j < img.cols; ++j) img.at<cv::Vec3b>(i, j) = image[{i, j}]; // cv::cvtColor(img, img, CV_BGR2GRAY); cv::Mat res(img.rows, img.cols, CV_8UC3); cv::fastNlMeansDenoisingColored(img, res); return std::make_unique<OpenCVImage>(res); }
void Texture::Load(const std::string& filename, bool mipmap/* = false*/) { if (m_opengl_id) Clear(); #if GG_USE_DEVIL_IMAGE_LOAD_LIBRARY InitDevIL(); ILuint id, error; ilGenImages(1, &id); CheckILErrors("ilGenImages(1, &id)"); ilBindImage(id); CheckILErrors("ilBindImage(id)"); ilLoadImage(const_cast<char*>(filename.c_str())); if ((error = ilGetError()) != IL_NO_ERROR) { std::string call = "ilLoadImage(\"" + filename + "\")"; if (VERBOSE_DEVIL_ERROR_REPORTING) { std::cerr << "IL call \"" << call << "\" failed with IL error \"" << iluErrorString(error) << "\" (code " << error << ")\n"; } CheckILErrors(call); throw BadFile("Could not load temporary DevIL image from file \"" + filename + "\""); } m_filename = filename; m_default_width = X(ilGetInteger(IL_IMAGE_WIDTH)); CheckILErrors("ilGetInteger(IL_IMAGE_WIDTH)"); m_default_height = Y(ilGetInteger(IL_IMAGE_HEIGHT)); CheckILErrors("ilGetInteger(IL_IMAGE_HEIGHT)"); m_bytes_pp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL); CheckILErrors("ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL)"); m_format = ilGetInteger(IL_IMAGE_FORMAT); CheckILErrors("ilGetInteger(IL_IMAGE_FORMAT)"); if (m_format == IL_COLOR_INDEX) { m_format = IL_RGBA; m_type = IL_UNSIGNED_BYTE; m_bytes_pp = 4; ilConvertImage(m_format, m_type); CheckILErrors("ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE)"); } else { m_type = ilGetInteger(IL_IMAGE_TYPE); CheckILErrors("ilGetInteger(IL_IMAGE_TYPE)"); } ILubyte* image_data = ilGetData(); CheckILErrors("ilGetData()"); Init(m_default_width, m_default_height, image_data, m_format, m_type, m_bytes_pp, mipmap); ilDeleteImages(1, &id); CheckILErrors("ilDeleteImages(1, &id)"); #else namespace gil = boost::gil; namespace fs = boost::filesystem; BOOST_STATIC_ASSERT((sizeof(gil::gray8_pixel_t) == 1)); BOOST_STATIC_ASSERT((sizeof(gil::gray_alpha8_pixel_t) == 2)); BOOST_STATIC_ASSERT((sizeof(gil::rgb8_pixel_t) == 3)); BOOST_STATIC_ASSERT((sizeof(gil::rgba8_pixel_t) == 4)); typedef boost::mpl::vector4< gil::gray8_image_t, gil::gray_alpha8_image_t, gil::rgb8_image_t, gil::rgba8_image_t > ImageTypes; typedef gil::any_image<ImageTypes> ImageType; fs::path path(filename); if (!fs::exists(path)) throw BadFile("Texture file \"" + filename + "\" does not exist"); if (!fs::is_regular_file(path)) throw BadFile("Texture \"file\" \"" + filename + "\" is not a file"); std::string extension = boost::algorithm::to_lower_copy(path.extension().string()); ImageType image; try { // First attempt -- try just to read the file in one of the default // formats above. #if GG_HAVE_LIBJPEG if (extension == ".jpg" || extension == ".jpe" || extension == ".jpeg") gil::jpeg_read_image(filename, image); else #endif #if GG_HAVE_LIBPNG if (extension == ".png") gil::png_read_image(filename, image); else #endif #if GG_HAVE_LIBTIFF if (extension == ".tif" || extension == ".tiff") gil::tiff_read_image(filename, image); else #endif throw BadFile("Texture file \"" + filename + "\" does not have a supported file extension"); } catch (const std::ios_base::failure &) { // Second attempt -- If *_read_image() throws, see if we can convert // the image to RGBA. This is needed for color-indexed images. #if GG_HAVE_LIBJPEG if (extension == ".jpg" || extension == ".jpe" || extension == ".jpeg") { gil::rgba8_image_t rgba_image; gil::jpeg_read_and_convert_image(filename, rgba_image); image.move_in(rgba_image); } #endif #if GG_HAVE_LIBPNG if (extension == ".png") { gil::rgba8_image_t rgba_image; gil::png_read_and_convert_image(filename, rgba_image); image.move_in(rgba_image); } #endif #if GG_HAVE_LIBTIFF if (extension == ".tif" || extension == ".tiff") { gil::rgba8_image_t rgba_image; gil::tiff_read_and_convert_image(filename, rgba_image); image.move_in(rgba_image); } #endif } m_filename = filename; m_default_width = X(image.width()); m_default_height = Y(image.height()); m_type = GL_UNSIGNED_BYTE; #define IF_IMAGE_TYPE_IS(image_prefix) \ if (image.current_type_is<image_prefix ## _image_t>()) { \ m_bytes_pp = sizeof(image_prefix ## _pixel_t); \ image_data = interleaved_view_get_raw_data( \ const_view(image._dynamic_cast<image_prefix ## _image_t>())); \ } const unsigned char* image_data = 0; IF_IMAGE_TYPE_IS(gil::gray8) else IF_IMAGE_TYPE_IS(gil::gray_alpha8) else IF_IMAGE_TYPE_IS(gil::rgb8) else IF_IMAGE_TYPE_IS(gil::rgba8) #undef IF_IMAGE_TYPE_IS switch (m_bytes_pp) { case 1: m_format = GL_LUMINANCE; break; case 2: m_format = GL_LUMINANCE_ALPHA; break; case 3: m_format = GL_RGB; break; case 4: m_format = GL_RGBA; break; default: throw BadFile("Texture file \"" + filename + "\" does not have a supported number of color channels (1-4)"); } assert(image_data); Init(m_default_width, m_default_height, image_data, m_format, m_type, m_bytes_pp, mipmap); #endif }