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