//---------------------------------------------------------- void ofTexture::allocate(const ofPixels& pix, bool bUseARBExtention){ allocate(pix.getWidth(), pix.getHeight(), ofGetGlInternalFormat(pix), bUseARBExtention, ofGetGlFormat(pix), ofGetGlType(pix)); if((pix.getPixelFormat()==OF_PIXELS_GRAY || pix.getPixelFormat()==OF_PIXELS_GRAY_ALPHA) && ofIsGLProgrammableRenderer()){ setRGToRGBASwizzles(true); } loadData(pix); }
//---------- bool Message::getData(ofPixels & data) const { auto & header = this->getHeader<Header::Pixels>(); if (this->hasHeader<Header::Pixels>()) { const auto & header = this->getHeader<Header::Pixels>(); auto bodySize = this->getBodySize(); ofPixelFormat pixelFormat = (ofPixelFormat)header.pixelFormat; //reallocate if we need to if (data.getWidth() != header.width || data.getHeight() != header.height || data.getPixelFormat() != pixelFormat) { data.allocate(header.width, header.height, pixelFormat); } if (data.size() != bodySize) { OFXSQUASHBUDDIES_ERROR << "Message body is of wrong size to fill pixels. Maybe a bug in sender?"; return false; } else { memcpy(data.getData(), this->getBodyData(), bodySize); return true; } } else { OFXSQUASHBUDDIES_WARNING << "Message Header doesn't match Pixels type"; return false; } }
//--------------------------------- int ofGetGlType(const ofPixels & pixels) { #ifndef TARGET_OPENGLES if(pixels.getPixelFormat()==OF_PIXELS_RGB565){ return GL_UNSIGNED_SHORT_5_6_5; }else{ #endif return GL_UNSIGNED_BYTE; #ifndef TARGET_OPENGLES } #endif }
//---------- void Message::setData(const ofPixels & data) { const auto headerSize = sizeof(Header::Pixels); const auto bodySize = data.size(); // inner payload this->headerAndData.resize(headerSize + bodySize); auto & header = this->getHeader<Header::Pixels>(true); header.width = data.getWidth(); header.height = data.getHeight(); header.pixelFormat = data.getPixelFormat(); auto body = this->getBodyData(); memcpy(body, data.getData(), bodySize); }
//-------------------------------------------- void ofCairoRenderer::draw(const ofPixels & raw, float x, float y, float z, float w, float h, float sx, float sy, float sw, float sh) const{ bool shouldCrop = sx != 0 || sy != 0 || sw != w || sh != h; ofPixels cropped; if(shouldCrop) { cropped.allocate(sw, sh, raw.getPixelFormat()); raw.cropTo(cropped, sx, sy, sw, sh); } const ofPixels & pix = shouldCrop ? cropped : raw; ofCairoRenderer * mut_this = const_cast<ofCairoRenderer*>(this); mut_this->pushMatrix(); mut_this->translate(x,y,z); mut_this->scale(w/pix.getWidth(),h/pix.getHeight()); cairo_surface_t *image; int stride=0; int picsize = pix.getWidth()* pix.getHeight(); const unsigned char *imgPix = pix.getData(); vector<unsigned char> swapPixels; switch(pix.getImageType()){ case OF_IMAGE_COLOR: #ifdef TARGET_LITTLE_ENDIAN swapPixels.resize(picsize * 4); for(int p= 0; p<picsize; p++) { swapPixels[p*4] = imgPix[p*3 +2]; swapPixels[p*4 +1] = imgPix[p*3 +1]; swapPixels[p*4 +2] = imgPix[p*3]; } #else swapPixels.resize(picsize * 4); for(int p= 0; p<picsize; p++) { swapPixels[p*4] = imgPix[p*3]; swapPixels[p*4 +1] = imgPix[p*3 +1]; swapPixels[p*4 +2] = imgPix[p*3 +2]; } #endif stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, pix.getWidth()); image = cairo_image_surface_create_for_data(&swapPixels[0], CAIRO_FORMAT_RGB24, pix.getWidth(), pix.getHeight(), stride); break; case OF_IMAGE_COLOR_ALPHA: #ifdef TARGET_LITTLE_ENDIAN swapPixels.resize(picsize * 4); for(int p= 0; p<picsize; p++) { swapPixels[p*4] = imgPix[p*4+2]; swapPixels[p*4 +1] = imgPix[p*4+1]; swapPixels[p*4 +2] = imgPix[p*4]; swapPixels[p*4 +3] = imgPix[p*4+3]; } stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, pix.getWidth()); image = cairo_image_surface_create_for_data(&swapPixels[0], CAIRO_FORMAT_ARGB32, pix.getWidth(), pix.getHeight(), stride); #else stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, pix.getWidth()); image = cairo_image_surface_create_for_data(pix.getData(), CAIRO_FORMAT_ARGB32, pix.getWidth(), pix.getHeight(), stride); #endif break; case OF_IMAGE_GRAYSCALE: swapPixels.resize(picsize * 4); for(int p= 0; p<picsize; p++) { swapPixels[p*4] = imgPix[p]; swapPixels[p*4 +1] = imgPix[p]; swapPixels[p*4 +2] = imgPix[p]; } stride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, pix.getWidth()); image = cairo_image_surface_create_for_data(&swapPixels[0], CAIRO_FORMAT_RGB24, pix.getWidth(), pix.getHeight(), stride); break; case OF_IMAGE_UNDEFINED: default: ofLogError("ofCairoRenderer") << "draw(): trying to draw undefined image type " << pix.getImageType(); mut_this->popMatrix(); return; break; } cairo_set_source_surface (cr, image, 0,0); cairo_paint (cr); cairo_surface_flush(image); cairo_surface_destroy (image); mut_this->popMatrix(); }
//--------------------------------- int ofGetGlInternalFormat(const ofPixels& pix) { return ofGetGLInternalFormatFromPixelFormat(pix.getPixelFormat()); }