Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
tiff_decoder.cpp
Go to the documentation of this file.
1// Copyright 2014 The PDFium Authors
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
7#include "core/fxcodec/tiff/tiff_decoder.h"
8
9#include <memory>
10#include <utility>
11
12#include "core/fxcodec/cfx_codec_memory.h"
13#include "core/fxcodec/fx_codec.h"
14#include "core/fxcodec/fx_codec_def.h"
15#include "core/fxcrt/check.h"
16#include "core/fxcrt/compiler_specific.h"
17#include "core/fxcrt/fx_memcpy_wrappers.h"
18#include "core/fxcrt/fx_safe_types.h"
19#include "core/fxcrt/fx_stream.h"
20#include "core/fxcrt/fx_system.h"
21#include "core/fxcrt/notreached.h"
22#include "core/fxcrt/numerics/safe_conversions.h"
23#include "core/fxcrt/retain_ptr.h"
24#include "core/fxcrt/span.h"
25#include "core/fxcrt/span_util.h"
26#include "core/fxge/dib/cfx_dibitmap.h"
27#include "core/fxge/dib/fx_dib.h"
28
29extern "C" {
30#if defined(USE_SYSTEM_LIBTIFF)
31#include <tiffio.h>
32#else
33#include "third_party/libtiff/tiffio.h"
34#endif
35} // extern C
36
37namespace {
38
39// For use with std::unique_ptr<TIFF>.
40struct TiffDeleter {
41 inline void operator()(TIFF* context) { TIFFClose(context); }
42};
43
44} // namespace
45
46class CTiffContext final : public ProgressiveDecoderIface::Context {
47 public:
48 CTiffContext() = default;
49 ~CTiffContext() override = default;
50
52 bool LoadFrameInfo(int32_t frame,
53 int32_t* width,
54 int32_t* height,
55 int32_t* comps,
56 int32_t* bpc,
57 CFX_DIBAttribute* pAttribute);
58 bool Decode(RetainPtr<CFX_DIBitmap> bitmap);
59
60 RetainPtr<IFX_SeekableReadStream> io_in() const { return m_io_in; }
61 uint32_t offset() const { return m_offset; }
62 void set_offset(uint32_t offset) { m_offset = offset; }
63
64 private:
66 uint32_t m_offset = 0;
67 std::unique_ptr<TIFF, TiffDeleter> m_tif_ctx;
68};
69
70void* _TIFFcalloc(tmsize_t nmemb, tmsize_t siz) {
71 return FXMEM_DefaultCalloc(nmemb, siz);
72}
73
74void* _TIFFmalloc(tmsize_t size) {
75 return FXMEM_DefaultAlloc(size);
76}
77
78void _TIFFfree(void* ptr) {
79 if (ptr)
81}
82
83void* _TIFFrealloc(void* ptr, tmsize_t size) {
84 return FXMEM_DefaultRealloc(ptr, size);
85}
86
87void _TIFFmemset(void* ptr, int val, tmsize_t size) {
88 UNSAFE_TODO(FXSYS_memset(ptr, val, static_cast<size_t>(size)));
89}
90
91void _TIFFmemcpy(void* des, const void* src, tmsize_t size) {
92 UNSAFE_TODO(FXSYS_memcpy(des, src, static_cast<size_t>(size)));
93}
94
95int _TIFFmemcmp(const void* ptr1, const void* ptr2, tmsize_t size) {
96 return UNSAFE_TODO(memcmp(ptr1, ptr2, static_cast<size_t>(size)));
97}
98
99namespace {
100
101tsize_t tiff_read(thandle_t context, tdata_t buf, tsize_t length) {
102 CTiffContext* pTiffContext = reinterpret_cast<CTiffContext*>(context);
103 FX_SAFE_UINT32 increment = pTiffContext->offset();
104 increment += length;
105 if (!increment.IsValid())
106 return 0;
107
108 FX_FILESIZE offset = pTiffContext->offset();
109 // SAFETY: required from caller.
110 if (!pTiffContext->io_in()->ReadBlockAtOffset(
111 UNSAFE_BUFFERS(pdfium::make_span(static_cast<uint8_t*>(buf),
112 static_cast<size_t>(length))),
113 offset)) {
114 return 0;
115 }
116 pTiffContext->set_offset(increment.ValueOrDie());
117 if (offset + length > pTiffContext->io_in()->GetSize()) {
118 return pdfium::checked_cast<tsize_t>(pTiffContext->io_in()->GetSize() -
119 offset);
120 }
121 return length;
122}
123
124tsize_t tiff_write(thandle_t context, tdata_t buf, tsize_t length) {
126}
127
128toff_t tiff_seek(thandle_t context, toff_t offset, int whence) {
129 CTiffContext* pTiffContext = reinterpret_cast<CTiffContext*>(context);
130 FX_SAFE_FILESIZE safe_offset = offset;
131 if (!safe_offset.IsValid())
132 return static_cast<toff_t>(-1);
133 FX_FILESIZE file_offset = safe_offset.ValueOrDie();
134
135 switch (whence) {
136 case 0: {
137 if (file_offset > pTiffContext->io_in()->GetSize())
138 return static_cast<toff_t>(-1);
139 pTiffContext->set_offset(pdfium::checked_cast<uint32_t>(file_offset));
140 return pTiffContext->offset();
141 }
142 case 1: {
143 FX_SAFE_UINT32 new_increment = pTiffContext->offset();
144 new_increment += file_offset;
145 if (!new_increment.IsValid())
146 return static_cast<toff_t>(-1);
147 pTiffContext->set_offset(new_increment.ValueOrDie());
148 return pTiffContext->offset();
149 }
150 case 2: {
151 if (pTiffContext->io_in()->GetSize() < file_offset)
152 return static_cast<toff_t>(-1);
153 pTiffContext->set_offset(pdfium::checked_cast<uint32_t>(
154 pTiffContext->io_in()->GetSize() - file_offset));
155 return pTiffContext->offset();
156 }
157 default:
158 return static_cast<toff_t>(-1);
159 }
160}
161
162int tiff_close(thandle_t context) {
163 return 0;
164}
165
166toff_t tiff_get_size(thandle_t context) {
167 CTiffContext* pTiffContext = reinterpret_cast<CTiffContext*>(context);
168 return static_cast<toff_t>(pTiffContext->io_in()->GetSize());
169}
170
171int tiff_map(thandle_t context, tdata_t*, toff_t*) {
172 return 0;
173}
174
175void tiff_unmap(thandle_t context, tdata_t, toff_t) {}
176
177} // namespace
178
179bool CTiffContext::InitDecoder(
180 const RetainPtr<IFX_SeekableReadStream>& file_ptr) {
181 m_io_in = file_ptr;
182 m_tif_ctx.reset(TIFFClientOpen(
183 /*name=*/"Tiff Image", /*mode=*/"r", /*clientdata=*/this, tiff_read,
184 tiff_write, tiff_seek, tiff_close, tiff_get_size, tiff_map, tiff_unmap));
185 return !!m_tif_ctx;
186}
187
188bool CTiffContext::LoadFrameInfo(int32_t frame,
189 int32_t* width,
190 int32_t* height,
191 int32_t* comps,
192 int32_t* bpc,
193 CFX_DIBAttribute* pAttribute) {
194 if (!TIFFSetDirectory(m_tif_ctx.get(), (uint16_t)frame))
195 return false;
196
197 uint32_t tif_width = 0;
198 uint32_t tif_height = 0;
199 uint16_t tif_comps = 0;
200 uint16_t tif_bpc = 0;
201 uint32_t tif_rps = 0;
202 TIFFGetField(m_tif_ctx.get(), TIFFTAG_IMAGEWIDTH, &tif_width);
203 TIFFGetField(m_tif_ctx.get(), TIFFTAG_IMAGELENGTH, &tif_height);
204 TIFFGetField(m_tif_ctx.get(), TIFFTAG_SAMPLESPERPIXEL, &tif_comps);
205 TIFFGetField(m_tif_ctx.get(), TIFFTAG_BITSPERSAMPLE, &tif_bpc);
206 TIFFGetField(m_tif_ctx.get(), TIFFTAG_ROWSPERSTRIP, &tif_rps);
207
208 uint16_t tif_resunit = 0;
209 if (TIFFGetField(m_tif_ctx.get(), TIFFTAG_RESOLUTIONUNIT, &tif_resunit)) {
210 pAttribute->m_wDPIUnit =
211 static_cast<CFX_DIBAttribute::ResUnit>(tif_resunit - 1);
212 } else {
213 pAttribute->m_wDPIUnit = CFX_DIBAttribute::kResUnitInch;
214 }
215
216 float tif_xdpi = 0.0f;
217 TIFFGetField(m_tif_ctx.get(), TIFFTAG_XRESOLUTION, &tif_xdpi);
218 if (tif_xdpi)
219 pAttribute->m_nXDPI = static_cast<int32_t>(tif_xdpi + 0.5f);
220
221 float tif_ydpi = 0.0f;
222 TIFFGetField(m_tif_ctx.get(), TIFFTAG_YRESOLUTION, &tif_ydpi);
223 if (tif_ydpi)
224 pAttribute->m_nYDPI = static_cast<int32_t>(tif_ydpi + 0.5f);
225
226 FX_SAFE_INT32 checked_width = tif_width;
227 FX_SAFE_INT32 checked_height = tif_height;
228 if (!checked_width.IsValid() || !checked_height.IsValid())
229 return false;
230
231 *width = checked_width.ValueOrDie();
232 *height = checked_height.ValueOrDie();
233 *comps = tif_comps;
234 *bpc = tif_bpc;
235 if (tif_rps > tif_height) {
236 tif_rps = tif_height;
237 TIFFSetField(m_tif_ctx.get(), TIFFTAG_ROWSPERSTRIP, tif_rps);
238 }
239 return true;
240}
241
242bool CTiffContext::Decode(RetainPtr<CFX_DIBitmap> bitmap) {
243 // TODO(crbug.com/355630556): Consider adding support for
244 // `FXDIB_Format::kBgraPremul`
245 CHECK_EQ(bitmap->GetFormat(), FXDIB_Format::kBgra);
246 const uint32_t img_width = bitmap->GetWidth();
247 const uint32_t img_height = bitmap->GetHeight();
248 uint32_t width = 0;
249 uint32_t height = 0;
250 TIFFGetField(m_tif_ctx.get(), TIFFTAG_IMAGEWIDTH, &width);
251 TIFFGetField(m_tif_ctx.get(), TIFFTAG_IMAGELENGTH, &height);
252 if (img_width != width || img_height != height) {
253 return false;
254 }
255
256 uint16_t rotation = ORIENTATION_TOPLEFT;
257 TIFFGetField(m_tif_ctx.get(), TIFFTAG_ORIENTATION, &rotation);
258 uint32_t* data =
259 fxcrt::reinterpret_span<uint32_t>(bitmap->GetWritableBuffer()).data();
260 if (!TIFFReadRGBAImageOriented(m_tif_ctx.get(), img_width, img_height, data,
261 rotation, 1)) {
262 return false;
263 }
264
265 for (uint32_t row = 0; row < img_height; row++) {
266 auto row_span = bitmap->GetWritableScanlineAs<FX_BGRA_STRUCT<uint8_t>>(row);
267 for (auto& pixel : row_span) {
268 std::swap(pixel.blue, pixel.red);
269 }
270 }
271 return true;
272}
273
274namespace fxcodec {
275
276// static
277std::unique_ptr<ProgressiveDecoderIface::Context> TiffDecoder::CreateDecoder(
278 const RetainPtr<IFX_SeekableReadStream>& file_ptr) {
279 auto pDecoder = std::make_unique<CTiffContext>();
280 if (!pDecoder->InitDecoder(file_ptr))
281 return nullptr;
282
283 return pDecoder;
284}
285
286// static
288 int32_t frame,
289 int32_t* width,
290 int32_t* height,
291 int32_t* comps,
292 int32_t* bpc,
293 CFX_DIBAttribute* pAttribute) {
294 DCHECK(pAttribute);
295
296 auto* ctx = static_cast<CTiffContext*>(pContext);
297 return ctx->LoadFrameInfo(frame, width, height, comps, bpc, pAttribute);
298}
299
300// static
302 RetainPtr<CFX_DIBitmap> bitmap) {
303 auto* ctx = static_cast<CTiffContext*>(pContext);
304 return ctx->Decode(std::move(bitmap));
305}
306
307} // namespace fxcodec
#define DCHECK
Definition check.h:33
#define CHECK_EQ(x, y)
Definition check_op.h:10
RetainPtr< IFX_SeekableReadStream > io_in() const
uint32_t offset() const
bool InitDecoder(const RetainPtr< IFX_SeekableReadStream > &file_ptr)
~CTiffContext() override=default
CTiffContext()=default
bool LoadFrameInfo(int32_t frame, int32_t *width, int32_t *height, int32_t *comps, int32_t *bpc, CFX_DIBAttribute *pAttribute)
bool Decode(RetainPtr< CFX_DIBitmap > bitmap)
void set_offset(uint32_t offset)
static bool Decode(ProgressiveDecoderIface::Context *ctx, RetainPtr< CFX_DIBitmap > bitmap)
static bool LoadFrameInfo(ProgressiveDecoderIface::Context *ctx, int32_t frame, int32_t *width, int32_t *height, int32_t *comps, int32_t *bpc, CFX_DIBAttribute *pAttribute)
#define UNSAFE_BUFFERS(...)
#define UNSAFE_TODO(...)
FXDIB_Format
Definition fx_dib.h:21
UNSAFE_BUFFER_USAGE void * FXSYS_memset(void *ptr1, int val, size_t len)
UNSAFE_BUFFER_USAGE void * FXSYS_memcpy(void *ptr1, const void *ptr2, size_t len)
void FXMEM_DefaultFree(void *pointer)
Definition fx_memory.cpp:78
pdfium::CheckedNumeric< FX_FILESIZE > FX_SAFE_FILESIZE
pdfium::CheckedNumeric< uint32_t > FX_SAFE_UINT32
pdfium::CheckedNumeric< int32_t > FX_SAFE_INT32
#define FX_FILESIZE
Definition fx_types.h:19
#define NOTREACHED_NORETURN()
Definition notreached.h:22
void _TIFFfree(void *ptr)
void * _TIFFrealloc(void *ptr, tmsize_t size)
int _TIFFmemcmp(const void *ptr1, const void *ptr2, tmsize_t size)
void _TIFFmemset(void *ptr, int val, tmsize_t size)
void * _TIFFcalloc(tmsize_t nmemb, tmsize_t siz)
void * _TIFFmalloc(tmsize_t size)
void _TIFFmemcpy(void *des, const void *src, tmsize_t size)