static bool mergeBitBltToRGBA(FIBITMAP *image, const std::string &pathToMask)
{
    if(!image)
        return false;

    if(!Files::fileExists(pathToMask))
        return false; //Nothing to do

    FIBITMAP *mask = loadImage(pathToMask);

    if(!mask)
        return false;//Nothing to do

    unsigned int img_w  = FreeImage_GetWidth(image);
    unsigned int img_h  = FreeImage_GetHeight(image);
    unsigned int mask_w = FreeImage_GetWidth(mask);
    unsigned int mask_h = FreeImage_GetHeight(mask);

    RGBQUAD Fpix;
    RGBQUAD Bpix;
    RGBQUAD Npix = {0x0, 0x0, 0x0, 0xFF};

    for(unsigned int y = 0; (y < img_h) && (y < mask_h); y++)
    {
        for(unsigned int x = 0; (x < img_w) && (x < mask_w); x++)
        {

            FreeImage_GetPixelColor(image, x, y, &Fpix);
            FreeImage_GetPixelColor(mask, x, y, &Bpix);

            Npix.rgbRed     = ((0x7F & Bpix.rgbRed) | Fpix.rgbRed);
            Npix.rgbGreen   = ((0x7F & Bpix.rgbGreen) | Fpix.rgbGreen);
            Npix.rgbBlue    = ((0x7F & Bpix.rgbBlue) | Fpix.rgbBlue);
            int newAlpha = 255 -
                           ((int(Bpix.rgbRed) +
                             int(Bpix.rgbGreen) +
                             int(Bpix.rgbBlue)) / 3);
            if((Bpix.rgbRed > 240u) //is almost White
               && (Bpix.rgbGreen > 240u)
               && (Bpix.rgbBlue > 240u))
                newAlpha = 0;

            newAlpha = newAlpha + ((int(Fpix.rgbRed) +
                                    int(Fpix.rgbGreen) +
                                    int(Fpix.rgbBlue)) / 3);
            if(newAlpha > 255)
                newAlpha = 255;
            Npix.rgbReserved = newAlpha;

            FreeImage_SetPixelColor(image, x, y, &Npix);
        }
    }
    FreeImage_Unload(mask);
    return true;
}
Пример #2
0
vec3 Texture::getPixel(vec2 texturePoint)
{
    RGBQUAD color;
    FreeImage_GetPixelColor(this->data, this->width * texturePoint.x, this->height * texturePoint.y, &color);

    return vec3(color.rgbRed, color.rgbGreen, color.rgbBlue) / 255.0f;
}
Пример #3
0
/** deliver B-value of bitmap at given position */
unsigned int Vt2IsoImageFreeImage_c::getB( unsigned int aui_x, unsigned int aui_y )
{
 if ( ( aui_x >= ui_width ) || ( aui_y >= ui_height ) ) return 0;

 if (mb_palettized)
 { // we can't raw-access the bitmap buffer with RGB, we need to get the RGB via the palette-index's colour
   fiuint8_t idx;
   FreeImage_GetPixelIndex (bitmap, aui_x, (ui_height - 1) - aui_y, &idx);
   return vtColourTable[idx].bgrBlue;
 }
 else
 {
  #if defined( WIN32 )
  RGBQUAD temp_pixel;
  // ( FreeImage library documentation states, that first scanline in memory is
  //   bottommost -> i.e. upsidedown in relation to other modellings
  //    -> change direction back to usual with ( ui_height - aui_y ) )
  FreeImage_GetPixelColor(bitmap, aui_x, ( (ui_height - 1) - aui_y ), &temp_pixel);
  return temp_pixel.rgbBlue;
  #else
  checkUpdateScanline( aui_y );
  return scanline[FI_RGBA_BLUE + ( bytespp * aui_x )];
  #endif
 }
}
Пример #4
0
void CFaceBitmap::ToFreeImage(FIBITMAP* image,int x,int y,RGBQUAD* fg)
{
	bool newfg=false;
	static int l = 2;
	RGBQUAD bg ;
	if(!fg)
	{
		fg = new RGBQUAD();
		memset(fg,0,sizeof(RGBQUAD));
		newfg = true;
	}
	int w = GetWidth();
	int h = GetHeight();
	for ( int i=0;i<h;i+=l )  
	{  
		l+=2;l%=3;
		for ( int j=0;j<w;j+=2 )  
		{  
			if((pBmp->buffer[i*w+j]!=0||pBmp->buffer[i*w+j]!=0|| pBmp->buffer[i*w+j]!=0))
			{
				FreeImage_SetPixelColor(image,j+x,h-i+y,fg);
				
			}
			else
			{
				
				FreeImage_GetPixelColor(image,j+x,h-i+y,&bg);
				FreeImage_SetPixelColor(image,j+x,h-i+y,&bg);
			}
		}  
		
	}
	if(newfg)
		delete fg;
}
pixel *read_img(char *name, int *width, int *height) {
	FIBITMAP *image;
	int i, j, pnum;
	RGBQUAD aPixel;
	pixel *data;

	if ((image = FreeImage_Load(FIF_TIFF, name, 0)) == NULL) {
		return NULL;
	}
	*width = FreeImage_GetWidth(image);
	*height = FreeImage_GetHeight(image);

	data = (pixel *)malloc((*height)*(*width)*sizeof(pixel *));
	pnum = 0;
	for (i = 0; i < (*height); i++) {
		for (j = 0; j < (*width); j++) {
			FreeImage_GetPixelColor(image, j, i, &aPixel);
			data[pnum].r = (aPixel.rgbRed);
			data[pnum].g = (aPixel.rgbGreen);
			data[pnum++].b = (aPixel.rgbBlue);
		}
	}
	FreeImage_Unload(image);
	return data;
}//read_img
unsigned char* loadImage(const char* _fileName, int& _width, int& _height)
{
	FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(_fileName);

	FIBITMAP *dib = FreeImage_Load(fif, _fileName, 0);
	
	int dimX = FreeImage_GetWidth(dib);
	int dimY = FreeImage_GetHeight(dib);

	unsigned char* result = (unsigned char*)malloc(sizeof(unsigned char) * dimX * dimY);

	int index = 0;
	for(unsigned y = 1; y <= dimY; ++y)
	{
		for(unsigned x = 0; x < dimX; ++x)
		{
			RGBQUAD pixel;
			bool ok = FreeImage_GetPixelColor(dib, x, dimY - y, &pixel);
			result[index] = method( &pixel );
			++index;
		}
	}
	_width = dimX;
	_height = dimY;
	return result;
}
Пример #7
0
bool process_image(std::string infile, std::string outfile, double brightness, double gamma)
{
	DIR *editdir = opendir("img/edit");
	struct dirent *file;
	while ((file = readdir(editdir)) != NULL)
	{
		if (strcmp(file -> d_name, outfile.c_str()) == 0)
		{
			return false;
		}
	}
	closedir(editdir);
	infile = "img/" + infile;
	outfile = "img/edit/" + outfile;
	FIBITMAP *input = FreeImage_Load(FIF_PNG, infile.c_str());
	if (input == NULL)
	{
		std::cerr << "Failed to load " << infile << std::endl;
		exit(1);
	}
	if (!FreeImage_Save(FIF_PNG, input, outfile.c_str()))
	{
		FreeImage_Unload(input);
		std::cerr << "Failed to save " << outfile << std::endl;
		exit(1);
	}
	FreeImage_Unload(input);
	FIBITMAP *output = FreeImage_Load(FIF_PNG, outfile.c_str());
	if (output == NULL)
	{
		std::cerr << "Failed to load " << outfile << std::endl;
		exit(1);
	}
	unsigned int img_w = FreeImage_GetWidth(output);
	unsigned int img_h = FreeImage_GetHeight(output);
	RGBQUAD pixel;
	for (unsigned int x = 0; x < img_w; x++)
	{
		for (unsigned int y = 0; y < img_h; y++)
		{
			FreeImage_GetPixelColor(output, x, y, &pixel);
			pixel.rgbRed = int(clamp(0, pixel.rgbRed * brightness, 255));
			pixel.rgbBlue = int(clamp(0, pixel.rgbBlue * brightness, 255));
			pixel.rgbGreen = int(clamp(0, pixel.rgbGreen * brightness, 255));
			pixel.rgbRed = int(clamp(0, pow(((double) pixel.rgbRed) / 255.0f, gamma) * 255.0, 255));
			pixel.rgbBlue = int(clamp(0, pow(((double) pixel.rgbBlue) / 255.0f, gamma) * 255.0, 255));
			pixel.rgbGreen = int(clamp(0, pow(((double) pixel.rgbGreen) / 255.0f, gamma) * 255.0, 255));
			FreeImage_SetPixelColor(output, x, y, &pixel);
		}
	}
	if (!FreeImage_Save(FIF_PNG, output, outfile.c_str()))
	{
		FreeImage_Unload(input);
		std::cerr << "Failed to save " << outfile << std::endl;
		exit(1);
	}
	FreeImage_Unload(output);
	return true;
}
Пример #8
0
/**
 * This method returns the color on the given position.
 *
 * @param ulX the horizontal (x) position, from which to get the color
 * @param ulY the vertical (y) position, from which to get the color
 * @param colorValue a pointer to the color value field wher to store
 * 	the color value
 * @return true if pixel color was evalued, else false
 */
