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

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

Issue 1438873002: Move SkBitmapRegionDecoder to include/android and src/android (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rebase (header guards) 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
« no previous file with comments | « tools/android/SkBitmapRegionCodec.h ('k') | tools/android/SkBitmapRegionDecoder.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
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 "SkAndroidCodec.h"
9 #include "SkBitmapRegionCodec.h"
10 #include "SkBitmapRegionDecoderPriv.h"
11 #include "SkCodecPriv.h"
12 #include "SkPixelRef.h"
13
14 SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec)
15 : INHERITED(codec->getInfo().width(), codec->getInfo().height())
16 , fCodec(codec)
17 {}
18
19 bool SkBitmapRegionCodec::decodeRegion(SkBitmap* bitmap, SkBRDAllocator* allocat or,
20 const SkIRect& desiredSubset, int sampleSize, SkColorType dstColorType,
21 bool requireUnpremul) {
22
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;
39 SkIRect subset = desiredSubset;
40 SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset , &outX, &outY);
41 if (SubsetType::kOutside_SubsetType == type) {
42 return false;
43 }
44
45 // Ask the codec for a scaled subset
46 if (!fCodec->getSupportedSubset(&subset)) {
47 SkCodecPrintf("Error: Could not get subset.\n");
48 return false;
49 }
50 SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset);
51
52 // Create the image info for the decode
53 SkAlphaType dstAlphaType = fCodec->getInfo().alphaType();
54 if (kOpaque_SkAlphaType != dstAlphaType) {
55 dstAlphaType = requireUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlpha Type;
56 }
57 SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.he ight(),
58 dstColorType, dstAlphaType);
59
60 // Construct a color table for the decode if necessary
61 SkAutoTUnref<SkColorTable> colorTable(nullptr);
62 SkPMColor* colorPtr = nullptr;
63 int* colorCountPtr = nullptr;
64 int maxColors = 256;
65 SkPMColor colors[256];
66 if (kIndex_8_SkColorType == dstColorType) {
67 // TODO (msarett): This performs a copy that is unnecessary since
68 // we have not yet initialized the color table.
69 // And then we need to use a const cast to get
70 // a pointer to the color table that we can
71 // modify during the decode. We could alternatively
72 // perform the decode before creating the bitmap and
73 // the color table. We still would need to copy the
74 // colors into the color table after the decode.
75 colorTable.reset(new SkColorTable(colors, maxColors));
76 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
77 colorCountPtr = &maxColors;
78 }
79
80 // Initialize the destination bitmap
81 int scaledOutX = 0;
82 int scaledOutY = 0;
83 int scaledOutWidth = scaledSize.width();
84 int scaledOutHeight = scaledSize.height();
85 if (SubsetType::kPartiallyInside_SubsetType == type) {
86 scaledOutX = outX / sampleSize;
87 scaledOutY = outY / sampleSize;
88 // 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 ());
90 const int extraY = SkTMax(0, desiredSubset.height() - outY - subset.heig ht());
91 const int scaledExtraX = extraX / sampleSize;
92 const int scaledExtraY = extraY / sampleSize;
93 scaledOutWidth += scaledOutX + scaledExtraX;
94 scaledOutHeight += scaledOutY + scaledExtraY;
95 }
96 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight);
97 bitmap->setInfo(outInfo);
98 if (!bitmap->tryAllocPixels(allocator, colorTable.get())) {
99 SkCodecPrintf("Error: Could not allocate pixels.\n");
100 return false;
101 }
102
103 // 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
105 // zero parts of the image that we won't overwrite with
106 // pixels?
107 SkCodec::ZeroInitialized zeroInit = allocator ? allocator->zeroInit() :
108 SkCodec::kNo_ZeroInitialized;
109 if (SubsetType::kPartiallyInside_SubsetType == type &&
110 SkCodec::kNo_ZeroInitialized == zeroInit) {
111 void* pixels = bitmap->getPixels();
112 size_t bytes = outInfo.getSafeSize(bitmap->rowBytes());
113 memset(pixels, 0, bytes);
114 }
115
116 // Decode into the destination bitmap
117 SkAndroidCodec::AndroidOptions options;
118 options.fSampleSize = sampleSize;
119 options.fSubset = &subset;
120 options.fColorPtr = colorPtr;
121 options.fColorCount = colorCountPtr;
122 options.fZeroInitialized = zeroInit;
123 void* dst = bitmap->getAddr(scaledOutX, scaledOutY);
124
125 // FIXME: skbug.com/4538
126 // It is important that we use the rowBytes on the pixelRef. They may not b e
127 // set properly on the bitmap.
128 SkPixelRef* pr = SkRef(bitmap->pixelRef());
129 size_t rowBytes = pr->rowBytes();
130 bitmap->setInfo(outInfo, rowBytes);
131 bitmap->setPixelRef(pr)->unref();
132 SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, rowBytes, &options);
133 if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) {
134 SkCodecPrintf("Error: Could not get pixels.\n");
135 return false;
136 }
137
138 return true;
139 }
140
141 bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) {
142 // FIXME: Call virtual function when it lands.
143 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fCodec->getInfo().alph aType(),
144 fCodec->getInfo().profileType());
145 return conversion_possible(info, fCodec->getInfo());
146 }
OLDNEW
« no previous file with comments | « tools/android/SkBitmapRegionCodec.h ('k') | tools/android/SkBitmapRegionDecoder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698