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