loadImage()

Image / Loading & Displaying

Description

Loads an image into a variable of type PImage. Supported formats: PNG, JPG, GIF, TGA. Images are loaded from the sketch's data folder.

Syntax

PImage* loadImage(std::string path)

Parameters

NameTypeDescription
pathstd::stringpath to the image file

Returns

PImage*

Related

Under the Hood

From Processing.h:

PImage* loadImage(const std::string& path);

Under the Hood

From Processing.cpp:

PImage* PApplet::loadImage(const std::string& path){ // Handle URLs: download with curl/wget and validate image magic bytes if (path.size()>7 && (path.substr(0,7)=="http://" || path.substr(0,8)=="https://")){ #ifdef _WIN32 std::string tmp=std::string(getenv("TEMP")?getenv("TEMP"):"C:\\Temp")+"\\pg_img_"; #else std::string tmp="/tmp/pg_img_"; #endif size_t sl=path.rfind('/'); std::string bn=(sl!=std::string::npos)?path.substr(sl+1):"img.png"; { size_t q=bn.find('?'); if(q!=std::string::npos) bn=bn.substr(0,q); } if(bn.empty()||bn.find('.')==std::string::npos) bn="img.png"; for(char& c:bn) if(c==':'||c=='*'||c=='<'||c=='>'||c=='|') c='_'; tmp+=bn; auto isValidImg=[&]()->bool{ FILE* f2=fopen(tmp.c_str(),"rb"); if(!f2) return false; fseek(f2,0,SEEK_END); long sz=ftell(f2); fseek(f2,0,SEEK_SET); unsigned char h[4]={}; fread(h,1,4,f2); fclose(f2); if(sz<100) return false; return (h[0]==0x89&&h[1]=='P')|| // PNG (h[0]==0xFF&&h[1]==0xD8)|| // JPEG (h[0]=='G'&&h[1]=='I')|| // GIF (h[0]=='B'&&h[1]=='M')|| // BMP (h[0]=='R'&&h[1]=='I'); // WEBP }; // Try curl #ifdef _WIN32 system(("curl -sL --max-time 15 -o \""+tmp+"\" \""+path+"\"").c_str()); #else system(("curl -sL --max-time 15 -o '"+tmp+"' '"+path+"'").c_str()); #endif if(!isValidImg()){ // Try wget as fallback #ifdef _WIN32 system(("wget -q -O \""+tmp+"\" \""+path+"\"").c_str()); #else system(("wget -q -O '"+tmp+"' '"+path+"'").c_str()); #endif } if(isValidImg()){ PImage* r2=loadImage(tmp); if(r2&&r2->width>0) return r2; } fprintf(stderr,"[loadImage] URL download failed: %s\n",path.c_str()); return nullptr; } // Search paths: current dir, data/, files/, and sketch subdirs // Check PROCESSING_SKETCH_PATH env var set by IDE std::string _sketchDir; if (const char* _sp = std::getenv("PROCESSING_SKETCH_PATH")) _sketchDir = std::string(_sp) + "/"; // Also get the directory of the running executable std::string _exeDir; { char _buf[4096] = {}; #ifdef _WIN32 GetModuleFileNameA(nullptr, _buf, sizeof(_buf)); std::string _ep(_buf); size_t _sl = _ep.find_last_of("\\\\"); #else ssize_t _len = readlink("/proc/self/exe", _buf, sizeof(_buf)-1); if (_len > 0) _buf[_len] = 0; std::string _ep(_buf); size_t _sl = _ep.find_last_of("/"); #endif if (_sl != std::string::npos) _exeDir = _ep.substr(0, _sl+1); } std::vector<std::string> tries = { path, "data/" + path, "files/" + path, "../data/" + path, _sketchDir + path, _sketchDir + "data/" + path, _exeDir + path, _exeDir + "data/" + path, // in case running from a subdirectory }; #ifdef PROCESSING_HAS_STB_IMAGE for (auto& p : tries) { int w, h, ch; unsigned char* data = stbi_load(p.c_str(), &w, &h, &ch, 4); if (!data) continue; PImage* img = new PImage(w, h); for (int i = 0; i < w*h; i++) { unsigned char r=data[i*4+0], g=data[i*4+1], b=data[i*4+2], a=data[i*4+3]; img->pixels[i] = ((unsigned int)a<<24)|((unsigned int)r<<16)|((unsigned int)g<<8)|b; } stbi_image_free(data); img->dirty = true; return img; } #endif for (auto& p : tries) std::cerr << "[loadImage] not found: " << p << "\n"; std::cerr << "[loadImage] returning empty image -- check the path above\n"; return new PImage(); // return empty (not null) so -> calls are safe }