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