2013-06-14 15:33:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2014-06-13 00:40:00 +00:00
|
|
|
|
2013-06-14 15:33:20 +00:00
|
|
|
#include "gm.h"
|
2014-06-18 21:32:48 +00:00
|
|
|
|
2015-01-26 20:49:00 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2014-06-18 21:32:48 +00:00
|
|
|
#include "Resources.h"
|
2013-06-14 15:33:20 +00:00
|
|
|
#include "SampleCode.h"
|
2013-09-06 14:16:12 +00:00
|
|
|
#include "SkBlurMask.h"
|
2013-06-14 15:33:20 +00:00
|
|
|
#include "SkBlurDrawLooper.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkForceLinking.h"
|
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkString.h"
|
|
|
|
#include "SkSystemEventTypes.h"
|
|
|
|
#include "SkTypes.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
|
|
|
|
__SK_FORCE_IMAGE_DECODER_LINKING;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interprets c as an unpremultiplied color, and returns the
|
|
|
|
* premultiplied equivalent.
|
|
|
|
*/
|
|
|
|
static SkPMColor premultiply_unpmcolor(SkPMColor c) {
|
|
|
|
U8CPU a = SkGetPackedA32(c);
|
|
|
|
U8CPU r = SkGetPackedR32(c);
|
|
|
|
U8CPU g = SkGetPackedG32(c);
|
|
|
|
U8CPU b = SkGetPackedB32(c);
|
|
|
|
return SkPreMultiplyARGB(a, r, g, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
class UnpremulView : public SampleView {
|
|
|
|
public:
|
|
|
|
UnpremulView(SkString res)
|
|
|
|
: fResPath(res)
|
|
|
|
, fPremul(true)
|
|
|
|
, fDecodeSucceeded(false) {
|
|
|
|
this->nextImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
2015-01-09 18:06:39 +00:00
|
|
|
bool onQuery(SkEvent* evt) SK_OVERRIDE {
|
2013-06-14 15:33:20 +00:00
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "unpremul");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SkUnichar uni;
|
|
|
|
if (SampleCode::CharQ(*evt, &uni)) {
|
|
|
|
char utf8[kMaxBytesInUTF8Sequence];
|
|
|
|
size_t size = SkUTF8_FromUnichar(uni, utf8);
|
|
|
|
// Only consider events for single char keys
|
|
|
|
if (1 == size) {
|
|
|
|
switch (utf8[0]) {
|
|
|
|
case fNextImageChar:
|
|
|
|
this->nextImage();
|
|
|
|
return true;
|
|
|
|
case fTogglePremulChar:
|
|
|
|
this->togglePremul();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
void onDrawBackground(SkCanvas* canvas) SK_OVERRIDE {
|
2015-01-26 20:49:00 +00:00
|
|
|
sk_tool_utils::draw_checkerboard(canvas, 0xFFCCCCCC, 0xFFFFFFFF, 12);
|
2013-06-14 15:33:20 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
|
2013-06-14 15:33:20 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setTextSize(SkIntToScalar(24));
|
2014-04-15 15:48:36 +00:00
|
|
|
SkAutoTUnref<SkBlurDrawLooper> looper(
|
|
|
|
SkBlurDrawLooper::Create(SK_ColorBLUE,
|
|
|
|
SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(2)),
|
|
|
|
0, 0));
|
2013-06-14 15:33:20 +00:00
|
|
|
paint.setLooper(looper);
|
|
|
|
SkScalar height = paint.getFontMetrics(NULL);
|
|
|
|
if (!fDecodeSucceeded) {
|
|
|
|
SkString failure;
|
|
|
|
if (fResPath.size() == 0) {
|
|
|
|
failure.printf("resource path is required!");
|
|
|
|
} else {
|
|
|
|
failure.printf("Failed to decode %s", fCurrFile.c_str());
|
|
|
|
}
|
|
|
|
canvas->drawText(failure.c_str(), failure.size(), 0, height, paint);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name, size of the file, and whether or not it is premultiplied.
|
2014-07-29 02:26:58 +00:00
|
|
|
SkString header(SkOSPath::Basename(fCurrFile.c_str()));
|
2013-06-14 15:33:20 +00:00
|
|
|
header.appendf(" [%dx%d] %s", fBitmap.width(), fBitmap.height(),
|
|
|
|
(fPremul ? "premultiplied" : "unpremultiplied"));
|
|
|
|
canvas->drawText(header.c_str(), header.size(), 0, height, paint);
|
|
|
|
canvas->translate(0, height);
|
|
|
|
|
|
|
|
// Help messages
|
|
|
|
header.printf("Press '%c' to move to the next image.'", fNextImageChar);
|
|
|
|
canvas->drawText(header.c_str(), header.size(), 0, height, paint);
|
|
|
|
canvas->translate(0, height);
|
|
|
|
|
|
|
|
header.printf("Press '%c' to toggle premultiplied decode.", fTogglePremulChar);
|
|
|
|
canvas->drawText(header.c_str(), header.size(), 0, height, paint);
|
|
|
|
|
|
|
|
// Now draw the image itself.
|
|
|
|
canvas->translate(height * 2, height * 2);
|
|
|
|
if (!fPremul) {
|
|
|
|
// A premultiplied bitmap cannot currently be drawn.
|
|
|
|
SkAutoLockPixels alp(fBitmap);
|
|
|
|
// Copy it to a bitmap which can be drawn, converting
|
|
|
|
// to premultiplied:
|
|
|
|
SkBitmap bm;
|
2014-09-02 19:50:45 +00:00
|
|
|
bm.allocN32Pixels(fBitmap.width(), fBitmap.height());
|
2013-06-14 15:33:20 +00:00
|
|
|
for (int i = 0; i < fBitmap.width(); ++i) {
|
|
|
|
for (int j = 0; j < fBitmap.height(); ++j) {
|
|
|
|
*bm.getAddr32(i, j) = premultiply_unpmcolor(*fBitmap.getAddr32(i, j));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->drawBitmap(bm, 0, 0);
|
|
|
|
} else {
|
|
|
|
canvas->drawBitmap(fBitmap, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const SkString fResPath;
|
|
|
|
SkString fCurrFile;
|
|
|
|
bool fPremul;
|
|
|
|
bool fDecodeSucceeded;
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
SkOSFile::Iter fFileIter;
|
|
|
|
|
|
|
|
static const char fNextImageChar = 'j';
|
|
|
|
static const char fTogglePremulChar = 'h';
|
|
|
|
|
|
|
|
void nextImage() {
|
|
|
|
if (fResPath.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SkString basename;
|
|
|
|
if (!fFileIter.next(&basename)) {
|
|
|
|
fFileIter.reset(fResPath.c_str());
|
|
|
|
if (!fFileIter.next(&basename)) {
|
|
|
|
// Perhaps this should draw some error message?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-07-29 02:26:58 +00:00
|
|
|
fCurrFile = SkOSPath::Join(fResPath.c_str(), basename.c_str());
|
2013-06-14 15:33:20 +00:00
|
|
|
this->decodeCurrFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
void decodeCurrFile() {
|
|
|
|
if (fCurrFile.size() == 0) {
|
|
|
|
fDecodeSucceeded = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SkFILEStream stream(fCurrFile.c_str());
|
|
|
|
SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(&stream));
|
|
|
|
if (NULL == decoder.get()) {
|
|
|
|
fDecodeSucceeded = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!fPremul) {
|
|
|
|
decoder->setRequireUnpremultipliedColors(true);
|
|
|
|
}
|
2014-06-13 00:40:00 +00:00
|
|
|
fDecodeSucceeded = decoder->decode(&stream, &fBitmap, kN32_SkColorType,
|
2014-10-22 19:07:00 +00:00
|
|
|
SkImageDecoder::kDecodePixels_Mode) != SkImageDecoder::kFailure;
|
2013-06-14 15:33:20 +00:00
|
|
|
this->inval(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void togglePremul() {
|
|
|
|
fPremul = !fPremul;
|
|
|
|
this->decodeCurrFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef SampleView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() {
|
2014-06-18 21:32:48 +00:00
|
|
|
return new UnpremulView(GetResourcePath());
|
2013-06-14 15:33:20 +00:00
|
|
|
}
|
|
|
|
static SkViewRegister reg(MyFactory);
|