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 /* | |
42 * Return a valid set of output dimensions for this decoder, given an input scal e | |
43 */ | |
44 SkISize SkScaledCodec::onGetScaledDimensions(float desiredScale) const { | |
45 // support scaling down by integer numbers. Ex: 1/2, 1/3, 1/4 ... | |
46 | |
47 if (desiredScale > 0.5f) { | |
48 // sampleSize = 1 | |
49 return fCodec->getInfo().dimensions(); | |
50 } | |
51 // sampleSize determines the step size between samples | |
52 // Ex: sampleSize = 2, sample every second pixel in x and y directions | |
53 int sampleSize = 1 / desiredScale; | |
54 | |
55 // if sampleSize > width or height, will only sample 1 pixel in that directi on | |
56 int sampleX = SkMin32(sampleSize, this->getInfo().width()); | |
57 int sampleY = SkMin32(sampleSize, this->getInfo().height()); | |
58 | |
59 // NOTE: we round down here for scaledWidth and scaledHeight | |
60 // Can be changed in the future to round up, or round to closest integer if we want | |
msarett
2015/07/30 13:55:38
I think Leon made a good point here. I would say
emmaleer
2015/07/30 17:50:39
Okay, I've create the function get_scaled_dimensio
| |
61 int scaledWidth = this->getInfo().width() / sampleX; | |
62 int scaledHeight = this->getInfo().height() / sampleY; | |
63 | |
64 // Return the calculated output dimensions for the given scale | |
65 return SkISize::Make(scaledWidth, scaledHeight); | |
66 } | |
67 | |
68 // check if scaling to dstInfo size from srcInfo size is possible | |
69 static bool scaling_supported(const SkImageInfo& dstInfo, const SkImageInfo& sr cInfo){ | |
70 const int dstWidth = dstInfo.width(); | |
71 const int dstHeight = dstInfo.height(); | |
72 const int srcWidth = srcInfo.width(); | |
73 const int srcHeight = srcInfo.height(); | |
74 // only support down sampling, not up sampling | |
75 if (dstWidth > srcWidth || dstHeight > srcHeight) { | |
76 return false; | |
77 } | |
78 // check that srcWidth is scaled down by an integer value | |
79 int sampleSizeX = srcWidth / dstWidth; | |
80 if (srcWidth / sampleSizeX != dstWidth) { | |
81 return false; | |
82 } | |
83 // check that src height is scaled down by an integer value | |
84 int sampleSizeY = srcHeight / dstHeight; | |
85 if (srcHeight / sampleSizeY != dstHeight) { | |
86 return false; | |
87 } | |
88 // sampleX and sampleY should be equal unless the original sampleSize reques ted was larger | |
89 // than srcWidth or srcHeight. If so, the result of this is dstWidth or dstH eight = 1. | |
90 // This functionality allows for tall thin images to still be scaled down by scaling factors. | |
91 if (sampleSizeX != sampleSizeY){ | |
92 if (1 != dstWidth && 1 != dstHeight) { | |
93 return false; | |
94 } | |
95 } | |
96 return true; | |
97 } | |
98 | |
99 // TODO: Implement subsetting in onGetPixels which works when and when not sampl ing | |
100 | |
101 SkCodec::Result SkScaledCodec::onGetPixels(const SkImageInfo& requestedInfo, voi d* dst, | |
102 size_t rowBytes, const Options& optio ns, | |
103 SkPMColor ctable[], int* ctableCount) { | |
104 | |
105 // try to do a fCodec native decode, as some types of code support scaling a nd subsetting | |
106 Result result = fCodec->getPixels(requestedInfo, dst, rowBytes, &options, ct able, ctableCount); | |
107 if (kInvalidScale != result) { | |
108 // no scaling requested | |
109 return result; | |
110 } | |
111 // scaling requested | |
112 if (!scaling_supported(requestedInfo, fCodec->getInfo())) { | |
113 return kInvalidScale; | |
114 } | |
115 | |
116 int dstHeight = requestedInfo.height(); | |
117 int srcHeight = fCodec->getInfo().height(); | |
118 | |
119 // set sample values in x and y directions | |
120 int sampleY = srcHeight / dstHeight; | |
121 int Y0 = sampleY >> 1; | |
122 | |
123 SkImageInfo info = requestedInfo; | |
124 // use original height as scanlineDecoder does not support y sampling native ly | |
125 info = info.makeWH(requestedInfo.width(), srcHeight); | |
126 | |
127 SkAutoTDelete<SkScanlineDecoder> scanlineDecoder(fCodec->getScanlineDecoder( info, | |
128 &options, ctable, ctableCou nt)); | |
129 if (NULL == scanlineDecoder) { | |
130 SkCodecPrintf("failed to create scanline decoder.\n"); | |
131 return kInvalidInput; | |
132 } | |
133 | |
134 bool isHardToSample = scanlineDecoder->isHardToSample(); | |
135 | |
136 if (isHardToSample) { | |
137 SkAutoMalloc storage(srcHeight * rowBytes); | |
138 void* storagePtr = storage.get(); | |
139 scanlineDecoder->getScanlines(storagePtr, srcHeight, rowBytes); | |
scroggo
2015/07/30 17:53:01
In one sense, this is correct, to remove result if
emmaleer
2015/07/30 22:27:56
Acknowledged.
| |
140 | |
141 storagePtr += Y0 * rowBytes; | |
142 for (int y = 0; y < dstHeight; y++) { | |
143 memcpy(dst, storagePtr, rowBytes); | |
144 storagePtr += sampleY * rowBytes; | |
145 dst += rowBytes; | |
146 } | |
147 } else { | |
148 // not interlaced | |
msarett
2015/07/30 13:55:38
nit: change comment
emmaleer
2015/07/30 17:50:39
Acknowledged.
| |
149 scanlineDecoder->skipScanlines(Y0); | |
150 for (int y = 0; y < dstHeight; y++) { | |
151 scanlineDecoder->getScanlines(dst, 1, rowBytes); | |
152 if (y < dstHeight - 1) { | |
153 scanlineDecoder->skipScanlines(sampleY - 1); | |
154 } | |
155 dst += rowBytes; | |
156 } | |
157 } | |
158 return kSuccess; | |
159 } | |
OLD | NEW |