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 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "codec_int.h" | 11 #include "core/src/fxcodec/codec/codec_int.h" |
12 #include "core/include/fpdfapi/fpdf_resource.h" | 12 #include "core/include/fpdfapi/fpdf_resource.h" |
13 #include "core/include/fxcodec/fx_codec.h" | 13 #include "core/include/fxcodec/fx_codec.h" |
14 #include "core/include/fxcrt/fx_safe_types.h" | 14 #include "core/include/fxcrt/fx_safe_types.h" |
15 #include "third_party/lcms2-2.6/include/lcms2.h" | 15 #include "third_party/lcms2-2.6/include/lcms2.h" |
16 #include "third_party/libopenjpeg20/openjpeg.h" | 16 #include "third_party/libopenjpeg20/openjpeg.h" |
17 | 17 |
18 static void fx_error_callback(const char* msg, void* client_data) { | 18 static void fx_error_callback(const char* msg, void* client_data) { |
19 (void)client_data; | 19 (void)client_data; |
20 } | 20 } |
21 static void fx_warning_callback(const char* msg, void* client_data) { | 21 static void fx_warning_callback(const char* msg, void* client_data) { |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 ++g; | 194 ++g; |
195 ++b; | 195 ++b; |
196 } | 196 } |
197 FX_Free(img->comps[0].data); | 197 FX_Free(img->comps[0].data); |
198 img->comps[0].data = d0; | 198 img->comps[0].data = d0; |
199 FX_Free(img->comps[1].data); | 199 FX_Free(img->comps[1].data); |
200 img->comps[1].data = d1; | 200 img->comps[1].data = d1; |
201 FX_Free(img->comps[2].data); | 201 FX_Free(img->comps[2].data); |
202 img->comps[2].data = d2; | 202 img->comps[2].data = d2; |
203 } | 203 } |
| 204 static bool sycc420_422_size_is_valid(opj_image_t* img) { |
| 205 return (img && img->comps[0].w != std::numeric_limits<OPJ_UINT32>::max() && |
| 206 (img->comps[0].w + 1) / 2 == img->comps[1].w && |
| 207 img->comps[1].w == img->comps[2].w && |
| 208 img->comps[1].h == img->comps[2].h); |
| 209 } |
| 210 static bool sycc420_size_is_valid(opj_image_t* img) { |
| 211 return (sycc420_422_size_is_valid(img) && |
| 212 img->comps[0].h != std::numeric_limits<OPJ_UINT32>::max() && |
| 213 (img->comps[0].h + 1) / 2 == img->comps[1].h); |
| 214 } |
| 215 static bool sycc422_size_is_valid(opj_image_t* img) { |
| 216 return (sycc420_422_size_is_valid(img) && img->comps[0].h == img->comps[1].h); |
| 217 } |
204 static void sycc422_to_rgb(opj_image_t* img) { | 218 static void sycc422_to_rgb(opj_image_t* img) { |
| 219 if (!sycc422_size_is_valid(img)) |
| 220 return; |
| 221 |
205 int prec = img->comps[0].prec; | 222 int prec = img->comps[0].prec; |
206 int offset = 1 << (prec - 1); | 223 int offset = 1 << (prec - 1); |
207 int upb = (1 << prec) - 1; | 224 int upb = (1 << prec) - 1; |
208 OPJ_UINT32 maxw = | 225 |
209 std::min(std::min(img->comps[0].w, img->comps[1].w), img->comps[2].w); | 226 OPJ_UINT32 maxw = img->comps[0].w; |
210 OPJ_UINT32 maxh = | 227 OPJ_UINT32 maxh = img->comps[0].h; |
211 std::min(std::min(img->comps[0].h, img->comps[1].h), img->comps[2].h); | |
212 FX_SAFE_SIZE_T max_size = maxw; | 228 FX_SAFE_SIZE_T max_size = maxw; |
213 max_size *= maxh; | 229 max_size *= maxh; |
214 if (!max_size.IsValid()) | 230 if (!max_size.IsValid()) |
215 return; | 231 return; |
216 | 232 |
217 const int* y = img->comps[0].data; | 233 const int* y = img->comps[0].data; |
218 const int* cb = img->comps[1].data; | 234 const int* cb = img->comps[1].data; |
219 const int* cr = img->comps[2].data; | 235 const int* cr = img->comps[2].data; |
220 int *d0, *d1, *d2, *r, *g, *b; | 236 int *d0, *d1, *d2, *r, *g, *b; |
221 d0 = r = FX_Alloc(int, max_size.ValueOrDie()); | 237 d0 = r = FX_Alloc(int, max_size.ValueOrDie()); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 img->comps[2].data = d2; | 271 img->comps[2].data = d2; |
256 img->comps[1].w = maxw; | 272 img->comps[1].w = maxw; |
257 img->comps[1].h = maxh; | 273 img->comps[1].h = maxh; |
258 img->comps[2].w = maxw; | 274 img->comps[2].w = maxw; |
259 img->comps[2].h = maxh; | 275 img->comps[2].h = maxh; |
260 img->comps[1].dx = img->comps[0].dx; | 276 img->comps[1].dx = img->comps[0].dx; |
261 img->comps[2].dx = img->comps[0].dx; | 277 img->comps[2].dx = img->comps[0].dx; |
262 img->comps[1].dy = img->comps[0].dy; | 278 img->comps[1].dy = img->comps[0].dy; |
263 img->comps[2].dy = img->comps[0].dy; | 279 img->comps[2].dy = img->comps[0].dy; |
264 } | 280 } |
265 static bool sycc420_size_is_valid(OPJ_UINT32 y, OPJ_UINT32 cbcr) { | |
266 if (!y || !cbcr) | |
267 return false; | |
268 | |
269 return (cbcr == y / 2) || ((y & 1) && (cbcr == y / 2 + 1)); | |
270 } | |
271 static bool sycc420_must_extend_cbcr(OPJ_UINT32 y, OPJ_UINT32 cbcr) { | 281 static bool sycc420_must_extend_cbcr(OPJ_UINT32 y, OPJ_UINT32 cbcr) { |
272 return (y & 1) && (cbcr == y / 2); | 282 return (y & 1) && (cbcr == y / 2); |
273 } | 283 } |
274 void sycc420_to_rgb(opj_image_t* img) { | 284 void sycc420_to_rgb(opj_image_t* img) { |
| 285 if (!sycc420_size_is_valid(img)) |
| 286 return; |
| 287 |
275 OPJ_UINT32 prec = img->comps[0].prec; | 288 OPJ_UINT32 prec = img->comps[0].prec; |
276 if (!prec) | 289 if (!prec) |
277 return; | 290 return; |
278 OPJ_UINT32 offset = 1 << (prec - 1); | 291 OPJ_UINT32 offset = 1 << (prec - 1); |
279 OPJ_UINT32 upb = (1 << prec) - 1; | 292 OPJ_UINT32 upb = (1 << prec) - 1; |
280 OPJ_UINT32 yw = img->comps[0].w; | 293 OPJ_UINT32 yw = img->comps[0].w; |
281 OPJ_UINT32 yh = img->comps[0].h; | 294 OPJ_UINT32 yh = img->comps[0].h; |
282 OPJ_UINT32 cbw = img->comps[1].w; | 295 OPJ_UINT32 cbw = img->comps[1].w; |
283 OPJ_UINT32 cbh = img->comps[1].h; | 296 OPJ_UINT32 cbh = img->comps[1].h; |
284 OPJ_UINT32 crw = img->comps[2].w; | 297 OPJ_UINT32 crw = img->comps[2].w; |
285 OPJ_UINT32 crh = img->comps[2].h; | |
286 if (cbw != crw || cbh != crh) | |
287 return; | |
288 if (!sycc420_size_is_valid(yw, cbw) || !sycc420_size_is_valid(yh, cbh)) | |
289 return; | |
290 bool extw = sycc420_must_extend_cbcr(yw, cbw); | 298 bool extw = sycc420_must_extend_cbcr(yw, cbw); |
291 bool exth = sycc420_must_extend_cbcr(yh, cbh); | 299 bool exth = sycc420_must_extend_cbcr(yh, cbh); |
292 FX_SAFE_DWORD safeSize = yw; | 300 FX_SAFE_DWORD safeSize = yw; |
293 safeSize *= yh; | 301 safeSize *= yh; |
294 if (!safeSize.IsValid()) | 302 if (!safeSize.IsValid()) |
295 return; | 303 return; |
296 int* r = FX_Alloc(int, safeSize.ValueOrDie()); | 304 int* r = FX_Alloc(int, safeSize.ValueOrDie()); |
297 int* g = FX_Alloc(int, safeSize.ValueOrDie()); | 305 int* g = FX_Alloc(int, safeSize.ValueOrDie()); |
298 int* b = FX_Alloc(int, safeSize.ValueOrDie()); | 306 int* b = FX_Alloc(int, safeSize.ValueOrDie()); |
299 int* d0 = r; | 307 int* d0 = r; |
(...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
852 *pPixel = (uint8_t)tmpPixel; | 860 *pPixel = (uint8_t)tmpPixel; |
853 } | 861 } |
854 } | 862 } |
855 } | 863 } |
856 } | 864 } |
857 } | 865 } |
858 return true; | 866 return true; |
859 } | 867 } |
860 | 868 |
861 CCodec_JpxModule::CCodec_JpxModule() {} | 869 CCodec_JpxModule::CCodec_JpxModule() {} |
862 CCodec_JpxModule::~CCodec_JpxModule() { | 870 CCodec_JpxModule::~CCodec_JpxModule() {} |
863 } | |
864 | 871 |
865 CJPX_Decoder* CCodec_JpxModule::CreateDecoder(const uint8_t* src_buf, | 872 CJPX_Decoder* CCodec_JpxModule::CreateDecoder(const uint8_t* src_buf, |
866 FX_DWORD src_size, | 873 FX_DWORD src_size, |
867 CPDF_ColorSpace* cs) { | 874 CPDF_ColorSpace* cs) { |
868 std::unique_ptr<CJPX_Decoder> decoder(new CJPX_Decoder(cs)); | 875 std::unique_ptr<CJPX_Decoder> decoder(new CJPX_Decoder(cs)); |
869 return decoder->Init(src_buf, src_size) ? decoder.release() : nullptr; | 876 return decoder->Init(src_buf, src_size) ? decoder.release() : nullptr; |
870 } | 877 } |
871 | 878 |
872 void CCodec_JpxModule::GetImageInfo(CJPX_Decoder* pDecoder, | 879 void CCodec_JpxModule::GetImageInfo(CJPX_Decoder* pDecoder, |
873 FX_DWORD* width, | 880 FX_DWORD* width, |
874 FX_DWORD* height, | 881 FX_DWORD* height, |
875 FX_DWORD* components) { | 882 FX_DWORD* components) { |
876 pDecoder->GetInfo(width, height, components); | 883 pDecoder->GetInfo(width, height, components); |
877 } | 884 } |
878 | 885 |
879 bool CCodec_JpxModule::Decode(CJPX_Decoder* pDecoder, | 886 bool CCodec_JpxModule::Decode(CJPX_Decoder* pDecoder, |
880 uint8_t* dest_data, | 887 uint8_t* dest_data, |
881 int pitch, | 888 int pitch, |
882 const std::vector<uint8_t>& offsets) { | 889 const std::vector<uint8_t>& offsets) { |
883 return pDecoder->Decode(dest_data, pitch, offsets); | 890 return pDecoder->Decode(dest_data, pitch, offsets); |
884 } | 891 } |
885 | 892 |
886 void CCodec_JpxModule::DestroyDecoder(CJPX_Decoder* pDecoder) { | 893 void CCodec_JpxModule::DestroyDecoder(CJPX_Decoder* pDecoder) { |
887 delete pDecoder; | 894 delete pDecoder; |
888 } | 895 } |
OLD | NEW |