示例#1
0
// Transmits an entire frame of imagery to the client
int sendFullFrame(SOCKET &clientSocket) {
	int status = 0;
	FIBITMAP *fiImage;
	FIMEMORY *fiBuffer;

	// Signal that a new frame is required and wait for frame
	InterlockedExchange( &g_lRequestFlag, TRUE );
	g_pRequestEvent->waitFor();
	
	// Enter critical section for frame buffer from UT2004
	// and copy new raw image to local buffer
	EnterCriticalSection( &g_CriticalSection );
	{
		fiImage = FreeImage_ConvertTo24Bits(g_fiImage);
	} 
	LeaveCriticalSection( &g_CriticalSection );

	// Create memory reference
	fiBuffer = FreeImage_OpenMemory();

	// Convert a raw frame to a useful image
	status = writeFrame( fiBuffer, fiImage, g_iImageType );
	if (status != 1) status = 0; // TODO: handle error here
	
	// Transmit frame over socket
	status = transmitFrame( fiBuffer, clientSocket, g_iImageType );

	// Delete memory references
	FreeImage_Unload( fiImage );
	FreeImage_CloseMemory( fiBuffer );

	return status;
}
示例#2
0
FIBITMAP *GraphicsHelps::loadImageRC(const char *file)
{
    unsigned char *memory = nullptr;
    size_t fileSize = 0;
    SDL_assert_release(RES_getMem(file, memory, fileSize));
    //{
        //pLogCritical("Resource file \"%s\" is not found!", file);
        //return nullptr;
    //}

    FIMEMORY *imgMEM = FreeImage_OpenMemory(memory, static_cast<FI_DWORD>(fileSize));
    FREE_IMAGE_FORMAT formato = FreeImage_GetFileTypeFromMemory(imgMEM);

    if(formato == FIF_UNKNOWN)
        return nullptr;

    FIBITMAP *img = FreeImage_LoadFromMemory(formato, imgMEM, 0);
    FreeImage_CloseMemory(imgMEM);

    if(!img)
        return nullptr;

    FIBITMAP *temp;
    temp = FreeImage_ConvertTo32Bits(img);

    if(!temp)
        return nullptr;

    FreeImage_Unload(img);
    img = temp;
    return img;
}
示例#3
0
    //---------------------------------------------------------------------
    DataStreamPtr FreeImageCodec::code(MemoryDataStreamPtr& input, Codec::CodecDataPtr& pData) const
    {        
		// Set error handler
		FreeImage_SetOutputMessage(FreeImageSaveErrorHandler);

		FIBITMAP* fiBitmap = encode(input, pData);

		// open memory chunk allocated by FreeImage
		FIMEMORY* mem = FreeImage_OpenMemory();
		// write data into memory
		FreeImage_SaveToMemory((FREE_IMAGE_FORMAT)mFreeImageType, fiBitmap, mem);
		// Grab data information
		BYTE* data;
		DWORD size;
		FreeImage_AcquireMemory(mem, &data, &size);
		// Copy data into our own buffer
		// Because we're asking MemoryDataStream to free this, must create in a compatible way
		BYTE* ourData = OGRE_ALLOC_T(BYTE, size, MEMCATEGORY_GENERAL);
		memcpy(ourData, data, size);
		// Wrap data in stream, tell it to free on close 
		DataStreamPtr outstream(OGRE_NEW MemoryDataStream(ourData, size, true));
		// Now free FreeImage memory buffers
		FreeImage_CloseMemory(mem);
		// Unload bitmap
		FreeImage_Unload(fiBitmap);

		return outstream;


    }
    //---------------------------------------------------------------------
    DataStreamPtr FreeImageCodec::code(MemoryDataStreamPtr& input, Codec::CodecDataPtr& pData) const
    {        
		FIBITMAP* fiBitmap = encode(input, pData);

		// open memory chunk allocated by FreeImage
		FIMEMORY* mem = FreeImage_OpenMemory();
		// write data into memory
		FreeImage_SaveToMemory((FREE_IMAGE_FORMAT)mFreeImageType, fiBitmap, mem);
		// Grab data information
		BYTE* data;
		DWORD size;
		FreeImage_AcquireMemory(mem, &data, &size);
		// Copy data into our own buffer
		BYTE* ourData = new BYTE[size];
		memcpy(ourData, data, size);
		// Wrap data in stream, tell it to free on close 
		DataStreamPtr outstream(new MemoryDataStream(ourData, size, true));
		// Now free FreeImage memory buffers
		FreeImage_CloseMemory(mem);
		// Unload bitmap
		FreeImage_Unload(fiBitmap);

		return outstream;


    }
