bool ofxThreadedImage::loadHttpImageBlocking(string url_){ alpha = 0; whatToDo = LOAD_HTTP; url = url_; readyToDraw = false; problemLoading = false; setUseTexture(false); ofxSimpleHttp http; http.setTimeOut(timeOut); ofxSimpleHttpResponse response = http.fetchURLBlocking(url); if (response.ok){ ofDirectory dir; dir.open(ofToDataPath(IMG_DOWNLOAD_FOLDER_NAME, false)); if ( !dir.exists()){ dir.create(); } string filePath = ofToDataPath( (string)IMG_DOWNLOAD_FOLDER_NAME + "/" + response.fileName, false ); FILE * file = fopen( filePath.c_str(), "wb"); fwrite (response.responseBody.c_str() , 1 , response.responseBody.length() , file ); fclose( file); }else{ ofLog(OF_LOG_ERROR, "loadHttpImageBlocking() failed (%d) > %s\n", response.status, url.c_str() ); return false; } imageLoaded = false; bool ok = loadImage((string)IMG_DOWNLOAD_FOLDER_NAME + "/" + response.fileName); if(ok){ resizeIfNeeded(); imageLoaded = true; } pendingTexture = true; return ok; }
void ofxThreadedImage::updateTextureIfNeeded(){ if (pendingTexture){ setUseTexture(true); tex.allocate(getPixelsRef()); update(); pendingTexture = false; } }
//---------- void Device::setUseTextures(bool useTexture) { auto sources = this->getSources(); for (auto source : sources) { auto imageSource = dynamic_pointer_cast<ofBaseHasTexture>(source); if (imageSource) { imageSource->setUseTexture(useTexture); } } }
void ofxThreadedImage::loadHttpImageThreaded(string url_){ alpha = 0; whatToDo = LOAD_HTTP; url = url_; pendingTexture = true; imageLoaded = false; setUseTexture(false); startThread(true, false); }
void ofxThreadedImage::loadImageBlocking(string fileName){ imageLoaded = false; whatToDo = LOAD; setUseTexture(false); alpha = 0.0; bool loaded = loadImage(fileName); if (loaded){ pendingTexture = true; imageLoaded = true; } }
void ofxThreadedImage::threadedFunction(){ #ifdef TARGET_OSX pthread_setname_np("ofxThreadedImage"); #endif if( lock() ){ switch (whatToDo) { case SAVE: ofSaveImage(getPixelsRef(), fileName, quality); break; case LOAD:{ alpha = 0.0f; //float t1 = ofGetElapsedTimef(); loadImageBlocking(fileName); //ofLog() << "time to load: " << ofGetElapsedTimef() - t1; }break; case LOAD_HTTP: alpha = 0; ofxSimpleHttp http; http.setTimeOut(timeOut); ofxSimpleHttpResponse response = http.fetchURLtoDiskBlocking(url, IMG_DOWNLOAD_FOLDER_NAME); if (response.ok){ setUseTexture(false); bool loaded = loadImage(response.absolutePath); if (loaded){ resizeIfNeeded(); imageLoaded = true; }else{ ofLog(OF_LOG_ERROR, "loadHttpImageBlocking() failed to load from disk (%d) > %s\n", response.status, url.c_str() ); } }else{ ofLog(OF_LOG_ERROR, "loadHttpImageBlocking() failed to download (%d) > %s\n", response.status, url.c_str() ); } break; } unlock(); } else { ofLogError("ofxThreadedImage::threadedFunction Can't %s %s, thread is already running", whatToDo == SAVE ? "Save" : "Load", fileName.c_str() ); } stopThread(); #if defined(TARGET_OSX) || defined(TARGET_LINUX) /*I'm not 100% sure of linux*/ pthread_detach( pthread_self() ); //this is a workaround for this issue https://github.com/openframeworks/openFrameworks/issues/2506 #endif }
void ofxThreadedImage::updateTextureIfNeeded(){ if (pendingTexture){ if (!problemLoading){ setUseTexture(true); tex.setCompression(compression); tex.allocate(getPixelsRef()); //tex.setTextureMinMagFilter(GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR); ofImage::update(); readyToDraw = true; pendingTexture = false; } pendingNotification = true; //texture is loaded, notify owner! } }
void ofxThreadedImage::threadedFunction(){ if( lock() ){ switch (whatToDo) { case SAVE: ofSaveImage(getPixelsRef(), fileName, quality); break; case LOAD: alpha = 0.0f; loadImageBlocking(fileName); break; case LOAD_HTTP: alpha = 0; ofxSimpleHttp http; http.setTimeOut(timeOut); ofxSimpleHttpResponse response = http.fetchURLBlocking(url); if (response.status == 200){ ofDirectory dir; dir.open(ofToDataPath(IMG_DOWNLOAD_FOLDER_NAME, false)); if ( !dir.exists() ){ dir.create(); } string filePath = ofToDataPath( (string)IMG_DOWNLOAD_FOLDER_NAME + "/" + response.fileName, false ); FILE * file = fopen( filePath.c_str(), "wb"); fwrite (response.responseBody.c_str() , 1 , response.responseBody.length() , file ); fclose( file); setUseTexture(false); bool loaded = loadImage((string)IMG_DOWNLOAD_FOLDER_NAME + "/" + response.fileName); if (loaded){ imageLoaded = true; }else{ ofLog(OF_LOG_ERROR, "loadHttpImageBlocking() failed to load from disk (%d) > %s\n", response.status, url.c_str() ); } }else{ ofLog(OF_LOG_ERROR, "loadHttpImageBlocking() failed to download (%d) > %s\n", response.status, url.c_str() ); } break; } unlock(); } else { printf("ofxThreadedImage::threadedFunction Can't %s %s, thread is already running", whatToDo == SAVE ? "Save" : "Load", fileName.c_str() ); } stopThread(); }
void ofxThreadedImage::loadImageBlocking(string fileName){ imageLoaded = false; whatToDo = LOAD; problemLoading = false; setUseTexture(false); alpha = 0.0; bool loaded = loadImage(fileName); if (!loaded){ ofLogError() << "ofxThreadedImage:: img couldnt load!" << endl; problemLoading = true; }else{ resizeIfNeeded(); imageLoaded = true; } pendingTexture = true; }
//=========================================================================== bool cBackground::loadFromFile(string a_filename) { // create image if not yet allocated cImage* image = new cImage(); // load file bool success = image->loadFromFile(a_filename); if (!success) { return (false); } // delete current texture if (m_texture != NULL) { delete (m_texture); } // retrieve dimension and decide if a 1D or 2D should be created unsigned int w = image->getWidth(); unsigned int h = image->getHeight(); // create 1D or 2D texture depending of image size if ((w == 1) || (h == 1)) { m_texture = new cTexture1d(); } else { m_texture = new cTexture2d(); } // copy image data m_texture->m_image->allocate(image->getWidth(), image->getHeight(), image->getFormat()); image->copyTo(m_texture->m_image); // enable texture setUseTexture(true); // enable vertex colors setUseVertexColors(false); setUseMaterial(false); // discard image delete image; // success return (true); }
//------------------------------------ void ofxRemoteCameraClient::initGrabber(int w,int h, int imageType_, bool useTexture_){ newData=false; connected=false; changeRequested=false; frameNew=false; verbose=false; fps=0; intSize=0; client=NULL; camWidth=requestedWidth=tmpW=w; camHeight=requestedHeight=tmpH=h; imageType=requestedImageType=tmpImageType=imageType_; compressionQuality=tmpCompressionQuality=NO_COMPRESSION; pixels=(unsigned char*)malloc(getPixelSize(imageType)*camWidth*camHeight*sizeof(char)); auxPixels=(unsigned char*)malloc(getPixelSize(imageType)*camWidth*camHeight*sizeof(char)); compressedData=(unsigned char*)malloc(getPixelSize(imageType)*camWidth*camHeight*sizeof(char)); handle=tjInitDecompress(); setUseTexture(useTexture_); }
//=========================================================================== bool cBackground::loadFromImage(cImage *a_image) { // delete current texture if (m_texture != NULL) { delete (m_texture); } // retrieve dimension and decide if a 1D or 2D should be created unsigned int w = a_image->getWidth(); unsigned int h = a_image->getHeight(); // create 1D or 2D texture depending of image size if ((w == 1) || (h == 1)) { m_texture = new cTexture1d(); } else { m_texture = new cTexture2d(); } // copy image data m_texture->m_image->allocate(a_image->getWidth(), a_image->getHeight(), a_image->getFormat()); a_image->copyTo(m_texture->m_image); // enable texture setUseTexture(true); // enable vertex colors setUseVertexColors(false); setUseMaterial(false); // success return (true); }
FileGrabber::~FileGrabber() { setUseTexture(false); }
VideoGrabber::VideoGrabber(){ setUseTexture(false); }
//-------------------------------------------------------------- bool ofxCLEye::initGrabber(int w, int h, int deviceID, int frameRate, bool useTexture, bool useGrayscale, bool useThread){ setDeviceID(deviceID); setDesiredFrameRate(frameRate); setUseThread(useThread); setUseGrayscale(useGrayscale); setUseTexture(useTexture); if(w == 640 && h == 480){ resolution = CLEYE_VGA; } else if(w == 320 && h == 240){ resolution = CLEYE_QVGA; } else{ ofLogWarning(OFX_CLEYE_MODULE_NAME) << "initGrabber(): selected resolution " + ofToString(w) + "x" + ofToString(h) + " is not available with ofxCLEye"; ofLogWarning(OFX_CLEYE_MODULE_NAME) << "initGrabber(): using 640x480 instead"; resolution = CLEYE_VGA; } if(desiredFrameRate < 0){ ofLogWarning(OFX_CLEYE_MODULE_NAME) << "initGrabber(): selected framerate" + ofToString(desiredFrameRate) + "is not available with ofxCLeye"; ofLogWarning(OFX_CLEYE_MODULE_NAME) << "initGrabber(): using 60fps instead"; desiredFrameRate = 60; } GUID guid = getDeviceGUID(requestedDeviceID); cam = CLEyeCreateCamera(guid, colorMode, resolution, desiredFrameRate); if(cam == NULL){ ofLogError(OFX_CLEYE_MODULE_NAME) << "initGrabber(): error when creating instance of CLEyeCamera."; return false; } initialized = CLEyeCameraStart(cam); if(!initialized){ ofLogError(OFX_CLEYE_MODULE_NAME) << "initGrabber(): can't start the CLEye camera."; return false; } CLEyeCameraGetFrameDimensions(cam, width, height); // oF code style says to not use ternary operators, but sometimes they are really convenient. // Native color image from camera is RGBA (4 channels) viPixels = new unsigned char[width * height * ((colorMode == CLEYE_MONO_PROCESSED) ? 1 : 4)]; pixels.allocate(width, height, (colorMode == CLEYE_MONO_PROCESSED) ? 1 : 3); pixels.set(0); if(usingTexture){ int glFormat = (colorMode == CLEYE_MONO_PROCESSED) ? GL_LUMINANCE : GL_RGB; texture.allocate(width, height, glFormat); texture.loadData((unsigned char *)pixels.getPixels(), width, height, glFormat); } if(usingThread){ startThread(true); ofLogVerbose(OFX_CLEYE_MODULE_NAME) << "initGrabber(): thread started."; } return true; }