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 "include/codec/SkCodec.h"
9#include "include/core/SkData.h"
10#include "include/core/SkRefCnt.h"
11#include "include/core/SkStream.h"
12#include "include/core/SkTypes.h"
13#include "include/private/SkColorData.h"
14#include "include/private/SkMutex.h"
15#include "include/private/SkTArray.h"
16#include "include/private/SkTemplates.h"
17#include "src/codec/SkCodecPriv.h"
18#include "src/codec/SkJpegCodec.h"
19#include "src/codec/SkRawCodec.h"
20#include "src/core/SkColorSpacePriv.h"
21#include "src/core/SkStreamPriv.h"
22#include "src/core/SkTaskGroup.h"
23
24#include "dng_area_task.h"
25#include "dng_color_space.h"
26#include "dng_errors.h"
27#include "dng_exceptions.h"
28#include "dng_host.h"
29#include "dng_info.h"
30#include "dng_memory.h"
31#include "dng_render.h"
32#include "dng_stream.h"
33
34#include "src/piex.h"
35
36#include <cmath> // for std::round,floor,ceil
37#include <limits>
38
39namespace {
40
41// Caluclates the number of tiles of tile_size that fit into the area in vertical and horizontal
42// directions.
43dng_point num_tiles_in_area(const dng_point &areaSize,
44 const dng_point_real64 &tileSize) {
45 // FIXME: Add a ceil_div() helper in SkCodecPriv.h
46 return dng_point(static_cast<int32>((areaSize.v + tileSize.v - 1) / tileSize.v),
47 static_cast<int32>((areaSize.h + tileSize.h - 1) / tileSize.h));
48}
49
50int num_tasks_required(const dng_point& tilesInTask,
51 const dng_point& tilesInArea) {
52 return ((tilesInArea.v + tilesInTask.v - 1) / tilesInTask.v) *
53 ((tilesInArea.h + tilesInTask.h - 1) / tilesInTask.h);
54}
55
56// Calculate the number of tiles to process per task, taking into account the maximum number of
57// tasks. It prefers to increase horizontally for better locality of reference.
58dng_point num_tiles_per_task(const int maxTasks,
59 const dng_point &tilesInArea) {
60 dng_point tilesInTask = {1, 1};
61 while (num_tasks_required(tilesInTask, tilesInArea) > maxTasks) {
62 if (tilesInTask.h < tilesInArea.h) {
63 ++tilesInTask.h;
64 } else if (tilesInTask.v < tilesInArea.v) {
65 ++tilesInTask.v;
66 } else {
67 ThrowProgramError("num_tiles_per_task calculation is wrong.");
68 }
69 }
70 return tilesInTask;
71}
72
73std::vector<dng_rect> compute_task_areas(const int maxTasks, const dng_rect& area,
74 const dng_point& tileSize) {
75 std::vector<dng_rect> taskAreas;
76 const dng_point tilesInArea = num_tiles_in_area(area.Size(), tileSize);
77 const dng_point tilesPerTask = num_tiles_per_task(maxTasks, tilesInArea);
78 const dng_point taskAreaSize = {tilesPerTask.v * tileSize.v,
79 tilesPerTask.h * tileSize.h};
80 for (int v = 0; v < tilesInArea.v; v += tilesPerTask.v) {
81 for (int h = 0; h < tilesInArea.h; h += tilesPerTask.h) {
82 dng_rect taskArea;
83 taskArea.t = area.t + v * tileSize.v;
84 taskArea.l = area.l + h * tileSize.h;
85 taskArea.b = Min_int32(taskArea.t + taskAreaSize.v, area.b);
86 taskArea.r = Min_int32(taskArea.l + taskAreaSize.h, area.r);
87
88 taskAreas.push_back(taskArea);
89 }
90 }
91 return taskAreas;
92}
93
94class SkDngHost : public dng_host {
95public:
96 explicit SkDngHost(dng_memory_allocator* allocater) : dng_host(allocater) {}
97
98 void PerformAreaTask(dng_area_task& task, const dng_rect& area) override {
99 SkTaskGroup taskGroup;
100
101 // tileSize is typically 256x256
102 const dng_point tileSize(task.FindTileSize(area));
103 const std::vector<dng_rect> taskAreas = compute_task_areas(this->PerformAreaTaskThreads(),
104 area, tileSize);
105 const int numTasks = static_cast<int>(taskAreas.size());
106
107 SkMutex mutex;
108 SkTArray<dng_exception> exceptions;
109 task.Start(numTasks, tileSize, &Allocator(), Sniffer());
110 for (int taskIndex = 0; taskIndex < numTasks; ++taskIndex) {
111 taskGroup.add([&mutex, &exceptions, &task, this, taskIndex, taskAreas, tileSize] {
112 try {
113 task.ProcessOnThread(taskIndex, taskAreas[taskIndex], tileSize, this->Sniffer());
114 } catch (dng_exception& exception) {
115 SkAutoMutexExclusive lock(mutex);
116 exceptions.push_back(exception);
117 } catch (...) {
118 SkAutoMutexExclusive lock(mutex);
119 exceptions.push_back(dng_exception(dng_error_unknown));
120 }
121 });
122 }
123
124 taskGroup.wait();
125 task.Finish(numTasks);
126
127 // We only re-throw the first exception.
128 if (!exceptions.empty()) {
129 Throw_dng_error(exceptions.front().ErrorCode(), nullptr, nullptr);
130 }
131 }
132
133 uint32 PerformAreaTaskThreads() override {
134#ifdef SK_BUILD_FOR_ANDROID
135 // Only use 1 thread. DNGs with the warp effect require a lot of memory,
136 // and the amount of memory required scales linearly with the number of
137 // threads. The sample used in CTS requires over 500 MB, so even two
138 // threads is significantly expensive. There is no good way to tell
139 // whether the image has the warp effect.
140 return 1;
141#else
142 return kMaxMPThreads;
143#endif
144 }
145
146private:
147 typedef dng_host INHERITED;
148};
149
150// T must be unsigned type.
151template <class T>
152bool safe_add_to_size_t(T arg1, T arg2, size_t* result) {
153 SkASSERT(arg1 >= 0);
154 SkASSERT(arg2 >= 0);
155 if (arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) {
156 T sum = arg1 + arg2;
157 if (sum <= std::numeric_limits<size_t>::max()) {
158 *result = static_cast<size_t>(sum);
159 return true;
160 }
161 }
162 return false;
163}
164
165bool is_asset_stream(const SkStream& stream) {
166 return stream.hasLength() && stream.hasPosition();
167}
168
169} // namespace
170
171class SkRawStream {
172public:
173 virtual ~SkRawStream() {}
174
175 /*
176 * Gets the length of the stream. Depending on the type of stream, this may require reading to
177 * the end of the stream.
178 */
179 virtual uint64 getLength() = 0;
180
181 virtual bool read(void* data, size_t offset, size_t length) = 0;
182
183 /*
184 * Creates an SkMemoryStream from the offset with size.
185 * Note: for performance reason, this function is destructive to the SkRawStream. One should
186 * abandon current object after the function call.
187 */
188 virtual std::unique_ptr<SkMemoryStream> transferBuffer(size_t offset, size_t size) = 0;
189};
190
191class SkRawLimitedDynamicMemoryWStream : public SkDynamicMemoryWStream {
192public:
193 ~SkRawLimitedDynamicMemoryWStream() override {}
194
195 bool write(const void* buffer, size_t size) override {
196 size_t newSize;
197 if (!safe_add_to_size_t(this->bytesWritten(), size, &newSize) ||
198 newSize > kMaxStreamSize)
199 {
200 SkCodecPrintf("Error: Stream size exceeds the limit.\n");
201 return false;
202 }
203 return this->INHERITED::write(buffer, size);
204 }
205
206private:
207 // Most of valid RAW images will not be larger than 100MB. This limit is helpful to avoid
208 // streaming too large data chunk. We can always adjust the limit here if we need.
209 const size_t kMaxStreamSize = 100 * 1024 * 1024; // 100MB
210
211 typedef SkDynamicMemoryWStream INHERITED;
212};
213
214// Note: the maximum buffer size is 100MB (limited by SkRawLimitedDynamicMemoryWStream).
215class SkRawBufferedStream : public SkRawStream {
216public:
217 explicit SkRawBufferedStream(std::unique_ptr<SkStream> stream)
218 : fStream(std::move(stream))
219 , fWholeStreamRead(false)
220 {
221 // Only use SkRawBufferedStream when the stream is not an asset stream.
222 SkASSERT(!is_asset_stream(*fStream));
223 }
224
225 ~SkRawBufferedStream() override {}
226
227 uint64 getLength() override {
228 if (!this->bufferMoreData(kReadToEnd)) { // read whole stream
229 ThrowReadFile();
230 }
231 return fStreamBuffer.bytesWritten();
232 }
233
234 bool read(void* data, size_t offset, size_t length) override {
235 if (length == 0) {
236 return true;
237 }
238
239 size_t sum;
240 if (!safe_add_to_size_t(offset, length, &sum)) {
241 return false;
242 }
243
244 return this->bufferMoreData(sum) && fStreamBuffer.read(data, offset, length);
245 }
246
247 std::unique_ptr<SkMemoryStream> transferBuffer(size_t offset, size_t size) override {
248 sk_sp<SkData> data(SkData::MakeUninitialized(size));
249 if (offset > fStreamBuffer.bytesWritten()) {
250 // If the offset is not buffered, read from fStream directly and skip the buffering.
251 const size_t skipLength = offset - fStreamBuffer.bytesWritten();
252 if (fStream->skip(skipLength) != skipLength) {
253 return nullptr;
254 }
255 const size_t bytesRead = fStream->read(data->writable_data(), size);
256 if (bytesRead < size) {
257 data = SkData::MakeSubset(data.get(), 0, bytesRead);
258 }
259 } else {
260 const size_t alreadyBuffered = std::min(fStreamBuffer.bytesWritten() - offset, size);
261 if (alreadyBuffered > 0 &&
262 !fStreamBuffer.read(data->writable_data(), offset, alreadyBuffered)) {
263 return nullptr;
264 }
265
266 const size_t remaining = size - alreadyBuffered;
267 if (remaining) {
268 auto* dst = static_cast<uint8_t*>(data->writable_data()) + alreadyBuffered;
269 const size_t bytesRead = fStream->read(dst, remaining);
270 size_t newSize;
271 if (bytesRead < remaining) {
272 if (!safe_add_to_size_t(alreadyBuffered, bytesRead, &newSize)) {
273 return nullptr;
274 }
275 data = SkData::MakeSubset(data.get(), 0, newSize);
276 }
277 }
278 }
279 return SkMemoryStream::Make(data);
280 }
281
282private:
283 // Note: if the newSize == kReadToEnd (0), this function will read to the end of stream.
284 bool bufferMoreData(size_t newSize) {
285 if (newSize == kReadToEnd) {
286 if (fWholeStreamRead) { // already read-to-end.
287 return true;
288 }
289
290 // TODO: optimize for the special case when the input is SkMemoryStream.
291 return SkStreamCopy(&fStreamBuffer, fStream.get());
292 }
293
294 if (newSize <= fStreamBuffer.bytesWritten()) { // already buffered to newSize
295 return true;
296 }
297 if (fWholeStreamRead) { // newSize is larger than the whole stream.
298 return false;
299 }
300
301 // Try to read at least 8192 bytes to avoid to many small reads.
302 const size_t kMinSizeToRead = 8192;
303 const size_t sizeRequested = newSize - fStreamBuffer.bytesWritten();
304 const size_t sizeToRead = std::max(kMinSizeToRead, sizeRequested);
305 SkAutoSTMalloc<kMinSizeToRead, uint8> tempBuffer(sizeToRead);
306 const size_t bytesRead = fStream->read(tempBuffer.get(), sizeToRead);
307 if (bytesRead < sizeRequested) {
308 return false;
309 }
310 return fStreamBuffer.write(tempBuffer.get(), bytesRead);
311 }
312
313 std::unique_ptr<SkStream> fStream;
314 bool fWholeStreamRead;
315
316 // Use a size-limited stream to avoid holding too huge buffer.
317 SkRawLimitedDynamicMemoryWStream fStreamBuffer;
318
319 const size_t kReadToEnd = 0;
320};
321
322class SkRawAssetStream : public SkRawStream {
323public:
324 explicit SkRawAssetStream(std::unique_ptr<SkStream> stream)
325 : fStream(std::move(stream))
326 {
327 // Only use SkRawAssetStream when the stream is an asset stream.
328 SkASSERT(is_asset_stream(*fStream));
329 }
330
331 ~SkRawAssetStream() override {}
332
333 uint64 getLength() override {
334 return fStream->getLength();
335 }
336
337
338 bool read(void* data, size_t offset, size_t length) override {
339 if (length == 0) {
340 return true;
341 }
342
343 size_t sum;
344 if (!safe_add_to_size_t(offset, length, &sum)) {
345 return false;
346 }
347
348 return fStream->seek(offset) && (fStream->read(data, length) == length);
349 }
350
351 std::unique_ptr<SkMemoryStream> transferBuffer(size_t offset, size_t size) override {
352 if (fStream->getLength() < offset) {
353 return nullptr;
354 }
355
356 size_t sum;
357 if (!safe_add_to_size_t(offset, size, &sum)) {
358 return nullptr;
359 }
360
361 // This will allow read less than the requested "size", because the JPEG codec wants to
362 // handle also a partial JPEG file.
363 const size_t bytesToRead = std::min(sum, fStream->getLength()) - offset;
364 if (bytesToRead == 0) {
365 return nullptr;
366 }
367
368 if (fStream->getMemoryBase()) { // directly copy if getMemoryBase() is available.
369 sk_sp<SkData> data(SkData::MakeWithCopy(
370 static_cast<const uint8_t*>(fStream->getMemoryBase()) + offset, bytesToRead));
371 fStream.reset();
372 return SkMemoryStream::Make(data);
373 } else {
374 sk_sp<SkData> data(SkData::MakeUninitialized(bytesToRead));
375 if (!fStream->seek(offset)) {
376 return nullptr;
377 }
378 const size_t bytesRead = fStream->read(data->writable_data(), bytesToRead);
379 if (bytesRead < bytesToRead) {
380 data = SkData::MakeSubset(data.get(), 0, bytesRead);
381 }
382 return SkMemoryStream::Make(data);
383 }
384 }
385private:
386 std::unique_ptr<SkStream> fStream;
387};
388
389class SkPiexStream : public ::piex::StreamInterface {
390public:
391 // Will NOT take the ownership of the stream.
392 explicit SkPiexStream(SkRawStream* stream) : fStream(stream) {}
393
394 ~SkPiexStream() override {}
395
396 ::piex::Error GetData(const size_t offset, const size_t length,
397 uint8* data) override {
398 return fStream->read(static_cast<void*>(data), offset, length) ?
399 ::piex::Error::kOk : ::piex::Error::kFail;
400 }
401
402private:
403 SkRawStream* fStream;
404};
405
406class SkDngStream : public dng_stream {
407public:
408 // Will NOT take the ownership of the stream.
409 SkDngStream(SkRawStream* stream) : fStream(stream) {}
410
411 ~SkDngStream() override {}
412
413 uint64 DoGetLength() override { return fStream->getLength(); }
414
415 void DoRead(void* data, uint32 count, uint64 offset) override {
416 size_t sum;
417 if (!safe_add_to_size_t(static_cast<uint64>(count), offset, &sum) ||
418 !fStream->read(data, static_cast<size_t>(offset), static_cast<size_t>(count))) {
419 ThrowReadFile();
420 }
421 }
422
423private:
424 SkRawStream* fStream;
425};
426
427class SkDngImage {
428public:
429 /*
430 * Initializes the object with the information from Piex in a first attempt. This way it can
431 * save time and storage to obtain the DNG dimensions and color filter array (CFA) pattern
432 * which is essential for the demosaicing of the sensor image.
433 * Note: this will take the ownership of the stream.
434 */
435 static SkDngImage* NewFromStream(SkRawStream* stream) {
436 std::unique_ptr<SkDngImage> dngImage(new SkDngImage(stream));
437#if defined(IS_FUZZING_WITH_LIBFUZZER)
438 // Libfuzzer easily runs out of memory after here. To avoid that
439 // We just pretend all streams are invalid. Our AFL-fuzzer
440 // should still exercise this code; it's more resistant to OOM.
441 return nullptr;
442#endif
443 if (!dngImage->initFromPiex() && !dngImage->readDng()) {
444 return nullptr;
445 }
446
447 return dngImage.release();
448 }
449
450 /*
451 * Renders the DNG image to the size. The DNG SDK only allows scaling close to integer factors
452 * down to 80 pixels on the short edge. The rendered image will be close to the specified size,
453 * but there is no guarantee that any of the edges will match the requested size. E.g.
454 * 100% size: 4000 x 3000
455 * requested size: 1600 x 1200
456 * returned size could be: 2000 x 1500
457 */
458 dng_image* render(int width, int height) {
459 if (!fHost || !fInfo || !fNegative || !fDngStream) {
460 if (!this->readDng()) {
461 return nullptr;
462 }
463 }
464
465 // DNG SDK preserves the aspect ratio, so it only needs to know the longer dimension.
466 const int preferredSize = std::max(width, height);
467 try {
468 // render() takes ownership of fHost, fInfo, fNegative and fDngStream when available.
469 std::unique_ptr<dng_host> host(fHost.release());
470 std::unique_ptr<dng_info> info(fInfo.release());
471 std::unique_ptr<dng_negative> negative(fNegative.release());
472 std::unique_ptr<dng_stream> dngStream(fDngStream.release());
473
474 host->SetPreferredSize(preferredSize);
475 host->ValidateSizes();
476
477 negative->ReadStage1Image(*host, *dngStream, *info);
478
479 if (info->fMaskIndex != -1) {
480 negative->ReadTransparencyMask(*host, *dngStream, *info);
481 }
482
483 negative->ValidateRawImageDigest(*host);
484 if (negative->IsDamaged()) {
485 return nullptr;
486 }
487
488 const int32 kMosaicPlane = -1;
489 negative->BuildStage2Image(*host);
490 negative->BuildStage3Image(*host, kMosaicPlane);
491
492 dng_render render(*host, *negative);
493 render.SetFinalSpace(dng_space_sRGB::Get());
494 render.SetFinalPixelType(ttByte);
495
496 dng_point stage3_size = negative->Stage3Image()->Size();
497 render.SetMaximumSize(std::max(stage3_size.h, stage3_size.v));
498
499 return render.Render();
500 } catch (...) {
501 return nullptr;
502 }
503 }
504
505 int width() const {
506 return fWidth;
507 }
508
509 int height() const {
510 return fHeight;
511 }
512
513 bool isScalable() const {
514 return fIsScalable;
515 }
516
517 bool isXtransImage() const {
518 return fIsXtransImage;
519 }
520
521 // Quick check if the image contains a valid TIFF header as requested by DNG format.
522 // Does not affect ownership of stream.
523 static bool IsTiffHeaderValid(SkRawStream* stream) {
524 const size_t kHeaderSize = 4;
525 unsigned char header[kHeaderSize];
526 if (!stream->read(header, 0 /* offset */, kHeaderSize)) {
527 return false;
528 }
529
530 // Check if the header is valid (endian info and magic number "42").
531 bool littleEndian;
532 if (!is_valid_endian_marker(header, &littleEndian)) {
533 return false;
534 }
535
536 return 0x2A == get_endian_short(header + 2, littleEndian);
537 }
538
539private:
540 bool init(int width, int height, const dng_point& cfaPatternSize) {
541 fWidth = width;
542 fHeight = height;
543
544 // The DNG SDK scales only during demosaicing, so scaling is only possible when
545 // a mosaic info is available.
546 fIsScalable = cfaPatternSize.v != 0 && cfaPatternSize.h != 0;
547 fIsXtransImage = fIsScalable ? (cfaPatternSize.v == 6 && cfaPatternSize.h == 6) : false;
548
549 return width > 0 && height > 0;
550 }
551
552 bool initFromPiex() {
553 // Does not take the ownership of rawStream.
554 SkPiexStream piexStream(fStream.get());
555 ::piex::PreviewImageData imageData;
556 if (::piex::IsRaw(&piexStream)
557 && ::piex::GetPreviewImageData(&piexStream, &imageData) == ::piex::Error::kOk)
558 {
559 dng_point cfaPatternSize(imageData.cfa_pattern_dim[1], imageData.cfa_pattern_dim[0]);
560 return this->init(static_cast<int>(imageData.full_width),
561 static_cast<int>(imageData.full_height), cfaPatternSize);
562 }
563 return false;
564 }
565
566 bool readDng() {
567 try {
568 // Due to the limit of DNG SDK, we need to reset host and info.
569 fHost.reset(new SkDngHost(&fAllocator));
570 fInfo.reset(new dng_info);
571 fDngStream.reset(new SkDngStream(fStream.get()));
572
573 fHost->ValidateSizes();
574 fInfo->Parse(*fHost, *fDngStream);
575 fInfo->PostParse(*fHost);
576 if (!fInfo->IsValidDNG()) {
577 return false;
578 }
579
580 fNegative.reset(fHost->Make_dng_negative());
581 fNegative->Parse(*fHost, *fDngStream, *fInfo);
582 fNegative->PostParse(*fHost, *fDngStream, *fInfo);
583 fNegative->SynchronizeMetadata();
584
585 dng_point cfaPatternSize(0, 0);
586 if (fNegative->GetMosaicInfo() != nullptr) {
587 cfaPatternSize = fNegative->GetMosaicInfo()->fCFAPatternSize;
588 }
589 return this->init(static_cast<int>(fNegative->DefaultCropSizeH().As_real64()),
590 static_cast<int>(fNegative->DefaultCropSizeV().As_real64()),
591 cfaPatternSize);
592 } catch (...) {
593 return false;
594 }
595 }
596
597 SkDngImage(SkRawStream* stream)
598 : fStream(stream)
599 {}
600
601 dng_memory_allocator fAllocator;
602 std::unique_ptr<SkRawStream> fStream;
603 std::unique_ptr<dng_host> fHost;
604 std::unique_ptr<dng_info> fInfo;
605 std::unique_ptr<dng_negative> fNegative;
606 std::unique_ptr<dng_stream> fDngStream;
607
608 int fWidth;
609 int fHeight;
610 bool fIsScalable;
611 bool fIsXtransImage;
612};
613
614/*
615 * Tries to handle the image with PIEX. If PIEX returns kOk and finds the preview image, create a
616 * SkJpegCodec. If PIEX returns kFail, then the file is invalid, return nullptr. In other cases,
617 * fallback to create SkRawCodec for DNG images.
618 */
619std::unique_ptr<SkCodec> SkRawCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
620 Result* result) {
621 std::unique_ptr<SkRawStream> rawStream;
622 if (is_asset_stream(*stream)) {
623 rawStream.reset(new SkRawAssetStream(std::move(stream)));
624 } else {
625 rawStream.reset(new SkRawBufferedStream(std::move(stream)));
626 }
627
628 // Does not take the ownership of rawStream.
629 SkPiexStream piexStream(rawStream.get());
630 ::piex::PreviewImageData imageData;
631 if (::piex::IsRaw(&piexStream)) {
632 ::piex::Error error = ::piex::GetPreviewImageData(&piexStream, &imageData);
633 if (error == ::piex::Error::kFail) {
634 *result = kInvalidInput;
635 return nullptr;
636 }
637
638 std::unique_ptr<SkEncodedInfo::ICCProfile> profile;
639 if (imageData.color_space == ::piex::PreviewImageData::kAdobeRgb) {
640 skcms_ICCProfile skcmsProfile;
641 skcms_Init(&skcmsProfile);
642 skcms_SetTransferFunction(&skcmsProfile, &SkNamedTransferFn::k2Dot2);
643 skcms_SetXYZD50(&skcmsProfile, &SkNamedGamut::kAdobeRGB);
644 profile = SkEncodedInfo::ICCProfile::Make(skcmsProfile);
645 }
646
647 // Theoretically PIEX can return JPEG compressed image or uncompressed RGB image. We only
648 // handle the JPEG compressed preview image here.
649 if (error == ::piex::Error::kOk && imageData.preview.length > 0 &&
650 imageData.preview.format == ::piex::Image::kJpegCompressed)
651 {
652 // transferBuffer() is destructive to the rawStream. Abandon the rawStream after this
653 // function call.
654 // FIXME: one may avoid the copy of memoryStream and use the buffered rawStream.
655 auto memoryStream = rawStream->transferBuffer(imageData.preview.offset,
656 imageData.preview.length);
657 if (!memoryStream) {
658 *result = kInvalidInput;
659 return nullptr;
660 }
661 return SkJpegCodec::MakeFromStream(std::move(memoryStream), result,
662 std::move(profile));
663 }
664 }
665
666 if (!SkDngImage::IsTiffHeaderValid(rawStream.get())) {
667 *result = kUnimplemented;
668 return nullptr;
669 }
670
671 // Takes the ownership of the rawStream.
672 std::unique_ptr<SkDngImage> dngImage(SkDngImage::NewFromStream(rawStream.release()));
673 if (!dngImage) {
674 *result = kInvalidInput;
675 return nullptr;
676 }
677
678 *result = kSuccess;
679 return std::unique_ptr<SkCodec>(new SkRawCodec(dngImage.release()));
680}
681
682SkCodec::Result SkRawCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst,
683 size_t dstRowBytes, const Options& options,
684 int* rowsDecoded) {
685 const int width = dstInfo.width();
686 const int height = dstInfo.height();
687 std::unique_ptr<dng_image> image(fDngImage->render(width, height));
688 if (!image) {
689 return kInvalidInput;
690 }
691
692 // Because the DNG SDK can not guarantee to render to requested size, we allow a small
693 // difference. Only the overlapping region will be converted.
694 const float maxDiffRatio = 1.03f;
695 const dng_point& imageSize = image->Size();
696 if (imageSize.h / (float) width > maxDiffRatio || imageSize.h < width ||
697 imageSize.v / (float) height > maxDiffRatio || imageSize.v < height) {
698 return SkCodec::kInvalidScale;
699 }
700
701 void* dstRow = dst;
702 SkAutoTMalloc<uint8_t> srcRow(width * 3);
703
704 dng_pixel_buffer buffer;
705 buffer.fData = &srcRow[0];
706 buffer.fPlane = 0;
707 buffer.fPlanes = 3;
708 buffer.fColStep = buffer.fPlanes;
709 buffer.fPlaneStep = 1;
710 buffer.fPixelType = ttByte;
711 buffer.fPixelSize = sizeof(uint8_t);
712 buffer.fRowStep = width * 3;
713
714 constexpr auto srcFormat = skcms_PixelFormat_RGB_888;
715 skcms_PixelFormat dstFormat;
716 if (!sk_select_xform_format(dstInfo.colorType(), false, &dstFormat)) {
717 return kInvalidConversion;
718 }
719
720 const skcms_ICCProfile* const srcProfile = this->getEncodedInfo().profile();
721 skcms_ICCProfile dstProfileStorage;
722 const skcms_ICCProfile* dstProfile = nullptr;
723 if (auto cs = dstInfo.colorSpace()) {
724 cs->toProfile(&dstProfileStorage);
725 dstProfile = &dstProfileStorage;
726 }
727
728 for (int i = 0; i < height; ++i) {
729 buffer.fArea = dng_rect(i, 0, i + 1, width);
730
731 try {
732 image->Get(buffer, dng_image::edge_zero);
733 } catch (...) {
734 *rowsDecoded = i;
735 return kIncompleteInput;
736 }
737
738 if (!skcms_Transform(&srcRow[0], srcFormat, skcms_AlphaFormat_Unpremul, srcProfile,
739 dstRow, dstFormat, skcms_AlphaFormat_Unpremul, dstProfile,
740 dstInfo.width())) {
741 SkDebugf("failed to transform\n");
742 *rowsDecoded = i;
743 return kInternalError;
744 }
745
746 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
747 }
748 return kSuccess;
749}
750
751SkISize SkRawCodec::onGetScaledDimensions(float desiredScale) const {
752 SkASSERT(desiredScale <= 1.f);
753
754 const SkISize dim = this->dimensions();
755 SkASSERT(dim.fWidth != 0 && dim.fHeight != 0);
756
757 if (!fDngImage->isScalable()) {
758 return dim;
759 }
760
761 // Limits the minimum size to be 80 on the short edge.
762 const float shortEdge = static_cast<float>(std::min(dim.fWidth, dim.fHeight));
763 if (desiredScale < 80.f / shortEdge) {
764 desiredScale = 80.f / shortEdge;
765 }
766
767 // For Xtrans images, the integer-factor scaling does not support the half-size scaling case
768 // (stronger downscalings are fine). In this case, returns the factor "3" scaling instead.
769 if (fDngImage->isXtransImage() && desiredScale > 1.f / 3.f && desiredScale < 1.f) {
770 desiredScale = 1.f / 3.f;
771 }
772
773 // Round to integer-factors.
774 const float finalScale = std::floor(1.f/ desiredScale);
775 return SkISize::Make(static_cast<int32_t>(std::floor(dim.fWidth / finalScale)),
776 static_cast<int32_t>(std::floor(dim.fHeight / finalScale)));
777}
778
779bool SkRawCodec::onDimensionsSupported(const SkISize& dim) {
780 const SkISize fullDim = this->dimensions();
781 const float fullShortEdge = static_cast<float>(std::min(fullDim.fWidth, fullDim.fHeight));
782 const float shortEdge = static_cast<float>(std::min(dim.fWidth, dim.fHeight));
783
784 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEdge / shortEdge));
785 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge / shortEdge));
786 return sizeFloor == dim || sizeCeil == dim;
787}
788
789SkRawCodec::~SkRawCodec() {}
790
791SkRawCodec::SkRawCodec(SkDngImage* dngImage)
792 : INHERITED(SkEncodedInfo::Make(dngImage->width(), dngImage->height(),
793 SkEncodedInfo::kRGB_Color,
794 SkEncodedInfo::kOpaque_Alpha, 8),
795 skcms_PixelFormat_RGBA_8888, nullptr)
796 , fDngImage(dngImage) {}
797