//---------- 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; } }
//---------------------------------------------------------- void ofTexture::readToPixels(ofPixels & pixels){ #ifndef TARGET_OPENGLES pixels.allocate(texData.width,texData.height,ofGetImageTypeFromGLType(texData.glTypeInternal)); bind(); glGetTexImage(texData.textureTarget,0,ofGetGlFormat(pixels),GL_UNSIGNED_BYTE, pixels.getPixels()); unbind(); #endif }
//---------------------------------------------------------- void ofTexture::readToPixels(ofPixels & pixels) const { #ifndef TARGET_OPENGLES pixels.allocate(texData.width,texData.height,ofGetImageTypeFromGLType(texData.glInternalFormat)); ofSetPixelStoreiAlignment(GL_PACK_ALIGNMENT,pixels.getWidth(),pixels.getBytesPerChannel(),pixels.getNumChannels()); glBindTexture(texData.textureTarget,texData.textureID); glGetTexImage(texData.textureTarget,0,ofGetGlFormat(pixels),GL_UNSIGNED_BYTE, pixels.getData()); glBindTexture(texData.textureTarget,0); #endif }
//---------------------------------------------------------- void ofFbo::readToPixels(ofPixels & pixels, int attachmentPoint) const{ if(!bIsAllocated) return; #ifndef TARGET_OPENGLES getTexture(attachmentPoint).readToPixels(pixels); #else pixels.allocate(settings.width,settings.height,ofGetImageTypeFromGLType(settings.internalformat)); bind(); int format = ofGetGLFormatFromInternal(settings.internalformat); glReadPixels(0,0,settings.width, settings.height, format, GL_UNSIGNED_BYTE, pixels.getData()); unbind(); #endif }
ofPixels ofxImageTS::alterColorRGB(ofPixels pixels,float R, float G, float B){ pixels.allocate(pixels.getWidth(), pixels.getHeight(), OF_PIXELS_RGB); ofPixels copy; copy = pixels; for(int i = 0; i < pixels.size()-3; i += 3){ copy[i] = R * pixels[i]; copy[i+1] = G * pixels[i+1]; copy[i+2] = B * pixels[i+2]; } return copy; }
ofPixels ofxImageTS::invertRB(ofPixels pixels){ pixels.allocate(pixels.getWidth(), pixels.getHeight(), OF_PIXELS_RGBA); ofPixels copy; copy = pixels; for(int i = 0; i < pixels.size()-3; i += 4){ copy[i] = pixels[i+2]; copy[i+1] = pixels[i+1]; copy[i+2] = pixels[i]; copy[i+3] = pixels[i+3]; } return copy; }
bool ofxTurboJpeg::load(const ofBuffer& buf, ofPixels &pix) { int w, h; int subsamp; int ok = tjDecompressHeader2(handleDecompress, (unsigned char*)buf.getData(), buf.size(), &w, &h, &subsamp); if (ok != 0) { printf("Error in tjDecompressHeader2():\n%s\n", tjGetErrorStr()); return false; } pix.allocate(w, h, 3); tjDecompress(handleDecompress, (unsigned char*)buf.getData(), buf.size(), pix.getData(), w, 0, h, 3, 0); return true; }
//--------------------------------------------------------------------- static void prepareBitmapTexture(){ if (!bBitmapTexturePrepared){ myLetterPixels.allocate(16*16, 16*16, 4); // letter size:8x14pixels, texture size:16x8letters, gl_rgba: 4bytes/1pixel myLetterPixels.set(0); bitmappedFontTexture.allocate(16*16, 16*16, GL_RGBA, false); bBitmapTexturePrepared = true; for (int i = 0; i < 256; i++) { const unsigned char * face = bmpChar_8x13_Map[i]; for (int j = 1; j < 15; j++){ for (int k = 0; k < 8; k++){ if ( ((face[15-j] << k) & (128)) > 0 ){ myLetterPixels[(((int)(i/16))*16*16*16+(i%16)*16 + (j-1)*16*16 + k)*4] = 255; myLetterPixels[(((int)(i/16))*16*16*16+(i%16)*16 + (j-1)*16*16 + k)*4+1] = 255; myLetterPixels[(((int)(i/16))*16*16*16+(i%16)*16 + (j-1)*16*16 + k)*4+2] = 255; myLetterPixels[(((int)(i/16))*16*16*16+(i%16)*16 + (j-1)*16*16 + k)*4+3] = 255; }else{ myLetterPixels[(((int)(i/16))*16*16*16+(i%16)*16 + (j-1)*16*16 + k)*4] = 0; myLetterPixels[(((int)(i/16))*16*16*16+(i%16)*16 + (j-1)*16*16 + k)*4+1] = 0; myLetterPixels[(((int)(i/16))*16*16*16+(i%16)*16 + (j-1)*16*16 + k)*4+2] = 0; myLetterPixels[(((int)(i/16))*16*16*16+(i%16)*16 + (j-1)*16*16 + k)*4+3] = 0; } } } } bitmappedFontTexture.loadData(myLetterPixels); bitmappedFontTexture.setTextureMinMagFilter(GL_LINEAR,GL_NEAREST); charMesh.setMode(OF_PRIMITIVE_TRIANGLES); } }
//---------------------------------------------------- void putBmpIntoPixels(FIBITMAP * bmp, ofPixels &pix, bool swapForLittleEndian = true){ int width = FreeImage_GetWidth(bmp); int height = FreeImage_GetHeight(bmp); int bpp = FreeImage_GetBPP(bmp); FIBITMAP * bmpTemp = NULL; switch (bpp){ case 8: if (FreeImage_GetColorType(bmp) == FIC_PALETTE) { bmpTemp = FreeImage_ConvertTo24Bits(bmp); bmp = bmpTemp; bpp = FreeImage_GetBPP(bmp); } else { // do nothing we are grayscale } break; case 24: // do nothing we are color break; case 32: // do nothing we are colorAlpha break; default: bmpTemp = FreeImage_ConvertTo24Bits(bmp); bmp = bmpTemp; bpp = FreeImage_GetBPP(bmp); break; } int bytesPerPixel = bpp / 8; pix.allocate(width, height, bpp); FreeImage_ConvertToRawBits(pix.getPixels(), bmp, width*bytesPerPixel, bpp, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, true); // get bits if (bmpTemp != NULL) FreeImage_Unload(bmpTemp); #ifdef TARGET_LITTLE_ENDIAN if(swapForLittleEndian) pix.swapRgb(); #endif }
// get pixels from a fbo or texture // untested void ftUtil::toPixels(ofTexture& _tex, ofPixels& _pixels) { ofTextureData& texData = _tex.getTextureData(); int format = texData.glInternalFormat; int readFormat, numChannels; switch(format){ case GL_R8: readFormat = GL_RED, numChannels = 1; break; // or is it GL_R case GL_RG8: readFormat = GL_RG, numChannels = 2; break; case GL_RGB8: readFormat = GL_RGB, numChannels = 3; break; case GL_RGBA8: readFormat = GL_RGBA, numChannels = 4; break; default: ofLogWarning("ftUtil") << "toPixels: " << "can only read char texturs to ofPixels"; return; } if (_pixels.getWidth() != texData.width || _pixels.getHeight() != texData.height || _pixels.getNumChannels() != numChannels) { _pixels.allocate(texData.width, texData.height, numChannels); } ofSetPixelStoreiAlignment(GL_PACK_ALIGNMENT, texData.width, 1, numChannels); glBindTexture(texData.textureTarget, texData.textureID); glGetTexImage(texData.textureTarget, 0, readFormat, GL_UNSIGNED_BYTE, _pixels.getData()); glBindTexture(texData.textureTarget, 0); }
// Set up our sketch. void setup() { ofSetWindowShape(1280, 720); // Set the window size. grabber.initGrabber(1280, 720); // Set the grabber size. pixels.allocate(1280, 720, OF_PIXELS_RGB); // Allocate memory for our pixels. }