This repository was archived by the owner on Jan 9, 2023. It is now read-only.
forked from google/skia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmipmap.cpp
158 lines (135 loc) · 5.47 KB
/
mipmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm/gm.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPaint.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTypes.h"
static sk_sp<SkImage> make_image() {
const SkImageInfo info = SkImageInfo::MakeN32Premul(319, 52);
auto surface(SkSurface::MakeRaster(info));
SkCanvas* canvas = surface->getCanvas();
canvas->drawColor(0xFFF8F8F8);
SkPaint paint;
paint.setAntiAlias(true);
paint.setStyle(SkPaint::kStroke_Style);
for (int i = 0; i < 20; ++i) {
canvas->drawCircle(-4, 25, 20, paint);
canvas->translate(25, 0);
}
return surface->makeImageSnapshot();
}
DEF_SIMPLE_GM(mipmap, canvas, 400, 200) {
sk_sp<SkImage> img(make_image());//SkImage::NewFromEncoded(data));
const SkRect dst = SkRect::MakeWH(177, 15);
SkString str;
str.printf("scale %g %g", dst.width() / img->width(), dst.height() / img->height());
// canvas->drawString(str, 300, 100, SkFont(nullptr, 30), paint);
const SkSamplingOptions samplings[] = {
SkSamplingOptions(SkFilterMode::kNearest),
SkSamplingOptions(SkFilterMode::kLinear),
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
SkSamplingOptions(SkCubicResampler::Mitchell()),
};
canvas->translate(20, 20);
for (size_t i = 0; i < SK_ARRAY_COUNT(samplings); ++i) {
canvas->drawImageRect(img.get(), dst, samplings[i], nullptr);
canvas->translate(0, 20);
}
canvas->drawImage(img.get(), 20, 20);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// create a circle image computed raw, so we can wrap it as a linear or srgb image
static sk_sp<SkImage> make(sk_sp<SkColorSpace> cs) {
const int N = 100;
SkImageInfo info = SkImageInfo::Make(N, N, kN32_SkColorType, kPremul_SkAlphaType, cs);
SkBitmap bm;
bm.allocPixels(info);
for (int y = 0; y < N; ++y) {
for (int x = 0; x < N; ++x) {
*bm.getAddr32(x, y) = (x ^ y) & 1 ? 0xFFFFFFFF : 0xFF000000;
}
}
bm.setImmutable();
return bm.asImage();
}
static void show_mips(SkCanvas* canvas, SkImage* img) {
SkSamplingOptions sampling(SkFilterMode::kLinear,
SkMipmapMode::kLinear);
// Want to ensure we never draw fractional pixels, so we use an IRect
SkIRect dst = SkIRect::MakeWH(img->width(), img->height());
while (dst.width() > 5) {
canvas->drawImageRect(img, SkRect::Make(dst), sampling, nullptr);
dst.offset(dst.width() + 10, 0);
dst.fRight = dst.fLeft + dst.width()/2;
dst.fBottom = dst.fTop + dst.height()/2;
}
}
/*
* Ensure that in L32 drawing mode, both images/mips look the same as each other, and
* their mips are darker than the original (since the mips should ignore the gamma in L32).
*
* Ensure that in S32 drawing mode, all images/mips look the same, and look correct (i.e.
* the mip levels match the original in brightness).
*/
DEF_SIMPLE_GM(mipmap_srgb, canvas, 260, 230) {
sk_sp<SkImage> limg = make(nullptr);
sk_sp<SkImage> simg = make(SkColorSpace::MakeSRGB());
canvas->translate(10, 10);
show_mips(canvas, limg.get());
canvas->translate(0, limg->height() + 10.0f);
show_mips(canvas, simg.get());
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// create a gradient image computed raw, so we can wrap it as a linear or srgb image
static sk_sp<SkImage> make_g8_gradient(sk_sp<SkColorSpace> cs) {
const int N = 100;
SkImageInfo info = SkImageInfo::Make(N, N, kGray_8_SkColorType, kOpaque_SkAlphaType, cs);
SkBitmap bm;
bm.allocPixels(info);
for (int y = 0; y < N; ++y) {
for (int x = 0; x < N; ++x) {
*bm.getAddr8(x, y) = static_cast<uint8_t>(255.0f * ((x + y) / (2.0f * (N - 1))));
}
}
bm.setImmutable();
return bm.asImage();
}
static void show_mips_only(SkCanvas* canvas, SkImage* img) {
SkSamplingOptions sampling(SkFilterMode::kLinear,
SkMipmapMode::kLinear);
// Want to ensure we never draw fractional pixels, so we use an IRect
SkIRect dst = SkIRect::MakeWH(img->width() / 2, img->height() / 2);
while (dst.width() > 5) {
canvas->drawImageRect(img, SkRect::Make(dst), sampling, nullptr);
dst.offset(dst.width() + 10, 0);
dst.fRight = dst.fLeft + dst.width() / 2;
dst.fBottom = dst.fTop + dst.height() / 2;
}
}
/*
* Ensure that in L32 drawing mode, both images/mips look the same as each other, and
* their mips are darker than the original (since the mips should ignore the gamma in L32).
*
* Ensure that in S32 drawing mode, all images/mips look the same, and look correct (i.e.
* the mip levels match the original in brightness).
*/
DEF_SIMPLE_GM(mipmap_gray8_srgb, canvas, 260, 230) {
sk_sp<SkImage> limg = make_g8_gradient(nullptr);
sk_sp<SkImage> simg = make_g8_gradient(SkColorSpace::MakeSRGB());
canvas->translate(10, 10);
show_mips_only(canvas, limg.get());
canvas->translate(0, limg->height() + 10.0f);
show_mips_only(canvas, simg.get());
}