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 #include "SkJpegCodec.h" | |
17 | |
18 #include "dng_color_space.h" | |
19 #include "dng_exceptions.h" | |
20 #include "dng_host.h" | |
21 #include "dng_info.h" | |
22 #include "dng_render.h" | |
23 #include "dng_stream.h" | |
24 | |
25 #include "src/piex.h" | |
26 | |
27 #include <cstring> | |
28 #include <cmath> | |
29 #include <memory> | |
30 #include <vector> | |
31 | |
32 namespace { | |
33 | |
34 /* | |
35 * For the dng sdk stream implementation we need to provide the full | |
36 * length of the image file. However, that is not too bad because we | |
37 * would need it anyway for the dng rendering pipeline. | |
38 */ | |
39 class SkDngStream : public dng_stream { | |
40 public: | |
41 SkDngStream(size_t size, void* data) | |
scroggo
2016/01/06 22:30:20
nit: I think we would typically switch the order:
yujieqin
2016/01/07 09:22:26
Done.
| |
42 : fData(data) | |
43 , fDataSize(size) {} | |
44 | |
45 ~SkDngStream() override {} | |
46 | |
47 protected: | |
48 uint64 DoGetLength() override { return fDataSize; } | |
49 void DoRead(void* data, uint32 count, uint64 offset) override { | |
50 if (fDataSize < offset + count) { | |
51 ThrowReadFile(); | |
52 } | |
53 memcpy(data, reinterpret_cast<const uint8*>(fData) + offset, count); | |
54 } | |
55 | |
56 private: | |
57 const void* fData; | |
scroggo
2016/01/06 22:30:20
Add a comment that this is owned by the codec, rat
yujieqin
2016/01/07 09:22:26
We may revisit here when we change the PiexStream
| |
58 const size_t fDataSize; | |
59 }; | |
60 | |
61 class SkPiexStream : public ::piex::StreamInterface { | |
62 public: | |
63 SkPiexStream(size_t size, void* data) | |
64 : fData(data) | |
65 , fDataSize(size) {} | |
66 | |
67 ::piex::Error GetData(const size_t offset, const size_t length, | |
68 uint8* data) override { | |
69 if (offset >= fDataSize || fDataSize - offset < length) { | |
70 return ::piex::Error::kFail; | |
71 } | |
72 memcpy(data, reinterpret_cast<const uint8*>(fData) + offset, length); | |
73 return ::piex::Error::kOk; | |
74 } | |
75 | |
76 private: | |
77 const void* fData; | |
78 const size_t fDataSize; | |
79 }; | |
80 | |
81 bool ReadDng(dng_stream* stream, dng_host* host, dng_info* info, | |
82 SkAutoTDelete<dng_negative>* negative) { | |
scroggo
2016/01/06 22:30:20
I mentioned this before, but I didn't see any resp
yujieqin
2016/01/07 09:22:26
Oh, we misunderstood your point before. It should
| |
83 const uint32 kPreferredSize = 0; | |
84 const uint32 kMinimumSize = 0; | |
85 const uint32 kMaximumSize = 0; | |
86 try { | |
87 host->SetPreferredSize(kPreferredSize); | |
88 host->SetMinimumSize(kMinimumSize); | |
89 host->SetMaximumSize(kMaximumSize); | |
90 | |
91 host->ValidateSizes(); | |
92 | |
93 // Read stage 1 image into the negative. | |
94 { | |
95 info->Parse(*host, *stream); | |
96 info->PostParse(*host); | |
97 | |
98 if (!info->IsValidDNG()) { | |
99 return false; | |
100 } | |
101 | |
102 negative->reset(host->Make_dng_negative()); | |
103 (*negative)->Parse(*host, *stream, *info); | |
104 (*negative)->PostParse(*host, *stream, *info); | |
105 } | |
106 | |
107 (*negative)->SynchronizeMetadata(); | |
108 } catch (const dng_exception& exception) { | |
msarett
2016/01/06 22:50:25
Can this be a single catch block? Since both do t
yujieqin
2016/01/07 09:22:26
Done.
| |
109 return false; | |
110 } catch (...) { | |
111 return false; | |
112 } | |
113 | |
114 return true; | |
115 } | |
116 | |
117 bool PrepareStage3(dng_stream* stream, dng_host* host, dng_info* info, | |
118 dng_negative* negative) { | |
119 if (negative->Stage3Image() == nullptr) { | |
120 negative->ReadStage1Image(*host, *stream, *info); | |
121 | |
122 if (info->fMaskIndex != -1) { | |
123 negative->ReadTransparencyMask(*host, *stream, *info); | |
124 } | |
125 | |
126 negative->ValidateRawImageDigest(*host); | |
127 if (negative->IsDamaged()) { | |
128 return false; | |
129 } | |
130 | |
131 const int32 kMosaicPlane = -1; | |
132 negative->BuildStage2Image(*host); | |
133 negative->BuildStage3Image(*host, kMosaicPlane); | |
134 } | |
135 | |
136 return true; | |
137 } | |
138 | |
139 } // namespace | |
140 | |
141 SkCodec* SkRawCodec::NewFromStream(SkStream* stream) { | |
142 SkAutoMalloc data; | |
143 const size_t dataSize = SkCopyStreamToStorage(&data, stream); | |
144 | |
145 SkPiexStream piexStream(dataSize, data.get()); | |
146 ::piex::PreviewImageData imageData; | |
147 if (::piex::IsRaw(&piexStream) && | |
148 ::piex::GetPreviewImageData(&piexStream, &imageData) == ::piex::Error::k Ok) { | |
149 return SkJpegCodec::NewFromStream(new SkMemoryStream( | |
scroggo
2016/01/06 22:30:20
nit: This is hard to read since the code lines up
yujieqin
2016/01/07 09:22:26
Done.
| |
150 reinterpret_cast<const uint8*>(data.get()) + imageData.jpeg_offset, | |
scroggo
2016/01/06 22:30:20
This should be indented 8 spaces, rather than 4
yujieqin
2016/01/07 09:22:26
Done.
| |
151 imageData.jpeg_length, true)); | |
152 } | |
153 | |
154 SkDngStream dngStream(dataSize, data.get()); | |
155 dng_host host; | |
156 dng_info info; | |
157 SkAutoTDelete<dng_negative> negative; | |
158 if (!ReadDng(&dngStream, &host, &info, &negative)) { | |
159 return nullptr; | |
160 } | |
161 | |
162 const SkImageInfo& imageInfo = SkImageInfo::Make( | |
163 negative->DefaultCropSizeH().As_real64(), | |
164 negative->DefaultCropSizeV().As_real64(), | |
165 kN32_SkColorType, kOpaque_SkAlphaType); | |
166 return new SkRawCodec(imageInfo, dataSize, data.detach()); | |
167 } | |
168 | |
169 SkCodec::Result SkRawCodec::onGetPixels(const SkImageInfo& requestedInfo, void* dst, | |
170 size_t dstRowBytes, const Options& optio ns, | |
171 SkPMColor ctable[], int* ctableCount, | |
172 int* rowsDecoded) { | |
173 const int width = requestedInfo.width(); | |
174 const int height = requestedInfo.height(); | |
175 SkAutoTDelete<dng_image> finalDngImage; | |
176 try { | |
177 SkDngStream dngStream(fDngLength, fDngStorage); | |
scroggo
2016/01/06 22:30:20
nit: this should be indented (although I think it
yujieqin
2016/01/07 09:22:26
Could you explain a bit more here about:
* indente
scroggo
2016/01/07 13:53:20
try {
SkDngStream dngStream(fDngLength, fDngSt
yujieqin
2016/01/07 15:52:01
We added the SK_CODEC_DECODES_RAW in codec.gyp:raw
| |
178 | |
179 dng_host host; | |
180 dng_info info; | |
181 SkAutoTDelete<dng_negative> negative; | |
182 if (!ReadDng(&dngStream, &host, &info, &negative) || | |
183 !PrepareStage3(&dngStream, &host, &info, negative.get())) { | |
184 return kInvalidInput; | |
185 } | |
186 | |
187 dng_render render(host, *negative); | |
188 render.SetFinalSpace(dng_space_sRGB::Get()); | |
189 render.SetFinalPixelType(ttByte); | |
190 | |
191 finalDngImage.reset(render.Render()); | |
192 } catch (const dng_exception& exception) { | |
193 return kInvalidInput; | |
194 } catch (...) { | |
195 return kInvalidInput; | |
196 } | |
197 | |
198 void* dstRow = dst; | |
199 uint8 srcRow[width * 3]; | |
200 SkAutoTDelete<SkSwizzler> swizzler(SkSwizzler::CreateSwizzler( | |
201 SkSwizzler::kRGB, nullptr, requestedInfo, options)); | |
202 if (!swizzler) { | |
msarett
2016/01/06 22:50:25
Maybe this instead can be:
SkASSERT(swizzler);
I
yujieqin
2016/01/07 09:22:26
Done.
| |
203 return kInvalidInput; | |
204 } | |
205 for (int i = 0; i < height; ++i) { | |
206 dng_pixel_buffer buffer; | |
207 buffer.fData = &srcRow[0]; | |
208 buffer.fArea = dng_rect(i, 0, i + 1, width); | |
209 buffer.fPlane = 0; | |
210 buffer.fPlanes = 3; | |
211 buffer.fColStep = buffer.fPlanes; | |
212 buffer.fPlaneStep = 1; | |
213 buffer.fPixelType = ttByte; | |
214 buffer.fPixelSize = sizeof(uint8); | |
215 buffer.fRowStep = sizeof(srcRow); | |
216 finalDngImage->Get(buffer, dng_image::edge_zero); | |
217 swizzler->swizzle(dstRow, &srcRow[0]); | |
218 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); | |
219 } | |
220 return kSuccess; | |
221 } | |
222 | |
223 SkRawCodec::~SkRawCodec() { | |
224 sk_free(fDngStorage); | |
scroggo
2016/01/06 22:30:20
nit: This should be indented four spaces (although
yujieqin
2016/01/07 09:22:26
Done.
| |
225 } | |
226 | |
227 SkRawCodec::SkRawCodec(const SkImageInfo& srcInfo, size_t length, void* storage) | |
228 : INHERITED(srcInfo, nullptr), fDngLength(length), fDngStorage(storage) {} | |
OLD | NEW |