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 "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 virtual ~SkDngMemoryAllocator() {} | |
| 56 | |
| 57 dng_memory_block* Allocate(uint32 size) override { | |
| 58 // To avoid arbitary allocation requests which might lead to out-of-memo ry, limit the | |
| 59 // amount of memory that can be allocated at once. | |
| 60 if (size > 300 * 1024 * 1024) { // 300 MB | |
|
scroggo
2016/01/19 21:05:44
How did you choose this amount? Should it be confi
yujieqin
2016/01/20 12:53:04
The memory limit is based on our experiments, it s
| |
| 61 ThrowMemoryFull(); | |
| 62 } | |
| 63 return dng_memory_allocator::Allocate(size); | |
| 64 } | |
| 65 }; | |
| 66 | |
| 67 } // namespace | |
| 68 | |
| 69 // Note: this class could throw exception if it is used as dng_stream. | |
| 70 class SkRawStream : public dng_stream, public ::piex::StreamInterface { | |
| 71 public: | |
| 72 // Note that this call will take the ownership of stream. | |
| 73 explicit SkRawStream(SkStream* stream) | |
| 74 : fStream(stream), fWholeStreamRead(false) {} | |
| 75 | |
| 76 ~SkRawStream() override {} | |
| 77 | |
| 78 /* | |
| 79 * Creates an SkMemoryStream from the offset with size. | |
| 80 * Note: for performance reason, this function is destructive to the SkRawSt ream. One should | |
| 81 * abandon current object after the function call. | |
| 82 */ | |
| 83 SkMemoryStream* transferBuffer(size_t offset, size_t size) { | |
| 84 SkAutoTUnref<SkData> data(SkData::NewUninitialized(size)); | |
| 85 if (offset > fStreamBuffer.bytesWritten()) { | |
| 86 // If the offset is not buffered, read from fStream directly and ski p the buffering. | |
| 87 const size_t skipLength = offset - fStreamBuffer.bytesWritten(); | |
| 88 if (fStream->skip(skipLength) != skipLength) { | |
| 89 return nullptr; | |
| 90 } | |
| 91 const size_t bytesRead = fStream->read(data->writable_data(), size); | |
| 92 if (bytesRead < size) { | |
| 93 data.reset(SkData::NewSubset(data.get(), 0, bytesRead)); | |
| 94 } | |
| 95 } else { | |
| 96 const size_t alreadyBuffered = fStreamBuffer.bytesWritten() - offset ; | |
| 97 if (!fStreamBuffer.read(data->writable_data(), offset, alreadyBuffer ed)) { | |
| 98 return nullptr; | |
| 99 } | |
| 100 | |
| 101 const size_t remaining = size - alreadyBuffered; | |
| 102 auto* dst = static_cast<uint8_t*>(data->writable_data()) + alreadyBu ffered; | |
|
scroggo
2016/01/19 21:05:44
nit: this could go inside the if (remaining) block
yujieqin
2016/01/20 12:53:04
Done.
| |
| 103 if (remaining) { | |
| 104 const size_t bytesRead = fStream->read(dst, remaining); | |
| 105 size_t newSize; | |
| 106 if (bytesRead < remaining) { | |
| 107 if (safe_add_to_size_t(alreadyBuffered, bytesRead, &newSize) ) { | |
|
scroggo
2016/01/19 21:05:44
I think this should say if (!safe_add_to_size_t(..
yujieqin
2016/01/20 12:53:04
Oh yes, typo :(
But this one could potentially fa
scroggo
2016/01/20 18:13:36
Agreed. But won't a huge "size" cause problems in
yujieqin
2016/01/20 21:42:19
I don't think there will be issue with a huge "siz
scroggo
2016/01/21 16:36:09
Streams are mostly in Skia for images and typeface
yujieqin
2016/01/21 17:44:25
En... good to know.
Could we maybe leave it as it
scroggo
2016/01/21 17:57:43
Well, he's just added code to "fuzz" our image dec
| |
| 108 return nullptr; | |
| 109 } | |
| 110 data.reset(SkData::NewSubset(data.get(), 0, newSize)); | |
| 111 } | |
| 112 } | |
| 113 } | |
| 114 return new SkMemoryStream(data); | |
| 115 } | |
| 116 | |
| 117 // For PIEX | |
| 118 ::piex::Error GetData(const size_t offset, const size_t length, | |
| 119 uint8* data) override { | |
| 120 if (offset == 0 && length == 0) { | |
| 121 return ::piex::Error::kOk; | |
| 122 } | |
| 123 size_t sum; | |
| 124 if (!safe_add_to_size_t(offset, length, &sum) || !this->bufferMoreData(s um)) { | |
| 125 return ::piex::Error::kFail; | |
| 126 } | |
| 127 if (!fStreamBuffer.read(data, offset, length)) { | |
| 128 return ::piex::Error::kFail; | |
| 129 } | |
| 130 return ::piex::Error::kOk; | |
| 131 } | |
| 132 | |
| 133 protected: | |
| 134 // For dng_stream | |
| 135 uint64 DoGetLength() override { | |
| 136 if (!this->bufferMoreData(kReadToEnd)) { // read whole stream | |
| 137 ThrowReadFile(); | |
| 138 } | |
| 139 return fStreamBuffer.bytesWritten(); | |
| 140 } | |
| 141 | |
| 142 // For dng_stream | |
| 143 void DoRead(void* data, uint32 count, uint64 offset) override { | |
| 144 if (count == 0 && offset == 0) { | |
| 145 return; | |
| 146 } | |
| 147 size_t sum; | |
| 148 if (!safe_add_to_size_t(static_cast<uint64>(count), offset, &sum) || | |
| 149 !this->bufferMoreData(sum)) { | |
| 150 ThrowReadFile(); | |
| 151 } | |
| 152 | |
| 153 if (!fStreamBuffer.read(data, offset, count)) { | |
| 154 ThrowReadFile(); | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 private: | |
| 159 // Note: if the newSize == kReadToEnd (0), this function will read to the en d of stream. | |
| 160 bool bufferMoreData(size_t newSize) { | |
| 161 if (newSize == kReadToEnd) { | |
| 162 if (fWholeStreamRead) { // already read-to-end. | |
| 163 return true; | |
| 164 } | |
| 165 | |
| 166 // TODO: optimize for the special case when the input is SkMemoryStr eam. | |
| 167 return SkStreamCopy(&fStreamBuffer, fStream.get()); | |
| 168 } | |
| 169 | |
| 170 if (newSize <= fStreamBuffer.bytesWritten()) { // already buffered to n ewSize | |
| 171 return true; | |
| 172 } | |
| 173 if (fWholeStreamRead) { // newSize is larger than the whole stream. | |
| 174 return false; | |
| 175 } | |
| 176 | |
| 177 const size_t sizeToRead = newSize - fStreamBuffer.bytesWritten(); | |
| 178 SkAutoTMalloc<uint8> tempBuffer(sizeToRead); | |
| 179 const size_t bytesRead = fStream->read(tempBuffer.get(), sizeToRead); | |
| 180 if (bytesRead != sizeToRead) { | |
| 181 return false; | |
| 182 } | |
| 183 return fStreamBuffer.write(tempBuffer.get(), bytesRead); | |
| 184 } | |
| 185 | |
| 186 SkAutoTDelete<SkStream> fStream; | |
| 187 bool fWholeStreamRead; | |
| 188 | |
| 189 SkDynamicMemoryWStream fStreamBuffer; | |
| 190 | |
| 191 const size_t kReadToEnd = 0; | |
| 192 }; | |
| 193 | |
| 194 class SkDngImage { | |
| 195 public: | |
| 196 static SkDngImage* NewFromStream(SkRawStream* stream) { | |
| 197 SkAutoTDelete<SkDngMemoryAllocator> allocator(new SkDngMemoryAllocator); | |
|
scroggo
2016/01/19 21:05:44
Is it possible to avoid creating and passing aroun
yujieqin
2016/01/20 12:53:04
* Unfortunately, we can not reuse fHost and fInfo
| |
| 198 SkAutoTDelete<dng_host> host(new dng_host(allocator.get())); | |
| 199 SkAutoTDelete<dng_info> info(new dng_info); | |
| 200 SkAutoTDelete<dng_negative> negative(SkDngImage::ReadDng(stream, host.ge t(), info.get())); | |
| 201 if (!negative) { | |
| 202 return nullptr; | |
| 203 } | |
| 204 return new SkDngImage(stream, allocator.release(), host.release(), info. release(), | |
| 205 negative.release()); | |
| 206 } | |
| 207 | |
| 208 /* | |
| 209 * Renders the DNG image to the size. The DNG SDK only allows scaling close to integer factors | |
| 210 * down to 80 pixels on the short edge. The rendered image will be close to the specified size, | |
| 211 * but there is no guarantee that any of the edges will match the requested size. E.g. | |
| 212 * 100% size: 4000 x 3000 | |
| 213 * requested size: 1600 x 1200 | |
| 214 * returned size could be: 2000 x 1500 | |
| 215 */ | |
| 216 dng_image* render(int width, int height) { | |
| 217 // render() takes ownership of fHost, fInfo and fNegative when available . | |
| 218 SkAutoTDelete<dng_host> host; | |
| 219 SkAutoTDelete<dng_info> info; | |
| 220 SkAutoTDelete<dng_negative> negative; | |
| 221 if (!fHost || !fInfo || !fNegative) { | |
| 222 SkCodecPrintf("Warning: SkDngImage::render() is called multiple time s."); | |
| 223 host.reset(new dng_host(fAllocator.get())); | |
| 224 info.reset(new dng_info); | |
| 225 negative.reset(SkDngImage::ReadDng(fStream.get(), host.get(), info.g et())); | |
| 226 if (!negative) { | |
| 227 return nullptr; | |
| 228 } | |
| 229 } else { | |
| 230 host.reset(fHost.release()); | |
| 231 info.reset(fInfo.release()); | |
| 232 negative.reset(fNegative.release()); | |
| 233 } | |
| 234 | |
| 235 // DNG SDK preserves the aspect ratio, so it only needs to know the long er dimension. | |
| 236 const int preferredSize = SkTMax(width, height); | |
| 237 try { | |
| 238 host->SetPreferredSize(preferredSize); | |
| 239 host->ValidateSizes(); | |
| 240 | |
| 241 negative->ReadStage1Image(*host, *fStream, *info); | |
| 242 | |
| 243 if (info->fMaskIndex != -1) { | |
| 244 negative->ReadTransparencyMask(*host, *fStream, *info); | |
| 245 } | |
| 246 | |
| 247 negative->ValidateRawImageDigest(*host); | |
| 248 if (negative->IsDamaged()) { | |
| 249 return nullptr; | |
| 250 } | |
| 251 | |
| 252 const int32 kMosaicPlane = -1; | |
| 253 negative->BuildStage2Image(*host); | |
| 254 negative->BuildStage3Image(*host, kMosaicPlane); | |
| 255 | |
| 256 dng_render render(*host, *negative); | |
| 257 render.SetFinalSpace(dng_space_sRGB::Get()); | |
| 258 render.SetFinalPixelType(ttByte); | |
| 259 | |
| 260 dng_point stage3_size = negative->Stage3Image()->Size(); | |
| 261 render.SetMaximumSize(SkTMax(stage3_size.h, stage3_size.v)); | |
| 262 | |
| 263 return render.Render(); | |
| 264 } catch (...) { | |
| 265 return nullptr; | |
| 266 } | |
| 267 } | |
| 268 | |
| 269 const SkImageInfo& getImageInfo() const { | |
| 270 return fImageInfo; | |
| 271 } | |
| 272 | |
| 273 bool IsXtransImage() const { | |
| 274 return fIsXtransImage; | |
| 275 } | |
| 276 | |
| 277 private: | |
| 278 static dng_negative* ReadDng(SkRawStream* stream, dng_host* host, dng_info* info) { | |
| 279 try { | |
| 280 host->ValidateSizes(); | |
| 281 info->Parse(*host, *stream); | |
| 282 info->PostParse(*host); | |
| 283 if (!info->IsValidDNG()) { | |
| 284 return nullptr; | |
| 285 } | |
| 286 | |
| 287 SkAutoTDelete<dng_negative> negative; | |
| 288 negative.reset(host->Make_dng_negative()); | |
| 289 negative->Parse(*host, *stream, *info); | |
| 290 negative->PostParse(*host, *stream, *info); | |
| 291 negative->SynchronizeMetadata(); | |
| 292 return negative.release(); | |
| 293 } catch (...) { | |
| 294 return nullptr; | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 SkDngImage(SkRawStream* stream, SkDngMemoryAllocator* allocator, dng_host* h ost, | |
| 299 dng_info* info, dng_negative* negative) | |
| 300 : fStream(stream) | |
| 301 , fAllocator(allocator) | |
| 302 , fHost(host) | |
| 303 , fInfo(info) | |
| 304 , fNegative(negative) | |
| 305 , fImageInfo(SkImageInfo::Make(fNegative->DefaultCropSizeH().As_real64() , | |
| 306 fNegative->DefaultCropSizeV().As_real64() , | |
| 307 kN32_SkColorType, kOpaque_SkAlphaType)) | |
| 308 , fIsXtransImage(negative && negative->GetMosaicInfo() != nullptr | |
|
scroggo
2016/01/19 21:05:44
I don't think you need to check "negative" here -
yujieqin
2016/01/20 12:53:04
Done
| |
| 309 ? (negative->GetMosaicInfo()->fCFAPatternSize.v == 6 | |
| 310 && negative->GetMosaicInfo()->fCFAPatternSize.h == 6 ) | |
| 311 : false) {} | |
| 312 | |
| 313 SkAutoTDelete<SkRawStream> fStream; | |
| 314 SkAutoTDelete<SkDngMemoryAllocator> fAllocator; | |
| 315 | |
| 316 SkAutoTDelete<dng_host> fHost; | |
| 317 SkAutoTDelete<dng_info> fInfo; | |
| 318 SkAutoTDelete<dng_negative> fNegative; | |
| 319 | |
| 320 const SkImageInfo fImageInfo; | |
| 321 const bool fIsXtransImage; | |
| 322 }; | |
| 323 | |
| 324 /* | |
| 325 * Tries to handle the image with PIEX. If PIEX returns kOk and finds the previe w image, create a | |
| 326 * SkJpegCodec. If PIEX returns kFail, then the file is invalid, return nullptr. In other cases, | |
| 327 * fallback to create SkRawCodec for DNG images. | |
| 328 */ | |
| 329 SkCodec* SkRawCodec::NewFromStream(SkStream* stream) { | |
| 330 SkAutoTDelete<SkRawStream> rawStream(new SkRawStream(stream)); | |
| 331 ::piex::PreviewImageData imageData; | |
| 332 if (::piex::IsRaw(rawStream.get())) { | |
| 333 ::piex::Error error = ::piex::GetPreviewImageData(rawStream.get(), &imag eData); | |
| 334 | |
| 335 if (error == ::piex::Error::kOk && imageData.preview_length > 0) { | |
| 336 #if !defined(GOOGLE3) | |
| 337 // transferBuffer() is destructive to the rawStream. Abandon the raw Stream after this | |
| 338 // function call. | |
| 339 SkMemoryStream* memoryStream = | |
| 340 rawStream->transferBuffer(imageData.preview_offset, imageDat a.preview_length); | |
| 341 return memoryStream ? SkJpegCodec::NewFromStream(memoryStream) : nul lptr; | |
| 342 #else | |
| 343 return nullptr; | |
| 344 #endif | |
| 345 } else if (error == ::piex::Error::kFail) { | |
| 346 return nullptr; | |
| 347 } | |
| 348 } | |
| 349 | |
| 350 SkAutoTDelete<SkDngImage> dngImage(SkDngImage::NewFromStream(rawStream.relea se())); | |
| 351 if (!dngImage) { | |
| 352 return nullptr; | |
| 353 } | |
| 354 | |
| 355 return new SkRawCodec(dngImage.release()); | |
| 356 } | |
| 357 | |
| 358 SkCodec::Result SkRawCodec::onGetPixels(const SkImageInfo& requestedInfo, void* dst, | |
| 359 size_t dstRowBytes, const Options& optio ns, | |
| 360 SkPMColor ctable[], int* ctableCount, | |
| 361 int* rowsDecoded) { | |
| 362 const int width = requestedInfo.width(); | |
| 363 const int height = requestedInfo.height(); | |
| 364 SkAutoTDelete<dng_image> image(fDngImage->render(width, height)); | |
| 365 if (!image) { | |
| 366 return kInvalidInput; | |
| 367 } | |
| 368 | |
| 369 // Because the DNG SDK can not guarantee to render to requested size, we all ow a small | |
| 370 // difference. Only the overlapping region will be converted in onGetPixels( ). | |
| 371 const float maxDiffRatio = 1.03f; | |
| 372 const dng_point& imageSize = image->Size(); | |
| 373 if (imageSize.h / width > maxDiffRatio || imageSize.h < width || | |
| 374 imageSize.v / height > maxDiffRatio || imageSize.v < height) { | |
| 375 return SkCodec::kInvalidScale; | |
| 376 } | |
| 377 | |
| 378 void* dstRow = dst; | |
| 379 uint8_t srcRow[width * 3]; | |
| 380 SkAutoTDelete<SkSwizzler> swizzler(SkSwizzler::CreateSwizzler( | |
| 381 SkSwizzler::kRGB, nullptr, requestedInfo, options)); | |
| 382 SkASSERT(swizzler); | |
| 383 | |
| 384 dng_pixel_buffer buffer; | |
| 385 buffer.fData = &srcRow[0]; | |
| 386 buffer.fPlane = 0; | |
| 387 buffer.fPlanes = 3; | |
| 388 buffer.fColStep = buffer.fPlanes; | |
| 389 buffer.fPlaneStep = 1; | |
| 390 buffer.fPixelType = ttByte; | |
| 391 buffer.fPixelSize = sizeof(uint8_t); | |
| 392 buffer.fRowStep = sizeof(srcRow); | |
| 393 | |
| 394 for (int i = 0; i < height; ++i) { | |
| 395 buffer.fArea = dng_rect(i, 0, i + 1, width); | |
| 396 | |
| 397 try { | |
| 398 image->Get(buffer, dng_image::edge_zero); | |
| 399 } catch (...) { | |
| 400 *rowsDecoded = i; | |
| 401 return kIncompleteInput; | |
| 402 } | |
| 403 | |
| 404 swizzler->swizzle(dstRow, &srcRow[0]); | |
| 405 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); | |
| 406 } | |
| 407 return kSuccess; | |
| 408 } | |
| 409 | |
| 410 SkISize SkRawCodec::onGetScaledDimensions(float desiredScale) const { | |
| 411 SkASSERT(desiredScale <= 1.f); | |
| 412 SkISize dim = this->getInfo().dimensions(); | |
| 413 const int shortEdge = SkTMin(dim.fWidth, dim.fHeight); | |
|
scroggo
2016/01/19 21:05:44
nit: you can cast to float here, simplifying the i
yujieqin
2016/01/20 12:53:04
Done.
| |
| 414 | |
| 415 // Limits the minimun size to be 80 on the short edge. | |
|
scroggo
2016/01/19 21:05:44
minimum*
yujieqin
2016/01/20 12:53:04
Done.
| |
| 416 if (desiredScale < 80.f / static_cast<float>(shortEdge)) { | |
| 417 desiredScale = 80.f / static_cast<float>(shortEdge); | |
| 418 } | |
| 419 | |
| 420 // For Xtrans images, the integer-factor scaling does not support the half-s ize scaling case | |
| 421 // (stronger downscalings are fine). In this case, returns the non-scaled ve rsion. | |
| 422 if (fDngImage->IsXtransImage() && desiredScale >= 0.5f) { | |
| 423 return dim; | |
| 424 } | |
| 425 | |
| 426 // Round to integer-factors. | |
| 427 const float finalScale = std::floor(1.f/ desiredScale); | |
| 428 | |
| 429 dim.fWidth = std::round(dim.fWidth / finalScale); | |
| 430 dim.fHeight = std::round(dim.fHeight / finalScale); | |
| 431 return dim; | |
| 432 } | |
| 433 | |
| 434 bool SkRawCodec::onDimensionsSupported(const SkISize& dim) { | |
| 435 const SkISize fullDim = this->getInfo().dimensions(); | |
| 436 const float fullShortEdge = SkTMin(fullDim.fWidth, fullDim.fHeight); | |
| 437 const float shortEdge = SkTMin(dim.fWidth, dim.fHeight); | |
| 438 | |
| 439 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEd ge / shortEdge)); | |
| 440 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge / shortEdge)); | |
| 441 return sizeFloor == dim || sizeCeil == dim; | |
| 442 } | |
| 443 | |
| 444 SkRawCodec::~SkRawCodec() {} | |
| 445 | |
| 446 SkRawCodec::SkRawCodec(SkDngImage* dngImage) | |
| 447 : INHERITED(dngImage->getImageInfo(), nullptr) | |
| 448 , fDngImage(dngImage) {} | |
| OLD | NEW |