Chromium Code Reviews| 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 bool isWebp = SkWebpCodec::IsWebp(stream); | |
| 16 if (!stream->rewind()) { | |
| 17 return NULL; | |
| 18 } | |
| 19 if (isWebp) { | |
| 20 // Webp codec supports scaling and subsetting natively | |
| 21 return SkWebpCodec::NewFromStream(stream); | |
| 22 } | |
| 23 | |
| 24 SkAutoTDelete<SkScanlineDecoder> scanlineDecoder(SkScanlineDecoder::NewFromS tream(stream)); | |
| 25 if (NULL == scanlineDecoder) { | |
| 26 return NULL; | |
| 27 } | |
| 28 | |
| 29 // wrap in new SkScaledCodec | |
| 30 return SkNEW_ARGS(SkScaledCodec, (scanlineDecoder.detach())); | |
| 31 } | |
| 32 | |
| 33 SkCodec* SkScaledCodec::NewFromData(SkData* data) { | |
| 34 if (!data) { | |
| 35 return NULL; | |
| 36 } | |
| 37 return NewFromStream(SkNEW_ARGS(SkMemoryStream, (data))); | |
| 38 } | |
| 39 | |
| 40 SkScaledCodec::SkScaledCodec(SkScanlineDecoder* scanlineDecoder) | |
| 41 : INHERITED(scanlineDecoder->getInfo(), NULL) | |
| 42 , fScanlineDecoder(scanlineDecoder) | |
| 43 {} | |
| 44 | |
| 45 SkScaledCodec::~SkScaledCodec() {} | |
| 46 | |
| 47 // returns a scaled dimension based on the original dimension and the sampleSize | |
| 48 // NOTE: we round down here for scaled dimension to match the behavior of SkImag eDecoder | |
| 49 static int get_scaled_dimension(int srcDimension, int sampleSize) { | |
| 50 if (sampleSize > srcDimension) { | |
| 51 return 1; | |
| 52 } | |
| 53 return srcDimension / sampleSize; | |
| 54 } | |
| 55 | |
| 56 static SkISize best_scaled_dimensions(const SkISize& origDims, const SkISize& na tiveDims, | |
| 57 const SkISize& scaledCodecDims, float desi redScale) { | |
| 58 if (nativeDims == scaledCodecDims) { | |
| 59 // does not matter which to return if equal. Return here to skip below c alculations | |
| 60 return nativeDims; | |
| 61 } | |
| 62 float idealWidth = origDims.width() * desiredScale; | |
| 63 float idealHeight = origDims.height() * desiredScale; | |
| 64 | |
| 65 // calculate difference between native dimensions and ideal dimensions | |
| 66 float nativeWDiff = SkTAbs(idealWidth - nativeDims.width()); | |
| 67 float nativeHDiff = SkTAbs(idealHeight - nativeDims.height()); | |
| 68 float nativeDiff = (nativeWDiff + nativeHDiff) / 2; | |
| 69 | |
| 70 // calculate difference between scaledCodec dimensions and ideal dimensions | |
| 71 float scaledCodecWDiff = SkTAbs(idealWidth - scaledCodecDims.width()); | |
| 72 float scaledCodecHDiff = SkTAbs(idealHeight - scaledCodecDims.height()); | |
| 73 float scaledCodecDiff = (scaledCodecWDiff + scaledCodecHDiff) / 2; | |
| 74 | |
| 75 // return dimensions closest to ideal dimensions. | |
| 76 // If the differences are equal, return nativeDims, as native scaling is mor e efficient. | |
| 77 return nativeDiff > scaledCodecDiff ? scaledCodecDims : nativeDims; | |
| 78 | |
| 79 } | |
| 80 /* | |
| 81 * Return a valid set of output dimensions for this decoder, given an input scal e | |
| 82 */ | |
| 83 SkISize SkScaledCodec::onGetScaledDimensions(float desiredScale) const { | |
| 84 SkISize nativeDimensions = fScanlineDecoder->getScaledDimensions(desiredScal e); | |
| 85 // support scaling down by integer numbers. Ex: 1/2, 1/3, 1/4 ... | |
| 86 SkISize scaledCodecDimensions; | |
| 87 if (desiredScale > 0.5f) { | |
| 88 // sampleSize = 1 | |
| 89 scaledCodecDimensions = fScanlineDecoder->getInfo().dimensions(); | |
| 90 } | |
| 91 // sampleSize determines the step size between samples | |
| 92 // Ex: sampleSize = 2, sample every second pixel in x and y directions | |
| 93 int sampleSize = int(1 / desiredScale); | |
| 94 | |
| 95 int scaledWidth = get_scaled_dimension(this->getInfo().width(), sampleSize); | |
| 96 int scaledHeight = get_scaled_dimension(this->getInfo().height(), sampleSize ); | |
| 97 | |
| 98 // Return the calculated output dimensions for the given scale | |
| 99 scaledCodecDimensions = SkISize::Make(scaledWidth, scaledHeight); | |
| 100 | |
| 101 return best_scaled_dimensions(this->getInfo().dimensions(), nativeDimensions , | |
| 102 scaledCodecDimensions, desiredScale); | |
| 103 } | |
| 104 | |
| 105 // check if scaling to dstInfo size from srcInfo size using sampleSize is possib le | |
| 106 static bool scaling_supported(const SkImageInfo& dstInfo, const SkImageInfo& src Info, | |
| 107 int sampleX, int sampleY) { | |
| 108 const int dstWidth = dstInfo.width(); | |
| 109 const int dstHeight = dstInfo.height(); | |
| 110 const int srcWidth = srcInfo.width(); | |
| 111 const int srcHeight = srcInfo.height(); | |
| 112 // only support down sampling, not up sampling | |
| 113 if (dstWidth > srcWidth || dstHeight > srcHeight) { | |
| 114 return false; | |
| 115 } | |
| 116 // check that srcWidth is scaled down by an integer value | |
| 117 if (get_scaled_dimension(srcWidth, sampleX) != dstWidth) { | |
| 118 return false; | |
| 119 } | |
| 120 // check that src height is scaled down by an integer value | |
| 121 if (get_scaled_dimension(srcHeight, sampleY) != dstHeight) { | |
| 122 return false; | |
| 123 } | |
| 124 // sampleX and sampleY should be equal unless the original sampleSize reque sted was larger | |
| 125 // than srcWidth or srcHeight. If so, the result of this is dstWidth or dst Height = 1. | |
| 126 // This functionality allows for tall thin images to still be scaled down b y scaling factors. | |
| 127 if (sampleX != sampleY){ | |
| 128 if (1 != dstWidth && 1 != dstHeight) { | |
| 129 return false; | |
| 130 } | |
| 131 } | |
| 132 return true; | |
| 133 } | |
| 134 | |
| 135 // calculates sampleSize in x and y direction | |
| 136 void SkScaledCodec::ComputeSampleSize(const SkImageInfo& dstInfo, const SkImageI nfo& srcInfo, | |
| 137 int* sampleSizeX, int* sampleSizeY) { | |
|
scroggo
2015/08/13 18:01:21
nit: I would probably name these "sampleXPtr" and
| |
| 138 int srcWidth = srcInfo.width(); | |
| 139 int dstWidth = dstInfo.width(); | |
| 140 int srcHeight = srcInfo.height(); | |
| 141 int dstHeight = dstInfo.height(); | |
| 142 | |
| 143 int sampleX = srcWidth / dstWidth; | |
| 144 int sampleY = srcHeight / dstHeight; | |
| 145 | |
| 146 // only support down sampling, not up sampling | |
| 147 SkASSERT(dstWidth <= srcWidth); | |
| 148 SkASSERT(dstHeight <= srcHeight); | |
| 149 | |
| 150 // sampleX and sampleY should be equal unless the original sampleSize reques ted was | |
| 151 // larger than srcWidth or srcHeight. | |
| 152 // If so, the result of this is dstWidth or dstHeight = 1. This functionalit y | |
| 153 // allows for tall thin images to still be scaled down by scaling factors. | |
| 154 | |
| 155 if (sampleX != sampleY){ | |
| 156 if (1 != dstWidth && 1 != dstHeight) { | |
| 157 | |
| 158 // rounding during onGetScaledDimensions can cause different sampleS izes | |
| 159 // Ex: srcWidth = 79, srcHeight = 20, sampleSize = 10 | |
| 160 // dstWidth = 7, dstHeight = 2, sampleX = 79/7 = 11, sampleY = 20/2 = 10 | |
| 161 // correct for this rounding by comparing width to sampleY and heigh t to sampleX | |
| 162 | |
| 163 if (get_scaled_dimension(srcWidth, sampleY) == dstWidth) { | |
| 164 sampleX = sampleY; | |
| 165 } else if (get_scaled_dimension(srcHeight, sampleX) == dstHeight) { | |
| 166 sampleY = sampleX; | |
| 167 } | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 if (sampleSizeX) { | |
| 172 *sampleSizeX = sampleX; | |
| 173 } | |
| 174 if (sampleSizeY) { | |
| 175 *sampleSizeY = sampleY; | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 // TODO: Implement subsetting in onGetPixels which works when and when not sampl ing | |
| 180 | |
| 181 SkCodec::Result SkScaledCodec::onGetPixels(const SkImageInfo& requestedInfo, voi d* dst, | |
| 182 size_t rowBytes, const Options& optio ns, | |
| 183 SkPMColor ctable[], int* ctableCount) { | |
| 184 | |
| 185 if (options.fSubset) { | |
| 186 // Subsets are not supported. | |
| 187 return kUnimplemented; | |
| 188 } | |
| 189 | |
| 190 Result result = fScanlineDecoder->start(requestedInfo, &options, ctable, cta bleCount); | |
| 191 if (kSuccess == result) { | |
| 192 // native decode supported | |
| 193 return fScanlineDecoder->getScanlines(dst, requestedInfo.height(), rowBy tes); | |
| 194 | |
| 195 } | |
| 196 | |
| 197 if (kInvalidScale != result) { | |
| 198 // no scaling requested | |
| 199 return result; | |
| 200 } | |
| 201 | |
| 202 // scaling requested | |
| 203 int sampleX; | |
| 204 int sampleY; | |
| 205 ComputeSampleSize(requestedInfo, fScanlineDecoder->getInfo(), &sampleX, &sam pleY); | |
| 206 if (!scaling_supported(requestedInfo, fScanlineDecoder->getInfo(), sampleX, sampleY)) { | |
| 207 return kInvalidScale; | |
| 208 } | |
| 209 // set first sample pixel in y direction | |
| 210 int Y0 = sampleY >> 1; | |
| 211 | |
| 212 int dstHeight = requestedInfo.height(); | |
| 213 int srcHeight = fScanlineDecoder->getInfo().height(); | |
| 214 | |
| 215 SkImageInfo info = requestedInfo; | |
| 216 // use original height as scanlineDecoder does not support y sampling native ly | |
| 217 info = info.makeWH(requestedInfo.width(), srcHeight); | |
| 218 | |
| 219 // update scanlineDecoder with new info | |
| 220 result = fScanlineDecoder->start(info, &options, ctable, ctableCount); | |
| 221 if (kSuccess != result) { | |
| 222 return result; | |
| 223 } | |
| 224 | |
| 225 const bool requiresPostYSampling = fScanlineDecoder->requiresPostYSampling() ; | |
| 226 | |
| 227 if (requiresPostYSampling) { | |
| 228 SkAutoMalloc storage(srcHeight * rowBytes); | |
| 229 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); | |
| 230 result = fScanlineDecoder->getScanlines(storagePtr, srcHeight, rowBytes) ; | |
| 231 if (kSuccess != result) { | |
| 232 return result; | |
| 233 } | |
| 234 storagePtr += Y0 * rowBytes; | |
| 235 for (int y = 0; y < dstHeight; y++) { | |
| 236 memcpy(dst, storagePtr, rowBytes); | |
| 237 storagePtr += sampleY * rowBytes; | |
| 238 dst = SkTAddOffset<void>(dst, rowBytes); | |
| 239 } | |
| 240 } else { | |
| 241 // does not require post y sampling | |
| 242 result = fScanlineDecoder->skipScanlines(Y0); | |
| 243 if (kSuccess != result) { | |
| 244 return result; | |
| 245 } | |
| 246 for (int y = 0; y < dstHeight; y++) { | |
| 247 result = fScanlineDecoder->getScanlines(dst, 1, rowBytes); | |
| 248 if (kSuccess != result) { | |
| 249 return result; | |
| 250 } | |
| 251 if (y < dstHeight - 1) { | |
| 252 result = fScanlineDecoder->skipScanlines(sampleY - 1); | |
| 253 if (kSuccess != result) { | |
| 254 return result; | |
| 255 } | |
| 256 } | |
| 257 dst = SkTAddOffset<void>(dst, rowBytes); | |
| 258 } | |
| 259 } | |
| 260 return kSuccess; | |
| 261 } | |
| OLD | NEW |