yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 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 "SkCodec.h" |
| 9 | #include "SkCodecPriv.h" |
| 10 | #include "SkColorPriv.h" |
| 11 | #include "SkData.h" |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 12 | #include "SkJpegCodec.h" |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 13 | #include "SkRawCodec.h" |
| 14 | #include "SkRefCnt.h" |
| 15 | #include "SkStream.h" |
| 16 | #include "SkStreamPriv.h" |
| 17 | #include "SkSwizzler.h" |
ebrauer | b84b5b4 | 2016-01-27 08:21:03 -0800 | [diff] [blame] | 18 | #include "SkTaskGroup.h" |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 19 | #include "SkTemplates.h" |
| 20 | #include "SkTypes.h" |
| 21 | |
ebrauer | b84b5b4 | 2016-01-27 08:21:03 -0800 | [diff] [blame] | 22 | #include "dng_area_task.h" |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 23 | #include "dng_color_space.h" |
| 24 | #include "dng_exceptions.h" |
| 25 | #include "dng_host.h" |
| 26 | #include "dng_info.h" |
| 27 | #include "dng_memory.h" |
| 28 | #include "dng_render.h" |
| 29 | #include "dng_stream.h" |
| 30 | |
| 31 | #include "src/piex.h" |
| 32 | |
| 33 | #include <cmath> // for std::round,floor,ceil |
| 34 | #include <limits> |
| 35 | |
| 36 | namespace { |
| 37 | |
ebrauer | b84b5b4 | 2016-01-27 08:21:03 -0800 | [diff] [blame] | 38 | // Caluclates the number of tiles of tile_size that fit into the area in vertical and horizontal |
| 39 | // directions. |
| 40 | dng_point num_tiles_in_area(const dng_point &areaSize, |
| 41 | const dng_point_real64 &tileSize) { |
| 42 | // FIXME: Add a ceil_div() helper in SkCodecPriv.h |
yujieqin | fda27a9 | 2016-01-27 09:03:20 -0800 | [diff] [blame] | 43 | return dng_point(static_cast<int32>((areaSize.v + tileSize.v - 1) / tileSize.v), |
| 44 | static_cast<int32>((areaSize.h + tileSize.h - 1) / tileSize.h)); |
ebrauer | b84b5b4 | 2016-01-27 08:21:03 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | int num_tasks_required(const dng_point& tilesInTask, |
| 48 | const dng_point& tilesInArea) { |
| 49 | return ((tilesInArea.v + tilesInTask.v - 1) / tilesInTask.v) * |
| 50 | ((tilesInArea.h + tilesInTask.h - 1) / tilesInTask.h); |
| 51 | } |
| 52 | |
| 53 | // Calculate the number of tiles to process per task, taking into account the maximum number of |
| 54 | // tasks. It prefers to increase horizontally for better locality of reference. |
| 55 | dng_point num_tiles_per_task(const int maxTasks, |
| 56 | const dng_point &tilesInArea) { |
| 57 | dng_point tilesInTask = {1, 1}; |
| 58 | while (num_tasks_required(tilesInTask, tilesInArea) > maxTasks) { |
| 59 | if (tilesInTask.h < tilesInArea.h) { |
| 60 | ++tilesInTask.h; |
| 61 | } else if (tilesInTask.v < tilesInArea.v) { |
| 62 | ++tilesInTask.v; |
| 63 | } else { |
| 64 | ThrowProgramError("num_tiles_per_task calculation is wrong."); |
| 65 | } |
| 66 | } |
| 67 | return tilesInTask; |
| 68 | } |
| 69 | |
| 70 | std::vector<dng_rect> compute_task_areas(const int maxTasks, const dng_rect& area, |
| 71 | const dng_point& tileSize) { |
| 72 | std::vector<dng_rect> taskAreas; |
| 73 | const dng_point tilesInArea = num_tiles_in_area(area.Size(), tileSize); |
| 74 | const dng_point tilesPerTask = num_tiles_per_task(maxTasks, tilesInArea); |
| 75 | const dng_point taskAreaSize = {tilesPerTask.v * tileSize.v, |
| 76 | tilesPerTask.h * tileSize.h}; |
| 77 | for (int v = 0; v < tilesInArea.v; v += tilesPerTask.v) { |
| 78 | for (int h = 0; h < tilesInArea.h; h += tilesPerTask.h) { |
| 79 | dng_rect taskArea; |
| 80 | taskArea.t = area.t + v * tileSize.v; |
| 81 | taskArea.l = area.l + h * tileSize.h; |
| 82 | taskArea.b = Min_int32(taskArea.t + taskAreaSize.v, area.b); |
| 83 | taskArea.r = Min_int32(taskArea.l + taskAreaSize.h, area.r); |
| 84 | |
| 85 | taskAreas.push_back(taskArea); |
| 86 | } |
| 87 | } |
| 88 | return taskAreas; |
| 89 | } |
| 90 | |
| 91 | class SkDngHost : public dng_host { |
| 92 | public: |
yujieqin | fda27a9 | 2016-01-27 09:03:20 -0800 | [diff] [blame] | 93 | explicit SkDngHost(dng_memory_allocator* allocater) : dng_host(allocater) {} |
ebrauer | b84b5b4 | 2016-01-27 08:21:03 -0800 | [diff] [blame] | 94 | |
| 95 | void PerformAreaTask(dng_area_task& task, const dng_rect& area) override { |
| 96 | // The area task gets split up into max_tasks sub-tasks. The max_tasks is defined by the |
| 97 | // dng-sdks default implementation of dng_area_task::MaxThreads() which returns 8 or 32 |
| 98 | // sub-tasks depending on the architecture. |
| 99 | const int maxTasks = static_cast<int>(task.MaxThreads()); |
| 100 | |
| 101 | SkTaskGroup taskGroup; |
| 102 | |
| 103 | // tileSize is typically 256x256 |
| 104 | const dng_point tileSize(task.FindTileSize(area)); |
| 105 | const std::vector<dng_rect> taskAreas = compute_task_areas(maxTasks, area, tileSize); |
yujieqin | fda27a9 | 2016-01-27 09:03:20 -0800 | [diff] [blame] | 106 | const int numTasks = static_cast<int>(taskAreas.size()); |
ebrauer | b84b5b4 | 2016-01-27 08:21:03 -0800 | [diff] [blame] | 107 | |
| 108 | task.Start(numTasks, tileSize, &Allocator(), Sniffer()); |
| 109 | for (int taskIndex = 0; taskIndex < numTasks; ++taskIndex) { |
mtklein | cb3d4fc | 2016-02-26 16:57:33 -0800 | [diff] [blame] | 110 | taskGroup.add([&task, this, taskIndex, taskAreas, tileSize] { |
| 111 | task.ProcessOnThread(taskIndex, taskAreas[taskIndex], tileSize, this->Sniffer()); |
ebrauer | b84b5b4 | 2016-01-27 08:21:03 -0800 | [diff] [blame] | 112 | }); |
| 113 | } |
| 114 | |
| 115 | taskGroup.wait(); |
| 116 | task.Finish(numTasks); |
| 117 | } |
| 118 | |
| 119 | uint32 PerformAreaTaskThreads() override { |
| 120 | // FIXME: Need to get the real amount of available threads used in the SkTaskGroup. |
| 121 | return kMaxMPThreads; |
| 122 | } |
| 123 | |
| 124 | private: |
| 125 | typedef dng_host INHERITED; |
| 126 | }; |
| 127 | |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 128 | // T must be unsigned type. |
| 129 | template <class T> |
| 130 | bool safe_add_to_size_t(T arg1, T arg2, size_t* result) { |
| 131 | SkASSERT(arg1 >= 0); |
| 132 | SkASSERT(arg2 >= 0); |
| 133 | if (arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) { |
| 134 | T sum = arg1 + arg2; |
| 135 | if (sum <= std::numeric_limits<size_t>::max()) { |
| 136 | *result = static_cast<size_t>(sum); |
| 137 | return true; |
| 138 | } |
| 139 | } |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | class SkDngMemoryAllocator : public dng_memory_allocator { |
| 144 | public: |
| 145 | ~SkDngMemoryAllocator() override {} |
| 146 | |
| 147 | dng_memory_block* Allocate(uint32 size) override { |
| 148 | // To avoid arbitary allocation requests which might lead to out-of-memory, limit the |
| 149 | // amount of memory that can be allocated at once. The memory limit is based on experiments |
| 150 | // and supposed to be sufficient for all valid DNG images. |
| 151 | if (size > 300 * 1024 * 1024) { // 300 MB |
| 152 | ThrowMemoryFull(); |
| 153 | } |
| 154 | return dng_memory_allocator::Allocate(size); |
| 155 | } |
| 156 | }; |
| 157 | |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 158 | bool is_asset_stream(const SkStream& stream) { |
| 159 | return stream.hasLength() && stream.hasPosition(); |
| 160 | } |
| 161 | |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 162 | } // namespace |
| 163 | |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 164 | class SkRawStream { |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 165 | public: |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 166 | virtual ~SkRawStream() {} |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 167 | |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 168 | /* |
| 169 | * Gets the length of the stream. Depending on the type of stream, this may require reading to |
| 170 | * the end of the stream. |
| 171 | */ |
| 172 | virtual uint64 getLength() = 0; |
| 173 | |
| 174 | virtual bool read(void* data, size_t offset, size_t length) = 0; |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 175 | |
| 176 | /* |
| 177 | * Creates an SkMemoryStream from the offset with size. |
| 178 | * Note: for performance reason, this function is destructive to the SkRawStream. One should |
| 179 | * abandon current object after the function call. |
| 180 | */ |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 181 | virtual SkMemoryStream* transferBuffer(size_t offset, size_t size) = 0; |
| 182 | }; |
| 183 | |
| 184 | class SkRawBufferedStream : public SkRawStream { |
| 185 | public: |
| 186 | // Will take the ownership of the stream. |
| 187 | explicit SkRawBufferedStream(SkStream* stream) |
| 188 | : fStream(stream) |
| 189 | , fWholeStreamRead(false) |
| 190 | { |
| 191 | // Only use SkRawBufferedStream when the stream is not an asset stream. |
| 192 | SkASSERT(!is_asset_stream(*stream)); |
| 193 | } |
| 194 | |
| 195 | ~SkRawBufferedStream() override {} |
| 196 | |
| 197 | uint64 getLength() override { |
| 198 | if (!this->bufferMoreData(kReadToEnd)) { // read whole stream |
| 199 | ThrowReadFile(); |
| 200 | } |
| 201 | return fStreamBuffer.bytesWritten(); |
| 202 | } |
| 203 | |
| 204 | bool read(void* data, size_t offset, size_t length) override { |
| 205 | if (length == 0) { |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | size_t sum; |
| 210 | if (!safe_add_to_size_t(offset, length, &sum)) { |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | return this->bufferMoreData(sum) && fStreamBuffer.read(data, offset, length); |
| 215 | } |
| 216 | |
| 217 | SkMemoryStream* transferBuffer(size_t offset, size_t size) override { |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 218 | SkAutoTUnref<SkData> data(SkData::NewUninitialized(size)); |
| 219 | if (offset > fStreamBuffer.bytesWritten()) { |
| 220 | // If the offset is not buffered, read from fStream directly and skip the buffering. |
| 221 | const size_t skipLength = offset - fStreamBuffer.bytesWritten(); |
| 222 | if (fStream->skip(skipLength) != skipLength) { |
| 223 | return nullptr; |
| 224 | } |
| 225 | const size_t bytesRead = fStream->read(data->writable_data(), size); |
| 226 | if (bytesRead < size) { |
| 227 | data.reset(SkData::NewSubset(data.get(), 0, bytesRead)); |
| 228 | } |
| 229 | } else { |
| 230 | const size_t alreadyBuffered = SkTMin(fStreamBuffer.bytesWritten() - offset, size); |
| 231 | if (alreadyBuffered > 0 && |
| 232 | !fStreamBuffer.read(data->writable_data(), offset, alreadyBuffered)) { |
| 233 | return nullptr; |
| 234 | } |
| 235 | |
| 236 | const size_t remaining = size - alreadyBuffered; |
| 237 | if (remaining) { |
| 238 | auto* dst = static_cast<uint8_t*>(data->writable_data()) + alreadyBuffered; |
| 239 | const size_t bytesRead = fStream->read(dst, remaining); |
| 240 | size_t newSize; |
| 241 | if (bytesRead < remaining) { |
| 242 | if (!safe_add_to_size_t(alreadyBuffered, bytesRead, &newSize)) { |
| 243 | return nullptr; |
| 244 | } |
| 245 | data.reset(SkData::NewSubset(data.get(), 0, newSize)); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | return new SkMemoryStream(data); |
| 250 | } |
| 251 | |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 252 | private: |
| 253 | // Note: if the newSize == kReadToEnd (0), this function will read to the end of stream. |
| 254 | bool bufferMoreData(size_t newSize) { |
| 255 | if (newSize == kReadToEnd) { |
| 256 | if (fWholeStreamRead) { // already read-to-end. |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | // TODO: optimize for the special case when the input is SkMemoryStream. |
| 261 | return SkStreamCopy(&fStreamBuffer, fStream.get()); |
| 262 | } |
| 263 | |
| 264 | if (newSize <= fStreamBuffer.bytesWritten()) { // already buffered to newSize |
| 265 | return true; |
| 266 | } |
| 267 | if (fWholeStreamRead) { // newSize is larger than the whole stream. |
| 268 | return false; |
| 269 | } |
| 270 | |
yujieqin | 22000d1 | 2016-02-02 08:09:07 -0800 | [diff] [blame] | 271 | // Try to read at least 8192 bytes to avoid to many small reads. |
| 272 | const size_t kMinSizeToRead = 8192; |
| 273 | const size_t sizeRequested = newSize - fStreamBuffer.bytesWritten(); |
| 274 | const size_t sizeToRead = SkTMax(kMinSizeToRead, sizeRequested); |
| 275 | SkAutoSTMalloc<kMinSizeToRead, uint8> tempBuffer(sizeToRead); |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 276 | const size_t bytesRead = fStream->read(tempBuffer.get(), sizeToRead); |
yujieqin | 22000d1 | 2016-02-02 08:09:07 -0800 | [diff] [blame] | 277 | if (bytesRead < sizeRequested) { |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | return fStreamBuffer.write(tempBuffer.get(), bytesRead); |
| 281 | } |
| 282 | |
| 283 | SkAutoTDelete<SkStream> fStream; |
| 284 | bool fWholeStreamRead; |
| 285 | |
| 286 | SkDynamicMemoryWStream fStreamBuffer; |
| 287 | |
| 288 | const size_t kReadToEnd = 0; |
| 289 | }; |
| 290 | |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 291 | class SkRawAssetStream : public SkRawStream { |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 292 | public: |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 293 | // Will take the ownership of the stream. |
| 294 | explicit SkRawAssetStream(SkStream* stream) |
| 295 | : fStream(stream) |
| 296 | { |
| 297 | // Only use SkRawAssetStream when the stream is an asset stream. |
| 298 | SkASSERT(is_asset_stream(*stream)); |
| 299 | } |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 300 | |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 301 | ~SkRawAssetStream() override {} |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 302 | |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 303 | uint64 getLength() override { |
| 304 | return fStream->getLength(); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | bool read(void* data, size_t offset, size_t length) override { |
| 309 | if (length == 0) { |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | size_t sum; |
| 314 | if (!safe_add_to_size_t(offset, length, &sum)) { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | return fStream->seek(offset) && (fStream->read(data, length) == length); |
| 319 | } |
| 320 | |
| 321 | SkMemoryStream* transferBuffer(size_t offset, size_t size) override { |
| 322 | if (fStream->getLength() < offset) { |
| 323 | return nullptr; |
| 324 | } |
| 325 | |
| 326 | size_t sum; |
| 327 | if (!safe_add_to_size_t(offset, size, &sum)) { |
| 328 | return nullptr; |
| 329 | } |
| 330 | |
| 331 | // This will allow read less than the requested "size", because the JPEG codec wants to |
| 332 | // handle also a partial JPEG file. |
| 333 | const size_t bytesToRead = SkTMin(sum, fStream->getLength()) - offset; |
| 334 | if (bytesToRead == 0) { |
| 335 | return nullptr; |
| 336 | } |
| 337 | |
| 338 | if (fStream->getMemoryBase()) { // directly copy if getMemoryBase() is available. |
| 339 | SkAutoTUnref<SkData> data(SkData::NewWithCopy( |
| 340 | static_cast<const uint8_t*>(fStream->getMemoryBase()) + offset, bytesToRead)); |
| 341 | fStream.free(); |
| 342 | return new SkMemoryStream(data); |
| 343 | } else { |
| 344 | SkAutoTUnref<SkData> data(SkData::NewUninitialized(bytesToRead)); |
| 345 | if (!fStream->seek(offset)) { |
| 346 | return nullptr; |
| 347 | } |
| 348 | const size_t bytesRead = fStream->read(data->writable_data(), bytesToRead); |
| 349 | if (bytesRead < bytesToRead) { |
| 350 | data.reset(SkData::NewSubset(data.get(), 0, bytesRead)); |
| 351 | } |
| 352 | return new SkMemoryStream(data); |
| 353 | } |
| 354 | } |
| 355 | private: |
| 356 | SkAutoTDelete<SkStream> fStream; |
| 357 | }; |
| 358 | |
| 359 | class SkPiexStream : public ::piex::StreamInterface { |
| 360 | public: |
| 361 | // Will NOT take the ownership of the stream. |
| 362 | explicit SkPiexStream(SkRawStream* stream) : fStream(stream) {} |
| 363 | |
| 364 | ~SkPiexStream() override {} |
| 365 | |
| 366 | ::piex::Error GetData(const size_t offset, const size_t length, |
| 367 | uint8* data) override { |
| 368 | return fStream->read(static_cast<void*>(data), offset, length) ? |
| 369 | ::piex::Error::kOk : ::piex::Error::kFail; |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | private: |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 373 | SkRawStream* fStream; |
| 374 | }; |
| 375 | |
| 376 | class SkDngStream : public dng_stream { |
| 377 | public: |
| 378 | // Will NOT take the ownership of the stream. |
| 379 | SkDngStream(SkRawStream* stream) : fStream(stream) {} |
| 380 | |
| 381 | ~SkDngStream() override {} |
| 382 | |
| 383 | uint64 DoGetLength() override { return fStream->getLength(); } |
| 384 | |
| 385 | void DoRead(void* data, uint32 count, uint64 offset) override { |
| 386 | size_t sum; |
| 387 | if (!safe_add_to_size_t(static_cast<uint64>(count), offset, &sum) || |
| 388 | !fStream->read(data, static_cast<size_t>(offset), static_cast<size_t>(count))) { |
| 389 | ThrowReadFile(); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | private: |
| 394 | SkRawStream* fStream; |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 395 | }; |
| 396 | |
| 397 | class SkDngImage { |
| 398 | public: |
ebrauer | 46d2aa8 | 2016-02-17 08:04:00 -0800 | [diff] [blame] | 399 | /* |
| 400 | * Initializes the object with the information from Piex in a first attempt. This way it can |
| 401 | * save time and storage to obtain the DNG dimensions and color filter array (CFA) pattern |
| 402 | * which is essential for the demosaicing of the sensor image. |
| 403 | * Note: this will take the ownership of the stream. |
| 404 | */ |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 405 | static SkDngImage* NewFromStream(SkRawStream* stream) { |
| 406 | SkAutoTDelete<SkDngImage> dngImage(new SkDngImage(stream)); |
ebrauer | 46d2aa8 | 2016-02-17 08:04:00 -0800 | [diff] [blame] | 407 | if (!dngImage->initFromPiex()) { |
| 408 | if (!dngImage->readDng()) { |
| 409 | return nullptr; |
| 410 | } |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 411 | } |
| 412 | |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 413 | return dngImage.release(); |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Renders the DNG image to the size. The DNG SDK only allows scaling close to integer factors |
| 418 | * down to 80 pixels on the short edge. The rendered image will be close to the specified size, |
| 419 | * but there is no guarantee that any of the edges will match the requested size. E.g. |
| 420 | * 100% size: 4000 x 3000 |
| 421 | * requested size: 1600 x 1200 |
| 422 | * returned size could be: 2000 x 1500 |
| 423 | */ |
| 424 | dng_image* render(int width, int height) { |
| 425 | if (!fHost || !fInfo || !fNegative || !fDngStream) { |
| 426 | if (!this->readDng()) { |
| 427 | return nullptr; |
| 428 | } |
| 429 | } |
| 430 | |
mtklein | cb3d4fc | 2016-02-26 16:57:33 -0800 | [diff] [blame] | 431 | // render() takes ownership of fHost, fInfo, fNegative and fDngStream when available. |
| 432 | SkAutoTDelete<dng_host> host(fHost.release()); |
| 433 | SkAutoTDelete<dng_info> info(fInfo.release()); |
| 434 | SkAutoTDelete<dng_negative> negative(fNegative.release()); |
| 435 | SkAutoTDelete<dng_stream> dngStream(fDngStream.release()); |
| 436 | |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 437 | // DNG SDK preserves the aspect ratio, so it only needs to know the longer dimension. |
| 438 | const int preferredSize = SkTMax(width, height); |
| 439 | try { |
| 440 | host->SetPreferredSize(preferredSize); |
| 441 | host->ValidateSizes(); |
| 442 | |
| 443 | negative->ReadStage1Image(*host, *dngStream, *info); |
| 444 | |
| 445 | if (info->fMaskIndex != -1) { |
| 446 | negative->ReadTransparencyMask(*host, *dngStream, *info); |
| 447 | } |
| 448 | |
| 449 | negative->ValidateRawImageDigest(*host); |
| 450 | if (negative->IsDamaged()) { |
| 451 | return nullptr; |
| 452 | } |
| 453 | |
| 454 | const int32 kMosaicPlane = -1; |
| 455 | negative->BuildStage2Image(*host); |
| 456 | negative->BuildStage3Image(*host, kMosaicPlane); |
| 457 | |
| 458 | dng_render render(*host, *negative); |
| 459 | render.SetFinalSpace(dng_space_sRGB::Get()); |
| 460 | render.SetFinalPixelType(ttByte); |
| 461 | |
| 462 | dng_point stage3_size = negative->Stage3Image()->Size(); |
| 463 | render.SetMaximumSize(SkTMax(stage3_size.h, stage3_size.v)); |
| 464 | |
| 465 | return render.Render(); |
| 466 | } catch (...) { |
| 467 | return nullptr; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | const SkImageInfo& getImageInfo() const { |
| 472 | return fImageInfo; |
| 473 | } |
| 474 | |
| 475 | bool isScalable() const { |
| 476 | return fIsScalable; |
| 477 | } |
| 478 | |
| 479 | bool isXtransImage() const { |
| 480 | return fIsXtransImage; |
| 481 | } |
| 482 | |
| 483 | private: |
ebrauer | 46d2aa8 | 2016-02-17 08:04:00 -0800 | [diff] [blame] | 484 | void init(const int width, const int height, const dng_point& cfaPatternSize) { |
| 485 | fImageInfo = SkImageInfo::Make(width, height, kN32_SkColorType, kOpaque_SkAlphaType); |
| 486 | |
| 487 | // The DNG SDK scales only during demosaicing, so scaling is only possible when |
| 488 | // a mosaic info is available. |
| 489 | fIsScalable = cfaPatternSize.v != 0 && cfaPatternSize.h != 0; |
| 490 | fIsXtransImage = fIsScalable ? (cfaPatternSize.v == 6 && cfaPatternSize.h == 6) : false; |
| 491 | } |
| 492 | |
| 493 | bool initFromPiex() { |
| 494 | // Does not take the ownership of rawStream. |
| 495 | SkPiexStream piexStream(fStream.get()); |
| 496 | ::piex::PreviewImageData imageData; |
| 497 | if (::piex::IsRaw(&piexStream) |
| 498 | && ::piex::GetPreviewImageData(&piexStream, &imageData) == ::piex::Error::kOk) |
| 499 | { |
| 500 | dng_point cfaPatternSize(imageData.cfa_pattern_dim[1], imageData.cfa_pattern_dim[0]); |
| 501 | this->init(static_cast<int>(imageData.full_width), |
| 502 | static_cast<int>(imageData.full_height), cfaPatternSize); |
| 503 | return true; |
| 504 | } |
| 505 | return false; |
| 506 | } |
| 507 | |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 508 | bool readDng() { |
mtklein | cb3d4fc | 2016-02-26 16:57:33 -0800 | [diff] [blame] | 509 | // Due to the limit of DNG SDK, we need to reset host and info. |
| 510 | fHost.reset(new SkDngHost(&fAllocator)); |
| 511 | fInfo.reset(new dng_info); |
| 512 | fDngStream.reset(new SkDngStream(fStream)); |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 513 | try { |
| 514 | fHost->ValidateSizes(); |
| 515 | fInfo->Parse(*fHost, *fDngStream); |
| 516 | fInfo->PostParse(*fHost); |
| 517 | if (!fInfo->IsValidDNG()) { |
| 518 | return false; |
| 519 | } |
| 520 | |
| 521 | fNegative.reset(fHost->Make_dng_negative()); |
| 522 | fNegative->Parse(*fHost, *fDngStream, *fInfo); |
| 523 | fNegative->PostParse(*fHost, *fDngStream, *fInfo); |
| 524 | fNegative->SynchronizeMetadata(); |
| 525 | |
ebrauer | 46d2aa8 | 2016-02-17 08:04:00 -0800 | [diff] [blame] | 526 | dng_point cfaPatternSize(0, 0); |
| 527 | if (fNegative->GetMosaicInfo() != nullptr) { |
| 528 | cfaPatternSize = fNegative->GetMosaicInfo()->fCFAPatternSize; |
| 529 | } |
| 530 | this->init(static_cast<int>(fNegative->DefaultCropSizeH().As_real64()), |
| 531 | static_cast<int>(fNegative->DefaultCropSizeV().As_real64()), |
| 532 | cfaPatternSize); |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 533 | return true; |
| 534 | } catch (...) { |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 535 | return false; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | SkDngImage(SkRawStream* stream) |
| 540 | : fStream(stream) {} |
| 541 | |
| 542 | SkDngMemoryAllocator fAllocator; |
| 543 | SkAutoTDelete<SkRawStream> fStream; |
| 544 | SkAutoTDelete<dng_host> fHost; |
| 545 | SkAutoTDelete<dng_info> fInfo; |
| 546 | SkAutoTDelete<dng_negative> fNegative; |
| 547 | SkAutoTDelete<dng_stream> fDngStream; |
| 548 | |
| 549 | SkImageInfo fImageInfo; |
| 550 | bool fIsScalable; |
| 551 | bool fIsXtransImage; |
| 552 | }; |
| 553 | |
| 554 | /* |
| 555 | * Tries to handle the image with PIEX. If PIEX returns kOk and finds the preview image, create a |
| 556 | * SkJpegCodec. If PIEX returns kFail, then the file is invalid, return nullptr. In other cases, |
| 557 | * fallback to create SkRawCodec for DNG images. |
| 558 | */ |
| 559 | SkCodec* SkRawCodec::NewFromStream(SkStream* stream) { |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 560 | SkAutoTDelete<SkRawStream> rawStream; |
| 561 | if (is_asset_stream(*stream)) { |
| 562 | rawStream.reset(new SkRawAssetStream(stream)); |
| 563 | } else { |
| 564 | rawStream.reset(new SkRawBufferedStream(stream)); |
| 565 | } |
| 566 | |
| 567 | // Does not take the ownership of rawStream. |
| 568 | SkPiexStream piexStream(rawStream.get()); |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 569 | ::piex::PreviewImageData imageData; |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 570 | if (::piex::IsRaw(&piexStream)) { |
| 571 | ::piex::Error error = ::piex::GetPreviewImageData(&piexStream, &imageData); |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 572 | |
yujieqin | 2d172eb | 2016-02-23 06:49:38 -0800 | [diff] [blame] | 573 | if (error == ::piex::Error::kOk && imageData.preview.length > 0) { |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 574 | // transferBuffer() is destructive to the rawStream. Abandon the rawStream after this |
| 575 | // function call. |
| 576 | // FIXME: one may avoid the copy of memoryStream and use the buffered rawStream. |
| 577 | SkMemoryStream* memoryStream = |
yujieqin | 2d172eb | 2016-02-23 06:49:38 -0800 | [diff] [blame] | 578 | rawStream->transferBuffer(imageData.preview.offset, imageData.preview.length); |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 579 | return memoryStream ? SkJpegCodec::NewFromStream(memoryStream) : nullptr; |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 580 | } else if (error == ::piex::Error::kFail) { |
| 581 | return nullptr; |
| 582 | } |
| 583 | } |
| 584 | |
yujieqin | 9c7a8a4 | 2016-02-05 08:21:19 -0800 | [diff] [blame] | 585 | // Takes the ownership of the rawStream. |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 586 | SkAutoTDelete<SkDngImage> dngImage(SkDngImage::NewFromStream(rawStream.release())); |
| 587 | if (!dngImage) { |
| 588 | return nullptr; |
| 589 | } |
| 590 | |
| 591 | return new SkRawCodec(dngImage.release()); |
| 592 | } |
| 593 | |
| 594 | SkCodec::Result SkRawCodec::onGetPixels(const SkImageInfo& requestedInfo, void* dst, |
| 595 | size_t dstRowBytes, const Options& options, |
| 596 | SkPMColor ctable[], int* ctableCount, |
| 597 | int* rowsDecoded) { |
| 598 | if (!conversion_possible(requestedInfo, this->getInfo())) { |
| 599 | SkCodecPrintf("Error: cannot convert input type to output type.\n"); |
| 600 | return kInvalidConversion; |
| 601 | } |
| 602 | |
| 603 | SkAutoTDelete<SkSwizzler> swizzler(SkSwizzler::CreateSwizzler( |
| 604 | SkSwizzler::kRGB, nullptr, requestedInfo, options)); |
| 605 | SkASSERT(swizzler); |
| 606 | |
| 607 | const int width = requestedInfo.width(); |
| 608 | const int height = requestedInfo.height(); |
| 609 | SkAutoTDelete<dng_image> image(fDngImage->render(width, height)); |
| 610 | if (!image) { |
| 611 | return kInvalidInput; |
| 612 | } |
| 613 | |
| 614 | // Because the DNG SDK can not guarantee to render to requested size, we allow a small |
| 615 | // difference. Only the overlapping region will be converted. |
| 616 | const float maxDiffRatio = 1.03f; |
| 617 | const dng_point& imageSize = image->Size(); |
| 618 | if (imageSize.h / width > maxDiffRatio || imageSize.h < width || |
| 619 | imageSize.v / height > maxDiffRatio || imageSize.v < height) { |
| 620 | return SkCodec::kInvalidScale; |
| 621 | } |
| 622 | |
| 623 | void* dstRow = dst; |
yujieqin | 24716be | 2016-01-27 07:59:00 -0800 | [diff] [blame] | 624 | SkAutoTMalloc<uint8_t> srcRow(width * 3); |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 625 | |
| 626 | dng_pixel_buffer buffer; |
| 627 | buffer.fData = &srcRow[0]; |
| 628 | buffer.fPlane = 0; |
| 629 | buffer.fPlanes = 3; |
| 630 | buffer.fColStep = buffer.fPlanes; |
| 631 | buffer.fPlaneStep = 1; |
| 632 | buffer.fPixelType = ttByte; |
| 633 | buffer.fPixelSize = sizeof(uint8_t); |
scroggo | e645965 | 2016-01-30 10:06:11 -0800 | [diff] [blame] | 634 | buffer.fRowStep = width * 3; |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 635 | |
| 636 | for (int i = 0; i < height; ++i) { |
| 637 | buffer.fArea = dng_rect(i, 0, i + 1, width); |
| 638 | |
| 639 | try { |
| 640 | image->Get(buffer, dng_image::edge_zero); |
| 641 | } catch (...) { |
| 642 | *rowsDecoded = i; |
| 643 | return kIncompleteInput; |
| 644 | } |
| 645 | |
| 646 | swizzler->swizzle(dstRow, &srcRow[0]); |
| 647 | dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); |
| 648 | } |
| 649 | return kSuccess; |
| 650 | } |
| 651 | |
| 652 | SkISize SkRawCodec::onGetScaledDimensions(float desiredScale) const { |
| 653 | SkASSERT(desiredScale <= 1.f); |
| 654 | const SkISize dim = this->getInfo().dimensions(); |
| 655 | if (!fDngImage->isScalable()) { |
| 656 | return dim; |
| 657 | } |
| 658 | |
| 659 | // Limits the minimum size to be 80 on the short edge. |
yujieqin | 076d83d | 2016-01-27 08:25:53 -0800 | [diff] [blame] | 660 | const float shortEdge = static_cast<float>(SkTMin(dim.fWidth, dim.fHeight)); |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 661 | if (desiredScale < 80.f / shortEdge) { |
| 662 | desiredScale = 80.f / shortEdge; |
| 663 | } |
| 664 | |
| 665 | // For Xtrans images, the integer-factor scaling does not support the half-size scaling case |
| 666 | // (stronger downscalings are fine). In this case, returns the factor "3" scaling instead. |
| 667 | if (fDngImage->isXtransImage() && desiredScale > 1.f / 3.f && desiredScale < 1.f) { |
| 668 | desiredScale = 1.f / 3.f; |
| 669 | } |
| 670 | |
| 671 | // Round to integer-factors. |
| 672 | const float finalScale = std::floor(1.f/ desiredScale); |
yujieqin | 076d83d | 2016-01-27 08:25:53 -0800 | [diff] [blame] | 673 | return SkISize::Make(static_cast<int32_t>(std::floor(dim.fWidth / finalScale)), |
| 674 | static_cast<int32_t>(std::floor(dim.fHeight / finalScale))); |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | bool SkRawCodec::onDimensionsSupported(const SkISize& dim) { |
| 678 | const SkISize fullDim = this->getInfo().dimensions(); |
yujieqin | 076d83d | 2016-01-27 08:25:53 -0800 | [diff] [blame] | 679 | const float fullShortEdge = static_cast<float>(SkTMin(fullDim.fWidth, fullDim.fHeight)); |
| 680 | const float shortEdge = static_cast<float>(SkTMin(dim.fWidth, dim.fHeight)); |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 681 | |
| 682 | SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEdge / shortEdge)); |
| 683 | SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge / shortEdge)); |
| 684 | return sizeFloor == dim || sizeCeil == dim; |
| 685 | } |
| 686 | |
| 687 | SkRawCodec::~SkRawCodec() {} |
| 688 | |
| 689 | SkRawCodec::SkRawCodec(SkDngImage* dngImage) |
| 690 | : INHERITED(dngImage->getImageInfo(), nullptr) |
| 691 | , fDngImage(dngImage) {} |