Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkCodecPriv.h" | 8 #include "SkCodecPriv.h" |
| 9 #include "SkScaledCodec.h" | 9 #include "SkScaledCodec.h" |
| 10 #include "SkStream.h" | 10 #include "SkStream.h" |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 // than srcWidth or srcHeight. If so, the result of this is dstWidth or dstH eight = 1. | 132 // than srcWidth or srcHeight. If so, the result of this is dstWidth or dstH eight = 1. |
| 133 // This functionality allows for tall thin images to still be scaled down by scaling factors. | 133 // This functionality allows for tall thin images to still be scaled down by scaling factors. |
| 134 if (*sampleX != *sampleY){ | 134 if (*sampleX != *sampleY){ |
| 135 if (1 != dstWidth && 1 != dstHeight) { | 135 if (1 != dstWidth && 1 != dstHeight) { |
| 136 return false; | 136 return false; |
| 137 } | 137 } |
| 138 } | 138 } |
| 139 return true; | 139 return true; |
| 140 } | 140 } |
| 141 | 141 |
| 142 inline int SkScaledCodec::GetStartCoord(int sampleFactor) { | |
| 143 return sampleFactor / 2; | |
| 144 } | |
| 145 | |
| 146 inline int SkScaledCodec::GetDstCoord(int srcCoord, int sampleFactor) { | |
| 147 return srcCoord / sampleFactor; | |
| 148 } | |
| 149 | |
| 150 bool SkScaledCodec::IsCoordNecessary(int srcCoord, int sampleFactor, int scaledD im) { | |
|
scroggo
2015/08/27 20:14:23
Any idea whether calling all these functions slows
msarett
2015/08/27 21:21:46
We do special case on sample size == 1 by using th
scroggo
2015/08/28 13:28:47
Oh, that's even better!
| |
| 151 // Get the first coordinate that we want to keep | |
| 152 int startCoord = GetStartCoord(sampleFactor); | |
| 153 | |
| 154 // Return false on edge cases | |
| 155 if (srcCoord < startCoord || | |
| 156 SkScaledCodec::GetDstCoord(srcCoord, sampleFactor) >= scaledDim) { | |
| 157 return false; | |
| 158 } | |
| 159 | |
| 160 // Every sampleFactor rows are necessary | |
| 161 return ((srcCoord - startCoord) % sampleFactor) == 0; | |
| 162 } | |
| 163 | |
| 142 // calculates sampleSize in x and y direction | 164 // calculates sampleSize in x and y direction |
| 143 void SkScaledCodec::ComputeSampleSize(const SkImageInfo& dstInfo, const SkImageI nfo& srcInfo, | 165 void SkScaledCodec::ComputeSampleSize(const SkImageInfo& dstInfo, const SkImageI nfo& srcInfo, |
| 144 int* sampleXPtr, int* sampleYPtr) { | 166 int* sampleXPtr, int* sampleYPtr) { |
| 145 int srcWidth = srcInfo.width(); | 167 int srcWidth = srcInfo.width(); |
| 146 int dstWidth = dstInfo.width(); | 168 int dstWidth = dstInfo.width(); |
| 147 int srcHeight = srcInfo.height(); | 169 int srcHeight = srcInfo.height(); |
| 148 int dstHeight = dstInfo.height(); | 170 int dstHeight = dstInfo.height(); |
| 149 | 171 |
| 150 int sampleX = srcWidth / dstWidth; | 172 int sampleX = srcWidth / dstWidth; |
| 151 int sampleY = srcHeight / dstHeight; | 173 int sampleY = srcHeight / dstHeight; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 | 213 |
| 192 if (options.fSubset) { | 214 if (options.fSubset) { |
| 193 // Subsets are not supported. | 215 // Subsets are not supported. |
| 194 return kUnimplemented; | 216 return kUnimplemented; |
| 195 } | 217 } |
| 196 | 218 |
| 197 Result result = fScanlineDecoder->start(requestedInfo, &options, ctable, cta bleCount); | 219 Result result = fScanlineDecoder->start(requestedInfo, &options, ctable, cta bleCount); |
| 198 if (kSuccess == result) { | 220 if (kSuccess == result) { |
| 199 // native decode supported | 221 // native decode supported |
| 200 return fScanlineDecoder->getScanlines(dst, requestedInfo.height(), rowBy tes); | 222 return fScanlineDecoder->getScanlines(dst, requestedInfo.height(), rowBy tes); |
| 201 | |
| 202 } | 223 } |
| 203 | 224 |
| 204 if (kInvalidScale != result) { | 225 if (kInvalidScale != result) { |
| 205 // no scaling requested | 226 // no scaling requested |
| 206 return result; | 227 return result; |
| 207 } | 228 } |
| 208 | 229 |
| 209 // scaling requested | 230 // scaling requested |
| 210 int sampleX; | 231 int sampleX; |
| 211 int sampleY; | 232 int sampleY; |
| 212 if (!scaling_supported(requestedInfo, fScanlineDecoder->getInfo(), &sampleX, &sampleY)) { | 233 if (!scaling_supported(requestedInfo, fScanlineDecoder->getInfo(), &sampleX, &sampleY)) { |
| 213 return kInvalidScale; | 234 return kInvalidScale; |
| 214 } | 235 } |
| 215 // set first sample pixel in y direction | 236 // set first sample pixel in y direction |
| 216 int Y0 = sampleY >> 1; | 237 int Y0 = SkScaledCodec::GetStartCoord(sampleY); |
| 217 | 238 |
| 218 int dstHeight = requestedInfo.height(); | 239 int dstHeight = requestedInfo.height(); |
| 219 int srcHeight = fScanlineDecoder->getInfo().height(); | 240 int srcHeight = fScanlineDecoder->getInfo().height(); |
| 220 | 241 |
| 221 SkImageInfo info = requestedInfo; | 242 SkImageInfo info = requestedInfo; |
| 222 // use original height as scanlineDecoder does not support y sampling native ly | 243 // use original height as scanlineDecoder does not support y sampling native ly |
| 223 info = info.makeWH(requestedInfo.width(), srcHeight); | 244 info = info.makeWH(requestedInfo.width(), srcHeight); |
| 224 | 245 |
| 225 // update scanlineDecoder with new info | 246 // update scanlineDecoder with new info |
| 226 result = fScanlineDecoder->start(info, &options, ctable, ctableCount); | 247 result = fScanlineDecoder->start(info, &options, ctable, ctableCount); |
| 227 if (kSuccess != result) { | 248 if (kSuccess != result) { |
| 228 return result; | 249 return result; |
| 229 } | 250 } |
| 230 | |
| 231 const bool requiresPostYSampling = fScanlineDecoder->requiresPostYSampling() ; | |
| 232 | 251 |
| 233 if (requiresPostYSampling) { | 252 switch(fScanlineDecoder->getScanlineOrder()) { |
| 234 SkAutoMalloc storage(srcHeight * rowBytes); | 253 case SkScanlineDecoder::kTopDown_SkScanlineOrder: { |
| 235 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); | 254 result = fScanlineDecoder->skipScanlines(Y0); |
| 236 result = fScanlineDecoder->getScanlines(storagePtr, srcHeight, rowBytes) ; | 255 if (kSuccess != result && kIncompleteInput != result) { |
| 237 if (kSuccess != result) { | 256 return result; |
| 238 return result; | 257 } |
| 258 for (int y = 0; y < dstHeight; y++) { | |
| 259 result = fScanlineDecoder->getScanlines(dst, 1, rowBytes); | |
| 260 if (kSuccess != result && kIncompleteInput != result) { | |
| 261 return result; | |
| 262 } | |
| 263 if (y < dstHeight - 1) { | |
| 264 result = fScanlineDecoder->skipScanlines(sampleY - 1); | |
| 265 if (kSuccess != result && kIncompleteInput != result) { | |
| 266 return result; | |
| 267 } | |
| 268 } | |
| 269 dst = SkTAddOffset<void>(dst, rowBytes); | |
| 270 } | |
| 271 return kSuccess; | |
| 239 } | 272 } |
| 240 storagePtr += Y0 * rowBytes; | 273 case SkScanlineDecoder::kOutOfOrder_SkScanlineOrder: { |
| 241 for (int y = 0; y < dstHeight; y++) { | 274 for (int y = 0; y < srcHeight; y++) { |
| 242 memcpy(dst, storagePtr, rowBytes); | 275 int srcY = fScanlineDecoder->getY(); |
| 243 storagePtr += sampleY * rowBytes; | 276 if (SkScaledCodec::IsCoordNecessary(srcY, sampleY, dstHeight)) { |
| 244 dst = SkTAddOffset<void>(dst, rowBytes); | 277 void* dstPtr = SkTAddOffset<void>(dst, |
| 278 rowBytes * SkScaledCodec::GetDstCoord(srcY, sampleY) ); | |
| 279 result = fScanlineDecoder->getScanlines(dstPtr, 1, rowBytes) ; | |
| 280 if (kSuccess != result && kIncompleteInput != result) { | |
| 281 return result; | |
| 282 } | |
| 283 } else { | |
| 284 result = fScanlineDecoder->skipScanlines(1); | |
| 285 if (kSuccess != result && kIncompleteInput != result) { | |
| 286 return result; | |
| 287 } | |
| 288 } | |
| 289 } | |
| 290 return kSuccess; | |
| 245 } | 291 } |
| 246 } else { | 292 case SkScanlineDecoder::kNone_SkScanlineOrder: { |
| 247 // does not require post y sampling | 293 SkAutoMalloc storage(srcHeight * rowBytes); |
| 248 result = fScanlineDecoder->skipScanlines(Y0); | 294 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); |
| 249 if (kSuccess != result) { | 295 result = fScanlineDecoder->getScanlines(storagePtr, srcHeight, rowBy tes); |
| 250 return result; | |
| 251 } | |
| 252 for (int y = 0; y < dstHeight; y++) { | |
| 253 result = fScanlineDecoder->getScanlines(dst, 1, rowBytes); | |
| 254 if (kSuccess != result) { | 296 if (kSuccess != result) { |
| 255 return result; | 297 return result; |
| 256 } | 298 } |
| 257 if (y < dstHeight - 1) { | 299 storagePtr += Y0 * rowBytes; |
| 258 result = fScanlineDecoder->skipScanlines(sampleY - 1); | 300 for (int y = 0; y < dstHeight; y++) { |
| 259 if (kSuccess != result) { | 301 memcpy(dst, storagePtr, rowBytes); |
| 260 return result; | 302 storagePtr += sampleY * rowBytes; |
| 261 } | 303 dst = SkTAddOffset<void>(dst, rowBytes); |
| 262 } | 304 } |
| 263 dst = SkTAddOffset<void>(dst, rowBytes); | 305 return kSuccess; |
| 264 } | 306 } |
| 307 default: | |
| 308 SkASSERT(false); | |
| 309 return kUnimplemented; | |
| 265 } | 310 } |
| 266 return kSuccess; | |
| 267 } | 311 } |
| OLD | NEW |