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