Surface pp::choose(cinder::Surface& imageA, cinder::Surface& imageB, cinder::Surface& errorA, cinder::Surface& secondWeight, float threshold) { // for each target pixel find one in source! float width = std::min(imageA.getWidth(), imageB.getWidth()); float height = std::min(imageB.getHeight(), imageB.getHeight()); Surface result(width, height, false); ivec2 v(0, 0); for (v.y = 0; v.y < height; v.y++) for (v.x = 0; v.x < width; v.x++) { // is errorA a local maximum? bool swap = true; Color8u eA = errorA.getPixel(v); float errA = (eA.r-127)*(eA.r-127) + (eA.g-127)*(eA.g-127) + (eA.b-127)*(eA.b-127); float alternative = secondWeight.getPixel(v).r; if (std::sqrt(errA)*alternative <= threshold*(3*127*127)) swap = false; else for (int i = 0; i < 3 && swap; i++) for (int j = 0; j < 3 && swap; j++) if (i != 1 || j != 1) { ivec2 offset(i-1, j-1); Color8u eO = errorA.getPixel(v+offset); float errOther = (eO.r-127)*(eO.r-127) + (eO.g-127)*(eO.g-127) + (eO.b-127)*(eO.b-127); if (errOther >= errA) swap = false; } if (swap) result.setPixel(v, imageB.getPixel(v)); else result.setPixel(v, imageA.getPixel(v)); } return result; }
SurfaceImage::SurfaceImage( const cinder::Surface &ciSurface ) : SurfaceBase( ciSurface.getWidth(), ciSurface.getHeight() ) { if( ciSurface.getChannelOrder() == cinder::SurfaceChannelOrder::BGRA ) { mCairoSurface = cairo_image_surface_create_for_data( const_cast<unsigned char*>( ciSurface.getData() ), CAIRO_FORMAT_ARGB32, ciSurface.getWidth(), ciSurface.getHeight(), ciSurface.getRowBytes() ); } else if( ciSurface.getChannelOrder() == cinder::SurfaceChannelOrder::BGRX ) { mCairoSurface = cairo_image_surface_create_for_data( const_cast<unsigned char*>( ciSurface.getData() ), CAIRO_FORMAT_RGB24, ciSurface.getWidth(), ciSurface.getHeight(), ciSurface.getRowBytes() ); } else throw; initCinderSurface( ciSurface.hasAlpha(), cairo_image_surface_get_data( mCairoSurface ), cairo_image_surface_get_stride( mCairoSurface ) ); }
void pp::getColors(cinder::Surface& source, Palette& result) { result.clear(); ivec2 v(0, 0); int width = source.getWidth(); int height = source.getHeight(); for (v.x = 0; v.x < width; v.x++) for (v.y = 0; v.y < height; v.y++) { Color8u pxl = source.getPixel(v); bool found = false; for (Palette::iterator it = result.begin(); it != result.end(); it++) if (distance2(Color8u(it->r, it->g, it->b), pxl) == 0) // found found = true; if (!found) result.push_back(pxl); } }
SurfaceImage::SurfaceImage( cinder::Surface ciSurface ) : SurfaceBase( ciSurface.getWidth(), ciSurface.getHeight() ) { bool needsManualCopy = true; cairo_format_t format = ( ciSurface.getChannelOrder() == cinder::SurfaceChannelOrder::BGRA ) ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24; bool legalRowBytes = cairo_format_stride_for_width( format, ciSurface.getWidth() ) == ciSurface.getRowBytes(); if( legalRowBytes && ( ciSurface.getChannelOrder() == cinder::SurfaceChannelOrder::BGRA ) ) { mCairoSurface = cairo_image_surface_create_for_data( const_cast<unsigned char*>( ciSurface.getData() ), CAIRO_FORMAT_ARGB32, ciSurface.getWidth(), ciSurface.getHeight(), ciSurface.getRowBytes() ); needsManualCopy = false; } else if( legalRowBytes && ( ciSurface.getChannelOrder() == cinder::SurfaceChannelOrder::BGRX ) ) { mCairoSurface = cairo_image_surface_create_for_data( const_cast<unsigned char*>( ciSurface.getData() ), CAIRO_FORMAT_RGB24, ciSurface.getWidth(), ciSurface.getHeight(), ciSurface.getRowBytes() ); needsManualCopy = false; } else { // we can't natively represent this Surface configuration, so we'll just allocate one and manually copy it mCairoSurface = cairo_image_surface_create( ciSurface.hasAlpha() ? CAIRO_FORMAT_RGB24 : CAIRO_FORMAT_ARGB32, ciSurface.getWidth(), ciSurface.getHeight() ); } initCinderSurface( ciSurface.hasAlpha(), cairo_image_surface_get_data( mCairoSurface ), cairo_image_surface_get_stride( mCairoSurface ) ); if( needsManualCopy ) mCinderSurface.copyFrom( ciSurface, ciSurface.getBounds(), Vec2i::zero() ); }