ccitt.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.CCITTFaxDecoder = void 0;
  27. var _util = require("../shared/util.js");
  28. const ccittEOL = -2;
  29. const ccittEOF = -1;
  30. const twoDimPass = 0;
  31. const twoDimHoriz = 1;
  32. const twoDimVert0 = 2;
  33. const twoDimVertR1 = 3;
  34. const twoDimVertL1 = 4;
  35. const twoDimVertR2 = 5;
  36. const twoDimVertL2 = 6;
  37. const twoDimVertR3 = 7;
  38. const twoDimVertL3 = 8;
  39. const twoDimTable = [[-1, -1], [-1, -1], [7, twoDimVertL3], [7, twoDimVertR3], [6, twoDimVertL2], [6, twoDimVertL2], [6, twoDimVertR2], [6, twoDimVertR2], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0]];
  40. const whiteTable1 = [[-1, -1], [12, ccittEOL], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [11, 1792], [11, 1792], [12, 1984], [12, 2048], [12, 2112], [12, 2176], [12, 2240], [12, 2304], [11, 1856], [11, 1856], [11, 1920], [11, 1920], [12, 2368], [12, 2432], [12, 2496], [12, 2560]];
  41. const whiteTable2 = [[-1, -1], [-1, -1], [-1, -1], [-1, -1], [8, 29], [8, 29], [8, 30], [8, 30], [8, 45], [8, 45], [8, 46], [8, 46], [7, 22], [7, 22], [7, 22], [7, 22], [7, 23], [7, 23], [7, 23], [7, 23], [8, 47], [8, 47], [8, 48], [8, 48], [6, 13], [6, 13], [6, 13], [6, 13], [6, 13], [6, 13], [6, 13], [6, 13], [7, 20], [7, 20], [7, 20], [7, 20], [8, 33], [8, 33], [8, 34], [8, 34], [8, 35], [8, 35], [8, 36], [8, 36], [8, 37], [8, 37], [8, 38], [8, 38], [7, 19], [7, 19], [7, 19], [7, 19], [8, 31], [8, 31], [8, 32], [8, 32], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 12], [6, 12], [6, 12], [6, 12], [6, 12], [6, 12], [6, 12], [6, 12], [8, 53], [8, 53], [8, 54], [8, 54], [7, 26], [7, 26], [7, 26], [7, 26], [8, 39], [8, 39], [8, 40], [8, 40], [8, 41], [8, 41], [8, 42], [8, 42], [8, 43], [8, 43], [8, 44], [8, 44], [7, 21], [7, 21], [7, 21], [7, 21], [7, 28], [7, 28], [7, 28], [7, 28], [8, 61], [8, 61], [8, 62], [8, 62], [8, 63], [8, 63], [8, 0], [8, 0], [8, 320], [8, 320], [8, 384], [8, 384], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [7, 27], [7, 27], [7, 27], [7, 27], [8, 59], [8, 59], [8, 60], [8, 60], [9, 1472], [9, 1536], [9, 1600], [9, 1728], [7, 18], [7, 18], [7, 18], [7, 18], [7, 24], [7, 24], [7, 24], [7, 24], [8, 49], [8, 49], [8, 50], [8, 50], [8, 51], [8, 51], [8, 52], [8, 52], [7, 25], [7, 25], [7, 25], [7, 25], [8, 55], [8, 55], [8, 56], [8, 56], [8, 57], [8, 57], [8, 58], [8, 58], [6, 192], [6, 192], [6, 192], [6, 192], [6, 192], [6, 192], [6, 192], [6, 192], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [8, 448], [8, 448], [8, 512], [8, 512], [9, 704], [9, 768], [8, 640], [8, 640], [8, 576], [8, 576], [9, 832], [9, 896], [9, 960], [9, 1024], [9, 1088], [9, 1152], [9, 1216], [9, 1280], [9, 1344], [9, 1408], [7, 256], [7, 256], [7, 256], [7, 256], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [6, 16], [6, 16], [6, 16], [6, 16], [6, 16], [6, 16], [6, 16], [6, 16], [6, 17], [6, 17], [6, 17], [6, 17], [6, 17], [6, 17], [6, 17], [6, 17], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [6, 14], [6, 14], [6, 14], [6, 14], [6, 14], [6, 14], [6, 14], [6, 14], [6, 15], [6, 15], [6, 15], [6, 15], [6, 15], [6, 15], [6, 15], [6, 15], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7]];
  42. const blackTable1 = [[-1, -1], [-1, -1], [12, ccittEOL], [12, ccittEOL], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [11, 1792], [11, 1792], [11, 1792], [11, 1792], [12, 1984], [12, 1984], [12, 2048], [12, 2048], [12, 2112], [12, 2112], [12, 2176], [12, 2176], [12, 2240], [12, 2240], [12, 2304], [12, 2304], [11, 1856], [11, 1856], [11, 1856], [11, 1856], [11, 1920], [11, 1920], [11, 1920], [11, 1920], [12, 2368], [12, 2368], [12, 2432], [12, 2432], [12, 2496], [12, 2496], [12, 2560], [12, 2560], [10, 18], [10, 18], [10, 18], [10, 18], [10, 18], [10, 18], [10, 18], [10, 18], [12, 52], [12, 52], [13, 640], [13, 704], [13, 768], [13, 832], [12, 55], [12, 55], [12, 56], [12, 56], [13, 1280], [13, 1344], [13, 1408], [13, 1472], [12, 59], [12, 59], [12, 60], [12, 60], [13, 1536], [13, 1600], [11, 24], [11, 24], [11, 24], [11, 24], [11, 25], [11, 25], [11, 25], [11, 25], [13, 1664], [13, 1728], [12, 320], [12, 320], [12, 384], [12, 384], [12, 448], [12, 448], [13, 512], [13, 576], [12, 53], [12, 53], [12, 54], [12, 54], [13, 896], [13, 960], [13, 1024], [13, 1088], [13, 1152], [13, 1216], [10, 64], [10, 64], [10, 64], [10, 64], [10, 64], [10, 64], [10, 64], [10, 64]];
  43. const blackTable2 = [[8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [11, 23], [11, 23], [12, 50], [12, 51], [12, 44], [12, 45], [12, 46], [12, 47], [12, 57], [12, 58], [12, 61], [12, 256], [10, 16], [10, 16], [10, 16], [10, 16], [10, 17], [10, 17], [10, 17], [10, 17], [12, 48], [12, 49], [12, 62], [12, 63], [12, 30], [12, 31], [12, 32], [12, 33], [12, 40], [12, 41], [11, 22], [11, 22], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [9, 15], [9, 15], [9, 15], [9, 15], [9, 15], [9, 15], [9, 15], [9, 15], [12, 128], [12, 192], [12, 26], [12, 27], [12, 28], [12, 29], [11, 19], [11, 19], [11, 20], [11, 20], [12, 34], [12, 35], [12, 36], [12, 37], [12, 38], [12, 39], [11, 21], [11, 21], [12, 42], [12, 43], [10, 0], [10, 0], [10, 0], [10, 0], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12]];
  44. const blackTable3 = [[-1, -1], [-1, -1], [-1, -1], [-1, -1], [6, 9], [6, 8], [5, 7], [5, 7], [4, 6], [4, 6], [4, 6], [4, 6], [4, 5], [4, 5], [4, 5], [4, 5], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2]];
  45. class CCITTFaxDecoder {
  46. constructor(source, options = {}) {
  47. if (!source || typeof source.next !== "function") {
  48. throw new Error('CCITTFaxDecoder - invalid "source" parameter.');
  49. }
  50. this.source = source;
  51. this.eof = false;
  52. this.encoding = options.K || 0;
  53. this.eoline = options.EndOfLine || false;
  54. this.byteAlign = options.EncodedByteAlign || false;
  55. this.columns = options.Columns || 1728;
  56. this.rows = options.Rows || 0;
  57. let eoblock = options.EndOfBlock;
  58. if (eoblock === null || eoblock === undefined) {
  59. eoblock = true;
  60. }
  61. this.eoblock = eoblock;
  62. this.black = options.BlackIs1 || false;
  63. this.codingLine = new Uint32Array(this.columns + 1);
  64. this.refLine = new Uint32Array(this.columns + 2);
  65. this.codingLine[0] = this.columns;
  66. this.codingPos = 0;
  67. this.row = 0;
  68. this.nextLine2D = this.encoding < 0;
  69. this.inputBits = 0;
  70. this.inputBuf = 0;
  71. this.outputBits = 0;
  72. this.rowsDone = false;
  73. let code1;
  74. while ((code1 = this._lookBits(12)) === 0) {
  75. this._eatBits(1);
  76. }
  77. if (code1 === 1) {
  78. this._eatBits(12);
  79. }
  80. if (this.encoding > 0) {
  81. this.nextLine2D = !this._lookBits(1);
  82. this._eatBits(1);
  83. }
  84. }
  85. readNextChar() {
  86. if (this.eof) {
  87. return -1;
  88. }
  89. const refLine = this.refLine;
  90. const codingLine = this.codingLine;
  91. const columns = this.columns;
  92. let refPos, blackPixels, bits, i;
  93. if (this.outputBits === 0) {
  94. if (this.rowsDone) {
  95. this.eof = true;
  96. }
  97. if (this.eof) {
  98. return -1;
  99. }
  100. this.err = false;
  101. let code1, code2, code3;
  102. if (this.nextLine2D) {
  103. for (i = 0; codingLine[i] < columns; ++i) {
  104. refLine[i] = codingLine[i];
  105. }
  106. refLine[i++] = columns;
  107. refLine[i] = columns;
  108. codingLine[0] = 0;
  109. this.codingPos = 0;
  110. refPos = 0;
  111. blackPixels = 0;
  112. while (codingLine[this.codingPos] < columns) {
  113. code1 = this._getTwoDimCode();
  114. switch (code1) {
  115. case twoDimPass:
  116. this._addPixels(refLine[refPos + 1], blackPixels);
  117. if (refLine[refPos + 1] < columns) {
  118. refPos += 2;
  119. }
  120. break;
  121. case twoDimHoriz:
  122. code1 = code2 = 0;
  123. if (blackPixels) {
  124. do {
  125. code1 += code3 = this._getBlackCode();
  126. } while (code3 >= 64);
  127. do {
  128. code2 += code3 = this._getWhiteCode();
  129. } while (code3 >= 64);
  130. } else {
  131. do {
  132. code1 += code3 = this._getWhiteCode();
  133. } while (code3 >= 64);
  134. do {
  135. code2 += code3 = this._getBlackCode();
  136. } while (code3 >= 64);
  137. }
  138. this._addPixels(codingLine[this.codingPos] + code1, blackPixels);
  139. if (codingLine[this.codingPos] < columns) {
  140. this._addPixels(codingLine[this.codingPos] + code2, blackPixels ^ 1);
  141. }
  142. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  143. refPos += 2;
  144. }
  145. break;
  146. case twoDimVertR3:
  147. this._addPixels(refLine[refPos] + 3, blackPixels);
  148. blackPixels ^= 1;
  149. if (codingLine[this.codingPos] < columns) {
  150. ++refPos;
  151. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  152. refPos += 2;
  153. }
  154. }
  155. break;
  156. case twoDimVertR2:
  157. this._addPixels(refLine[refPos] + 2, blackPixels);
  158. blackPixels ^= 1;
  159. if (codingLine[this.codingPos] < columns) {
  160. ++refPos;
  161. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  162. refPos += 2;
  163. }
  164. }
  165. break;
  166. case twoDimVertR1:
  167. this._addPixels(refLine[refPos] + 1, blackPixels);
  168. blackPixels ^= 1;
  169. if (codingLine[this.codingPos] < columns) {
  170. ++refPos;
  171. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  172. refPos += 2;
  173. }
  174. }
  175. break;
  176. case twoDimVert0:
  177. this._addPixels(refLine[refPos], blackPixels);
  178. blackPixels ^= 1;
  179. if (codingLine[this.codingPos] < columns) {
  180. ++refPos;
  181. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  182. refPos += 2;
  183. }
  184. }
  185. break;
  186. case twoDimVertL3:
  187. this._addPixelsNeg(refLine[refPos] - 3, blackPixels);
  188. blackPixels ^= 1;
  189. if (codingLine[this.codingPos] < columns) {
  190. if (refPos > 0) {
  191. --refPos;
  192. } else {
  193. ++refPos;
  194. }
  195. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  196. refPos += 2;
  197. }
  198. }
  199. break;
  200. case twoDimVertL2:
  201. this._addPixelsNeg(refLine[refPos] - 2, blackPixels);
  202. blackPixels ^= 1;
  203. if (codingLine[this.codingPos] < columns) {
  204. if (refPos > 0) {
  205. --refPos;
  206. } else {
  207. ++refPos;
  208. }
  209. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  210. refPos += 2;
  211. }
  212. }
  213. break;
  214. case twoDimVertL1:
  215. this._addPixelsNeg(refLine[refPos] - 1, blackPixels);
  216. blackPixels ^= 1;
  217. if (codingLine[this.codingPos] < columns) {
  218. if (refPos > 0) {
  219. --refPos;
  220. } else {
  221. ++refPos;
  222. }
  223. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  224. refPos += 2;
  225. }
  226. }
  227. break;
  228. case ccittEOF:
  229. this._addPixels(columns, 0);
  230. this.eof = true;
  231. break;
  232. default:
  233. (0, _util.info)("bad 2d code");
  234. this._addPixels(columns, 0);
  235. this.err = true;
  236. }
  237. }
  238. } else {
  239. codingLine[0] = 0;
  240. this.codingPos = 0;
  241. blackPixels = 0;
  242. while (codingLine[this.codingPos] < columns) {
  243. code1 = 0;
  244. if (blackPixels) {
  245. do {
  246. code1 += code3 = this._getBlackCode();
  247. } while (code3 >= 64);
  248. } else {
  249. do {
  250. code1 += code3 = this._getWhiteCode();
  251. } while (code3 >= 64);
  252. }
  253. this._addPixels(codingLine[this.codingPos] + code1, blackPixels);
  254. blackPixels ^= 1;
  255. }
  256. }
  257. let gotEOL = false;
  258. if (this.byteAlign) {
  259. this.inputBits &= ~7;
  260. }
  261. if (!this.eoblock && this.row === this.rows - 1) {
  262. this.rowsDone = true;
  263. } else {
  264. code1 = this._lookBits(12);
  265. if (this.eoline) {
  266. while (code1 !== ccittEOF && code1 !== 1) {
  267. this._eatBits(1);
  268. code1 = this._lookBits(12);
  269. }
  270. } else {
  271. while (code1 === 0) {
  272. this._eatBits(1);
  273. code1 = this._lookBits(12);
  274. }
  275. }
  276. if (code1 === 1) {
  277. this._eatBits(12);
  278. gotEOL = true;
  279. } else if (code1 === ccittEOF) {
  280. this.eof = true;
  281. }
  282. }
  283. if (!this.eof && this.encoding > 0 && !this.rowsDone) {
  284. this.nextLine2D = !this._lookBits(1);
  285. this._eatBits(1);
  286. }
  287. if (this.eoblock && gotEOL && this.byteAlign) {
  288. code1 = this._lookBits(12);
  289. if (code1 === 1) {
  290. this._eatBits(12);
  291. if (this.encoding > 0) {
  292. this._lookBits(1);
  293. this._eatBits(1);
  294. }
  295. if (this.encoding >= 0) {
  296. for (i = 0; i < 4; ++i) {
  297. code1 = this._lookBits(12);
  298. if (code1 !== 1) {
  299. (0, _util.info)("bad rtc code: " + code1);
  300. }
  301. this._eatBits(12);
  302. if (this.encoding > 0) {
  303. this._lookBits(1);
  304. this._eatBits(1);
  305. }
  306. }
  307. }
  308. this.eof = true;
  309. }
  310. } else if (this.err && this.eoline) {
  311. while (true) {
  312. code1 = this._lookBits(13);
  313. if (code1 === ccittEOF) {
  314. this.eof = true;
  315. return -1;
  316. }
  317. if (code1 >> 1 === 1) {
  318. break;
  319. }
  320. this._eatBits(1);
  321. }
  322. this._eatBits(12);
  323. if (this.encoding > 0) {
  324. this._eatBits(1);
  325. this.nextLine2D = !(code1 & 1);
  326. }
  327. }
  328. if (codingLine[0] > 0) {
  329. this.outputBits = codingLine[this.codingPos = 0];
  330. } else {
  331. this.outputBits = codingLine[this.codingPos = 1];
  332. }
  333. this.row++;
  334. }
  335. let c;
  336. if (this.outputBits >= 8) {
  337. c = this.codingPos & 1 ? 0 : 0xff;
  338. this.outputBits -= 8;
  339. if (this.outputBits === 0 && codingLine[this.codingPos] < columns) {
  340. this.codingPos++;
  341. this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
  342. }
  343. } else {
  344. bits = 8;
  345. c = 0;
  346. do {
  347. if (typeof this.outputBits !== "number") {
  348. throw new _util.FormatError('Invalid /CCITTFaxDecode data, "outputBits" must be a number.');
  349. }
  350. if (this.outputBits > bits) {
  351. c <<= bits;
  352. if (!(this.codingPos & 1)) {
  353. c |= 0xff >> 8 - bits;
  354. }
  355. this.outputBits -= bits;
  356. bits = 0;
  357. } else {
  358. c <<= this.outputBits;
  359. if (!(this.codingPos & 1)) {
  360. c |= 0xff >> 8 - this.outputBits;
  361. }
  362. bits -= this.outputBits;
  363. this.outputBits = 0;
  364. if (codingLine[this.codingPos] < columns) {
  365. this.codingPos++;
  366. this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
  367. } else if (bits > 0) {
  368. c <<= bits;
  369. bits = 0;
  370. }
  371. }
  372. } while (bits);
  373. }
  374. if (this.black) {
  375. c ^= 0xff;
  376. }
  377. return c;
  378. }
  379. _addPixels(a1, blackPixels) {
  380. const codingLine = this.codingLine;
  381. let codingPos = this.codingPos;
  382. if (a1 > codingLine[codingPos]) {
  383. if (a1 > this.columns) {
  384. (0, _util.info)("row is wrong length");
  385. this.err = true;
  386. a1 = this.columns;
  387. }
  388. if (codingPos & 1 ^ blackPixels) {
  389. ++codingPos;
  390. }
  391. codingLine[codingPos] = a1;
  392. }
  393. this.codingPos = codingPos;
  394. }
  395. _addPixelsNeg(a1, blackPixels) {
  396. const codingLine = this.codingLine;
  397. let codingPos = this.codingPos;
  398. if (a1 > codingLine[codingPos]) {
  399. if (a1 > this.columns) {
  400. (0, _util.info)("row is wrong length");
  401. this.err = true;
  402. a1 = this.columns;
  403. }
  404. if (codingPos & 1 ^ blackPixels) {
  405. ++codingPos;
  406. }
  407. codingLine[codingPos] = a1;
  408. } else if (a1 < codingLine[codingPos]) {
  409. if (a1 < 0) {
  410. (0, _util.info)("invalid code");
  411. this.err = true;
  412. a1 = 0;
  413. }
  414. while (codingPos > 0 && a1 < codingLine[codingPos - 1]) {
  415. --codingPos;
  416. }
  417. codingLine[codingPos] = a1;
  418. }
  419. this.codingPos = codingPos;
  420. }
  421. _findTableCode(start, end, table, limit) {
  422. const limitValue = limit || 0;
  423. for (let i = start; i <= end; ++i) {
  424. let code = this._lookBits(i);
  425. if (code === ccittEOF) {
  426. return [true, 1, false];
  427. }
  428. if (i < end) {
  429. code <<= end - i;
  430. }
  431. if (!limitValue || code >= limitValue) {
  432. const p = table[code - limitValue];
  433. if (p[0] === i) {
  434. this._eatBits(i);
  435. return [true, p[1], true];
  436. }
  437. }
  438. }
  439. return [false, 0, false];
  440. }
  441. _getTwoDimCode() {
  442. let code = 0;
  443. let p;
  444. if (this.eoblock) {
  445. code = this._lookBits(7);
  446. p = twoDimTable[code];
  447. if (p && p[0] > 0) {
  448. this._eatBits(p[0]);
  449. return p[1];
  450. }
  451. } else {
  452. const result = this._findTableCode(1, 7, twoDimTable);
  453. if (result[0] && result[2]) {
  454. return result[1];
  455. }
  456. }
  457. (0, _util.info)("Bad two dim code");
  458. return ccittEOF;
  459. }
  460. _getWhiteCode() {
  461. let code = 0;
  462. let p;
  463. if (this.eoblock) {
  464. code = this._lookBits(12);
  465. if (code === ccittEOF) {
  466. return 1;
  467. }
  468. if (code >> 5 === 0) {
  469. p = whiteTable1[code];
  470. } else {
  471. p = whiteTable2[code >> 3];
  472. }
  473. if (p[0] > 0) {
  474. this._eatBits(p[0]);
  475. return p[1];
  476. }
  477. } else {
  478. let result = this._findTableCode(1, 9, whiteTable2);
  479. if (result[0]) {
  480. return result[1];
  481. }
  482. result = this._findTableCode(11, 12, whiteTable1);
  483. if (result[0]) {
  484. return result[1];
  485. }
  486. }
  487. (0, _util.info)("bad white code");
  488. this._eatBits(1);
  489. return 1;
  490. }
  491. _getBlackCode() {
  492. let code, p;
  493. if (this.eoblock) {
  494. code = this._lookBits(13);
  495. if (code === ccittEOF) {
  496. return 1;
  497. }
  498. if (code >> 7 === 0) {
  499. p = blackTable1[code];
  500. } else if (code >> 9 === 0 && code >> 7 !== 0) {
  501. p = blackTable2[(code >> 1) - 64];
  502. } else {
  503. p = blackTable3[code >> 7];
  504. }
  505. if (p[0] > 0) {
  506. this._eatBits(p[0]);
  507. return p[1];
  508. }
  509. } else {
  510. let result = this._findTableCode(2, 6, blackTable3);
  511. if (result[0]) {
  512. return result[1];
  513. }
  514. result = this._findTableCode(7, 12, blackTable2, 64);
  515. if (result[0]) {
  516. return result[1];
  517. }
  518. result = this._findTableCode(10, 13, blackTable1);
  519. if (result[0]) {
  520. return result[1];
  521. }
  522. }
  523. (0, _util.info)("bad black code");
  524. this._eatBits(1);
  525. return 1;
  526. }
  527. _lookBits(n) {
  528. let c;
  529. while (this.inputBits < n) {
  530. if ((c = this.source.next()) === -1) {
  531. if (this.inputBits === 0) {
  532. return ccittEOF;
  533. }
  534. return this.inputBuf << n - this.inputBits & 0xffff >> 16 - n;
  535. }
  536. this.inputBuf = this.inputBuf << 8 | c;
  537. this.inputBits += 8;
  538. }
  539. return this.inputBuf >> this.inputBits - n & 0xffff >> 16 - n;
  540. }
  541. _eatBits(n) {
  542. if ((this.inputBits -= n) < 0) {
  543. this.inputBits = 0;
  544. }
  545. }
  546. }
  547. exports.CCITTFaxDecoder = CCITTFaxDecoder;