yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 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 "SkCodec.h" |
| 9 | #include "SkCodecPriv.h" |
| 10 | #include "SkColorPriv.h" |
| 11 | #include "SkData.h" |
| 12 | #if !defined(GOOGLE3) |
| 13 | #include "SkJpegCodec.h" |
| 14 | #endif |
| 15 | #include "SkRawCodec.h" |
| 16 | #include "SkRefCnt.h" |
| 17 | #include "SkStream.h" |
| 18 | #include "SkStreamPriv.h" |
| 19 | #include "SkSwizzler.h" |
| 20 | #include "SkTemplates.h" |
| 21 | #include "SkTypes.h" |
| 22 | |
| 23 | #include "dng_color_space.h" |
| 24 | #include "dng_exceptions.h" |
| 25 | #include "dng_host.h" |
| 26 | #include "dng_info.h" |
| 27 | #include "dng_memory.h" |
| 28 | #include "dng_render.h" |
| 29 | #include "dng_stream.h" |
| 30 | |
| 31 | #include "src/piex.h" |
| 32 | |
| 33 | #include <cmath> // for std::round,floor,ceil |
| 34 | #include <limits> |
| 35 | |
| 36 | namespace { |
| 37 | |
| 38 | // T must be unsigned type. |
| 39 | template <class T> |
| 40 | bool safe_add_to_size_t(T arg1, T arg2, size_t* result) { |
| 41 | SkASSERT(arg1 >= 0); |
| 42 | SkASSERT(arg2 >= 0); |
| 43 | if (arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) { |
| 44 | T sum = arg1 + arg2; |
| 45 | if (sum <= std::numeric_limits<size_t>::max()) { |
| 46 | *result = static_cast<size_t>(sum); |
| 47 | return true; |
| 48 | } |
| 49 | } |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | class SkDngMemoryAllocator : public dng_memory_allocator { |
| 54 | public: |
| 55 | ~SkDngMemoryAllocator() override {} |
| 56 | |
| 57 | dng_memory_block* Allocate(uint32 size) override { |
| 58 | // To avoid arbitary allocation requests which might lead to out-of-memory, limit the |
| 59 | // amount of memory that can be allocated at once. The memory limit is based on experiments |
| 60 | // and supposed to be sufficient for all valid DNG images. |
| 61 | if (size > 300 * 1024 * 1024) { // 300 MB |
| 62 | ThrowMemoryFull(); |
| 63 | } |
| 64 | return dng_memory_allocator::Allocate(size); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | } // namespace |
| 69 | |
| 70 | // Note: this class could throw exception if it is used as dng_stream. |
| 71 | class SkRawStream : public ::piex::StreamInterface { |
| 72 | public: |
| 73 | // Note that this call will take the ownership of stream. |
| 74 | explicit SkRawStream(SkStream* stream) |
| 75 | : fStream(stream), fWholeStreamRead(false) {} |
| 76 | |
| 77 | ~SkRawStream() override {} |
| 78 | |
| 79 | /* |
| 80 | * Creates an SkMemoryStream from the offset with size. |
| 81 | * Note: for performance reason, this function is destructive to the SkRawStream. One should |
| 82 | * abandon current object after the function call. |
| 83 | */ |
| 84 | SkMemoryStream* transferBuffer(size_t offset, size_t size) { |
| 85 | SkAutoTUnref<SkData> data(SkData::NewUninitialized(size)); |
| 86 | if (offset > fStreamBuffer.bytesWritten()) { |
| 87 | // If the offset is not buffered, read from fStream directly and skip the buffering. |
| 88 | const size_t skipLength = offset - fStreamBuffer.bytesWritten(); |
| 89 | if (fStream->skip(skipLength) != skipLength) { |
| 90 | return nullptr; |
| 91 | } |
| 92 | const size_t bytesRead = fStream->read(data->writable_data(), size); |
| 93 | if (bytesRead < size) { |
| 94 | data.reset(SkData::NewSubset(data.get(), 0, bytesRead)); |
| 95 | } |
| 96 | } else { |
| 97 | const size_t alreadyBuffered = SkTMin(fStreamBuffer.bytesWritten() - offset, size); |
| 98 | if (alreadyBuffered > 0 && |
| 99 | !fStreamBuffer.read(data->writable_data(), offset, alreadyBuffered)) { |
| 100 | return nullptr; |
| 101 | } |
| 102 | |
| 103 | const size_t remaining = size - alreadyBuffered; |
| 104 | if (remaining) { |
| 105 | auto* dst = static_cast<uint8_t*>(data->writable_data()) + alreadyBuffered; |
| 106 | const size_t bytesRead = fStream->read(dst, remaining); |
| 107 | size_t newSize; |
| 108 | if (bytesRead < remaining) { |
| 109 | if (!safe_add_to_size_t(alreadyBuffered, bytesRead, &newSize)) { |
| 110 | return nullptr; |
| 111 | } |
| 112 | data.reset(SkData::NewSubset(data.get(), 0, newSize)); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | return new SkMemoryStream(data); |
| 117 | } |
| 118 | |
| 119 | // For PIEX |
| 120 | ::piex::Error GetData(const size_t offset, const size_t length, |
| 121 | uint8* data) override { |
| 122 | if (offset == 0 && length == 0) { |
| 123 | return ::piex::Error::kOk; |
| 124 | } |
| 125 | size_t sum; |
| 126 | if (!safe_add_to_size_t(offset, length, &sum) || !this->bufferMoreData(sum)) { |
| 127 | return ::piex::Error::kFail; |
| 128 | } |
| 129 | if (!fStreamBuffer.read(data, offset, length)) { |
| 130 | return ::piex::Error::kFail; |
| 131 | } |
| 132 | return ::piex::Error::kOk; |
| 133 | } |
| 134 | |
| 135 | // For dng_stream |
| 136 | uint64 getLength() { |
| 137 | if (!this->bufferMoreData(kReadToEnd)) { // read whole stream |
| 138 | ThrowReadFile(); |
| 139 | } |
| 140 | return fStreamBuffer.bytesWritten(); |
| 141 | } |
| 142 | |
| 143 | // For dng_stream |
| 144 | void read(void* data, uint32 count, uint64 offset) { |
| 145 | if (count == 0 && offset == 0) { |
| 146 | return; |
| 147 | } |
| 148 | size_t sum; |
| 149 | if (!safe_add_to_size_t(static_cast<uint64>(count), offset, &sum) || |
| 150 | !this->bufferMoreData(sum)) { |
| 151 | ThrowReadFile(); |
| 152 | } |
| 153 | |
| 154 | if (!fStreamBuffer.read(data, offset, count)) { |
| 155 | ThrowReadFile(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | private: |
| 160 | // Note: if the newSize == kReadToEnd (0), this function will read to the end of stream. |
| 161 | bool bufferMoreData(size_t newSize) { |
| 162 | if (newSize == kReadToEnd) { |
| 163 | if (fWholeStreamRead) { // already read-to-end. |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | // TODO: optimize for the special case when the input is SkMemoryStream. |
| 168 | return SkStreamCopy(&fStreamBuffer, fStream.get()); |
| 169 | } |
| 170 | |
| 171 | if (newSize <= fStreamBuffer.bytesWritten()) { // already buffered to newSize |
| 172 | return true; |
| 173 | } |
| 174 | if (fWholeStreamRead) { // newSize is larger than the whole stream. |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | const size_t sizeToRead = newSize - fStreamBuffer.bytesWritten(); |
| 179 | SkAutoTMalloc<uint8> tempBuffer(sizeToRead); |
| 180 | const size_t bytesRead = fStream->read(tempBuffer.get(), sizeToRead); |
| 181 | if (bytesRead != sizeToRead) { |
| 182 | return false; |
| 183 | } |
| 184 | return fStreamBuffer.write(tempBuffer.get(), bytesRead); |
| 185 | } |
| 186 | |
| 187 | SkAutoTDelete<SkStream> fStream; |
| 188 | bool fWholeStreamRead; |
| 189 | |
| 190 | SkDynamicMemoryWStream fStreamBuffer; |
| 191 | |
| 192 | const size_t kReadToEnd = 0; |
| 193 | }; |
| 194 | |
| 195 | class SkDngStream : public dng_stream { |
| 196 | public: |
| 197 | SkDngStream(SkRawStream* rawStream) : fRawStream(rawStream) {} |
| 198 | |
| 199 | uint64 DoGetLength() override { return fRawStream->getLength(); } |
| 200 | |
| 201 | void DoRead(void* data, uint32 count, uint64 offset) override { |
| 202 | fRawStream->read(data, count, offset); |
| 203 | } |
| 204 | |
| 205 | private: |
| 206 | SkRawStream* fRawStream; |
| 207 | }; |
| 208 | |
| 209 | class SkDngImage { |
| 210 | public: |
| 211 | static SkDngImage* NewFromStream(SkRawStream* stream) { |
| 212 | SkAutoTDelete<SkDngImage> dngImage(new SkDngImage(stream)); |
| 213 | if (!dngImage->readDng()) { |
| 214 | return nullptr; |
| 215 | } |
| 216 | |
| 217 | SkASSERT(dngImage->fNegative); |
| 218 | return dngImage.release(); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Renders the DNG image to the size. The DNG SDK only allows scaling close to integer factors |
| 223 | * down to 80 pixels on the short edge. The rendered image will be close to the specified size, |
| 224 | * but there is no guarantee that any of the edges will match the requested size. E.g. |
| 225 | * 100% size: 4000 x 3000 |
| 226 | * requested size: 1600 x 1200 |
| 227 | * returned size could be: 2000 x 1500 |
| 228 | */ |
| 229 | dng_image* render(int width, int height) { |
| 230 | if (!fHost || !fInfo || !fNegative || !fDngStream) { |
| 231 | if (!this->readDng()) { |
| 232 | return nullptr; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // render() takes ownership of fHost, fInfo, fNegative and fDngStream when available. |
| 237 | SkAutoTDelete<dng_host> host(fHost.release()); |
| 238 | SkAutoTDelete<dng_info> info(fInfo.release()); |
| 239 | SkAutoTDelete<dng_negative> negative(fNegative.release()); |
| 240 | SkAutoTDelete<dng_stream> dngStream(fDngStream.release()); |
| 241 | |
| 242 | // DNG SDK preserves the aspect ratio, so it only needs to know the longer dimension. |
| 243 | const int preferredSize = SkTMax(width, height); |
| 244 | try { |
| 245 | host->SetPreferredSize(preferredSize); |
| 246 | host->ValidateSizes(); |
| 247 | |
| 248 | negative->ReadStage1Image(*host, *dngStream, *info); |
| 249 | |
| 250 | if (info->fMaskIndex != -1) { |
| 251 | negative->ReadTransparencyMask(*host, *dngStream, *info); |
| 252 | } |
| 253 | |
| 254 | negative->ValidateRawImageDigest(*host); |
| 255 | if (negative->IsDamaged()) { |
| 256 | return nullptr; |
| 257 | } |
| 258 | |
| 259 | const int32 kMosaicPlane = -1; |
| 260 | negative->BuildStage2Image(*host); |
| 261 | negative->BuildStage3Image(*host, kMosaicPlane); |
| 262 | |
| 263 | dng_render render(*host, *negative); |
| 264 | render.SetFinalSpace(dng_space_sRGB::Get()); |
| 265 | render.SetFinalPixelType(ttByte); |
| 266 | |
| 267 | dng_point stage3_size = negative->Stage3Image()->Size(); |
| 268 | render.SetMaximumSize(SkTMax(stage3_size.h, stage3_size.v)); |
| 269 | |
| 270 | return render.Render(); |
| 271 | } catch (...) { |
| 272 | return nullptr; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | const SkImageInfo& getImageInfo() const { |
| 277 | return fImageInfo; |
| 278 | } |
| 279 | |
| 280 | bool isScalable() const { |
| 281 | return fIsScalable; |
| 282 | } |
| 283 | |
| 284 | bool isXtransImage() const { |
| 285 | return fIsXtransImage; |
| 286 | } |
| 287 | |
| 288 | private: |
| 289 | bool readDng() { |
| 290 | // Due to the limit of DNG SDK, we need to reset host and info. |
| 291 | fHost.reset(new dng_host(&fAllocator)); |
| 292 | fInfo.reset(new dng_info); |
| 293 | fDngStream.reset(new SkDngStream(fStream)); |
| 294 | try { |
| 295 | fHost->ValidateSizes(); |
| 296 | fInfo->Parse(*fHost, *fDngStream); |
| 297 | fInfo->PostParse(*fHost); |
| 298 | if (!fInfo->IsValidDNG()) { |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | fNegative.reset(fHost->Make_dng_negative()); |
| 303 | fNegative->Parse(*fHost, *fDngStream, *fInfo); |
| 304 | fNegative->PostParse(*fHost, *fDngStream, *fInfo); |
| 305 | fNegative->SynchronizeMetadata(); |
| 306 | |
| 307 | fImageInfo = SkImageInfo::Make(fNegative->DefaultCropSizeH().As_real64(), |
| 308 | fNegative->DefaultCropSizeV().As_real64(), |
| 309 | kN32_SkColorType, kOpaque_SkAlphaType); |
| 310 | |
| 311 | // The DNG SDK scales only for at demosaicing, so only when a mosaic info |
| 312 | // is available also scale is available. |
| 313 | fIsScalable = fNegative->GetMosaicInfo() != nullptr; |
| 314 | fIsXtransImage = fIsScalable |
| 315 | ? (fNegative->GetMosaicInfo()->fCFAPatternSize.v == 6 |
| 316 | && fNegative->GetMosaicInfo()->fCFAPatternSize.h == 6) |
| 317 | : false; |
| 318 | return true; |
| 319 | } catch (...) { |
| 320 | fNegative.reset(nullptr); |
| 321 | return false; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | SkDngImage(SkRawStream* stream) |
| 326 | : fStream(stream) {} |
| 327 | |
| 328 | SkDngMemoryAllocator fAllocator; |
| 329 | SkAutoTDelete<SkRawStream> fStream; |
| 330 | SkAutoTDelete<dng_host> fHost; |
| 331 | SkAutoTDelete<dng_info> fInfo; |
| 332 | SkAutoTDelete<dng_negative> fNegative; |
| 333 | SkAutoTDelete<dng_stream> fDngStream; |
| 334 | |
| 335 | SkImageInfo fImageInfo; |
| 336 | bool fIsScalable; |
| 337 | bool fIsXtransImage; |
| 338 | }; |
| 339 | |
| 340 | /* |
| 341 | * Tries to handle the image with PIEX. If PIEX returns kOk and finds the preview image, create a |
| 342 | * SkJpegCodec. If PIEX returns kFail, then the file is invalid, return nullptr. In other cases, |
| 343 | * fallback to create SkRawCodec for DNG images. |
| 344 | */ |
| 345 | SkCodec* SkRawCodec::NewFromStream(SkStream* stream) { |
| 346 | SkAutoTDelete<SkRawStream> rawStream(new SkRawStream(stream)); |
| 347 | ::piex::PreviewImageData imageData; |
| 348 | // FIXME: ::piex::GetPreviewImageData() calls GetData() frequently with small amounts, |
| 349 | // resulting in many calls to bufferMoreData(). Could we make this more efficient by grouping |
| 350 | // smaller requests together? |
| 351 | if (::piex::IsRaw(rawStream.get())) { |
| 352 | ::piex::Error error = ::piex::GetPreviewImageData(rawStream.get(), &imageData); |
| 353 | |
| 354 | if (error == ::piex::Error::kOk && imageData.preview_length > 0) { |
| 355 | #if !defined(GOOGLE3) |
| 356 | // transferBuffer() is destructive to the rawStream. Abandon the rawStream after this |
| 357 | // function call. |
| 358 | // FIXME: one may avoid the copy of memoryStream and use the buffered rawStream. |
| 359 | SkMemoryStream* memoryStream = |
| 360 | rawStream->transferBuffer(imageData.preview_offset, imageData.preview_length); |
| 361 | return memoryStream ? SkJpegCodec::NewFromStream(memoryStream) : nullptr; |
| 362 | #else |
| 363 | return nullptr; |
| 364 | #endif |
| 365 | } else if (error == ::piex::Error::kFail) { |
| 366 | return nullptr; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | SkAutoTDelete<SkDngImage> dngImage(SkDngImage::NewFromStream(rawStream.release())); |
| 371 | if (!dngImage) { |
| 372 | return nullptr; |
| 373 | } |
| 374 | |
| 375 | return new SkRawCodec(dngImage.release()); |
| 376 | } |
| 377 | |
| 378 | SkCodec::Result SkRawCodec::onGetPixels(const SkImageInfo& requestedInfo, void* dst, |
| 379 | size_t dstRowBytes, const Options& options, |
| 380 | SkPMColor ctable[], int* ctableCount, |
| 381 | int* rowsDecoded) { |
| 382 | if (!conversion_possible(requestedInfo, this->getInfo())) { |
| 383 | SkCodecPrintf("Error: cannot convert input type to output type.\n"); |
| 384 | return kInvalidConversion; |
| 385 | } |
| 386 | |
| 387 | SkAutoTDelete<SkSwizzler> swizzler(SkSwizzler::CreateSwizzler( |
| 388 | SkSwizzler::kRGB, nullptr, requestedInfo, options)); |
| 389 | SkASSERT(swizzler); |
| 390 | |
| 391 | const int width = requestedInfo.width(); |
| 392 | const int height = requestedInfo.height(); |
| 393 | SkAutoTDelete<dng_image> image(fDngImage->render(width, height)); |
| 394 | if (!image) { |
| 395 | return kInvalidInput; |
| 396 | } |
| 397 | |
| 398 | // Because the DNG SDK can not guarantee to render to requested size, we allow a small |
| 399 | // difference. Only the overlapping region will be converted. |
| 400 | const float maxDiffRatio = 1.03f; |
| 401 | const dng_point& imageSize = image->Size(); |
| 402 | if (imageSize.h / width > maxDiffRatio || imageSize.h < width || |
| 403 | imageSize.v / height > maxDiffRatio || imageSize.v < height) { |
| 404 | return SkCodec::kInvalidScale; |
| 405 | } |
| 406 | |
| 407 | void* dstRow = dst; |
| 408 | uint8_t srcRow[width * 3]; |
| 409 | |
| 410 | dng_pixel_buffer buffer; |
| 411 | buffer.fData = &srcRow[0]; |
| 412 | buffer.fPlane = 0; |
| 413 | buffer.fPlanes = 3; |
| 414 | buffer.fColStep = buffer.fPlanes; |
| 415 | buffer.fPlaneStep = 1; |
| 416 | buffer.fPixelType = ttByte; |
| 417 | buffer.fPixelSize = sizeof(uint8_t); |
| 418 | buffer.fRowStep = sizeof(srcRow); |
| 419 | |
| 420 | for (int i = 0; i < height; ++i) { |
| 421 | buffer.fArea = dng_rect(i, 0, i + 1, width); |
| 422 | |
| 423 | try { |
| 424 | image->Get(buffer, dng_image::edge_zero); |
| 425 | } catch (...) { |
| 426 | *rowsDecoded = i; |
| 427 | return kIncompleteInput; |
| 428 | } |
| 429 | |
| 430 | swizzler->swizzle(dstRow, &srcRow[0]); |
| 431 | dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); |
| 432 | } |
| 433 | return kSuccess; |
| 434 | } |
| 435 | |
| 436 | SkISize SkRawCodec::onGetScaledDimensions(float desiredScale) const { |
| 437 | SkASSERT(desiredScale <= 1.f); |
| 438 | const SkISize dim = this->getInfo().dimensions(); |
| 439 | if (!fDngImage->isScalable()) { |
| 440 | return dim; |
| 441 | } |
| 442 | |
| 443 | // Limits the minimum size to be 80 on the short edge. |
| 444 | const float shortEdge = SkTMin(dim.fWidth, dim.fHeight); |
| 445 | if (desiredScale < 80.f / shortEdge) { |
| 446 | desiredScale = 80.f / shortEdge; |
| 447 | } |
| 448 | |
| 449 | // For Xtrans images, the integer-factor scaling does not support the half-size scaling case |
| 450 | // (stronger downscalings are fine). In this case, returns the factor "3" scaling instead. |
| 451 | if (fDngImage->isXtransImage() && desiredScale > 1.f / 3.f && desiredScale < 1.f) { |
| 452 | desiredScale = 1.f / 3.f; |
| 453 | } |
| 454 | |
| 455 | // Round to integer-factors. |
| 456 | const float finalScale = std::floor(1.f/ desiredScale); |
| 457 | return SkISize::Make(std::floor(dim.fWidth / finalScale), |
| 458 | std::floor(dim.fHeight / finalScale)); |
| 459 | } |
| 460 | |
| 461 | bool SkRawCodec::onDimensionsSupported(const SkISize& dim) { |
| 462 | const SkISize fullDim = this->getInfo().dimensions(); |
| 463 | const float fullShortEdge = SkTMin(fullDim.fWidth, fullDim.fHeight); |
| 464 | const float shortEdge = SkTMin(dim.fWidth, dim.fHeight); |
| 465 | |
| 466 | SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEdge / shortEdge)); |
| 467 | SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge / shortEdge)); |
| 468 | return sizeFloor == dim || sizeCeil == dim; |
| 469 | } |
| 470 | |
| 471 | SkRawCodec::~SkRawCodec() {} |
| 472 | |
| 473 | SkRawCodec::SkRawCodec(SkDngImage* dngImage) |
| 474 | : INHERITED(dngImage->getImageInfo(), nullptr) |
| 475 | , fDngImage(dngImage) {} |