Ejemplo n.º 1
0
/* Loads the stereo background */
void loadStereoBackground(void) {
	printf("Left Background loaded = %s", leftBackground.c_str());
	
	// Import left image
	FREE_IMAGE_FORMAT fImageFormatLeft = FreeImage_GetFileType(leftBackground.c_str(), 0);
	FIBITMAP *fBitMapLeft = FreeImage_Load(fImageFormatLeft, leftBackground.c_str(), 0);
	fBitMapLeft = FreeImage_ConvertTo24Bits(fBitMapLeft);
	
	glGenTextures(1, &bgLeftID);
	glBindTexture(GL_TEXTURE_2D, bgLeftID);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	
	BYTE *bitsLeft = new BYTE[FreeImage_GetWidth(fBitMapLeft) * FreeImage_GetHeight(fBitMapLeft) * 3];
	BYTE *pixelsLeft = (BYTE*)FreeImage_GetBits(fBitMapLeft);
	// Switch in RGB format
	for(int pix=0; pix<FreeImage_GetWidth(fBitMapLeft) * FreeImage_GetHeight(fBitMapLeft); pix++) {
		bitsLeft[pix*3+0]=pixelsLeft[pix*3+2];
		bitsLeft[pix*3+1]=pixelsLeft[pix*3+1];
		bitsLeft[pix*3+2]=pixelsLeft[pix*3+0];
	}
	
	glTexImage2D(GL_TEXTURE_2D, 0, 3, FreeImage_GetWidth(fBitMapLeft), FreeImage_GetHeight(fBitMapLeft), 
				 0, GL_RGB, GL_UNSIGNED_BYTE, bitsLeft);
	FreeImage_Unload(fBitMapLeft);
	delete bitsLeft;
	
	printf("Right Background loaded = %s", rightBackground.c_str());
	// Import right image
	FREE_IMAGE_FORMAT fImageFormatRight = FreeImage_GetFileType(rightBackground.c_str(), 0);
	FIBITMAP *fBitMapRight = FreeImage_Load(fImageFormatRight, rightBackground.c_str(), 0);
	fBitMapRight = FreeImage_ConvertTo24Bits(fBitMapRight);
	
	glGenTextures(1, &bgRightID);
	glBindTexture(GL_TEXTURE_2D, bgRightID);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	
	BYTE *bitsRight = new BYTE[FreeImage_GetWidth(fBitMapRight) * FreeImage_GetHeight(fBitMapRight) * 3];
	BYTE *pixelsRight = (BYTE*)FreeImage_GetBits(fBitMapRight);
	// Switch in RGB format
	for(int pix=0; pix<FreeImage_GetWidth(fBitMapRight) * FreeImage_GetHeight(fBitMapRight); pix++) {
		bitsRight[pix*3+0]=pixelsRight[pix*3+2];
		bitsRight[pix*3+1]=pixelsRight[pix*3+1];
		bitsRight[pix*3+2]=pixelsRight[pix*3+0];
	}
	
	glTexImage2D(GL_TEXTURE_2D, 0, 3, FreeImage_GetWidth(fBitMapRight), FreeImage_GetHeight(fBitMapRight), 
				 0, GL_RGB, GL_UNSIGNED_BYTE, bitsRight);
	FreeImage_Unload(fBitMapRight);
	delete bitsRight;
	
	stereoBackground = true;
}
Ejemplo n.º 2
0
/**
 * Create twitch texture
 */
