blob: 7ce9d26b4d3ed877e4fd0cf9d59baba8a3fd246e [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,
Matt Sarett68feef42017-04-11 09:51:32 -040021 bool requireUnpremul, sk_sp<SkColorSpace> prefColorSpace) {
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);
Matt Sarett68feef42017-04-11 09:51:32 -040055 sk_sp<SkColorSpace> dstColorSpace = fCodec->computeOutputColorSpace(dstColorType,
56 prefColorSpace);
msarette26a8ad2016-09-01 17:47:46 -070057 SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.height(),
Matt Sarett966bb342016-12-12 16:30:13 -050058 dstColorType, dstAlphaType, dstColorSpace);
msarett26ad17b2015-10-22 07:29:19 -070059
60 // Construct a color table for the decode if necessary
Hal Canary67b39de2016-11-07 11:47:44 -050061 sk_sp<SkColorTable> colorTable(nullptr);
msarett26ad17b2015-10-22 07:29:19 -070062 int maxColors = 256;
63 SkPMColor colors[256];
64 if (kIndex_8_SkColorType == dstColorType) {
msarett26ad17b2015-10-22 07:29:19 -070065 colorTable.reset(new SkColorTable(colors, maxColors));
msarett26ad17b2015-10-22 07:29:19 -070066 }
67
68 // Initialize the destination bitmap
msarett26ad17b2015-10-22 07:29:19 -070069 int scaledOutX = 0;
70 int scaledOutY = 0;
71 int scaledOutWidth = scaledSize.width();
72 int scaledOutHeight = scaledSize.height();
73 if (SubsetType::kPartiallyInside_SubsetType == type) {
74 scaledOutX = outX / sampleSize;
75 scaledOutY = outY / sampleSize;
76 // We need to be safe here because getSupportedSubset() may have modified the subset.
msarett35e5d1b2015-10-27 12:50:25 -070077 const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width());
78 const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.height());
msarett26ad17b2015-10-22 07:29:19 -070079 const int scaledExtraX = extraX / sampleSize;
80 const int scaledExtraY = extraY / sampleSize;
81 scaledOutWidth += scaledOutX + scaledExtraX;
82 scaledOutHeight += scaledOutY + scaledExtraY;
83 }
84 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight);
msarett9a0e3462015-12-11 07:38:50 -080085 if (kGray_8_SkColorType == dstColorType) {
86 // The legacy implementations of BitmapFactory and BitmapRegionDecoder
87 // used kAlpha8 for grayscale images (before kGray8 existed). While
88 // the codec recognizes kGray8, we need to decode into a kAlpha8
89 // bitmap in order to avoid a behavior change.
msarett577967c2016-06-03 08:23:40 -070090 outInfo = outInfo.makeColorType(kAlpha_8_SkColorType).makeAlphaType(kPremul_SkAlphaType);
msarett9a0e3462015-12-11 07:38:50 -080091 }
msarettfcff08c2015-11-05 15:00:56 -080092 bitmap->setInfo(outInfo);
msarett35e5d1b2015-10-27 12:50:25 -070093 if (!bitmap->tryAllocPixels(allocator, colorTable.get())) {
msarett26ad17b2015-10-22 07:29:19 -070094 SkCodecPrintf("Error: Could not allocate pixels.\n");
msarett35e5d1b2015-10-27 12:50:25 -070095 return false;
msarett26ad17b2015-10-22 07:29:19 -070096 }
97
98 // Zero the bitmap if the region is not completely within the image.
99 // TODO (msarett): Can we make this faster by implementing it to only
100 // zero parts of the image that we won't overwrite with
101 // pixels?
msarettcb8d7192015-11-11 13:30:43 -0800102 SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() :
103 SkCodec::kNo_ZeroInitialized;
104 if (SubsetType::kPartiallyInside_SubsetType == type &&
105 SkCodec::kNo_ZeroInitialized == zeroInit) {
msarett26ad17b2015-10-22 07:29:19 -0700106 void* pixels = bitmap->getPixels();
107 size_t bytes = outInfo.getSafeSize(bitmap->rowBytes());
108 memset(pixels, 0, bytes);
109 }
110
111 // Decode into the destination bitmap
112 SkAndroidCodec::AndroidOptions options;
113 options.fSampleSize = sampleSize;
114 options.fSubset = &subset;
msarettb1be46b2016-05-17 08:52:11 -0700115 options.fColorPtr = colors;
116 options.fColorCount = &maxColors;
msarettcb8d7192015-11-11 13:30:43 -0800117 options.fZeroInitialized = zeroInit;
msarett26ad17b2015-10-22 07:29:19 -0700118 void* dst = bitmap->getAddr(scaledOutX, scaledOutY);
msarettfcff08c2015-11-05 15:00:56 -0800119
msarett3d477322016-05-19 07:50:24 -0700120 SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, bitmap->rowBytes(),
121 &options);
msarett26ad17b2015-10-22 07:29:19 -0700122 if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) {
123 SkCodecPrintf("Error: Could not get pixels.\n");
msarett35e5d1b2015-10-27 12:50:25 -0700124 return false;
msarett26ad17b2015-10-22 07:29:19 -0700125 }
126
msarettb1be46b2016-05-17 08:52:11 -0700127 // Intialize the color table
128 if (kIndex_8_SkColorType == dstColorType) {
129 colorTable->dangerous_overwriteColors(colors, maxColors);
130 }
131
msarett35e5d1b2015-10-27 12:50:25 -0700132 return true;
msarett26ad17b2015-10-22 07:29:19 -0700133}
134
135bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) {
Matt Sarett966bb342016-12-12 16:30:13 -0500136 SkImageInfo dstInfo = fCodec->getInfo().makeColorType(colorType);
Matt Saretta225e9b2016-11-07 10:26:17 -0500137 return conversion_possible(dstInfo, fCodec->getInfo());
msarett26ad17b2015-10-22 07:29:19 -0700138}