2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2015-08-05 20:57:49 +00:00
|
|
|
|
2009-08-20 21:29:45 +00:00
|
|
|
#include "SampleCode.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkCanvas.h"
|
2009-08-20 21:29:45 +00:00
|
|
|
#include "SkColorPriv.h"
|
2009-08-27 19:28:37 +00:00
|
|
|
#include "SkGradientShader.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkPath.h"
|
2017-02-13 14:32:54 +00:00
|
|
|
#include "SkRegion.h"
|
2015-08-05 01:44:56 +00:00
|
|
|
#include "SkUtils.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkView.h"
|
2009-08-20 21:29:45 +00:00
|
|
|
|
2009-08-27 19:28:37 +00:00
|
|
|
static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
|
|
|
|
canvas->drawRect(r, p);
|
|
|
|
|
|
|
|
SkPaint frame(p);
|
2015-08-27 14:41:13 +00:00
|
|
|
frame.setShader(nullptr);
|
2009-08-27 19:28:37 +00:00
|
|
|
frame.setStyle(SkPaint::kStroke_Style);
|
|
|
|
canvas->drawRect(r, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_gradient(SkCanvas* canvas) {
|
|
|
|
SkRect r = { 0, 0, SkIntToScalar(256), SkIntToScalar(32) };
|
2011-05-19 19:58:58 +00:00
|
|
|
SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } };
|
2009-08-27 19:28:37 +00:00
|
|
|
SkColor colors[] = { 0xFF000000, 0xFFFF0000 };
|
|
|
|
SkPaint p;
|
2016-03-09 02:50:00 +00:00
|
|
|
p.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode));
|
2009-08-27 19:28:37 +00:00
|
|
|
draw_rect(canvas, r, p);
|
|
|
|
|
|
|
|
canvas->translate(0, SkIntToScalar(40));
|
|
|
|
p.setDither(true);
|
|
|
|
draw_rect(canvas, r, p);
|
|
|
|
}
|
|
|
|
|
2014-01-10 14:53:49 +00:00
|
|
|
static bool test_pathregion() {
|
2009-08-25 19:15:31 +00:00
|
|
|
SkPath path;
|
|
|
|
SkRegion region;
|
2012-08-23 18:19:56 +00:00
|
|
|
path.moveTo(25071800.f, -141823808.f);
|
2009-08-25 19:15:31 +00:00
|
|
|
path.lineTo(25075500.f, -141824000.f);
|
|
|
|
path.lineTo(25075400.f, -141827712.f);
|
|
|
|
path.lineTo(25071810.f, -141827600.f);
|
|
|
|
path.close();
|
|
|
|
|
|
|
|
SkIRect bounds;
|
|
|
|
path.getBounds().round(&bounds);
|
|
|
|
SkRegion clip(bounds);
|
2014-01-10 14:53:49 +00:00
|
|
|
return region.setPath(path, clip); // <-- !! DOWN !!
|
2009-08-25 19:15:31 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 21:29:45 +00:00
|
|
|
static SkBitmap make_bitmap() {
|
2013-10-10 14:44:56 +00:00
|
|
|
SkPMColor c[256];
|
2009-08-20 21:29:45 +00:00
|
|
|
for (int i = 0; i < 256; i++) {
|
2009-08-22 03:44:57 +00:00
|
|
|
c[i] = SkPackARGB32(0xFF, i, 0, 0);
|
2009-08-20 21:29:45 +00:00
|
|
|
}
|
2014-09-29 19:10:27 +00:00
|
|
|
SkColorTable* ctable = new SkColorTable(c, 256);
|
2013-10-10 14:44:56 +00:00
|
|
|
|
|
|
|
SkBitmap bm;
|
2014-09-29 19:10:27 +00:00
|
|
|
bm.allocPixels(SkImageInfo::Make(256, 32, kIndex_8_SkColorType, kPremul_SkAlphaType),
|
2015-08-27 14:41:13 +00:00
|
|
|
nullptr, ctable);
|
2009-08-20 21:29:45 +00:00
|
|
|
ctable->unref();
|
|
|
|
|
|
|
|
bm.lockPixels();
|
|
|
|
for (int y = 0; y < bm.height(); y++) {
|
|
|
|
uint8_t* p = bm.getAddr8(0, y);
|
|
|
|
for (int x = 0; x < 256; x++) {
|
|
|
|
p[x] = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bm.unlockPixels();
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
2011-05-11 05:58:58 +00:00
|
|
|
class DitherBitmapView : public SampleView {
|
2009-08-20 21:29:45 +00:00
|
|
|
SkBitmap fBM8;
|
|
|
|
SkBitmap fBM32;
|
2014-01-10 14:53:49 +00:00
|
|
|
bool fResult;
|
2009-08-20 21:29:45 +00:00
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
DitherBitmapView() {
|
2014-01-10 14:53:49 +00:00
|
|
|
fResult = test_pathregion();
|
2009-08-20 21:29:45 +00:00
|
|
|
fBM8 = make_bitmap();
|
2014-04-11 17:15:40 +00:00
|
|
|
fBM8.copyTo(&fBM32, kN32_SkColorType);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-11 05:58:58 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
2009-08-20 21:29:45 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-08-20 21:29:45 +00:00
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "DitherBitmap");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-08-22 03:44:57 +00:00
|
|
|
static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) {
|
|
|
|
SkAutoLockPixels alp(*bm); // needed for ctable
|
2013-10-21 14:00:07 +00:00
|
|
|
bm->setAlphaType(isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
|
2009-08-22 03:44:57 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-08-20 21:29:45 +00:00
|
|
|
static void draw2(SkCanvas* canvas, const SkBitmap& bm) {
|
|
|
|
SkPaint paint;
|
2009-08-22 03:44:57 +00:00
|
|
|
SkBitmap bitmap(bm);
|
|
|
|
|
|
|
|
setBitmapOpaque(&bitmap, false);
|
|
|
|
paint.setDither(false);
|
|
|
|
canvas->drawBitmap(bitmap, 0, 0, &paint);
|
|
|
|
paint.setDither(true);
|
|
|
|
canvas->drawBitmap(bitmap, 0, SkIntToScalar(bm.height() + 10), &paint);
|
|
|
|
|
|
|
|
setBitmapOpaque(&bitmap, true);
|
|
|
|
SkScalar x = SkIntToScalar(bm.width() + 10);
|
|
|
|
paint.setDither(false);
|
|
|
|
canvas->drawBitmap(bitmap, x, 0, &paint);
|
2009-08-20 21:29:45 +00:00
|
|
|
paint.setDither(true);
|
2009-08-22 03:44:57 +00:00
|
|
|
canvas->drawBitmap(bitmap, x, SkIntToScalar(bm.height() + 10), &paint);
|
2009-08-20 21:29:45 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-11 05:58:58 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2009-08-20 21:29:45 +00:00
|
|
|
canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
|
|
|
|
|
|
|
|
draw2(canvas, fBM8);
|
|
|
|
canvas->translate(0, SkIntToScalar(fBM8.height() *3));
|
|
|
|
draw2(canvas, fBM32);
|
2009-08-27 19:28:37 +00:00
|
|
|
|
|
|
|
canvas->translate(0, SkIntToScalar(fBM8.height() *3));
|
|
|
|
draw_gradient(canvas);
|
2014-01-10 14:53:49 +00:00
|
|
|
|
|
|
|
char resultTrue[] = "SkRegion::setPath returned true";
|
|
|
|
char resultFalse[] = "SkRegion::setPath returned false";
|
|
|
|
SkPaint p;
|
|
|
|
if (fResult)
|
|
|
|
canvas->drawText(resultTrue, sizeof(resultTrue) - 1, 0, 50, p);
|
|
|
|
else
|
|
|
|
canvas->drawText(resultFalse, sizeof(resultFalse) - 1, 0, 50, p);
|
2009-08-20 21:29:45 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-08-20 21:29:45 +00:00
|
|
|
private:
|
2011-05-11 05:58:58 +00:00
|
|
|
typedef SampleView INHERITED;
|
2009-08-20 21:29:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new DitherBitmapView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|