Implement sk_tool_utils::copy_to_g8(), used by gms

BUG=skia:

Change-Id: Ic5b2b07fb3f408edf1f2b982235424c22795fe50
Reviewed-on: https://skia-review.googlesource.com/7187
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Matt Sarett <msarett@google.com>
This commit is contained in:
Matt Sarett 2017-01-18 11:49:33 -05:00 committed by Skia Commit-Bot
parent 1517224346
commit 4897fb898f
4 changed files with 37 additions and 35 deletions

View File

@ -11,6 +11,7 @@
#include "SkGradientShader.h"
#include "SkStream.h"
#include "SkTypeface.h"
#include "sk_tool_utils.h"
static void setTypeface(SkPaint* paint, const char name[], SkFontStyle style) {
sk_tool_utils::set_portable_typeface(paint, name, style);
@ -169,7 +170,7 @@ public:
}
if (fConvertToG8) {
SkBitmap tmp;
fBM.copyTo(&tmp, kGray_8_SkColorType);
sk_tool_utils::copy_to_g8(&tmp, fBM);
fBM = tmp;
}
}
@ -203,7 +204,7 @@ protected:
if (fConvertToG8) {
SkBitmap tmp;
fBM.copyTo(&tmp, kGray_8_SkColorType);
sk_tool_utils::copy_to_g8(&tmp, fBM);
fBM = tmp;
}
}

View File

@ -216,41 +216,9 @@ DEF_GM( return new ShowMipLevels(256); )
///////////////////////////////////////////////////////////////////////////////////////////////////
static void copy_32_to_g8(void* dst, size_t dstRB, const void* src, const SkImageInfo& srcInfo,
size_t srcRB) {
uint8_t* dst8 = (uint8_t*)dst;
const uint32_t* src32 = (const uint32_t*)src;
const int w = srcInfo.width();
const int h = srcInfo.height();
const bool isBGRA = (kBGRA_8888_SkColorType == srcInfo.colorType());
for (int y = 0; y < h; ++y) {
if (isBGRA) {
// BGRA
for (int x = 0; x < w; ++x) {
uint32_t s = src32[x];
dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
}
} else {
// RGBA
for (int x = 0; x < w; ++x) {
uint32_t s = src32[x];
dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
}
}
src32 = (const uint32_t*)((const char*)src32 + srcRB);
dst8 += dstRB;
}
}
void copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
if (kGray_8_SkColorType == dstColorType) {
SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
dst->allocPixels(grayInfo);
copy_32_to_g8(dst->getPixels(), dst->rowBytes(), src.getPixels(), src.info(),
src.rowBytes());
return;
return sk_tool_utils::copy_to_g8(dst, src);
}
src.copyTo(dst, dstColorType);

View File

@ -555,5 +555,35 @@ SkRect compute_tallest_occluder(const SkRRect& rr) {
return SkRect::MakeLTRB(r.fLeft + maxL, r.fTop, r.fRight - maxR, r.fBottom);
}
void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
SkASSERT(kBGRA_8888_SkColorType == src.colorType() ||
kRGBA_8888_SkColorType == src.colorType());
SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType);
dst->allocPixels(grayInfo);
uint8_t* dst8 = (uint8_t*)dst->getPixels();
const uint32_t* src32 = (const uint32_t*)src.getPixels();
const int w = src.width();
const int h = src.height();
const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType());
for (int y = 0; y < h; ++y) {
if (isBGRA) {
// BGRA
for (int x = 0; x < w; ++x) {
uint32_t s = src32[x];
dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
}
} else {
// RGBA
for (int x = 0; x < w; ++x) {
uint32_t s = src32[x];
dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
}
}
src32 = (const uint32_t*)((const char*)src32 + src.rowBytes());
dst8 += dst->rowBytes();
}
}
} // namespace sk_tool_utils

View File

@ -251,6 +251,9 @@ namespace sk_tool_utils {
};
return sk_make_sp<EncodeImagePixelSerializer>();
}
void copy_to_g8(SkBitmap* dst, const SkBitmap& src);
} // namespace sk_tool_utils
#endif // sk_tool_utils_DEFINED