示例#5
0
static BlockReference* 
FreeImage_SavePageToBlock(MULTIBITMAPHEADER *header, FIBITMAP *data) {
	if (header->read_only || !header->locked_pages.empty())
		return NULL;

	DWORD compressed_size = 0;
	BYTE *compressed_data = NULL;

	// compress the bitmap data

	// open a memory handle
	FIMEMORY *hmem = FreeImage_OpenMemory();
	if(hmem==NULL) return NULL;
	// save the file to memory
	if(!FreeImage_SaveToMemory(header->cache_fif, data, hmem, 0)) {
		FreeImage_CloseMemory(hmem);
		return NULL;
	}
	// get the buffer from the memory stream
	if(!FreeImage_AcquireMemory(hmem, &compressed_data, &compressed_size)) {
		FreeImage_CloseMemory(hmem);
		return NULL;
	}

	// write the compressed data to the cache
	int ref = header->m_cachefile->writeFile(compressed_data, compressed_size);
	// get rid of the compressed data
	FreeImage_CloseMemory(hmem);

	return new(std::nothrow) BlockReference(ref, compressed_size);
}
示例#6
0
// 独立函数,读取gif图片,写白背景,转为PNG并写入缓存,返回缓存地址
FOX_DLL FIMEMORY * gif2png_bufopen(char *gifpath, BYTE ** buffpointeraddr, DWORD * bufflenaddr)
{
	FIBITMAP * hImage ;
	RGBQUAD * pal ;
	FIMEMORY * hMemory = NULL ;
	BYTE *mem_buffer = NULL ;
	DWORD size_in_bytes = 0 ;

	hImage = FreeImage_Load(FIF_GIF, gifpath, 0);

	pal = FreeImage_GetPalette(hImage);
	pal[70].rgbRed = 255 ;
	pal[70].rgbGreen = 255 ;
	pal[70].rgbBlue = 255 ;
	FreeImage_SetTransparent(hImage, false);

	hMemory = FreeImage_OpenMemory() ;
	FreeImage_SaveToMemory(FIF_PNG, hImage, hMemory, PNG_DEFAULT) ;
	FreeImage_Unload(hImage) ;

	FreeImage_AcquireMemory(hMemory, &mem_buffer, &size_in_bytes);
	*buffpointeraddr = mem_buffer ;
	*bufflenaddr = size_in_bytes ;
	
	return hMemory ;
//	FreeImage_CloseMemory(hMemory) ; // 使用完缓存记得要释放
}
示例#7
0
void testAcquireMemIO(const char *lpszPathName) {
    FIMEMORY *hmem = NULL;

    // load a regular file
    FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(lpszPathName);
    FIBITMAP *dib = FreeImage_Load(fif, lpszPathName, 0);

    // open and allocate a memory stream
    hmem = FreeImage_OpenMemory();

    // save the file to memory
    FreeImage_SaveToMemory(FIF_PNG, dib, hmem, PNG_DEFAULT);

    FreeImage_Unload(dib);

    // get the buffer from the memory stream
    BYTE *mem_buffer = NULL;
    DWORD size_in_bytes = 0;

    FreeImage_AcquireMemory(hmem, &mem_buffer, &size_in_bytes);

    // save the buffer in a file stream
    FILE *stream = fopen("buffer.png", "wb");
    if(stream) {
        fwrite(mem_buffer, sizeof(BYTE), size_in_bytes, stream);
        fclose(stream);
    }

    // close and free the memory stream
    FreeImage_CloseMemory(hmem);

}
示例#8
0
文件: glMain.cpp 项目: MGZero/Harvest
unsigned char * HVSTGFX::loadImageFile(CFileData *image, HVSTGFX::IMAGEFILE *imgFile, GLuint &texture)
{
	FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(image->getName().c_str());
	FIMEMORY * stream = 0;
	unsigned char tempRGB;
	GLuint tempTex = 0;
	GLenum errCode;
	bool error = false;

	stream = FreeImage_OpenMemory(image->getData());

	if(FreeImage_FIFSupportsReading(fif))
	imgFile->dib = FreeImage_LoadFromMemory(fif, stream);

	if(!imgFile->dib)
	{
		glbl->debugger->writeString("failed to open sprite " + image->getName());
		return NULL;
	}

	//pointer to image data
	unsigned char* bits;

	bits = FreeImage_GetBits(imgFile->dib);
	imgFile->width = FreeImage_GetWidth(imgFile->dib); imgFile->height = FreeImage_GetHeight(imgFile->dib);
	imgFile->size = sizeof(bits);
	int size = imgFile->width*imgFile->height;//(FreeImage_GetWidth(dib) * FreeImage_GetHeight(dib));

	for (int imageIDx = 0; imageIDx < size * 4; imageIDx += 4)
	{
		tempRGB = bits[imageIDx];
		bits[imageIDx] = bits[imageIDx + 2];
		bits[imageIDx + 2] = tempRGB;
	}
	
	glGenTextures(1, &tempTex);
	texture = tempTex;
	
	errCode = glGetError();
	if (errCode != GL_NO_ERROR)
	{
		MessageBox(NULL, _T("Unable to detect OpenGL support. Check if you have your graphics driver installed, or if your graphics card supports OpenGL."), NULL, NULL);
	}
	glBindTexture(GL_TEXTURE_2D, texture);
	
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imgFile->width, imgFile->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bits);
	
	//FreeImage_Unload(dib);
	
