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

Side by Side Diff: tools/SkBitmapRegionCodec.cpp

Issue 1417393004: Reorganize BRD code in new tools directory (Closed) Base URL: https://skia.googlesource.com/skia.git@delete-imdec
Patch Set: tools/android 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/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 "SkBitmapRegionCodec.h"
9 #include "SkAndroidCodec.h"
10 #include "SkCodecPriv.h"
11 #include "SkCodecTools.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, SkBitmap::Allocator* al locator,
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 // TODO (msarett): This could be skipped if memory is zero initialized.
108 // This would matter if this code is moved to Android and
109 // uses Android bitmaps.
110 if (SubsetType::kPartiallyInside_SubsetType == type) {
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 void* dst = bitmap->getAddr(scaledOutX, scaledOutY);
123
124 // FIXME: skbug.com/4538
125 // It is important that we use the rowBytes on the pixelRef. They may not b e
126 // set properly on the bitmap.
127 SkPixelRef* pr = SkRef(bitmap->pixelRef());
128 size_t rowBytes = pr->rowBytes();
129 bitmap->setInfo(outInfo, rowBytes);
130 bitmap->setPixelRef(pr)->unref();
131 SkCodec::Result result = fCodec->getAndroidPixels(decodeInfo, dst, rowBytes, &options);
132 if (SkCodec::kSuccess != result && SkCodec::kIncompleteInput != result) {
133 SkCodecPrintf("Error: Could not get pixels.\n");
134 return false;
135 }
136
137 return true;
138 }
139
140 bool SkBitmapRegionCodec::conversionSupported(SkColorType colorType) {
141 // FIXME: Call virtual function when it lands.
142 SkImageInfo info = SkImageInfo::Make(0, 0, colorType, fCodec->getInfo().alph aType(),
143 fCodec->getInfo().profileType());
144 return conversion_possible(info, fCodec->getInfo());
145 }
OLDNEW
« no previous file with comments | « tools/SkBitmapRegionCodec.h ('k') | tools/SkBitmapRegionDecoder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698