blob: be3d5bcce7179dd7e29871ad72f1d602c2d22bd0 [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
msarett26ad17b2015-10-22 07:29:19 -07008#include "SkAndroidCodec.h"
msarett84a80652015-11-10 15:49:46 -08009#include "SkBitmapRegionCodec.h"
10#include "SkBitmapRegionDecoderPriv.h"
msarett26ad17b2015-10-22 07:29:19 -070011#include "SkCodecPriv.h"
msarettfcff08c2015-11-05 15:00:56 -080012#include "SkPixelRef.h"
msarett26ad17b2015-10-22 07:29:19 -070013
14SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec)
15 : INHERITED(codec->getInfo().width(), codec->getInfo().height())
16 , fCodec(codec)
17{}
18
msarettcb8d7192015-11-11 13:30:43 -080019bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocator,
msarett9a0e3462015-12-11 07:38:50 -080020 const SkIRect& desiredSubset, int sampleSize, SkColorType prefColorType,
msarett35e5d1b2015-10-27 12:50:25 -070021 bool requireUnpremul) {
msarett26ad17b2015-10-22 07:29:19 -070022
23 // Fix the input sampleSize if necessary.
24 if (sampleSize < 1) {
25 sampleSize = 1;
26 }
27
28 // The size of the output bitmap is determined by the size of the
29 // requested subset, not by the size of the intersection of the subset
30 // and the image dimensions.
31 // If inputX is negative, we will need to place decoded pixels into the
32 // output bitmap starting at a left offset. Call this outX.
33 // If outX is non-zero, subsetX must be zero.
34 // If inputY is negative, we will need to place decoded pixels into the
35 // output bitmap starting at a top offset. Call this outY.
36 // If outY is non-zero, subsetY must be zero.
37 int outX;
38 int outY;
msarett35e5d1b2015-10-27 12:50:25 -070039 SkIRect subset = desiredSubset;
msarett26ad17b2015-10-22 07:29:19 -070040 SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset, &outX, &outY);
41 if (SubsetType::kOutside_SubsetType == type) {
msarett35e5d1b2015-10-27 12:50:25 -070042 return false;
msarett26ad17b2015-10-22 07:29:19 -070043 }
44
45 // Ask the codec for a scaled subset
46 if (!fCodec->getSupportedSubset(&subset)) {
47 SkCodecPrintf("Error: Could not get subset.\n");
msarett35e5d1b2015-10-27 12:50:25 -070048 return false;
msarett26ad17b2015-10-22 07:29:19 -070049 }
50 SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset);
51
52 // Create the image info for the decode
msarett9a0e3462015-12-11 07:38:50 -080053 SkColorType dstColorType = fCodec->computeOutputColorType(prefColorType);
54 SkAlphaType dstAlphaType = fCodec->computeOutputAlphaType(requireUnpremul);
msarett26ad17b2015-10-22 07:29:19 -070055 SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.height(),
56 dstColorType, dstAlphaType);
57
58 // Construct a color table for the decode if necessary
59 SkAutoTUnref<SkColorTable> colorTable(nullptr);
60 SkPMColor* colorPtr = nullptr;
61 int* colorCountPtr = nullptr;
62 int maxColors = 256;
63 SkPMColor colors[256];
64 if (kIndex_8_SkColorType == dstColorType) {
65 // TODO (msarett): This performs a copy that is unnecessary since
66 // we have not yet initialized the color table.
67 // And then we need to use a const cast to get
68 // a pointer to the color table that we can
69 // modify during the decode. We could alternatively
70 // perform the decode before creating the bitmap and
71 // the color table. We still would need to copy the
72 // colors into the color table after the decode.
73 colorTable.reset(new SkColorTable(colors, maxColors));
74 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
75 colorCountPtr = &maxColors;
76 }
77
78 // Initialize the destination bitmap
msarett26ad17b2015-10-22 07:29:19 -070079 int scaledOutX = 0;
80 int scaledOutY = 0;
81 int scaledOutWidth = scaledSize.width();
82 int scaledOutHeight = scaledSize.height();
83 if (SubsetType::kPartiallyInside_SubsetType == type) {
84 scaledOutX = outX / sampleSize;
85 scaledOutY = outY / sampleSize;
86 // We need to be safe here because getSupportedSubset() may have modified the subset.
msarett35e5d1b2015-10-27 12:50:25 -070087 const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width());
88 const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.height());
msarett26ad17b2015-10-22 07:29:19 -070089 const int scaledExtraX = extraX / sampleSize;
90 const int scaledExtraY = extraY / sampleSize;
91 scaledOutWidth += scaledOutX + scaledExtraX;
92 scaledOutHeight += scaledOutY + scaledExtraY;
93 }
94 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight);
msarett9a0e3462015-12-11 07:38:50 -080095 if (kGray_8_SkColorType == dstColorType) {
96 // The legacy implementations of BitmapFactory and BitmapRegionDecoder
97 // used kAlpha8 for grayscale images (before kGray8 existed). While
98 // the codec recognizes kGray8, we need to decode into a kAlpha8
99 // bitmap in order to avoid a behavior change.
100 outInfo = SkImageInfo::MakeA8(scaledOutWidth, scaledOutHeight);
101 }
msarettfcff08c2015-11-05 15:00:56 -0800102 bitmap->setInfo(outInfo);
msarett35e5d1b2015-10-27 12:50:25 -0700103 if (!bitmap->tryAllocPixels(allocator, colorTable.get())) {
msarett26ad17b2015-10-22 07:29:19 -0700104 SkCodecPrintf("Error: Could not allocate pixels.\n");
msarett35e5d1b2015-10-27 12:50:25 -0700105 return false;
msarett26ad17b2015-10-22 07:29:19 -0700106 }
107
108 // Zero the bitmap if the region is not completely within the image.
109 // TODO (msarett): Can we make this faster by implementing it to only
110 // zero parts of the image that we won't overwrite with
111 // pixels?
msarettcb8d7192015-11-11 13:30:43 -0800112 SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() :
113 SkCodec::kNo_ZeroInitialized;
114 if (SubsetType::kPartiallyInside_SubsetType == type &&
115 SkCodec::kNo_ZeroInitialized == zeroInit) {
msarett26ad17b2015-10-22 07:29:19 -0700116 void* pixels = bitmap->getPixels();
117 size_t bytes = outInfo.getSafeSize(bitmap->rowBytes());
118 memset(pixels, 0, bytes);
119 }
120
121 // Decode into the destination bitmap
122 SkAndroidCodec::AndroidOptions options;
123 options.fSampleSize = sampleSize;
124 options.fSubset = &subset;
125 options.fColorPtr = colorPtr;
126 options.fColorCount = colorCountPtr;
msarettcb8d7192015-11-11 13:30:43 -0800127 options.fZeroInitialized = zeroInit;
msarett26ad17b2015-10-22 07:29:19 -0700128 void* dst = bitmap->getAddr(scaledOutX, scaledOutY);
msarettfcff08c2015-11-05 15:00:56 -0800129
130 // FIXME: skbug.com/4538
131 // It is important that we use the rowBytes on the pixelRef. They may not be
132 // set properly on the bitmap.
133 SkPixelRef* pr = SkRef(bitmap->pixelRef());
134 size_t rowBytes = pr->rowBytes();
135 bitmap->setInfo(outInfo, rowBytes);
136 bitmap->setPixelRef(pr)->unref();
msarett7fdda602015-11-13 05:56:27 -0800137 bitmap->lockPixels();
msarett26ad17b2015-10-22 07:29:19 -0700138 SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, rowBytes, &options);
139 if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) {
140 SkCodecPrintf("Error: Could not get pixels.\n");
msarett35e5d1b2015-10-27 12:50:25 -0700141 return false;
msarett26ad17b2015-10-22 07:29:19 -0700142 }
143
msarett35e5d1b2015-10-27 12:50:25 -0700144 return true;
msarett26ad17b2015-10-22 07:29:19 -0700145}
146
147bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) {
148 // FIXME: Call virtual function when it lands.
149 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fCodec->getInfo().alphaType(),
150 fCodec->getInfo().profileType());
151 return conversion_possible(info, fCodec->getInfo());
152}