Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: tools/sk_tool_utils.cpp

Issue 1298763002: Move normal map creation methods to sk_tools (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: cleanup Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/sk_tool_utils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2014 Google Inc. 2 * Copyright 2014 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "sk_tool_utils.h" 8 #include "sk_tool_utils.h"
9 #include "sk_tool_utils_flags.h" 9 #include "sk_tool_utils_flags.h"
10 10
11 #include "Resources.h" 11 #include "Resources.h"
12 #include "SkBitmap.h" 12 #include "SkBitmap.h"
13 #include "SkCanvas.h" 13 #include "SkCanvas.h"
14 #include "SkCommonFlags.h" 14 #include "SkCommonFlags.h"
15 #include "SkPoint3.h"
15 #include "SkShader.h" 16 #include "SkShader.h"
16 #include "SkTestScalerContext.h" 17 #include "SkTestScalerContext.h"
17 #include "SkTextBlob.h" 18 #include "SkTextBlob.h"
18 19
19 DEFINE_bool(portableFonts, false, "Use portable fonts"); 20 DEFINE_bool(portableFonts, false, "Use portable fonts");
20 21
21 namespace sk_tool_utils { 22 namespace sk_tool_utils {
22 23
23 /* these are the default fonts chosen by Chrome for serif, sans-serif, and monos pace */ 24 /* these are the default fonts chosen by Chrome for serif, sans-serif, and monos pace */
24 static const char* gStandardFontNames[][3] = { 25 static const char* gStandardFontNames[][3] = {
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 size_t len = strlen(text); 203 size_t len = strlen(text);
203 glyphs.append(paint.textToGlyphs(text, len, NULL)); 204 glyphs.append(paint.textToGlyphs(text, len, NULL));
204 paint.textToGlyphs(text, len, glyphs.begin()); 205 paint.textToGlyphs(text, len, glyphs.begin());
205 206
206 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); 207 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
207 const SkTextBlobBuilder::RunBuffer& run = builder->allocRun(paint, glyphs.co unt(), x, y, 208 const SkTextBlobBuilder::RunBuffer& run = builder->allocRun(paint, glyphs.co unt(), x, y,
208 NULL); 209 NULL);
209 memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t)); 210 memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
210 } 211 }
211 212
213 static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm ) {
214 SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f));
215 unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255);
216 unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255) ;
217 unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255);
218 *bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
219 }
220
221 void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) {
222 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
223 dst.fTop + (dst.height() / 2.0f));
224 const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2. 0f);
225
226 SkVector3 norm;
227
228 for (int y = dst.fTop; y < dst.fBottom; ++y) {
229 for (int x = dst.fLeft; x < dst.fRight; ++x) {
230 norm.fX = (x + 0.5f - center.fX) / halfSize.fX;
231 norm.fY = (y + 0.5f - center.fY) / halfSize.fY;
232
233 SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY;
234 if (tmp >= 1.0f) {
235 norm.set(0.0f, 0.0f, 1.0f);
236 } else {
237 norm.fZ = sqrt(1.0f - tmp);
238 }
239
240 norm_to_rgb(bm, x, y, norm);
241 }
242 }
243 }
244
245 void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) {
246 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
247 dst.fTop + (dst.height() / 2.0f));
248
249 SkIRect inner = dst;
250 inner.inset(dst.width()/4, dst.height()/4);
251
252 SkPoint3 norm;
253 const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarR oot2Over2);
254 const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarR oot2Over2);
255 const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarR oot2Over2);
256 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarR oot2Over2);
257
258 for (int y = dst.fTop; y < dst.fBottom; ++y) {
259 for (int x = dst.fLeft; x < dst.fRight; ++x) {
260 if (inner.contains(x, y)) {
261 norm.set(0.0f, 0.0f, 1.0f);
262 } else {
263 SkScalar locX = x + 0.5f - center.fX;
264 SkScalar locY = y + 0.5f - center.fY;
265
266 if (locX >= 0.0f) {
267 if (locY > 0.0f) {
268 norm = locX >= locY ? right : down; // LR corner
269 } else {
270 norm = locX > -locY ? right : up; // UR corner
271 }
272 } else {
273 if (locY > 0.0f) {
274 norm = -locX > locY ? left : down; // LL corner
275 } else {
276 norm = locX > locY ? up : left; // UL corner
277 }
278 }
279 }
280
281 norm_to_rgb(bm, x, y, norm);
282 }
283 }
284 }
285
286 void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) {
287 const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f),
288 dst.fTop + (dst.height() / 2.0f));
289
290 static const SkScalar k1OverRoot3 = 0.5773502692f;
291
292 SkPoint3 norm;
293 const SkPoint3 leftUp = SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRo ot3);
294 const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3, -k1OverRoot3, k1OverRo ot3);
295 const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarR oot2Over2);
296
297 for (int y = dst.fTop; y < dst.fBottom; ++y) {
298 for (int x = dst.fLeft; x < dst.fRight; ++x) {
299 SkScalar locX = x + 0.5f - center.fX;
300 SkScalar locY = y + 0.5f - center.fY;
301
302 if (locX >= 0.0f) {
303 if (locY > 0.0f) {
304 norm = locX >= locY ? rightUp : down; // LR corner
305 } else {
306 norm = rightUp;
307 }
308 } else {
309 if (locY > 0.0f) {
310 norm = -locX > locY ? leftUp : down; // LL corner
311 } else {
312 norm = leftUp;
313 }
314 }
315
316 norm_to_rgb(bm, x, y, norm);
317 }
318 }
319 }
212 320
213 } // namespace sk_tool_utils 321 } // namespace sk_tool_utils
OLDNEW
« no previous file with comments | « tools/sk_tool_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698