blob: cac99452e0d37bcffdf762063bf9e17c7c0515c5 [file] [log] [blame]
msarett26ad17b2015-10-22 07:29:19 -07001/*
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
8#include "SkBitmapRegionCodec.h"
9#include "SkAndroidCodec.h"
10#include "SkCodecPriv.h"
11#include "SkCodecTools.h"
12
13SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec)
14 : INHERITED(codec->getInfo().width(), codec->getInfo().height())
15 , fCodec(codec)
16{}
17
msarett35e5d1b2015-10-27 12:50:25 -070018bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBitmap::Allocator* allocator,
19 const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType,
20 bool requireUnpremul) {
msarett26ad17b2015-10-22 07:29:19 -070021
22 // Fix the input sampleSize if necessary.
23 if (sampleSize < 1) {
24 sampleSize = 1;
25 }
26
27 // The size of the output bitmap is determined by the size of the
28 // requested subset, not by the size of the intersection of the subset
29 // and the image dimensions.
30 // If inputX is negative, we will need to place decoded pixels into the
31 // output bitmap starting at a left offset. Call this outX.
32 // If outX is non-zero, subsetX must be zero.
33 // If inputY is negative, we will need to place decoded pixels into the
34 // output bitmap starting at a top offset. Call this outY.
35 // If outY is non-zero, subsetY must be zero.
36 int outX;
37 int outY;
msarett35e5d1b2015-10-27 12:50:25 -070038 SkIRect subset = desiredSubset;
msarett26ad17b2015-10-22 07:29:19 -070039 SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset, &outX, &outY);
40 if (SubsetType::kOutside_SubsetType == type) {
msarett35e5d1b2015-10-27 12:50:25 -070041 return false;
msarett26ad17b2015-10-22 07:29:19 -070042 }
43
44 // Ask the codec for a scaled subset
45 if (!fCodec->getSupportedSubset(&subset)) {
46 SkCodecPrintf("Error: Could not get subset.\n");
msarett35e5d1b2015-10-27 12:50:25 -070047 return false;
msarett26ad17b2015-10-22 07:29:19 -070048 }
49 SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset);
50
51 // Create the image info for the decode
52 SkAlphaType dstAlphaType = fCodec->getInfo().alphaType();
msarett35e5d1b2015-10-27 12:50:25 -070053 if (kOpaque_SkAlphaType != dstAlphaType) {
54 dstAlphaType = requireUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
msarett26ad17b2015-10-22 07:29:19 -070055 }
56 SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.height(),
57 dstColorType, dstAlphaType);
58
59 // Construct a color table for the decode if necessary
60 SkAutoTUnref<SkColorTable> colorTable(nullptr);
61 SkPMColor* colorPtr = nullptr;
62 int* colorCountPtr = nullptr;
63 int maxColors = 256;
64 SkPMColor colors[256];
65 if (kIndex_8_SkColorType == dstColorType) {
66 // TODO (msarett): This performs a copy that is unnecessary since
67 // we have not yet initialized the color table.
68 // And then we need to use a const cast to get
69 // a pointer to the color table that we can
70 // modify during the decode. We could alternatively
71 // perform the decode before creating the bitmap and
72 // the color table. We still would need to copy the
73 // colors into the color table after the decode.
74 colorTable.reset(new SkColorTable(colors, maxColors));
75 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
76 colorCountPtr = &maxColors;
77 }
78
79 // Initialize the destination bitmap
msarett26ad17b2015-10-22 07:29:19 -070080 int scaledOutX = 0;
81 int scaledOutY = 0;
82 int scaledOutWidth = scaledSize.width();
83 int scaledOutHeight = scaledSize.height();
84 if (SubsetType::kPartiallyInside_SubsetType == type) {
85 scaledOutX = outX / sampleSize;
86 scaledOutY = outY / sampleSize;
87 // We need to be safe here because getSupportedSubset() may have modified the subset.
msarett35e5d1b2015-10-27 12:50:25 -070088 const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width());
89 const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.height());
msarett26ad17b2015-10-22 07:29:19 -070090 const int scaledExtraX = extraX / sampleSize;
91 const int scaledExtraY = extraY / sampleSize;
92 scaledOutWidth += scaledOutX + scaledExtraX;
93 scaledOutHeight += scaledOutY + scaledExtraY;
94 }
95 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight);
msarett35e5d1b2015-10-27 12:50:25 -070096 bitmap->setInfo(outInfo, outInfo.minRowBytes());
97 if (!bitmap->tryAllocPixels(allocator, colorTable.get())) {
msarett26ad17b2015-10-22 07:29:19 -070098 SkCodecPrintf("Error: Could not allocate pixels.\n");
msarett35e5d1b2015-10-27 12:50:25 -070099 return false;
msarett26ad17b2015-10-22 07:29:19 -0700100 }
101
102 // Zero the bitmap if the region is not completely within the image.
103 // TODO (msarett): Can we make this faster by implementing it to only
104 // zero parts of the image that we won't overwrite with
105 // pixels?
106 // TODO (msarett): This could be skipped if memory is zero initialized.
107 // This would matter if this code is moved to Android and
108 // uses Android bitmaps.
109 if (SubsetType::kPartiallyInside_SubsetType == type) {
110 void* pixels = bitmap->getPixels();
111 size_t bytes = outInfo.getSafeSize(bitmap->rowBytes());
112 memset(pixels, 0, bytes);
113 }
114
115 // Decode into the destination bitmap
116 SkAndroidCodec::AndroidOptions options;
117 options.fSampleSize = sampleSize;
118 options.fSubset = &subset;
119 options.fColorPtr = colorPtr;
120 options.fColorCount = colorCountPtr;
121 void* dst = bitmap->getAddr(scaledOutX, scaledOutY);
122 size_t rowBytes = bitmap->rowBytes();
123 SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, rowBytes, &options);
124 if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) {
125 SkCodecPrintf("Error: Could not get pixels.\n");
msarett35e5d1b2015-10-27 12:50:25 -0700126 return false;
msarett26ad17b2015-10-22 07:29:19 -0700127 }
128
msarett35e5d1b2015-10-27 12:50:25 -0700129 return true;
msarett26ad17b2015-10-22 07:29:19 -0700130}
131
132bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) {
133 // FIXME: Call virtual function when it lands.
134 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fCodec->getInfo().alphaType(),
135 fCodec->getInfo().profileType());
136 return conversion_possible(info, fCodec->getInfo());
137}