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