OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #ifndef CORE_FXCODEC_CODEC_CODEC_INT_H_ | 7 #ifndef CORE_FXCODEC_CODEC_CODEC_INT_H_ |
8 #define CORE_FXCODEC_CODEC_CODEC_INT_H_ | 8 #define CORE_FXCODEC_CODEC_CODEC_INT_H_ |
9 | 9 |
10 #include <limits.h> | 10 #include <limits.h> |
11 | 11 |
12 #include <list> | 12 #include <list> |
13 #include <map> | 13 #include <map> |
14 #include <memory> | 14 #include <memory> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "core/fxcodec/include/fx_codec.h" | 17 #include "core/fxcodec/include/fx_codec.h" |
18 #include "core/fxcodec/jbig2/JBig2_Context.h" | 18 #include "core/fxcodec/jbig2/JBig2_Context.h" |
19 #include "third_party/libopenjpeg20/openjpeg.h" // For OPJ_SIZE_T. | 19 #include "third_party/libopenjpeg20/openjpeg.h" |
20 | 20 |
21 class CFX_IccProfileCache; | |
22 class CFX_IccTransformCache; | |
23 class CPDF_ColorSpace; | 21 class CPDF_ColorSpace; |
24 | 22 |
25 class CCodec_BasicModule : public ICodec_BasicModule { | |
26 public: | |
27 // ICodec_BasicModule: | |
28 FX_BOOL RunLengthEncode(const uint8_t* src_buf, | |
29 uint32_t src_size, | |
30 uint8_t*& dest_buf, | |
31 uint32_t& dest_size) override; | |
32 FX_BOOL A85Encode(const uint8_t* src_buf, | |
33 uint32_t src_size, | |
34 uint8_t*& dest_buf, | |
35 uint32_t& dest_size) override; | |
36 ICodec_ScanlineDecoder* CreateRunLengthDecoder(const uint8_t* src_buf, | |
37 uint32_t src_size, | |
38 int width, | |
39 int height, | |
40 int nComps, | |
41 int bpc) override; | |
42 }; | |
43 | |
44 class CCodec_ScanlineDecoder : public ICodec_ScanlineDecoder { | |
45 public: | |
46 CCodec_ScanlineDecoder(); | |
47 ~CCodec_ScanlineDecoder() override; | |
48 | |
49 // ICodec_ScanlineDecoder | |
50 const uint8_t* GetScanline(int line) override; | |
51 FX_BOOL SkipToScanline(int line, IFX_Pause* pPause) override; | |
52 int GetWidth() override { return m_OutputWidth; } | |
53 int GetHeight() override { return m_OutputHeight; } | |
54 int CountComps() override { return m_nComps; } | |
55 int GetBPC() override { return m_bpc; } | |
56 | |
57 protected: | |
58 virtual FX_BOOL v_Rewind() = 0; | |
59 virtual uint8_t* v_GetNextLine() = 0; | |
60 | |
61 uint8_t* ReadNextLine(); | |
62 | |
63 int m_OrigWidth; | |
64 int m_OrigHeight; | |
65 int m_OutputWidth; | |
66 int m_OutputHeight; | |
67 int m_nComps; | |
68 int m_bpc; | |
69 uint32_t m_Pitch; | |
70 int m_NextLine; | |
71 uint8_t* m_pLastScanline; | |
72 }; | |
73 | |
74 class CCodec_FaxModule : public ICodec_FaxModule { | |
75 public: | |
76 // ICodec_FaxModule: | |
77 ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf, | |
78 uint32_t src_size, | |
79 int width, | |
80 int height, | |
81 int K, | |
82 FX_BOOL EndOfLine, | |
83 FX_BOOL EncodedByteAlign, | |
84 FX_BOOL BlackIs1, | |
85 int Columns, | |
86 int Rows) override; | |
87 FX_BOOL Encode(const uint8_t* src_buf, | |
88 int width, | |
89 int height, | |
90 int pitch, | |
91 uint8_t*& dest_buf, | |
92 uint32_t& dest_size) override; | |
93 }; | |
94 | |
95 class CCodec_FlateModule : public ICodec_FlateModule { | |
96 public: | |
97 virtual ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf, | |
98 uint32_t src_size, | |
99 int width, | |
100 int height, | |
101 int nComps, | |
102 int bpc, | |
103 int predictor, | |
104 int Colors, | |
105 int BitsPerComponent, | |
106 int Columns); | |
107 virtual uint32_t FlateOrLZWDecode(FX_BOOL bLZW, | |
108 const uint8_t* src_buf, | |
109 uint32_t src_size, | |
110 FX_BOOL bEarlyChange, | |
111 int predictor, | |
112 int Colors, | |
113 int BitsPerComponent, | |
114 int Columns, | |
115 uint32_t estimated_size, | |
116 uint8_t*& dest_buf, | |
117 uint32_t& dest_size); | |
118 virtual FX_BOOL Encode(const uint8_t* src_buf, | |
119 uint32_t src_size, | |
120 int predictor, | |
121 int Colors, | |
122 int BitsPerComponent, | |
123 int Columns, | |
124 uint8_t*& dest_buf, | |
125 uint32_t& dest_size); | |
126 virtual FX_BOOL Encode(const uint8_t* src_buf, | |
127 uint32_t src_size, | |
128 uint8_t*& dest_buf, | |
129 uint32_t& dest_size); | |
130 }; | |
131 | |
132 class CCodec_JpegModule : public ICodec_JpegModule { | |
133 public: | |
134 CCodec_JpegModule() {} | |
135 ICodec_ScanlineDecoder* CreateDecoder(const uint8_t* src_buf, | |
136 uint32_t src_size, | |
137 int width, | |
138 int height, | |
139 int nComps, | |
140 FX_BOOL ColorTransform) override; | |
141 FX_BOOL LoadInfo(const uint8_t* src_buf, | |
142 uint32_t src_size, | |
143 int& width, | |
144 int& height, | |
145 int& num_components, | |
146 int& bits_per_components, | |
147 FX_BOOL& color_transform, | |
148 uint8_t** icc_buf_ptr, | |
149 uint32_t* icc_length) override; | |
150 FX_BOOL Encode(const CFX_DIBSource* pSource, | |
151 uint8_t*& dest_buf, | |
152 FX_STRSIZE& dest_size, | |
153 int quality, | |
154 const uint8_t* icc_buf, | |
155 uint32_t icc_length) override; | |
156 void* Start() override; | |
157 void Finish(void* pContext) override; | |
158 void Input(void* pContext, | |
159 const uint8_t* src_buf, | |
160 uint32_t src_size) override; | |
161 #ifndef PDF_ENABLE_XFA | |
162 int ReadHeader(void* pContext, int* width, int* height, int* nComps) override; | |
163 #else // PDF_ENABLE_XFA | |
164 int ReadHeader(void* pContext, | |
165 int* width, | |
166 int* height, | |
167 int* nComps, | |
168 CFX_DIBAttribute* pAttribute) override; | |
169 #endif // PDF_ENABLE_XFA | |
170 int StartScanline(void* pContext, int down_scale) override; | |
171 FX_BOOL ReadScanline(void* pContext, uint8_t* dest_buf) override; | |
172 uint32_t GetAvailInput(void* pContext, uint8_t** avail_buf_ptr) override; | |
173 }; | |
174 | |
175 #ifdef PDF_ENABLE_XFA | |
176 #define PNG_ERROR_SIZE 256 | |
177 class CCodec_PngModule : public ICodec_PngModule { | |
178 public: | |
179 CCodec_PngModule() { FXSYS_memset(m_szLastError, '\0', PNG_ERROR_SIZE); } | |
180 | |
181 virtual void* Start(void* pModule); | |
182 virtual void Finish(void* pContext); | |
183 virtual FX_BOOL Input(void* pContext, | |
184 const uint8_t* src_buf, | |
185 uint32_t src_size, | |
186 CFX_DIBAttribute* pAttribute); | |
187 | |
188 protected: | |
189 FX_CHAR m_szLastError[PNG_ERROR_SIZE]; | |
190 }; | |
191 class CCodec_GifModule : public ICodec_GifModule { | |
192 public: | |
193 CCodec_GifModule() { FXSYS_memset(m_szLastError, '\0', 256); } | |
194 virtual void* Start(void* pModule); | |
195 virtual void Finish(void* pContext); | |
196 virtual uint32_t GetAvailInput(void* pContext, uint8_t** avail_buf_ptr); | |
197 virtual void Input(void* pContext, const uint8_t* src_buf, uint32_t src_size); | |
198 | |
199 virtual int32_t ReadHeader(void* pContext, | |
200 int* width, | |
201 int* height, | |
202 int* pal_num, | |
203 void** pal_pp, | |
204 int* bg_index, | |
205 CFX_DIBAttribute* pAttribute); | |
206 | |
207 virtual int32_t LoadFrameInfo(void* pContext, int* frame_num); | |
208 | |
209 virtual int32_t LoadFrame(void* pContext, | |
210 int frame_num, | |
211 CFX_DIBAttribute* pAttribute); | |
212 | |
213 protected: | |
214 FX_CHAR m_szLastError[256]; | |
215 }; | |
216 class CCodec_BmpModule : public ICodec_BmpModule { | |
217 public: | |
218 CCodec_BmpModule() { FXSYS_memset(m_szLastError, 0, sizeof(m_szLastError)); } | |
219 void* Start(void* pModule) override; | |
220 void Finish(void* pContext) override; | |
221 uint32_t GetAvailInput(void* pContext, uint8_t** avail_buf_ptr) override; | |
222 void Input(void* pContext, | |
223 const uint8_t* src_buf, | |
224 uint32_t src_size) override; | |
225 int32_t ReadHeader(void* pContext, | |
226 int32_t* width, | |
227 int32_t* height, | |
228 FX_BOOL* tb_flag, | |
229 int32_t* components, | |
230 int32_t* pal_num, | |
231 uint32_t** pal_pp, | |
232 CFX_DIBAttribute* pAttribute) override; | |
233 int32_t LoadImage(void* pContext) override; | |
234 | |
235 protected: | |
236 FX_CHAR m_szLastError[256]; | |
237 }; | |
238 #endif // PDF_ENABLE_XFA | |
239 | |
240 class CCodec_IccModule : public ICodec_IccModule { | |
241 public: | |
242 ~CCodec_IccModule() override; | |
243 | |
244 // ICodec_IccModule: | |
245 IccCS GetProfileCS(const uint8_t* pProfileData, | |
246 unsigned int dwProfileSize) override; | |
247 IccCS GetProfileCS(IFX_FileRead* pFile) override; | |
248 void* CreateTransform(ICodec_IccModule::IccParam* pInputParam, | |
249 ICodec_IccModule::IccParam* pOutputParam, | |
250 ICodec_IccModule::IccParam* pProofParam = NULL, | |
251 uint32_t dwIntent = Icc_INTENT_PERCEPTUAL, | |
252 uint32_t dwFlag = Icc_FLAGS_DEFAULT, | |
253 uint32_t dwPrfIntent = Icc_INTENT_ABSOLUTE_COLORIMETRIC, | |
254 uint32_t dwPrfFlag = Icc_FLAGS_SOFTPROOFING) override; | |
255 void* CreateTransform_sRGB( | |
256 const uint8_t* pProfileData, | |
257 uint32_t dwProfileSize, | |
258 uint32_t& nComponents, | |
259 int32_t intent = 0, | |
260 uint32_t dwSrcFormat = Icc_FORMAT_DEFAULT) override; | |
261 void* CreateTransform_CMYK( | |
262 const uint8_t* pSrcProfileData, | |
263 uint32_t dwSrcProfileSize, | |
264 uint32_t& nSrcComponents, | |
265 const uint8_t* pDstProfileData, | |
266 uint32_t dwDstProfileSize, | |
267 int32_t intent = 0, | |
268 uint32_t dwSrcFormat = Icc_FORMAT_DEFAULT, | |
269 uint32_t dwDstFormat = Icc_FORMAT_DEFAULT) override; | |
270 void DestroyTransform(void* pTransform) override; | |
271 void Translate(void* pTransform, | |
272 FX_FLOAT* pSrcValues, | |
273 FX_FLOAT* pDestValues) override; | |
274 void TranslateScanline(void* pTransform, | |
275 uint8_t* pDest, | |
276 const uint8_t* pSrc, | |
277 int pixels) override; | |
278 void SetComponents(uint32_t nComponents) override { | |
279 m_nComponents = nComponents; | |
280 } | |
281 | |
282 protected: | |
283 enum Icc_CLASS { | |
284 Icc_CLASS_INPUT = 0, | |
285 Icc_CLASS_OUTPUT, | |
286 Icc_CLASS_PROOF, | |
287 Icc_CLASS_MAX | |
288 }; | |
289 void* CreateProfile(ICodec_IccModule::IccParam* pIccParam, | |
290 Icc_CLASS ic, | |
291 CFX_BinaryBuf* pTransformKey); | |
292 | |
293 uint32_t m_nComponents; | |
294 std::map<CFX_ByteString, CFX_IccTransformCache*> m_MapTranform; | |
295 std::map<CFX_ByteString, CFX_IccProfileCache*> m_MapProfile; | |
296 }; | |
297 | |
298 class CCodec_JpxModule : public ICodec_JpxModule { | |
299 public: | |
300 CCodec_JpxModule(); | |
301 ~CCodec_JpxModule() override; | |
302 | |
303 // ICodec_JpxModule: | |
304 CJPX_Decoder* CreateDecoder(const uint8_t* src_buf, | |
305 uint32_t src_size, | |
306 CPDF_ColorSpace* cs) override; | |
307 void GetImageInfo(CJPX_Decoder* pDecoder, | |
308 uint32_t* width, | |
309 uint32_t* height, | |
310 uint32_t* components) override; | |
311 bool Decode(CJPX_Decoder* pDecoder, | |
312 uint8_t* dest_data, | |
313 int pitch, | |
314 const std::vector<uint8_t>& offsets) override; | |
315 void DestroyDecoder(CJPX_Decoder* pDecoder) override; | |
316 }; | |
317 | |
318 #ifdef PDF_ENABLE_XFA | |
319 class CCodec_TiffModule : public ICodec_TiffModule { | |
320 public: | |
321 // ICodec_TiffModule | |
322 void* CreateDecoder(IFX_FileRead* file_ptr) override; | |
323 void GetFrames(void* ctx, int32_t& frames) override; | |
324 FX_BOOL LoadFrameInfo(void* ctx, | |
325 int32_t frame, | |
326 uint32_t& width, | |
327 uint32_t& height, | |
328 uint32_t& comps, | |
329 uint32_t& bpc, | |
330 CFX_DIBAttribute* pAttribute) override; | |
331 FX_BOOL Decode(void* ctx, class CFX_DIBitmap* pDIBitmap) override; | |
332 void DestroyDecoder(void* ctx) override; | |
333 | |
334 protected: | |
335 ~CCodec_TiffModule() override {} | |
336 }; | |
337 #endif // PDF_ENABLE_XFA | |
338 | |
339 class CCodec_Jbig2Context { | |
340 public: | |
341 CCodec_Jbig2Context(); | |
342 ~CCodec_Jbig2Context() {} | |
343 | |
344 uint32_t m_width; | |
345 uint32_t m_height; | |
346 CPDF_StreamAcc* m_pGlobalStream; | |
347 CPDF_StreamAcc* m_pSrcStream; | |
348 uint8_t* m_dest_buf; | |
349 uint32_t m_dest_pitch; | |
350 IFX_Pause* m_pPause; | |
351 CJBig2_Context* m_pContext; | |
352 CJBig2_Image* m_dest_image; | |
353 }; | |
354 class CCodec_Jbig2Module : public ICodec_Jbig2Module { | |
355 public: | |
356 CCodec_Jbig2Module() {} | |
357 ~CCodec_Jbig2Module() override; | |
358 | |
359 // ICodec_Jbig2Module | |
360 void* CreateJbig2Context() override; | |
361 FXCODEC_STATUS StartDecode(void* pJbig2Context, | |
362 CFX_PrivateData* pPrivateData, | |
363 uint32_t width, | |
364 uint32_t height, | |
365 CPDF_StreamAcc* src_stream, | |
366 CPDF_StreamAcc* global_stream, | |
367 uint8_t* dest_buf, | |
368 uint32_t dest_pitch, | |
369 IFX_Pause* pPause) override; | |
370 FXCODEC_STATUS ContinueDecode(void* pJbig2Context, | |
371 IFX_Pause* pPause) override; | |
372 void DestroyJbig2Context(void* pJbig2Context) override; | |
373 }; | |
374 | |
375 struct DecodeData { | 23 struct DecodeData { |
376 public: | 24 public: |
377 DecodeData(unsigned char* src_data, OPJ_SIZE_T src_size) | 25 DecodeData(unsigned char* src_data, OPJ_SIZE_T src_size) |
378 : src_data(src_data), src_size(src_size), offset(0) {} | 26 : src_data(src_data), src_size(src_size), offset(0) {} |
379 unsigned char* src_data; | 27 unsigned char* src_data; |
380 OPJ_SIZE_T src_size; | 28 OPJ_SIZE_T src_size; |
381 OPJ_SIZE_T offset; | 29 OPJ_SIZE_T offset; |
382 }; | 30 }; |
383 | 31 |
384 void sycc420_to_rgb(opj_image_t* img); | 32 void sycc420_to_rgb(opj_image_t* img); |
385 | 33 |
386 /* Wrappers for C-style callbacks. */ | 34 /* Wrappers for C-style callbacks. */ |
387 OPJ_SIZE_T opj_read_from_memory(void* p_buffer, | 35 OPJ_SIZE_T opj_read_from_memory(void* p_buffer, |
388 OPJ_SIZE_T nb_bytes, | 36 OPJ_SIZE_T nb_bytes, |
389 void* p_user_data); | 37 void* p_user_data); |
390 OPJ_SIZE_T opj_write_from_memory(void* p_buffer, | 38 OPJ_SIZE_T opj_write_from_memory(void* p_buffer, |
391 OPJ_SIZE_T nb_bytes, | 39 OPJ_SIZE_T nb_bytes, |
392 void* p_user_data); | 40 void* p_user_data); |
393 OPJ_OFF_T opj_skip_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data); | 41 OPJ_OFF_T opj_skip_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data); |
394 OPJ_BOOL opj_seek_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data); | 42 OPJ_BOOL opj_seek_from_memory(OPJ_OFF_T nb_bytes, void* p_user_data); |
395 | 43 |
396 #endif // CORE_FXCODEC_CODEC_CODEC_INT_H_ | 44 #endif // CORE_FXCODEC_CODEC_CODEC_INT_H_ |
OLD | NEW |