diff options
author | Caolán McNamara <caolanm@redhat.com> | 2022-05-20 10:00:14 +0100 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2022-05-21 11:52:56 +0200 |
commit | e59c9be85f8d00dc64c80e21dee91304fe232be2 (patch) | |
tree | 03982fa7ba387ad357d2c16e4667a2786c649044 | |
parent | 22b50f1937de67e4ad9e692d6964aa5b8d33af7a (diff) |
drop old tiff filter
Change-Id: I3bfeae4751cb9173fbe2efdbda531a9a1a491c8e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134695
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
-rw-r--r-- | vcl/Library_vcl.mk | 2 | ||||
-rw-r--r-- | vcl/source/filter/itiff/ccidecom.cxx | 1100 | ||||
-rw-r--r-- | vcl/source/filter/itiff/itiff.cxx | 1695 | ||||
-rw-r--r-- | vcl/source/filter/itiff/lzwdecom.cxx | 215 |
4 files changed, 0 insertions, 3012 deletions
diff --git a/vcl/Library_vcl.mk b/vcl/Library_vcl.mk index 5828084eb5a9..6910927bfde0 100644 --- a/vcl/Library_vcl.mk +++ b/vcl/Library_vcl.mk @@ -455,9 +455,7 @@ $(eval $(call gb_Library_add_exception_objects,vcl,\ vcl/source/filter/ipdf/pdfdocument \ vcl/source/filter/iras/iras \ vcl/source/filter/itga/itga \ - vcl/source/filter/itiff/ccidecom \ vcl/source/filter/itiff/itiff \ - vcl/source/filter/itiff/lzwdecom \ vcl/source/filter/ixbm/xbmread \ vcl/source/filter/ixpm/xpmread \ vcl/source/filter/jpeg/Exif \ diff --git a/vcl/source/filter/itiff/ccidecom.cxx b/vcl/source/filter/itiff/ccidecom.cxx deleted file mode 100644 index a82041afa445..000000000000 --- a/vcl/source/filter/itiff/ccidecom.cxx +++ /dev/null @@ -1,1100 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * This file incorporates work covered by the following license notice: - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed - * with this work for additional information regarding copyright - * ownership. The ASF licenses this file to you under the Apache - * License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.apache.org/licenses/LICENSE-2.0 . - */ - - -#include "ccidecom.hxx" -#include <tools/stream.hxx> - -//=============================== Huffman tables ======================== - -//---------------------------- White-Run ------------------------------ - -#define CCIWhiteTableSize 105 - -const CCIHuffmanTableEntry CCIWhiteTable[CCIWhiteTableSize]={ - { 0, 0x0035, 8 }, - { 1, 0x0007, 6 }, - { 2, 0x0007, 4 }, - { 3, 0x0008, 4 }, - { 4, 0x000b, 4 }, - { 5, 0x000c, 4 }, - { 6, 0x000e, 4 }, - { 7, 0x000f, 4 }, - { 8, 0x0013, 5 }, - { 9, 0x0014, 5 }, - { 10, 0x0007, 5 }, - { 11, 0x0008, 5 }, - { 12, 0x0008, 6 }, - { 13, 0x0003, 6 }, - { 14, 0x0034, 6 }, - { 15, 0x0035, 6 }, - { 16, 0x002a, 6 }, - { 17, 0x002b, 6 }, - { 18, 0x0027, 7 }, - { 19, 0x000c, 7 }, - { 20, 0x0008, 7 }, - { 21, 0x0017, 7 }, - { 22, 0x0003, 7 }, - { 23, 0x0004, 7 }, - { 24, 0x0028, 7 }, - { 25, 0x002b, 7 }, - { 26, 0x0013, 7 }, - { 27, 0x0024, 7 }, - { 28, 0x0018, 7 }, - { 29, 0x0002, 8 }, - { 30, 0x0003, 8 }, - { 31, 0x001a, 8 }, - { 32, 0x001b, 8 }, - { 33, 0x0012, 8 }, - { 34, 0x0013, 8 }, - { 35, 0x0014, 8 }, - { 36, 0x0015, 8 }, - { 37, 0x0016, 8 }, - { 38, 0x0017, 8 }, - { 39, 0x0028, 8 }, - { 40, 0x0029, 8 }, - { 41, 0x002a, 8 }, - { 42, 0x002b, 8 }, - { 43, 0x002c, 8 }, - { 44, 0x002d, 8 }, - { 45, 0x0004, 8 }, - { 46, 0x0005, 8 }, - { 47, 0x000a, 8 }, - { 48, 0x000b, 8 }, - { 49, 0x0052, 8 }, - { 50, 0x0053, 8 }, - { 51, 0x0054, 8 }, - { 52, 0x0055, 8 }, - { 53, 0x0024, 8 }, - { 54, 0x0025, 8 }, - { 55, 0x0058, 8 }, - { 56, 0x0059, 8 }, - { 57, 0x005a, 8 }, - { 58, 0x005b, 8 }, - { 59, 0x004a, 8 }, - { 60, 0x004b, 8 }, - { 61, 0x0032, 8 }, - { 62, 0x0033, 8 }, - { 63, 0x0034, 8 }, - { 64, 0x001b, 5 }, - { 128, 0x0012, 5 }, - { 192, 0x0017, 6 }, - { 256, 0x0037, 7 }, - { 320, 0x0036, 8 }, - { 384, 0x0037, 8 }, - { 448, 0x0064, 8 }, - { 512, 0x0065, 8 }, - { 576, 0x0068, 8 }, - { 640, 0x0067, 8 }, - { 704, 0x00cc, 9 }, - { 768, 0x00cd, 9 }, - { 832, 0x00d2, 9 }, - { 896, 0x00d3, 9 }, - { 960, 0x00d4, 9 }, - { 1024, 0x00d5, 9 }, - { 1088, 0x00d6, 9 }, - { 1152, 0x00d7, 9 }, - { 1216, 0x00d8, 9 }, - { 1280, 0x00d9, 9 }, - { 1344, 0x00da, 9 }, - { 1408, 0x00db, 9 }, - { 1472, 0x0098, 9 }, - { 1536, 0x0099, 9 }, - { 1600, 0x009a, 9 }, - { 1664, 0x0018, 6 }, - { 1728, 0x009b, 9 }, - { 1792, 0x0008, 11 }, - { 1856, 0x000c, 11 }, - { 1920, 0x000d, 11 }, - { 1984, 0x0012, 12 }, - { 2048, 0x0013, 12 }, - { 2112, 0x0014, 12 }, - { 2176, 0x0015, 12 }, - { 2240, 0x0016, 12 }, - { 2304, 0x0017, 12 }, - { 2368, 0x001c, 12 }, - { 2432, 0x001d, 12 }, - { 2496, 0x001e, 12 }, - { 2560, 0x001f, 12 }, - { 9999, 0x0001, 12 } // EOL -}; - -//---------------------------- Black-Run ------------------------------ - -#define CCIBlackTableSize 105 - -const CCIHuffmanTableEntry CCIBlackTable[CCIBlackTableSize]={ - { 0, 0x0037, 10 }, - { 1, 0x0002, 3 }, - { 2, 0x0003, 2 }, - { 3, 0x0002, 2 }, - { 4, 0x0003, 3 }, - { 5, 0x0003, 4 }, - { 6, 0x0002, 4 }, - { 7, 0x0003, 5 }, - { 8, 0x0005, 6 }, - { 9, 0x0004, 6 }, - { 10, 0x0004, 7 }, - { 11, 0x0005, 7 }, - { 12, 0x0007, 7 }, - { 13, 0x0004, 8 }, - { 14, 0x0007, 8 }, - { 15, 0x0018, 9 }, - { 16, 0x0017, 10 }, - { 17, 0x0018, 10 }, - { 18, 0x0008, 10 }, - { 19, 0x0067, 11 }, - { 20, 0x0068, 11 }, - { 21, 0x006c, 11 }, - { 22, 0x0037, 11 }, - { 23, 0x0028, 11 }, - { 24, 0x0017, 11 }, - { 25, 0x0018, 11 }, - { 26, 0x00ca, 12 }, - { 27, 0x00cb, 12 }, - { 28, 0x00cc, 12 }, - { 29, 0x00cd, 12 }, - { 30, 0x0068, 12 }, - { 31, 0x0069, 12 }, - { 32, 0x006a, 12 }, - { 33, 0x006b, 12 }, - { 34, 0x00d2, 12 }, - { 35, 0x00d3, 12 }, - { 36, 0x00d4, 12 }, - { 37, 0x00d5, 12 }, - { 38, 0x00d6, 12 }, - { 39, 0x00d7, 12 }, - { 40, 0x006c, 12 }, - { 41, 0x006d, 12 }, - { 42, 0x00da, 12 }, - { 43, 0x00db, 12 }, - { 44, 0x0054, 12 }, - { 45, 0x0055, 12 }, - { 46, 0x0056, 12 }, - { 47, 0x0057, 12 }, - { 48, 0x0064, 12 }, - { 49, 0x0065, 12 }, - { 50, 0x0052, 12 }, - { 51, 0x0053, 12 }, - { 52, 0x0024, 12 }, - { 53, 0x0037, 12 }, - { 54, 0x0038, 12 }, - { 55, 0x0027, 12 }, - { 56, 0x0028, 12 }, - { 57, 0x0058, 12 }, - { 58, 0x0059, 12 }, - { 59, 0x002b, 12 }, - { 60, 0x002c, 12 }, - { 61, 0x005a, 12 }, - { 62, 0x0066, 12 }, - { 63, 0x0067, 12 }, - { 64, 0x000f, 10 }, - { 128, 0x00c8, 12 }, - { 192, 0x00c9, 12 }, - { 256, 0x005b, 12 }, - { 320, 0x0033, 12 }, - { 384, 0x0034, 12 }, - { 448, 0x0035, 12 }, - { 512, 0x006c, 13 }, - { 576, 0x006d, 13 }, - { 640, 0x004a, 13 }, - { 704, 0x004b, 13 }, - { 768, 0x004c, 13 }, - { 832, 0x004d, 13 }, - { 896, 0x0072, 13 }, - { 960, 0x0073, 13 }, - { 1024, 0x0074, 13 }, - { 1088, 0x0075, 13 }, - { 1152, 0x0076, 13 }, - { 1216, 0x0077, 13 }, - { 1280, 0x0052, 13 }, - { 1344, 0x0053, 13 }, - { 1408, 0x0054, 13 }, - { 1472, 0x0055, 13 }, - { 1536, 0x005a, 13 }, - { 1600, 0x005b, 13 }, - { 1664, 0x0064, 13 }, - { 1728, 0x0065, 13 }, - { 1792, 0x0008, 11 }, - { 1856, 0x000c, 11 }, - { 1920, 0x000d, 11 }, - { 1984, 0x0012, 12 }, - { 2048, 0x0013, 12 }, - { 2112, 0x0014, 12 }, - { 2176, 0x0015, 12 }, - { 2240, 0x0016, 12 }, - { 2304, 0x0017, 12 }, - { 2368, 0x001c, 12 }, - { 2432, 0x001d, 12 }, - { 2496, 0x001e, 12 }, - { 2560, 0x001f, 12 }, - { 9999, 0x0001, 12 } // EOL -}; - - -//---------------------------- 2D-Mode -------------------------------- - -#define CCI2DMODE_UNCOMP 0 -#define CCI2DMODE_PASS 1 -#define CCI2DMODE_HORZ 2 -#define CCI2DMODE_VERT_L3 3 -#define CCI2DMODE_VERT_L2 4 -#define CCI2DMODE_VERT_L1 5 -#define CCI2DMODE_VERT_0 6 -#define CCI2DMODE_VERT_R1 7 -#define CCI2DMODE_VERT_R2 8 -#define CCI2DMODE_VERT_R3 9 - -#define CCI2DModeTableSize 10 - -const CCIHuffmanTableEntry CCI2DModeTable[CCI2DModeTableSize]={ - { CCI2DMODE_UNCOMP , 0x000f, 10 }, - { CCI2DMODE_PASS , 0x0001, 4 }, - { CCI2DMODE_HORZ , 0x0001, 3 }, - { CCI2DMODE_VERT_L3, 0x0002, 7 }, - { CCI2DMODE_VERT_L2, 0x0002, 6 }, - { CCI2DMODE_VERT_L1, 0x0002, 3 }, - { CCI2DMODE_VERT_0 , 0x0001, 1 }, - { CCI2DMODE_VERT_R1, 0x0003, 3 }, - { CCI2DMODE_VERT_R2, 0x0003, 6 }, - { CCI2DMODE_VERT_R3, 0x0003, 7 } -}; - - -//-------------------------- 2D-Uncompressed-Mode ---------------------- - -#define CCIUNCOMP_0White_1Black 0 -#define CCIUNCOMP_1White_1Black 1 -#define CCIUNCOMP_2White_1Black 2 -#define CCIUNCOMP_3White_1Black 3 -#define CCIUNCOMP_4White_1Black 4 -#define CCIUNCOMP_5White 5 -#define CCIUNCOMP_0White_End 6 -#define CCIUNCOMP_1White_End 7 -#define CCIUNCOMP_2White_End 8 -#define CCIUNCOMP_3White_End 9 -#define CCIUNCOMP_4White_End 10 - -#define CCIUncompTableSize 11 - -const CCIHuffmanTableEntry CCIUncompTable[CCIUncompTableSize]={ - { CCIUNCOMP_0White_1Black, 0x0001, 1 }, - { CCIUNCOMP_1White_1Black, 0x0001, 2 }, - { CCIUNCOMP_2White_1Black, 0x0001, 3 }, - { CCIUNCOMP_3White_1Black, 0x0001, 4 }, - { CCIUNCOMP_4White_1Black, 0x0001, 5 }, - { CCIUNCOMP_5White , 0x0001, 6 }, - { CCIUNCOMP_0White_End , 0x0001, 7 }, - { CCIUNCOMP_1White_End , 0x0001, 8 }, - { CCIUNCOMP_2White_End , 0x0001, 9 }, - { CCIUNCOMP_3White_End , 0x0001, 10 }, - { CCIUNCOMP_4White_End , 0x0001, 11 } -}; - - -//================== backup of the Huffman tables ============================ -// To make sure that the Huffman tables do not contain errors they were entered -// from two different sources (Phew) and compared. -// Since an error could creep into the source code while maintaining it -// (e.g. an accidental key press in the editor) the tables are listed twice -// and are compared during runtime. (If the comparison fails CCIDcompressor -// throws an error) The whole thing may appear insane, but an error within the -// tables would otherwise be really hard to discover and it's very unlikely that -// one or more sample files run through all codes. - -const CCIHuffmanTableEntry CCIWhiteTableSave[CCIWhiteTableSize]={ - { 0, 0x0035, 8 }, - { 1, 0x0007, 6 }, - { 2, 0x0007, 4 }, - { 3, 0x0008, 4 }, - { 4, 0x000b, 4 }, - { 5, 0x000c, 4 }, - { 6, 0x000e, 4 }, - { 7, 0x000f, 4 }, - { 8, 0x0013, 5 }, - { 9, 0x0014, 5 }, - { 10, 0x0007, 5 }, - { 11, 0x0008, 5 }, - { 12, 0x0008, 6 }, - { 13, 0x0003, 6 }, - { 14, 0x0034, 6 }, - { 15, 0x0035, 6 }, - { 16, 0x002a, 6 }, - { 17, 0x002b, 6 }, - { 18, 0x0027, 7 }, - { 19, 0x000c, 7 }, - { 20, 0x0008, 7 }, - { 21, 0x0017, 7 }, - { 22, 0x0003, 7 }, - { 23, 0x0004, 7 }, - { 24, 0x0028, 7 }, - { 25, 0x002b, 7 }, - { 26, 0x0013, 7 }, - { 27, 0x0024, 7 }, - { 28, 0x0018, 7 }, - { 29, 0x0002, 8 }, - { 30, 0x0003, 8 }, - { 31, 0x001a, 8 }, - { 32, 0x001b, 8 }, - { 33, 0x0012, 8 }, - { 34, 0x0013, 8 }, - { 35, 0x0014, 8 }, - { 36, 0x0015, 8 }, - { 37, 0x0016, 8 }, - { 38, 0x0017, 8 }, - { 39, 0x0028, 8 }, - { 40, 0x0029, 8 }, - { 41, 0x002a, 8 }, - { 42, 0x002b, 8 }, - { 43, 0x002c, 8 }, - { 44, 0x002d, 8 }, - { 45, 0x0004, 8 }, - { 46, 0x0005, 8 }, - { 47, 0x000a, 8 }, - { 48, 0x000b, 8 }, - { 49, 0x0052, 8 }, - { 50, 0x0053, 8 }, - { 51, 0x0054, 8 }, - { 52, 0x0055, 8 }, - { 53, 0x0024, 8 }, - { 54, 0x0025, 8 }, - { 55, 0x0058, 8 }, - { 56, 0x0059, 8 }, - { 57, 0x005a, 8 }, - { 58, 0x005b, 8 }, - { 59, 0x004a, 8 }, - { 60, 0x004b, 8 }, - { 61, 0x0032, 8 }, - { 62, 0x0033, 8 }, - { 63, 0x0034, 8 }, - { 64, 0x001b, 5 }, - { 128, 0x0012, 5 }, - { 192, 0x0017, 6 }, - { 256, 0x0037, 7 }, - { 320, 0x0036, 8 }, - { 384, 0x0037, 8 }, - { 448, 0x0064, 8 }, - { 512, 0x0065, 8 }, - { 576, 0x0068, 8 }, - { 640, 0x0067, 8 }, - { 704, 0x00cc, 9 }, - { 768, 0x00cd, 9 }, - { 832, 0x00d2, 9 }, - { 896, 0x00d3, 9 }, - { 960, 0x00d4, 9 }, - { 1024, 0x00d5, 9 }, - { 1088, 0x00d6, 9 }, - { 1152, 0x00d7, 9 }, - { 1216, 0x00d8, 9 }, - { 1280, 0x00d9, 9 }, - { 1344, 0x00da, 9 }, - { 1408, 0x00db, 9 }, - { 1472, 0x0098, 9 }, - { 1536, 0x0099, 9 }, - { 1600, 0x009a, 9 }, - { 1664, 0x0018, 6 }, - { 1728, 0x009b, 9 }, - { 1792, 0x0008, 11 }, - { 1856, 0x000c, 11 }, - { 1920, 0x000d, 11 }, - { 1984, 0x0012, 12 }, - { 2048, 0x0013, 12 }, - { 2112, 0x0014, 12 }, - { 2176, 0x0015, 12 }, - { 2240, 0x0016, 12 }, - { 2304, 0x0017, 12 }, - { 2368, 0x001c, 12 }, - { 2432, 0x001d, 12 }, - { 2496, 0x001e, 12 }, - { 2560, 0x001f, 12 }, - { 9999, 0x0001, 12 } // EOL -}; - -const CCIHuffmanTableEntry CCIBlackTableSave[CCIBlackTableSize]={ - { 0, 0x0037, 10 }, - { 1, 0x0002, 3 }, - { 2, 0x0003, 2 }, - { 3, 0x0002, 2 }, - { 4, 0x0003, 3 }, - { 5, 0x0003, 4 }, - { 6, 0x0002, 4 }, - { 7, 0x0003, 5 }, - { 8, 0x0005, 6 }, - { 9, 0x0004, 6 }, - { 10, 0x0004, 7 }, - { 11, 0x0005, 7 }, - { 12, 0x0007, 7 }, - { 13, 0x0004, 8 }, - { 14, 0x0007, 8 }, - { 15, 0x0018, 9 }, - { 16, 0x0017, 10 }, - { 17, 0x0018, 10 }, - { 18, 0x0008, 10 }, - { 19, 0x0067, 11 }, - { 20, 0x0068, 11 }, - { 21, 0x006c, 11 }, - { 22, 0x0037, 11 }, - { 23, 0x0028, 11 }, - { 24, 0x0017, 11 }, - { 25, 0x0018, 11 }, - { 26, 0x00ca, 12 }, - { 27, 0x00cb, 12 }, - { 28, 0x00cc, 12 }, - { 29, 0x00cd, 12 }, - { 30, 0x0068, 12 }, - { 31, 0x0069, 12 }, - { 32, 0x006a, 12 }, - { 33, 0x006b, 12 }, - { 34, 0x00d2, 12 }, - { 35, 0x00d3, 12 }, - { 36, 0x00d4, 12 }, - { 37, 0x00d5, 12 }, - { 38, 0x00d6, 12 }, - { 39, 0x00d7, 12 }, - { 40, 0x006c, 12 }, - { 41, 0x006d, 12 }, - { 42, 0x00da, 12 }, - { 43, 0x00db, 12 }, - { 44, 0x0054, 12 }, - { 45, 0x0055, 12 }, - { 46, 0x0056, 12 }, - { 47, 0x0057, 12 }, - { 48, 0x0064, 12 }, - { 49, 0x0065, 12 }, - { 50, 0x0052, 12 }, - { 51, 0x0053, 12 }, - { 52, 0x0024, 12 }, - { 53, 0x0037, 12 }, - { 54, 0x0038, 12 }, - { 55, 0x0027, 12 }, - { 56, 0x0028, 12 }, - { 57, 0x0058, 12 }, - { 58, 0x0059, 12 }, - { 59, 0x002b, 12 }, - { 60, 0x002c, 12 }, - { 61, 0x005a, 12 }, - { 62, 0x0066, 12 }, - { 63, 0x0067, 12 }, - { 64, 0x000f, 10 }, - { 128, 0x00c8, 12 }, - { 192, 0x00c9, 12 }, - { 256, 0x005b, 12 }, - { 320, 0x0033, 12 }, - { 384, 0x0034, 12 }, - { 448, 0x0035, 12 }, - { 512, 0x006c, 13 }, - { 576, 0x006d, 13 }, - { 640, 0x004a, 13 }, - { 704, 0x004b, 13 }, - { 768, 0x004c, 13 }, - { 832, 0x004d, 13 }, - { 896, 0x0072, 13 }, - { 960, 0x0073, 13 }, - { 1024, 0x0074, 13 }, - { 1088, 0x0075, 13 }, - { 1152, 0x0076, 13 }, - { 1216, 0x0077, 13 }, - { 1280, 0x0052, 13 }, - { 1344, 0x0053, 13 }, - { 1408, 0x0054, 13 }, - { 1472, 0x0055, 13 }, - { 1536, 0x005a, 13 }, - { 1600, 0x005b, 13 }, - { 1664, 0x0064, 13 }, - { 1728, 0x0065, 13 }, - { 1792, 0x0008, 11 }, - { 1856, 0x000c, 11 }, - { 1920, 0x000d, 11 }, - { 1984, 0x0012, 12 }, - { 2048, 0x0013, 12 }, - { 2112, 0x0014, 12 }, - { 2176, 0x0015, 12 }, - { 2240, 0x0016, 12 }, - { 2304, 0x0017, 12 }, - { 2368, 0x001c, 12 }, - { 2432, 0x001d, 12 }, - { 2496, 0x001e, 12 }, - { 2560, 0x001f, 12 }, - { 9999, 0x0001, 12 } // EOL -}; - - -const CCIHuffmanTableEntry CCI2DModeTableSave[CCI2DModeTableSize]={ - { CCI2DMODE_UNCOMP , 0x000f, 10 }, - { CCI2DMODE_PASS , 0x0001, 4 }, - { CCI2DMODE_HORZ , 0x0001, 3 }, - { CCI2DMODE_VERT_L3, 0x0002, 7 }, - { CCI2DMODE_VERT_L2, 0x0002, 6 }, - { CCI2DMODE_VERT_L1, 0x0002, 3 }, - { CCI2DMODE_VERT_0 , 0x0001, 1 }, - { CCI2DMODE_VERT_R1, 0x0003, 3 }, - { CCI2DMODE_VERT_R2, 0x0003, 6 }, - { CCI2DMODE_VERT_R3, 0x0003, 7 } -}; - - -const CCIHuffmanTableEntry CCIUncompTableSave[CCIUncompTableSize]={ - { CCIUNCOMP_0White_1Black, 0x0001, 1 }, - { CCIUNCOMP_1White_1Black, 0x0001, 2 }, - { CCIUNCOMP_2White_1Black, 0x0001, 3 }, - { CCIUNCOMP_3White_1Black, 0x0001, 4 }, - { CCIUNCOMP_4White_1Black, 0x0001, 5 }, - { CCIUNCOMP_5White , 0x0001, 6 }, - { CCIUNCOMP_0White_End , 0x0001, 7 }, - { CCIUNCOMP_1White_End , 0x0001, 8 }, - { CCIUNCOMP_2White_End , 0x0001, 9 }, - { CCIUNCOMP_3White_End , 0x0001, 10 }, - { CCIUNCOMP_4White_End , 0x0001, 11 } -}; - - -CCIDecompressor::CCIDecompressor( sal_uInt32 nOpts, sal_uInt32 nImageWidth ) : - m_bTableBad ( false ), - m_bStatus ( false ), - m_pIStream ( nullptr ), - m_nEOLCount ( 0 ), - m_nWidth ( nImageWidth ), - m_nOptions ( nOpts ), - m_bFirstEOL ( false ), - m_nInputBitsBuf( 0 ), - m_nInputBitsBufSize( 0 ), - m_nLastLineSize( 0 ) -{ - if ( nOpts & CCI_OPTION_INVERSEBITORDER ) - { - m_pByteSwap.reset( new sal_uInt8[ 256 ] ); - for ( int i = 0; i < 256; i++ ) - { - m_pByteSwap[ i ] = sal::static_int_cast< sal_uInt8 >( - ( i << 7 ) | ( ( i & 2 ) << 5 ) | ( ( i & 4 ) << 3 ) | ( ( i & 8 ) << 1 ) | - ( ( i & 16 ) >> 1 ) | ( ( i & 32 ) >> 3 ) | ( ( i & 64 ) >> 5 ) | ( ( i & 128 ) >> 7 )); - } - } - - MakeLookUp(CCIWhiteTable,CCIWhiteTableSave,m_pWhiteLookUp.data(),CCIWhiteTableSize,13); - MakeLookUp(CCIBlackTable,CCIBlackTableSave,m_pBlackLookUp.data(),CCIBlackTableSize,13); - MakeLookUp(CCI2DModeTable,CCI2DModeTableSave,m_p2DModeLookUp.data(),CCI2DModeTableSize,10); - MakeLookUp(CCIUncompTable,CCIUncompTableSave,m_pUncompLookUp.data(),CCIUncompTableSize,11); -} - - -CCIDecompressor::~CCIDecompressor() -{ -} - - -void CCIDecompressor::StartDecompression( SvStream & rIStream ) -{ - m_pIStream = &rIStream; - m_nInputBitsBufSize = 0; - m_bFirstEOL = true; - m_bStatus = true; - m_nEOLCount = 0; - - if ( m_bTableBad ) - return; -} - -DecompressStatus CCIDecompressor::DecompressScanline( sal_uInt8 * pTarget, sal_uInt64 nTargetBits, bool bLastLine ) -{ - //Read[1|2]DScanlineData take a sal_uInt16, so its either limit here or expand there - if (nTargetBits > SAL_MAX_UINT16) - return DecompressStatus(false, true); - - if ( m_nEOLCount >= 5 ) // RTC (Return To Controller) - return DecompressStatus(true, true); - - if ( !m_bStatus ) - return DecompressStatus(false, true); - - // If EOL-Codes exist, the EOL-Code also appeared in front of the first line. - // (and I thought it means 'End of Line'...) - // Therefore we read the EOL-Code always at the beginning of each line first: - if ( m_nOptions & CCI_OPTION_EOL ) - { - if ( m_bFirstEOL ) - { - sal_uInt32 nCurPos = m_pIStream->Tell(); - sal_uInt16 nOldInputBitsBufSize = m_nInputBitsBufSize; - sal_uInt32 nOldInputBitsBuf = m_nInputBitsBuf; - if ( !ReadEOL() ) - { - m_nInputBitsBufSize = nOldInputBitsBufSize; - m_nInputBitsBuf = nOldInputBitsBuf; - m_pIStream->Seek( nCurPos ); - m_nOptions &=~ CCI_OPTION_EOL; // CCITT Group 3 - Compression Type 2 - } - m_bFirstEOL = false; - } - else - { - if ( !ReadEOL() ) - { - return DecompressStatus(m_bStatus, true); - } - } - } - - if ( m_nEOLCount >= 5 ) // RTC (Return To Controller) - return DecompressStatus(true, true); - - // should the situation arise, generate a white previous line for 2D: - if ( m_nOptions & CCI_OPTION_2D ) - { - if ( m_pLastLine == nullptr || m_nLastLineSize != ( ( nTargetBits + 7 ) >> 3 ) ) - { - m_nLastLineSize = ( nTargetBits + 7 ) >> 3; - m_pLastLine.reset( new sal_uInt8[ m_nLastLineSize ] ); - memset(m_pLastLine.get(), 0, m_nLastLineSize); - } - } - // conditionally align start of line to next byte: - if ( m_nOptions & CCI_OPTION_BYTEALIGNROW ) - m_nInputBitsBufSize &= 0xfff8; - - bool b2D; - // is it a 2D row? - if ( m_nOptions & CCI_OPTION_2D ) - { - if ( m_nOptions & CCI_OPTION_EOL ) - b2D = Read2DTag(); - else - b2D = true; - } - else - b2D = false; - - bool bUnchanged; - // read scanline: - if ( b2D ) - bUnchanged = Read2DScanlineData(pTarget, nTargetBits); - else - bUnchanged = Read1DScanlineData(pTarget, nTargetBits); - - // if we're in 2D mode we have to remember the line: - if ( m_nOptions & CCI_OPTION_2D && m_bStatus ) - { - memcpy(m_pLastLine.get(), pTarget, m_nLastLineSize); - } - - // #i122984# - if( !m_bStatus && bLastLine ) - { - m_bStatus = true; - } - - if ( m_pIStream->GetError() ) - m_bStatus = false; - - return DecompressStatus(m_bStatus, bUnchanged); -} - - -void CCIDecompressor::MakeLookUp(const CCIHuffmanTableEntry * pHufTab, - const CCIHuffmanTableEntry * pHufTabSave, - CCILookUpTableEntry * pLookUp, - sal_uInt16 nHuffmanTableSize, - sal_uInt16 nMaxCodeBits) -{ - sal_uInt16 nLookUpSize = 1 << nMaxCodeBits; - memset(pLookUp, 0, nLookUpSize * sizeof(CCILookUpTableEntry)); - - if (m_bTableBad) - return; - - sal_uInt16 nMask = 0xffff >> (16-nMaxCodeBits); - - for (sal_uInt16 i = 0; i < nHuffmanTableSize; ++i) - { - if ( pHufTab[i].nValue!=pHufTabSave[i].nValue || - pHufTab[i].nCode!=pHufTabSave[i].nCode || - pHufTab[i].nCodeBits!=pHufTabSave[i].nCodeBits || - pHufTab[i].nCodeBits==0 || - pHufTab[i].nCodeBits>nMaxCodeBits ) - { - m_bTableBad=true; - return; - } - sal_uInt16 nMinCode = nMask & (pHufTab[i].nCode << (nMaxCodeBits-pHufTab[i].nCodeBits)); - sal_uInt16 nMaxCode = nMinCode | (nMask >> pHufTab[i].nCodeBits); - for (sal_uInt16 j=nMinCode; j<=nMaxCode; ++j) - { - if (pLookUp[j].nCodeBits!=0) - { - m_bTableBad=true; - return; - } - pLookUp[j].nValue=pHufTab[i].nValue; - pLookUp[j].nCodeBits=pHufTab[i].nCodeBits; - } - } -} - - -bool CCIDecompressor::ReadEOL() -{ - sal_uInt16 nCode; - sal_uInt8 nByte; - - // if (nOptions&CCI_OPTION_BYTEALIGNEOL) nMaxFillBits=7; else nMaxFillBits=0; - // D'oh: Either the option in itiff.cxx is not set correctly (-> error in documentation) - // or there exist some nasty export filter who always do align. - // In addition files were found in which more than the necessary maximum of 7 filling - // bits were found. Therefore we now generally accept up to 32 nonsense bits in front of the EOL-Code: - // And I found a file in which up to ??? nonsense bits are written. Furthermore the byte order is switched in it. (SJ) - - sal_uInt32 nMaxPos = m_pIStream->Tell(); - nMaxPos += m_nWidth >> 3; - - for ( ;; ) - { - while ( m_nInputBitsBufSize < 12 ) - { - m_pIStream->ReadUChar( nByte ); - if (!m_pIStream->good()) - return false; - if ( m_pIStream->Tell() > nMaxPos ) - return false; - - if ( m_nOptions & CCI_OPTION_INVERSEBITORDER ) - nByte = m_pByteSwap[ nByte ]; - m_nInputBitsBuf=(m_nInputBitsBuf<<8) | static_cast<sal_uInt64>(nByte); - m_nInputBitsBufSize += 8; - } - nCode = static_cast<sal_uInt16>( ( m_nInputBitsBuf >> ( m_nInputBitsBufSize - 12 ) ) & 0x0fff ); - if ( nCode == 0x0001 ) - { - m_nEOLCount++; - m_nInputBitsBufSize -= 12; - break; - } - else - m_nInputBitsBufSize--; - } - return true; -} - - -bool CCIDecompressor::Read2DTag() -{ - // read a bit and return sal_True if it's 0, otherwise return sal_False - if (m_nInputBitsBufSize==0) { - sal_uInt8 nByte(0); - m_pIStream->ReadUChar( nByte ); - if ( m_nOptions & CCI_OPTION_INVERSEBITORDER ) - nByte = m_pByteSwap[ nByte ]; - m_nInputBitsBuf = nByte; - m_nInputBitsBufSize=8; - } - m_nInputBitsBufSize--; - return ((m_nInputBitsBuf>>m_nInputBitsBufSize)&0x0001) == 0; -} - - -sal_uInt8 CCIDecompressor::ReadBlackOrWhite() -{ - // read a bit and deliver 0x00 if it's 0, otherwise 0xff - if (m_nInputBitsBufSize==0) { - sal_uInt8 nByte(0); - m_pIStream->ReadUChar( nByte ); - if ( m_nOptions & CCI_OPTION_INVERSEBITORDER ) - nByte = m_pByteSwap[ nByte ]; - m_nInputBitsBuf = nByte; - m_nInputBitsBufSize=8; - } - m_nInputBitsBufSize--; - if ( (m_nInputBitsBuf>>m_nInputBitsBufSize) & 0x0001 ) return 0xff; - else return 0x00; -} - - -sal_uInt16 CCIDecompressor::ReadCodeAndDecode(const CCILookUpTableEntry * pLookUp, - sal_uInt16 nMaxCodeBits) -{ - // read a Huffman code and decode it: - while (m_nInputBitsBufSize<nMaxCodeBits) - { - sal_uInt8 nByte(0); - m_pIStream->ReadUChar( nByte ); - if ( m_nOptions & CCI_OPTION_INVERSEBITORDER ) - nByte = m_pByteSwap[ nByte ]; - m_nInputBitsBuf=(m_nInputBitsBuf<<8) | static_cast<sal_uInt64>(nByte); - m_nInputBitsBufSize+=8; - } - sal_uInt16 nCode = static_cast<sal_uInt16>((m_nInputBitsBuf>>(m_nInputBitsBufSize-nMaxCodeBits)) - &(0xffff>>(16-nMaxCodeBits))); - sal_uInt16 nCodeBits = pLookUp[nCode].nCodeBits; - if (nCodeBits==0) m_bStatus=false; - m_nInputBitsBufSize = m_nInputBitsBufSize - nCodeBits; - return pLookUp[nCode].nValue; -} - - -void CCIDecompressor::FillBits(sal_uInt8 * pTarget, sal_uInt16 nTargetBits, - sal_uInt16 nBitPos, sal_uInt16 nNumBits, - sal_uInt8 nBlackOrWhite) -{ - if ( nBitPos >= nTargetBits ) - return; - if ( nBitPos + nNumBits > nTargetBits ) - nNumBits = nTargetBits - nBitPos; - - pTarget+=nBitPos>>3; - nBitPos&=7; - - if (nBlackOrWhite==0x00) *pTarget &= 0xff << (8-nBitPos); - else *pTarget |= 0xff >> nBitPos; - if (nNumBits>8-nBitPos) { - nNumBits-=8-nBitPos; - while (nNumBits>=8) { - *(++pTarget)=nBlackOrWhite; - nNumBits-=8; - } - if (nNumBits>0) *(++pTarget)=nBlackOrWhite; - } -} - -sal_uInt16 CCIDecompressor::CountBits(const sal_uInt8 * pData, sal_uInt16 nDataSizeBits, - sal_uInt16 nBitPos, sal_uInt8 nBlackOrWhite) -{ - // here the number of bits belonging together is being counted - // which all have the color nBlackOrWhite (0xff or 0x00) - // from the position nBitPos on - sal_uInt32 nPos = nBitPos; - for (;;) - { - if (nPos>=nDataSizeBits) - { - nPos=nDataSizeBits; - break; - } - sal_uInt8 nData = pData[nPos>>3]; - sal_uInt16 nLo = nPos & 7; - if (nLo==0 && nData==nBlackOrWhite) - { - nPos+=8; - } - else - { - if ( ((nData^nBlackOrWhite) & (0x80 >> nLo))!=0) - break; - ++nPos; - } - } - return nPos-nBitPos; -} - -bool CCIDecompressor::Read1DScanlineData(sal_uInt8 * pTarget, sal_uInt16 nBitsToRead) -{ - sal_uInt16 nTargetBits = nBitsToRead; - sal_uInt16 nCode,nCodeBits,nDataBits,nTgtFreeByteBits; - sal_uInt8 nBlackOrWhite; // is 0xff for black or 0x00 for white - bool bTerminatingCode; - - // the first code is always a "white-code": - nBlackOrWhite=0x00; - - // number of bits that aren't written in the byte *pTarget yet: - nTgtFreeByteBits=8; - - // loop through codes from the input stream: - do { - - // fetch next 13 bits into nCodem but don't remove them from - // the input buffer: - while (m_nInputBitsBufSize<13) { - sal_uInt8 nByte(0); - m_pIStream->ReadUChar( nByte ); - if ( m_nOptions & CCI_OPTION_INVERSEBITORDER ) - nByte = m_pByteSwap[ nByte ]; - m_nInputBitsBuf=(m_nInputBitsBuf<<8) | static_cast<sal_uInt64>(nByte); - m_nInputBitsBufSize+=8; - } - nCode=static_cast<sal_uInt16>((m_nInputBitsBuf>>(m_nInputBitsBufSize-13))&0x1fff); - - // determine the number of DataBits CodeBits: - if (nBlackOrWhite) { - nCodeBits=m_pBlackLookUp[nCode].nCodeBits; - nDataBits=m_pBlackLookUp[nCode].nValue; - } - else { - nCodeBits=m_pWhiteLookUp[nCode].nCodeBits; - nDataBits=m_pWhiteLookUp[nCode].nValue; - } - // is that an invalid code? - if ( nDataBits == 9999 ) - { - return nTargetBits == nBitsToRead; - } - if ( nCodeBits == 0 ) - { - return nTargetBits == nBitsToRead; // could be filling bits now - } - m_nEOLCount = 0; - // too much data? - if (nDataBits>nTargetBits) { - // Yes, could be a subsequent error cause by an invalid code - // Therefore continue anyhow: - nDataBits=nTargetBits; - } - - // is that a 'Terminating-Code'? - bTerminatingCode = nDataBits<64; - - // remove the read bits from the input buffer: - m_nInputBitsBufSize = m_nInputBitsBufSize - nCodeBits; - - // write the number of data bits into the scanline: - if (nDataBits>0) { - nTargetBits = nTargetBits - nDataBits; - if (nBlackOrWhite==0x00) *pTarget &= 0xff << nTgtFreeByteBits; - else *pTarget |= 0xff >> (8-nTgtFreeByteBits); - if (nDataBits<=nTgtFreeByteBits) { - if (nDataBits==nTgtFreeByteBits) { - pTarget++; - nTgtFreeByteBits=8; - } - else nTgtFreeByteBits = nTgtFreeByteBits - nDataBits; - } - else { - nDataBits = nDataBits - nTgtFreeByteBits; - pTarget++; - nTgtFreeByteBits=8; - if (nDataBits >= 8) - { - const sal_uInt16 nDataBytes = nDataBits / 8; - memset(pTarget, nBlackOrWhite, nDataBytes); - pTarget += nDataBytes; - nDataBits -= nDataBytes * 8; - } - - if (nDataBits>0) { - *pTarget=nBlackOrWhite; - nTgtFreeByteBits = nTgtFreeByteBits - nDataBits; - } - } - } - - // should the situation arise, switch Black <-> White: - if (bTerminatingCode) nBlackOrWhite = ~nBlackOrWhite; - - } while (nTargetBits>0 || !bTerminatingCode); - - return nTargetBits == nBitsToRead; -} - -bool CCIDecompressor::Read2DScanlineData(sal_uInt8 * pTarget, sal_uInt16 nTargetBits) -{ - sal_uInt16 n2DMode,nBitPos,nUncomp,nRun,nRun2,nt; - sal_uInt8 nBlackOrWhite; - - nBlackOrWhite=0x00; - nBitPos=0; - - while (nBitPos<nTargetBits && m_bStatus) { - - n2DMode=ReadCodeAndDecode(m_p2DModeLookUp.data(),10); - if (!m_bStatus) - return nBitPos == 0; - - if (n2DMode==CCI2DMODE_UNCOMP) { - for (;;) { - nUncomp=ReadCodeAndDecode(m_pUncompLookUp.data(),11); - if (!m_bStatus) - break; - if ( nUncomp <= CCIUNCOMP_4White_1Black ) { - nRun=nUncomp-CCIUNCOMP_0White_1Black; - FillBits(pTarget,nTargetBits,nBitPos,nRun,0x00); - nBitPos = nBitPos + nRun; - FillBits(pTarget,nTargetBits,nBitPos,1,0xff); - nBitPos++; - } - else if ( nUncomp == CCIUNCOMP_5White ) { - FillBits(pTarget,nTargetBits,nBitPos,5,0x00); - nBitPos = nBitPos + 5; - } - else { - nRun=nUncomp-CCIUNCOMP_0White_End; - FillBits(pTarget,nTargetBits,nBitPos,nRun,0x00); - nBitPos = nBitPos + nRun; - nBlackOrWhite=ReadBlackOrWhite(); - break; - } - } - } - - else if (n2DMode==CCI2DMODE_PASS) { - if (nBitPos==0 && nBlackOrWhite==0x00 && CountBits(m_pLastLine.get(), nTargetBits, 0, 0xff)!=0) nRun=0; - else { - nRun = CountBits(m_pLastLine.get(), nTargetBits, nBitPos, ~nBlackOrWhite); - nRun = nRun + CountBits(m_pLastLine.get(), nTargetBits, nBitPos + nRun, nBlackOrWhite); - } - nRun = nRun + CountBits(m_pLastLine.get(), nTargetBits, nBitPos + nRun, ~nBlackOrWhite); - FillBits(pTarget,nTargetBits,nBitPos,nRun,nBlackOrWhite); - nBitPos = nBitPos + nRun; - } - - else if (n2DMode==CCI2DMODE_HORZ) { - if (nBlackOrWhite==0x00) { - nRun=0; - do { - nt=ReadCodeAndDecode(m_pWhiteLookUp.data(),13); - nRun = nRun + nt; - } while (nt>=64); - nRun2=0; - do { - nt=ReadCodeAndDecode(m_pBlackLookUp.data(),13); - nRun2 = nRun2 + nt; - } while (nt>=64); - } - else { - nRun=0; - do { - nt=ReadCodeAndDecode(m_pBlackLookUp.data(),13); - nRun = nRun + nt; - } while (nt>=64); - nRun2=0; - do { - nt=ReadCodeAndDecode(m_pWhiteLookUp.data(),13); - nRun2 = nRun2 + nt; - } while (nt>=64); - } - FillBits(pTarget,nTargetBits,nBitPos,nRun,nBlackOrWhite); - nBitPos = nBitPos + nRun; - FillBits(pTarget,nTargetBits,nBitPos,nRun2,~nBlackOrWhite); - nBitPos = nBitPos + nRun2; - } - - else { // it's one of the modes CCI2DMODE_VERT_... - if (nBitPos==0 && nBlackOrWhite==0x00 && CountBits(m_pLastLine.get(), nTargetBits, 0, 0xff)!=0) nRun=0; - else { - nRun = CountBits(m_pLastLine.get(), nTargetBits, nBitPos, ~nBlackOrWhite); - nRun = nRun + CountBits(m_pLastLine.get(), nTargetBits, nBitPos + nRun, nBlackOrWhite); - } - nRun+=n2DMode-CCI2DMODE_VERT_0; - FillBits(pTarget,nTargetBits,nBitPos,nRun,nBlackOrWhite); - nBitPos = nBitPos + nRun; - nBlackOrWhite=~nBlackOrWhite; - } - } - - return nBitPos == 0; -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/filter/itiff/itiff.cxx b/vcl/source/filter/itiff/itiff.cxx index b434471b2317..8bce9e705b35 100644 --- a/vcl/source/filter/itiff/itiff.cxx +++ b/vcl/source/filter/itiff/itiff.cxx @@ -20,22 +20,16 @@ #include <sal/config.h> #include <sal/log.hxx> -#include <unotools/configmgr.hxx> -#include <vcl/FilterConfigItem.hxx> #include <vcl/graph.hxx> #include <vcl/BitmapTools.hxx> #include <vcl/animate/Animation.hxx> #include <bitmap/BitmapWriteAccess.hxx> -#include <tools/fract.hxx> #include <tools/stream.hxx> -#include "lzwdecom.hxx" -#include "ccidecom.hxx" #include <tiffio.h> #include <filter/TiffReader.hxx> -#if 1 namespace { struct Context @@ -169,1694 +163,5 @@ bool ImportTiffGraphicImport(SvStream& rTIFF, Graphic& rGraphic) return false; } -#else - -namespace { - -template< typename T > T BYTESWAP(T nByte) { - return ( nByte << 7 ) | ( ( nByte & 2 ) << 5 ) | ( ( nByte & 4 ) << 3 ) | - ( ( nByte & 8 ) << 1 ) | ( ( nByte & 16 ) >> 1 ) | - ( ( nByte & 32 ) >> 3 ) | ( ( nByte & 64 ) >> 5 ) | - ( ( nByte & 128 ) >> 7 ); -} - -//============================ TIFFReader ================================== - -class TIFFReader -{ - -private: - - bool bStatus; // Whether until now no error occurred - Animation aAnimation; - - SvStream* pTIFF; // the TIFF file that should be read - std::vector<sal_uInt8> maBitmap; - Size maBitmapPixelSize; - std::vector<Color> mvPalette; - MapMode maBitmapPrefMapMode; - Size maBitmapPrefSize; - sal_uInt16 nDstBitsPerPixel; - int nLargestPixelIndex; - - sal_uInt64 nOrigPos; // start position in pTIFF - sal_uInt64 nEndOfFile; // end of file position in pTIFF - - - sal_uInt16 nDataType; - // Data taken from the TIFF tags: - bool bByteSwap; // sal_True if bits 0..7 -> 7..0 should get converted ( FILLORDER = 2 ); - - sal_uInt32 nNewSubFile; - sal_uInt32 nSubFile; - sal_Int32 nImageWidth; // picture width in pixels - sal_Int32 nImageLength; // picture height in pixels - sal_uInt32 nBitsPerSample; // bits per pixel per layer - sal_uInt32 nCompression; // kind of compression - sal_uInt32 nPhotometricInterpretation; - sal_uInt32 nThresholding; - sal_uInt32 nCellWidth; - sal_uInt32 nCellLength; - sal_uInt32 nFillOrder; - std::vector<sal_uInt64> aStripOffsets; // field of offsets to the Bitmap-Data-"Strips" - sal_uInt32 nOrientation; - sal_uInt32 nSamplesPerPixel; // number of layers - sal_uInt32 nRowsPerStrip; // if it's not compressed: number of rows per Strip - std::vector<sal_uInt32> aStripByteCounts; // if compressed (in a certain way): size of the strips - sal_uInt32 nMinSampleValue; - sal_uInt32 nMaxSampleValue; - double fXResolution; // X-resolution or 0.0 - double fYResolution; // Y-resolution or 0.0 - sal_uInt32 nPlanarConfiguration; - sal_uInt32 nGroup3Options; - sal_uInt32 nGroup4Options; - sal_uInt32 nResolutionUnit; // unit of fX/YResolution: 1=unknown, 2(default)=inch, 3=cm - sal_uInt32 nPredictor; - std::vector<sal_uInt32> aColorMap; // color palette - sal_uInt32 nNumColors; // number of colors within the color palette - - sal_uInt32 nPlanes; // number of layers within the Tiff file - sal_uInt32 nStripsPerPlane; // number of Strips per layer - sal_uInt32 nBytesPerRow; // Bytes per line per Layer in the Tiff file ( uncompressed ) - std::vector<sal_uInt8> aMap[4]; // temporary Scanline - - - sal_uInt32 DataTypeSize(); - sal_uInt32 ReadIntData(); - double ReadDoubleData(); - - void ReadHeader(); - void ReadTagData( sal_uInt16 nTagType, sal_uInt32 nDataLen ); - - sal_uInt8* getMapData(sal_uInt32 np); - - bool ReadMap(); - // reads/decompress the bitmap data and fills aMap - - sal_uInt32 GetBits(const sal_uInt8 * pSrc, sal_uInt32 nBitsPos, sal_uInt32 nBitsCount) const; - // fetches BitsCount bits from pSrc[..] at the position nBitsPos - - void MakePalCol(); - // Create the bitmap from the temporary bitmap aMap - // and partly deletes aMap while doing this. - - bool ConvertScanline(sal_Int32 nY); - // converts a Scanline to the Windows-BMP format - - bool HasAlphaChannel() const; - - void SetPixel(tools::Long nY, tools::Long nX, sal_uInt8 cIndex); - void SetPixel(tools::Long nY, tools::Long nX, Color c); - void SetPixelAlpha(tools::Long nY, tools::Long nX, sal_uInt8 nAlpha); - -public: - - TIFFReader() - : bStatus(false) - , pTIFF(nullptr) - , nDstBitsPerPixel(0) - , nLargestPixelIndex(-1) - , nOrigPos(0) - , nEndOfFile(0) - , nDataType(0) - , bByteSwap(false) - , nNewSubFile(0) - , nSubFile(0) - , nImageWidth(0) - , nImageLength(0) - , nBitsPerSample(1) - , nCompression(1) - , nPhotometricInterpretation(0) - , nThresholding(1) - , nCellWidth(1) - , nCellLength(1) - , nFillOrder(1) - , nOrientation(1) - , nSamplesPerPixel(1) - , nRowsPerStrip(0xffffffff) - , nMinSampleValue(0) - , nMaxSampleValue(0) - , fXResolution(0.0) - , fYResolution(0.0) - , nPlanarConfiguration(1) - , nGroup3Options(0) - , nGroup4Options(0) - , nResolutionUnit(2) - , nPredictor(0) - , nNumColors(0) - , nPlanes(0) - , nStripsPerPlane(0) - , nBytesPerRow(0) - { - } - - sal_uInt32 GetRowsPerStrip() const - { - //Rows Per Strip: - // - //(TIFF format only) The number of rows of pixels per strip to use for - //encoding the TIFF image. A value greater than zero specifies the - //number of rows per strip. A value of 0 sets the rows per strip equal - //to the image length, resulting in a single strip. A value of -1 (the - //default) sets the rows per strip equal to infinity, resulting in a - //single strip. - return nRowsPerStrip == 0 ? nImageLength : nRowsPerStrip; - } - - bool ReadTIFF( SvStream & rTIFF, Graphic & rGraphic ); -}; - -} - -//=================== Methods of TIFFReader ============================== - -sal_uInt32 TIFFReader::DataTypeSize() -{ - sal_uInt32 nSize; - switch ( nDataType ) - { - case 1 : // BYTE - case 2 : // ASCII - case 6 : // SIGNED Byte - case 7 : // UNDEFINED - nSize = 1; - break; - case 3 : // UINT16 - case 8 : // INT16 - nSize = 2; - break; - case 4 : // UINT32 - case 9 : // INT32 - case 11 : // FLOAT - nSize = 4; - break; - case 5 : // RATIONAL - case 10 : // SIGNED RATIONAL - case 12 : // DOUBLE - nSize = 8; - break; - default: - pTIFF->SetError(SVSTREAM_FILEFORMAT_ERROR); - nSize=1; - } - return nSize; -} - -sal_uInt32 TIFFReader::ReadIntData() -{ - double nDOUBLE(0.0); - float nFLOAT(0); - sal_uInt32 nUINT32a(0), nUINT32b(0); - sal_Int32 nINT32(0); - sal_uInt16 nUINT16(0); - sal_Int16 nINT16(0); - sal_uInt8 nBYTE(0); - char nCHAR(0); - - switch( nDataType ) - { - case 0 : //?? - case 1 : - case 2 : - case 7 : - pTIFF->ReadUChar( nBYTE ); - nUINT32a = nBYTE; - break; - case 3 : - pTIFF->ReadUInt16( nUINT16 ); - nUINT32a = nUINT16; - break; - case 9 : - case 4 : - pTIFF->ReadUInt32( nUINT32a ); - break; - case 5 : - pTIFF->ReadUInt32( nUINT32a ).ReadUInt32( nUINT32b ); - if ( nUINT32b != 0 ) - nUINT32a /= nUINT32b; - break; - case 6 : - pTIFF->ReadChar( nCHAR ); - nUINT32a = static_cast<sal_Int32>(nCHAR); - break; - case 8 : - pTIFF->ReadInt16( nINT16 ); - nUINT32a = static_cast<sal_Int32>(nINT16); - break; - case 10 : - pTIFF->ReadUInt32( nUINT32a ).ReadInt32( nINT32 ); - if ( nINT32 != 0 ) - nUINT32a /= nINT32; - break; - case 11 : - pTIFF->ReadFloat( nFLOAT ); - if (!std::isnan(nFLOAT) && nFLOAT > SAL_MIN_INT32 - 1.0 - && nFLOAT < SAL_MAX_INT32 + 1.0) - { - nUINT32a = static_cast<sal_Int32>(nFLOAT); - } - else - { - SAL_INFO("filter.tiff", "float " << nFLOAT << " outsider of sal_Int32 range"); - } - break; - case 12 : - pTIFF->ReadDouble( nDOUBLE ); - if (!std::isnan(nDOUBLE) && nDOUBLE > SAL_MIN_INT32 - 1.0 - && nDOUBLE < SAL_MAX_INT32 + 1.0) - { - nUINT32a = static_cast<sal_Int32>(nDOUBLE); - } - else - { - SAL_INFO("filter.tiff", "double " << nDOUBLE << " outsider of sal_Int32 range"); - } - break; - default: - pTIFF->ReadUInt32( nUINT32a ); - break; - } - return nUINT32a; -} - -double TIFFReader::ReadDoubleData() -{ - switch (nDataType) { - case 5: - { - sal_uInt32 nulong(0); - pTIFF->ReadUInt32( nulong ); - double nd = static_cast<double>(nulong); - nulong = 0; - pTIFF->ReadUInt32( nulong ); - if ( nulong != 0 ) - nd /= static_cast<double>(nulong); - return nd; - } - - case 11: - { - float x = 0; - pTIFF->ReadFloat(x); - return x; - } - - case 12: - { - double x = 0; - pTIFF->ReadDouble(x); - return x; - } - - default: - return static_cast<double>(ReadIntData()); - } -} - -void TIFFReader::ReadTagData( sal_uInt16 nTagType, sal_uInt32 nDataLen) -{ - if ( !bStatus ) - return; - - switch ( nTagType ) - { - case 0x00fe: // New Sub File - nNewSubFile = ReadIntData(); - SAL_INFO("filter.tiff","NewSubFile: " << nNewSubFile); - break; - - case 0x00ff: // Sub File - nSubFile = ReadIntData(); - SAL_INFO("filter.tiff","SubFile: " << nSubFile); - break; - - case 0x0100: // Image Width - nImageWidth = ReadIntData(); - SAL_INFO("filter.tiff","ImageWidth: " << nImageWidth); - break; - - case 0x0101: // Image Length - nImageLength = ReadIntData(); - SAL_INFO("filter.tiff","ImageLength: " << nImageLength); - break; - - case 0x0102: // Bits Per Sample - nBitsPerSample = ReadIntData(); - SAL_INFO("filter.tiff","BitsPerSample: " << nBitsPerSample); - if ( nBitsPerSample >= 32 ) // 32 bit and larger samples are not supported - bStatus = false; - break; - - case 0x0103: // Compression - nCompression = ReadIntData(); - SAL_INFO("filter.tiff","Compression: " << nCompression); - break; - - case 0x0106: // Photometric Interpretation - nPhotometricInterpretation = ReadIntData(); - SAL_INFO("filter.tiff","PhotometricInterpretation: " << nPhotometricInterpretation); - break; - - case 0x0107: // Thresholding - nThresholding = ReadIntData(); - SAL_INFO("filter.tiff","Thresholding: " << nThresholding); - break; - - case 0x0108: // Cell Width - nCellWidth = ReadIntData(); - break; - - case 0x0109: // Cell Length - nCellLength = ReadIntData(); - break; - - case 0x010a: // Fill Order - nFillOrder = ReadIntData(); - SAL_INFO("filter.tiff","FillOrder: " << nFillOrder); - break; - - case 0x0111: { // Strip Offset(s) - size_t nOldNumSO = aStripOffsets.size(); - nDataLen += nOldNumSO; - size_t const nMaxAllocAllowed = SAL_MAX_INT32 / sizeof(sal_uInt32); - size_t nMaxRecordsAvailable = pTIFF->remainingSize() / DataTypeSize(); - if (nDataLen > nOldNumSO && nDataLen < nMaxAllocAllowed && - (nDataLen - nOldNumSO) <= nMaxRecordsAvailable) - { - try - { - aStripOffsets.resize(nDataLen); - if (nOrigPos) - { - for (size_t i = 0; i < nOldNumSO; ++i) - aStripOffsets[i] += nOrigPos; - } - for (size_t i = nOldNumSO; i < aStripOffsets.size(); ++i) - aStripOffsets[i] = ReadIntData() + nOrigPos; - } - catch (const std::bad_alloc &) - { - aStripOffsets.clear(); - } - } - SAL_INFO("filter.tiff","StripOffsets (Number:) " << nDataLen); - break; - } - case 0x0112: // Orientation - nOrientation = ReadIntData(); - SAL_INFO("filter.tiff","Orientation: " << nOrientation); - break; - - case 0x0115: // Samples Per Pixel - nSamplesPerPixel = ReadIntData(); - SAL_INFO("filter.tiff","SamplesPerPixel: " << nSamplesPerPixel); - - if (nSamplesPerPixel > USHRT_MAX) // ofz#15993 the expected type is SHORT - bStatus = false; - - break; - - case 0x0116: // Rows Per Strip - nRowsPerStrip = ReadIntData(); - SAL_INFO("filter.tiff","RowsPerStrip: " << nRowsPerStrip); - break; - - case 0x0117: { // Strip Byte Counts - size_t nOldNumSBC = aStripByteCounts.size(); - nDataLen += nOldNumSBC; - size_t const nMaxAllocAllowed = SAL_MAX_INT32 / sizeof(sal_uInt32); - size_t nMaxRecordsAvailable = pTIFF->remainingSize() / DataTypeSize(); - if (nDataLen > nOldNumSBC && nDataLen < nMaxAllocAllowed && - (nDataLen - nOldNumSBC) <= nMaxRecordsAvailable) - { - try - { - aStripByteCounts.resize(nDataLen); - for (size_t i = nOldNumSBC; i < aStripByteCounts.size(); ++i) - aStripByteCounts[i] = ReadIntData(); - } - catch (const std::bad_alloc &) - { - aStripByteCounts.clear(); - } - } - SAL_INFO("filter.tiff","StripByteCounts (Number:) " << nDataLen); - break; - } - case 0x0118: // Min Sample Value - nMinSampleValue = ReadIntData(); - SAL_INFO("filter.tiff","MinSampleValue: " << nMinSampleValue); - break; - - case 0x0119: // Max Sample Value - nMaxSampleValue = ReadIntData(); - SAL_INFO("filter.tiff","MaxSampleValue: " << nMaxSampleValue); - break; - - case 0x011a: // X Resolution - fXResolution = ReadDoubleData(); - break; - - case 0x011b: // Y Resolution - fYResolution = ReadDoubleData(); - break; - - case 0x011c: // Planar Configuration - nPlanarConfiguration = ReadIntData(); - SAL_INFO("filter.tiff","PlanarConfiguration: " << nPlanarConfiguration); - break; - - case 0x0124: // Group 3 Options - nGroup3Options = ReadIntData(); - SAL_INFO("filter.tiff","Group3Options: " << nGroup3Options); - break; - - case 0x0125: // Group 4 Options - nGroup4Options = ReadIntData(); - SAL_INFO("filter.tiff","Group4Options: " << nGroup4Options); - break; - - case 0x0128: // Resolution Unit - nResolutionUnit = ReadIntData(); - break; - - case 0x013d: // Predictor - nPredictor = ReadIntData(); - SAL_INFO("filter.tiff","Predictor: " << nPredictor); - break; - - case 0x0140: { // Color Map - sal_uInt16 nVal; - nNumColors = (sal_uInt32(1) << nBitsPerSample); - if ( nDataType == 3 && nNumColors <= 256) - { - aColorMap.resize(256); - for (sal_uInt32 i = 0; i < nNumColors; ++i) - aColorMap[i] = 0; - for (sal_uInt32 i = 0; i < nNumColors; ++i) - { - pTIFF->ReadUInt16( nVal ); - aColorMap[i] |= ( static_cast<sal_uInt32>(nVal) << 8 ) & 0x00ff0000; - } - for (sal_uInt32 i = 0; i < nNumColors; ++i) - { - pTIFF->ReadUInt16( nVal ); - aColorMap[i] |= static_cast<sal_uInt32>(nVal) & 0x0000ff00; - } - for (sal_uInt32 i = 0; i < nNumColors; ++i) - { - pTIFF->ReadUInt16( nVal ); - aColorMap[i] |= ( static_cast<sal_uInt32>(nVal) >> 8 ) & 0x000000ff; - } - } - else - bStatus = false; - SAL_INFO("filter.tiff","ColorMap (number of colors): " << nNumColors); - break; - } - - case 0x0153: { // SampleFormat - sal_uInt32 nSampleFormat = ReadIntData(); - if ( nSampleFormat == 3 ) // IEEE floating point samples are not supported yet - bStatus = false; - break; - } - } - - if ( pTIFF->GetError() ) - bStatus = false; -} - -sal_uInt8* TIFFReader::getMapData(sal_uInt32 np) -{ - aMap[np].resize(nBytesPerRow); - return aMap[np].data(); -} - -bool TIFFReader::ReadMap() -{ - //when fuzzing with a max len set, max decompress to 250 times that limit - static size_t nMaxAllowedDecompression = [](const char* pEnv) { size_t nRet = pEnv ? std::atoi(pEnv) : 0; return nRet * 250; }(std::getenv("FUZZ_MAX_INPUT_LEN")); - size_t nTotalDataRead = 0; - - if ( nCompression == 1 || nCompression == 32771 ) - { - sal_uInt32 nStripBytesPerRow; - - if ( nCompression == 1 ) - nStripBytesPerRow = nBytesPerRow; - else - nStripBytesPerRow = ( nBytesPerRow + 1 ) & 0xfffffffe; - for (sal_Int32 ny = 0; ny < nImageLength; ++ny) - { - for (sal_uInt32 np = 0; np < nPlanes; ++np) - { - if (np >= SAL_N_ELEMENTS(aMap)) - return false; - sal_uInt32 nStrip = ny / GetRowsPerStrip() + np * nStripsPerPlane; - if ( nStrip >= aStripOffsets.size()) - return false; - pTIFF->Seek( aStripOffsets[ nStrip ] + ( ny % GetRowsPerStrip() ) * nStripBytesPerRow ); - // tdf#126147 allow a short incomplete read - auto pDest = getMapData(np); - auto nRead = pTIFF->ReadBytes(pDest, nBytesPerRow); - if (nRead != nBytesPerRow) - memset(pDest + nRead, 0, nBytesPerRow - nRead); - } - if ( !ConvertScanline( ny ) ) - return false; - } - } - else if ( nCompression == 2 || nCompression == 3 || nCompression == 4 ) - { - sal_uInt32 nOptions; - if ( nCompression == 2 ) - { - nOptions = CCI_OPTION_BYTEALIGNROW; - } - else if ( nCompression == 3 ) - { - nOptions = CCI_OPTION_EOL; - if ( nGroup3Options & 0x00000001 ) - nOptions |= CCI_OPTION_2D; - if ( nGroup3Options & 0x00000004 ) - nOptions |= CCI_OPTION_BYTEALIGNEOL; - if ( nGroup3Options & 0xfffffffa ) - return false; - } - else - { // nCompression==4 - nOptions = CCI_OPTION_2D; - if ( nGroup4Options & 0xffffffff ) - return false; - } - if ( nFillOrder == 2 ) - { - nOptions |= CCI_OPTION_INVERSEBITORDER; - bByteSwap = false; - } - sal_uInt32 nStrip = 0; - if (nStrip >= aStripOffsets.size()) - return false; - sal_uInt64 nOffset = aStripOffsets[nStrip]; - if (nOffset > nEndOfFile) - return false; - pTIFF->Seek(aStripOffsets[nStrip]); - - CCIDecompressor aCCIDecom( nOptions, nImageWidth ); - - aCCIDecom.StartDecompression( *pTIFF ); - - const bool bHasAlphaChannel = HasAlphaChannel(); - for (sal_Int32 ny = 0; ny < nImageLength; ++ny) - { - bool bDifferentToPrev = ny == 0; - for (sal_uInt32 np = 0; np < nPlanes; ++np) - { - if ( ny / GetRowsPerStrip() + np * nStripsPerPlane > nStrip ) - { - nStrip=ny/GetRowsPerStrip()+np*nStripsPerPlane; - if (nStrip >= aStripOffsets.size()) - return false; - nOffset = aStripOffsets[nStrip]; - if (nOffset > nEndOfFile) - return false; - pTIFF->Seek(nOffset); - aCCIDecom.StartDecompression( *pTIFF ); - } - if (np >= SAL_N_ELEMENTS(aMap)) - return false; - DecompressStatus aResult = aCCIDecom.DecompressScanline(getMapData(np), nImageWidth * nBitsPerSample * nSamplesPerPixel / nPlanes, np + 1 == nPlanes); - if (!aResult.m_bSuccess) - return false; - bDifferentToPrev |= !aResult.m_bBufferUnchanged; - if ( pTIFF->GetError() ) - return false; - nTotalDataRead += nBytesPerRow; - if (nMaxAllowedDecompression && nTotalDataRead > nMaxAllowedDecompression) - return false; - } - if (!bDifferentToPrev) - { - //if the buffer for this line didn't change, then just copy the - //previous scanline instead of painfully decoding and setting - //each pixel one by one again - const int nColorSize = bHasAlphaChannel ? 4 : 3; - memcpy( maBitmap.data() + (ny * maBitmapPixelSize.Width()) * nColorSize, - maBitmap.data() + ((ny-1) * maBitmapPixelSize.Width()) * nColorSize, - maBitmapPixelSize.Width() * nColorSize); - } - else - { - if (!ConvertScanline(ny)) - return false; - } - } - } - else if ( nCompression == 5 ) - { - LZWDecompressor aLZWDecom; - sal_uInt32 nStrip(0); - if (nStrip >= aStripOffsets.size()) - return false; - pTIFF->Seek(aStripOffsets[nStrip]); - aLZWDecom.StartDecompression(*pTIFF); - for (sal_Int32 ny = 0; ny < nImageLength; ++ny) - { - for (sal_uInt32 np = 0; np < nPlanes; ++np) - { - if ( ny / GetRowsPerStrip() + np * nStripsPerPlane > nStrip ) - { - nStrip = ny / GetRowsPerStrip() + np * nStripsPerPlane; - if (nStrip >= aStripOffsets.size()) - return false; - pTIFF->Seek(aStripOffsets[nStrip]); - aLZWDecom.StartDecompression(*pTIFF); - } - if (np >= SAL_N_ELEMENTS(aMap)) - return false; - if ( ( aLZWDecom.Decompress(getMapData(np), nBytesPerRow) != nBytesPerRow ) || pTIFF->GetError() ) - return false; - } - - nTotalDataRead += nBytesPerRow; - if (nMaxAllowedDecompression && nTotalDataRead > nMaxAllowedDecompression) - return false; - - if ( !ConvertScanline( ny ) ) - return false; - } - } - else if ( nCompression == 32773 ) - { - sal_uInt32 nStrip(0); - if (nStrip >= aStripOffsets.size()) - return false; - pTIFF->Seek(aStripOffsets[nStrip]); - for (sal_Int32 ny = 0; ny < nImageLength; ++ny) - { - for (sal_uInt32 np = 0; np < nPlanes; ++np) - { - if ( ny / GetRowsPerStrip() + np * nStripsPerPlane > nStrip ) - { - nStrip=ny/GetRowsPerStrip()+np*nStripsPerPlane; - if (nStrip >= aStripOffsets.size()) - return false; - pTIFF->Seek(aStripOffsets[nStrip]); - } - sal_uInt32 nRowBytesLeft = nBytesPerRow; - if (np >= SAL_N_ELEMENTS(aMap)) - return false; - sal_uInt8* pdst = getMapData(np); - do - { - sal_uInt8 nRecHeader(0); - pTIFF->ReadUChar(nRecHeader); - sal_uInt32 nRecCount; - if ((nRecHeader&0x80)==0) - { - nRecCount=0x00000001 + static_cast<sal_uInt32>(nRecHeader); - if ( nRecCount > nRowBytesLeft ) - return false; - pTIFF->ReadBytes(pdst, nRecCount); - if (!pTIFF->good()) - return false; - pdst+=nRecCount; - nRowBytesLeft-=nRecCount; - } - else if ( nRecHeader != 0x80 ) - { - nRecCount = 0x000000101 - static_cast<sal_uInt32>(nRecHeader); - if ( nRecCount > nRowBytesLeft ) - { - nRecCount = nRowBytesLeft; - } - sal_uInt8 nRecData(0); - pTIFF->ReadUChar( nRecData ); - for (sal_uInt32 i = 0; i < nRecCount; ++i) - *(pdst++) = nRecData; - nRowBytesLeft -= nRecCount; - } - } while ( nRowBytesLeft != 0 ); - if ( pTIFF->GetError() ) - return false; - } - if ( !ConvertScanline( ny ) ) - return false; - } - } - else - return false; - return true; -} - -sal_uInt32 TIFFReader::GetBits( const sal_uInt8 * pSrc, sal_uInt32 nBitsPos, sal_uInt32 nBitsCount) const -{ - sal_uInt32 nRes; - if ( bByteSwap ) - { - pSrc += ( nBitsPos >> 3 ); - nBitsPos &= 7; - sal_uInt8 nDat = *pSrc; - nRes = static_cast<sal_uInt32>( BYTESWAP( nDat ) & ( 0xff >> nBitsPos ) ); - - if ( nBitsCount <= 8 - nBitsPos ) - { - nRes >>= ( 8 - nBitsPos - nBitsCount ); - } - else - { - pSrc++; - nBitsCount -= 8 - nBitsPos; - while ( nBitsCount >= 8 ) - { - nDat = *(pSrc++); - nRes = ( nRes << 8 ) | static_cast<sal_uInt32>(BYTESWAP( nDat )); - nBitsCount -= 8; - } - if ( nBitsCount > 0 ) - { - nDat = *pSrc; - nRes = ( nRes << nBitsCount ) | (static_cast<sal_uInt32>(BYTESWAP(nDat))>>(8-nBitsCount)); - } - } - } - else - { - pSrc += ( nBitsPos >> 3 ); - nBitsPos &= 7; - nRes = static_cast<sal_uInt32>((*pSrc)&(0xff>>nBitsPos)); - if ( nBitsCount <= 8 - nBitsPos ) - { - nRes >>= ( 8 - nBitsPos - nBitsCount ); - } - else - { - pSrc++; - nBitsCount -= 8 - nBitsPos; - while ( nBitsCount >= 8 ) - { - nRes = ( nRes << 8 ) | static_cast<sal_uInt32>(*(pSrc++)); - nBitsCount -= 8; - } - if ( nBitsCount > 0 ) - nRes = ( nRes << nBitsCount ) | (static_cast<sal_uInt32>(*pSrc)>>(8-nBitsCount)); - } - } - return nRes; -} - -void TIFFReader::SetPixel(tools::Long nY, tools::Long nX, sal_uInt8 cIndex) -{ - maBitmap[(maBitmapPixelSize.Width() * nY + nX) * (HasAlphaChannel() ? 4 : 3)] = cIndex; - nLargestPixelIndex = std::max<int>(nLargestPixelIndex, cIndex); -} - -void TIFFReader::SetPixel(tools::Long nY, tools::Long nX, Color c) -{ - auto p = maBitmap.data() + ((maBitmapPixelSize.Width() * nY + nX) * (HasAlphaChannel() ? 4 : 3)); - *p = c.GetRed(); - p++; - *p = c.GetGreen(); - p++; - *p = c.GetBlue(); - if (HasAlphaChannel()) - { - p++; - *p = 0xff; // alpha - } -} - -void TIFFReader::SetPixelAlpha(tools::Long nY, tools::Long nX, sal_uInt8 nAlpha) -{ - assert(HasAlphaChannel()); - maBitmap[((maBitmapPixelSize.Width() * nY + nX) * 4) + 3] = nAlpha; -} - -bool TIFFReader::ConvertScanline(sal_Int32 nY) -{ - sal_uInt32 nRed, nGreen, nBlue, ns, nVal; - sal_uInt8 nByteVal; - - if ( nDstBitsPerPixel == 24 ) - { - if ( nBitsPerSample == 8 && nSamplesPerPixel >= 3 && - nPlanes == 1 && nPhotometricInterpretation == 2 ) - { - sal_uInt8* pt = getMapData(0); - - // are the values being saved as difference? - if ( 2 == nPredictor ) - { - sal_uInt8 nLRed = 0; - sal_uInt8 nLGreen = 0; - sal_uInt8 nLBlue = 0; - sal_uInt8 nLAlpha = 0; - for (sal_Int32 nx = 0; nx < nImageWidth; nx++, pt += nSamplesPerPixel) - { - // The following computations rely on sal_uInt8 wrap-around when adding the - // (unsigned) pt deltas; the "& 0xFF" is only conceptual, but helps prevent - // sanitizer warnings: - nLRed = (nLRed + pt[ 0 ]) & 0xFF; - nLGreen = (nLGreen + pt[ 1 ]) & 0xFF; - nLBlue = (nLBlue + pt[ 2 ]) & 0xFF; - SetPixel(nY, nx, Color(nLRed, nLGreen, nLBlue)); - if (HasAlphaChannel()) - { - nLAlpha = (nLAlpha + pt[ 3 ]) & 0xFF; - SetPixelAlpha(nY, nx, ~nLAlpha); - } - } - } - else - { - for (sal_Int32 nx = 0; nx < nImageWidth; nx++, pt += nSamplesPerPixel) - { - SetPixel(nY, nx, Color(pt[0], pt[1], pt[2])); - if (HasAlphaChannel()) - { - sal_uInt8 nAlpha = pt[3]; - SetPixelAlpha(nY, nx, ~nAlpha); - } - } - } - } - else if ( - ( nPhotometricInterpretation == 2 && nSamplesPerPixel >= 3 ) || - ( nPhotometricInterpretation == 5 && nSamplesPerPixel == 3 ) - ) - { - if ( nMaxSampleValue > nMinSampleValue ) - { - sal_uInt32 nMinMax = nMinSampleValue * 255 / ( nMaxSampleValue - nMinSampleValue ); - for (sal_Int32 nx = 0; nx < nImageWidth; ++nx) - { - if ( nPlanes < 3 ) - { - nRed = GetBits( getMapData(0), ( nx * nSamplesPerPixel + 0 ) * nBitsPerSample, nBitsPerSample ); - nGreen = GetBits( getMapData(0), ( nx * nSamplesPerPixel + 1 ) * nBitsPerSample, nBitsPerSample ); - nBlue = GetBits( getMapData(0), ( nx * nSamplesPerPixel + 2 ) * nBitsPerSample, nBitsPerSample ); - } - else - { - nRed = GetBits( getMapData(0), nx * nBitsPerSample, nBitsPerSample ); - nGreen = GetBits( getMapData(1), nx * nBitsPerSample, nBitsPerSample ); - nBlue = GetBits( getMapData(2), nx * nBitsPerSample, nBitsPerSample ); - } - if (nPhotometricInterpretation == 2) - SetPixel(nY, nx, Color(static_cast<sal_uInt8>(nRed - nMinMax), static_cast<sal_uInt8>(nGreen - nMinMax), static_cast<sal_uInt8>(nBlue - nMinMax))); - else - SetPixel(nY, nx, Color(255 - static_cast<sal_uInt8>(nRed - nMinMax), 255 - static_cast<sal_uInt8>(nGreen - nMinMax), 255 - static_cast<sal_uInt8>(nBlue - nMinMax))); - } - } - } - else if( nPhotometricInterpretation == 5 && nSamplesPerPixel == 4 ) - { - if ( nMaxSampleValue > nMinSampleValue ) - { - sal_uInt8 nSamp[ 4 ]; - sal_uInt8 nSampLast[ 4 ] = { 0, 0, 0, 0 }; - - for(sal_Int32 nx = 0; nx < nImageWidth; ++nx) - { - // are the values being saved as difference? - if( 2 == nPredictor ) - { - for( ns = 0; ns < 4; ns++ ) - { - if( nPlanes < 3 ) - nSampLast[ ns ] = nSampLast[ ns ] + static_cast<sal_uInt8>(GetBits( getMapData(0), ( nx * nSamplesPerPixel + ns ) * nBitsPerSample, nBitsPerSample )); - else - nSampLast[ ns ] = nSampLast[ ns ] + static_cast<sal_uInt8>(GetBits( getMapData(ns), nx * nBitsPerSample, nBitsPerSample )); - nSamp[ ns ] = nSampLast[ ns ]; - } - } - else - { - for( ns = 0; ns < 4; ns++ ) - { - if( nPlanes < 3 ) - nSamp[ ns ] = static_cast<sal_uInt8>(GetBits( getMapData(0), ( nx * nSamplesPerPixel + ns ) * nBitsPerSample, nBitsPerSample )); - else - nSamp[ ns ]= static_cast<sal_uInt8>(GetBits( getMapData(ns), nx * nBitsPerSample, nBitsPerSample )); - } - } - const tools::Long nBlack = nSamp[ 3 ]; - nRed = static_cast<sal_uInt8>(std::max<sal_Int32>( 0, 255 - ( ( static_cast<sal_Int32>(nSamp[ 0 ]) + nBlack - static_cast<sal_Int32>(nMinSampleValue << 1U ) ) * - 255L/static_cast<sal_Int32>(nMaxSampleValue-nMinSampleValue) ) )); - nGreen = static_cast<sal_uInt8>(std::max<sal_Int32>( 0, 255 - ( ( static_cast<sal_Int32>(nSamp[ 1 ]) + nBlack - static_cast<sal_Int32>(nMinSampleValue << 1U ) ) * - 255L/static_cast<sal_Int32>(nMaxSampleValue-nMinSampleValue) ) )); - nBlue = static_cast<sal_uInt8>(std::max<sal_Int32>( 0, 255 - ( ( static_cast<sal_Int32>(nSamp[ 2 ]) + nBlack - static_cast<sal_Int32>(nMinSampleValue << 1U ) ) * - 255L/static_cast<sal_Int32>(nMaxSampleValue-nMinSampleValue) ) )); - SetPixel(nY, nx, Color(static_cast<sal_uInt8>(nRed), static_cast<sal_uInt8>(nGreen), static_cast<sal_uInt8>(nBlue))); - } - } - } - } - else if ( nSamplesPerPixel == 1 && ( nPhotometricInterpretation <= 1 || nPhotometricInterpretation == 3 ) ) - { - if ( nMaxSampleValue > nMinSampleValue ) - { - sal_uInt8* pt = getMapData(0); - sal_uInt8* ptend = pt + nBytesPerRow; - - if (nBitsPerSample > 8) - { - sal_uInt32 nMinMax = nMinSampleValue * 255 / ( nMaxSampleValue - nMinSampleValue ); - for (sal_Int32 nx = 0; nx < nImageWidth; ++nx) - { - nVal = GetBits(pt, nx * nSamplesPerPixel * nBitsPerSample, nBitsPerSample); - SetPixel(nY, nx, static_cast<sal_uInt8>(nVal - nMinMax)); - } - } - else - { - sal_uInt32 nMinMax = ( ( 1 << nDstBitsPerPixel ) - 1 ) / ( nMaxSampleValue - nMinSampleValue ); - sal_uInt8 nShift; - - switch ( nDstBitsPerPixel ) - { - case 8 : - { - if (pt + nImageWidth > ptend) - return false; - - if ( bByteSwap ) - { - if ( nPredictor == 2 ) - { - sal_uInt8 nLast = 0; - for (sal_Int32 nx = 0; nx < nImageWidth; ++nx) - { - nLast += nx == 0 ? BYTESWAP( *pt++ ) : *pt++; - SetPixel(nY, nx, nLast); - } - } - else - { - for (sal_Int32 nx = 0; nx < nImageWidth; ++nx) - { - sal_uInt8 nLast = *pt++; - SetPixel(nY, nx, static_cast<sal_uInt8>( (BYTESWAP(static_cast<sal_uInt32>(nLast)) - nMinSampleValue) * nMinMax )); - } - } - } - else - { - if ( nPredictor == 2 ) - { - sal_uInt8 nLast = 0; - for (sal_Int32 nx = 0; nx < nImageWidth; ++nx) - { - nLast += *pt++; - SetPixel(nY, nx, nLast); - } - } - else - { - for (sal_Int32 nx = 0; nx < nImageWidth; ++nx) - { - SetPixel(nY, nx, static_cast<sal_uInt8>( (static_cast<sal_uInt32>(*pt++) - nMinSampleValue) * nMinMax )); - } - } - } - } - break; - - case 7 : - case 6 : - case 5 : - case 4 : - case 3 : - case 2 : - { - for (sal_Int32 nx = 0; nx < nImageWidth; ++nx) - { - nVal = ( GetBits( pt, nx * nBitsPerSample, nBitsPerSample ) - nMinSampleValue ) * nMinMax; - SetPixel(nY, nx, static_cast<sal_uInt8>(nVal)); - } - } - break; - - case 1 : - { - sal_uInt32 nByteCount = nImageWidth >> 3; - - sal_uInt32 nBytesNeeded = nByteCount; - if (nImageWidth & 7) - ++nBytesNeeded; - if (pt + nBytesNeeded > ptend) - return false; - - if ( bByteSwap ) - { - sal_Int32 nx = 0; - while (nByteCount--) - { - nByteVal = *pt++; - SetPixel(nY, nx++, nByteVal & 1); - nByteVal >>= 1; - SetPixel(nY, nx++, nByteVal & 1); - nByteVal >>= 1; - SetPixel(nY, nx++, nByteVal & 1); - nByteVal >>= 1; - SetPixel(nY, nx++, nByteVal & 1); - nByteVal >>= 1; - SetPixel(nY, nx++, nByteVal & 1); - nByteVal >>= 1; - SetPixel(nY, nx++, nByteVal & 1); - nByteVal >>= 1; - SetPixel(nY, nx++, nByteVal & 1); - nByteVal >>= 1; - SetPixel(nY, nx++, nByteVal); - } - if ( nImageWidth & 7 ) - { - nByteVal = *pt++; - while ( nx < nImageWidth ) - { - SetPixel(nY, nx++, nByteVal & 1); - nByteVal >>= 1; - } - } - } - else - { - sal_Int32 nx = 7; - while (nByteCount--) - { - nByteVal = *pt++; - SetPixel(nY, nx, nByteVal & 1); - nByteVal >>= 1; - SetPixel(nY, --nx, nByteVal & 1); - nByteVal >>= 1; - SetPixel(nY, --nx, nByteVal & 1); - nByteVal >>= 1; - SetPixel(nY, --nx, nByteVal & 1); - nByteVal >>= 1; - SetPixel(nY, --nx, nByteVal & 1); - nByteVal >>= 1; - SetPixel(nY, --nx, nByteVal & 1); - nByteVal >>= 1; - SetPixel(nY, --nx, nByteVal & 1); - nByteVal >>= 1; - SetPixel(nY, --nx, nByteVal); - nx += 15; - } - if ( nImageWidth & 7 ) - { - nx -= 7; - nByteVal = *pt++; - nShift = 7; - while ( nx < nImageWidth ) - { - SetPixel(nY, nx++, ( nByteVal >> nShift ) & 1); - --nShift; - } - } - } - } - break; - - default : - return false; - } - } - } - } - else if ( ( nSamplesPerPixel == 2 ) && ( nBitsPerSample == 8 ) && - ( nPlanarConfiguration == 1 ) && aColorMap.empty() ) // grayscale + alpha - { - if ( nMaxSampleValue > nMinSampleValue ) - { - sal_uInt8* pt = getMapData(0); - - if (nPredictor == 2) - { - sal_uInt8 nLastPixel = 0; - sal_uInt8 nLastAlpha = 0; - for (sal_Int32 nx = 0; nx < nImageWidth; nx++, pt += 2) - { - nLastPixel = (nLastPixel + pt[0]) & 0xFF; - SetPixel(nY, nx, nLastPixel); - - nLastAlpha = (nLastAlpha + pt[1]) & 0xFF; - SetPixelAlpha(nY, nx, ~nLastAlpha); - } - } - else - { - sal_uInt32 nMinMax = ( ( 1 << 8 /*nDstBitsPerPixel*/ ) - 1 ) / ( nMaxSampleValue - nMinSampleValue ); - for (sal_Int32 nx = 0; nx < nImageWidth; nx++, pt += 2) - { - SetPixel(nY, nx, static_cast<sal_uInt8>( (static_cast<sal_uInt32>(pt[0]) - nMinSampleValue) * nMinMax )); - sal_uInt8 nAlpha = static_cast<sal_uInt8>( (static_cast<sal_uInt32>(pt[1]) - nMinSampleValue) * nMinMax ); - SetPixelAlpha(nY, nx, ~nAlpha); - } - } - } - } - else - return false; - return true; -} - -void TIFFReader::MakePalCol() -{ - if ( nDstBitsPerPixel <= 8 ) - { - aColorMap.resize(256); - if ( nPhotometricInterpretation <= 1 ) - { - nNumColors = sal_uInt32(1) << nBitsPerSample; - if ( nNumColors > 256 ) - nNumColors = 256; - - if (nLargestPixelIndex >= static_cast<int>(nNumColors)) - { - SAL_WARN("filter.tiff", "palette has less entries that largest index used. Expanding palette to match"); - nNumColors = nLargestPixelIndex + 1; - } - - for (sal_uInt32 i = 0; i < nNumColors; ++i) - { - sal_uInt32 nVal = ( i * 255 / ( nNumColors - 1 ) ) & 0xff; - sal_uInt32 n0RGB = nVal | ( nVal << 8 ) | ( nVal << 16 ); - if ( nPhotometricInterpretation == 1 ) - aColorMap[i] = n0RGB; - else - aColorMap[nNumColors - i - 1] = n0RGB; - } - } - mvPalette.resize(std::max<sal_uInt16>(nNumColors, mvPalette.size())); - for (sal_uInt32 i = 0; i < nNumColors; ++i) - { - mvPalette[i] = Color( static_cast<sal_uInt8>( aColorMap[ i ] >> 16 ), - static_cast<sal_uInt8>( aColorMap[ i ] >> 8 ), static_cast<sal_uInt8>(aColorMap[ i ]) ); - } - } - - if ( !(fXResolution > 1.0 && fYResolution > 1.0 && ( nResolutionUnit == 2 || nResolutionUnit == 3 )) ) - return; - - sal_uInt32 nRX, nRY; - if (nResolutionUnit==2) - { - nRX=static_cast<sal_uInt32>(fXResolution+0.5); - nRY=static_cast<sal_uInt32>(fYResolution+0.5); - } - else - { - nRX=static_cast<sal_uInt32>(fXResolution*2.54+0.5); - nRY=static_cast<sal_uInt32>(fYResolution*2.54+0.5); - } - MapMode aMapMode(MapUnit::MapInch,Point(0,0),Fraction(1,nRX),Fraction(1,nRY)); - maBitmapPrefMapMode = aMapMode; - maBitmapPrefSize = Size(nImageWidth,nImageLength); -} - - -void TIFFReader::ReadHeader() -{ - sal_uInt8 nbyte1(0), nbyte2(0); - sal_uInt16 nushort(0); - - pTIFF->ReadUChar( nbyte1 ); - if ( nbyte1 == 'I' ) - pTIFF->SetEndian( SvStreamEndian::LITTLE ); - else - pTIFF->SetEndian( SvStreamEndian::BIG ); - - pTIFF->ReadUChar( nbyte2 ).ReadUInt16( nushort ); - if ( nbyte1 != nbyte2 || ( nbyte1 != 'I' && nbyte1 != 'M' ) || nushort != 0x002a ) - bStatus = false; -} - -bool TIFFReader::HasAlphaChannel() const -{ - /*There are undoubtedly more variants we could support, but keep it simple for now*/ - bool bRGBA = nDstBitsPerPixel == 24 && - nBitsPerSample == 8 && - nSamplesPerPixel >= 4 && - nPlanes == 1 && - nPhotometricInterpretation == 2; - if (bRGBA) - return true; - - // additionally support the format used in tdf#126460 - bool bGrayScaleAlpha = nDstBitsPerPixel == 8 && - nBitsPerSample == 8 && - nSamplesPerPixel == 2 && - nPlanarConfiguration == 1; - - return bGrayScaleAlpha; -} - -namespace -{ - Color SanitizePaletteIndex(sal_uInt8 nIndex, const std::vector<Color>& rPalette) - { - const size_t nPaletteEntryCount = rPalette.size(); - if (nPaletteEntryCount && nIndex >= nPaletteEntryCount) - { - auto nSanitizedIndex = nIndex % nPaletteEntryCount; - SAL_WARN_IF(nIndex != nSanitizedIndex, "vcl", "invalid colormap index: " - << static_cast<unsigned int>(nIndex) << ", colormap len is: " - << nPaletteEntryCount); - nIndex = nSanitizedIndex; - } - - return rPalette[nIndex]; - } -} - -bool TIFFReader::ReadTIFF(SvStream & rTIFF, Graphic & rGraphic ) -{ - sal_uInt16 i, nNumTags(0), nTagType(0); - sal_uInt32 nFirstIfd(0), nDataLen; - - bStatus = true; - - pTIFF = &rTIFF; - sal_uInt64 nMaxPos = nOrigPos = pTIFF->Tell(); - nEndOfFile = nOrigPos + pTIFF->remainingSize(); - // number format of pTIFF at the beginning - SvStreamEndian nOrigNumberFormat = pTIFF->GetEndian(); - - // read header: - ReadHeader(); - - // read first IFD: - pTIFF->ReadUInt32( nFirstIfd ); - - if( !nFirstIfd || pTIFF->GetError() ) - bStatus = false; - - if ( bStatus ) - { - sal_uInt32 nOffset = nFirstIfd; - - std::vector<sal_uInt32> aSeenOffsets; - // calculate length of TIFF file - do - { - if (std::find(aSeenOffsets.begin(), aSeenOffsets.end(), nOffset) != aSeenOffsets.end()) - { - SAL_WARN("filter.tiff", "Parsing error: " << nOffset << - " already processed, format loop"); - bStatus = false; - break; - } - pTIFF->Seek(nOrigPos + nOffset); - aSeenOffsets.push_back(nOffset); - - if( pTIFF->GetError() ) - { - pTIFF->ResetError(); - break; - } - nMaxPos = std::max( pTIFF->Tell(), nMaxPos ); - - pTIFF->ReadUInt16( nNumTags ); - - const size_t nMinRecordSize = 12; - const size_t nMaxRecords = pTIFF->remainingSize() / nMinRecordSize; - if (nNumTags > nMaxRecords) - { - SAL_WARN("filter.tiff", "Parsing error: " << nMaxRecords << - " max possible entries, but " << nNumTags << " claimed, truncating"); - nNumTags = nMaxRecords; - } - - // loop through tags: - for( i = 0; i < nNumTags; i++ ) - { - nTagType = 0; - nDataType = USHRT_MAX; - nDataLen = 0; - nOffset = 0; - pTIFF->ReadUInt16( nTagType ).ReadUInt16( nDataType ).ReadUInt32( nDataLen ).ReadUInt32( nOffset ); - - if( DataTypeSize() * nDataLen > 4 ) - nMaxPos = std::max(nOrigPos + nOffset + DataTypeSize() * nDataLen, nMaxPos); - } - pTIFF->ReadUInt32( nOffset ); - if (!pTIFF->good()) - nOffset = 0; - - nMaxPos = std::max( pTIFF->Tell(), nMaxPos ); - if ( !nOffset ) - nMaxPos = std::max( pTIFF->Tell(), nMaxPos ); - } - while( nOffset ); - - std::vector<sal_uInt32> aSeenIfds; - - for ( sal_uInt32 nNextIfd = nFirstIfd; nNextIfd && bStatus; ) - { - if (std::find(aSeenIfds.begin(), aSeenIfds.end(), nNextIfd) != aSeenIfds.end()) - { - SAL_WARN("filter.tiff", "Parsing error: " << nNextIfd << - " already processed, format loop"); - bStatus = false; - break; - } - pTIFF->Seek(nOrigPos + nNextIfd); - aSeenIfds.push_back(nNextIfd); - { - bByteSwap = false; - - nNewSubFile = 0; - nSubFile = 0; - nImageWidth = 0; - nImageLength = 0; - nBitsPerSample = 1; // default value according to the documentation - nCompression = 1; - nPhotometricInterpretation = 0; - nThresholding = 1; // default value according to the documentation - nCellWidth = 1; - nCellLength = 1; - nFillOrder = 1; // default value according to the documentation - nOrientation = 1; - nSamplesPerPixel = 1; // default value according to the documentation - nRowsPerStrip = 0xffffffff; // default value according to the documentation - nMinSampleValue = 0; // default value according to the documentation - nMaxSampleValue = 0; - fXResolution = 0.0; - fYResolution = 0.0; - nPlanarConfiguration = 1; - nGroup3Options = 0; // default value according to the documentation - nGroup4Options = 0; // default value according to the documentation - nResolutionUnit = 2; // default value according to the documentation - nPredictor = 1; - nNumColors = 0; - - aStripOffsets.clear(); - aStripByteCounts.clear(); - for (auto& j : aMap) - j.clear(); - - pTIFF->ReadUInt16( nNumTags ); - sal_uInt64 nPos = pTIFF->Tell(); - - const size_t nMinRecordSize = 8; - const size_t nMaxRecords = pTIFF->remainingSize() / nMinRecordSize; - if (nNumTags > nMaxRecords) - { - SAL_WARN("filter.tiff", "Parsing error: " << nMaxRecords << - " max possible entries, but " << nNumTags << " claimed, truncating"); - nNumTags = nMaxRecords; - } - - for( i = 0; i < nNumTags; i++ ) - { - pTIFF->ReadUInt16( nTagType ).ReadUInt16( nDataType ).ReadUInt32( nDataLen ); - - if( DataTypeSize() * nDataLen > 4 ) - { - pTIFF->ReadUInt32( nOffset ); - if (!checkSeek(*pTIFF, nOrigPos + nOffset)) - { - bStatus = false; - break; - } - } - ReadTagData( nTagType, nDataLen ); - nPos += 12; pTIFF->Seek( nPos ); - - if ( pTIFF->GetError() ) - bStatus = false; - - if ( !bStatus ) - break; - } - pTIFF->ReadUInt32( nNextIfd ); - if (!pTIFF->good()) - nNextIfd = 0; - } - if ( !nBitsPerSample || ( nBitsPerSample > 32 ) ) - bStatus = false; - if (nImageWidth <= 0 || nImageLength <= 0) - bStatus = false; - if ( bStatus ) - { - nLargestPixelIndex = -1; - if ( nMaxSampleValue == 0 ) - { - if ( nBitsPerSample == 32 ) // sj: i93300, compiler bug, 1 << 32 gives 1 one 32bit windows platforms, - nMaxSampleValue = 0xffffffff; // (up from 80286 only the lower 5 bits are used when shifting a 32bit register) - else - { - nMaxSampleValue = (1U << nBitsPerSample) - 1; - } - } - if ( nPhotometricInterpretation == 2 || nPhotometricInterpretation == 5 || nPhotometricInterpretation == 6 ) - nDstBitsPerPixel = 24; - else if ( nBitsPerSample*nSamplesPerPixel <= 1 ) - nDstBitsPerPixel = 1; - else if ( nBitsPerSample*nSamplesPerPixel <= 4 ) - nDstBitsPerPixel = 4; - else - nDstBitsPerPixel = 8; - - if ( nPlanarConfiguration == 1 ) - nPlanes = 1; - else - nPlanes = nSamplesPerPixel; - - bStatus = nPlanes != 0; - } - - sal_uInt32 nDiv = GetRowsPerStrip(); - - if ( bStatus ) - { - bStatus = (nDiv != 0); - } - - if ( bStatus ) - { - if ( ( nFillOrder == 2 ) && ( nCompression != 5 ) ) // in the LZW mode bits are already being inverted - bByteSwap = true; - nStripsPerPlane = ( nImageLength - 1 ) / nDiv + 1; - bStatus = nSamplesPerPixel != 0; - } - - if ( bStatus ) - { - sal_uInt64 nRowSize = (static_cast<sal_uInt64>(nImageWidth) * nSamplesPerPixel / nPlanes * nBitsPerSample + 7) >> 3; - auto nMaxSize = SAL_MAX_INT32 / SAL_N_ELEMENTS(aMap); - if (utl::ConfigManager::IsFuzzing()) - nMaxSize /= 2; - if (nRowSize > nMaxSize) - { - SAL_WARN("filter.tiff", "Ludicrous row size of: " << nRowSize << " required"); - bStatus = false; - } - else - nBytesPerRow = nRowSize; - } - - if (bStatus) - { - //sanity check consider ReadMap condition for last row and - //last plane - if (nCompression == 1 || nCompression == 32771) - { - sal_uInt32 nStripBytesPerRow; - if (nCompression == 1) - nStripBytesPerRow = nBytesPerRow; - else - nStripBytesPerRow = ( nBytesPerRow + 1 ) & 0xfffffffe; - sal_uInt32 np = nPlanes - 1; - if (np >= SAL_N_ELEMENTS(aMap)) - bStatus = false; - sal_Int32 ny = nImageLength - 1; - sal_uInt32 nStrip(0); - nDiv = GetRowsPerStrip(); - if (bStatus) - bStatus = nDiv != 0; - if (bStatus) - { - nStrip = ny / nDiv + np * nStripsPerPlane; - if (nStrip >= aStripOffsets.size()) - bStatus = false; - } - if (bStatus) - { - auto nStart = aStripOffsets[ nStrip ] + ( ny % GetRowsPerStrip() ) * nStripBytesPerRow; - if (nStart > nEndOfFile) - bStatus = false; - } - } - else if (nCompression == 2 || nCompression == 3 || nCompression == 4) - { - if (nCompression == 3 && nGroup3Options & 0xfffffffa) - bStatus = false; - else if (nCompression == 4 && nGroup4Options & 0xffffffff) - bStatus = false; - sal_uInt32 np = nPlanes - 1; - if (np >= SAL_N_ELEMENTS(aMap)) - bStatus = false; - sal_Int32 ny = nImageLength - 1; - sal_uInt32 nStrip(0); - nDiv = GetRowsPerStrip(); - if (bStatus) - bStatus = nDiv != 0; - if (bStatus) - { - nStrip = ny / nDiv + np * nStripsPerPlane; - if (nStrip >= aStripOffsets.size()) - bStatus = false; - } - if (bStatus) - { - auto nStart = aStripOffsets[nStrip]; - if (nStart > nEndOfFile) - bStatus = false; - } - - if (bStatus) - { - sal_uInt64 nTargetBits = nImageWidth * nBitsPerSample * nSamplesPerPixel / nPlanes; - if (nTargetBits > SAL_MAX_UINT16) - bStatus = false; - } - } - else if (nCompression == 5) - { - sal_uInt32 np = nPlanes - 1; - if (np >= SAL_N_ELEMENTS(aMap)) - bStatus = false; - sal_Int32 ny = nImageLength - 1; - sal_uInt32 nStrip(0); - nDiv = GetRowsPerStrip(); - if (bStatus) - bStatus = nDiv != 0; - if (bStatus) - { - nStrip = ny / nDiv + np * nStripsPerPlane; - if (nStrip >= aStripOffsets.size()) - bStatus = false; - } - if (bStatus) - { - auto nStart = aStripOffsets[nStrip]; - if (nStart > nEndOfFile) - bStatus = false; - } - } - else if (nCompression == 32773) - { - } - else - { - bStatus = false; - } - } - - sal_Int32 nImageDataSize(0); - if (bStatus) - { - if (o3tl::checked_multiply<sal_Int32>(nImageWidth, nImageLength, nImageDataSize) || - o3tl::checked_multiply<sal_Int32>(nImageDataSize, (HasAlphaChannel() ? 4 : 3), nImageDataSize) || - nImageDataSize > SAL_MAX_INT32/4) - { - bStatus = false; - } - } - - if (bStatus) - { - sal_Int32 nResult = 0; - if (utl::ConfigManager::IsFuzzing() && (o3tl::checked_multiply(nImageWidth, nImageLength, nResult) || nResult > 4000000)) - bStatus = false; - } - - if ( bStatus ) - { - maBitmapPixelSize = Size(nImageWidth, nImageLength); - maBitmap.resize(nImageDataSize, 0); - - if (bStatus && ReadMap()) - { - nMaxPos = std::max( pTIFF->Tell(), nMaxPos ); - MakePalCol(); - nMaxPos = std::max( pTIFF->Tell(), nMaxPos ); - // convert palette-ized images to 24-bit color - if (!mvPalette.empty()) - { - for (sal_Int32 nY = 0; nY < nImageLength; ++nY) - { - for (sal_Int32 nX = 0; nX < nImageWidth; ++nX) - { - auto p = maBitmap.data() + ((maBitmapPixelSize.Width() * nY + nX) * (HasAlphaChannel() ? 4 : 3)); - auto c = SanitizePaletteIndex(*p, mvPalette); - *p = c.GetRed(); - p++; - *p = c.GetGreen(); - p++; - *p = c.GetBlue(); - } - } - } - } - else - bStatus = false; - - if ( bStatus ) - { - BitmapEx aImage = vcl::bitmap::CreateFromData(maBitmap.data(), nImageWidth, nImageLength, - nImageWidth * (HasAlphaChannel() ? 4 : 3), // scanline bytes - HasAlphaChannel() ? vcl::PixelFormat::N32_BPP : vcl::PixelFormat::N24_BPP); - aImage.SetPrefMapMode(maBitmapPrefMapMode); - aImage.SetPrefSize(maBitmapPrefSize); - - AnimationBitmap aAnimationBitmap( aImage, Point( 0, 0 ), maBitmapPixelSize, - ANIMATION_TIMEOUT_ON_CLICK, Disposal::Back ); - - aAnimation.Insert( aAnimationBitmap ); - } - } - - // Clean up: - for (auto& j : aMap) - j.clear(); - aColorMap.clear(); - aStripOffsets.clear(); - aStripByteCounts.clear(); - } - } - - // seek to end of TIFF if succeeded - pTIFF->SetEndian( nOrigNumberFormat ); - pTIFF->Seek(bStatus ? STREAM_SEEK_TO_END: nOrigPos); - - if ( aAnimation.Count() ) - { - if ( aAnimation.Count() == 1 ) - rGraphic = aAnimation.GetBitmapEx(); - else - rGraphic = aAnimation; //aBitmap; - - return true; - } - else - return false; -} - -bool ImportTiffGraphicImport(SvStream & rStream, Graphic & rGraphic) -{ - TIFFReader aTIFFReader; - try - { - return aTIFFReader.ReadTIFF(rStream, rGraphic); - } - catch (const std::bad_alloc &) - { - return false; - } -} -#endif /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/filter/itiff/lzwdecom.cxx b/vcl/source/filter/itiff/lzwdecom.cxx deleted file mode 100644 index cf2ed67abc4e..000000000000 --- a/vcl/source/filter/itiff/lzwdecom.cxx +++ /dev/null @@ -1,215 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * This file incorporates work covered by the following license notice: - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed - * with this work for additional information regarding copyright - * ownership. The ASF licenses this file to you under the Apache - * License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.apache.org/licenses/LICENSE-2.0 . - */ - - -#include "lzwdecom.hxx" -#include <sal/log.hxx> -#include <tools/stream.hxx> - -constexpr sal_Int16 CODE_CLEAR = 256; -constexpr sal_Int16 CODE_EOI = 257; -constexpr sal_Int16 CODE_FIRSTMULTICHAR = 258; - -LZWDecompressor::LZWDecompressor() - : m_pIStream(nullptr) - , m_aTable{{}} - , m_nTableCurrentId(0) - , m_bEOIFound(false) - , m_bInvert(false) - , m_nOldCode(0) - , m_pOutBufData(nullptr) - , m_nOutBufDataLen(0) - , m_nInputBitsBuf(0) - , m_nInputBitsBufSize(0) -{ -} - - -void LZWDecompressor::StartDecompression(SvStream & rIStream) -{ - for (sal_uInt16 i=0; i<MAX_TABLE_SIZE; i++) - { - m_aTable[i].nPrevCode = 0; - m_aTable[i].nDataCount = 1; - m_aTable[i].nData = static_cast<sal_uInt8>(i); - } - m_pIStream = &rIStream; - - m_nTableCurrentId = CODE_FIRSTMULTICHAR; - - m_bEOIFound = false; - - m_nOldCode = 0; - m_pOutBufData = nullptr; - - m_nOutBufDataLen = 0; - - m_pIStream->ReadUChar( m_nInputBitsBuf ); - - m_nInputBitsBufSize=8; - - m_bInvert = m_nInputBitsBuf == 1; - - if ( m_bInvert ) - { - m_nInputBitsBuf = ( ( m_nInputBitsBuf & 1 ) << 7 ) | ( ( m_nInputBitsBuf & 2 ) << 5 ) | ( ( m_nInputBitsBuf & 4 ) << 3 ) | ( ( m_nInputBitsBuf & 8 ) << 1 ) | ( ( m_nInputBitsBuf & 16 ) >> 1 ) | ( ( m_nInputBitsBuf & 32 ) >> 3 ) | ( ( m_nInputBitsBuf & 64 ) >> 5 ) | ( (m_nInputBitsBuf & 128 ) >> 7 ); - } -} - - -sal_uInt64 LZWDecompressor::Decompress(sal_uInt8 * pTarget, sal_uInt32 nMaxCount) -{ - if (m_pIStream == nullptr) return 0; - - sal_uInt32 nCount=0; - for (;;) { - - if (m_pIStream->GetError()) break; - - if (m_nOutBufDataLen >= nMaxCount) { - m_nOutBufDataLen = m_nOutBufDataLen - static_cast<sal_uInt16>(nMaxCount); - nCount += nMaxCount; - while (nMaxCount > 0) { - *(pTarget++)=*(m_pOutBufData++); - nMaxCount--; - } - break; - } - - nMaxCount -= static_cast<sal_uInt32>(m_nOutBufDataLen); - nCount += m_nOutBufDataLen; - while (m_nOutBufDataLen>0) { - *(pTarget++) = *(m_pOutBufData++); - m_nOutBufDataLen--; - } - - if (m_bEOIFound) break; - - DecompressSome(); - - } - - return nCount; -} - - -sal_uInt16 LZWDecompressor::GetNextCode() -{ - sal_uInt16 nBits; - - if (m_nTableCurrentId<511) nBits = 9; - else if (m_nTableCurrentId<1023) nBits = 10; - else if (m_nTableCurrentId<2047) nBits = 11; - else nBits = 12; - - sal_uInt16 nCode = 0; - do { - if (m_nInputBitsBufSize <= nBits) - { - nCode = (nCode << m_nInputBitsBufSize) | m_nInputBitsBuf; - nBits = nBits - m_nInputBitsBufSize; - m_pIStream->ReadUChar( m_nInputBitsBuf ); - if ( m_bInvert ) - { - m_nInputBitsBuf = ( ( m_nInputBitsBuf & 1 ) << 7 ) | ( ( m_nInputBitsBuf & 2 ) << 5 ) | ( ( m_nInputBitsBuf & 4 ) << 3 ) | ( ( m_nInputBitsBuf & 8 ) << 1 ) | ( ( m_nInputBitsBuf & 16 ) >> 1 ) | ( ( m_nInputBitsBuf & 32 ) >> 3 ) | ( ( m_nInputBitsBuf & 64 ) >> 5 ) | ( (m_nInputBitsBuf & 128 ) >> 7 ); - } - m_nInputBitsBufSize=8; - } - else - { - nCode = (nCode<<nBits) | ( m_nInputBitsBuf >> (m_nInputBitsBufSize-nBits) ); - m_nInputBitsBufSize = m_nInputBitsBufSize - nBits; - m_nInputBitsBuf &= 0x00ff >> (8 - m_nInputBitsBufSize); - nBits = 0; - } - } while (nBits > 0); - - return nCode; -} - - -void LZWDecompressor::AddToTable(sal_uInt16 nPrevCode, sal_uInt16 nCodeFirstData) -{ - if (m_nTableCurrentId >= MAX_TABLE_SIZE) - { - //It might be possible to force emit a 256 to flush the buffer and try - //to continue later? - SAL_WARN("filter.tiff", "Too much data at scanline"); - m_bEOIFound = true; - return; - } - - unsigned char aSeenIndexes[MAX_TABLE_SIZE] = {0}; - while (m_aTable[nCodeFirstData].nDataCount > 1) - { - if (aSeenIndexes[nCodeFirstData]) - { - SAL_WARN("filter.tiff", "Loop in chain"); - m_bEOIFound = true; - return; - } - aSeenIndexes[nCodeFirstData] = 1; - nCodeFirstData = m_aTable[nCodeFirstData].nPrevCode; - } - - m_aTable[m_nTableCurrentId].nPrevCode = nPrevCode; - m_aTable[m_nTableCurrentId].nDataCount = m_aTable[nPrevCode].nDataCount+1; - m_aTable[m_nTableCurrentId].nData = m_aTable[nCodeFirstData].nData; - - m_nTableCurrentId++; -} - - -void LZWDecompressor::DecompressSome() -{ - sal_uInt16 nCode = GetNextCode(); - if (nCode == CODE_CLEAR) - { - m_nTableCurrentId = CODE_FIRSTMULTICHAR; - nCode = GetNextCode(); - if (nCode == CODE_EOI) - { - m_bEOIFound = true; - } - } - else if (nCode < m_nTableCurrentId) - AddToTable(m_nOldCode, nCode); - else if (nCode == m_nTableCurrentId) - AddToTable(m_nOldCode, m_nOldCode); - else - { - m_bEOIFound = true; - } - - if (m_bEOIFound) - return; - - m_nOldCode = nCode; - - m_nOutBufDataLen = m_aTable[nCode].nDataCount; - m_pOutBufData = m_pOutBuf.data() + m_nOutBufDataLen; - for (sal_uInt16 i = 0; i < m_nOutBufDataLen; i++) - { - *(--m_pOutBufData) = m_aTable[nCode].nData; - nCode = m_aTable[nCode].nPrevCode; - } -} - - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |