Ejemplo n.º 1
0
std::shared_ptr<FIBITMAP> load_image(const std::wstring& filename)
{
	if(!boost::filesystem::exists(filename))
		BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));

	FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
	fif = FreeImage_GetFileTypeU(filename.c_str(), 0);
	if(fif == FIF_UNKNOWN) 
		fif = FreeImage_GetFIFFromFilenameU(filename.c_str());
		
	if(fif == FIF_UNKNOWN || !FreeImage_FIFSupportsReading(fif)) 
		BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format."));
		
	auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_LoadU(fif, filename.c_str(), 0), FreeImage_Unload);
		  
	if(FreeImage_GetBPP(bitmap.get()) != 32)
	{
		bitmap = std::shared_ptr<FIBITMAP>(FreeImage_ConvertTo32Bits(bitmap.get()), FreeImage_Unload);
		if(!bitmap)
			BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format."));			
	}

	//PNG-images need to be premultiplied with their alpha
	if(fif == FIF_PNG)
	{
		image_view<bgra_pixel> original_view(FreeImage_GetBits(bitmap.get()), FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get()));
		premultiply(original_view);
	}
	
	return bitmap;
}
Ejemplo n.º 2
0
BOOL fipImage::loadU(const wchar_t* lpszPathName, int flag) {
	FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;

	// check the file signature and get its format
	// (the second argument is currently not used by FreeImage)
	fif = FreeImage_GetFileTypeU(lpszPathName, 0);
	if(fif == FIF_UNKNOWN) {
		// no signature ?
		// try to guess the file format from the file extension
		fif = FreeImage_GetFIFFromFilenameU(lpszPathName);
	}
	// check that the plugin has reading capabilities ...
	if((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
		// Free the previous dib
		if(_dib) {
			FreeImage_Unload(_dib);			
		}
		// Load the file
		_dib = FreeImage_LoadU(fif, lpszPathName, flag);
		_bHasChanged = TRUE;
		if(_dib == NULL)
			return FALSE;
		return TRUE;
	}
	return FALSE;
}
	FREEIMAGE_BMP*
	FREEIMAGE_LoadImage(const UString& filePath)
	{
		FREEIMAGE_BMP* vix_bmp = new FREEIMAGE_BMP;
		vix_bmp->path = filePath;
		vix_bmp->name = getFileName(filePath);
		vix_bmp->format = FREEIMAGE_FormatFromExtension(getFileExtension(filePath, false));
		vix_bmp->data = NULL;
		vix_bmp->bitmap = NULL;

		/*Here we must */
		
		//Check file signature and deduce format
#ifdef UNICODE
		vix_bmp->format = FreeImage_GetFileTypeU(filePath.c_str());
#else
		vix_bmp->format = FreeImage_GetFileType(filePath.c_str());
#endif
		if (vix_bmp->format == FIF_UNKNOWN) {
#ifdef UNICODE
			vix_bmp->format = FreeImage_GetFIFFromFilenameU(filePath.c_str());
#else
			vix_bmp->format = FreeImage_GetFIFFromFilename(filePath.c_str());
#endif
		}
			
		//if still unknown, return NULL;
		if (vix_bmp->format == FIF_UNKNOWN)
			return NULL;

		//Check if FreeImage has reading capabilities
		if (FreeImage_FIFSupportsReading(vix_bmp->format)) {
#ifdef UNICODE
			//read image into struct pointer
			vix_bmp->bitmap = FreeImage_LoadU(vix_bmp->format, filePath.c_str());
#else
			vix_bmp->bitmap = FreeImage_Load(vix_bmp->format, filePath.c_str());
#endif
			
		}

		//If image failed to load, return NULL
		if (!vix_bmp->bitmap)
			return NULL;

		FreeImage_FlipVertical(vix_bmp->bitmap);

		//Retrieve image data
		vix_bmp->data = FreeImage_GetBits(vix_bmp->bitmap);
		//Retrieve image width
		vix_bmp->header.width = FreeImage_GetWidth(vix_bmp->bitmap);
		//Retrieve image height
		vix_bmp->header.height = FreeImage_GetHeight(vix_bmp->bitmap);
		if (vix_bmp->data == 0 || vix_bmp->header.width == 0 || vix_bmp->header.height == 0)
			return NULL;

		//return bitmap
		return vix_bmp;
	}
Ejemplo n.º 4
0
void FI(loadImageFile)(Bitmap& bitmap, const std::wstring& filename)
{
#ifdef GOSU_IS_WIN
    FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeU(filename.c_str());
    FIBITMAP* fib = FreeImage_LoadU(fif, filename.c_str(), GOSU_FIFLAGS);
#else
    std::string utf8Filename = wstringToUTF8(filename);
    FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(utf8Filename.c_str());
    FIBITMAP* fib = FreeImage_Load(fif, utf8Filename.c_str(), GOSU_FIFLAGS);
#endif
    checkForFreeImageErrors(fib != 0);
    fibToBitmap(bitmap, fib, fif);
}
Ejemplo n.º 5
0
static INT_PTR serviceLoad(WPARAM wParam, LPARAM lParam)
{
	char *lpszFilename = (char *)wParam;
	if(lpszFilename==NULL) return 0;
	FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;

	if(lParam & IMGL_WCHAR)
		fif = FreeImage_GetFileTypeU((wchar_t *)lpszFilename, 0);
	else
		fif = FreeImage_GetFileType(lpszFilename, 0);

	if(fif == FIF_UNKNOWN) {
		if(lParam & IMGL_WCHAR)
			fif = FreeImage_GetFIFFromFilenameU((wchar_t *)lpszFilename);
		else
			fif = FreeImage_GetFIFFromFilename(lpszFilename);
	}
	// check that the plugin has reading capabilities ...

	if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
		// ok, let's load the file
		FIBITMAP *dib;

		if (lParam & IMGL_WCHAR)
			dib = FreeImage_LoadU(fif, (wchar_t *)lpszFilename, 0);
		else
			dib = FreeImage_Load(fif, lpszFilename, 0);

		if(dib == NULL || (lParam & IMGL_RETURNDIB))
			return (INT_PTR)dib;

		HBITMAP hbm = FreeImage_CreateHBITMAPFromDIB(dib);
		FreeImage_Unload(dib);
		FI_CorrectBitmap32Alpha(hbm, FALSE);
		return ((INT_PTR)hbm);
	}
	return NULL;
}
Ejemplo n.º 6
0
	bool FreeImage::onLoad() {

		switch ( this -> loadingType ) {
			case LoadingType::EMPTY:
				{
					this -> freeImage = FreeImage_Allocate( this -> size.x, this -> size.y, this -> BPP, 0, 0, 0 );
					break;
				}
			case LoadingType::FILE: 
				{
					// Check the file signature and deduce its format.
					#ifdef WIN32
					FREE_IMAGE_FORMAT imageFormat = FreeImage_GetFileTypeU( fileNameW.toCString(), 0 );
					#else
					FREE_IMAGE_FORMAT imageFormat = FreeImage_GetFileType( fileName.toCString(), 0 );
					#endif

					// If still unknown, try to guess the file format from the file extension.  
					if ( imageFormat == FIF_UNKNOWN ) {
						#ifdef WIN32
						imageFormat = FreeImage_GetFIFFromFilenameU( fileNameW.toCString() );
						#else
						imageFormat = FreeImage_GetFIFFromFilename( fileName.toCString() );
						#endif
					}

					// If still unknown, return failure.  
					if ( imageFormat == FIF_UNKNOWN ) {
						error( String( "Free Image was unable to detect the file format : " ) << fileName );
						return false;
					}

					// Check that the plugin has reading capabilities and load the file.  
					if ( FreeImage_FIFSupportsReading( imageFormat ) ) {
						#ifdef WIN32
						this -> freeImage = FreeImage_LoadU( imageFormat, fileNameW.toCString() );
						#else
						this -> freeImage = FreeImage_Load( imageFormat, fileName.toCString() );
						#endif
					}

					if ( this -> freeImage == NULL ) {
						error( String( "Free Image was unable to load the picture : " ) << fileName );
						return false;
					}
					if ( this -> size.x == 0 || this -> size.y == 0 ) {
						this -> size.x = FreeImage_GetWidth( this -> freeImage );
						this -> size.y = FreeImage_GetHeight( this -> freeImage );
					}


					if ( this -> loadingFormat == Format::UNDEFINED ) {
						switch ( FreeImage_GetColorType( this -> freeImage ) ) {
							case FIC_PALETTE:
								_updateFormat( Format::R );
								break;
							case FIC_RGB:
								_updateFormat( Format::RGB );
								break;
							default:
								_updateFormat( Format::RGBA );
								break;
						}
					}

					log( this -> fileName << this -> size << " has been loaded successfully !" );
					break;
				}

		}


		//if we have to flip vertically.
		if ( this -> invertY )
			FreeImage_FlipVertical( this -> freeImage );
		//Change BPP
		if ( this -> BPP != FreeImage_GetBPP( this -> freeImage ) )
			_updateBPP();
		//resize
		if ( this -> size != Math::Vec2<Size>( FreeImage_GetWidth( this -> freeImage ), FreeImage_GetHeight( this -> freeImage ) ) )
			_updateSize();

		this -> stride = FreeImage_GetPitch( this -> freeImage );
		return true;
	}
