+namespace NPatchBuffer
+{
+
+void SetLoadedNPatchData( NPatchLoader::Data* data, Devel::PixelBuffer& pixelBuffer )
+{
+ if( data->border == Rect< int >( 0, 0, 0, 0 ) )
+ {
+ NPatchUtility::ParseBorders( pixelBuffer, data->stretchPixelsX, data->stretchPixelsY );
+
+ // Crop the image
+ pixelBuffer.Crop( 1, 1, pixelBuffer.GetWidth() - 2, pixelBuffer.GetHeight() - 2 );
+ }
+ else
+ {
+ data->stretchPixelsX.PushBack( Uint16Pair( data->border.left, ( (pixelBuffer.GetWidth() >= static_cast< unsigned int >( data->border.right )) ? pixelBuffer.GetWidth() - data->border.right : 0 ) ) );
+ data->stretchPixelsY.PushBack( Uint16Pair( data->border.top, ( (pixelBuffer.GetHeight() >= static_cast< unsigned int >( data->border.bottom )) ? pixelBuffer.GetHeight() - data->border.bottom : 0 ) ) );
+ }
+
+ data->croppedWidth = pixelBuffer.GetWidth();
+ data->croppedHeight = pixelBuffer.GetHeight();
+
+ // Create opacity map
+ data->renderingMap = RenderingAddOn::Get().IsValid() ? RenderingAddOn::Get().BuildNPatch(pixelBuffer, data ) : nullptr;
+
+ PixelData pixels = Devel::PixelBuffer::Convert( pixelBuffer ); // takes ownership of buffer
+
+ Texture texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
+ texture.Upload( pixels );
+
+ data->textureSet = TextureSet::New();
+ data->textureSet.SetTexture( 0u, texture );
+
+ data->loadCompleted = true;
+}
+
+} // namespace NPatchBuffer
+
+NPatchLoader::Data::~Data()
+{
+ // If there is an opacity map, it has to be destroyed using addon call
+ if( renderingMap )
+ {
+ RenderingAddOn::Get().DestroyNPatch( renderingMap );
+ }
+}
+