OLD | NEW |
---|---|
(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 "SkCodecPriv.h" | |
9 #include "SkScaledCodec.h" | |
10 #include "SkStream.h" | |
11 #include "SkWebpCodec.h" | |
12 | |
13 | |
14 SkCodec* SkScaledCodec::NewFromStream(SkStream* stream) { | |
15 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream)); | |
16 if (NULL == codec) { | |
17 return NULL; | |
18 } | |
19 if (codec->getEncodedFormat() == SkEncodedFormat::kWEBP_SkEncodedFormat) { | |
20 // Webp codec supports scaling and subsetting natively | |
21 return codec.detach(); | |
22 } | |
23 // wrap in new SkScaledCodec | |
24 return SkNEW_ARGS(SkScaledCodec, (codec.detach())); | |
25 } | |
26 | |
27 SkCodec* SkScaledCodec::NewFromData(SkData* data) { | |
28 if (!data) { | |
29 return NULL; | |
30 } | |
31 return NewFromStream(SkNEW_ARGS(SkMemoryStream, (data))); | |
32 } | |
33 | |
34 SkScaledCodec::SkScaledCodec(SkCodec* codec) | |
35 : INHERITED(codec->getInfo(), NULL) | |
36 , fCodec(codec) | |
37 {} | |
38 | |
39 SkScaledCodec::~SkScaledCodec() {} | |
40 | |
41 // returns a scaled dimension based on the original dimension and the sampleSize | |
42 // NOTE: we round down here for scaled dimension to match the behavior of SkImag eDecoder | |
43 static int get_scaled_dimension(int srcDimension, int sampleSize) { | |
44 if (sampleSize > srcDimension) { | |
45 return 1; | |
46 } | |
47 return srcDimension / sampleSize; | |
48 } | |
49 | |
50 static SkISize best_scaled_dimensions(const SkISize& origDims, const SkISize& na tiveDims, | |
51 const SkISize& scaledCodecDims, float desi redScale) { | |
52 if (nativeDims == scaledCodecDims) { | |
53 // does not matter which to return if equal. Return here to skip below c alculations | |
54 return nativeDims; | |
55 } | |
56 float idealWidth = origDims.width() * desiredScale; | |
57 float idealHeight = origDims.height() * desiredScale; | |
58 | |
59 // calculate difference between native dimensions and ideal dimensions | |
60 float nativeWDiff = SkTAbs(idealWidth - nativeDims.width()); | |
61 float nativeHDiff = SkTAbs(idealHeight - nativeDims.height()); | |
62 float nativeDiff = (nativeWDiff + nativeHDiff) / 2; | |
63 | |
64 // calculate difference between scaledCodec dimensions and ideal dimensions | |
65 float scaledCodecWDiff = SkTAbs(idealWidth - scaledCodecDims.width()); | |
66 float scaledCodecHDiff = SkTAbs(idealHeight - scaledCodecDims.height()); | |
67 float scaledCodecDiff = (scaledCodecWDiff + scaledCodecHDiff) / 2; | |
68 | |
69 // return dimensions closest to ideal dimensions. | |
70 // If the differences are equal, return nativeDims, as native scaling is mor e efficient. | |
71 return nativeDiff > scaledCodecDiff ? scaledCodecDims : nativeDims; | |
72 | |
73 } | |
74 /* | |
75 * Return a valid set of output dimensions for this decoder, given an input scal e | |
76 */ | |
77 SkISize SkScaledCodec::onGetScaledDimensions(float desiredScale) const { | |
78 SkISize nativeDimensions = fCodec->getScaledDimensions(desiredScale); | |
79 // support scaling down by integer numbers. Ex: 1/2, 1/3, 1/4 ... | |
80 SkISize scaledCodecDimensions; | |
81 if (desiredScale > 0.5f) { | |
82 // sampleSize = 1 | |
83 scaledCodecDimensions = fCodec->getInfo().dimensions(); | |
84 } | |
85 // sampleSize determines the step size between samples | |
86 // Ex: sampleSize = 2, sample every second pixel in x and y directions | |
87 int sampleSize = 1 / desiredScale; | |
88 | |
89 int scaledWidth = get_scaled_dimension(this->getInfo().width(), sampleSize); | |
90 int scaledHeight = get_scaled_dimension(this->getInfo().height(), sampleSize ); | |
91 | |
92 // Return the calculated output dimensions for the given scale | |
93 scaledCodecDimensions = SkISize::Make(scaledWidth, scaledHeight); | |
94 | |
95 return best_scaled_dimensions(this->getInfo().dimensions(), nativeDimensions , | |
96 scaledCodecDimensions, desiredScale); | |
97 } | |
98 | |
99 // check if scaling to dstInfo size from srcInfo size is possible | |
100 static bool scaling_supported(const SkImageInfo& dstInfo, const SkImageInfo& sr cInfo){ | |
101 const int dstWidth = dstInfo.width(); | |
102 const int dstHeight = dstInfo.height(); | |
103 const int srcWidth = srcInfo.width(); | |
104 const int srcHeight = srcInfo.height(); | |
105 // only support down sampling, not up sampling | |
106 if (dstWidth > srcWidth || dstHeight > srcHeight) { | |
107 return false; | |
108 } | |
109 // check that srcWidth is scaled down by an integer value | |
110 int sampleSizeX = SkScaledCodec::GetSampleSize(srcWidth, dstWidth); | |
111 if (get_scaled_dimension(srcWidth, sampleSizeX) != dstWidth) { | |
112 return false; | |
113 } | |
114 // check that src height is scaled down by an integer value | |
115 int sampleSizeY = SkScaledCodec::GetSampleSize(srcHeight, dstHeight); | |
116 if (get_scaled_dimension(srcHeight, sampleSizeY) != dstHeight) { | |
117 return false; | |
118 } | |
119 // sampleX and sampleY should be equal unless the original sampleSize reques ted was larger | |
120 // than srcWidth or srcHeight. If so, the result of this is dstWidth or dstH eight = 1. | |
121 // This functionality allows for tall thin images to still be scaled down by scaling factors. | |
122 if (sampleSizeX != sampleSizeY){ | |
123 if (1 != dstWidth && 1 != dstHeight) { | |
124 return false; | |
125 } | |
126 } | |
127 return true; | |
128 } | |
129 | |
130 // TODO: Implement subsetting in onGetPixels which works when and when not sampl ing | |
131 | |
132 SkCodec::Result SkScaledCodec::onGetPixels(const SkImageInfo& requestedInfo, voi d* dst, | |
133 size_t rowBytes, const Options& optio ns, | |
134 SkPMColor ctable[], int* ctableCount) { | |
135 | |
136 // try to do a fCodec native decode, as some types of code support scaling a nd subsetting | |
137 Result result = fCodec->getPixels(requestedInfo, dst, rowBytes, &options, ct able, ctableCount); | |
138 if (kInvalidScale != result) { | |
139 // no scaling requested | |
140 return result; | |
141 } | |
142 // scaling requested | |
143 if (!scaling_supported(requestedInfo, fCodec->getInfo())) { | |
144 return kInvalidScale; | |
145 } | |
146 | |
147 int dstHeight = requestedInfo.height(); | |
148 int srcHeight = fCodec->getInfo().height(); | |
149 | |
150 // set sample values in x and y directions | |
151 int sampleY = GetSampleSize(srcHeight, dstHeight); | |
152 int Y0 = sampleY >> 1; | |
153 | |
154 SkImageInfo info = requestedInfo; | |
155 // attempt to partially natively scale | |
156 int newSampleSize = fCodec->partiallyNativelyScale(1 / (float)sampleY); | |
157 if (newSampleSize != -1) { | |
158 // partial scale supported | |
159 sampleY = newSampleSize; | |
160 Y0 = sampleY >> 1; | |
161 // update info for partial native scaling | |
162 info = info.makeWH(requestedInfo.width(), requestedInfo.height()); | |
163 } else { | |
164 // use original height as scanlineDecoder does not support y sampling na tively | |
165 info = info.makeWH(requestedInfo.width(), srcHeight); | |
166 } | |
167 | |
168 SkAutoTDelete<SkScanlineDecoder> scanlineDecoder(fCodec->getScanlineDecoder( info, | |
169 &options, ctable, ctableCou nt)); | |
170 | |
171 if (NULL == scanlineDecoder) { | |
172 SkCodecPrintf("failed to create scanline decoder.\n"); | |
173 return kInvalidInput; | |
174 } | |
175 | |
176 const bool requiresPostYSampling = scanlineDecoder->requiresPostYSampling(); | |
177 | |
178 if (requiresPostYSampling) { | |
179 SkAutoMalloc storage(srcHeight * rowBytes); | |
180 void* storagePtr = storage.get(); | |
181 result = scanlineDecoder->getScanlines(storagePtr, srcHeight, rowBytes); | |
182 if (kSuccess != result) { | |
183 return result; | |
184 } | |
185 storagePtr += Y0 * rowBytes; | |
186 for (int y = 0; y < dstHeight; y++) { | |
187 memcpy(dst, storagePtr, rowBytes); | |
188 storagePtr += sampleY * rowBytes; | |
189 dst += rowBytes; | |
190 } | |
191 } else { | |
192 // does not require post y sampling | |
193 scanlineDecoder->skipScanlines(Y0); | |
scroggo
2015/08/04 20:05:21
Can't this return a result other than success? I t
emmaleer
2015/08/05 14:47:51
Acknowledged.
| |
194 for (int y = 0; y < dstHeight; y++) { | |
195 result = scanlineDecoder->getScanlines(dst, 1, rowBytes); | |
196 if (kSuccess != result) { | |
197 return result; | |
198 } | |
199 if (y < dstHeight - 1) { | |
200 result = scanlineDecoder->skipScanlines(sampleY - 1); | |
201 if (kSuccess != result) { | |
202 return result; | |
203 } | |
204 } | |
205 dst += rowBytes; | |
206 } | |
207 } | |
208 return kSuccess; | |
209 } | |
OLD | NEW |