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 // use original height as scanlineDecoder does not support y sampling native
ly |
| 156 info = info.makeWH(requestedInfo.width(), srcHeight); |
| 157 |
| 158 SkAutoTDelete<SkScanlineDecoder> scanlineDecoder(fCodec->getScanlineDecoder(
info, |
| 159 &options, ctable, ctableCou
nt)); |
| 160 if (NULL == scanlineDecoder) { |
| 161 SkCodecPrintf("failed to create scanline decoder.\n"); |
| 162 return kInvalidInput; |
| 163 } |
| 164 |
| 165 const bool requiresPostYSampling = scanlineDecoder->requiresPostYSampling(); |
| 166 |
| 167 if (requiresPostYSampling) { |
| 168 SkAutoMalloc storage(srcHeight * rowBytes); |
| 169 void* storagePtr = storage.get(); |
| 170 result = scanlineDecoder->getScanlines(storagePtr, srcHeight, rowBytes); |
| 171 if (kSuccess != result) { |
| 172 return result; |
| 173 } |
| 174 storagePtr += Y0 * rowBytes; |
| 175 for (int y = 0; y < dstHeight; y++) { |
| 176 memcpy(dst, storagePtr, rowBytes); |
| 177 storagePtr += sampleY * rowBytes; |
| 178 dst += rowBytes; |
| 179 } |
| 180 } else { |
| 181 // not hard to sample |
| 182 scanlineDecoder->skipScanlines(Y0); |
| 183 for (int y = 0; y < dstHeight; y++) { |
| 184 result = scanlineDecoder->getScanlines(dst, 1, rowBytes); |
| 185 if (kSuccess != result) { |
| 186 return result; |
| 187 } |
| 188 if (y < dstHeight - 1) { |
| 189 result = scanlineDecoder->skipScanlines(sampleY - 1); |
| 190 if (kSuccess != result) { |
| 191 return result; |
| 192 } |
| 193 } |
| 194 dst += rowBytes; |
| 195 } |
| 196 } |
| 197 return kSuccess; |
| 198 } |
OLD | NEW |