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

Side by Side Diff: tools/SkBitmapRegionCodec.cpp

Issue 1402863002: Implementation of SkBitmapRegionDecoder using SkAndroidCodec (Closed) Base URL: https://skia.googlesource.com/skia.git@split1
Patch Set: Add AndroidCodec strategy to benchmark 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/SkBitmapRegionCodec.h ('k') | tools/SkBitmapRegionDecoderInterface.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 "SkBitmapRegionCodec.h"
9 #include "SkAndroidCodec.h"
10 #include "SkCodecPriv.h"
11 #include "SkCodecTools.h"
12
13 SkBitmapRegionCodec::SkBitmapRegionCodec(SkAndroidCodec* codec)
14 : INHERITED(codec->getInfo().width(), codec->getInfo().height())
15 , fCodec(codec)
16 {}
17
18 /*
19 * Three differences from the Android version:
20 * Returns a skia bitmap instead of an Android bitmap.
21 * Android version attempts to reuse a recycled bitmap.
22 * Removed the options object and used parameters for color type and sample size.
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.
29 if (sampleSize < 1) {
30 sampleSize = 1;
31 }
32
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;
43 int outY;
44 SkIRect subset = SkIRect::MakeXYWH(inputX, inputY, inputWidth, inputHeight);
45 SubsetType type = adjust_subset_rect(fCodec->getInfo().dimensions(), &subset , &outX, &outY);
46 if (SubsetType::kOutside_SubsetType == type) {
47 return nullptr;
48 }
49
50 // Ask the codec for a scaled subset
51 if (!fCodec->getSupportedSubset(&subset)) {
52 SkCodecPrintf("Error: Could not get subset.\n");
53 return nullptr;
54 }
55 SkISize scaledSize = fCodec->getSampledSubsetDimensions(sampleSize, subset);
56
57 // Create the image info for the decode
58 SkAlphaType dstAlphaType = fCodec->getInfo().alphaType();
59 if (kUnpremul_SkAlphaType == dstAlphaType) {
60 dstAlphaType = kPremul_SkAlphaType;
61 }
62 SkImageInfo decodeInfo = SkImageInfo::Make(scaledSize.width(), scaledSize.he ight(),
63 dstColorType, dstAlphaType);
64
65 // Construct a color table for the decode if necessary
66 SkAutoTUnref<SkColorTable> colorTable(nullptr);
67 SkPMColor* colorPtr = nullptr;
68 int* colorCountPtr = nullptr;
69 int maxColors = 256;
70 SkPMColor colors[256];
71 if (kIndex_8_SkColorType == dstColorType) {
72 // TODO (msarett): This performs a copy that is unnecessary since
73 // we have not yet initialized the color table.
74 // And then we need to use a const cast to get
75 // a pointer to the color table that we can
76 // modify during the decode. We could alternatively
77 // perform the decode before creating the bitmap and
78 // the color table. We still would need to copy the
79 // colors into the color table after the decode.
80 colorTable.reset(new SkColorTable(colors, maxColors));
81 colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
82 colorCountPtr = &maxColors;
83 }
84
85 // Initialize the destination bitmap
86 SkAutoTDelete<SkBitmap> bitmap(new SkBitmap());
87 int scaledOutX = 0;
88 int scaledOutY = 0;
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 }
102 SkImageInfo outInfo = decodeInfo.makeWH(scaledOutWidth, scaledOutHeight);
103 if (!bitmap->tryAllocPixels(outInfo, nullptr, colorTable.get())) {
104 SkCodecPrintf("Error: Could not allocate pixels.\n");
105 return nullptr;
106 }
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?
112 // TODO (msarett): This could be skipped if memory is zero initialized.
113 // This would matter if this code is moved to Android and
114 // uses Android bitmaps.
115 if (SubsetType::kPartiallyInside_SubsetType == type) {
116 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;
127 void* dst = bitmap->getAddr(scaledOutX, scaledOutY);
128 size_t rowBytes = bitmap->rowBytes();
129 SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, rowBytes, &options);
130 if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) {
131 SkCodecPrintf("Error: Could not get pixels.\n");
132 return nullptr;
133 }
134
135 return bitmap.detach();
136 }
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
« no previous file with comments | « tools/SkBitmapRegionCodec.h ('k') | tools/SkBitmapRegionDecoderInterface.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698