Ejemplo n.º 7
0
		void OpenGL::LoadTexture( Gwen::Texture* pTexture )
		{
			const wchar_t *wFileName = pTexture->name.GetUnicode().c_str();

			FREE_IMAGE_FORMAT imageFormat = FreeImage_GetFileTypeU( wFileName );

			if ( imageFormat == FIF_UNKNOWN )
				imageFormat = FreeImage_GetFIFFromFilenameU( wFileName );

			// Image failed to load..
			if ( imageFormat == FIF_UNKNOWN )
			{
				pTexture->failed = true;
				return;
			}

			// Try to load the image..
			FIBITMAP* bits = FreeImage_LoadU( imageFormat, wFileName );
			if ( !bits )
			{
				pTexture->failed = true;
				return;
			}

			// Convert to 32bit
			FIBITMAP * bits32 = FreeImage_ConvertTo32Bits( bits );
			FreeImage_Unload( bits );
			if ( !bits32 )
			{
				pTexture->failed = true;
				return;
			}

			// Flip
			::FreeImage_FlipVertical( bits32 );


			// Create a little texture pointer..
			GLuint* pglTexture = new GLuint;

			// Sort out our GWEN texture
			pTexture->data = pglTexture;
			pTexture->width = FreeImage_GetWidth( bits32 );
			pTexture->height = FreeImage_GetHeight( bits32 );

			// Create the opengl texture
			glGenTextures( 1, pglTexture );
			glBindTexture( GL_TEXTURE_2D, *pglTexture );
			glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
			glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

			#ifdef FREEIMAGE_BIGENDIAN
			GLenum format = GL_RGBA;
			#else
			GLenum format = GL_BGRA;
			#endif

			glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, pTexture->width, pTexture->height, 0, format, GL_UNSIGNED_BYTE, (const GLvoid*)FreeImage_GetBits( bits32 ) );

			FreeImage_Unload( bits32 );

		}