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

Side by Side Diff: tools/SkBitmapRegionCodec.cpp

Issue 1418093006: Refactor SkBitmapRegionDecoderInterface for Android (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 1 month 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
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 "SkBitmapRegionCodec.h" 8 #include "SkBitmapRegionCodec.h"
9 #include "SkAndroidCodec.h" 9 #include "SkAndroidCodec.h"
10 #include "SkCodecPriv.h" 10 #include "SkCodecPriv.h"
11 #include "SkCodecTools.h" 11 #include "SkCodecTools.h"
12 12
13 SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec) 13 SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec)
14 : INHERITED(codec->getInfo().width(), codec->getInfo().height()) 14 : INHERITED(codec->getInfo().width(), codec->getInfo().height())
15 , fCodec(codec) 15 , fCodec(codec)
16 {} 16 {}
17 17
18 /* 18 bool SkBitmapRegionCodec::prepareRegion(const SkIRect& desiredSubset,
19 * Three differences from the Android version: 19 int sampleSize,
20 * Returns a skia bitmap instead of an Android bitmap. 20 SkColorType dstColorType,
21 * Android version attempts to reuse a recycled bitmap. 21 bool requireUnpremul,
22 * Removed the options object and used parameters for color type and sample size. 22 SkImageInfo* outInfo) {
23 */
24 // FIXME: Should this function should take in SkIRect?
25 SkBitmap* SkBitmapRegionCodec::decodeRegion(int inputX, int inputY, int inputWid th, int inputHeight,
26 int sampleSize, SkColorType dstColorType) {
27
28 // Fix the input sampleSize if necessary. 23 // Fix the input sampleSize if necessary.
29 if (sampleSize < 1) { 24 if (sampleSize < 1) {
30 sampleSize = 1; 25 sampleSize = 1;
31 } 26 }
27 fSampleSize = sampleSize;
32 28
33 // The size of the output bitmap is determined by the size of the
34 // requested subset, not by the size of the intersection of the subset
35 // and the image dimensions.
36 // If inputX is negative, we will need to place decoded pixels into the
37 // output bitmap starting at a left offset. Call this outX.
38 // If outX is non-zero, subsetX must be zero.
39 // If inputY is negative, we will need to place decoded pixels into the
40 // output bitmap starting at a top offset. Call this outY.
41 // If outY is non-zero, subsetY must be zero.
42 int outX; 29 int outX;
43 int outY; 30 int outY;
44 SkIRect subset = SkIRect::MakeXYWH(inputX, inputY, inputWidth, inputHeight); 31 fSubset = desiredSubset;
45 SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset , &outX, &outY); 32 fSubsetType = adjust_subset_rect(fCodec->getInfo().dimensions(), &fSubset, & outX, &outY);
scroggo 2015/10/27 15:00:50 If you wanted to, you could put an SkIRect on the
46 if (SubsetType::kOutside_SubsetType == type) { 33 if (SubsetType::kOutside_SubsetType == fSubsetType) {
47 return nullptr; 34 return false;
48 } 35 }
49 36
50 // Ask the codec for a scaled subset 37 // Ask the codec for a scaled subset
51 if (!fCodec->getSupportedSubset(&subset)) { 38 if (!fCodec->getSupportedSubset(&fSubset)) {
52 SkCodecPrintf("Error: Could not get subset.\n"); 39 SkCodecPrintf("Error: Could not get subset.\n");
53 return nullptr; 40 return nullptr;
54 } 41 }
55 SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset); 42 SkISize scaledSize = fCodec->getSampledSubsetDimensions(fSampleSize, fSubset );
56 43
57 // Create the image info for the decode 44 // Create the image info for the decode.
58 SkAlphaType dstAlphaType = fCodec->getInfo().alphaType(); 45 SkAlphaType dstAlphaType = fCodec->getInfo().alphaType();
59 if (kUnpremul_SkAlphaType == dstAlphaType) { 46 if (kOpaque_SkAlphaType != dstAlphaType) {
60 dstAlphaType = kPremul_SkAlphaType; 47 dstAlphaType = requireUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlpha Type;
61 } 48 }
62 SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.he ight(), 49 fDecodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.height(),
63 dstColorType, dstAlphaType); 50 dstColorType, dstAlphaType);
64 51
65 // Construct a color table for the decode if necessary 52 // Calculate the output bitmap dimensions.
66 SkAutoTUnref<SkColorTable> colorTable(nullptr); 53 fScaledOutX = 0;
67 SkPMColor* colorPtr = nullptr; 54 fScaledOutY = 0;
68 int* colorCountPtr = nullptr; 55 int scaledOutWidth = scaledSize.width();
69 int maxColors = 256; 56 int scaledOutHeight = scaledSize.height();
70 SkPMColor colors[256]; 57 if (SubsetType::kPartiallyInside_SubsetType == fSubsetType) {
71 if (kIndex_8_SkColorType == dstColorType) { 58 fScaledOutX = outX / sampleSize;
72 // TODO (msarett): This performs a copy that is unnecessary since 59 fScaledOutY = outY / sampleSize;
73 // we have not yet initialized the color table. 60 // We need to be safe here because getSupportedSubset() may have modifie d the subset.
74 // And then we need to use a const cast to get 61 const int extraX = SkTMax(0, desiredSubset.width() - outX - fSubset.widt h());
75 // a pointer to the color table that we can 62 const int extraY = SkTMax(0, desiredSubset.height() - outY - fSubset.hei ght());
76 // modify during the decode. We could alternatively 63 const int scaledExtraX = extraX / fSampleSize;
77 // perform the decode before creating the bitmap and 64 const int scaledExtraY = extraY / fSampleSize;
78 // the color table. We still would need to copy the 65 scaledOutWidth += fScaledOutX + scaledExtraX;
79 // colors into the color table after the decode. 66 scaledOutHeight += fScaledOutY + scaledExtraY;
80 colorTable.reset(new SkColorTable(colors, maxColors)); 67 }
81 colorPtr = const_cast<SkPMColor*>(colorTable->readColors()); 68 fOutInfo = fDecodeInfo.makeWH(scaledOutWidth, scaledOutHeight);
82 colorCountPtr = &maxColors; 69 *outInfo = fOutInfo;
70
71 return conversion_possible(fOutInfo, fCodec->getInfo());
72 }
73
74 bool SkBitmapRegionCodec::decodeRegion(SkBitmap& bitmap) {
75 if (fOutInfo.isEmpty()) {
76 SkCodecPrintf("Error: Call prepareRegion() first.");
77 return false;
83 } 78 }
84 79
85 // Initialize the destination bitmap 80 if (bitmap.width() < fOutInfo.width() || bitmap.height() < fOutInfo.height() ||
scroggo 2015/10/27 15:00:50 Should this just check whether bitmap.info() == fO
86 SkAutoTDelete<SkBitmap> bitmap(new SkBitmap()); 81 bitmap.colorType() != fOutInfo.colorType()) {
87 int scaledOutX = 0; 82 SkCodecPrintf("Error: Invalid size or color type for input bitmap.");
88 int scaledOutY = 0; 83 return false;
89 int scaledOutWidth = scaledSize.width();
90 int scaledOutHeight = scaledSize.height();
91 if (SubsetType::kPartiallyInside_SubsetType == type) {
92 scaledOutX = outX / sampleSize;
93 scaledOutY = outY / sampleSize;
94 // We need to be safe here because getSupportedSubset() may have modifie d the subset.
95 const int extraX = SkTMax(0, inputWidth - outX - subset.width());
96 const int extraY = SkTMax(0, inputHeight - outY - subset.height());
97 const int scaledExtraX = extraX / sampleSize;
98 const int scaledExtraY = extraY / sampleSize;
99 scaledOutWidth += scaledOutX + scaledExtraX;
100 scaledOutHeight += scaledOutY + scaledExtraY;
101 } 84 }
102 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight); 85 bitmap.setAlphaType(fOutInfo.alphaType());
103 if (!bitmap->tryAllocPixels(outInfo, nullptr, colorTable.get())) {
104 SkCodecPrintf("Error: Could not allocate pixels.\n");
105 return nullptr;
106 }
107 86
108 // Zero the bitmap if the region is not completely within the image. 87 // 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 88 // TODO (msarett): Can we make this faster by implementing it to only
110 // zero parts of the image that we won't overwrite with 89 // zero parts of the image that we won't overwrite with
111 // pixels? 90 // pixels?
112 // TODO (msarett): This could be skipped if memory is zero initialized. 91 // TODO (msarett): This could be skipped if memory is zero initialized.
113 // This would matter if this code is moved to Android and 92 if (SubsetType::kPartiallyInside_SubsetType == fSubsetType) {
114 // uses Android bitmaps. 93 void* pixels = bitmap.getPixels();
115 if (SubsetType::kPartiallyInside_SubsetType == type) { 94 size_t bytes = fOutInfo.getSafeSize(bitmap.rowBytes());
116 void* pixels = bitmap->getPixels();
117 size_t bytes = outInfo.getSafeSize(bitmap->rowBytes());
118 memset(pixels, 0, bytes); 95 memset(pixels, 0, bytes);
119 } 96 }
120 97
121 // Decode into the destination bitmap 98 // Decode into the destination bitmap
122 SkAndroidCodec::AndroidOptions options; 99 SkAndroidCodec::AndroidOptions options;
123 options.fSampleSize = sampleSize; 100 options.fSampleSize = fSampleSize;
124 options.fSubset = &subset; 101 options.fSubset = &fSubset;
125 options.fColorPtr = colorPtr; 102 SkColorTable* colorTable = bitmap.getColorTable();
126 options.fColorCount = colorCountPtr; 103 options.fColorPtr = colorTable ? const_cast<SkPMColor*>(colorTable->readColo rs()) : nullptr;
127 void* dst = bitmap->getAddr(scaledOutX, scaledOutY); 104 int numColors = 0;
128 size_t rowBytes = bitmap->rowBytes(); 105 options.fColorCount = &numColors;
129 SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, rowBytes, &options); 106 void* dst = bitmap.getAddr(fScaledOutX, fScaledOutY);
107 size_t rowBytes = bitmap.rowBytes();
108 SkCodec::Result result = fCodec->getAndroidPixels(fDecodeInfo, dst, rowBytes , &options);
130 if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) { 109 if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) {
131 SkCodecPrintf("Error: Could not get pixels.\n"); 110 SkCodecPrintf("Error: Could not get pixels.\n");
132 return nullptr; 111 return false;
133 } 112 }
134 113
135 return bitmap.detach(); 114 return true;
136 } 115 }
137
138 bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) {
139 // FIXME: Call virtual function when it lands.
140 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fCodec->getInfo().alph aType(),
141 fCodec->getInfo().profileType());
142 return conversion_possible(info, fCodec->getInfo());
143 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698