#ifdef _DEBUG
	if (!error)
		glbl->debugger->writeString("successfully loaded sprite " + image->getName() + "\n");
#endif
	return bits;

}
Ogre::TexturePtr textureFromBytes(const QByteArray &ba,
                                  const std::string &name) {

  static bool fi_init = false;
  if (!fi_init) {
    FreeImage_Initialise();
  }

  void *data = const_cast<char *>(ba.data());
  FIMEMORY *mem =
      FreeImage_OpenMemory(reinterpret_cast<BYTE *>(data), ba.size());
  FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(mem, 0);
  if (fif == FIF_UNKNOWN) {
    FreeImage_CloseMemory(mem);
    throw std::runtime_error("Image format is not supported for loading");
  }
  FIBITMAP *bmp = FreeImage_LoadFromMemory(fif, mem, 0);
  FreeImage_CloseMemory(mem);
  if (!bmp) {
    throw std::runtime_error("Failed to decode image");
  }
  FIBITMAP *converted = FreeImage_ConvertTo24Bits(bmp);
  FreeImage_Unload(bmp);
  if (!converted) {
    throw std::runtime_error("Failed to convert image to 24 bit");
  }

  const unsigned w = FreeImage_GetWidth(converted);
  const unsigned h = FreeImage_GetHeight(converted);
  const unsigned data_size = w * h * 3;
  BYTE *image_data = FreeImage_GetBits(converted);

  ROS_INFO("Loading a %u x %u texture", w, h);

  //  create texture
  Ogre::TexturePtr texture;
  try {
    Ogre::DataStreamPtr data_stream;
    data_stream.bind(new Ogre::MemoryDataStream(image_data, data_size));

    const Ogre::String res_group =
        Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME;
    Ogre::TextureManager &texture_manager =
        Ogre::TextureManager::getSingleton();
    texture =
        texture_manager.loadRawData(name, res_group, data_stream, w, h,
                                    Ogre::PF_R8G8B8, Ogre::TEX_TYPE_2D, 0);
  }
  catch (...) {
    //  clean up FreeImage before re-throwing
    FreeImage_Unload(converted);
    throw;
  }

  return texture;
}
示例#10
0
	bool isThisFormat(MemChunk& mc)
	{
		FIMEMORY*         mem = FreeImage_OpenMemory((BYTE*)mc.getData(), mc.getSize());
		FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(mem, 0);
		FreeImage_CloseMemory(mem);
		if (fif == FIF_UNKNOWN)
			return false;
		else
			return true;
	}
