OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2015 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 "SkRawCodec.h" |
| 10 #include "SkCodecPriv.h" |
| 11 #include "SkColorPriv.h" |
| 12 #include "SkStream.h" |
| 13 #include "SkTemplates.h" |
| 14 #include "SkTypes.h" |
| 15 #include "SkSwizzler.h" |
| 16 |
| 17 #include "dng_color_space.h" |
| 18 #include "dng_exceptions.h" |
| 19 #include "dng_host.h" |
| 20 #include "dng_info.h" |
| 21 #include "dng_render.h" |
| 22 |
| 23 namespace { |
| 24 |
| 25 class DngStream : public dng_stream { |
| 26 public: |
| 27 explicit DngStream(SkStream* stream) : data_(stream->getMemoryBase()), |
| 28 data_size_(stream->getLength()) {} |
| 29 ~DngStream() override {} |
| 30 |
| 31 protected: |
| 32 uint64 DoGetLength() override { return data_size_; } |
| 33 void DoRead(void* data, uint32 count, uint64 offset) override { |
| 34 if (data_size_ < offset + count) { |
| 35 ThrowReadFile(); |
| 36 } |
| 37 |
| 38 std::memcpy(data, reinterpret_cast<const uint8*>(data_) + offset, count); |
| 39 } |
| 40 |
| 41 private: |
| 42 const void* data_; |
| 43 size_t data_size_; |
| 44 }; |
| 45 |
| 46 bool IsDng(SkStream* data) { |
| 47 try { |
| 48 DngStream stream(data); |
| 49 dng_host host; |
| 50 dng_info info; |
| 51 info.Parse(host, stream); |
| 52 info.PostParse(host); |
| 53 return info.IsValidDNG(); |
| 54 } catch (...) { // Handle all kinds of exceptions as "false". |
| 55 return false; |
| 56 } |
| 57 } |
| 58 |
| 59 bool ReadDng(dng_stream* stream, dng_host* host, dng_info* info, |
| 60 std::unique_ptr<dng_negative>* negative) { |
| 61 const uint32 kPreferredSize = 0; |
| 62 const uint32 kMinimumSize = 0; |
| 63 const uint32 kMaximumSize = 0; |
| 64 try { |
| 65 host->SetPreferredSize(kPreferredSize); |
| 66 host->SetMinimumSize(kMinimumSize); |
| 67 host->SetMaximumSize(kMaximumSize); |
| 68 |
| 69 host->ValidateSizes(); |
| 70 |
| 71 // Read stage 1 image into the negative. |
| 72 { |
| 73 info->Parse(*host, *stream); |
| 74 info->PostParse(*host); |
| 75 |
| 76 if (!info->IsValidDNG()) { |
| 77 return false; |
| 78 } |
| 79 |
| 80 negative->reset(host->Make_dng_negative()); |
| 81 (*negative)->Parse(*host, *stream, *info); |
| 82 (*negative)->PostParse(*host, *stream, *info); |
| 83 } |
| 84 |
| 85 (*negative)->SynchronizeMetadata(); |
| 86 } catch (const dng_exception& exception) { |
| 87 return false; |
| 88 } catch (...) { // Following the example from dng_validate.cc we also catch |
| 89 // at this point. |
| 90 return false; |
| 91 } |
| 92 |
| 93 return true; |
| 94 } |
| 95 |
| 96 bool PrepareStage3(dng_stream* stream, dng_host* host, dng_info* info, |
| 97 dng_negative* negative) { |
| 98 if (negative->Stage3Image() == nullptr) { |
| 99 negative->ReadStage1Image(*host, *stream, *info); |
| 100 |
| 101 if (info->fMaskIndex != -1) { |
| 102 negative->ReadTransparencyMask(*host, *stream, *info); |
| 103 } |
| 104 |
| 105 negative->ValidateRawImageDigest(*host); |
| 106 if (negative->IsDamaged()) { |
| 107 return false; |
| 108 } |
| 109 |
| 110 const int32 kMosaicPlane = -1; |
| 111 negative->BuildStage2Image(*host); |
| 112 negative->BuildStage3Image(*host, kMosaicPlane); |
| 113 } |
| 114 |
| 115 return true; |
| 116 } |
| 117 |
| 118 } // namespace |
| 119 |
| 120 bool SkRawCodec::IsRaw(SkStream* stream) { |
| 121 return IsDng(stream); |
| 122 } |
| 123 |
| 124 SkCodec* SkRawCodec::NewFromStream(SkStream* data) { |
| 125 DngStream stream(data); |
| 126 dng_host host; |
| 127 dng_info info; |
| 128 std::unique_ptr<dng_negative> negative; |
| 129 if (!ReadDng(&stream, &host, &info, &negative) && |
| 130 !PrepareStage3(&stream, &host, &info, negative.get())) { |
| 131 return nullptr; |
| 132 } |
| 133 const SkImageInfo& imageInfo = SkImageInfo::Make( |
| 134 negative->DefaultCropSizeH().As_real64(), |
| 135 negative->DefaultCropSizeV().As_real64(), kRGBA_8888_SkColorType, |
| 136 kOpaque_SkAlphaType); |
| 137 return new SkRawCodec(imageInfo, data); |
| 138 } |
| 139 |
| 140 SkCodec::Result SkRawCodec::onGetPixels(const SkImageInfo& requestedInfo, void*
dst, |
| 141 size_t dstRowBytes, const Options& optio
ns, |
| 142 SkPMColor ctable[], int* ctableCount, |
| 143 int* rowsDecoded) { |
| 144 const int width = requestedInfo.width(); |
| 145 //const int height = requestedInfo.height(); |
| 146 std::unique_ptr<dng_image> final_dng_image; |
| 147 try { |
| 148 DngStream stream(this->stream()); |
| 149 dng_host host; |
| 150 dng_info info; |
| 151 std::unique_ptr<dng_negative> negative; |
| 152 if (!ReadDng(&stream, &host, &info, &negative) || |
| 153 !PrepareStage3(&stream, &host, &info, negative.get())) { |
| 154 return kInvalidInput; |
| 155 } |
| 156 |
| 157 dng_render render(host, *negative); |
| 158 render.SetFinalSpace(dng_space_sRGB::Get()); |
| 159 render.SetFinalPixelType(ttByte); |
| 160 |
| 161 final_dng_image.reset(render.Render()); |
| 162 } catch (const dng_exception& exception) { |
| 163 return kInvalidInput; |
| 164 } catch (...) { |
| 165 return kInvalidInput; |
| 166 } |
| 167 |
| 168 void* dst_row = dst; |
| 169 //uint8 src_row[width * 3]; |
| 170 //const SkPMColor color = SkPreMultiplyColor(0xFFFFFFFF); |
| 171 /*std::unique_ptr<SkSwizzler> swizzler(SkSwizzler::CreateSwizzler( |
| 172 SkSwizzler::kRGB, nullptr, requestedInfo, options));*/ |
| 173 for (int i = 0; i < height - 1; ++i) { |
| 174 dng_pixel_buffer buffer; |
| 175 buffer.fData = dst_row; |
| 176 buffer.fArea = dng_rect(i, 0, i + 1, width); |
| 177 buffer.fPlane = 0; |
| 178 buffer.fPlanes = 4; |
| 179 buffer.fColStep = buffer.fPlanes; |
| 180 buffer.fPlaneStep = 1; |
| 181 buffer.fPixelType = ttByte; |
| 182 buffer.fPixelSize = sizeof(uint8); |
| 183 buffer.fRowStep = dstRowBytes; |
| 184 final_dng_image->Get(buffer, dng_image::edge_zero); |
| 185 //swizzler->swizzle(dst_row, &src_row[0]); |
| 186 dst_row = SkTAddOffset<void>(dst_row, dstRowBytes); |
| 187 } |
| 188 return kSuccess; |
| 189 } |
| 190 |
| 191 SkRawCodec::SkRawCodec(const SkImageInfo& srcInfo, SkStream* stream) |
| 192 : INHERITED(srcInfo, stream) {} |
| 193 |
OLD | NEW |