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 | |
12 | |
13 SkCodec* SkScaledCodec::NewFromStream(SkStream* stream) { | |
14 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream)); | |
scroggo
2015/07/27 15:09:19
This should be indented only four characters.
A l
emmaleer
2015/07/27 18:31:38
Yes, it's usually set to 4 spaced per indent.. I t
| |
15 if (NULL == codec) { | |
16 return NULL; | |
17 } | |
18 return SkNEW_ARGS(SkScaledCodec, (codec.detach())); | |
scroggo
2015/07/27 15:09:19
Since SkWebpCodec supports subsets and scaling nat
emmaleer
2015/07/27 18:31:38
Acknowledged.
| |
19 } | |
20 | |
21 SkCodec* SkScaledCodec::NewFromData(SkData* data) { | |
22 if (!data) { | |
23 return NULL; | |
24 } | |
25 return NewFromStream(SkNEW_ARGS(SkMemoryStream, (data))); | |
26 } | |
27 | |
28 SkScaledCodec::SkScaledCodec(SkCodec* codec) | |
29 : INHERITED(codec->getInfo(), NULL) | |
30 , fCodec(codec) | |
31 {} | |
32 | |
33 SkScaledCodec::~SkScaledCodec() {} | |
34 | |
35 /* | |
36 * Return a valid set of output dimensions for this decoder, given an input scal e | |
37 * TODO: This should be the default implementation once the other codecs support this. | |
scroggo
2015/07/27 15:09:19
This TODO is no longer needed.
emmaleer
2015/07/27 18:31:38
Acknowledged.
| |
38 */ | |
39 SkISize SkScaledCodec::onGetScaledDimensions(float desiredScale) const { | |
40 // support scaling down by integer numbers. Ex: 1/2, 1/3, 1/4 ... | |
41 | |
42 if (desiredScale > 0.5f) { | |
43 // sampleSize = 1 | |
44 return fCodec->getInfo().dimensions(); | |
45 } | |
46 // sampleSize determines the step size between samples | |
47 // Ex: sampleSize = 2, sample every second pixel in x and y directions | |
48 int sampleSize = 1 / desiredScale; | |
49 | |
50 // if sampleSize > width or height, will only sample 1 pixel in that directi on | |
51 int sampleX = SkMin32(sampleSize, this->getInfo().width()); | |
52 int sampleY = SkMin32(sampleSize, this->getInfo().height()); | |
53 | |
54 int scaledWidth = this->getInfo().width() / sampleX; | |
55 int scaledHeight = this->getInfo().height() / sampleY; | |
56 | |
57 // Return the calculated output dimensions for the given scale | |
58 return SkISize::Make(scaledWidth, scaledHeight); | |
59 } | |
60 | |
61 // check if scaling to dstInfo size from srcInfo size is possible | |
62 static bool scaling_supported(const SkImageInfo& dstInfo, const SkImageInfo& sr cInfo){ | |
63 const int dstWidth = dstInfo.width(); | |
64 const int dstHeight = dstInfo.height(); | |
65 const int srcWidth = srcInfo.width(); | |
66 const int srcHeight = srcInfo.height(); | |
67 // only support down sampling, not up sampling | |
68 if (dstWidth > srcWidth || dstHeight > srcHeight) { | |
69 return false; | |
70 } | |
71 // check that srcWidth is scaled down by an integer value | |
72 int sampleSizeX = srcWidth / dstWidth; | |
73 if (srcWidth / sampleSizeX != dstWidth) { | |
74 return false; | |
75 } | |
76 // check that src height is scaled down by an integer value | |
77 int sampleSizeY = srcHeight / dstHeight; | |
78 if (srcHeight / sampleSizeY != dstHeight) { | |
79 return false; | |
80 } | |
81 // sampleX and sampleY should be equal unless the original sampleSize reques ted was larger | |
82 // than srcWidth or srcHeight. If so, the result of this is dstWidth or dstH eight = 1. | |
83 // This functionality allows for tall thin images to still be scaled down by scaling factors. | |
84 if (sampleSizeX != sampleSizeY){ | |
85 if (1 != dstWidth && 1 != dstHeight) { | |
86 return false; | |
87 } | |
88 } | |
89 return true; | |
90 } | |
91 | |
92 SkCodec::Result SkScaledCodec::onGetPixels(const SkImageInfo& requestedInfo, voi d* dst, | |
93 size_t rowBytes, const Options& optio ns, | |
94 SkPMColor ctable[], int* ctableCount) { | |
95 | |
96 Result result = fCodec->getPixels(requestedInfo, dst, rowBytes, &options , ctable, ctableCount); | |
97 if (kInvalidScale != result) { | |
scroggo
2015/07/27 15:09:19
I wonder if there are other cases this should hand
emmaleer
2015/07/27 18:31:38
Maybe we should add a return value of kInvalidSubs
scroggo
2015/07/27 19:29:57
Yes. This is currently supported by BitmapRegionDe
emmaleer
2015/07/28 14:19:16
Acknowledged.
| |
98 // no scaling requested | |
99 return result; | |
100 } | |
101 // scaling requested | |
102 | |
103 if (!scaling_supported(requestedInfo, fCodec->getInfo())) { | |
104 return kInvalidScale; | |
105 } | |
106 | |
107 bool isInterlaced = fCodec->isInterlaced(); | |
108 Options scaledOptions = options; | |
109 scaledOptions.fScaled = true; | |
110 | |
111 int dstHeight = requestedInfo.height(); | |
112 int srcHeight = fCodec->getInfo().height(); | |
113 | |
114 SkImageInfo info = requestedInfo; | |
115 if (isInterlaced){ | |
116 // must decode entire height for interlaced, then scale after | |
117 info = info.makeWH(requestedInfo.width(), srcHeight); | |
118 } | |
119 | |
120 SkAutoTDelete<SkScanlineDecoder> scanlineDecoder(fCodec->getScanlineDeco der(info, | |
121 &scaledOptions, ctable, ctableCount)); | |
122 if (NULL == scanlineDecoder) { | |
123 SkCodecPrintf("failed to create scanline decoder.\n"); | |
124 return kInvalidInput; | |
125 } | |
126 | |
127 // set sample values in x and y directions | |
128 fSampleY = srcHeight / dstHeight; | |
129 fY0 = fSampleY >> 1; | |
130 int sampleX = fCodec->getInfo().width() / requestedInfo.width(); | |
131 if (!scanlineDecoder->setSampleX(sampleX)) { | |
132 return kInvalidScale; | |
133 } | |
134 | |
135 if (!isInterlaced) { | |
scroggo
2015/07/27 15:09:19
nit: I tend to prefer:
if (condition) {
} else {
emmaleer
2015/07/27 18:31:38
Acknowledged.
| |
136 scanlineDecoder->skipScanlines(fY0); | |
137 for (int y = 0; y < dstHeight; y++) { | |
138 scanlineDecoder->getScanlines(dst, 1, rowBytes); | |
139 if (y < dstHeight - 1) { | |
140 scanlineDecoder->skipScanlines(fSampleY - 1); | |
141 } | |
142 dst += rowBytes; | |
143 } | |
144 } else { | |
145 // interlaced | |
146 SkAutoMalloc storage(srcHeight * rowBytes); | |
147 void* storagePtr = storage.get(); | |
148 result = scanlineDecoder->getScanlines(storagePtr, srcHeight, rowBytes); | |
149 | |
150 storagePtr += fY0 * rowBytes; | |
151 for (int y = 0; y < dstHeight; y++) { | |
152 memcpy(dst, storagePtr, rowBytes); | |
153 storagePtr += fSampleY * rowBytes; | |
154 dst += rowBytes; | |
155 } | |
156 } | |
157 return kSuccess; | |
158 } | |
OLD | NEW |