Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(310)

Side by Side Diff: core/fxcodec/codec/fx_codec_tiff.cpp

Issue 2053573003: Clean up fx_codec_tiff.cpp. (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: Move code into anonymous namespace. Remove dead code. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "core/fxcodec/codec/codec_int.h" 7 #include "core/fxcodec/codec/codec_int.h"
8 #include "core/fxcodec/include/fx_codec.h" 8 #include "core/fxcodec/include/fx_codec.h"
9 #include "core/fxge/include/fx_dib.h" 9 #include "core/fxge/include/fx_dib.h"
10 10
11 extern "C" { 11 extern "C" {
12 #include "third_party/libtiff/tiffiop.h" 12 #include "third_party/libtiff/tiffiop.h"
13 } 13 }
14 14
15 void* IccLib_CreateTransform_sRGB(const unsigned char* pProfileData, 15 void* IccLib_CreateTransform_sRGB(const unsigned char* pProfileData,
16 unsigned int dwProfileSize, 16 unsigned int dwProfileSize,
17 int nComponents, 17 int nComponents,
18 int intent, 18 int intent,
19 uint32_t dwSrcFormat = Icc_FORMAT_DEFAULT); 19 uint32_t dwSrcFormat = Icc_FORMAT_DEFAULT);
20 void IccLib_TranslateImage(void* pTransform, 20 void IccLib_TranslateImage(void* pTransform,
21 unsigned char* pDest, 21 unsigned char* pDest,
22 const unsigned char* pSrc, 22 const unsigned char* pSrc,
23 int pixels); 23 int pixels);
24 void IccLib_DestroyTransform(void* pTransform); 24 void IccLib_DestroyTransform(void* pTransform);
25
25 class CCodec_TiffContext { 26 class CCodec_TiffContext {
26 public: 27 public:
27 CCodec_TiffContext(); 28 CCodec_TiffContext();
28 ~CCodec_TiffContext(); 29 ~CCodec_TiffContext();
29 30
30 FX_BOOL InitDecoder(IFX_FileRead* file_ptr); 31 FX_BOOL InitDecoder(IFX_FileRead* file_ptr);
31 void GetFrames(int32_t& frames); 32 void GetFrames(int32_t& frames);
32 FX_BOOL LoadFrameInfo(int32_t frame, 33 FX_BOOL LoadFrameInfo(int32_t frame,
33 uint32_t& width, 34 uint32_t& width,
34 uint32_t& height, 35 uint32_t& height,
35 uint32_t& comps, 36 uint32_t& comps,
36 uint32_t& bpc, 37 uint32_t& bpc,
37 CFX_DIBAttribute* pAttribute); 38 CFX_DIBAttribute* pAttribute);
38 FX_BOOL Decode(CFX_DIBitmap* pDIBitmap); 39 FX_BOOL Decode(CFX_DIBitmap* pDIBitmap);
39 40
40 union { 41 union {
41 IFX_FileRead* in; 42 IFX_FileRead* in;
42 IFX_FileStream* out; 43 IFX_FileStream* out;
43 } io; 44 } io;
44 45
45 uint32_t offset; 46 uint32_t offset;
46 47
47 TIFF* tif_ctx; 48 TIFF* tif_ctx;
48 void* icc_ctx; 49 void* icc_ctx;
49 int32_t frame_num; 50 int32_t frame_num;
50 int32_t frame_cur; 51 int32_t frame_cur;
51 FX_BOOL isDecoder; 52 FX_BOOL isDecoder;
Tom Sepez 2016/06/09 17:53:16 Agree this is always true and can be removed.
Lei Zhang 2016/06/09 22:00:38 Acknowledged.
52 53
53 private: 54 private:
54 FX_BOOL isSupport(CFX_DIBitmap* pDIBitmap); 55 FX_BOOL isSupport(CFX_DIBitmap* pDIBitmap);
55 void SetPalette(CFX_DIBitmap* pDIBitmap, uint16_t bps); 56 void SetPalette(CFX_DIBitmap* pDIBitmap, uint16_t bps);
56 FX_BOOL Decode1bppRGB(CFX_DIBitmap* pDIBitmap, 57 FX_BOOL Decode1bppRGB(CFX_DIBitmap* pDIBitmap,
57 int32_t height, 58 int32_t height,
58 int32_t width, 59 int32_t width,
59 uint16_t bps, 60 uint16_t bps,
60 uint16_t spp); 61 uint16_t spp);
61 FX_BOOL Decode8bppRGB(CFX_DIBitmap* pDIBitmap, 62 FX_BOOL Decode8bppRGB(CFX_DIBitmap* pDIBitmap,
62 int32_t height, 63 int32_t height,
63 int32_t width, 64 int32_t width,
64 uint16_t bps, 65 uint16_t bps,
65 uint16_t spp); 66 uint16_t spp);
66 FX_BOOL Decode24bppRGB(CFX_DIBitmap* pDIBitmap, 67 FX_BOOL Decode24bppRGB(CFX_DIBitmap* pDIBitmap,
67 int32_t height, 68 int32_t height,
68 int32_t width, 69 int32_t width,
69 uint16_t bps, 70 uint16_t bps,
70 uint16_t spp); 71 uint16_t spp);
71 }; 72 };
72 CCodec_TiffContext::CCodec_TiffContext() { 73
73 offset = 0; 74 void* _TIFFmalloc(tmsize_t size) {
Tom Sepez 2016/06/09 17:53:16 nit: _CAPS is reserved for the pre-processor. Fix
Lei Zhang 2016/06/09 22:00:38 We'll have to argue with libtiff on these. See ext
74 frame_num = 0; 75 return FXMEM_DefaultAlloc(size, 0);
75 frame_cur = 0;
76 io.in = nullptr;
77 tif_ctx = nullptr;
78 icc_ctx = nullptr;
79 isDecoder = TRUE;
80 } 76 }
81 CCodec_TiffContext::~CCodec_TiffContext() { 77
82 if (icc_ctx) { 78 void _TIFFfree(void* ptr) {
83 IccLib_DestroyTransform(icc_ctx); 79 FXMEM_DefaultFree(ptr, 0);
84 icc_ctx = nullptr;
85 }
86 if (tif_ctx) {
87 TIFFClose(tif_ctx);
88 }
89 } 80 }
90 static tsize_t _tiff_read(thandle_t context, tdata_t buf, tsize_t length) { 81
82 void* _TIFFrealloc(void* ptr, tmsize_t size) {
83 return FXMEM_DefaultRealloc(ptr, size, 0);
84 }
85
86 void _TIFFmemset(void* ptr, int val, tmsize_t size) {
87 FXSYS_memset(ptr, val, (size_t)size);
88 }
89
90 void _TIFFmemcpy(void* des, const void* src, tmsize_t size) {
91 FXSYS_memcpy(des, src, (size_t)size);
92 }
93
94 int _TIFFmemcmp(const void* ptr1, const void* ptr2, tmsize_t size) {
95 return FXSYS_memcmp(ptr1, ptr2, (size_t)size);
96 }
97
98 TIFFErrorHandler _TIFFwarningHandler = nullptr;
99 TIFFErrorHandler _TIFFerrorHandler = nullptr;
100
101 namespace {
102
103 tsize_t tiff_read(thandle_t context, tdata_t buf, tsize_t length) {
91 CCodec_TiffContext* pTiffContext = (CCodec_TiffContext*)context; 104 CCodec_TiffContext* pTiffContext = (CCodec_TiffContext*)context;
92 FX_BOOL ret = FALSE; 105 FX_BOOL ret = FALSE;
93 if (pTiffContext->isDecoder) { 106 if (pTiffContext->isDecoder) {
94 ret = pTiffContext->io.in->ReadBlock(buf, pTiffContext->offset, length); 107 ret = pTiffContext->io.in->ReadBlock(buf, pTiffContext->offset, length);
95 } else { 108 } else {
96 ret = pTiffContext->io.out->ReadBlock(buf, pTiffContext->offset, length); 109 ret = pTiffContext->io.out->ReadBlock(buf, pTiffContext->offset, length);
97 } 110 }
98 if (!ret) { 111 if (!ret) {
99 return 0; 112 return 0;
100 } 113 }
101 pTiffContext->offset += (uint32_t)length; 114 pTiffContext->offset += (uint32_t)length;
102 return length; 115 return length;
103 } 116 }
104 static tsize_t _tiff_write(thandle_t context, tdata_t buf, tsize_t length) { 117
118 tsize_t tiff_write(thandle_t context, tdata_t buf, tsize_t length) {
105 CCodec_TiffContext* pTiffContext = (CCodec_TiffContext*)context; 119 CCodec_TiffContext* pTiffContext = (CCodec_TiffContext*)context;
106 ASSERT(!pTiffContext->isDecoder); 120 ASSERT(!pTiffContext->isDecoder);
107 if (!pTiffContext->io.out->WriteBlock(buf, pTiffContext->offset, length)) { 121 if (!pTiffContext->io.out->WriteBlock(buf, pTiffContext->offset, length)) {
108 return 0; 122 return 0;
109 } 123 }
110 pTiffContext->offset += (uint32_t)length; 124 pTiffContext->offset += (uint32_t)length;
111 return length; 125 return length;
112 } 126 }
113 static toff_t _tiff_seek(thandle_t context, toff_t offset, int whence) { 127
128 toff_t tiff_seek(thandle_t context, toff_t offset, int whence) {
114 CCodec_TiffContext* pTiffContext = (CCodec_TiffContext*)context; 129 CCodec_TiffContext* pTiffContext = (CCodec_TiffContext*)context;
115 switch (whence) { 130 switch (whence) {
116 case 0: 131 case 0:
117 pTiffContext->offset = (uint32_t)offset; 132 pTiffContext->offset = (uint32_t)offset;
118 break; 133 break;
119 case 1: 134 case 1:
120 pTiffContext->offset += (uint32_t)offset; 135 pTiffContext->offset += (uint32_t)offset;
121 break; 136 break;
122 case 2: 137 case 2:
123 if (pTiffContext->isDecoder) { 138 if (pTiffContext->isDecoder) {
(...skipping 11 matching lines...) Expand all
135 } 150 }
136 break; 151 break;
137 default: 152 default:
138 return static_cast<toff_t>(-1); 153 return static_cast<toff_t>(-1);
139 } 154 }
140 ASSERT(pTiffContext->isDecoder ? (pTiffContext->offset <= 155 ASSERT(pTiffContext->isDecoder ? (pTiffContext->offset <=
141 (uint32_t)pTiffContext->io.in->GetSize()) 156 (uint32_t)pTiffContext->io.in->GetSize())
142 : TRUE); 157 : TRUE);
143 return pTiffContext->offset; 158 return pTiffContext->offset;
144 } 159 }
145 static int _tiff_close(thandle_t context) { 160
161 int tiff_close(thandle_t context) {
146 return 0; 162 return 0;
147 } 163 }
148 static toff_t _tiff_get_size(thandle_t context) { 164
165 toff_t tiff_get_size(thandle_t context) {
149 CCodec_TiffContext* pTiffContext = (CCodec_TiffContext*)context; 166 CCodec_TiffContext* pTiffContext = (CCodec_TiffContext*)context;
150 return pTiffContext->isDecoder ? (toff_t)pTiffContext->io.in->GetSize() 167 return pTiffContext->isDecoder ? (toff_t)pTiffContext->io.in->GetSize()
151 : (toff_t)pTiffContext->io.out->GetSize(); 168 : (toff_t)pTiffContext->io.out->GetSize();
152 } 169 }
153 static int _tiff_map(thandle_t context, tdata_t*, toff_t*) { 170
171 int tiff_map(thandle_t context, tdata_t*, toff_t*) {
154 return 0; 172 return 0;
155 } 173 }
156 static void _tiff_unmap(thandle_t context, tdata_t, toff_t) {} 174
157 TIFF* _tiff_open(void* context, const char* mode) { 175 void tiff_unmap(thandle_t context, tdata_t, toff_t) {}
158 TIFF* tif = TIFFClientOpen("Tiff Image", mode, (thandle_t)context, _tiff_read, 176
159 _tiff_write, _tiff_seek, _tiff_close, 177 TIFF* tiff_open(void* context, const char* mode) {
160 _tiff_get_size, _tiff_map, _tiff_unmap); 178 TIFF* tif = TIFFClientOpen("Tiff Image", mode, (thandle_t)context, tiff_read,
179 tiff_write, tiff_seek, tiff_close, tiff_get_size,
180 tiff_map, tiff_unmap);
161 if (tif) { 181 if (tif) {
162 tif->tif_fd = (int)(intptr_t)context; 182 tif->tif_fd = (int)(intptr_t)context;
163 } 183 }
164 return tif; 184 return tif;
165 } 185 }
166 void* _TIFFmalloc(tmsize_t size) {
167 return FXMEM_DefaultAlloc(size, 0);
168 }
169 void _TIFFfree(void* ptr) {
170 FXMEM_DefaultFree(ptr, 0);
171 }
172 void* _TIFFrealloc(void* ptr, tmsize_t size) {
173 return FXMEM_DefaultRealloc(ptr, size, 0);
174 }
175 void _TIFFmemset(void* ptr, int val, tmsize_t size) {
176 FXSYS_memset(ptr, val, (size_t)size);
177 }
178 void _TIFFmemcpy(void* des, const void* src, tmsize_t size) {
179 FXSYS_memcpy(des, src, (size_t)size);
180 }
181 int _TIFFmemcmp(const void* ptr1, const void* ptr2, tmsize_t size) {
182 return FXSYS_memcmp(ptr1, ptr2, (size_t)size);
183 }
184
185 TIFFErrorHandler _TIFFwarningHandler = nullptr;
186 TIFFErrorHandler _TIFFerrorHandler = nullptr;
187
188 int TIFFCmyk2Rgb(thandle_t context,
189 uint8 c,
190 uint8 m,
191 uint8 y,
192 uint8 k,
193 uint8* r,
194 uint8* g,
195 uint8* b) {
196 if (!context)
197 return 0;
198
199 CCodec_TiffContext* p = (CCodec_TiffContext*)context;
200 if (p->icc_ctx) {
201 unsigned char cmyk[4], bgr[3];
202 cmyk[0] = c, cmyk[1] = m, cmyk[2] = y, cmyk[3] = k;
203 IccLib_TranslateImage(p->icc_ctx, bgr, cmyk, 1);
204 *r = bgr[2], *g = bgr[1], *b = bgr[0];
205 } else {
206 AdobeCMYK_to_sRGB1(c, m, y, k, *r, *g, *b);
207 }
208 return 1;
209 }
210 FX_BOOL CCodec_TiffContext::InitDecoder(IFX_FileRead* file_ptr) {
211 io.in = file_ptr;
212 return !!_tiff_open(this, "r");
213 }
214 void CCodec_TiffContext::GetFrames(int32_t& frames) {
215 frames = frame_num = TIFFNumberOfDirectories(tif_ctx);
216 }
217 #define TIFF_EXIF_GETINFO(key, T, tag) \
218 { \
219 T val = (T)0; \
220 TIFFGetField(tif_ctx, tag, &val); \
221 if (val) { \
222 (key) = FX_Alloc(uint8_t, sizeof(T)); \
223 if ((key)) { \
224 T* ptr = (T*)(key); \
225 *ptr = val; \
226 pExif->m_TagVal.SetAt(tag, (key)); \
227 } \
228 } \
229 } \
230 (key) = nullptr;
231 #define TIFF_EXIF_GETSTRINGINFO(key, tag) \
232 { \
233 uint32_t size = 0; \
234 uint8_t* buf = nullptr; \
235 TIFFGetField(tif_ctx, tag, &size, &buf); \
236 if (size && buf) { \
237 (key) = FX_Alloc(uint8_t, size); \
238 if ((key)) { \
239 FXSYS_memcpy((key), buf, size); \
240 pExif->m_TagVal.SetAt(tag, (key)); \
241 } \
242 } \
243 } \
244 (key) = nullptr;
245
246 namespace {
247 186
248 template <class T> 187 template <class T>
249 FX_BOOL Tiff_Exif_GetInfo(TIFF* tif_ctx, ttag_t tag, CFX_DIBAttribute* pAttr) { 188 FX_BOOL Tiff_Exif_GetInfo(TIFF* tif_ctx, ttag_t tag, CFX_DIBAttribute* pAttr) {
250 T val = 0; 189 T val = 0;
251 TIFFGetField(tif_ctx, tag, &val); 190 TIFFGetField(tif_ctx, tag, &val);
252 if (!val) 191 if (!val)
253 return FALSE; 192 return FALSE;
254 T* ptr = FX_Alloc(T, 1); 193 T* ptr = FX_Alloc(T, 1);
255 *ptr = val; 194 *ptr = val;
256 pAttr->m_Exif[tag] = (void*)ptr; 195 pAttr->m_Exif[tag] = (void*)ptr;
257 return TRUE; 196 return TRUE;
258 } 197 }
198
259 void Tiff_Exif_GetStringInfo(TIFF* tif_ctx, 199 void Tiff_Exif_GetStringInfo(TIFF* tif_ctx,
260 ttag_t tag, 200 ttag_t tag,
261 CFX_DIBAttribute* pAttr) { 201 CFX_DIBAttribute* pAttr) {
262 FX_CHAR* buf = nullptr; 202 FX_CHAR* buf = nullptr;
263 TIFFGetField(tif_ctx, tag, &buf); 203 TIFFGetField(tif_ctx, tag, &buf);
264 if (!buf) 204 if (!buf)
265 return; 205 return;
266 FX_STRSIZE size = FXSYS_strlen(buf); 206 FX_STRSIZE size = FXSYS_strlen(buf);
267 uint8_t* ptr = FX_Alloc(uint8_t, size + 1); 207 uint8_t* ptr = FX_Alloc(uint8_t, size + 1);
268 FXSYS_memcpy(ptr, buf, size); 208 FXSYS_memcpy(ptr, buf, size);
269 ptr[size] = 0; 209 ptr[size] = 0;
270 pAttr->m_Exif[tag] = ptr; 210 pAttr->m_Exif[tag] = ptr;
271 } 211 }
272 212
213 void TiffBGRA2RGBA(uint8_t* pBuf, int32_t pixel, int32_t spp) {
214 for (int32_t n = 0; n < pixel; n++) {
215 uint8_t tmp = pBuf[0];
216 pBuf[0] = pBuf[2];
217 pBuf[2] = tmp;
218 pBuf += spp;
219 }
220 }
221
273 } // namespace 222 } // namespace
274 223
224 CCodec_TiffContext::CCodec_TiffContext()
225 : offset(0),
226 tif_ctx(nullptr),
227 icc_ctx(nullptr),
228 frame_num(0),
229 frame_cur(0),
230 isDecoder(TRUE) {
231 io.in = nullptr;
232 }
233
234 CCodec_TiffContext::~CCodec_TiffContext() {
235 if (icc_ctx) {
236 IccLib_DestroyTransform(icc_ctx);
237 icc_ctx = nullptr;
238 }
239 if (tif_ctx) {
240 TIFFClose(tif_ctx);
241 }
242 }
243
244 FX_BOOL CCodec_TiffContext::InitDecoder(IFX_FileRead* file_ptr) {
245 io.in = file_ptr;
246 return !!tiff_open(this, "r");
247 }
248 void CCodec_TiffContext::GetFrames(int32_t& frames) {
249 frames = frame_num = TIFFNumberOfDirectories(tif_ctx);
250 }
251
275 FX_BOOL CCodec_TiffContext::LoadFrameInfo(int32_t frame, 252 FX_BOOL CCodec_TiffContext::LoadFrameInfo(int32_t frame,
276 uint32_t& width, 253 uint32_t& width,
277 uint32_t& height, 254 uint32_t& height,
278 uint32_t& comps, 255 uint32_t& comps,
279 uint32_t& bpc, 256 uint32_t& bpc,
280 CFX_DIBAttribute* pAttribute) { 257 CFX_DIBAttribute* pAttribute) {
281 if (!TIFFSetDirectory(tif_ctx, (uint16)frame)) { 258 if (!TIFFSetDirectory(tif_ctx, (uint16)frame)) {
282 return FALSE; 259 return FALSE;
283 } 260 }
284 uint16_t tif_cs; 261 uint16_t tif_cs;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 Tiff_Exif_GetStringInfo(tif_ctx, TIFFTAG_IMAGEDESCRIPTION, pAttribute); 293 Tiff_Exif_GetStringInfo(tif_ctx, TIFFTAG_IMAGEDESCRIPTION, pAttribute);
317 Tiff_Exif_GetStringInfo(tif_ctx, TIFFTAG_MAKE, pAttribute); 294 Tiff_Exif_GetStringInfo(tif_ctx, TIFFTAG_MAKE, pAttribute);
318 Tiff_Exif_GetStringInfo(tif_ctx, TIFFTAG_MODEL, pAttribute); 295 Tiff_Exif_GetStringInfo(tif_ctx, TIFFTAG_MODEL, pAttribute);
319 } 296 }
320 bpc = tif_bpc; 297 bpc = tif_bpc;
321 if (tif_rps > height) { 298 if (tif_rps > height) {
322 TIFFSetField(tif_ctx, TIFFTAG_ROWSPERSTRIP, tif_rps = height); 299 TIFFSetField(tif_ctx, TIFFTAG_ROWSPERSTRIP, tif_rps = height);
323 } 300 }
324 return TRUE; 301 return TRUE;
325 } 302 }
326 void _TiffBGRA2RGBA(uint8_t* pBuf, int32_t pixel, int32_t spp) { 303
327 for (int32_t n = 0; n < pixel; n++) {
328 uint8_t tmp = pBuf[0];
329 pBuf[0] = pBuf[2];
330 pBuf[2] = tmp;
331 pBuf += spp;
332 }
333 }
334 FX_BOOL CCodec_TiffContext::isSupport(CFX_DIBitmap* pDIBitmap) { 304 FX_BOOL CCodec_TiffContext::isSupport(CFX_DIBitmap* pDIBitmap) {
335 if (TIFFIsTiled(tif_ctx)) { 305 if (TIFFIsTiled(tif_ctx)) {
336 return FALSE; 306 return FALSE;
337 } 307 }
338 uint16_t photometric; 308 uint16_t photometric;
339 if (!TIFFGetField(tif_ctx, TIFFTAG_PHOTOMETRIC, &photometric)) { 309 if (!TIFFGetField(tif_ctx, TIFFTAG_PHOTOMETRIC, &photometric)) {
340 return FALSE; 310 return FALSE;
341 } 311 }
342 switch (pDIBitmap->GetBPP()) { 312 switch (pDIBitmap->GetBPP()) {
343 case 1: 313 case 1:
(...skipping 12 matching lines...) Expand all
356 } 326 }
357 uint16_t planarconfig; 327 uint16_t planarconfig;
358 if (!TIFFGetFieldDefaulted(tif_ctx, TIFFTAG_PLANARCONFIG, &planarconfig)) { 328 if (!TIFFGetFieldDefaulted(tif_ctx, TIFFTAG_PLANARCONFIG, &planarconfig)) {
359 return FALSE; 329 return FALSE;
360 } 330 }
361 if (planarconfig == PLANARCONFIG_SEPARATE) { 331 if (planarconfig == PLANARCONFIG_SEPARATE) {
362 return FALSE; 332 return FALSE;
363 } 333 }
364 return TRUE; 334 return TRUE;
365 } 335 }
336
366 void CCodec_TiffContext::SetPalette(CFX_DIBitmap* pDIBitmap, uint16_t bps) { 337 void CCodec_TiffContext::SetPalette(CFX_DIBitmap* pDIBitmap, uint16_t bps) {
367 uint16_t *red_orig, *green_orig, *blue_orig; 338 uint16_t *red_orig, *green_orig, *blue_orig;
368 TIFFGetField(tif_ctx, TIFFTAG_COLORMAP, &red_orig, &green_orig, &blue_orig); 339 TIFFGetField(tif_ctx, TIFFTAG_COLORMAP, &red_orig, &green_orig, &blue_orig);
369 for (int32_t i = (1L << bps) - 1; i >= 0; i--) { 340 for (int32_t i = (1L << bps) - 1; i >= 0; i--) {
370 #define CVT(x) ((uint16_t)((x) >> 8)) 341 #define CVT(x) ((uint16_t)((x) >> 8))
371 red_orig[i] = CVT(red_orig[i]); 342 red_orig[i] = CVT(red_orig[i]);
372 green_orig[i] = CVT(green_orig[i]); 343 green_orig[i] = CVT(green_orig[i]);
373 blue_orig[i] = CVT(blue_orig[i]); 344 blue_orig[i] = CVT(blue_orig[i]);
374 #undef CVT 345 #undef CVT
375 } 346 }
376 int32_t len = 1 << bps; 347 int32_t len = 1 << bps;
377 for (int32_t index = 0; index < len; index++) { 348 for (int32_t index = 0; index < len; index++) {
378 uint32_t r = red_orig[index] & 0xFF; 349 uint32_t r = red_orig[index] & 0xFF;
379 uint32_t g = green_orig[index] & 0xFF; 350 uint32_t g = green_orig[index] & 0xFF;
380 uint32_t b = blue_orig[index] & 0xFF; 351 uint32_t b = blue_orig[index] & 0xFF;
381 uint32_t color = (uint32_t)b | ((uint32_t)g << 8) | ((uint32_t)r << 16) | 352 uint32_t color = (uint32_t)b | ((uint32_t)g << 8) | ((uint32_t)r << 16) |
382 (((uint32)0xffL) << 24); 353 (((uint32)0xffL) << 24);
383 pDIBitmap->SetPaletteEntry(index, color); 354 pDIBitmap->SetPaletteEntry(index, color);
384 } 355 }
385 } 356 }
357
386 FX_BOOL CCodec_TiffContext::Decode1bppRGB(CFX_DIBitmap* pDIBitmap, 358 FX_BOOL CCodec_TiffContext::Decode1bppRGB(CFX_DIBitmap* pDIBitmap,
387 int32_t height, 359 int32_t height,
388 int32_t width, 360 int32_t width,
389 uint16_t bps, 361 uint16_t bps,
390 uint16_t spp) { 362 uint16_t spp) {
391 if (pDIBitmap->GetBPP() != 1 || spp != 1 || bps != 1 || 363 if (pDIBitmap->GetBPP() != 1 || spp != 1 || bps != 1 ||
392 !isSupport(pDIBitmap)) { 364 !isSupport(pDIBitmap)) {
393 return FALSE; 365 return FALSE;
394 } 366 }
395 SetPalette(pDIBitmap, bps); 367 SetPalette(pDIBitmap, bps);
396 int32_t size = (int32_t)TIFFScanlineSize(tif_ctx); 368 int32_t size = (int32_t)TIFFScanlineSize(tif_ctx);
397 uint8_t* buf = (uint8_t*)_TIFFmalloc(size); 369 uint8_t* buf = (uint8_t*)_TIFFmalloc(size);
398 if (!buf) { 370 if (!buf) {
399 TIFFError(TIFFFileName(tif_ctx), "No space for scanline buffer"); 371 TIFFError(TIFFFileName(tif_ctx), "No space for scanline buffer");
400 return FALSE; 372 return FALSE;
401 } 373 }
402 uint8_t* bitMapbuffer = (uint8_t*)pDIBitmap->GetBuffer(); 374 uint8_t* bitMapbuffer = (uint8_t*)pDIBitmap->GetBuffer();
403 uint32_t pitch = pDIBitmap->GetPitch(); 375 uint32_t pitch = pDIBitmap->GetPitch();
404 for (int32_t row = 0; row < height; row++) { 376 for (int32_t row = 0; row < height; row++) {
405 TIFFReadScanline(tif_ctx, buf, row, 0); 377 TIFFReadScanline(tif_ctx, buf, row, 0);
406 for (int32_t j = 0; j < size; j++) { 378 for (int32_t j = 0; j < size; j++) {
407 bitMapbuffer[row * pitch + j] = buf[j]; 379 bitMapbuffer[row * pitch + j] = buf[j];
408 } 380 }
409 } 381 }
410 _TIFFfree(buf); 382 _TIFFfree(buf);
411 return TRUE; 383 return TRUE;
412 } 384 }
385
413 FX_BOOL CCodec_TiffContext::Decode8bppRGB(CFX_DIBitmap* pDIBitmap, 386 FX_BOOL CCodec_TiffContext::Decode8bppRGB(CFX_DIBitmap* pDIBitmap,
414 int32_t height, 387 int32_t height,
415 int32_t width, 388 int32_t width,
416 uint16_t bps, 389 uint16_t bps,
417 uint16_t spp) { 390 uint16_t spp) {
418 if (pDIBitmap->GetBPP() != 8 || spp != 1 || (bps != 4 && bps != 8) || 391 if (pDIBitmap->GetBPP() != 8 || spp != 1 || (bps != 4 && bps != 8) ||
419 !isSupport(pDIBitmap)) { 392 !isSupport(pDIBitmap)) {
420 return FALSE; 393 return FALSE;
421 } 394 }
422 SetPalette(pDIBitmap, bps); 395 SetPalette(pDIBitmap, bps);
(...skipping 15 matching lines...) Expand all
438 break; 411 break;
439 case 8: 412 case 8:
440 bitMapbuffer[row * pitch + j] = buf[j]; 413 bitMapbuffer[row * pitch + j] = buf[j];
441 break; 414 break;
442 } 415 }
443 } 416 }
444 } 417 }
445 _TIFFfree(buf); 418 _TIFFfree(buf);
446 return TRUE; 419 return TRUE;
447 } 420 }
421
448 FX_BOOL CCodec_TiffContext::Decode24bppRGB(CFX_DIBitmap* pDIBitmap, 422 FX_BOOL CCodec_TiffContext::Decode24bppRGB(CFX_DIBitmap* pDIBitmap,
449 int32_t height, 423 int32_t height,
450 int32_t width, 424 int32_t width,
451 uint16_t bps, 425 uint16_t bps,
452 uint16_t spp) { 426 uint16_t spp) {
453 if (pDIBitmap->GetBPP() != 24 || !isSupport(pDIBitmap)) { 427 if (pDIBitmap->GetBPP() != 24 || !isSupport(pDIBitmap)) {
454 return FALSE; 428 return FALSE;
455 } 429 }
456 int32_t size = (int32_t)TIFFScanlineSize(tif_ctx); 430 int32_t size = (int32_t)TIFFScanlineSize(tif_ctx);
457 uint8_t* buf = (uint8_t*)_TIFFmalloc(size); 431 uint8_t* buf = (uint8_t*)_TIFFmalloc(size);
458 if (!buf) { 432 if (!buf) {
459 TIFFError(TIFFFileName(tif_ctx), "No space for scanline buffer"); 433 TIFFError(TIFFFileName(tif_ctx), "No space for scanline buffer");
460 return FALSE; 434 return FALSE;
461 } 435 }
462 uint8_t* bitMapbuffer = (uint8_t*)pDIBitmap->GetBuffer(); 436 uint8_t* bitMapbuffer = (uint8_t*)pDIBitmap->GetBuffer();
463 uint32_t pitch = pDIBitmap->GetPitch(); 437 uint32_t pitch = pDIBitmap->GetPitch();
464 for (int32_t row = 0; row < height; row++) { 438 for (int32_t row = 0; row < height; row++) {
465 TIFFReadScanline(tif_ctx, buf, row, 0); 439 TIFFReadScanline(tif_ctx, buf, row, 0);
466 for (int32_t j = 0; j < size - 2; j += 3) { 440 for (int32_t j = 0; j < size - 2; j += 3) {
467 bitMapbuffer[row * pitch + j + 0] = buf[j + 2]; 441 bitMapbuffer[row * pitch + j + 0] = buf[j + 2];
468 bitMapbuffer[row * pitch + j + 1] = buf[j + 1]; 442 bitMapbuffer[row * pitch + j + 1] = buf[j + 1];
469 bitMapbuffer[row * pitch + j + 2] = buf[j + 0]; 443 bitMapbuffer[row * pitch + j + 2] = buf[j + 0];
470 } 444 }
471 } 445 }
472 _TIFFfree(buf); 446 _TIFFfree(buf);
473 return TRUE; 447 return TRUE;
474 } 448 }
449
475 FX_BOOL CCodec_TiffContext::Decode(CFX_DIBitmap* pDIBitmap) { 450 FX_BOOL CCodec_TiffContext::Decode(CFX_DIBitmap* pDIBitmap) {
476 uint32_t img_wid = pDIBitmap->GetWidth(); 451 uint32_t img_wid = pDIBitmap->GetWidth();
477 uint32_t img_hei = pDIBitmap->GetHeight(); 452 uint32_t img_hei = pDIBitmap->GetHeight();
478 uint32_t width = 0; 453 uint32_t width = 0;
479 uint32_t height = 0; 454 uint32_t height = 0;
480 TIFFGetField(tif_ctx, TIFFTAG_IMAGEWIDTH, &width); 455 TIFFGetField(tif_ctx, TIFFTAG_IMAGEWIDTH, &width);
481 TIFFGetField(tif_ctx, TIFFTAG_IMAGELENGTH, &height); 456 TIFFGetField(tif_ctx, TIFFTAG_IMAGELENGTH, &height);
482 if (img_wid != width || img_hei != height) { 457 if (img_wid != width || img_hei != height) {
483 return FALSE; 458 return FALSE;
484 } 459 }
485 if (pDIBitmap->GetBPP() == 32) { 460 if (pDIBitmap->GetBPP() == 32) {
486 uint16_t rotation = ORIENTATION_TOPLEFT; 461 uint16_t rotation = ORIENTATION_TOPLEFT;
487 TIFFGetField(tif_ctx, TIFFTAG_ORIENTATION, &rotation); 462 TIFFGetField(tif_ctx, TIFFTAG_ORIENTATION, &rotation);
488 if (TIFFReadRGBAImageOriented(tif_ctx, img_wid, img_hei, 463 if (TIFFReadRGBAImageOriented(tif_ctx, img_wid, img_hei,
489 (uint32*)pDIBitmap->GetBuffer(), rotation, 464 (uint32*)pDIBitmap->GetBuffer(), rotation,
490 1)) { 465 1)) {
491 for (uint32_t row = 0; row < img_hei; row++) { 466 for (uint32_t row = 0; row < img_hei; row++) {
492 uint8_t* row_buf = (uint8_t*)pDIBitmap->GetScanline(row); 467 uint8_t* row_buf = (uint8_t*)pDIBitmap->GetScanline(row);
493 _TiffBGRA2RGBA(row_buf, img_wid, 4); 468 TiffBGRA2RGBA(row_buf, img_wid, 4);
494 } 469 }
495 return TRUE; 470 return TRUE;
496 } 471 }
497 } 472 }
498 uint16_t spp, bps; 473 uint16_t spp, bps;
499 TIFFGetField(tif_ctx, TIFFTAG_SAMPLESPERPIXEL, &spp); 474 TIFFGetField(tif_ctx, TIFFTAG_SAMPLESPERPIXEL, &spp);
500 TIFFGetField(tif_ctx, TIFFTAG_BITSPERSAMPLE, &bps); 475 TIFFGetField(tif_ctx, TIFFTAG_BITSPERSAMPLE, &bps);
501 uint32_t bpp = bps * spp; 476 uint32_t bpp = bps * spp;
502 if (bpp == 1) { 477 if (bpp == 1) {
503 return Decode1bppRGB(pDIBitmap, height, width, bps, spp); 478 return Decode1bppRGB(pDIBitmap, height, width, bps, spp);
(...skipping 29 matching lines...) Expand all
533 } 508 }
534 509
535 FX_BOOL CCodec_TiffModule::Decode(CCodec_TiffContext* ctx, 510 FX_BOOL CCodec_TiffModule::Decode(CCodec_TiffContext* ctx,
536 class CFX_DIBitmap* pDIBitmap) { 511 class CFX_DIBitmap* pDIBitmap) {
537 return ctx->Decode(pDIBitmap); 512 return ctx->Decode(pDIBitmap);
538 } 513 }
539 514
540 void CCodec_TiffModule::DestroyDecoder(CCodec_TiffContext* ctx) { 515 void CCodec_TiffModule::DestroyDecoder(CCodec_TiffContext* ctx) {
541 delete ctx; 516 delete ctx;
542 } 517 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698