示例#11
0
FIBITMAP* FreeImage_LoadFromMem(FREE_IMAGE_FORMAT fif, fiio_mem_handle *handle, int flags)
{
	if (handle && handle->data) {
		FIMEMORY *hmem = FreeImage_OpenMemory((BYTE *)handle->data, handle->datalen);
		FREE_IMAGE_FORMAT _fif = (fif != FIF_UNKNOWN) ? fif : FreeImage_GetFileTypeFromMemory(hmem, 0);
		FIBITMAP *dib = FreeImage_LoadFromMemory(_fif, hmem, flags);
		FreeImage_CloseMemory(hmem);
	}

	return NULL;
}
示例#12
0
void FI(loadImageFile)(Bitmap& bitmap, Gosu::Reader input)
{
    // Read all available input
    std::vector<BYTE> data(input.resource().size() - input.position());
    input.read(&data[0], data.size());
    FIMEMORY* fim = FreeImage_OpenMemory(&data[0], data.size());
    FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(fim);
    FIBITMAP* fib = FreeImage_LoadFromMemory(fif, fim, GOSU_FIFLAGS);
    checkForFreeImageErrors(fib != 0);
    fibToBitmap(bitmap, fib, fif);
}
示例#13
0
/** 
Get the embedded JPEG preview image from RAW picture with included Exif Data. 
@param RawProcessor Libraw handle
@param flags JPEG load flags
@return Returns the loaded dib if successfull, returns NULL otherwise
*/
static FIBITMAP * 
libraw_LoadEmbeddedPreview(LibRaw *RawProcessor, int flags) {
	FIBITMAP *dib = NULL;
	libraw_processed_image_t *thumb_image = NULL;
	
	try {
		// unpack data
		if(RawProcessor->unpack_thumb() != LIBRAW_SUCCESS) {
			// run silently "LibRaw : failed to run unpack_thumb"
			return NULL;
		}

		// retrieve thumb image
		int error_code = 0;
		thumb_image = RawProcessor->dcraw_make_mem_thumb(&error_code);
		if(thumb_image) {
			if(thumb_image->type != LIBRAW_IMAGE_BITMAP) {
				// attach the binary data to a memory stream
				FIMEMORY *hmem = FreeImage_OpenMemory((BYTE*)thumb_image->data, (DWORD)thumb_image->data_size);
				// get the file type
				FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem, 0);
				if(fif == FIF_JPEG) {
					// rotate according to Exif orientation
					flags |= JPEG_EXIFROTATE;
				}
				// load an image from the memory stream
				dib = FreeImage_LoadFromMemory(fif, hmem, flags);
				// close the stream
				FreeImage_CloseMemory(hmem);
			} else if((flags & FIF_LOAD_NOPIXELS) != FIF_LOAD_NOPIXELS) {
				// convert processed data to output dib
				dib = libraw_ConvertProcessedImageToDib(thumb_image);
			}
		} else {
			throw "LibRaw : failed to run dcraw_make_mem_thumb";
		}

		// clean-up and return
		RawProcessor->dcraw_clear_mem(thumb_image);

		return dib;

	} catch(const char *text) {
		// clean-up and return
		if(thumb_image) {
			RawProcessor->dcraw_clear_mem(thumb_image);
		}
		if(text != NULL) {
			FreeImage_OutputMessageProc(s_format_id, text);
		}
	}

	return NULL;
}
示例#14
0
int CPropertiesFiles::previewWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	static bool fDowned=false;

	switch (uMsg) {
		case WM_INITDIALOG:
			if (!fiBitmap) {
				FIMEMORY* fiMemory=FreeImage_OpenMemory((BYTE*)lParam,bitmapInfo->unc_size);
				RECT rect;
				POINT pt={0,0};

				if (fiBitmap=FreeImage_LoadFromMemory(_tcsicmp(bitmapInfo->name+_tcslen(bitmapInfo->name)-4,".tga")?_tcsicmp(bitmapInfo->name+_tcslen(bitmapInfo->name)-4,".bmp")?FIF_JPEG:FIF_BMP:FIF_TARGA,fiMemory)) {
					::GetWindowRect(hwndDlg,&rect);
					::ClientToScreen(hwndDlg,&pt);
					rect.right=FreeImage_GetWidth(fiBitmap)+(pt.x-rect.left);
					rect.bottom=FreeImage_GetHeight(fiBitmap)+(pt
						.y-rect.top);;
					::MoveWindow(hwndDlg,rect.left,rect.top,rect.right,rect.bottom,FALSE);
					FreeImage_CloseMemory(fiMemory);

					::SetWindowText(hwndDlg,_tcsrchr(bitmapInfo->name,'/')+1);
					fDowned=false;
				} else {
					fDowned=true;
					::EndDialog(hwndDlg,0);
				}
			}
			break;
		case WM_LBUTTONDOWN:
		case WM_RBUTTONDOWN:
			fDowned=true;
			break;
		case WM_CLOSE:
			fDowned=true;
		case WM_LBUTTONUP:
		case WM_RBUTTONUP:
			if (fDowned) {
				FreeImage_Unload(fiBitmap);
				fiBitmap=NULL;
				::EndDialog(hwndDlg,0);
			}
			break;
		case WM_PAINT:
			{
				HDC hDC=::GetDC(hwndDlg);
				::SetStretchBltMode(hDC, COLORONCOLOR);
				::StretchDIBits(hDC, 0,0,FreeImage_GetWidth(fiBitmap), FreeImage_GetHeight(fiBitmap), 
					0, 0, FreeImage_GetWidth(fiBitmap), FreeImage_GetHeight(fiBitmap),
					FreeImage_GetBits(fiBitmap), FreeImage_GetInfo(fiBitmap), DIB_RGB_COLORS, SRCCOPY);
				::ReleaseDC(hwndDlg,hDC);
			}
	}
	return FALSE;
}
示例#15
0
void TextureManager::loadTexture2DMem(unsigned char* imageData,long length) {
    FIMEMORY* hmem = FreeImage_OpenMemory(imageData,length);
    FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem);
    FIBITMAP* image = FreeImage_LoadFromMemory(fif, hmem, 0);

    FIBITMAP* temp = image;
    image = FreeImage_ConvertTo32Bits(image);
    FreeImage_Unload(temp);
    FreeImage_CloseMemory(hmem);

    int w = FreeImage_GetWidth(image);
    int h = FreeImage_GetHeight(image);


    printf("[Texture Manager] Image loaded from memory, is %d x %d\n",w,h);

    GLubyte* textureBytes = new GLubyte[4*w*h];
    char* pixels = (char*)FreeImage_GetBits(image);

    //FreeImage loads in BGR format, so we need to swap some bytes(Or use GL_BGR)

    for(int j= 0; j<w*h; j++) {
        textureBytes[j*4+0]= pixels[j*4+2];
        textureBytes[j*4+1]= pixels[j*4+1];
        textureBytes[j*4+2]= pixels[j*4+0];
        textureBytes[j*4+3]= pixels[j*4+3];
    }


    glGenTextures(1, &(textInt));

    glBindTexture(GL_TEXTURE_2D, textInt);
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, w, h, 0, GL_RGBA,GL_UNSIGNED_BYTE,(GLvoid*)textureBytes );



    Textures.push_back(textInt);


    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);


    GLenum errorLoading = glGetError();
    if(errorLoading) {
        cout<<"There was an error loading the texture: "<<errorLoading<<endl;
    }


}
示例#16
0
void DLL_CALLCONV
FreeImage_UnlockPage(FIMULTIBITMAP *bitmap, FIBITMAP *page, BOOL changed) {
	if ((bitmap) && (page)) {
		MULTIBITMAPHEADER *header = FreeImage_GetMultiBitmapHeader(bitmap);

		// find out if the page we try to unlock is actually locked...

		if (header->locked_pages.find(page) != header->locked_pages.end()) {
			// store the bitmap compressed in the cache for later writing
			
			if (changed && !header->read_only) {
				header->changed = TRUE;

				// cut loose the block from the rest

				BlockListIterator i = FreeImage_FindBlock(bitmap, header->locked_pages[page]);

				// compress the data

				DWORD compressed_size = 0;
				BYTE *compressed_data = NULL;

				// open a memory handle
				FIMEMORY *hmem = FreeImage_OpenMemory();
				// save the page to memory
				FreeImage_SaveToMemory(header->cache_fif, page, hmem, 0);
				// get the buffer from the memory stream
				FreeImage_AcquireMemory(hmem, &compressed_data, &compressed_size);

				// write the data to the cache
				
				if (i->m_type == BLOCK_REFERENCE) {
					header->m_cachefile.deleteFile(i->getReference());
				}
				
				int iPage = header->m_cachefile.writeFile(compressed_data, compressed_size);
				
				*i = PageBlock(BLOCK_REFERENCE, iPage, compressed_size);
				
				// get rid of the compressed data

				FreeImage_CloseMemory(hmem);
			}

			// reset the locked page so that another page can be locked

			FreeImage_Unload(page);

			header->locked_pages.erase(page);
		}
	}
}
示例#17
0
FIMEMORY* FreeImage_SaveToMem(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, fiio_mem_handle *handle, int flags)
{
	if (handle) {
		FIMEMORY *hmem = FreeImage_OpenMemory(NULL, 0);
		if(fif == FIF_UNKNOWN)
			fif = FIF_BMP;
		handle->curpos = 0;
		FreeImage_SaveToMemory(fif, dib, hmem, flags);
		FreeImage_AcquireMemory(hmem, (BYTE **)&handle->data, (DWORD *)&handle->datalen);
		return hmem;
	}
	return NULL;
}
示例#18
0
bool FreeImageData::loadMem(unsigned char*data, unsigned long size, std::string filename)
{
    FIMEMORY * mem = FreeImage_OpenMemory(data, size);
    FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(mem, size);
    if (fif == FIF_UNKNOWN){
        fif = FreeImage_GetFIFFromFilename(filename.c_str());
    }
    if(fif == FIF_UNKNOWN)
        return false;
    m_bitmap = FreeImage_LoadFromMemory(fif, mem);
    FreeImage_CloseMemory(mem);
    return m_bitmap != NULL;
}
示例#19
0
//----------------------------------------------------
bool ofImage::loadImageFromMemory(const ofBuffer & buffer, ofPixels &pix){

	int					width, height, bpp;
	bool bLoaded		= false;
	FIBITMAP * bmp		= NULL;
	FIMEMORY *hmem		= NULL;
	
	printf("loadImageFromMemory\n");

	hmem = FreeImage_OpenMemory((unsigned char*)buffer.getBuffer(), buffer.size());
	if (hmem == NULL){
		ofLog(OF_LOG_ERROR,"couldn't create memory handle! \n");
		return false;
	}

	//get the file type!
	FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem);
	if( fif == -1 ){
		ofLog(OF_LOG_ERROR,"unable to guess format", fif);
		return false;
		FreeImage_CloseMemory(hmem);
	}


	//make the image!!
	bmp = FreeImage_LoadFromMemory(fif, hmem, 0);
	
	if( bmp != NULL ){
		bLoaded = true;
		ofLog(OF_LOG_VERBOSE,"FreeImage_LoadFromMemory worked!\n");
	}
	
	//-----------------------------

	if (bLoaded){
		putBmpIntoPixels(bmp,pix);
	} else {
		width = height = bpp = 0;
	}

	if (bmp != NULL){
		FreeImage_Unload(bmp);
	}
	
	if( hmem != NULL ){
		FreeImage_CloseMemory(hmem);
	}

	return bLoaded;
}
示例#20
0
static bool loadImage(ofPixels_<PixelType> & pix, const ofBuffer & buffer, const ofImageLoadSettings &settings){
	ofInitFreeImage();
	bool bLoaded = false;
	FIBITMAP* bmp = nullptr;
	FIMEMORY* hmem = nullptr;
	
	hmem = FreeImage_OpenMemory((unsigned char*) buffer.getData(), buffer.size());
	if (hmem == nullptr){
		ofLogError("ofImage") << "loadImage(): couldn't load image from ofBuffer, opening FreeImage memory failed";
		return false;
	}

	//get the file type!
	FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem);
	if( fif == -1 ){
		ofLogError("ofImage") << "loadImage(): couldn't load image from ofBuffer, unable to guess image format from memory";
		FreeImage_CloseMemory(hmem);
		return false;
	}


	//make the image!!
	if(fif == FIF_JPEG) {
		int option = getJpegOptionFromImageLoadSetting(settings);
		bmp = FreeImage_LoadFromMemory(fif, hmem, option);
	} else {
		bmp = FreeImage_LoadFromMemory(fif, hmem, 0);
	}
	
	if( bmp != nullptr ){
		bLoaded = true;
	}
	
	//-----------------------------
	
	if (bLoaded){
		putBmpIntoPixels(bmp,pix);
	}

	if (bmp != nullptr){
		FreeImage_Unload(bmp);
	}
	
	if( hmem != nullptr ){
		FreeImage_CloseMemory(hmem);
	}

	return bLoaded;
}
示例#21
0
void DLL_CALLCONV
FreeImage_InsertPage(FIMULTIBITMAP *bitmap, int page, FIBITMAP *data) {
	if ((bitmap) && (data)) {
		if (page < FreeImage_GetPageCount(bitmap)) {
			MULTIBITMAPHEADER *header = FreeImage_GetMultiBitmapHeader(bitmap);

			if ((!header->read_only) && (header->locked_pages.empty())) {
				DWORD compressed_size = 0;
				BYTE *compressed_data = NULL;

				// compress the bitmap data

				// open a memory handle
				FIMEMORY *hmem = FreeImage_OpenMemory();
				// save the file to memory
				FreeImage_SaveToMemory(header->cache_fif, data, hmem, 0);
				// get the buffer from the memory stream
				FreeImage_AcquireMemory(hmem, &compressed_data, &compressed_size);

				// write the compressed data to the cache

				int ref = header->m_cachefile->writeFile(compressed_data, compressed_size);

				// add a block

				if (page > 0) {
					BlockListIterator block_source = FreeImage_FindBlock(bitmap, page);

					BlockReference *block = new BlockReference(ref, compressed_size);

					header->m_blocks.insert(block_source, (BlockTypeS *)block);
				} else {
					BlockReference *block = new BlockReference(ref, compressed_size);

					header->m_blocks.push_front((BlockTypeS *)block);
				}

				// get rid of the compressed buffer

				FreeImage_CloseMemory(hmem);

				header->changed = TRUE;
				header->page_count = -1;
			}
		}
	}
}
示例#22
0
static bool loadImage(ofPixels_<PixelType> & pix, const ofBuffer & buffer){
	ofInitFreeImage();
	bool bLoaded = false;
	FIBITMAP* bmp = NULL;
	FIMEMORY* hmem = NULL;
	
	hmem = FreeImage_OpenMemory((unsigned char*) buffer.getBinaryBuffer(), buffer.size());
	if (hmem == NULL){
		ofLogError("ofImage") << "loadImage(): couldn't load image from ofBuffer, opening FreeImage memory failed";
		return false;
	}

	//get the file type!
	FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem);
	if( fif == -1 ){
		ofLogError("ofImage") << "loadImage(): couldn't load image from ofBuffer, unable to guess image format from memory";
		return false;
		FreeImage_CloseMemory(hmem);
	}


	//make the image!!
	bmp = FreeImage_LoadFromMemory(fif, hmem, 0);
	
	if( bmp != NULL ){
		bLoaded = true;
	}
	
	//-----------------------------
	
	if (bLoaded){
		putBmpIntoPixels(bmp,pix);
	}

	if (bmp != NULL){
		FreeImage_Unload(bmp);
	}
	
	if( hmem != NULL ){
		FreeImage_CloseMemory(hmem);
	}

	return bLoaded;
}
示例#23
0
/**
Write a metadata model as a TIF IFD, returns the IFD as a buffer.
The buffer is allocated by the function and must be freed by the caller, using 'free'.
@param dib Input FIBITMAP
@param md_model Metadata model to write
@param ppbProfile Returned buffer
@param uProfileLength Returned buffer size
@return Returns TRUE if successful, FALSE otherwise
@see tiff_write_ifd
*/
BOOL
tiff_get_ifd_profile(FIBITMAP *dib, FREE_IMAGE_MDMODEL md_model, BYTE **ppbProfile, unsigned *uProfileLength) {
	FIMEMORY *hmem = NULL;

	try {
		// open a memory stream
		hmem = FreeImage_OpenMemory(NULL, 0);
		if(!hmem) {
			throw(1);
		}

		// write the metadata model as a TIF IFD
		BOOL bResult = tiff_write_ifd(dib, md_model, hmem);

		if(bResult) {
			BYTE *data = NULL;
			DWORD size_in_bytes = 0;

			// get a pointer to the stream buffer
			FreeImage_AcquireMemory(hmem, &data, &size_in_bytes);
			
			// (re-)allocate output buffer
			BYTE *pbProfile = *ppbProfile;
			pbProfile = (BYTE*)realloc(pbProfile, size_in_bytes);
			if(!pbProfile) {
				throw(1);
			} else {
				// copy IFD
				memcpy(pbProfile, data, size_in_bytes);
				*ppbProfile = pbProfile;
				*uProfileLength = size_in_bytes;
			}
		}

		// free the memory stream
		FreeImage_CloseMemory(hmem);

		return bResult;

	} catch(int) {
		FreeImage_CloseMemory(hmem);
		return FALSE;
	}
}
示例#24
0
static bool loadImage(ofPixels_<PixelType> & pix, const ofBuffer & buffer){
	ofInitFreeImage();
	bool bLoaded = false;
	FIBITMAP* bmp = NULL;
	FIMEMORY* hmem = NULL;
	
	hmem = FreeImage_OpenMemory((unsigned char*) buffer.getBinaryBuffer(), buffer.size());
	if (hmem == NULL){
		ofLog(OF_LOG_ERROR, "couldn't create memory handle!");
		return false;
	}

	//get the file type!
	FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem);
	if( fif == -1 ){
		ofLog(OF_LOG_ERROR, "unable to guess format", fif);
		return false;
		FreeImage_CloseMemory(hmem);
	}


	//make the image!!
	bmp = FreeImage_LoadFromMemory(fif, hmem, 0);
	
	if( bmp != NULL ){
		bLoaded = true;
	}
	
	//-----------------------------
	
	if (bLoaded){
		putBmpIntoPixels(bmp,pix);
	}

	if (bmp != NULL){
		FreeImage_Unload(bmp);
	}
	
	if( hmem != NULL ){
		FreeImage_CloseMemory(hmem);
	}

	return bLoaded;
}
示例#25
0
static INT_PTR serviceLoadFromMem(WPARAM wParam, LPARAM lParam)
{
	IMGSRVC_MEMIO *mio = (IMGSRVC_MEMIO *)wParam;
	if(mio->iLen == 0 || mio->pBuf == NULL)
		return 0;

	FIMEMORY *hmem = FreeImage_OpenMemory((BYTE *)mio->pBuf, mio->iLen);
	FREE_IMAGE_FORMAT fif = (mio->fif != FIF_UNKNOWN) ? mio->fif : mio->fif = FreeImage_GetFileTypeFromMemory(hmem, 0);
	FIBITMAP *dib = FreeImage_LoadFromMemory(fif, hmem, mio->flags);
	FreeImage_CloseMemory(hmem);

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

	HBITMAP hbm = FreeImage_CreateHBITMAPFromDIB(dib);

	FreeImage_Unload(dib);
	return (INT_PTR)hbm;
}
示例#26
0
std::shared_ptr<FIBITMAP> load_png_from_memory(const void* memory_location, size_t size)
{
	FREE_IMAGE_FORMAT fif = FIF_PNG;

	auto memory = std::unique_ptr<FIMEMORY, decltype(&FreeImage_CloseMemory)>(
			FreeImage_OpenMemory(static_cast<BYTE*>(const_cast<void*>(memory_location)), size),
			FreeImage_CloseMemory);
	auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_LoadFromMemory(fif, memory.get(), 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."));
	}

	return bitmap;
}
示例#27
0
    //---------------------------------------------------------------------
    String FreeImageCodec::magicNumberToFileExt(const char *magicNumberPtr, size_t maxbytes) const
    {
        FIMEMORY* fiMem =
            FreeImage_OpenMemory((BYTE*)magicNumberPtr, static_cast<DWORD>(maxbytes));

        FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(fiMem, (int)maxbytes);
        FreeImage_CloseMemory(fiMem);

        if (fif != FIF_UNKNOWN)
        {
            String ext(FreeImage_GetFormatFromFIF(fif));
            StringUtil::toLowerCase(ext);
            return ext;
        }
        else
        {
            return StringUtil::BLANK;
        }
    }
