OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "DMSrcSink.h" | 8 #include "DMSrcSink.h" |
9 #include "SamplePipeControllers.h" | 9 #include "SamplePipeControllers.h" |
10 #include "SkCodec.h" | 10 #include "SkCodec.h" |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 case SkCodec::kIncompleteInput: | 348 case SkCodec::kIncompleteInput: |
349 break; | 349 break; |
350 default: | 350 default: |
351 return SkStringPrintf("Cannot skip scanlines for %s.
", fPath.c_str()); | 351 return SkStringPrintf("Cannot skip scanlines for %s.
", fPath.c_str()); |
352 } | 352 } |
353 } | 353 } |
354 } | 354 } |
355 canvas->drawBitmap(bitmap, 0, 0); | 355 canvas->drawBitmap(bitmap, 0, 0); |
356 break; | 356 break; |
357 } | 357 } |
| 358 case kSubset_Mode: { |
| 359 // Arbitrarily choose a divisor. |
| 360 int divisor = 2; |
| 361 // Total width/height of the image. |
| 362 const int W = codec->getInfo().width(); |
| 363 const int H = codec->getInfo().height(); |
| 364 if (divisor > W || divisor > H) { |
| 365 return Error::Nonfatal(SkStringPrintf("Cannot codec subset: divi
sor %d is too big " |
| 366 "for %s with dimensions (%
d x %d)", divisor, |
| 367 fPath.c_str(), W, H)); |
| 368 } |
| 369 // subset dimensions |
| 370 // SkWebpCodec, the only one that supports subsets, requires even to
p/left boundaries. |
| 371 const int w = SkAlign2(W / divisor); |
| 372 const int h = SkAlign2(H / divisor); |
| 373 SkIRect subset; |
| 374 SkCodec::Options opts; |
| 375 opts.fSubset = ⊂ |
| 376 SkBitmap subsetBm; |
| 377 // We will reuse pixel memory from bitmap. |
| 378 void* pixels = bitmap.getPixels(); |
| 379 // Keep track of left and top (for drawing subsetBm into canvas). We
could use |
| 380 // fScale * x and fScale * y, but we want integers such that the nex
t subset will start |
| 381 // where the last one ended. So we'll add decodeInfo.width() and hei
ght(). |
| 382 int left = 0; |
| 383 for (int x = 0; x < W; x += w) { |
| 384 int top = 0; |
| 385 for (int y = 0; y < H; y+= h) { |
| 386 // Do not make the subset go off the edge of the image. |
| 387 const int preScaleW = SkTMin(w, W - x); |
| 388 const int preScaleH = SkTMin(h, H - y); |
| 389 subset.setXYWH(x, y, preScaleW, preScaleH); |
| 390 // And scale |
| 391 // FIXME: Should we have a version of getScaledDimensions th
at takes a subset |
| 392 // into account? |
| 393 decodeInfo = decodeInfo.makeWH(SkScalarRoundToInt(preScaleW
* fScale), |
| 394 SkScalarRoundToInt(preScaleH
* fScale)); |
| 395 size_t rowBytes = decodeInfo.minRowBytes(); |
| 396 if (!subsetBm.installPixels(decodeInfo, pixels, rowBytes, co
lorTable.get(), |
| 397 NULL, NULL)) { |
| 398 return SkStringPrintf("could not install pixels for %s."
, fPath.c_str()); |
| 399 } |
| 400 const SkCodec::Result result = codec->getPixels(decodeInfo,
pixels, rowBytes, |
| 401 &opts, colorPtr, colorCountPtr); |
| 402 switch (result) { |
| 403 case SkCodec::kSuccess: |
| 404 case SkCodec::kIncompleteInput: |
| 405 break; |
| 406 case SkCodec::kInvalidConversion: |
| 407 if (0 == (x|y)) { |
| 408 // First subset is okay to return unimplemented. |
| 409 return Error::Nonfatal("Incompatible colortype c
onversion"); |
| 410 } |
| 411 // If the first subset succeeded, a later one should
not fail. |
| 412 // fall through to failure |
| 413 case SkCodec::kUnimplemented: |
| 414 if (0 == (x|y)) { |
| 415 // First subset is okay to return unimplemented. |
| 416 return Error::Nonfatal("subset codec not support
ed"); |
| 417 } |
| 418 // If the first subset succeeded, why would a later
one fail? |
| 419 // fall through to failure |
| 420 default: |
| 421 return SkStringPrintf("subset codec failed to decode
(%d, %d, %d, %d) " |
| 422 "from %s with dimensions (%d x
%d)\t error %d", |
| 423 x, y, decodeInfo.width(), deco
deInfo.height(), |
| 424 fPath.c_str(), W, H, result); |
| 425 } |
| 426 canvas->drawBitmap(subsetBm, SkIntToScalar(left), SkIntToSca
lar(top)); |
| 427 // translate by the scaled height. |
| 428 top += decodeInfo.height(); |
| 429 } |
| 430 // translate by the scaled width. |
| 431 left += decodeInfo.width(); |
| 432 } |
| 433 return ""; |
| 434 } |
358 } | 435 } |
359 return ""; | 436 return ""; |
360 } | 437 } |
361 | 438 |
362 SkISize CodecSrc::size() const { | 439 SkISize CodecSrc::size() const { |
363 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); | 440 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str())); |
364 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); | 441 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded)); |
365 if (NULL != codec) { | 442 if (NULL != codec) { |
366 SkISize size = codec->getScaledDimensions(fScale); | 443 SkISize size = codec->getScaledDimensions(fScale); |
367 return size; | 444 return size; |
(...skipping 621 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
989 skr.visit<void>(i, drawsAsSingletonPictures); | 1066 skr.visit<void>(i, drawsAsSingletonPictures); |
990 } | 1067 } |
991 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); | 1068 SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture()); |
992 | 1069 |
993 canvas->drawPicture(macroPic); | 1070 canvas->drawPicture(macroPic); |
994 return ""; | 1071 return ""; |
995 }); | 1072 }); |
996 } | 1073 } |
997 | 1074 |
998 } // namespace DM | 1075 } // namespace DM |
OLD | NEW |