Bug 983591 - Add a Moz2d helper to copy pixel data from a B8G8R8X8 surface to a packed B8G8R8 array. r=Bas

This commit is contained in:
Jonathan Watt 2014-04-08 10:14:52 +01:00
parent a8ccc1139f
commit 4678063331
2 changed files with 67 additions and 0 deletions

View File

@ -48,6 +48,29 @@ CopySurfaceDataToPackedArray(uint8_t* aSrc, uint8_t* aDst, IntSize aSrcSize,
}
}
void
CopyBGRXSurfaceDataToPackedBGRArray(uint8_t* aSrc, uint8_t* aDst,
IntSize aSrcSize, int32_t aSrcStride)
{
int packedStride = aSrcSize.width * 3;
uint8_t* srcPx = aSrc;
uint8_t* dstPx = aDst;
for (int row = 0; row < aSrcSize.height; ++row) {
for (int col = 0; col < aSrcSize.height; ++col) {
dstPx[0] = srcPx[0];
dstPx[1] = srcPx[1];
dstPx[2] = srcPx[2];
// srcPx[3] (unused or alpha component) dropped on floor
srcPx += 4;
dstPx += 3;
}
srcPx = aSrc += aSrcStride;
dstPx = aDst += packedStride;
}
}
uint8_t*
SurfaceToPackedBGRA(DataSourceSurface *aSurface)
{
@ -82,5 +105,37 @@ SurfaceToPackedBGRA(DataSourceSurface *aSurface)
return imageBuffer;
}
uint8_t*
SurfaceToPackedBGR(DataSourceSurface *aSurface)
{
SurfaceFormat format = aSurface->GetFormat();
MOZ_ASSERT(format == SurfaceFormat::B8G8R8X8, "Format not supported");
if (format != SurfaceFormat::B8G8R8X8) {
// To support B8G8R8A8 we'd need to un-pre-multiply alpha
return nullptr;
}
IntSize size = aSurface->GetSize();
uint8_t* imageBuffer = new (std::nothrow) uint8_t[size.width * size.height * 3 * sizeof(uint8_t)];
if (!imageBuffer) {
return nullptr;
}
DataSourceSurface::MappedSurface map;
if (!aSurface->Map(DataSourceSurface::MapType::READ, &map)) {
delete [] imageBuffer;
return nullptr;
}
CopyBGRXSurfaceDataToPackedBGRArray(map.mData, imageBuffer, size,
map.mStride);
aSurface->Unmap();
return imageBuffer;
}
}
}

View File

@ -30,5 +30,17 @@ CopySurfaceDataToPackedArray(uint8_t* aSrc, uint8_t* aDst, IntSize aSrcSize,
uint8_t*
SurfaceToPackedBGRA(DataSourceSurface *aSurface);
/**
* Convert aSurface to a packed buffer in BGR format. The pixel data is
* returned in a buffer allocated with new uint8_t[].
*
* This function is currently only intended for use with surfaces of format
* SurfaceFormat::B8G8R8X8 since the X components of the pixel data are simply
* dropped (no attempt is made to un-pre-multiply alpha from the color
* components).
*/
uint8_t*
SurfaceToPackedBGR(DataSourceSurface *aSurface);
}
}