Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: src/android/SkBitmapRegionCodec.cpp

Issue 1513023002: Make BitmapRegionDecoder succeed on invalid requests (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Update comments Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/android/SkBitmapRegionCanvas.cpp ('k') | src/codec/SkAndroidCodec.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "SkAndroidCodec.h" 8 #include "SkAndroidCodec.h"
9 #include "SkBitmapRegionCodec.h" 9 #include "SkBitmapRegionCodec.h"
10 #include "SkBitmapRegionDecoderPriv.h" 10 #include "SkBitmapRegionDecoderPriv.h"
11 #include "SkCodecPriv.h" 11 #include "SkCodecPriv.h"
12 #include "SkPixelRef.h" 12 #include "SkPixelRef.h"
13 13
14 SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec) 14 SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec)
15 : INHERITED(codec->getInfo().width(), codec->getInfo().height()) 15 : INHERITED(codec->getInfo().width(), codec->getInfo().height())
16 , fCodec(codec) 16 , fCodec(codec)
17 {} 17 {}
18 18
19 bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocat or, 19 bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocat or,
20 const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType, 20 const SkIRect& desiredSubset, int sampleSize, SkColorType prefColorType,
21 bool requireUnpremul) { 21 bool requireUnpremul) {
22 22
23 // Fix the input sampleSize if necessary. 23 // Fix the input sampleSize if necessary.
24 if (sampleSize < 1) { 24 if (sampleSize < 1) {
25 sampleSize = 1; 25 sampleSize = 1;
26 } 26 }
27 27
28 // The size of the output bitmap is determined by the size of the 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 29 // requested subset, not by the size of the intersection of the subset
30 // and the image dimensions. 30 // and the image dimensions.
(...skipping 12 matching lines...) Expand all
43 } 43 }
44 44
45 // Ask the codec for a scaled subset 45 // Ask the codec for a scaled subset
46 if (!fCodec->getSupportedSubset(&subset)) { 46 if (!fCodec->getSupportedSubset(&subset)) {
47 SkCodecPrintf("Error: Could not get subset.\n"); 47 SkCodecPrintf("Error: Could not get subset.\n");
48 return false; 48 return false;
49 } 49 }
50 SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset); 50 SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset);
51 51
52 // Create the image info for the decode 52 // Create the image info for the decode
53 SkAlphaType dstAlphaType = fCodec->getInfo().alphaType(); 53 SkColorType dstColorType = fCodec->computeOutputColorType(prefColorType);
54 if (kOpaque_SkAlphaType != dstAlphaType) { 54 SkAlphaType dstAlphaType = fCodec->computeOutputAlphaType(requireUnpremul);
55 dstAlphaType = requireUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlpha Type;
56 }
57 SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.he ight(), 55 SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.he ight(),
58 dstColorType, dstAlphaType); 56 dstColorType, dstAlphaType);
59 57
60 // Construct a color table for the decode if necessary 58 // Construct a color table for the decode if necessary
61 SkAutoTUnref<SkColorTable> colorTable(nullptr); 59 SkAutoTUnref<SkColorTable> colorTable(nullptr);
62 SkPMColor* colorPtr = nullptr; 60 SkPMColor* colorPtr = nullptr;
63 int* colorCountPtr = nullptr; 61 int* colorCountPtr = nullptr;
64 int maxColors = 256; 62 int maxColors = 256;
65 SkPMColor colors[256]; 63 SkPMColor colors[256];
66 if (kIndex_8_SkColorType == dstColorType) { 64 if (kIndex_8_SkColorType == dstColorType) {
(...skipping 20 matching lines...) Expand all
87 scaledOutY = outY / sampleSize; 85 scaledOutY = outY / sampleSize;
88 // We need to be safe here because getSupportedSubset() may have modifie d the subset. 86 // We need to be safe here because getSupportedSubset() may have modifie d the subset.
89 const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width ()); 87 const int extraX = SkTMax(0, desiredSubset.width() - outX - subset.width ());
90 const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.heig ht()); 88 const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.heig ht());
91 const int scaledExtraX = extraX / sampleSize; 89 const int scaledExtraX = extraX / sampleSize;
92 const int scaledExtraY = extraY / sampleSize; 90 const int scaledExtraY = extraY / sampleSize;
93 scaledOutWidth += scaledOutX + scaledExtraX; 91 scaledOutWidth += scaledOutX + scaledExtraX;
94 scaledOutHeight += scaledOutY + scaledExtraY; 92 scaledOutHeight += scaledOutY + scaledExtraY;
95 } 93 }
96 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight); 94 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight);
95 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 }
97 bitmap->setInfo(outInfo); 102 bitmap->setInfo(outInfo);
98 if (!bitmap->tryAllocPixels(allocator, colorTable.get())) { 103 if (!bitmap->tryAllocPixels(allocator, colorTable.get())) {
99 SkCodecPrintf("Error: Could not allocate pixels.\n"); 104 SkCodecPrintf("Error: Could not allocate pixels.\n");
100 return false; 105 return false;
101 } 106 }
102 107
103 // Zero the bitmap if the region is not completely within the image. 108 // Zero the bitmap if the region is not completely within the image.
104 // TODO (msarett): Can we make this faster by implementing it to only 109 // TODO (msarett): Can we make this faster by implementing it to only
105 // zero parts of the image that we won't overwrite with 110 // zero parts of the image that we won't overwrite with
106 // pixels? 111 // pixels?
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 143
139 return true; 144 return true;
140 } 145 }
141 146
142 bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) { 147 bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) {
143 // FIXME: Call virtual function when it lands. 148 // FIXME: Call virtual function when it lands.
144 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fCodec->getInfo().alph aType(), 149 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fCodec->getInfo().alph aType(),
145 fCodec->getInfo().profileType()); 150 fCodec->getInfo().profileType());
146 return conversion_possible(info, fCodec->getInfo()); 151 return conversion_possible(info, fCodec->getInfo());
147 } 152 }
OLDNEW
« no previous file with comments | « src/android/SkBitmapRegionCanvas.cpp ('k') | src/codec/SkAndroidCodec.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698