blob: c1eb6ad2e2d3344f7cc3e5786dc12bba8f0b0fb4 [file] [log] [blame]
yujieqin916de9f2016-01-25 08:26:16 -08001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkStreamPriv.h"
22#include "src/core/SkTaskGroup.h"
yujieqin916de9f2016-01-25 08:26:16 -080023
ebrauerb84b5b42016-01-27 08:21:03 -080024#include "dng_area_task.h"
yujieqin916de9f2016-01-25 08:26:16 -080025#include "dng_color_space.h"
yujieqinf236ee42016-02-29 07:14:42 -080026#include "dng_errors.h"
yujieqin916de9f2016-01-25 08:26:16 -080027#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>
John Stilesfbd050b2020-08-03 13:21:46 -040038#include <memory>
yujieqin916de9f2016-01-25 08:26:16 -080039
40namespace {
41
ebrauerb84b5b42016-01-27 08:21:03 -080042// Caluclates the number of tiles of tile_size that fit into the area in vertical and horizontal
43// directions.
44dng_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
yujieqinfda27a92016-01-27 09:03:20 -080047 return dng_point(static_cast<int32>((areaSize.v + tileSize.v - 1) / tileSize.v),
48 static_cast<int32>((areaSize.h + tileSize.h - 1) / tileSize.h));
ebrauerb84b5b42016-01-27 08:21:03 -080049}
50
51int 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.
59dng_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
74std::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
95class SkDngHost : public dng_host {
96public:
yujieqinfda27a92016-01-27 09:03:20 -080097 explicit SkDngHost(dng_memory_allocator* allocater) : dng_host(allocater) {}
ebrauerb84b5b42016-01-27 08:21:03 -080098
99 void PerformAreaTask(dng_area_task& task, const dng_rect& area) override {
ebrauerb84b5b42016-01-27 08:21:03 -0800100 SkTaskGroup taskGroup;
101
102 // tileSize is typically 256x256
103 const dng_point tileSize(task.FindTileSize(area));
Leon Scroggins III37819d02018-04-20 11:04:32 -0400104 const std::vector<dng_rect> taskAreas = compute_task_areas(this->PerformAreaTaskThreads(),
105 area, tileSize);
yujieqinfda27a92016-01-27 09:03:20 -0800106 const int numTasks = static_cast<int>(taskAreas.size());
ebrauerb84b5b42016-01-27 08:21:03 -0800107
yujieqinf236ee42016-02-29 07:14:42 -0800108 SkMutex mutex;
109 SkTArray<dng_exception> exceptions;
ebrauerb84b5b42016-01-27 08:21:03 -0800110 task.Start(numTasks, tileSize, &Allocator(), Sniffer());
111 for (int taskIndex = 0; taskIndex < numTasks; ++taskIndex) {
yujieqinf236ee42016-02-29 07:14:42 -0800112 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) {
Herb Derby9b869552019-05-10 12:16:17 -0400116 SkAutoMutexExclusive lock(mutex);
yujieqinf236ee42016-02-29 07:14:42 -0800117 exceptions.push_back(exception);
118 } catch (...) {
Herb Derby9b869552019-05-10 12:16:17 -0400119 SkAutoMutexExclusive lock(mutex);
yujieqinf236ee42016-02-29 07:14:42 -0800120 exceptions.push_back(dng_exception(dng_error_unknown));
121 }
ebrauerb84b5b42016-01-27 08:21:03 -0800122 });
123 }
124
125 taskGroup.wait();
126 task.Finish(numTasks);
yujieqinf236ee42016-02-29 07:14:42 -0800127
Leon Scroggins III37819d02018-04-20 11:04:32 -0400128 // We only re-throw the first exception.
yujieqinf236ee42016-02-29 07:14:42 -0800129 if (!exceptions.empty()) {
130 Throw_dng_error(exceptions.front().ErrorCode(), nullptr, nullptr);
131 }
ebrauerb84b5b42016-01-27 08:21:03 -0800132 }
133
134 uint32 PerformAreaTaskThreads() override {
Leon Scroggins III37819d02018-04-20 11:04:32 -0400135#ifdef SK_BUILD_FOR_ANDROID
Leon Scroggins III1530f392018-05-23 16:12:32 -0400136 // 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;
Leon Scroggins III37819d02018-04-20 11:04:32 -0400142#else
ebrauerb84b5b42016-01-27 08:21:03 -0800143 return kMaxMPThreads;
Leon Scroggins III37819d02018-04-20 11:04:32 -0400144#endif
ebrauerb84b5b42016-01-27 08:21:03 -0800145 }
146
147private:
John Stiles7571f9e2020-09-02 22:42:33 -0400148 using INHERITED = dng_host;
ebrauerb84b5b42016-01-27 08:21:03 -0800149};
150
yujieqin916de9f2016-01-25 08:26:16 -0800151// T must be unsigned type.
152template <class T>
153bool 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
yujieqin9c7a8a42016-02-05 08:21:19 -0800166bool is_asset_stream(const SkStream& stream) {
167 return stream.hasLength() && stream.hasPosition();
168}
169
yujieqin916de9f2016-01-25 08:26:16 -0800170} // namespace
171
yujieqin9c7a8a42016-02-05 08:21:19 -0800172class SkRawStream {
yujieqin916de9f2016-01-25 08:26:16 -0800173public:
yujieqin9c7a8a42016-02-05 08:21:19 -0800174 virtual ~SkRawStream() {}
yujieqin916de9f2016-01-25 08:26:16 -0800175
Ben Wagner63fd7602017-10-09 15:45:33 -0400176 /*
yujieqin9c7a8a42016-02-05 08:21:19 -0800177 * 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;
yujieqin916de9f2016-01-25 08:26:16 -0800183
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 */
Mike Reedede7bac2017-07-23 15:30:02 -0400189 virtual std::unique_ptr<SkMemoryStream> transferBuffer(size_t offset, size_t size) = 0;
yujieqin9c7a8a42016-02-05 08:21:19 -0800190};
191
yujieqinc04df212016-03-09 13:49:36 -0800192class SkRawLimitedDynamicMemoryWStream : public SkDynamicMemoryWStream {
193public:
Brian Salomond3b65972017-03-22 12:05:03 -0400194 ~SkRawLimitedDynamicMemoryWStream() override {}
yujieqinc04df212016-03-09 13:49:36 -0800195
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
207private:
yujieqind6215cf2016-03-10 05:15:49 -0800208 // 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.
yujieqinc04df212016-03-09 13:49:36 -0800210 const size_t kMaxStreamSize = 100 * 1024 * 1024; // 100MB
211
John Stiles7571f9e2020-09-02 22:42:33 -0400212 using INHERITED = SkDynamicMemoryWStream;
yujieqinc04df212016-03-09 13:49:36 -0800213};
214
215// Note: the maximum buffer size is 100MB (limited by SkRawLimitedDynamicMemoryWStream).
yujieqin9c7a8a42016-02-05 08:21:19 -0800216class SkRawBufferedStream : public SkRawStream {
217public:
Mike Reedede7bac2017-07-23 15:30:02 -0400218 explicit SkRawBufferedStream(std::unique_ptr<SkStream> stream)
219 : fStream(std::move(stream))
yujieqin9c7a8a42016-02-05 08:21:19 -0800220 , fWholeStreamRead(false)
221 {
222 // Only use SkRawBufferedStream when the stream is not an asset stream.
Mike Reedede7bac2017-07-23 15:30:02 -0400223 SkASSERT(!is_asset_stream(*fStream));
yujieqin9c7a8a42016-02-05 08:21:19 -0800224 }
225
226 ~SkRawBufferedStream() override {}
227
228 uint64 getLength() override {
229 if (!this->bufferMoreData(kReadToEnd)) { // read whole stream
230 ThrowReadFile();
231 }
232 return fStreamBuffer.bytesWritten();
233 }
234
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
Mike Reedede7bac2017-07-23 15:30:02 -0400248 std::unique_ptr<SkMemoryStream> transferBuffer(size_t offset, size_t size) override {
bungeman38d909e2016-08-02 14:40:46 -0700249 sk_sp<SkData> data(SkData::MakeUninitialized(size));
yujieqin916de9f2016-01-25 08:26:16 -0800250 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) {
bungeman38d909e2016-08-02 14:40:46 -0700258 data = SkData::MakeSubset(data.get(), 0, bytesRead);
yujieqin916de9f2016-01-25 08:26:16 -0800259 }
260 } else {
Brian Osman788b9162020-02-07 10:36:46 -0500261 const size_t alreadyBuffered = std::min(fStreamBuffer.bytesWritten() - offset, size);
yujieqin916de9f2016-01-25 08:26:16 -0800262 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 }
bungeman38d909e2016-08-02 14:40:46 -0700276 data = SkData::MakeSubset(data.get(), 0, newSize);
yujieqin916de9f2016-01-25 08:26:16 -0800277 }
278 }
279 }
Mike Reed847068c2017-07-26 11:35:53 -0400280 return SkMemoryStream::Make(data);
yujieqin916de9f2016-01-25 08:26:16 -0800281 }
282
yujieqin916de9f2016-01-25 08:26:16 -0800283private:
284 // Note: if the newSize == kReadToEnd (0), this function will read to the end of stream.
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
yujieqin22000d12016-02-02 08:09:07 -0800302 // 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();
Brian Osman788b9162020-02-07 10:36:46 -0500305 const size_t sizeToRead = std::max(kMinSizeToRead, sizeRequested);
yujieqin22000d12016-02-02 08:09:07 -0800306 SkAutoSTMalloc<kMinSizeToRead, uint8> tempBuffer(sizeToRead);
yujieqin916de9f2016-01-25 08:26:16 -0800307 const size_t bytesRead = fStream->read(tempBuffer.get(), sizeToRead);
yujieqin22000d12016-02-02 08:09:07 -0800308 if (bytesRead < sizeRequested) {
yujieqin916de9f2016-01-25 08:26:16 -0800309 return false;
310 }
311 return fStreamBuffer.write(tempBuffer.get(), bytesRead);
312 }
313
Ben Wagner145dbcd2016-11-03 14:40:50 -0400314 std::unique_ptr<SkStream> fStream;
yujieqin916de9f2016-01-25 08:26:16 -0800315 bool fWholeStreamRead;
316
yujieqinc04df212016-03-09 13:49:36 -0800317 // Use a size-limited stream to avoid holding too huge buffer.
318 SkRawLimitedDynamicMemoryWStream fStreamBuffer;
yujieqin916de9f2016-01-25 08:26:16 -0800319
320 const size_t kReadToEnd = 0;
321};
322
yujieqin9c7a8a42016-02-05 08:21:19 -0800323class SkRawAssetStream : public SkRawStream {
yujieqin916de9f2016-01-25 08:26:16 -0800324public:
Mike Reedede7bac2017-07-23 15:30:02 -0400325 explicit SkRawAssetStream(std::unique_ptr<SkStream> stream)
326 : fStream(std::move(stream))
yujieqin9c7a8a42016-02-05 08:21:19 -0800327 {
328 // Only use SkRawAssetStream when the stream is an asset stream.
Mike Reedede7bac2017-07-23 15:30:02 -0400329 SkASSERT(is_asset_stream(*fStream));
yujieqin9c7a8a42016-02-05 08:21:19 -0800330 }
yujieqin916de9f2016-01-25 08:26:16 -0800331
yujieqin9c7a8a42016-02-05 08:21:19 -0800332 ~SkRawAssetStream() override {}
yujieqin916de9f2016-01-25 08:26:16 -0800333
yujieqin9c7a8a42016-02-05 08:21:19 -0800334 uint64 getLength() override {
335 return fStream->getLength();
336 }
337
338
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
Mike Reedede7bac2017-07-23 15:30:02 -0400352 std::unique_ptr<SkMemoryStream> transferBuffer(size_t offset, size_t size) override {
yujieqin9c7a8a42016-02-05 08:21:19 -0800353 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.
Brian Osman788b9162020-02-07 10:36:46 -0500364 const size_t bytesToRead = std::min(sum, fStream->getLength()) - offset;
yujieqin9c7a8a42016-02-05 08:21:19 -0800365 if (bytesToRead == 0) {
366 return nullptr;
367 }
368
369 if (fStream->getMemoryBase()) { // directly copy if getMemoryBase() is available.
bungeman38d909e2016-08-02 14:40:46 -0700370 sk_sp<SkData> data(SkData::MakeWithCopy(
yujieqin9c7a8a42016-02-05 08:21:19 -0800371 static_cast<const uint8_t*>(fStream->getMemoryBase()) + offset, bytesToRead));
mtklein852f15d2016-03-17 10:51:27 -0700372 fStream.reset();
Mike Reed847068c2017-07-26 11:35:53 -0400373 return SkMemoryStream::Make(data);
yujieqin9c7a8a42016-02-05 08:21:19 -0800374 } else {
bungeman38d909e2016-08-02 14:40:46 -0700375 sk_sp<SkData> data(SkData::MakeUninitialized(bytesToRead));
yujieqin9c7a8a42016-02-05 08:21:19 -0800376 if (!fStream->seek(offset)) {
377 return nullptr;
378 }
379 const size_t bytesRead = fStream->read(data->writable_data(), bytesToRead);
380 if (bytesRead < bytesToRead) {
bungeman38d909e2016-08-02 14:40:46 -0700381 data = SkData::MakeSubset(data.get(), 0, bytesRead);
yujieqin9c7a8a42016-02-05 08:21:19 -0800382 }
Mike Reed847068c2017-07-26 11:35:53 -0400383 return SkMemoryStream::Make(data);
yujieqin9c7a8a42016-02-05 08:21:19 -0800384 }
385 }
386private:
Ben Wagner145dbcd2016-11-03 14:40:50 -0400387 std::unique_ptr<SkStream> fStream;
yujieqin9c7a8a42016-02-05 08:21:19 -0800388};
389
390class SkPiexStream : public ::piex::StreamInterface {
391public:
392 // Will NOT take the ownership of the stream.
393 explicit SkPiexStream(SkRawStream* stream) : fStream(stream) {}
394
395 ~SkPiexStream() override {}
396
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;
yujieqin916de9f2016-01-25 08:26:16 -0800401 }
402
403private:
yujieqin9c7a8a42016-02-05 08:21:19 -0800404 SkRawStream* fStream;
405};
406
407class SkDngStream : public dng_stream {
408public:
409 // Will NOT take the ownership of the stream.
410 SkDngStream(SkRawStream* stream) : fStream(stream) {}
411
412 ~SkDngStream() override {}
413
414 uint64 DoGetLength() override { return fStream->getLength(); }
415
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
424private:
425 SkRawStream* fStream;
yujieqin916de9f2016-01-25 08:26:16 -0800426};
427
428class SkDngImage {
429public:
ebrauer46d2aa82016-02-17 08:04:00 -0800430 /*
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 */
yujieqin916de9f2016-01-25 08:26:16 -0800436 static SkDngImage* NewFromStream(SkRawStream* stream) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400437 std::unique_ptr<SkDngImage> dngImage(new SkDngImage(stream));
Kevin Lubick493f89e2020-09-14 08:37:35 -0400438#if defined(SK_BUILD_FOR_LIBFUZZER)
Kevin Lubick2416f962018-02-12 08:26:39 -0500439 // 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#endif
Leon Scroggins III588fb042017-07-14 16:32:31 -0400444 if (!dngImage->initFromPiex() && !dngImage->readDng()) {
yujieqind6215cf2016-03-10 05:15:49 -0800445 return nullptr;
446 }
447
yujieqin916de9f2016-01-25 08:26:16 -0800448 return dngImage.release();
449 }
450
451 /*
452 * Renders the DNG image to the size. The DNG SDK only allows scaling close to integer factors
453 * down to 80 pixels on the short edge. The rendered image will be close to the specified size,
454 * but there is no guarantee that any of the edges will match the requested size. E.g.
455 * 100% size: 4000 x 3000
456 * requested size: 1600 x 1200
457 * returned size could be: 2000 x 1500
458 */
459 dng_image* render(int width, int height) {
460 if (!fHost || !fInfo || !fNegative || !fDngStream) {
461 if (!this->readDng()) {
462 return nullptr;
463 }
464 }
465
yujieqin916de9f2016-01-25 08:26:16 -0800466 // DNG SDK preserves the aspect ratio, so it only needs to know the longer dimension.
Brian Osman788b9162020-02-07 10:36:46 -0500467 const int preferredSize = std::max(width, height);
yujieqin916de9f2016-01-25 08:26:16 -0800468 try {
yujieqinf236ee42016-02-29 07:14:42 -0800469 // render() takes ownership of fHost, fInfo, fNegative and fDngStream when available.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400470 std::unique_ptr<dng_host> host(fHost.release());
471 std::unique_ptr<dng_info> info(fInfo.release());
472 std::unique_ptr<dng_negative> negative(fNegative.release());
473 std::unique_ptr<dng_stream> dngStream(fDngStream.release());
yujieqinf236ee42016-02-29 07:14:42 -0800474
yujieqin916de9f2016-01-25 08:26:16 -0800475 host->SetPreferredSize(preferredSize);
476 host->ValidateSizes();
477
478 negative->ReadStage1Image(*host, *dngStream, *info);
479
480 if (info->fMaskIndex != -1) {
481 negative->ReadTransparencyMask(*host, *dngStream, *info);
482 }
483
484 negative->ValidateRawImageDigest(*host);
485 if (negative->IsDamaged()) {
486 return nullptr;
487 }
488
489 const int32 kMosaicPlane = -1;
490 negative->BuildStage2Image(*host);
491 negative->BuildStage3Image(*host, kMosaicPlane);
492
493 dng_render render(*host, *negative);
494 render.SetFinalSpace(dng_space_sRGB::Get());
495 render.SetFinalPixelType(ttByte);
496
497 dng_point stage3_size = negative->Stage3Image()->Size();
Brian Osman788b9162020-02-07 10:36:46 -0500498 render.SetMaximumSize(std::max(stage3_size.h, stage3_size.v));
yujieqin916de9f2016-01-25 08:26:16 -0800499
500 return render.Render();
501 } catch (...) {
502 return nullptr;
503 }
504 }
505
msarettc30c4182016-04-20 11:53:35 -0700506 int width() const {
507 return fWidth;
508 }
509
510 int height() const {
511 return fHeight;
yujieqin916de9f2016-01-25 08:26:16 -0800512 }
513
514 bool isScalable() const {
515 return fIsScalable;
516 }
517
518 bool isXtransImage() const {
519 return fIsXtransImage;
520 }
521
yujieqind6215cf2016-03-10 05:15:49 -0800522 // Quick check if the image contains a valid TIFF header as requested by DNG format.
Leon Scroggins III588fb042017-07-14 16:32:31 -0400523 // Does not affect ownership of stream.
524 static bool IsTiffHeaderValid(SkRawStream* stream) {
yujieqind6215cf2016-03-10 05:15:49 -0800525 const size_t kHeaderSize = 4;
Leon Scroggins III588fb042017-07-14 16:32:31 -0400526 unsigned char header[kHeaderSize];
527 if (!stream->read(header, 0 /* offset */, kHeaderSize)) {
yujieqind6215cf2016-03-10 05:15:49 -0800528 return false;
529 }
530
531 // Check if the header is valid (endian info and magic number "42").
msarett0e6274f2016-03-21 08:04:40 -0700532 bool littleEndian;
533 if (!is_valid_endian_marker(header, &littleEndian)) {
534 return false;
535 }
536
537 return 0x2A == get_endian_short(header + 2, littleEndian);
yujieqind6215cf2016-03-10 05:15:49 -0800538 }
539
Leon Scroggins III588fb042017-07-14 16:32:31 -0400540private:
Leon Scroggins IIId87fbee2016-12-02 16:47:53 -0500541 bool init(int width, int height, const dng_point& cfaPatternSize) {
msarettc30c4182016-04-20 11:53:35 -0700542 fWidth = width;
543 fHeight = height;
ebrauer46d2aa82016-02-17 08:04:00 -0800544
545 // The DNG SDK scales only during demosaicing, so scaling is only possible when
546 // a mosaic info is available.
547 fIsScalable = cfaPatternSize.v != 0 && cfaPatternSize.h != 0;
548 fIsXtransImage = fIsScalable ? (cfaPatternSize.v == 6 && cfaPatternSize.h == 6) : false;
Leon Scroggins IIId87fbee2016-12-02 16:47:53 -0500549
550 return width > 0 && height > 0;
ebrauer46d2aa82016-02-17 08:04:00 -0800551 }
552
553 bool initFromPiex() {
554 // Does not take the ownership of rawStream.
555 SkPiexStream piexStream(fStream.get());
556 ::piex::PreviewImageData imageData;
557 if (::piex::IsRaw(&piexStream)
558 && ::piex::GetPreviewImageData(&piexStream, &imageData) == ::piex::Error::kOk)
559 {
560 dng_point cfaPatternSize(imageData.cfa_pattern_dim[1], imageData.cfa_pattern_dim[0]);
Leon Scroggins IIId87fbee2016-12-02 16:47:53 -0500561 return this->init(static_cast<int>(imageData.full_width),
562 static_cast<int>(imageData.full_height), cfaPatternSize);
ebrauer46d2aa82016-02-17 08:04:00 -0800563 }
564 return false;
565 }
566
yujieqin916de9f2016-01-25 08:26:16 -0800567 bool readDng() {
yujieqin916de9f2016-01-25 08:26:16 -0800568 try {
yujieqinf236ee42016-02-29 07:14:42 -0800569 // Due to the limit of DNG SDK, we need to reset host and info.
John Stilesfbd050b2020-08-03 13:21:46 -0400570 fHost = std::make_unique<SkDngHost>(&fAllocator);
571 fInfo = std::make_unique<dng_info>();
572 fDngStream = std::make_unique<SkDngStream>(fStream.get());
yujieqinf236ee42016-02-29 07:14:42 -0800573
yujieqin916de9f2016-01-25 08:26:16 -0800574 fHost->ValidateSizes();
575 fInfo->Parse(*fHost, *fDngStream);
576 fInfo->PostParse(*fHost);
577 if (!fInfo->IsValidDNG()) {
578 return false;
579 }
580
581 fNegative.reset(fHost->Make_dng_negative());
582 fNegative->Parse(*fHost, *fDngStream, *fInfo);
583 fNegative->PostParse(*fHost, *fDngStream, *fInfo);
584 fNegative->SynchronizeMetadata();
585
ebrauer46d2aa82016-02-17 08:04:00 -0800586 dng_point cfaPatternSize(0, 0);
587 if (fNegative->GetMosaicInfo() != nullptr) {
588 cfaPatternSize = fNegative->GetMosaicInfo()->fCFAPatternSize;
589 }
Leon Scroggins IIId87fbee2016-12-02 16:47:53 -0500590 return this->init(static_cast<int>(fNegative->DefaultCropSizeH().As_real64()),
591 static_cast<int>(fNegative->DefaultCropSizeV().As_real64()),
592 cfaPatternSize);
yujieqin916de9f2016-01-25 08:26:16 -0800593 } catch (...) {
yujieqin916de9f2016-01-25 08:26:16 -0800594 return false;
595 }
596 }
597
598 SkDngImage(SkRawStream* stream)
msarettac6c7502016-04-25 09:30:24 -0700599 : fStream(stream)
msarettac6c7502016-04-25 09:30:24 -0700600 {}
yujieqin916de9f2016-01-25 08:26:16 -0800601
Leon Scroggins IIIe7fd7ff2018-04-18 10:17:45 -0400602 dng_memory_allocator fAllocator;
Ben Wagner145dbcd2016-11-03 14:40:50 -0400603 std::unique_ptr<SkRawStream> fStream;
604 std::unique_ptr<dng_host> fHost;
605 std::unique_ptr<dng_info> fInfo;
606 std::unique_ptr<dng_negative> fNegative;
607 std::unique_ptr<dng_stream> fDngStream;
yujieqin916de9f2016-01-25 08:26:16 -0800608
msarettc30c4182016-04-20 11:53:35 -0700609 int fWidth;
610 int fHeight;
yujieqin916de9f2016-01-25 08:26:16 -0800611 bool fIsScalable;
612 bool fIsXtransImage;
613};
614
615/*
616 * Tries to handle the image with PIEX. If PIEX returns kOk and finds the preview image, create a
617 * SkJpegCodec. If PIEX returns kFail, then the file is invalid, return nullptr. In other cases,
618 * fallback to create SkRawCodec for DNG images.
619 */
Mike Reedede7bac2017-07-23 15:30:02 -0400620std::unique_ptr<SkCodec> SkRawCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
621 Result* result) {
Ben Wagner145dbcd2016-11-03 14:40:50 -0400622 std::unique_ptr<SkRawStream> rawStream;
yujieqin9c7a8a42016-02-05 08:21:19 -0800623 if (is_asset_stream(*stream)) {
John Stilesfbd050b2020-08-03 13:21:46 -0400624 rawStream = std::make_unique<SkRawAssetStream>(std::move(stream));
yujieqin9c7a8a42016-02-05 08:21:19 -0800625 } else {
John Stilesfbd050b2020-08-03 13:21:46 -0400626 rawStream = std::make_unique<SkRawBufferedStream>(std::move(stream));
yujieqin9c7a8a42016-02-05 08:21:19 -0800627 }
628
629 // Does not take the ownership of rawStream.
630 SkPiexStream piexStream(rawStream.get());
yujieqin916de9f2016-01-25 08:26:16 -0800631 ::piex::PreviewImageData imageData;
yujieqin9c7a8a42016-02-05 08:21:19 -0800632 if (::piex::IsRaw(&piexStream)) {
633 ::piex::Error error = ::piex::GetPreviewImageData(&piexStream, &imageData);
Matt Sarettc5eabe72017-02-24 14:51:08 -0500634 if (error == ::piex::Error::kFail) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400635 *result = kInvalidInput;
Matt Sarettc5eabe72017-02-24 14:51:08 -0500636 return nullptr;
637 }
638
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400639 std::unique_ptr<SkEncodedInfo::ICCProfile> profile;
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400640 if (imageData.color_space == ::piex::PreviewImageData::kAdobeRgb) {
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400641 skcms_ICCProfile skcmsProfile;
642 skcms_Init(&skcmsProfile);
Brian Osman82ebe042019-01-04 17:03:00 -0500643 skcms_SetTransferFunction(&skcmsProfile, &SkNamedTransferFn::k2Dot2);
644 skcms_SetXYZD50(&skcmsProfile, &SkNamedGamut::kAdobeRGB);
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400645 profile = SkEncodedInfo::ICCProfile::Make(skcmsProfile);
Matt Sarettc5eabe72017-02-24 14:51:08 -0500646 }
yujieqin916de9f2016-01-25 08:26:16 -0800647
yujieqine34635d2016-04-14 07:04:00 -0700648 // Theoretically PIEX can return JPEG compressed image or uncompressed RGB image. We only
649 // handle the JPEG compressed preview image here.
650 if (error == ::piex::Error::kOk && imageData.preview.length > 0 &&
651 imageData.preview.format == ::piex::Image::kJpegCompressed)
652 {
yujieqin916de9f2016-01-25 08:26:16 -0800653 // transferBuffer() is destructive to the rawStream. Abandon the rawStream after this
654 // function call.
655 // FIXME: one may avoid the copy of memoryStream and use the buffered rawStream.
Mike Reedede7bac2017-07-23 15:30:02 -0400656 auto memoryStream = rawStream->transferBuffer(imageData.preview.offset,
657 imageData.preview.length);
Leon Scroggins III588fb042017-07-14 16:32:31 -0400658 if (!memoryStream) {
659 *result = kInvalidInput;
660 return nullptr;
661 }
Mike Reedede7bac2017-07-23 15:30:02 -0400662 return SkJpegCodec::MakeFromStream(std::move(memoryStream), result,
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400663 std::move(profile));
yujieqin916de9f2016-01-25 08:26:16 -0800664 }
665 }
666
Leon Scroggins III588fb042017-07-14 16:32:31 -0400667 if (!SkDngImage::IsTiffHeaderValid(rawStream.get())) {
668 *result = kUnimplemented;
669 return nullptr;
670 }
671
yujieqin9c7a8a42016-02-05 08:21:19 -0800672 // Takes the ownership of the rawStream.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400673 std::unique_ptr<SkDngImage> dngImage(SkDngImage::NewFromStream(rawStream.release()));
yujieqin916de9f2016-01-25 08:26:16 -0800674 if (!dngImage) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400675 *result = kInvalidInput;
yujieqin916de9f2016-01-25 08:26:16 -0800676 return nullptr;
677 }
678
Leon Scroggins III588fb042017-07-14 16:32:31 -0400679 *result = kSuccess;
Mike Reedede7bac2017-07-23 15:30:02 -0400680 return std::unique_ptr<SkCodec>(new SkRawCodec(dngImage.release()));
yujieqin916de9f2016-01-25 08:26:16 -0800681}
682
Matt Saretta225e9b2016-11-07 10:26:17 -0500683SkCodec::Result SkRawCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst,
yujieqin916de9f2016-01-25 08:26:16 -0800684 size_t dstRowBytes, const Options& options,
yujieqin916de9f2016-01-25 08:26:16 -0800685 int* rowsDecoded) {
Matt Saretta225e9b2016-11-07 10:26:17 -0500686 const int width = dstInfo.width();
687 const int height = dstInfo.height();
Ben Wagner145dbcd2016-11-03 14:40:50 -0400688 std::unique_ptr<dng_image> image(fDngImage->render(width, height));
yujieqin916de9f2016-01-25 08:26:16 -0800689 if (!image) {
690 return kInvalidInput;
691 }
692
693 // Because the DNG SDK can not guarantee to render to requested size, we allow a small
694 // difference. Only the overlapping region will be converted.
695 const float maxDiffRatio = 1.03f;
696 const dng_point& imageSize = image->Size();
Matt Sarette18c97b2016-11-28 18:46:37 -0500697 if (imageSize.h / (float) width > maxDiffRatio || imageSize.h < width ||
698 imageSize.v / (float) height > maxDiffRatio || imageSize.v < height) {
yujieqin916de9f2016-01-25 08:26:16 -0800699 return SkCodec::kInvalidScale;
700 }
701
702 void* dstRow = dst;
yujieqin24716be2016-01-27 07:59:00 -0800703 SkAutoTMalloc<uint8_t> srcRow(width * 3);
yujieqin916de9f2016-01-25 08:26:16 -0800704
705 dng_pixel_buffer buffer;
706 buffer.fData = &srcRow[0];
707 buffer.fPlane = 0;
708 buffer.fPlanes = 3;
709 buffer.fColStep = buffer.fPlanes;
710 buffer.fPlaneStep = 1;
711 buffer.fPixelType = ttByte;
712 buffer.fPixelSize = sizeof(uint8_t);
scroggoe6459652016-01-30 10:06:11 -0800713 buffer.fRowStep = width * 3;
yujieqin916de9f2016-01-25 08:26:16 -0800714
Leon Scroggins III96765962018-12-07 14:04:47 -0500715 constexpr auto srcFormat = skcms_PixelFormat_RGB_888;
716 skcms_PixelFormat dstFormat;
Leon Scroggins III179559f2018-12-10 12:30:12 -0500717 if (!sk_select_xform_format(dstInfo.colorType(), false, &dstFormat)) {
718 return kInvalidConversion;
Leon Scroggins III96765962018-12-07 14:04:47 -0500719 }
720
721 const skcms_ICCProfile* const srcProfile = this->getEncodedInfo().profile();
722 skcms_ICCProfile dstProfileStorage;
723 const skcms_ICCProfile* dstProfile = nullptr;
724 if (auto cs = dstInfo.colorSpace()) {
725 cs->toProfile(&dstProfileStorage);
726 dstProfile = &dstProfileStorage;
727 }
728
yujieqin916de9f2016-01-25 08:26:16 -0800729 for (int i = 0; i < height; ++i) {
730 buffer.fArea = dng_rect(i, 0, i + 1, width);
731
732 try {
733 image->Get(buffer, dng_image::edge_zero);
734 } catch (...) {
735 *rowsDecoded = i;
nagarajan.nf398dd52017-09-19 17:46:52 +0530736 return kIncompleteInput;
yujieqin916de9f2016-01-25 08:26:16 -0800737 }
738
Leon Scroggins III96765962018-12-07 14:04:47 -0500739 if (!skcms_Transform(&srcRow[0], srcFormat, skcms_AlphaFormat_Unpremul, srcProfile,
740 dstRow, dstFormat, skcms_AlphaFormat_Unpremul, dstProfile,
741 dstInfo.width())) {
742 SkDebugf("failed to transform\n");
743 *rowsDecoded = i;
744 return kInternalError;
Matt Saretta225e9b2016-11-07 10:26:17 -0500745 }
Leon Scroggins III96765962018-12-07 14:04:47 -0500746
nagarajan.nf398dd52017-09-19 17:46:52 +0530747 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
yujieqin916de9f2016-01-25 08:26:16 -0800748 }
749 return kSuccess;
750}
751
752SkISize SkRawCodec::onGetScaledDimensions(float desiredScale) const {
753 SkASSERT(desiredScale <= 1.f);
yujieqind0e08852016-03-03 07:38:27 -0800754
Leon Scroggins III712476e2018-10-03 15:47:00 -0400755 const SkISize dim = this->dimensions();
yujieqind0e08852016-03-03 07:38:27 -0800756 SkASSERT(dim.fWidth != 0 && dim.fHeight != 0);
757
yujieqin916de9f2016-01-25 08:26:16 -0800758 if (!fDngImage->isScalable()) {
759 return dim;
760 }
761
762 // Limits the minimum size to be 80 on the short edge.
Brian Osman788b9162020-02-07 10:36:46 -0500763 const float shortEdge = static_cast<float>(std::min(dim.fWidth, dim.fHeight));
yujieqin916de9f2016-01-25 08:26:16 -0800764 if (desiredScale < 80.f / shortEdge) {
765 desiredScale = 80.f / shortEdge;
766 }
767
768 // For Xtrans images, the integer-factor scaling does not support the half-size scaling case
769 // (stronger downscalings are fine). In this case, returns the factor "3" scaling instead.
770 if (fDngImage->isXtransImage() && desiredScale > 1.f / 3.f && desiredScale < 1.f) {
771 desiredScale = 1.f / 3.f;
772 }
773
774 // Round to integer-factors.
775 const float finalScale = std::floor(1.f/ desiredScale);
yujieqin076d83d2016-01-27 08:25:53 -0800776 return SkISize::Make(static_cast<int32_t>(std::floor(dim.fWidth / finalScale)),
777 static_cast<int32_t>(std::floor(dim.fHeight / finalScale)));
yujieqin916de9f2016-01-25 08:26:16 -0800778}
779
780bool SkRawCodec::onDimensionsSupported(const SkISize& dim) {
Leon Scroggins III712476e2018-10-03 15:47:00 -0400781 const SkISize fullDim = this->dimensions();
Brian Osman788b9162020-02-07 10:36:46 -0500782 const float fullShortEdge = static_cast<float>(std::min(fullDim.fWidth, fullDim.fHeight));
783 const float shortEdge = static_cast<float>(std::min(dim.fWidth, dim.fHeight));
yujieqin916de9f2016-01-25 08:26:16 -0800784
785 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEdge / shortEdge));
786 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge / shortEdge));
787 return sizeFloor == dim || sizeCeil == dim;
788}
789
790SkRawCodec::~SkRawCodec() {}
791
792SkRawCodec::SkRawCodec(SkDngImage* dngImage)
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400793 : INHERITED(SkEncodedInfo::Make(dngImage->width(), dngImage->height(),
794 SkEncodedInfo::kRGB_Color,
795 SkEncodedInfo::kOpaque_Alpha, 8),
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400796 skcms_PixelFormat_RGBA_8888, nullptr)
yujieqin916de9f2016-01-25 08:26:16 -0800797 , fDngImage(dngImage) {}