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
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "samplecode/DecodeFile.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/core/SkBlurMask.h"
|
|
|
|
#include "src/core/SkOSFile.h"
|
|
|
|
#include "src/utils/SkOSPath.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
|
|
|
#include "tools/Resources.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2013-06-14 15:33:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class UnpremulView : public Sample {
|
2013-06-14 15:33:20 +00:00
|
|
|
public:
|
|
|
|
UnpremulView(SkString res)
|
|
|
|
: fResPath(res)
|
|
|
|
, fPremul(true)
|
|
|
|
, fDecodeSucceeded(false) {
|
|
|
|
this->nextImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("unpremul"); }
|
|
|
|
|
2019-07-03 19:53:04 +00:00
|
|
|
bool onChar(SkUnichar uni) override {
|
2018-07-25 20:52:48 +00:00
|
|
|
char utf8[SkUTF::kMaxBytesInUTF8Sequence];
|
|
|
|
size_t size = SkUTF::ToUTF8(uni, utf8);
|
2013-06-14 15:33:20 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2019-07-03 19:53:04 +00:00
|
|
|
return false;
|
2013-06-14 15:33:20 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDrawBackground(SkCanvas* canvas) override {
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::draw_checkerboard(canvas, 0xFFCCCCCC, 0xFFFFFFFF, 12);
|
2013-06-14 15:33:20 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2013-06-14 15:33:20 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2019-01-02 17:21:01 +00:00
|
|
|
|
|
|
|
SkFont font;
|
|
|
|
font.setSize(24);
|
|
|
|
SkScalar height = font.getMetrics(nullptr);
|
2013-06-14 15:33:20 +00:00
|
|
|
if (!fDecodeSucceeded) {
|
|
|
|
SkString failure;
|
|
|
|
if (fResPath.size() == 0) {
|
|
|
|
failure.printf("resource path is required!");
|
|
|
|
} else {
|
|
|
|
failure.printf("Failed to decode %s", fCurrFile.c_str());
|
|
|
|
}
|
2019-01-07 14:36:09 +00:00
|
|
|
canvas->drawString(failure, 0, height, font, paint);
|
2013-06-14 15:33:20 +00:00
|
|
|
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"));
|
2019-01-07 14:36:09 +00:00
|
|
|
canvas->drawString(header, 0, height, font, paint);
|
2013-06-14 15:33:20 +00:00
|
|
|
canvas->translate(0, height);
|
|
|
|
|
|
|
|
// Help messages
|
|
|
|
header.printf("Press '%c' to move to the next image.'", fNextImageChar);
|
2019-01-07 14:36:09 +00:00
|
|
|
canvas->drawString(header, 0, height, font, paint);
|
2013-06-14 15:33:20 +00:00
|
|
|
canvas->translate(0, height);
|
|
|
|
|
|
|
|
header.printf("Press '%c' to toggle premultiplied decode.", fTogglePremulChar);
|
2019-01-07 14:36:09 +00:00
|
|
|
canvas->drawString(header, 0, height, font, paint);
|
2013-06-14 15:33:20 +00:00
|
|
|
|
|
|
|
// Now draw the image itself.
|
|
|
|
canvas->translate(height * 2, height * 2);
|
|
|
|
if (!fPremul) {
|
|
|
|
// A premultiplied bitmap cannot currently be drawn.
|
|
|
|
// 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;
|
|
|
|
}
|
2016-03-18 22:48:49 +00:00
|
|
|
fDecodeSucceeded = decode_file(fCurrFile.c_str(), &fBitmap, kN32_SkColorType, !fPremul);
|
2013-06-14 15:33:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void togglePremul() {
|
|
|
|
fPremul = !fPremul;
|
|
|
|
this->decodeCurrFile();
|
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2013-06-14 15:33:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new UnpremulView(GetResourcePath("images")); )
|