bool fipImage::getPixelColor( const unsigned long ulX, const unsigned long ulY, RGBQUAD * colorValue ) const{

/*	if ( colorValue == NULL ){
		return false;
	}
*/
	return FreeImage_GetPixelColor( pImageData, ulX, ulY, colorValue );
}
Пример #9
0
RebColor RebGLSkinManager::GetPixelColor(void * data, unsigned int x, unsigned int y)
{
	FIBITMAP * img = (FIBITMAP*)data;
	RGBQUAD col;
	FreeImage_GetPixelColor(img, x, y, &col);
	RebColor ret;
	ret.Set(col.rgbRed / 256, col.rgbGreen / 256, col.rgbBlue / 256, col.rgbReserved / 256);
	return ret;
}
Пример #10
0
GLuint Texture::loadTexture()
{
	FIBITMAP *dib1 = NULL;

	FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(_filename);

	dib1 = FreeImage_Load(fif, _filename, JPEG_DEFAULT);
	if (!dib1)
	{
		std::cerr << "Erreur ouverture d\'image" << std::endl;
		exit(0);

	}

	GLuint tex_id = 0;
	int x, y;
	int height, width;

	RGBQUAD rgbquad;


	FREE_IMAGE_TYPE type;
	BITMAPINFOHEADER *header;

	type = FreeImage_GetImageType(dib1);


	height = FreeImage_GetHeight(dib1);
	width = FreeImage_GetWidth(dib1);

	header = FreeImage_GetInfoHeader(dib1);
	int scanLineWidh = ((3 * width) % 4 == 0) ? 3 * width : ((3 * width) / 4) * 4 + 4;
	unsigned char * texels = (GLubyte*)calloc(height*scanLineWidh, sizeof(GLubyte));
	for (x = 0; x<width; x++)
		for (y = 0; y<height; y++)
		{
			FreeImage_GetPixelColor(dib1, x, y, &rgbquad);

			texels[(y*scanLineWidh + 3 * x)] = ((GLubyte*)&rgbquad)[2];
			texels[(y*scanLineWidh + 3 * x) + 1] = ((GLubyte*)&rgbquad)[1];
			texels[(y*scanLineWidh + 3 * x) + 2] = ((GLubyte*)&rgbquad)[0];
		}

	glGenTextures(1, &tex_id);
	glBindTexture(GL_TEXTURE_2D, tex_id);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
		width, height, 0, GL_RGB,
		GL_UNSIGNED_BYTE, texels);
	free(texels);

	return tex_id;
}
Пример #11
0
static rgb_color sample_rgb(uint16_t x, uint16_t y) {
  RGBQUAD quad;
  rgb_color ret;
  FreeImage_GetPixelColor(dib,x,y,&quad);
  ret.r = RED(&quad);
  ret.g = GREEN(&quad);
  ret.b = BLUE(&quad);
  return ret;
}
GLfloat Heightmap::getY(FIBITMAP * sprite, int _x, int _y, float scale_y)
{
    RGBQUAD colour;
    FreeImage_GetPixelColor(sprite, _x, _y, &colour);

    auto y = 0.0f;
    y += float(colour.rgbGreen) / 255.0f;
    y += float(colour.rgbBlue) / 255.0f;
    y += float(colour.rgbRed) / 255.0f;
    return (-2 + y*scale_y);
}
static void splitRGBAtoBitBlt(FIBITMAP * &image, FIBITMAP *&mask)
{
    unsigned int img_w   = FreeImage_GetWidth(image);
    unsigned int img_h   = FreeImage_GetHeight(image);

    mask = FreeImage_AllocateT(FIT_BITMAP,
                               img_w, img_h,
                               FreeImage_GetBPP(image),
                               FreeImage_GetRedMask(image),
                               FreeImage_GetGreenMask(image),
                               FreeImage_GetBlueMask(image));

    RGBQUAD Fpix;
    RGBQUAD Npix = {0x0, 0x0, 0x0, 0xFF};

    for(unsigned int y = 0; (y < img_h); y++)
    {
        for(unsigned int x = 0; (x < img_w); x++)
        {
            FreeImage_GetPixelColor(image, x, y, &Fpix);

            uint8_t grey = (255 - Fpix.rgbReserved);
            Npix.rgbRed  = grey;
            Npix.rgbGreen= grey;
            Npix.rgbBlue = grey;
            Npix.rgbReserved = 255;

            Fpix.rgbRed  = subtractAlpha(Fpix.rgbRed, grey);
            Fpix.rgbGreen= subtractAlpha(Fpix.rgbGreen, grey);
            Fpix.rgbBlue = subtractAlpha(Fpix.rgbBlue, grey);
            Fpix.rgbReserved = 255;

            FreeImage_SetPixelColor(image, x, y, &Fpix);
            FreeImage_SetPixelColor(mask,  x, y, &Npix);
        }
    }
}
Пример #14
0
CImageDataLoader::CImageData::CImageData(IDataStream* pData)
{
	if(pData->IsValid())
	{
		static bool init = false;
		if(!init)
		{
			init = true;
			
			TLS_data backup = *TLS::getData();
			TLS::getData()->domain_ptr = nullptr;
			TLS::getData()->domain_guid = guidNull;
			FreeImage_Initialise();
			*TLS::getData() = backup;
		}

		TLS_data backup = *TLS::getData();
		TLS::getData()->domain_ptr = nullptr;
		TLS::getData()->domain_guid = guidNull;
		FIBITMAP* fibitmap = FreeImage_LoadFromHandle(FIF_PNG,&ImageLoaderIO,(fi_handle)pData);
		*TLS::getData() = backup;

		if(fibitmap)
		{
			_Width = FreeImage_GetWidth(fibitmap);
			_Height = FreeImage_GetHeight(fibitmap);
			_Format = R8G8B8A8;
			_Data = new u8[_Width*_Height*sizeof(u32)];
			for(int iy =0; iy < _Height; ++iy)
				for(int ix =0; ix < _Width; ++ix)
				{
					RGBQUAD color;
					FreeImage_GetPixelColor(fibitmap,ix,iy,&color);
					((u32*)_Data)[iy*_Width+ix] = ((u32)color.rgbRed << 0) | ((u32)color.rgbGreen << 8) | ((u32)color.rgbBlue << 16) | ((u32)0xff << 24);
				}
			
			TLS_data backup = *TLS::getData();
			TLS::getData()->domain_ptr = nullptr;
			TLS::getData()->domain_guid = guidNull;
	
			FreeImage_Unload(fibitmap);

			*TLS::getData() = backup;

			_Levels = 1;
			return;
		}
		pData->Seek(0,IDataStream::ORIGIN_BEGINNING);
		if(TryLoadBLP(pData))
			return;
		
		pData->Seek(0,IDataStream::ORIGIN_BEGINNING);
		/*
		SparseVirtualTextureFile hsvt;
		if(hsvt.Open(pData))
		{
			_Width = 5120;
			_Height = 5120;
			_Format = R8G8B8A8;
			_Levels = 1;
			_Data = new u8[_Width*_Height*sizeof(u32)];


			hsvt.ReadImageFromVirtualTexture(0,0,_Width,_Height,_Data,6);


			return;
		}
		
		pData->Seek(0,IDataStream::ORIGIN_BEGINNING);*/


	}

	// loading failed
	_Width = 256;
	_Height = 256;
	_Format = R8G8B8A8;
	_Levels = 1;
	_Data = new u8[_Width*_Height*sizeof(u32)];
	for(int iy =0; iy < _Height; ++iy)
		for(int ix =0; ix < _Width; ++ix)
			((u32*)_Data)[iy*_Width+ix] = ((1-((iy/64)+(ix/64))%2) * 0x00ffffff) | 0xff000000;
}
Пример #15
0
//!!!be sure to release memory before return null
ResHandler* WIPResourceManager::alloc(const char* filename, WIPResourceType type)
{
    ResHandler* res = new ResHandler;
    switch (type)
    {
    case TEXT:
    {

    }
    break;
    case TEXTURE:
    {
        TextureData *data = new TextureData;
        //初始化FreeImage
        FreeImage_Initialise(TRUE);

        FIBITMAP* bmpConverted = NULL;
        FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
        fif = FreeImage_GetFileType(filename);
        if (fif == FIF_UNKNOWN)
            fif = FreeImage_GetFIFFromFilename(filename);
        if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif))
        {

            FIBITMAP *dib = FreeImage_Load(fif, filename);

            if (!dib)
            {
                printf("[fatal error]: \"%s\" load error!\n", filename);
                delete data;
                delete_handler(res);
                return NULL;
            }

            // we are top left, FIBITMAP is bottom left
            FreeImage_FlipVertical(dib);
            //get infomation
            FREE_IMAGE_TYPE imgType = FreeImage_GetImageType(dib);
            FREE_IMAGE_COLOR_TYPE colorType = FreeImage_GetColorType(dib);
            unsigned int bpp = FreeImage_GetBPP(dib);

            data->bpp = bpp;
            data->color_type = colorType;
            data->image_type = imgType;
            data->channel = 4;

            int x, y;
            RGBQUAD m_rgb;

            //获取图片长宽
            int width = (int)FreeImage_GetWidth(dib);
            int height = (int)FreeImage_GetHeight(dib);

            unsigned char* imgBuf = new unsigned char[width*height * 4];



            bool is_tr = FreeImage_IsTransparent(dib);

            bool is_little = FreeImage_IsLittleEndian();
            //获取图片数据
            //按RGBA格式保存到数组中
            for (y = 0; y<height; y++)
            {
                for (x = 0; x<width; x++)
                {
                    //获取像素值
                    FreeImage_GetPixelColor(dib, x, y, &m_rgb);

                    if (is_little)
                    {
                        imgBuf[y*width * 4 + x * 4 + 0] = m_rgb.rgbRed;
                        imgBuf[y*width * 4 + x * 4 + 1] = m_rgb.rgbGreen;
                        imgBuf[y*width * 4 + x * 4 + 2] = m_rgb.rgbBlue;
                        //判断是否透明图片
                        //如果是就取alpha值保存
                        if (is_tr)
                            imgBuf[y*width * 4 + x * 4 + 3] = m_rgb.rgbReserved;
                        else
                            imgBuf[y*width * 4 + x * 4 + 3] = 255;
                    }
                    else
                    {
                        //大端警告!
                        //Big Endian Warnning!
                        printf("Note:This is a Big endian!\n");
                    }
                }
            }


            data->width = width;
            data->height = height;
            data->size = height*width * 4;


            FreeImage_Unload(dib);

            res->file_name = filename;
            res->extra = data;
            res->nref = 1;
            res->ptr = imgBuf;
            res->size = width*height * 4;
            res->type = TEXTURE;

            _map[filename] = res;

            return res;
        }
    }
    break;

    case SOUND:
    {
    }
    default:
        return res;
        break;
    }
    delete_handler(res);
    return NULL;
}
Пример #16
0
//加载图片
//以RGBA格式存储图片
static bool LoadImg(const char* fname,GLint nID)
{
    
    //初始化FreeImage
    FreeImage_Initialise(TRUE);
    
    //定义图片格式为未知
    FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
    
    //获取图片格式
    fif = FreeImage_GetFileType(fname,0);
    
    //根据获取格式读取图片数据
    FIBITMAP* bitmap = FreeImage_Load(fif,fname,0);
    
    if(!bitmap)
    {
        printf("load error!\n");
        return false;
    }
    
    int x,y;
    RGBQUAD m_rgb;
    
    //获取图片长宽
    width = (int)FreeImage_GetWidth(bitmap);
    height = (int)FreeImage_GetHeight(bitmap);
    
    imgBuf = new unsigned char[width*height*4];
    
    //获取图片数据
    //按RGBA格式保存到数组中
    for(y=0;y<height;y++)
    {
        for(x=0;x<width;x++)
        {
            //获取像素值
            FreeImage_GetPixelColor(bitmap,x,y,&m_rgb);
            
            //将RGB值存入数组
            imgBuf[y*width*4+x*4+0] = m_rgb.rgbRed;
            imgBuf[y*width*4+x*4+1] = m_rgb.rgbGreen;
            imgBuf[y*width*4+x*4+2] = m_rgb.rgbBlue;
            
            //判断是否透明图片
            //如果是就取alpha值保存
            if(FreeImage_IsTransparent(bitmap))
                imgBuf[y*width*4+x*4+3] = m_rgb.rgbReserved;
            else
                imgBuf[y*width*4+x*4+3] = 255;
        }
    }
    
    //绑定纹理ID
    
    if(nID==0)
    {
        glGenTextures(1, &g_texture1);
        glBindTexture(GL_TEXTURE_2D, g_texture1);
    }
    else
    {
        glGenTextures(1, &g_texture2);
        glBindTexture(GL_TEXTURE_2D, g_texture2);
    }
    
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imgBuf);
    
    //释放FreeImage
    delete imgBuf;
    FreeImage_Unload(bitmap);
    return true;
}
Пример #17
0
void *process_thread(void *data)
{
	struct thread_data *args;
	args = (struct thread_data*) data;
	std::queue<std::string> *input = args -> input;
	std::mutex *lock = args -> lock;
	double brightness = args -> brightness;
	double gamma = args -> gamma;
	std::string infile;
	std::string outfile;
	bool exists;
	while (true)
	{
		exists = false;
		while (!(lock -> try_lock()));
		if (input -> empty())
		{
			lock -> unlock();
			break;
		}
		infile = input -> front();
		input -> pop();
		lock -> unlock();
		gen_edit_filename(infile, outfile);
		DIR *editdir = opendir("img/edit");
		struct dirent *file;
		while ((file = readdir(editdir)) != NULL)
		{
			if (strcmp(file -> d_name, outfile.c_str()) == 0)
			{
				exists = true;
				break;
			}
		}
		closedir(editdir);
		infile = "img/" + infile;
		outfile = "img/edit/" + outfile;
		if (exists) { continue; }
		else
		{
			while (!(lock -> try_lock()));
			std::cout << "Processing " << infile << std::endl;
			lock -> unlock();
		}
		FIBITMAP *input = FreeImage_Load(FIF_PNG, infile.c_str());
		if (input == NULL)
		{
			while (!(lock -> try_lock()));
			std::cerr << "Failed to load " << infile << std::endl;
			lock -> unlock();
			exit(1);
		}
		if (!FreeImage_Save(FIF_PNG, input, outfile.c_str()))
		{
			FreeImage_Unload(input);
			while (!(lock -> try_lock()));
			std::cerr << "Failed to save " << outfile << std::endl;
			lock -> unlock();
			exit(1);
		}
		FreeImage_Unload(input);
		FIBITMAP *output = FreeImage_Load(FIF_PNG, outfile.c_str());
		if (output == NULL)
		{
			while (!(lock -> try_lock()));
			std::cerr << "Failed to load " << outfile << std::endl;
			lock -> unlock();
			exit(1);
		}
		unsigned int img_w = FreeImage_GetWidth(output);
		unsigned int img_h = FreeImage_GetHeight(output);
		RGBQUAD pixel;
		for (unsigned int x = 0; x < img_w; x++)
		{
			for (unsigned int y = 0; y < img_h; y++)
			{
				FreeImage_GetPixelColor(output, x, y, &pixel);
				pixel.rgbRed = int(clamp(0, pixel.rgbRed * brightness, 255));
				pixel.rgbBlue = int(clamp(0, pixel.rgbBlue * brightness, 255));
				pixel.rgbGreen = int(clamp(0, pixel.rgbGreen * brightness, 255));
				pixel.rgbRed = int(clamp(0, pow(((double) pixel.rgbRed) / 255.0f, gamma) * 255.0, 255));
				pixel.rgbBlue = int(clamp(0, pow(((double) pixel.rgbBlue) / 255.0f, gamma) * 255.0, 255));
				pixel.rgbGreen = int(clamp(0, pow(((double) pixel.rgbGreen) / 255.0f, gamma) * 255.0, 255));
				FreeImage_SetPixelColor(output, x, y, &pixel);
			}
		}
		if (!FreeImage_Save(FIF_PNG, output, outfile.c_str()))
		{
			FreeImage_Unload(input);
			while (!(lock -> try_lock()));
			std::cerr << "Failed to save " << outfile << std::endl;
			lock -> unlock();
			exit(1);
		}
		FreeImage_Unload(output);
		while (!(lock -> try_lock()));
		std::cout << "Saved " << outfile << std::endl;
		lock -> unlock();
	}
	pthread_exit(NULL);
}
Пример #18
0
void Image::getPixel(RGBQUAD &rgb, int x, int y) const {
	FreeImage_GetPixelColor(m_fiImage, x, y, &rgb);
}
Пример #19
0
bool Image::load (const char* fname)
{
	release () ;

//	createTestData () ;
//	return true ;

	char buf[256] ;
	sprintf_s (buf, sizeof (buf), "image\\%s", fname) ;
//	fname = "image\\avatar000.png" ;

	FILE* pFile = NULL ;
	fopen_s (&pFile, buf, "rb") ;
	if (pFile == NULL)
		return false ;

	FreeImageIO io;

	io.read_proc  = myReadProc;
	io.write_proc = myWriteProc;
	io.seek_proc  = mySeekProc;
	io.tell_proc  = myTellProc;

	// find the buffer format
	FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromHandle(&io, (fi_handle)pFile, 0);

	if(fif == FIF_UNKNOWN) 
	{
	   /* Close the file */
		fclose(pFile);
		return false ;
	}

	// load from the file handle
	FIBITMAP *dib = FreeImage_LoadFromHandle(fif, &io, (fi_handle)pFile, 0);
		
	BYTE* pPixelData = FreeImage_GetBits(dib) ;

	int width, height,pitch  ;
	width =  FreeImage_GetWidth(dib);
	height = FreeImage_GetHeight(dib);
	pitch = FreeImage_GetPitch(dib);
	int bpp = FreeImage_GetBPP(dib) ;
	assert (bpp == 32) ;

	FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(dib) ;
	assert (image_type == FIT_BITMAP) ;

//	/*
	//轉換rgba(因為rgba,的順序不同)
	RGBQUAD rgba ;
	BYTE* pBit = pPixelData ;
	for (int i = 0 ; i<height; i++)
	{
		BYTE* pPixel = pBit ;
		for (int m = 0; m<width; m++)
		{
			if (FreeImage_GetPixelColor(dib, m, i, &rgba))
			{
				BYTE tmp = rgba.rgbBlue ;
				rgba.rgbBlue = rgba.rgbRed ;
				rgba.rgbRed = tmp ;
				FreeImage_SetPixelColor (dib, m, i, &rgba) ;
			}
		}
		pBit += pitch ;
	}
//			*/

//---------------------------------------------------
   //把pixel copy給
   //最大記憶體
	if (pPixelData != NULL)
	{
		assert (textureID == NO_TEXTURE_ID) ;
		if (textureID == NO_TEXTURE_ID)
		{
			//空的要先建立
			// Allocates one texture handle
			glGenTextures(1, &textureID);
		}

		assert (textureID != NO_TEXTURE_ID) ;


		// Binds this texture handle so we can load the data into it
		glBindTexture(GL_TEXTURE_2D, textureID);

		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pPixelData);

		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

		// free the loaded FIBITMAP
		FreeImage_Unload(dib);
	}


	return true ;
}
Vector3f ImportBitmapButton::genNormal(FIBITMAP* myBitmap, int w, int h, int width, int height, float flip){

        //remember folks - the image is flipped!

        Vector4f influences=Vector4f(0,0,0,0);

        RGBQUAD *myColor=new RGBQUAD;

        Vector3f myNormal;

        FreeImage_GetPixelColor(myBitmap,w,h,myColor);
        float myInfluence=(float)myColor->rgbReserved;
        if (myInfluence==0){
            //cout << "bad Normal!!!" << endl;
            return Vector3f(0.0,0.0,0.0);
        }
          //Gathering influences
        if (w>=2 && w<width-2 && h>=2 && h<height-2){

            FreeImage_GetPixelColor(myBitmap,w-1,h,myColor);
            influences.x=(int)myColor->rgbReserved;

            FreeImage_GetPixelColor(myBitmap,w+1,h,myColor);
            influences.z=(int)myColor->rgbReserved;


            FreeImage_GetPixelColor(myBitmap,w,h+1,myColor);
            influences.y=(int)myColor->rgbReserved;


            FreeImage_GetPixelColor(myBitmap,w,h-1,myColor);
            influences.w=(int)myColor->rgbReserved;
        }


        float xN,yN,zN, falloffN;

        falloffN= 16.0;
        //edges
        if (influences.x==0){
            myNormal.x+=-1.0;
            myNormal.z=0.0;
        }

        if (influences.z==0){
            myNormal.x+=1.0;
            myNormal.z=0.0;
        }

        if (influences.y==0){
            myNormal.y+=1.0;
            myNormal.z=0.0;
        }

        if (influences.w==0){
            myNormal.y+=-1.0;
            myNormal.z=0.0;
        }

        if (myNormal!=Vector3f(0,0,0)){
            return myNormal;
        }

        //make linear, although we know it's wrong... it looks good :-)
        xN = (influences.x-influences.z)/falloffN;
        yN = (influences.w-influences.y)/falloffN;
        zN = -max(0.0,1.0-xN-yN);


        myNormal=Vector3f(xN,yN,zN * flip);

        myNormal.normalize();
        return myNormal;
}
void ImportBitmapButton::assembleImage(FIBITMAP* myBitmap, int imageWidth, int imageHeight, float flip){


    sceneData->brush->bMouseControlled=false;
    //go through all of the image
    for (int h=0;h<imageHeight;h++){
    //reset after every line
        for (int w=0;w<imageWidth;w++){

            RGBQUAD *myColor=new RGBQUAD;

            FreeImage_GetPixelColor(myBitmap,w,h,myColor);
            sceneData->brush->color.r=(float)myColor->rgbRed/255.0f;
            sceneData->brush->color.g=(float)myColor->rgbGreen/255.0f;
            sceneData->brush->color.b=(float)myColor->rgbBlue/255.0f;

            Vector3f bLoc;
            bLoc.x=sceneData->brush->drawing->location.x- 10 * (float) ((float)imageWidth/2.0 - (float)w)/(float)imageWidth;
            bLoc.y=sceneData->brush->drawing->location.y+ 10 *  (float)h/(float)imageHeight;
            bLoc.z=sceneData->brush->drawing->location.z- flip * (float)myColor->rgbReserved/32.0f;
            sceneData->brush->setLocation(bLoc);
            //flip y and z when shift down
            if (input->bShiftDown){
                bLoc.z=sceneData->brush->drawing->location.z+ 10 *  (float)h/(float)imageHeight;
                bLoc.y=sceneData->brush->drawing->location.y+ flip * (float)myColor->rgbReserved/32.0f;
                sceneData->brush->setLocation(bLoc);
            }

            Vector3f xyzNormal;

            if ((int)myColor->rgbReserved>0){

                input->mouseVector=Vector3f(0.1,0.1,0.1);   //HACK so that we actually paint something!
                sceneData->brush->setLocation(sceneData->brush->location*sceneData->brush->drawing->scale);
				input->mouse3D=sceneData->brush->location;
                //xyzNormal=genNormal(myBitmap,w,h,imageWidth,imageHeight,flip);
                //xyzNormal+=genNormal(myBitmap,w+1,h,imageWidth,imageHeight,flip);
                //xyzNormal+=genNormal(myBitmap,w-1,h,imageWidth,imageHeight,flip);
                //xyzNormal+=genNormal(myBitmap,w,h+1,imageWidth,imageHeight,flip);
                //xyzNormal+=genNormal(myBitmap,w,h-1,imageWidth,imageHeight,flip);
                //xyzNormal.normalize();
                //sceneData->brush->pNormal=xyzNormal;

                ((DrawTool*)sceneData->controller->currentTool)->paint();

            ///fill holes
            /*
                float myDepth=(float)myColor->rgbReserved;
                float fillStep=1.0;

                //find lowest fillDepth around us
                FreeImage_GetPixelColor(myBitmap,w-1,h,myColor);
                float fillDepth =(float)myColor->rgbReserved;

                FreeImage_GetPixelColor(myBitmap,w+1,h,myColor);
                float fillDepthRight =(float)myColor->rgbReserved;
                if (fillDepthRight<fillDepth)
                    fillDepth=fillDepthRight;

                FreeImage_GetPixelColor(myBitmap,w,h-1,myColor);
                float fillDepthDown =(float)myColor->rgbReserved;
                if (fillDepthDown<fillDepth)
                    fillDepth=fillDepthDown;

                FreeImage_GetPixelColor(myBitmap,w,h+1,myColor);
                float fillDepthUp =(float)myColor->rgbReserved;
                if (fillDepthUp<fillDepth)
                    fillDepth=fillDepthUp;

                if (fillDepth + fillStep/32.0f <myDepth){
                    int i=1;
                    while    if (!sceneData->brush->drawing){
        sceneData->makeWarningPopUp("No drawing! \n please place a drawing first!", this);
        return;
    }
 (fillDepth<myDepth){
                        sceneData->brush->setLocation(sceneData->brush->location+ Vector3f(0,0,fillStep/32.0f));
                        input->mouse3D=sceneData->brush->location;
						 ((DrawTool*)sceneData->controller->currentTool)->paint();
                        fillDepth+=fillStep;
                        i++;
                    }
                }

        ///end fill holes
        */
                //flip y and z when shift down
                //if (input->bShiftDown){
                    //float interim=xyzNormal.y;
                    //xyzNormal.y=xyzNormal.z;
                    //xyzNormal.z=interim;
                //}

            //sceneData->brush->pNormal=xyzNormal;

            }//filling holes
        }//inner for loop
    }//outer for loop

    sceneData->brush->bMouseControlled=true;
}
Пример #22
0
bool IPLFileIO::loadMemory(void* mem, IPLImage*& image)
{
    FIMEMORY* hmem = (FIMEMORY*) mem;
    FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
    FreeImage_SeekMemory(hmem, 0L, SEEK_SET);
    fif = FreeImage_GetFileTypeFromMemory(hmem, 0);

    if(fif == FIF_UNKNOWN)
    {
        // always close the memory stream
        FreeImage_CloseMemory(hmem);
        return false;
    }

    FIBITMAP *dib = FreeImage_LoadFromMemory(fif, hmem);
    int width = FreeImage_GetWidth(dib);
    int height = FreeImage_GetHeight(dib);

    // all files need to be flipped
    FreeImage_FlipVertical(dib);

    if(FreeImage_GetBPP(dib) == 8)
    {
        // grayscale images

        // convert to 32 bit
        FIBITMAP *dib2 = FreeImage_ConvertToGreyscale(dib);

        // clear old image
        delete image;
        // create new instance with the right dimensions
        image = new IPLImage(IPLData::IMAGE_GRAYSCALE, width, height);

        for(int y = 0; y < height; y++)
        {
            for(int x = 0; x < width; x++)
            {
                BYTE value;
                FreeImage_GetPixelIndex(dib2, x, y, &value);
                image->plane(0)->p(x, y) = (value  * FACTOR_TO_FLOAT);
            }
        }

        FreeImage_Unload(dib2);
    }
    else
    {
        // color images

        // convert to 32 bit
        FIBITMAP *dib2 = FreeImage_ConvertTo32Bits(dib);

        // clear old image
        delete image;
        // create new instance with the right dimensions
        image = new IPLImage(IPLData::IMAGE_COLOR, width, height);

        for(int y = 0; y < height; y++)
        {
            for(int x = 0; x < width; x++)
            {
                RGBQUAD rgb;
                FreeImage_GetPixelColor(dib2, x, y, &rgb);
                image->plane(0)->p(x, y) = (rgb.rgbRed   * FACTOR_TO_FLOAT);    // R
                image->plane(1)->p(x, y) = (rgb.rgbGreen * FACTOR_TO_FLOAT);    // G
                image->plane(2)->p(x, y) = (rgb.rgbBlue  * FACTOR_TO_FLOAT);    // B
            }
        }

        FreeImage_Unload(dib2);
    }

    // always close the memory stream
    FreeImage_CloseMemory(hmem);


    // free temporary memory
    FreeImage_Unload(dib);

    return true;
}
Пример #23
0
BOOL fipImage::getPixelColor(unsigned x, unsigned y, RGBQUAD *value) const {
	return FreeImage_GetPixelColor(_dib, x, y, value);
}
Пример #24
0
/*!
 * \brief IPLFileIO::loadFile
 * \param filename
 * \param image pass by pointer reference, because we need to change the pointer
 * \return
 */