bool FreeImageLoader::canLoad(OS::IStream* file){
	if(m_isDummy)
		return false;
	if(file->isStream())
		return false;
	byte* data = new byte[file->length()];
	FIMEMORY* fiMem = FreeImage_OpenMemory((BYTE*)data, static_cast<DWORD>(file->length()));
	file->read(data, file->length());

	FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(fiMem, (int)file->length());
	FreeImage_CloseMemory(fiMem);
	delete[] data;
	//image format
//	FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
	//check the file signature and deduce its format
//	fif = FreeImage_GetFileTypeFromMemory((FIMEMORY*)data,file->length());
	if(fif == FIF_UNKNOWN)
		return false;
	return fif==m_FTType;
}
示例#29
0
void testSaveMemIO(const char *lpszPathName) {
    FIMEMORY *hmem = NULL;

    // load a regular file
    FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(lpszPathName);
    FIBITMAP *dib = FreeImage_Load(fif, lpszPathName, 0);

    // open a memory handle
    hmem = FreeImage_OpenMemory();

    // save the file to memory
    FreeImage_SaveToMemory(fif, dib, hmem, 0);

    // at this point, hmem contains the entire PNG data in memory.
    // the amount of space used by the memory is equal to file_size
    long file_size = FreeImage_TellMemory(hmem);
    printf("File size : %ld\n", file_size);


    // its easy load an image from memory as well

    // seek to the start of the memory stream
    FreeImage_SeekMemory(hmem, 0L, SEEK_SET);

    // get the file type
    FREE_IMAGE_FORMAT mem_fif = FreeImage_GetFileTypeFromMemory(hmem, 0);

    // load an image from the memory handle
    FIBITMAP *check = FreeImage_LoadFromMemory(mem_fif, hmem, 0);

    // save as a regular file
    FreeImage_Save(FIF_PNG, check, "dump.png", PNG_DEFAULT);

    // make sure to free the data since FreeImage_SaveToMemory
    // will cause it to be malloc'd
    FreeImage_CloseMemory(hmem);

    FreeImage_Unload(check);
    FreeImage_Unload(dib);
}
示例#30
0
VALUE rb_graffik_from_memory(VALUE self, VALUE rb_data) {
  FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
  Check_Type(rb_data, T_STRING);
  
  // Get info about our in-memory image
  BYTE *data_ptr = (BYTE *) RSTRING_PTR(rb_data);
  DWORD data_length = RSTRING_LEN(rb_data);
  
  // Open up the memory stream
  FIMEMORY *stream = FreeImage_OpenMemory(data_ptr, data_length);
  
  // Make sure the stream was open
  if (stream == NULL) {
    rb_raise(rb_eTypeError, "Unable to read image from memory");
  }
  
  fif = FreeImage_GetFileTypeFromMemory(stream, 0);
  if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
    int flags = ((fif == FIF_JPEG) ? JPEG_ACCURATE : 0);
    // Load the image from disk
    FIBITMAP *image = FreeImage_LoadFromMemory(fif, stream, flags);
    // Release memory
    FreeImage_CloseMemory(stream);
    // Develop an instance for Ruby
    VALUE instance = Data_Wrap_Struct(self, NULL, NULL, image);
    // Store the image type as a FixNum
    rb_iv_set(instance, "@file_type", INT2FIX(fif));
    
    // If a block is given, yield to it, if not, return the instance
    if (rb_block_given_p()) {
      return rb_ensure(rb_yield, instance, rb_graffik_close, instance);
    } else {
      return instance;
    }
  }
  
  // If we couldn't load it, throw and error
  rb_raise(rb_eTypeError, "Unknown file format");
}