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

Side by Side Diff: src/utils/SkBitmapRegionCanvas.cpp

Issue 1288963002: Provides multiple implementations of Android's SkBitmapRegionDecoder (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 4 months 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
(Empty)
1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "SkBitmapRegionCanvas.h"
18 #include "SkCanvas.h"
19 #include "SkScanlineDecoder.h"
20
21 SkBitmapRegionCanvas::SkBitmapRegionCanvas(SkScanlineDecoder* decoder)
22 : INHERITED(decoder->getInfo().width(), decoder->getInfo().height())
23 , fDecoder(decoder)
24 {}
25
26 /*
27 * This has several key differences from the Android version:
28 * Returns a Skia bitmap instead of an Android bitmap.
29 * Android version attempts to reuse a recycled bitmap.
30 */
31 SkBitmap* SkBitmapRegionCanvas::decodeRegion(int input_x, int input_y,
32 int input_w, int input_h,
33 int sampleSize,
34 SkColorType colorType) {
scroggo 2015/08/13 16:53:07 nit: Maybe this should be dstColorType?
msarett 2015/08/13 18:10:23 Done.
35 // Reject color types not supported by this method
36 if (kIndex_8_SkColorType == colorType || kGray_8_SkColorType == colorType) {
37 SkDebugf("Error: Color type not supported.\n");
38 return NULL;
39 }
40
41 // Correct the output region if necessary
42 int left = SkTMax(0, input_x);
43 int leftOffset = left - input_x;
44 int top = SkTMax(0, input_y);
45 int topOffset = top - input_y;
46 int width = SkTMin(this->width() - left, input_w - leftOffset);
47 int height = SkTMin(this->height() - top, input_h - topOffset);
48 if (width <= 0 || height <= 0) {
49 SkDebugf("Error: Region must intersect part of the image.\n");
50 return NULL;
51 }
52
53 // Create the image info the decode
54 SkAlphaType alphaType = fDecoder->getInfo().alphaType();
55 if (kUnpremul_SkAlphaType == alphaType) {
56 alphaType = kPremul_SkAlphaType;
57 }
58 SkImageInfo decodeInfo = SkImageInfo::Make(this->width(), this->height(),
59 colorType, alphaType);
60
61 // Start the scanline decoder
62 SkCodec::Result r = fDecoder->start(decodeInfo);
63 if (SkCodec::kSuccess != r) {
64 SkDebugf("Error: Could not start scanline decoder.\n");
65 return NULL;
66 }
67
68 // Allocate a bitmap for the unscaled decode
69 SkBitmap tmp;
70 SkImageInfo tmpInfo = decodeInfo.makeWH(this->width(), height);
71 if (!tmp.tryAllocPixels(tmpInfo)) {
72 SkDebugf("Error: Could not allocate pixels.\n");
73 return NULL;
74 }
75
76 // Skip the unneeded rows
77 SkCodec::Result result =
78 fDecoder->skipScanlines(top);
79 switch (result) {
80 case SkCodec::kSuccess:
81 case SkCodec::kIncompleteInput:
scroggo 2015/08/13 16:53:07 It seems like if we got incomplete at this point,
msarett 2015/08/13 18:10:23 Agreed. Changing this.
82 break;
83 default:
84 SkDebugf("Error: Failed to skip scanlines.\n");
85 return NULL;
86 }
87
88 // Decode the necessary rows
89 result = fDecoder->getScanlines(tmp.getAddr(0, 0), height,
90 tmp.rowBytes());
91 switch (result) {
92 case SkCodec::kSuccess:
93 case SkCodec::kIncompleteInput:
94 break;
95 default:
96 SkDebugf("Error: Failed to get scanlines.\n");
97 return NULL;
98 }
99
100 // Calculate the size of the output
101 int outWidth = SkTMax(1, input_w / sampleSize);
102 int outHeight = SkTMax(1, input_h / sampleSize);
103
104 // Initialize the destination bitmap
105 SkAutoTDelete<SkBitmap> bitmap(SkNEW(SkBitmap));
106 SkImageInfo dstInfo = decodeInfo.makeWH(outWidth, outHeight);
107 if (!bitmap->tryAllocPixels(dstInfo)) {
108 SkDebugf("Error: Could not allocate pixels.\n");
109 return NULL;
110 }
111 bitmap->eraseColor(0);
msarett 2015/08/13 15:16:34 This was necessary for images with transparency.
scroggo 2015/08/13 16:53:07 Seems like a good candidate for a FIXME.
msarett 2015/08/13 18:10:23 Yes on all counts. This has been added. SkFilter
112
113 // Use a canvas to crop and scale to the destination bitmap
114 SkAutoTDelete<SkCanvas> canvas(SkNEW_ARGS(SkCanvas, (*bitmap)));
115 SkRect src = SkRect::MakeXYWH(left, 0, width, height);
116 SkRect dst = SkRect::MakeXYWH(leftOffset / sampleSize, topOffset / sampleSiz e,
117 SkTMax(1, width / sampleSize), SkTMax(1, height / sampleSize));
118 canvas->drawBitmapRect(tmp, src, dst, NULL);
119
120 return bitmap.detach();
121 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698