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