bool IPLFileIO::loadFile(const std::string filename, IPLImage*& image, std::string& information)
{
    std::string formatNames[37] =  {"BMP", "ICO", "JPEG", "JNG",
                                    "KOALA", "LBM", "MNG", "PBM",
                                    "PBMRAW", "PCD", "PCX", "PGM", "PGMRAW",
                                    "PNG", "PPM", "PPMRAW", "RAS",
                                    "TARGA", "TIFF", "WBMP", "PSD", "CUT",
                                    "XBM", "XPM", "DDS", "GIF", "HDR",
                                    "FAXG3", "SGI", "EXR", "J2K", "JP2",
                                    "PFM", "PICT", "RAW", "WEBP", "JXR"};

    std::string typeNames[13] =   {"UNKNOWN", "BITMAP", "UINT16", "INT16",
                                    "UINT32", "INT32", "FLOAT", "DOUBLE",
                                    "COMPLEX", "RGB16", "RGBA16", "RGBF",
                                    "RGBAF"};

    FREE_IMAGE_FORMAT format = FIF_UNKNOWN;
    format = FreeImage_GetFileType(filename.c_str());
    if(format == FIF_UNKNOWN)
    {
        return false;
    }

    FIBITMAP *dib = FreeImage_Load(format, filename.c_str());
    int width = FreeImage_GetWidth(dib);
    int height = FreeImage_GetHeight(dib);

    // all files need to be flipped
    FreeImage_FlipVertical(dib);

    if(FreeImage_GetBPP(dib) == 8)
    {
        // grayscale images

        // convert to 32 bit
        FIBITMAP *dib2 = FreeImage_ConvertToGreyscale(dib);

        // clear old image
        delete image;
        // create new instance with the right dimensions
        image = new IPLImage(IPLData::IMAGE_GRAYSCALE, width, height);

        for(int y = 0; y < height; y++)
        {
            for(int x = 0; x < width; x++)
            {
                BYTE value;
                FreeImage_GetPixelIndex(dib2, x, y, &value);
                image->plane(0)->p(x, y) = (value  * FACTOR_TO_FLOAT);
            }
        }

        FreeImage_Unload(dib2);
    }
    else
    {
        // color images

        // convert to 32 bit
        FIBITMAP *dib2 = FreeImage_ConvertTo32Bits(dib);

        // clear old image
        delete image;
        // create new instance with the right dimensions
        image = new IPLImage(IPLData::IMAGE_COLOR, width, height);

        for(int y = 0; y < height; y++)
        {
            for(int x = 0; x < width; x++)
            {
                RGBQUAD rgb;
                FreeImage_GetPixelColor(dib2, x, y, &rgb);
                image->plane(0)->p(x, y) = (rgb.rgbRed   * FACTOR_TO_FLOAT);    // R
                image->plane(1)->p(x, y) = (rgb.rgbGreen * FACTOR_TO_FLOAT);    // G
                image->plane(2)->p(x, y) = (rgb.rgbBlue  * FACTOR_TO_FLOAT);    // B
            }
        }

        FreeImage_Unload(dib2);
    }

    FREE_IMAGE_TYPE type = FreeImage_GetImageType(dib);

    // collect information
    std::stringstream s;
    s << "<b>Type: </b>" << typeNames[type] << "\n";
    s << "<b>Format: </b>" << formatNames[format] << "\n";
    s << "<b>Bits per Pixel: </b>" << FreeImage_GetBPP(dib) << "\n";
    s << "<b>Width: </b>" << width << "\n";
    s << "<b>Height: </b>" << height << "";

    information = s.str();

    // free temporary memory
    FreeImage_Unload(dib);

    return true;
}
Пример #25
0
int liimg_png_load (
	const char* file,
	int*        result_width,
	int*        result_height,
	void**      result_pixels)
{
#ifdef HAVE_FREEIMAGE
	size_t i;
	size_t x;
	size_t y;
	size_t w;
	size_t h;
	uint8_t* pixels;
	FIBITMAP* image;
	RGBQUAD color;

	/* Load the image. */
	image = FreeImage_Load (FIF_PNG, file, PNG_DEFAULT);
	if (image == NULL)
		return 0;

	/* Allocate pixel data. */
	w = FreeImage_GetWidth (image);
	h = FreeImage_GetHeight (image);
	if (w > 0 && h > 0)
	{
		pixels = lisys_calloc (w * h * 4, 1);
		if (pixels == NULL)
		{
			FreeImage_Unload (image);
			return 0;
		}
	}
	else
		pixels = NULL;

	/* Copy the pixel data. */
	for (y = 0, i = 0 ; y < h ; y++)
	{
		for (x = 0 ; x < w ; x++, i++)
		{
			FreeImage_GetPixelColor (image, x, h - y - 1, &color);
			pixels[4 * i + 0] = color.rgbRed;
			pixels[4 * i + 1] = color.rgbGreen;
			pixels[4 * i + 2] = color.rgbBlue;
			if (FreeImage_GetBPP (image) == 32)
				pixels[4 * i + 3] = color.rgbReserved;
			else
				pixels[4 * i + 3] = 0xFF;
		}
	}

	/* Set the results. */
	*result_width = w;
	*result_height = h;
	*result_pixels = pixels;
	FreeImage_Unload (image);

	return 1;
#else
	int x;
	int y;
	int depth;
	int width;
	int height;
	char* dst;
	void* pixels;
	FILE* fp;
	png_bytepp rows;
	png_infop info;
	png_structp png;

	/* Initialize structures. */
	png = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
	if (png == NULL)
	{
		lisys_error_set (ENOMEM, NULL);
		return 0;
	}
	info = png_create_info_struct (png);
	if (info == NULL)
	{
		png_destroy_read_struct (&png, NULL, NULL);
		lisys_error_set (ENOMEM, NULL);
		return 0;
	}

	/* Open file. */
	fp = fopen (file, "rb");
	if (fp == NULL)
	{
		lisys_error_set (EIO, "cannot open file `%s'", file);
		png_destroy_read_struct (&png, &info, NULL);
		return 0;
	}

	/* Read data. */
	if (setjmp (png_jmpbuf (png)))
	{
		lisys_error_set (EIO, "error while reading `%s'", file);
		png_destroy_read_struct (&png, &info, NULL);
		fclose (fp);
		return 0;
	}
	png_init_io (png, fp);
	png_read_png (png, info, PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING, NULL);
	width = png_get_image_width (png, info);
	height = png_get_image_height (png, info);
	rows = png_get_rows (png, info);
	depth = png_get_rowbytes (png, info);
	depth /= width;
	fclose (fp);

	/* Allocate pixel data. */
	pixels = lisys_malloc (width * height * 4);
	if (pixels == NULL)
	{
		png_destroy_read_struct (&png, &info, NULL);
		return 0;
	}

	/* Copy pixel data. */
	if (depth == 3)
	{
		for (y = 0 ; y < height ; y++)
		{
			dst = pixels + 4 * width * y;
			for (x = 0 ; x < width ; x++)
			{
				dst[4 * x + 0] = ((char*) rows[y])[3 * x + 0];
				dst[4 * x + 1] = ((char*) rows[y])[3 * x + 1];
				dst[4 * x + 2] = ((char*) rows[y])[3 * x + 2];
				dst[4 * x + 3] = 0xFF;
			}
		}
	}
	else
	{
		for (y = 0 ; y < height ; y++)
		{
			dst = pixels + 4 * width * y;
			memcpy (dst, rows[y], 4 * width);
		}
	}
	*result_pixels = pixels;
	*result_width = width;
	*result_height = height;
	png_destroy_read_struct (&png, &info, NULL);

	return 1;
#endif
}
static struct graphics_image_priv *
image_new(struct graphics_priv *gr, struct graphics_image_methods *meth,
	  char *path, int *w, int *h, struct point *hot, int rotation)
{
#if USE_FREEIMAGE
	FIBITMAP *image;
	RGBQUAD aPixel;
	unsigned char *data;
	int width, height, i, j;
	struct graphics_image_priv *gi;
	//check if image already exists in hashmap
	struct graphics_image_priv *curr_elem =
	    g_hash_table_lookup(hImageData, path);
	if (curr_elem == &image_error) {
		//found but couldn't be loaded
		return NULL;
	} else if (curr_elem) {
		//found and OK -> use hastable entry
		*w = curr_elem->w;
		*h = curr_elem->h;
		hot->x = curr_elem->w / 2 - 1;
		hot->y = curr_elem->h / 2 - 1;
		return curr_elem;
	} else {
		if (strlen(path) < 4) {
			g_hash_table_insert(hImageData, g_strdup(path),
					    &image_error);
			return NULL;
		}
		char *ext_str = path + strlen(path) - 3;
		if (strstr(ext_str, "png") || strstr(path, "PNG")) {
			if ((image =
			     FreeImage_Load(FIF_PNG, path, 0)) == NULL) {
				g_hash_table_insert(hImageData,
						    g_strdup(path),
						    &image_error);
				return NULL;
			}
		} else if (strstr(ext_str, "xpm") || strstr(path, "XPM")) {
			if ((image =
			     FreeImage_Load(FIF_XPM, path, 0)) == NULL) {
				g_hash_table_insert(hImageData,
						    g_strdup(path),
						    &image_error);
				return NULL;
			}
		} else if (strstr(ext_str, "svg") || strstr(path, "SVG")) {
			char path_new[256];
			snprintf(path_new, strlen(path) - 3, "%s", path);
			strcat(path_new, "_48_48.png");

			if ((image =
			     FreeImage_Load(FIF_PNG, path_new,
					    0)) == NULL) {
				g_hash_table_insert(hImageData,
						    g_strdup(path),
						    &image_error);
				return NULL;
			}
		} else {
			g_hash_table_insert(hImageData, g_strdup(path),
					    &image_error);
			return NULL;
		}

		if (FreeImage_GetBPP(image) == 64) {
			FIBITMAP *image2;
			image2 = FreeImage_ConvertTo32Bits(image);
			FreeImage_Unload(image);
			image = image2;
		}
#if FREEIMAGE_MAJOR_VERSION*100+FREEIMAGE_MINOR_VERSION  >= 313
		if (rotation) {
			FIBITMAP *image2;
			image2 = FreeImage_Rotate(image, rotation, NULL);
			image = image2;
		}
#endif

		gi = g_new0(struct graphics_image_priv, 1);

		width = FreeImage_GetWidth(image);
		height = FreeImage_GetHeight(image);

		if ((*w != width || *h != height) && 0 < *w && 0 < *h) {
			FIBITMAP *image2;
			image2 = FreeImage_Rescale(image, *w, *h, NULL);
			FreeImage_Unload(image);
			image = image2;
			width = *w;
			height = *h;
		}

		data = (unsigned char *) malloc(width * height * 4);

		RGBQUAD *palette = NULL;
		if (FreeImage_GetBPP(image) == 8) {
			palette = FreeImage_GetPalette(image);
		}

		for (i = 0; i < height; i++) {
			for (j = 0; j < width; j++) {
				unsigned char idx;
				if (FreeImage_GetBPP(image) == 8) {
					FreeImage_GetPixelIndex(image, j,
								height -
								i - 1,
								&idx);
					data[4 * width * i + 4 * j + 0] =
					    palette[idx].rgbRed;
					data[4 * width * i + 4 * j + 1] =
					    palette[idx].rgbGreen;
					data[4 * width * i + 4 * j + 2] =
					    palette[idx].rgbBlue;
					data[4 * width * i + 4 * j + 3] =
					    255;
				} else if (FreeImage_GetBPP(image) == 16
					   || FreeImage_GetBPP(image) == 24
					   || FreeImage_GetBPP(image) ==
					   32) {
					FreeImage_GetPixelColor(image, j,
								height -
								i - 1,
								&aPixel);
					int transparent =
					    (aPixel.rgbRed == 0
					     && aPixel.rgbBlue == 0
					     && aPixel.rgbGreen == 0);
					data[4 * width * i + 4 * j + 0] =
					    transparent ? 0 : (aPixel.
							       rgbRed);
					data[4 * width * i + 4 * j + 1] =
					    (aPixel.rgbGreen);
					data[4 * width * i + 4 * j + 2] =
					    transparent ? 0 : (aPixel.
							       rgbBlue);
					data[4 * width * i + 4 * j + 3] =
					    transparent ? 0 : 255;

				}
			}
		}

		FreeImage_Unload(image);

		*w = width;
		*h = height;
		gi->w = width;
		gi->h = height;
		gi->hot_x = width / 2 - 1;
		gi->hot_y = height / 2 - 1;
		hot->x = width / 2 - 1;
		hot->y = height / 2 - 1;
		gi->data = data;
		gi->path = path;
		//add to hashtable
		g_hash_table_insert(hImageData, g_strdup(path), gi);
		return gi;
	}
#else
	return NULL;
#endif
}