Example #1
0
void ofImage_<PixelType>::changeTypeOfPixels(ofPixels_<PixelType> &pix, ofImageType newType){
	int oldType = pix.getImageType();
		
	if (oldType == newType) {
		return; // no need to reallocate
	}

	FIBITMAP * bmp = getBmpFromPixels(pix);
	FIBITMAP * convertedBmp = NULL;

	switch (newType){
		case OF_IMAGE_GRAYSCALE:
			convertedBmp = FreeImage_ConvertToGreyscale(bmp);
			break;
		case OF_IMAGE_COLOR:
			convertedBmp = FreeImage_ConvertTo24Bits(bmp);
			break;
		case OF_IMAGE_COLOR_ALPHA:
			convertedBmp = FreeImage_ConvertTo32Bits(bmp);
			break;
		default:
			ofLog(OF_LOG_ERROR, "changeTypeOfPixels: format not supported");
			break;
	}
	
	putBmpIntoPixels(convertedBmp, pix, false);

	if (bmp != NULL) {
		FreeImage_Unload(bmp);
	}
	if (convertedBmp != NULL) {
		FreeImage_Unload(convertedBmp);
	}

	if(bUseTexture){
		// always reallocate the texture. if ofTexture doesn't need reallocation,
		// it doesn't have to. but it needs to change the internal format.
		tex.allocate(pixels.getWidth(), pixels.getHeight(), ofGetGlInternalFormat(pixels));
	}
}
Example #2
0
void ofImage_<PixelType>::allocate(int w, int h, ofImageType newType){
	
	if (width == w && height == h && newType == type){
		return;
	}
#if defined(TARGET_ANDROID) || defined(TARGET_OF_IOS)
	registerImage(this);
#endif
	pixels.allocate(w, h, newType);

	// take care of texture allocation --
	if (pixels.isAllocated() && bUseTexture){
		tex.allocate(pixels.getWidth(), pixels.getHeight(), ofGetGlInternalFormat(pixels));
		if(ofGetGLProgrammableRenderer() && (pixels.getNumChannels()==1 || pixels.getNumChannels()==2)){
			tex.setRGToRGBASwizzles(true);
		}
	}
	
	width	= pixels.getWidth();
	height	= pixels.getHeight();
	bpp		= pixels.getBitsPerPixel();
	type	= pixels.getImageType();
}
Example #3
0
void ofImage_<PixelType>::allocate(int w, int h, ofImageType newType){
	
	if (width == w && height == h && newType == type){
		return;
	}
#if defined(TARGET_ANDROID)
	ofAddListener(ofxAndroidEvents().unloadGL,this,&ofImage_<PixelType>::unloadTexture);
	ofAddListener(ofxAndroidEvents().reloadGL,this,&ofImage_<PixelType>::update);
#endif
	pixels.allocate(w, h, newType);

	// take care of texture allocation --
	if (pixels.isAllocated() && bUseTexture){
		tex.allocate(pixels.getWidth(), pixels.getHeight(), ofGetGlInternalFormat(pixels));
		if(ofIsGLProgrammableRenderer() && (pixels.getPixelFormat()==OF_PIXELS_GRAY || pixels.getPixelFormat()==OF_PIXELS_GRAY_ALPHA)){
			tex.setRGToRGBASwizzles(true);
		}
	}
	
	width	= pixels.getWidth();
	height	= pixels.getHeight();
	bpp		= pixels.getBitsPerPixel();
	type	= pixels.getImageType();
}
void ofImage_<PixelType>::grabScreen(int _x, int _y, int _w, int _h){

	allocate(_w, _h, OF_IMAGE_COLOR);

    int sw = ofGetViewportWidth();
    int sh = ofGetViewportHeight();     // if we are in a FBO or other viewport, this fails: ofGetHeight();
    
	if (!((width == _w) && (height == _h))){
		resize(_w, _h);
	}

	#ifndef TARGET_OPENGLES
    
    _y = sh - _y;
    _y -= _h; // top, bottom issues
    
    glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );											// be nice to anyone else who might use pixelStore
    glPixelStorei(GL_PACK_ALIGNMENT, 1);
    glReadPixels(_x, _y, _w, _h, ofGetGlInternalFormat(pixels), GL_UNSIGNED_BYTE, pixels.getPixels()); // read the memory....
    glPopClientAttrib();
    
	int sizeOfOneLineOfPixels = pixels.getWidth() * pixels.getBytesPerPixel();
	PixelType * tempLineOfPix = new PixelType[sizeOfOneLineOfPixels];
	PixelType * linea;
	PixelType * lineb;
	for (int i = 0; i < pixels.getHeight()/2; i++){
		linea = pixels.getPixels() + i * sizeOfOneLineOfPixels;
		lineb = pixels.getPixels() + (pixels.getHeight()-i-1) * sizeOfOneLineOfPixels;
		memcpy(tempLineOfPix, linea, sizeOfOneLineOfPixels);
		memcpy(linea, lineb, sizeOfOneLineOfPixels);
		memcpy(lineb, tempLineOfPix, sizeOfOneLineOfPixels);
	}
	delete [] tempLineOfPix;
	
    #else
    
    int numPixels   = width*height;
    if( numPixels == 0 ){
        ofLog(OF_LOG_ERROR, "grabScreen width or height is 0 - returning");
        return;
    }
    
    int numRGBA         = numPixels*4;
    GLubyte *bufferRGBA = (GLubyte *) malloc(numRGBA);

    if(ofGetOrientation() == OF_ORIENTATION_DEFAULT) {
        
        _y = sh - _y;   // screen is flipped vertically.
        _y -= _h;
        
        glPixelStorei(GL_PACK_ALIGNMENT, 1);
        glReadPixels(_x, _y, _w, _h, GL_RGBA, GL_UNSIGNED_BYTE, bufferRGBA);
        
        for(int y = 0; y < _h; y++){  
            for(int x = 0; x < _w; x++){
                
                int i = y * _w * 3 + x * 3;
                int j = (_h-1-y) * _w * 4 + x * 4;  // rotate 90.
                
                pixels.getPixels()[i]   = bufferRGBA[j];
                pixels.getPixels()[i+1] = bufferRGBA[j+1];
                pixels.getPixels()[i+2] = bufferRGBA[j+2];
            }
        }
    }
    else if(ofGetOrientation() == OF_ORIENTATION_180) {
        
        _x = sw - _x;   // screen is flipped horizontally.
        _x -= _w;
        
        glPixelStorei(GL_PACK_ALIGNMENT, 1);
        glReadPixels(_x, _y, _w, _h, GL_RGBA, GL_UNSIGNED_BYTE, bufferRGBA);
        
        for(int y = 0; y < _h; y++){  
            for(int x = 0; x < _w; x++){
                
                int i = y * _w * 3 + x * 3;
                int j = y * _w * 4 + (_w-1-x) * 4;  // rotate 90.
                
                pixels.getPixels()[i]   = bufferRGBA[j];
                pixels.getPixels()[i+1] = bufferRGBA[j+1];
                pixels.getPixels()[i+2] = bufferRGBA[j+2];
            }
        }
    }
    else if(ofGetOrientation() == OF_ORIENTATION_90_RIGHT) {
        
        int tempW = _w;     // swap width and height.
        _w = _h;
        _h = tempW;
        
        int tempY = _y;     // swap x and y.
        _y = _x;
        _x = tempY;
        
        glPixelStorei(GL_PACK_ALIGNMENT, 1);
        glReadPixels(_x, _y, _w, _h, GL_RGBA, GL_UNSIGNED_BYTE, bufferRGBA);
        
        for(int y = 0; y < _h; y++){  
            for(int x = 0; x < _w; x++){
                
                int i = x * _h * 3 + y * 3;
                int j = y * _w * 4 + x * 4;
                
                pixels.getPixels()[i]   = bufferRGBA[j];
                pixels.getPixels()[i+1] = bufferRGBA[j+1];
                pixels.getPixels()[i+2] = bufferRGBA[j+2];
            }
        }
    }
    else if(ofGetOrientation() == OF_ORIENTATION_90_LEFT) {
        
        int tempW = _w; // swap width and height.
        _w = _h;
        _h = tempW;
        
        int tempY = _y; // swap x and y.
        _y = _x;
        _x = tempY;
        
        _x = sw - _x;   // screen is flipped horizontally.
        _x -= _w;
        
        _y = sh - _y;   // screen is flipped vertically.
        _y -= _h;
        
        glPixelStorei(GL_PACK_ALIGNMENT, 1);
        glReadPixels(_x, _y, _w, _h, GL_RGBA, GL_UNSIGNED_BYTE, bufferRGBA);
        
        for(int y = 0; y < _h; y++){  
            for(int x = 0; x < _w; x++){
                
                int i = x * _h * 3 + y * 3;
                int j = (_h-1-y) * _w * 4 + (_w-1-x) * 4;
                
                pixels.getPixels()[i]   = bufferRGBA[j];
                pixels.getPixels()[i+1] = bufferRGBA[j+1];
                pixels.getPixels()[i+2] = bufferRGBA[j+2];
            }
        }
    }
    
    free(bufferRGBA);    
    
    #endif

	update();
}
void ofImage_<PixelType>::reloadTexture(){
	if (pixels.isAllocated() && bUseTexture){
		tex.allocate(pixels.getWidth(), pixels.getHeight(), ofGetGlInternalFormat(pixels));
		tex.loadData(pixels);
	}
}
Example #6
0
//----------------------------------------------------------
void ofTexture::allocate(const ofFloatPixels& pix, bool bUseARBExtention){
	allocate(pix.getWidth(), pix.getHeight(), ofGetGlInternalFormat(pix), bUseARBExtention, ofGetGlFormat(pix), ofGetGlType(pix));
}
Example #7
0
//----------------------------------------------------------
void ofTexture::allocate(const ofFloatPixels& pix){
	allocate(pix.getWidth(), pix.getHeight(), ofGetGlInternalFormat(pix), ofGetUsingArbTex(), ofGetGlFormat(pix), ofGetGlType(pix));
}
Example #8
0
int ofGetGlFormat(const T& pix) {
	int glFormat, glType;
	ofGetGlFormatAndType(ofGetGlInternalFormat(pix), glFormat, glType);
	return glFormat;
}
Example #9
0
void ofxClouds::setup() {
    
    // we need GL_TEXTURE_2D for our models coords.
    ofDisableArbTex();
    
    ofPixels pixels,comp;
        
    comp.allocate(TEXTURE_SIZE, TEXTURE_SIZE, OF_IMAGE_GRAYSCALE);
    pixels.allocate(TEXTURE_SIZE, TEXTURE_SIZE, OF_IMAGE_COLOR_ALPHA);
    
    for(int i=0; i<TEXTURE_MEMORY_SIZE*4; i++ ) {
        pixels[i] = 0;
    }
    
    for (int j=0; j<4; j++) {
        for(int i=0; i<TEXTURE_MEMORY_SIZE; i++ ) {
           comp[i] = rand()&255;
        }
        
        interpolate(comp.getPixels(), 1 << (j*2+1));
        
        for(int i=0; i<TEXTURE_MEMORY_SIZE; i++ ) {
            pixels[i*4+j] = comp[i];
        }
        
    }
    
    
    
    fbm.allocate(pixels.getWidth(), pixels.getHeight(), ofGetGlInternalFormat(pixels));
    fbm.loadData(pixels);
    
    glBindTexture(GL_TEXTURE_2D, fbm.getTextureData().textureID);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    
    ofEnableAlphaBlending();
    
	shader.load("shaders/clouds.vert", "shaders/clouds.frag");
	

    shader.begin();
    
    shader.setUniform2f("resolution", fbm.getWidth(),fbm.getHeight());
    shader.setUniformTexture("tex0", fbm, 0); 
    
    shader.setUniform2f("d0", 0.0f,2.5f);
    shader.setUniform2f("d1", 3.0f,0.0f);
    shader.setUniform2f("d2", -3.5f,0.0f);
    shader.setUniform2f("d3", 0.0f,4.0f);
    
    float a=0.2;
    float b=0.8;
    
    shader.setUniform1f("c", -a/(b-a));
    shader.setUniform1f("d", (1-a)/(b-a));
    
    shader.setUniform3f("color0", 0.7f,0.7f,0.7f);
    shader.setUniform3f("color1", 1.0f,1.0f,1.0f);
    
    shader.end();
    
    
	
    
}
Example #10
0
	ofFbo & VideoFrame::getFboRef(){
		if(!data->fbo.isAllocated()){
			data->fbo.allocate(data->pixels.getWidth(),data->pixels.getHeight(),ofGetGlInternalFormat(data->pixels));
		}
		return data->fbo;
	}
