| 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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 // than srcWidth or srcHeight. If so, the result of this is dstWidth or dstH
eight = 1. | 126 // than srcWidth or srcHeight. If so, the result of this is dstWidth or dstH
eight = 1. |
| 127 // This functionality allows for tall thin images to still be scaled down by
scaling factors. | 127 // This functionality allows for tall thin images to still be scaled down by
scaling factors. |
| 128 if (*sampleX != *sampleY){ | 128 if (*sampleX != *sampleY){ |
| 129 if (1 != dstWidth && 1 != dstHeight) { | 129 if (1 != dstWidth && 1 != dstHeight) { |
| 130 return false; | 130 return false; |
| 131 } | 131 } |
| 132 } | 132 } |
| 133 return true; | 133 return true; |
| 134 } | 134 } |
| 135 | 135 |
| 136 inline int SkScaledCodec::GetStartCoord(int sampleFactor) { |
| 137 return sampleFactor / 2; |
| 138 } |
| 139 |
| 140 inline int SkScaledCodec::GetDstCoord(int srcCoord, int sampleFactor) { |
| 141 return srcCoord / sampleFactor; |
| 142 } |
| 143 |
| 144 bool SkScaledCodec::IsCoordNecessary(int srcCoord, int sampleFactor, int scaledD
im) { |
| 145 // Get the first coordinate that we want to keep |
| 146 int startCoord = GetStartCoord(sampleFactor); |
| 147 |
| 148 // Return false on edge cases |
| 149 if (srcCoord < startCoord || |
| 150 SkScaledCodec::GetDstCoord(srcCoord, sampleFactor) >= scaledDim) { |
| 151 return false; |
| 152 } |
| 153 |
| 154 // Every sampleFactor rows are necessary |
| 155 return ((srcCoord - startCoord) % sampleFactor) == 0; |
| 156 } |
| 157 |
| 136 // calculates sampleSize in x and y direction | 158 // calculates sampleSize in x and y direction |
| 137 void SkScaledCodec::ComputeSampleSize(const SkImageInfo& dstInfo, const SkImageI
nfo& srcInfo, | 159 void SkScaledCodec::ComputeSampleSize(const SkImageInfo& dstInfo, const SkImageI
nfo& srcInfo, |
| 138 int* sampleXPtr, int* sampleYPtr) { | 160 int* sampleXPtr, int* sampleYPtr) { |
| 139 int srcWidth = srcInfo.width(); | 161 int srcWidth = srcInfo.width(); |
| 140 int dstWidth = dstInfo.width(); | 162 int dstWidth = dstInfo.width(); |
| 141 int srcHeight = srcInfo.height(); | 163 int srcHeight = srcInfo.height(); |
| 142 int dstHeight = dstInfo.height(); | 164 int dstHeight = dstInfo.height(); |
| 143 | 165 |
| 144 int sampleX = srcWidth / dstWidth; | 166 int sampleX = srcWidth / dstWidth; |
| 145 int sampleY = srcHeight / dstHeight; | 167 int sampleY = srcHeight / dstHeight; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 | 207 |
| 186 if (options.fSubset) { | 208 if (options.fSubset) { |
| 187 // Subsets are not supported. | 209 // Subsets are not supported. |
| 188 return kUnimplemented; | 210 return kUnimplemented; |
| 189 } | 211 } |
| 190 | 212 |
| 191 Result result = fScanlineDecoder->start(requestedInfo, &options, ctable, cta
bleCount); | 213 Result result = fScanlineDecoder->start(requestedInfo, &options, ctable, cta
bleCount); |
| 192 if (kSuccess == result) { | 214 if (kSuccess == result) { |
| 193 // native decode supported | 215 // native decode supported |
| 194 return fScanlineDecoder->getScanlines(dst, requestedInfo.height(), rowBy
tes); | 216 return fScanlineDecoder->getScanlines(dst, requestedInfo.height(), rowBy
tes); |
| 195 | |
| 196 } | 217 } |
| 197 | 218 |
| 198 if (kInvalidScale != result) { | 219 if (kInvalidScale != result) { |
| 199 // no scaling requested | 220 // no scaling requested |
| 200 return result; | 221 return result; |
| 201 } | 222 } |
| 202 | 223 |
| 203 // scaling requested | 224 // scaling requested |
| 204 int sampleX; | 225 int sampleX; |
| 205 int sampleY; | 226 int sampleY; |
| 206 if (!scaling_supported(requestedInfo, fScanlineDecoder->getInfo(), &sampleX,
&sampleY)) { | 227 if (!scaling_supported(requestedInfo, fScanlineDecoder->getInfo(), &sampleX,
&sampleY)) { |
| 207 return kInvalidScale; | 228 return kInvalidScale; |
| 208 } | 229 } |
| 209 // set first sample pixel in y direction | 230 // set first sample pixel in y direction |
| 210 int Y0 = sampleY >> 1; | 231 int Y0 = SkScaledCodec::GetStartCoord(sampleY); |
| 211 | 232 |
| 212 int dstHeight = requestedInfo.height(); | 233 int dstHeight = requestedInfo.height(); |
| 213 int srcHeight = fScanlineDecoder->getInfo().height(); | 234 int srcHeight = fScanlineDecoder->getInfo().height(); |
| 214 | 235 |
| 215 SkImageInfo info = requestedInfo; | 236 SkImageInfo info = requestedInfo; |
| 216 // use original height as scanlineDecoder does not support y sampling native
ly | 237 // use original height as scanlineDecoder does not support y sampling native
ly |
| 217 info = info.makeWH(requestedInfo.width(), srcHeight); | 238 info = info.makeWH(requestedInfo.width(), srcHeight); |
| 218 | 239 |
| 219 // update scanlineDecoder with new info | 240 // update scanlineDecoder with new info |
| 220 result = fScanlineDecoder->start(info, &options, ctable, ctableCount); | 241 result = fScanlineDecoder->start(info, &options, ctable, ctableCount); |
| 221 if (kSuccess != result) { | 242 if (kSuccess != result) { |
| 222 return result; | 243 return result; |
| 223 } | 244 } |
| 224 | |
| 225 const bool requiresPostYSampling = fScanlineDecoder->requiresPostYSampling()
; | |
| 226 | 245 |
| 227 if (requiresPostYSampling) { | 246 switch(fScanlineDecoder->getScanlineOrder()) { |
| 228 SkAutoMalloc storage(srcHeight * rowBytes); | 247 case SkScanlineDecoder::kTopDown_SkScanlineOrder: { |
| 229 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); | 248 result = fScanlineDecoder->skipScanlines(Y0); |
| 230 result = fScanlineDecoder->getScanlines(storagePtr, srcHeight, rowBytes)
; | 249 if (kSuccess != result && kIncompleteInput != result) { |
| 231 if (kSuccess != result) { | 250 return result; |
| 232 return result; | 251 } |
| 252 for (int y = 0; y < dstHeight; y++) { |
| 253 result = fScanlineDecoder->getScanlines(dst, 1, rowBytes); |
| 254 if (kSuccess != result && kIncompleteInput != result) { |
| 255 return result; |
| 256 } |
| 257 if (y < dstHeight - 1) { |
| 258 result = fScanlineDecoder->skipScanlines(sampleY - 1); |
| 259 if (kSuccess != result && kIncompleteInput != result) { |
| 260 return result; |
| 261 } |
| 262 } |
| 263 dst = SkTAddOffset<void>(dst, rowBytes); |
| 264 } |
| 265 return kSuccess; |
| 233 } | 266 } |
| 234 storagePtr += Y0 * rowBytes; | 267 case SkScanlineDecoder::kOutOfOrder_SkScanlineOrder: { |
| 235 for (int y = 0; y < dstHeight; y++) { | 268 for (int y = 0; y < srcHeight; y++) { |
| 236 memcpy(dst, storagePtr, rowBytes); | 269 int srcY = fScanlineDecoder->getY(); |
| 237 storagePtr += sampleY * rowBytes; | 270 if (SkScaledCodec::IsCoordNecessary(srcY, sampleY, dstHeight)) { |
| 238 dst = SkTAddOffset<void>(dst, rowBytes); | 271 void* dstPtr = SkTAddOffset<void>(dst, |
| 272 rowBytes * SkScaledCodec::GetDstCoord(srcY, sampleY)
); |
| 273 result = fScanlineDecoder->getScanlines(dstPtr, 1, rowBytes)
; |
| 274 if (kSuccess != result && kIncompleteInput != result) { |
| 275 return result; |
| 276 } |
| 277 } else { |
| 278 result = fScanlineDecoder->skipScanlines(1); |
| 279 if (kSuccess != result && kIncompleteInput != result) { |
| 280 return result; |
| 281 } |
| 282 } |
| 283 } |
| 284 return kSuccess; |
| 239 } | 285 } |
| 240 } else { | 286 case SkScanlineDecoder::kNone_SkScanlineOrder: { |
| 241 // does not require post y sampling | 287 SkAutoMalloc storage(srcHeight * rowBytes); |
| 242 result = fScanlineDecoder->skipScanlines(Y0); | 288 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); |
| 243 if (kSuccess != result) { | 289 result = fScanlineDecoder->getScanlines(storagePtr, srcHeight, rowBy
tes); |
| 244 return result; | |
| 245 } | |
| 246 for (int y = 0; y < dstHeight; y++) { | |
| 247 result = fScanlineDecoder->getScanlines(dst, 1, rowBytes); | |
| 248 if (kSuccess != result) { | 290 if (kSuccess != result) { |
| 249 return result; | 291 return result; |
| 250 } | 292 } |
| 251 if (y < dstHeight - 1) { | 293 storagePtr += Y0 * rowBytes; |
| 252 result = fScanlineDecoder->skipScanlines(sampleY - 1); | 294 for (int y = 0; y < dstHeight; y++) { |
| 253 if (kSuccess != result) { | 295 memcpy(dst, storagePtr, rowBytes); |
| 254 return result; | 296 storagePtr += sampleY * rowBytes; |
| 255 } | 297 dst = SkTAddOffset<void>(dst, rowBytes); |
| 256 } | 298 } |
| 257 dst = SkTAddOffset<void>(dst, rowBytes); | 299 return kSuccess; |
| 258 } | 300 } |
| 301 default: |
| 302 SkASSERT(false); |
| 303 return kUnimplemented; |
| 259 } | 304 } |
| 260 return kSuccess; | |
| 261 } | 305 } |
| OLD | NEW |