blob: 0fdccb9e69e4c1a620ac7916608ed9dc18235723 [file] [log] [blame]
Dan Sinclair0bb13332017-03-30 16:12:02 -04001// Copyright 2017 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangffa67dd2019-06-17 20:27:52 +00007#ifndef CORE_FXCODEC_JPX_CJPX_DECODER_H_
8#define CORE_FXCODEC_JPX_CJPX_DECODER_H_
Dan Sinclair0bb13332017-03-30 16:12:02 -04009
Ryan Harrison808b52a2017-09-08 10:10:26 -040010#include <memory>
Dan Sinclair0bb13332017-03-30 16:12:02 -040011
Dan Sinclairaee0db02017-09-21 16:53:58 -040012#include "core/fxcrt/unowned_ptr.h"
Tom Sepez00ba8bb2018-06-27 18:02:30 +000013#include "third_party/base/span.h"
Felix Kauselmann8d72a472019-02-13 23:44:56 +000014
Lei Zhang20707712019-06-17 20:29:12 +000015#if defined(USE_SYSTEM_LIBOPENJPEG2)
Felix Kauselmann8d72a472019-02-13 23:44:56 +000016#include <openjpeg.h>
17#else
Dan Sinclair0bb13332017-03-30 16:12:02 -040018#include "third_party/libopenjpeg20/openjpeg.h"
Felix Kauselmann8d72a472019-02-13 23:44:56 +000019#endif
Dan Sinclair0bb13332017-03-30 16:12:02 -040020
Lei Zhang20707712019-06-17 20:29:12 +000021namespace fxcodec {
22
23struct DecodeData;
24
Dan Sinclair0bb13332017-03-30 16:12:02 -040025class CJPX_Decoder {
26 public:
Tom Sepez7ade67c2019-05-31 21:34:51 +000027 enum ColorSpaceOption {
28 kNoColorSpace,
29 kNormalColorSpace,
30 kIndexedColorSpace
31 };
32
Lei Zhangde679a22020-01-29 21:57:15 +000033 struct JpxImageInfo {
34 uint32_t width;
35 uint32_t height;
36 uint32_t components;
37 };
38
Lei Zhang20707712019-06-17 20:29:12 +000039 static void Sycc420ToRgbForTesting(opj_image_t* img);
40
Tom Sepez7ade67c2019-05-31 21:34:51 +000041 explicit CJPX_Decoder(ColorSpaceOption option);
Dan Sinclair0bb13332017-03-30 16:12:02 -040042 ~CJPX_Decoder();
43
Tom Sepez00ba8bb2018-06-27 18:02:30 +000044 bool Init(pdfium::span<const uint8_t> src_data);
Lei Zhangde679a22020-01-29 21:57:15 +000045 JpxImageInfo GetInfo() const;
Nicolas Penafa7983e2018-11-16 21:54:51 +000046 bool StartDecode();
Dan Sinclair0bb13332017-03-30 16:12:02 -040047 bool Decode(uint8_t* dest_buf,
Ryan Harrisonb6e01172018-06-20 13:43:04 +000048 uint32_t pitch,
Tom Sepez237c4dd2019-11-20 23:52:03 +000049 pdfium::span<const uint8_t> offsets);
Dan Sinclair0bb13332017-03-30 16:12:02 -040050
51 private:
Tom Sepez7ade67c2019-05-31 21:34:51 +000052 const ColorSpaceOption m_ColorSpaceOption;
Tom Sepez00ba8bb2018-06-27 18:02:30 +000053 pdfium::span<const uint8_t> m_SrcData;
54 UnownedPtr<opj_image_t> m_Image;
55 UnownedPtr<opj_codec_t> m_Codec;
Ryan Harrison808b52a2017-09-08 10:10:26 -040056 std::unique_ptr<DecodeData> m_DecodeData;
Tom Sepez00ba8bb2018-06-27 18:02:30 +000057 UnownedPtr<opj_stream_t> m_Stream;
Ryan Harrison808b52a2017-09-08 10:10:26 -040058 opj_dparameters_t m_Parameters;
Dan Sinclair0bb13332017-03-30 16:12:02 -040059};
60
Lei Zhang20707712019-06-17 20:29:12 +000061} // namespace fxcodec
62
63using fxcodec::CJPX_Decoder;
64
Lei Zhangffa67dd2019-06-17 20:27:52 +000065#endif // CORE_FXCODEC_JPX_CJPX_DECODER_H_