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 /* | |
| 42 * Return a valid set of output dimensions for this decoder, given an input scal e | |
| 43 */ | |
| 44 SkISize SkScaledCodec::onGetScaledDimensions(float desiredScale) const { | |
| 45 // support scaling down by integer numbers. Ex: 1/2, 1/3, 1/4 ... | |
| 46 | |
| 47 if (desiredScale > 0.5f) { | |
| 48 // sampleSize = 1 | |
| 49 return fCodec->getInfo().dimensions(); | |
| 50 } | |
| 51 // sampleSize determines the step size between samples | |
| 52 // Ex: sampleSize = 2, sample every second pixel in x and y directions | |
| 53 int sampleSize = 1 / desiredScale; | |
| 54 | |
| 55 // if sampleSize > width or height, will only sample 1 pixel in that directi on | |
| 56 int sampleX = SkMin32(sampleSize, this->getInfo().width()); | |
| 57 int sampleY = SkMin32(sampleSize, this->getInfo().height()); | |
| 58 | |
| 59 // NOTE: we round down here for scaledWidth and scaledHeight | |
| 60 // Can be changed in the future to round up, or round to closest integer if we want | |
|
scroggo
2015/07/28 15:58:10
FWIW, I think you would need to update your code t
emmaleer
2015/07/29 21:55:08
Okay. Do you think I should remove the comment say
scroggo
2015/07/30 17:53:01
I would probably keep it simple. Something like "W
emmaleer
2015/07/30 22:27:55
Acknowledged.
| |
| 61 int scaledWidth = this->getInfo().width() / sampleX; | |
| 62 int scaledHeight = this->getInfo().height() / sampleY; | |
| 63 | |
| 64 // Return the calculated output dimensions for the given scale | |
| 65 return SkISize::Make(scaledWidth, scaledHeight); | |
| 66 } | |
| 67 | |
| 68 // check if scaling to dstInfo size from srcInfo size is possible | |
| 69 static bool scaling_supported(const SkImageInfo& dstInfo, const SkImageInfo& sr cInfo){ | |
| 70 const int dstWidth = dstInfo.width(); | |
| 71 const int dstHeight = dstInfo.height(); | |
| 72 const int srcWidth = srcInfo.width(); | |
| 73 const int srcHeight = srcInfo.height(); | |
| 74 // only support down sampling, not up sampling | |
| 75 if (dstWidth > srcWidth || dstHeight > srcHeight) { | |
| 76 return false; | |
| 77 } | |
| 78 // check that srcWidth is scaled down by an integer value | |
| 79 int sampleSizeX = srcWidth / dstWidth; | |
| 80 if (srcWidth / sampleSizeX != dstWidth) { | |
| 81 return false; | |
| 82 } | |
| 83 // check that src height is scaled down by an integer value | |
| 84 int sampleSizeY = srcHeight / dstHeight; | |
| 85 if (srcHeight / sampleSizeY != dstHeight) { | |
| 86 return false; | |
| 87 } | |
| 88 // sampleX and sampleY should be equal unless the original sampleSize reques ted was larger | |
| 89 // than srcWidth or srcHeight. If so, the result of this is dstWidth or dstH eight = 1. | |
| 90 // This functionality allows for tall thin images to still be scaled down by scaling factors. | |
| 91 if (sampleSizeX != sampleSizeY){ | |
| 92 if (1 != dstWidth && 1 != dstHeight) { | |
| 93 return false; | |
| 94 } | |
| 95 } | |
| 96 return true; | |
| 97 } | |
| 98 | |
| 99 // TODO: Implement subsetting in onGetPixels which works when and when not sampl ing | |
| 100 | |
| 101 SkCodec::Result SkScaledCodec::onGetPixels(const SkImageInfo& requestedInfo, voi d* dst, | |
| 102 size_t rowBytes, const Options& optio ns, | |
| 103 SkPMColor ctable[], int* ctableCount) { | |
| 104 | |
| 105 // try to do a fCodec native decode, as some types of code support scaling a nd subsetting | |
| 106 Result result = fCodec->getPixels(requestedInfo, dst, rowBytes, &options, ct able, ctableCount); | |
| 107 if (kInvalidScale != result) { | |
| 108 // no scaling requested | |
| 109 return result; | |
| 110 } | |
| 111 // scaling requested | |
| 112 | |
| 113 if (!scaling_supported(requestedInfo, fCodec->getInfo())) { | |
| 114 return kInvalidScale; | |
| 115 } | |
| 116 | |
| 117 int dstHeight = requestedInfo.height(); | |
| 118 int srcHeight = fCodec->getInfo().height(); | |
| 119 | |
| 120 // set sample values in x and y directions | |
| 121 int sampleY = srcHeight / dstHeight; | |
|
scroggo
2015/07/28 15:58:10
I think we need to consolidate our calculations (a
emmaleer
2015/07/29 21:55:08
Do you mean consolidate the calculations of sample
emmaleer
2015/07/30 17:50:39
I've created a function called get_sample_size() w
scroggo
2015/07/30 17:53:01
I'm not sure where all we have it, but we should j
| |
| 122 int Y0 = sampleY >> 1; | |
| 123 | |
| 124 Options scaledOptions = options; | |
| 125 scaledOptions.fSampleX = fCodec->getInfo().width() / requestedInfo.width(); | |
| 126 | |
| 127 SkImageInfo info = requestedInfo; | |
| 128 // use original height as scanlineDecoder does not support y sampling native ly | |
| 129 info = info.makeWH(requestedInfo.width(), srcHeight); | |
| 130 | |
| 131 SkAutoTDelete<SkScanlineDecoder> scanlineDecoder(fCodec->getScanlineDecoder( info, | |
| 132 &scaledOptions, ctable, cta bleCount)); | |
| 133 if (NULL == scanlineDecoder) { | |
| 134 SkCodecPrintf("failed to create scanline decoder.\n"); | |
| 135 return kInvalidInput; | |
| 136 } | |
| 137 | |
| 138 bool isInterlaced = scanlineDecoder->isInterlaced(); | |
| 139 | |
| 140 if (isInterlaced) { | |
| 141 SkAutoMalloc storage(srcHeight * rowBytes); | |
| 142 void* storagePtr = storage.get(); | |
| 143 result = scanlineDecoder->getScanlines(storagePtr, srcHeight, rowBytes); | |
| 144 | |
| 145 storagePtr += Y0 * rowBytes; | |
| 146 for (int y = 0; y < dstHeight; y++) { | |
| 147 memcpy(dst, storagePtr, rowBytes); | |
| 148 storagePtr += sampleY * rowBytes; | |
| 149 dst += rowBytes; | |
| 150 } | |
| 151 } else { | |
| 152 // not interlaced | |
| 153 scanlineDecoder->skipScanlines(Y0); | |
| 154 for (int y = 0; y < dstHeight; y++) { | |
| 155 scanlineDecoder->getScanlines(dst, 1, rowBytes); | |
| 156 if (y < dstHeight - 1) { | |
| 157 scanlineDecoder->skipScanlines(sampleY - 1); | |
| 158 } | |
| 159 dst += rowBytes; | |
| 160 } | |
| 161 } | |
| 162 return kSuccess; | |
| 163 } | |
| OLD | NEW |