bool CreateTexture()
{
	const char* textureFile = "twitch.png";

	FREE_IMAGE_FORMAT format = FreeImage_GetFileType(textureFile, 0);

	FIBITMAP* bitmap = FreeImage_Load(format, textureFile);

	if ( bitmap == nullptr )
	{
		std::string filePath = std::string("..\\..\\") + textureFile;

		format = FreeImage_GetFileType(filePath.c_str(), 0);

		bitmap = FreeImage_Load(format, filePath.c_str() );

		if ( bitmap == nullptr )
		{
			ReportError("Failed to load texture");
			return false;
		}
	}	
 
	FIBITMAP* bitmap32 = FreeImage_ConvertTo32Bits(bitmap);	
 
	int w = FreeImage_GetWidth(bitmap32);
	int h = FreeImage_GetHeight(bitmap32);

	char* buffer = (char*)FreeImage_GetBits(bitmap32);
 
	glGenTextures(1, &gTwitchTexture);

	glBindTexture(GL_TEXTURE_2D, gTwitchTexture);

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

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid*)buffer );

	FreeImage_Unload(bitmap32);
	FreeImage_Unload(bitmap);

	if ( glGetError() != GL_NO_ERROR )
	{
		ReportError("Failed to load texture");
		glBindTexture(GL_TEXTURE_2D, 0);
		return false;
	}
	
	glBindTexture(GL_TEXTURE_2D, 0);

	return true;
}
Ejemplo n.º 3
0
GLuint TextureFromFile(const char* filename)
{
    FREE_IMAGE_FORMAT fileFormat = FIF_UNKNOWN;
    FIBITMAP *image(0);
  
    BYTE* bits(0);
    unsigned int width(0), height(0);
     
    fileFormat = FreeImage_GetFileType(filename, 0);
    image = FreeImage_Load(fileFormat, filename);
  
    if(!image)
        return 0;
  
    bits = FreeImage_GetBits(image);
    width = FreeImage_GetWidth(image);
  
    height = FreeImage_GetHeight(image);
  
    GLuint tex;
    glGenTextures(1, &tex);
  
    glBindTexture(GL_TEXTURE_2D, tex);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_BGR, GL_UNSIGNED_BYTE, bits);
  
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  
    FreeImage_Unload(image);
  
    return tex;
}
Ejemplo n.º 4
0
BOOL fipImage::load(const char* 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_GetFileType(lpszPathName, 0);
	if(fif == FIF_UNKNOWN) {
		// no signature ?
		// try to guess the file format from the file extension
		fif = FreeImage_GetFIFFromFilename(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_Load(fif, lpszPathName, flag);
		_bHasChanged = TRUE;
		if(_dib == NULL)
			return FALSE;
		return TRUE;
	}
	return FALSE;
}
Ejemplo n.º 5
0
image* createTexture(char* filePath){
	//FreeImage_Initialise(FALSE);
	FREE_IMAGE_FORMAT formato = FreeImage_GetFileType(filePath,0);//Automatocally detects the format(from over 20 formats!)
	FIBITMAP* imagen = FreeImage_Load(formato, filePath,JPEG_ACCURATE);
	FIBITMAP* temp = imagen;
	imagen = FreeImage_ConvertTo32Bits(imagen);
	FreeImage_Unload(temp);

	int w = FreeImage_GetWidth(imagen);
	int h = FreeImage_GetHeight(imagen);
	GLubyte* textura = (GLubyte*) malloc(4*w*h);
	char* pixeles = (char*)FreeImage_GetBits(imagen);
	//FreeImage loads in BGR format, so you need to swap some bytes(Or use GL_BGR).
	int j = 0;
	for(; j<w*h; j++){
		textura[j*4+0]= pixeles[j*4+2];
		textura[j*4+1]= pixeles[j*4+1];
		textura[j*4+2]= pixeles[j*4+0];
		textura[j*4+3]= pixeles[j*4+3];
	}
	free(pixeles);
	FreeImage_Unload(imagen);
	image* imageToReturn = malloc(sizeof(image));
	imageToReturn->height = h;
	imageToReturn->width = w;
	imageToReturn->texture = textura;
	return imageToReturn;
}
Ejemplo n.º 6
0
void pre_lzw::do_lzw(const QDir & d){
    FreeImage_Initialise(true);
    QString label_out = "";
    QString dst = ui.lineEdit_2->text();
    QString d_src = d.path();
    QString serial_str = d_src.right(5);
    QDir dst_dir(dst);
    dst_dir.mkdir(serial_str);
    QString image_name = "", image_name_path(""), saved_name("");
    int dot_pos = 0;
    QFileInfoList handled_images = d.entryInfoList();
    QList<QFileInfo>::iterator images_iter = handled_images.begin();
    
    FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
    for(; images_iter < handled_images.end(); ++images_iter){
        image_name_path = (*images_iter).absoluteFilePath();
        if((*images_iter).isDir()){
            continue;
        }
        FIBITMAP *handled_image = 0;
        image_name = (*images_iter).fileName();
        dot_pos = image_name.indexOf(".");
        saved_name = dst + "\\" + serial_str + "\\" + image_name.left(dot_pos) + "_c.tif";
        label_out += saved_name + "\n";
        fif = FreeImage_GetFileType(image_name_path.toStdString().c_str());
        handled_image = FreeImage_Load(fif, image_name_path.toStdString().c_str());
        FreeImage_Save(FIF_TIFF, handled_image, saved_name.toStdString().c_str(), TIFF_LZW);
        FreeImage_Unload(handled_image);
    }
//    ui.label->setText(label_out);
    FreeImage_DeInitialise();

}
Ejemplo n.º 7
0
bool CTexture::loadTexture2D(string a_sPath, bool bGenerateMipMaps)
{
	FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
	FIBITMAP* dib(0);

	fif = FreeImage_GetFileType(a_sPath.c_str(), 0); // Check the file signature and deduce its format

	if(fif == FIF_UNKNOWN) // If still unknown, try to guess the file format from the file extension
		fif = FreeImage_GetFIFFromFilename(a_sPath.c_str());
	
	if(fif == FIF_UNKNOWN) // If still unknown, return failure
		return false;

	if(FreeImage_FIFSupportsReading(fif)) // Check if the plugin has reading capabilities and load the file
		dib = FreeImage_Load(fif, a_sPath.c_str());
	if(!dib)
		return false;

	BYTE* bDataPointer = FreeImage_GetBits(dib); // Retrieve the image data

	// If somehow one of these failed (they shouldn't), return failure
	if(bDataPointer == NULL || FreeImage_GetWidth(dib) == 0 || FreeImage_GetHeight(dib) == 0)
		return false;

	GLenum format = FreeImage_GetBPP(dib) == 24 ? GL_BGR : FreeImage_GetBPP(dib) == 8 ? GL_LUMINANCE : 0;
	createFromData(bDataPointer, FreeImage_GetWidth(dib), FreeImage_GetHeight(dib), FreeImage_GetBPP(dib), format, bGenerateMipMaps);
	
	FreeImage_Unload(dib);

	sPath = a_sPath;

	return true; // Success
}
Ejemplo n.º 8
0
/**
Test extracting a memory buffer from a memory stream
*/
void testAcquireMemIO(const char *lpszPathName) {

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

	// open and allocate a memory stream
	fipMemoryIO memIO;

	// save the file to memory
	memIO.write(FIF_PNG, dib, PNG_DEFAULT);

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

	memIO.acquire(&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 (memIO is destroyed)
}
Ejemplo n.º 9
0
GLuint make_texture(const char *filename) {

    GLuint texture;

    // Get the image file type from FreeImage.
    FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(filename, 0);

    // Actually load the image file.
    FIBITMAP *dib = FreeImage_Load(fifmt, filename, 0);

    // Now, there is no guarantee that the image file
    // loaded will be GL_RGB, so we force FreeImage to
    // convert the image to GL_RGB.
    dib = FreeImage_ConvertTo32Bits(dib);

    if (dib != NULL) {
        glGenTextures(1, &texture);
        glBindTexture(GL_TEXTURE_2D, texture);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

        // This is important to note, FreeImage loads textures in
        // BGR format. Now we could just use the GL_BGR extension
        // But, we will simply swap the B and R components ourselves.
        // Firstly, allocate the new bit data doe the image.
        //BYTE *bits = malloc(FreeImage_GetWidth(dib) * FreeImage_GetHeight(dib) * 4);

        // get a pointer to FreeImage's data.
        BYTE *pixels = (BYTE*) FreeImage_GetBits(dib);

        // Iterate through the pixels, copying the data
        // from 'pixels' to 'bits' except in RGB format.
        /*
        int pix;
        for (pix = 0; pix < FreeImage_GetWidth(dib) * FreeImage_GetHeight(dib); pix++) {
            bits[pix * 4 + 0] = pixels[pix * 4 + 2];
            bits[pix * 4 + 1] = pixels[pix * 4 + 1];
            bits[pix * 4 + 2] = pixels[pix * 4 + 0];
            bits[pix * 4 + 3] = pixels[pix * 4 + 3];
        }*/

        // The new 'glTexImage2D' function, the prime difference
        // being that it gets the width, height and pixel information
        // from 'bits', which is the RGB pixel data..
        //glTexImage2D(GL_TEXTURE_2D, 0, 4, FreeImage_GetWidth(dib), FreeImage_GetHeight(dib), 0,
        //       GL_RGBA, GL_UNSIGNED_BYTE, bits);
        glTexImage2D(GL_TEXTURE_2D, 0, 4, FreeImage_GetWidth(dib), FreeImage_GetHeight(dib), 0,
                GL_BGRA, GL_UNSIGNED_BYTE, pixels);


        // Unload the image.
        // and free the bit data.
        FreeImage_Unload(dib);
        //free(bits);
    } else {
        fprintf(stderr, "Can't load texture: %s\n", filename);
    }
    
    return texture;
}
Ejemplo n.º 10
0
BOOL DLL_CALLCONV 
FreeImage_JPEGCrop(const char *src_file, const char *dst_file, int left, int top, int right, int bottom) {
	char crop[64];

	try {
		// check the src file format
		if(FreeImage_GetFileType(src_file) != FIF_JPEG) {
			throw FI_MSG_ERROR_MAGIC_NUMBER;
		}
		
		// normalize the rectangle
		if(right < left) {
			INPLACESWAP(left, right);
		}
		if(bottom < top) {
			INPLACESWAP(top, bottom);
		}

		// build the crop option
		sprintf(crop, "%dx%d+%d+%d", right - left, bottom - top, left, top);

		// setup IO
		FilenameIO filenameIO;
		memset(&filenameIO, 0, sizeof(FilenameIO));
		filenameIO.src_file = src_file;
		filenameIO.dst_file = dst_file;

		// perform the transformation
		return LosslessTransform(&filenameIO, FIJPEG_OP_NONE, crop, FALSE);

	} catch(const char *text) {
		FreeImage_OutputMessageProc(FIF_JPEG, text);
		return FALSE;
	}
}
Ejemplo n.º 11
0
bool LoadTexture(const char* filename,	//where to load the file from
	GLuint &texID,
	//does not have to be generated with glGenTextures
	GLenum image_format,		//format the image is in
	GLint internal_format,		//format to store the image in
	GLint level ,					//mipmapping level
	GLint border ) {
	//image format
	FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
	//pointer to the image, once loaded
	FIBITMAP *dib(0);
	//pointer to the image data
	BYTE* bits(0);
	//image width and height
	unsigned int width(0), height(0);

	//check the file signature and deduce its format
	fif = FreeImage_GetFileType(filename, 0);
	//if still unknown, try to guess the file format from the file extension
	if (fif == FIF_UNKNOWN)
		fif = FreeImage_GetFIFFromFilename(filename);
	//if still unkown, return failure
	if (fif == FIF_UNKNOWN)
		return false;

	//check that the plugin has reading capabilities and load the file
	if (FreeImage_FIFSupportsReading(fif))
		dib = FreeImage_Load(fif, filename);
	//if the image failed to load, return failure
	if (!dib)
		return false;

	//retrieve the image data
	bits = FreeImage_GetBits(dib);
	//get the image width and height
	width = FreeImage_GetWidth(dib);
	height = FreeImage_GetHeight(dib);
	//if this somehow one of these failed (they shouldn't), return failure
	if ((bits == 0) || (width == 0) || (height == 0))
		return false;
	//generate an OpenGL texture ID for this texture
	glGenTextures(1, &texID);
	//store the texture ID mapping
	//bind to the new texture ID
	
	glBindTexture(GL_TEXTURE_2D, texID);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	// 线形滤波
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);   // 线形滤波
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
																		//store the texture data for OpenGL use
	glTexImage2D(GL_TEXTURE_2D, level, internal_format, width, height,
		border, image_format, GL_UNSIGNED_BYTE, bits);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	//Free FreeImage's copy of the data
	FreeImage_Unload(dib);

	//return success
	return true;
}
Ejemplo n.º 12
0
//----------------------------------------------------------------
void  ofImage::saveImageFromPixels(string fileName, ofPixels &pix){

	if (pix.bAllocated == false){
		ofLog(OF_LOG_ERROR,"error saving image - pixels aren't allocated");
		return;
	}

	#ifdef TARGET_LITTLE_ENDIAN
		if (pix.bytesPerPixel != 1) swapRgb(pix);
	#endif

	FIBITMAP * bmp	= getBmpFromPixels(pix);

	#ifdef TARGET_LITTLE_ENDIAN
		if (pix.bytesPerPixel != 1) swapRgb(pix);
	#endif

	fileName = ofToDataPath(fileName);
	if (pix.bAllocated == true){
		FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
		fif = FreeImage_GetFileType(fileName.c_str(), 0);
		if(fif == FIF_UNKNOWN) {
			// or guess via filename
			fif = FreeImage_GetFIFFromFilename(fileName.c_str());
		}
		if((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
			FreeImage_Save(fif, bmp, fileName.c_str(), 0);
		}
	}

	if (bmp != NULL){
		FreeImage_Unload(bmp);
	}
}
Ejemplo n.º 13
0
int _tmain(int argc, _TCHAR* argv[])
{
	const char* filename = "1.jpg";
	FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(filename, 0);
	FIBITMAP *dib(0);
	//pointer to the image data
	BYTE* bits(0);
	//image width and height
	unsigned int width(0), height(0);

	if(fif == FIF_UNKNOWN) 
		fif = FreeImage_GetFIFFromFilename(filename);

	if(fif == FIF_UNKNOWN)
		return 1;

	if(FreeImage_FIFSupportsReading(fif))
		dib = FreeImage_Load(fif, filename);

	//retrieve the image data
	bits = FreeImage_GetBits(dib);
	//get the image width and height
	width = FreeImage_GetWidth(dib);
	height = FreeImage_GetHeight(dib);
	//if this somehow one of these failed (they shouldn't), return failure
	if((bits == 0) || (width == 0) || (height == 0))
		return 1;
	return 0;
}
Ejemplo n.º 14
0
/*
* \brief Method that import a texture to be used on the model
* \param[in] fTexture a constant char array containing the path to the file of the texture
* \param[in] tex_number an unsigned integer representing the type of texture (i.e. 0 = Diffuse color, 1 = Normalmap, 2 = Specular Color)
* \return a boolean indicating if the import was successful of not
*/
GLboolean Model::importTexture(const GLchar * fTexture, GLuint tex_number)
{
	GLboolean import_is_ok = GL_FALSE;
	FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
	FIBITMAP *dib(0);
	BYTE* bits(0);
	GLuint width(0), height(0);

	fif = FreeImage_GetFileType(fTexture, 0);

	if (FreeImage_FIFSupportsReading(fif))
	{
		dib = FreeImage_Load(fif, fTexture);
		dib = FreeImage_ConvertTo32Bits(dib);
		bits = FreeImage_GetBits(dib);
		width = FreeImage_GetWidth(dib);
		height = FreeImage_GetHeight(dib);
		import_is_ok = GL_TRUE;

		glBindTexture(GL_TEXTURE_2D, m_texture_id[tex_number]);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, &bits[0]);
		glGenerateMipmap(GL_TEXTURE_2D);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);

		FreeImage_Unload(dib);
	}
	return import_is_ok;
}
Ejemplo n.º 15
0
GLuint Engine::useTexture(char* texDir, bool wrap) {
	FIBITMAP* imagePtr =
		FreeImage_ConvertTo32Bits(
			FreeImage_Load(
				FreeImage_GetFileType(texDir, 0), texDir)
			);
	GLuint texture;
	glGenTextures(1, &texture); // i used to be 1
	glBindTexture(GL_TEXTURE_2D, texture);

	glTexImage2D(GL_TEXTURE_2D, 0,
		GL_SRGB_ALPHA,
		FreeImage_GetWidth(imagePtr),
		FreeImage_GetHeight(imagePtr),
		0, GL_BGRA, GL_UNSIGNED_BYTE,
		(void*)FreeImage_GetBits(imagePtr));

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP);

	// Clear from RAM
	FreeImage_Unload(imagePtr);
	// Unbind when finished uplloading
	glBindTexture(GL_TEXTURE_2D, 0);
	return texture;
}
Ejemplo n.º 16
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);

}
Ejemplo n.º 17
0
void TextureLoader::loadImageToGLTexture(unsigned& texture_handle, std::string const& image_path, unsigned color_format, unsigned texture_unit)
{
	glGenTextures(1, &texture_handle);

	glActiveTexture(texture_unit);
	glBindTexture(GL_TEXTURE_2D, texture_handle);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	FIBITMAP* bitmap = FreeImage_Load(
		FreeImage_GetFileType(image_path.c_str(), 0),
		image_path.c_str());

	FIBITMAP * pImage = FreeImage_ConvertTo24Bits(bitmap);
	int nWidth = FreeImage_GetWidth(pImage);
	int nHeight = FreeImage_GetHeight(pImage);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, nWidth, nHeight,
		0, GL_BGR, GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(pImage));

	FreeImage_Unload(pImage);

}
Ejemplo n.º 18
0
void loadTexture(int n, GLuint texture, GLint sampler, int texUnit, const char *filename)
{
    FREE_IMAGE_FORMAT imageFormat = FreeImage_GetFileType(filename, 0);
    FIBITMAP *bitmap = FreeImage_Load(imageFormat, filename, 0);
    unsigned int width = FreeImage_GetWidth(bitmap);
    unsigned int height = FreeImage_GetHeight(bitmap);
    unsigned char *data = FreeImage_GetBits(bitmap);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
    if (texUnit == 0)
    {
        glActiveTexture(GL_TEXTURE0);
        g_texUnit0 = true;
    }
    else
    {
        glActiveTexture(GL_TEXTURE1);
        g_texUnit1 = true;
    }

    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    glUniform1d(sampler, texUnit);
    glClear(GL_COLOR_BUFFER_BIT);
    if (g_texUnit0 && g_texUnit1)
    {
        glDrawArrays(GL_TRIANGLE_STRIP, 0, n);
    }

    FreeImage_Unload(bitmap);
}
Ejemplo n.º 19
0
int TextureLoader::LoadTexture(const char * imagepath)
{    
    // Load image using the Free Image library
    FREE_IMAGE_FORMAT format = FreeImage_GetFileType(imagepath, 0);
    FIBITMAP* image = FreeImage_Load(format, imagepath);
    FIBITMAP* image32bits = FreeImage_ConvertTo32Bits(image);
    
    // Get an available texture index from OpenGL
    GLuint texture = 0;
    glGenTextures(1, &texture);
    assert(texture != 0);
    
    // Set OpenGL filtering properties (bi-linear interpolation)
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    // Retrieve width and hight
    int width = FreeImage_GetWidth(image32bits);
    int height = FreeImage_GetHeight(image32bits);
    
    // This will upload the texture to the GPU memory
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height,
                 0, GL_BGRA, GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(image32bits));
    
    // Free images
    FreeImage_Unload(image);
    FreeImage_Unload(image32bits);
 
    return texture;
}
Ejemplo n.º 20
0
int Init ( AAContext *aaContext )
{
    UserData *userData = (UserData*) aaContext->userData;
	userData->animation = (Animation*) malloc(sizeof(Animation));
	memset(userData->animation, 0x0, sizeof(Animation));
    glGenTextures(1, &userData->earth_texture);
	glBindTexture(GL_TEXTURE_2D, userData->earth_texture);
    FIBITMAP* bitmap = FreeImage_Load(
        FreeImage_GetFileType("earth.png", 0),
        "earth.png");
    FIBITMAP *pImage = FreeImage_ConvertTo32Bits(bitmap);
    int nWidth = FreeImage_GetWidth(pImage);
    int nHeight = FreeImage_GetHeight(pImage);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, nWidth, nHeight,
		0, GL_BGRA, GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(pImage));
	FreeImage_Unload(pImage);
	FreeImage_Unload(bitmap); // correct?
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);

   const char vShaderStr[] =  
	  "uniform mat4 u_mvp;      					 \n"
	  "attribute vec2 a_texCoords;                   \n"
	  "varying vec2 v_texCoords;						 \n"
      "attribute vec4 a_position;					 \n"
	  "out float debug;								\n"
      "void main()									 \n"
      "{											 \n"
	  "vec4 pos = u_mvp * a_position;		\n"
	  "gl_Position = pos;                      \n"
	  "v_texCoords = a_texCoords;					\n"
	  "debug = a_position.y;                      \n"
      "}                                             \n";
   
   const char fShaderStr[] =  
	  "in float debug;                                     \n"
	  "varying vec2 v_texCoords;								\n"
	  "uniform sampler2D s_earthTexture;                   \n"
      "precision mediump float;                            \n"
      "void main()                                         \n"
      "{                                                   \n"
	  "vec4 color = texture(s_earthTexture, v_texCoords);     \n"
