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 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(SkISize origDims, SkISize nativeDims, | |
|
scroggo
2015/08/03 17:02:55
All the SkISize parameters should be const referen
emmaleer
2015/08/03 19:00:40
Acknowledged.
| |
| 51 SkISize scaledCodecDims, float desiredScal e) { | |
| 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 = fabs(idealWidth - nativeDims.width()); | |
|
scroggo
2015/08/03 17:02:55
nit: You should use SkTAbs, from SkTypes.h
emmaleer
2015/08/03 19:00:40
Acknowledged.
| |
| 61 float nativeHDiff = fabs(idealHeight - nativeDims.height()); | |
| 62 float nativeDiff = (nativeWDiff + nativeHDiff) / 2; | |
| 63 | |
| 64 // calculate difference between scaledCodec dimensions and ideal dimensions | |
| 65 float scaledCodecWDiff = fabs(idealWidth - scaledCodecDims.width()); | |
| 66 float scaledCodecHDiff = fabs(idealHeight - scaledCodecDims.height()); | |
| 67 float scaledCodecDiff = (scaledCodecWDiff + scaledCodecHDiff) / 2; | |
| 68 | |
| 69 // return dimensions closest to ideal dimensions | |
| 70 return nativeDiff > scaledCodecDiff ? scaledCodecDims : nativeDims; | |
|
scroggo
2015/08/03 17:02:55
This might deserve a comment - did you deliberatel
emmaleer
2015/08/03 19:00:40
Yep, I made it so we would choose the native dimen
| |
| 71 | |
| 72 } | |
| 73 /* | |
| 74 * Return a valid set of output dimensions for this decoder, given an input scal e | |
| 75 */ | |
| 76 SkISize SkScaledCodec::onGetScaledDimensions(float desiredScale) const { | |
| 77 // support scaling down by integer numbers. Ex: 1/2, 1/3, 1/4 ... | |
|
scroggo
2015/08/03 17:02:55
This comment actually applies to line 79. The imme
emmaleer
2015/08/03 19:00:40
Acknowledged.
| |
| 78 SkISize nativeDimensions = fCodec->getScaledDimensions(desiredScale); | |
| 79 SkISize scaledCodecDimensions; | |
| 80 if (desiredScale > 0.5f) { | |
| 81 // sampleSize = 1 | |
| 82 scaledCodecDimensions = fCodec->getInfo().dimensions(); | |
| 83 } | |
| 84 // sampleSize determines the step size between samples | |
| 85 // Ex: sampleSize = 2, sample every second pixel in x and y directions | |
| 86 int sampleSize = 1 / desiredScale; | |
| 87 | |
| 88 int scaledWidth = get_scaled_dimension(this->getInfo().width(), sampleSize); | |
| 89 int scaledHeight = get_scaled_dimension(this->getInfo().height(), sampleSize ); | |
| 90 | |
| 91 // Return the calculated output dimensions for the given scale | |
| 92 scaledCodecDimensions = SkISize::Make(scaledWidth, scaledHeight); | |
| 93 | |
| 94 return best_scaled_dimensions(this->getInfo().dimensions(), nativeDimensions , | |
| 95 scaledCodecDimensions, desiredScale); | |
| 96 } | |
| 97 | |
| 98 // check if scaling to dstInfo size from srcInfo size is possible | |
| 99 static bool scaling_supported(const SkImageInfo& dstInfo, const SkImageInfo& sr cInfo){ | |
| 100 const int dstWidth = dstInfo.width(); | |
| 101 const int dstHeight = dstInfo.height(); | |
| 102 const int srcWidth = srcInfo.width(); | |
| 103 const int srcHeight = srcInfo.height(); | |
| 104 // only support down sampling, not up sampling | |
| 105 if (dstWidth > srcWidth || dstHeight > srcHeight) { | |
| 106 return false; | |
| 107 } | |
| 108 // check that srcWidth is scaled down by an integer value | |
| 109 int sampleSizeX = SkScaledCodec::GetSampleSize(srcWidth, dstWidth); | |
| 110 if (get_scaled_dimension(srcWidth, sampleSizeX) != dstWidth) { | |
| 111 return false; | |
| 112 } | |
| 113 // check that src height is scaled down by an integer value | |
| 114 int sampleSizeY = SkScaledCodec::GetSampleSize(srcHeight, dstHeight); | |
| 115 if (get_scaled_dimension(srcHeight, sampleSizeY) != dstHeight) { | |
| 116 return false; | |
| 117 } | |
| 118 // sampleX and sampleY should be equal unless the original sampleSize reques ted was larger | |
| 119 // than srcWidth or srcHeight. If so, the result of this is dstWidth or dstH eight = 1. | |
| 120 // This functionality allows for tall thin images to still be scaled down by scaling factors. | |
| 121 if (sampleSizeX != sampleSizeY){ | |
| 122 if (1 != dstWidth && 1 != dstHeight) { | |
| 123 return false; | |
| 124 } | |
| 125 } | |
| 126 return true; | |
| 127 } | |
| 128 | |
| 129 // TODO: Implement subsetting in onGetPixels which works when and when not sampl ing | |
| 130 | |
| 131 SkCodec::Result SkScaledCodec::onGetPixels(const SkImageInfo& requestedInfo, voi d* dst, | |
| 132 size_t rowBytes, const Options& optio ns, | |
| 133 SkPMColor ctable[], int* ctableCount) { | |
| 134 | |
| 135 // try to do a fCodec native decode, as some types of code support scaling a nd subsetting | |
| 136 Result result = fCodec->getPixels(requestedInfo, dst, rowBytes, &options, ct able, ctableCount); | |
| 137 if (kInvalidScale != result) { | |
| 138 // no scaling requested | |
| 139 return result; | |
| 140 } | |
| 141 // scaling requested | |
| 142 if (!scaling_supported(requestedInfo, fCodec->getInfo())) { | |
| 143 return kInvalidScale; | |
| 144 } | |
| 145 | |
| 146 int dstHeight = requestedInfo.height(); | |
| 147 int srcHeight = fCodec->getInfo().height(); | |
| 148 | |
| 149 // set sample values in x and y directions | |
| 150 int sampleY = GetSampleSize(srcHeight, dstHeight); | |
| 151 int Y0 = sampleY >> 1; | |
| 152 | |
| 153 SkImageInfo info = requestedInfo; | |
| 154 // use original height as scanlineDecoder does not support y sampling native ly | |
| 155 info = info.makeWH(requestedInfo.width(), srcHeight); | |
| 156 | |
| 157 SkAutoTDelete<SkScanlineDecoder> scanlineDecoder(fCodec->getScanlineDecoder( info, | |
| 158 &options, ctable, ctableCou nt)); | |
| 159 if (NULL == scanlineDecoder) { | |
| 160 SkCodecPrintf("failed to create scanline decoder.\n"); | |
| 161 return kInvalidInput; | |
| 162 } | |
| 163 | |
| 164 const bool isHardToSample = scanlineDecoder->isHardToSample(); | |
| 165 | |
| 166 if (isHardToSample) { | |
| 167 SkAutoMalloc storage(srcHeight * rowBytes); | |
| 168 void* storagePtr = storage.get(); | |
| 169 result = scanlineDecoder->getScanlines(storagePtr, srcHeight, rowBytes); | |
| 170 if (kSuccess != result) { | |
| 171 return result; | |
| 172 } | |
| 173 storagePtr += Y0 * rowBytes; | |
| 174 for (int y = 0; y < dstHeight; y++) { | |
| 175 memcpy(dst, storagePtr, rowBytes); | |
| 176 storagePtr += sampleY * rowBytes; | |
| 177 dst += rowBytes; | |
| 178 } | |
| 179 } else { | |
| 180 // not hard to sample | |
| 181 scanlineDecoder->skipScanlines(Y0); | |
| 182 for (int y = 0; y < dstHeight; y++) { | |
| 183 result = scanlineDecoder->getScanlines(dst, 1, rowBytes); | |
| 184 if (kSuccess != result) { | |
| 185 return result; | |
| 186 } | |
| 187 if (y < dstHeight - 1) { | |
| 188 result = scanlineDecoder->skipScanlines(sampleY - 1); | |
| 189 if (kSuccess != result) { | |
| 190 return result; | |
| 191 } | |
| 192 } | |
| 193 dst += rowBytes; | |
| 194 } | |
| 195 } | |
| 196 return kSuccess; | |
| 197 } | |
| OLD | NEW |