SkBitmapRegionCodec.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/codec/SkAndroidCodec.h"
  8. #include "src/android/SkBitmapRegionCodec.h"
  9. #include "src/android/SkBitmapRegionDecoderPriv.h"
  10. #include "src/codec/SkCodecPriv.h"
  11. SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec)
  12. : INHERITED(codec->getInfo().width(), codec->getInfo().height())
  13. , fCodec(codec)
  14. {}
  15. bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator,
  16. const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType,
  17. bool requireUnpremul, sk_sp<SkColorSpace> dstColorSpace) {
  18. // Fix the input sampleSize if necessary.
  19. if (sampleSize < 1) {
  20. sampleSize = 1;
  21. }
  22. // The size of the output bitmap is determined by the size of the
  23. // requested subset, not by the size of the intersection of the subset
  24. // and the image dimensions.
  25. // If inputX is negative, we will need to place decoded pixels into the
  26. // output bitmap starting at a left offset. Call this outX.
  27. // If outX is non-zero, subsetX must be zero.
  28. // If inputY is negative, we will need to place decoded pixels into the
  29. // output bitmap starting at a top offset. Call this outY.
  30. // If outY is non-zero, subsetY must be zero.
  31. int outX;
  32. int outY;
  33. SkIRect subset = desiredSubset;
  34. SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset, &outX, &outY);
  35. if (SubsetType::kOutside_SubsetType == type) {
  36. return false;
  37. }
  38. // Ask the codec for a scaled subset
  39. if (!fCodec->getSupportedSubset(&subset)) {
  40. SkCodecPrintf("Error: Could not get subset.\n");
  41. return false;
  42. }
  43. SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset);
  44. // Create the image info for the decode
  45. SkAlphaType dstAlphaType = fCodec->computeOutputAlphaType(requireUnpremul);
  46. SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.height(),
  47. dstColorType, dstAlphaType, dstColorSpace);
  48. // Initialize the destination bitmap
  49. int scaledOutX = 0;
  50. int scaledOutY = 0;
  51. int scaledOutWidth = scaledSize.width();
  52. int scaledOutHeight = scaledSize.height();
  53. if (SubsetType::kPartiallyInside_SubsetType == type) {
  54. scaledOutX = outX / sampleSize;
  55. scaledOutY = outY / sampleSize;
  56. // We need to be safe here because getSupportedSubset() may have modified the subset.
  57. const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width());
  58. const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.height());
  59. const int scaledExtraX = extraX / sampleSize;
  60. const int scaledExtraY = extraY / sampleSize;
  61. scaledOutWidth += scaledOutX + scaledExtraX;
  62. scaledOutHeight += scaledOutY + scaledExtraY;
  63. }
  64. SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight);
  65. if (kGray_8_SkColorType == dstColorType) {
  66. // The legacy implementations of BitmapFactory and BitmapRegionDecoder
  67. // used kAlpha8 for grayscale images (before kGray8 existed). While
  68. // the codec recognizes kGray8, we need to decode into a kAlpha8
  69. // bitmap in order to avoid a behavior change.
  70. outInfo = outInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType);
  71. }
  72. bitmap->setInfo(outInfo);
  73. if (!bitmap->tryAllocPixels(allocator)) {
  74. SkCodecPrintf("Error: Could not allocate pixels.\n");
  75. return false;
  76. }
  77. // Zero the bitmap if the region is not completely within the image.
  78. // TODO (msarett): Can we make this faster by implementing it to only
  79. // zero parts of the image that we won't overwrite with
  80. // pixels?
  81. SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() :
  82. SkCodec::kNo_ZeroInitialized;
  83. if (SubsetType::kPartiallyInside_SubsetType == type &&
  84. SkCodec::kNo_ZeroInitialized == zeroInit) {
  85. void* pixels = bitmap->getPixels();
  86. size_t bytes = outInfo.computeByteSize(bitmap->rowBytes());
  87. memset(pixels, 0, bytes);
  88. }
  89. // Decode into the destination bitmap
  90. SkAndroidCodec::AndroidOptions options;
  91. options.fSampleSize = sampleSize;
  92. options.fSubset = &subset;
  93. options.fZeroInitialized = zeroInit;
  94. void* dst = bitmap->getAddr(scaledOutX, scaledOutY);
  95. SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, bitmap->rowBytes(),
  96. &options);
  97. switch (result) {
  98. case SkCodec::kSuccess:
  99. case SkCodec::kIncompleteInput:
  100. case SkCodec::kErrorInInput:
  101. return true;
  102. default:
  103. SkCodecPrintf("Error: Could not get pixels with message \"%s\".\n",
  104. SkCodec::ResultToString(result));
  105. return false;
  106. }
  107. }