Example #11
0
//----------------------------------------------------------
void ofTexture::loadData(ofPixels & pix){
	loadData(pix.getPixels(), pix.getWidth(), pix.getHeight(), ofGetGlInternalFormat(pix));
}
cmsUInt32Number ofxLittleCMS::getTypeFromPixels( ofPixels & pix ){
    switch (ofGetGlInternalFormat(pix)) {
            // ofPixels
#ifndef TARGET_OPENGLES
            case GL_RGB8:
            return TYPE_RGB_8;
            break;
            
            case GL_RGBA8:
            return TYPE_RGBA_8;
            break;
            
            case GL_RG8:
            case GL_LUMINANCE_ALPHA:
            return TYPE_GRAYA_8;
            break;
            
            case GL_R8:
            case GL_LUMINANCE8:
            return TYPE_GRAY_8;
            break;
            
#else
            case GL_RGB:
            return TYPE_RGB_8;
            break;
            
            case GL_RGBA:
            return TYPE_RGBA_8;
            break;
            
            case GL_LUMINANCE_ALPHA:
            return TYPE_GRAYA_8;
            break;
            
            case GL_LUMINANCE:
            return TYPE_GRAY_8;
            break;
#endif
            
            // ofShortPixels
#ifndef TARGET_OPENGLES
            case GL_RGB16:
            return TYPE_RGB_16;
            break;
            
            case GL_RGBA16:
            return TYPE_RGBA_16;
            break;
            
            case GL_RG16:
            case GL_LUMINANCE16_ALPHA16:
            return TYPE_GRAYA_16;
            break;
            
            case GL_R16:
            case GL_LUMINANCE16:
            return TYPE_GRAY_16;
            break;
            
        // ofFloatPixels
            case GL_RGB32F:
            return TYPE_RGB_FLT;
            break;
            
            case GL_RGBA32F:
            return TYPE_RGBA_FLT;
            break;
            
            case GL_RG32F:
            case GL_LUMINANCE_ALPHA32F_ARB:
            ofLogWarning("ofxLittleCMS", "No Gray+A float :(");
            return TYPE_GRAY_FLT;
            break;
            
            case GL_R32F:
            case GL_LUMINANCE32F_ARB:
            return TYPE_GRAY_FLT;
            break;
#endif
            
        default:
            return TYPE_RGB_8;
            break;
    }
}