//	  "if(debug>0) \n"
//	  "color = vec4(1.0, 0.0, 0.0, 1.0); \n"
	  "gl_FragColor = color;                               \n"
      "}                                                   \n";

   // Load the shaders and get a linked program object
   userData->programObject = LoadProgram ( vShaderStr, fShaderStr );

   // Get the attribute locations
   userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" );
   userData->mvpLoc = glGetUniformLocation(userData->programObject, "u_mvp");

   userData->texCoordsLoc = glGetAttribLocation(userData->programObject, "a_texCoords");
   userData->texLoc = glGetUniformLocation(userData->programObject, "s_earthTexture");
   glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
   glEnable(GL_DEPTH_TEST);
   return TRUE;
}
Ejemplo n.º 21
0
/* Loads the background image */
void loadBackground(void) {
	//printf("Background image loaded %s", backgroundName.c_str());
	
	FREE_IMAGE_FORMAT fImageFormat = FreeImage_GetFileType(backgroundName.c_str(), 0);
	FIBITMAP *fBitMap = FreeImage_Load(fImageFormat, backgroundName.c_str(), 0);
	fBitMap = FreeImage_ConvertTo24Bits(fBitMap);
	
	glGenTextures(1, &bgTextureID);
	glBindTexture(GL_TEXTURE_2D, bgTextureID);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	
	BYTE *bits = new BYTE[FreeImage_GetWidth(fBitMap) * FreeImage_GetHeight(fBitMap) * 3];
	BYTE *pixels = (BYTE*)FreeImage_GetBits(fBitMap);
	
    // Switch in RGB format
	for(int pix=0; pix<FreeImage_GetWidth(fBitMap) * FreeImage_GetHeight(fBitMap); pix++) {
		bits[pix*3+0]=pixels[pix*3+2];
		bits[pix*3+1]=pixels[pix*3+1];
		bits[pix*3+2]=pixels[pix*3+0];
	}
	
	glTexImage2D(GL_TEXTURE_2D, 0, 3, FreeImage_GetWidth(fBitMap), FreeImage_GetHeight(fBitMap), 
				 0, GL_RGB, GL_UNSIGNED_BYTE, bits);
	FreeImage_Unload(fBitMap);
	delete bits;
	
	background = true;
}
Ejemplo n.º 22
0
RGBAImage* RGBAImage::ReadFromFile(const char* filename)
{
	const FREE_IMAGE_FORMAT fileType = FreeImage_GetFileType(filename);
	if(FIF_UNKNOWN == fileType)
	{
		printf("Unknown filetype %s\n", filename);
		return 0;
	}

	FIBITMAP* freeImage = 0;
	if(FIBITMAP* temporary = FreeImage_Load(fileType, filename, 0))
	{
		freeImage = FreeImage_ConvertTo32Bits(temporary);
		FreeImage_Unload(temporary);
	}
	if(!freeImage)
	{
		printf( "Failed to load the image %s\n", filename);
		return 0;
	}

	RGBAImage *result = ToRGBAImage(freeImage);

	FreeImage_Unload(freeImage);

	return result;
}
Ejemplo n.º 23
0
std::shared_ptr<RGBAImage> read_from_file(const std::string &filename)
{
    const auto file_type = FreeImage_GetFileType(filename.c_str());
    if (FIF_UNKNOWN == file_type)
    {
        throw RGBImageException("Unknown filetype '" + filename + "'");
    }

    FIBITMAP *free_image = nullptr;
    if (auto temporary = FreeImage_Load(file_type, filename.c_str(), 0))
    {
        free_image = FreeImage_ConvertTo32Bits(temporary);
        FreeImage_Unload(temporary);
    }
    if (not free_image)
    {
        throw RGBImageException("Failed to load the image " + filename);
    }

    auto result = to_rgba_image(free_image);

    FreeImage_Unload(free_image);

    return result;
}
Ejemplo n.º 24
0
void loadTexture(char *textureFileName, GLuint &textureMapID)	
{
	FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(textureFileName, 0);

	FIBITMAP *dib = FreeImage_Load(fifmt, textureFileName,0);

	FIBITMAP *temp = dib;
    dib = FreeImage_ConvertTo32Bits(temp);
    FreeImage_Unload(temp);	 
	
    if( dib != NULL )
	{
        
        glGenTextures( 1, &textureMapID );
		glBindTexture( GL_TEXTURE_2D, textureMapID );
        
         
        BYTE *pixels = (BYTE*)FreeImage_GetBits(dib);
        
        //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FreeImage_GetWidth(dib), FreeImage_GetHeight(dib), 0, GL_BGRA, GL_UNSIGNED_BYTE, pixels);		
      
        ConfigureAndLoadTexture(pixels,  FreeImage_GetWidth(dib),  FreeImage_GetHeight(dib) );           
 
     glActiveTexture (GL_TEXTURE0);                                                                                                                                            
    glBindTexture(GL_TEXTURE_2D, textureMapID); 
//===================================================================================================      
    
//===================================================================================================      
      
      
        free(pixels);
        FreeImage_Unload(dib);
	}

}
Ejemplo n.º 25
0
VALUE rb_graffik_open(VALUE self, VALUE rb_filename) {
  char *filename = STR2CSTR(rb_filename);
  FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
  
  // Try various methods to thet the image format
  fif = FreeImage_GetFileType(filename, 0);
  if (fif == FIF_UNKNOWN) {
    fif = FreeImage_GetFIFFromFilename(filename);
  }
  
  if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
    int flags = ((fif == FIF_JPEG) ? JPEG_ACCURATE : 0);
    // Load the image from disk
    FIBITMAP *image = FreeImage_Load(fif, filename, flags);
    // 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");
}
Ejemplo n.º 26
0
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;
}
Ejemplo n.º 27
0
Texture::Texture(const std::string& texname)
{
    name = texname;
    FIBITMAP* texture;
    FREE_IMAGE_FORMAT fif;

    const char* path = ("res/texture/"+texname).c_str();

    fif = FreeImage_GetFileType(path,0);
    if( FreeImage_FIFSupportsReading(fif))
    {
        texture = FreeImage_Load(fif, path);
        int imgWidth = FreeImage_GetWidth(texture);
        int imgHeight = FreeImage_GetHeight(texture);

        //std::cout << imgWidth << " "<< imgHeight <<"\n";

        glGenTextures( 1, &t );
        //std::cout << "\nACTIVE TEXTURE VALUE IS: " << GL_TEXTURE0+t << "\n";
        glActiveTexture(GL_TEXTURE0);
        glBindTexture( GL_TEXTURE_2D, t );
        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, imgWidth, imgHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(texture));
        FreeImage_Unload(texture);
        std::cout << "Image file is good\n";
        std::cout << "Loaded: " << texname << "\n";
    }
    else
    {
        std::cout << "File not supported!\n";
    }
}
Ejemplo n.º 28
0
bool CTexture::loadTexture2D(std::string sTexturePath, bool bGenerateMipMaps)
{
	FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
	FIBITMAP* dib(0);

	fif = FreeImage_GetFileType(sTexturePath.c_str(), 0); // 检查文件签名,推导其格式
	if(fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(sTexturePath.c_str()); 	// 从扩展名猜测格式
	if(fif == FIF_UNKNOWN) return false;

	//clock_t begin = clock();
	if(FreeImage_FIFSupportsReading(fif)) dib = FreeImage_Load(fif, sTexturePath.c_str());
	if(!dib) return false;
	//clock_t end = clock();
	//cout << end - begin << endl;
		
	GLubyte* bDataPointer = FreeImage_GetBits(dib);
	if(bDataPointer == NULL || FreeImage_GetWidth(dib) == 0 || FreeImage_GetHeight(dib) == 0)
		return false;

	GLenum format = FreeImage_GetBPP(dib) == 24 ? GL_BGR : FreeImage_GetBPP(dib) == 8 ? GL_LUMINANCE : 0;
	createFromData(bDataPointer, 
		FreeImage_GetWidth(dib), FreeImage_GetHeight(dib), FreeImage_GetBPP(dib), format, bGenerateMipMaps);

	FreeImage_Unload(dib);

	m_sTexturePath = sTexturePath;
	return true;
}
Ejemplo n.º 29
0
void loadTexture(const char* filename, const unsigned int textureId) {
  FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
  FIBITMAP* dib = 0;
  BYTE* bits = 0;
  unsigned int width, height = 0;
  GLuint glTextureId;
  
  fif = FreeImage_GetFileType(filename, 0);
  
  dib = FreeImage_Load(fif, filename);
  
  bits = FreeImage_GetBits(dib);
  width = FreeImage_GetWidth(dib);
  height = FreeImage_GetHeight(dib);
  
  std::clog << width << " " << height << std::endl;
  
  glGenTextures(1, &glTextureId);
  
  glBindTexture(GL_TEXTURE_2D, glTextureId);
  
  frontTexture = glTextureId;
  
  gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_BGR, GL_UNSIGNED_BYTE, bits);
}
Ejemplo n.º 30
0
bool TextureManager::LoadTexture(const char *filename, const unsigned int texID, bool generate, GLenum target,
                                 GLenum image_format, GLint internal_format, GLint level, GLint border) {
  // image format
  FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
  // pointer to the image, once loaded
  FIBITMAP *dib(0);
  // pointer to the image data
  BYTE *bits(0);
  // image width and height
  unsigned int width(0), height(0);
  // OpenGL's image ID to map to
  GLuint gl_texID;

  // check the file signature and deduce its format
  fif = FreeImage_GetFileType(filename, 0);
  // if still unknown, try to guess the file format from the file extension
  if (fif == FIF_UNKNOWN)
    fif = FreeImage_GetFIFFromFilename(filename);
  // if still unkown, return failure
  if (fif == FIF_UNKNOWN)
    return false;

  // check that the plugin has reading capabilities and load the file
  if (FreeImage_FIFSupportsReading(fif))
    dib = FreeImage_Load(fif, filename);
  // if the image failed to load, return failure
  if (!dib)
    return false;

  // retrieve the image data
  bits = FreeImage_GetBits(dib);
  // get the image width and height
  width = FreeImage_GetWidth(dib);
  height = FreeImage_GetHeight(dib);
  // if this somehow one of these failed (they shouldn't), return failure
  if ((bits == 0) || (width == 0) || (height == 0))
    return false;

  // if this texture ID is in use, unload the current texture
  if (m_texID.find(texID) != m_texID.end())
    glDeleteTextures(1, &(m_texID[texID]));

  if (generate) {
    // generate an OpenGL texture ID for this texture
    glGenTextures(1, &gl_texID);
    // store the texture ID mapping
    m_texID[texID] = gl_texID;
    // bind to the new texture ID
    glBindTexture(target, gl_texID);
  }

  // store the texture data for OpenGL use
  glTexImage2D(target, level, internal_format, width, height, border, image_format, GL_UNSIGNED_BYTE, bits);

  // Free FreeImage's copy of the data
  FreeImage_Unload(dib);

  // return success
  return true;
}