ccitt.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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 CCITTFaxDecoder = function CCITTFaxDecoder() {
  29. const ccittEOL = -2;
  30. const ccittEOF = -1;
  31. const twoDimPass = 0;
  32. const twoDimHoriz = 1;
  33. const twoDimVert0 = 2;
  34. const twoDimVertR1 = 3;
  35. const twoDimVertL1 = 4;
  36. const twoDimVertR2 = 5;
  37. const twoDimVertL2 = 6;
  38. const twoDimVertR3 = 7;
  39. const twoDimVertL3 = 8;
  40. 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]];
  41. 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]];
  42. 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]];
  43. 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]];
  44. 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]];
  45. 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]];
  46. function CCITTFaxDecoder(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. CCITTFaxDecoder.prototype = {
  86. readNextChar() {
  87. if (this.eof) {
  88. return -1;
  89. }
  90. const refLine = this.refLine;
  91. const codingLine = this.codingLine;
  92. const columns = this.columns;
  93. let refPos, blackPixels, bits, i;
  94. if (this.outputBits === 0) {
  95. if (this.rowsDone) {
  96. this.eof = true;
  97. }
  98. if (this.eof) {
  99. return -1;
  100. }
  101. this.err = false;
  102. let code1, code2, code3;
  103. if (this.nextLine2D) {
  104. for (i = 0; codingLine[i] < columns; ++i) {
  105. refLine[i] = codingLine[i];
  106. }
  107. refLine[i++] = columns;
  108. refLine[i] = columns;
  109. codingLine[0] = 0;
  110. this.codingPos = 0;
  111. refPos = 0;
  112. blackPixels = 0;
  113. while (codingLine[this.codingPos] < columns) {
  114. code1 = this._getTwoDimCode();
  115. switch (code1) {
  116. case twoDimPass:
  117. this._addPixels(refLine[refPos + 1], blackPixels);
  118. if (refLine[refPos + 1] < columns) {
  119. refPos += 2;
  120. }
  121. break;
  122. case twoDimHoriz:
  123. code1 = code2 = 0;
  124. if (blackPixels) {
  125. do {
  126. code1 += code3 = this._getBlackCode();
  127. } while (code3 >= 64);
  128. do {
  129. code2 += code3 = this._getWhiteCode();
  130. } while (code3 >= 64);
  131. } else {
  132. do {
  133. code1 += code3 = this._getWhiteCode();
  134. } while (code3 >= 64);
  135. do {
  136. code2 += code3 = this._getBlackCode();
  137. } while (code3 >= 64);
  138. }
  139. this._addPixels(codingLine[this.codingPos] + code1, blackPixels);
  140. if (codingLine[this.codingPos] < columns) {
  141. this._addPixels(codingLine[this.codingPos] + code2, blackPixels ^ 1);
  142. }
  143. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  144. refPos += 2;
  145. }
  146. break;
  147. case twoDimVertR3:
  148. this._addPixels(refLine[refPos] + 3, blackPixels);
  149. blackPixels ^= 1;
  150. if (codingLine[this.codingPos] < columns) {
  151. ++refPos;
  152. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  153. refPos += 2;
  154. }
  155. }
  156. break;
  157. case twoDimVertR2:
  158. this._addPixels(refLine[refPos] + 2, blackPixels);
  159. blackPixels ^= 1;
  160. if (codingLine[this.codingPos] < columns) {
  161. ++refPos;
  162. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  163. refPos += 2;
  164. }
  165. }
  166. break;
  167. case twoDimVertR1:
  168. this._addPixels(refLine[refPos] + 1, blackPixels);
  169. blackPixels ^= 1;
  170. if (codingLine[this.codingPos] < columns) {
  171. ++refPos;
  172. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  173. refPos += 2;
  174. }
  175. }
  176. break;
  177. case twoDimVert0:
  178. this._addPixels(refLine[refPos], blackPixels);
  179. blackPixels ^= 1;
  180. if (codingLine[this.codingPos] < columns) {
  181. ++refPos;
  182. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  183. refPos += 2;
  184. }
  185. }
  186. break;
  187. case twoDimVertL3:
  188. this._addPixelsNeg(refLine[refPos] - 3, blackPixels);
  189. blackPixels ^= 1;
  190. if (codingLine[this.codingPos] < columns) {
  191. if (refPos > 0) {
  192. --refPos;
  193. } else {
  194. ++refPos;
  195. }
  196. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  197. refPos += 2;
  198. }
  199. }
  200. break;
  201. case twoDimVertL2:
  202. this._addPixelsNeg(refLine[refPos] - 2, blackPixels);
  203. blackPixels ^= 1;
  204. if (codingLine[this.codingPos] < columns) {
  205. if (refPos > 0) {
  206. --refPos;
  207. } else {
  208. ++refPos;
  209. }
  210. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  211. refPos += 2;
  212. }
  213. }
  214. break;
  215. case twoDimVertL1:
  216. this._addPixelsNeg(refLine[refPos] - 1, blackPixels);
  217. blackPixels ^= 1;
  218. if (codingLine[this.codingPos] < columns) {
  219. if (refPos > 0) {
  220. --refPos;
  221. } else {
  222. ++refPos;
  223. }
  224. while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
  225. refPos += 2;
  226. }
  227. }
  228. break;
  229. case ccittEOF:
  230. this._addPixels(columns, 0);
  231. this.eof = true;
  232. break;
  233. default:
  234. (0, _util.info)("bad 2d code");
  235. this._addPixels(columns, 0);
  236. this.err = true;
  237. }
  238. }
  239. } else {
  240. codingLine[0] = 0;
  241. this.codingPos = 0;
  242. blackPixels = 0;
  243. while (codingLine[this.codingPos] < columns) {
  244. code1 = 0;
  245. if (blackPixels) {
  246. do {
  247. code1 += code3 = this._getBlackCode();
  248. } while (code3 >= 64);
  249. } else {
  250. do {
  251. code1 += code3 = this._getWhiteCode();
  252. } while (code3 >= 64);
  253. }
  254. this._addPixels(codingLine[this.codingPos] + code1, blackPixels);
  255. blackPixels ^= 1;
  256. }
  257. }
  258. let gotEOL = false;
  259. if (this.byteAlign) {
  260. this.inputBits &= ~7;
  261. }
  262. if (!this.eoblock && this.row === this.rows - 1) {
  263. this.rowsDone = true;
  264. } else {
  265. code1 = this._lookBits(12);
  266. if (this.eoline) {
  267. while (code1 !== ccittEOF && code1 !== 1) {
  268. this._eatBits(1);
  269. code1 = this._lookBits(12);
  270. }
  271. } else {
  272. while (code1 === 0) {
  273. this._eatBits(1);
  274. code1 = this._lookBits(12);
  275. }
  276. }
  277. if (code1 === 1) {
  278. this._eatBits(12);
  279. gotEOL = true;
  280. } else if (code1 === ccittEOF) {
  281. this.eof = true;
  282. }
  283. }
  284. if (!this.eof && this.encoding > 0 && !this.rowsDone) {
  285. this.nextLine2D = !this._lookBits(1);
  286. this._eatBits(1);
  287. }
  288. if (this.eoblock && gotEOL && this.byteAlign) {
  289. code1 = this._lookBits(12);
  290. if (code1 === 1) {
  291. this._eatBits(12);
  292. if (this.encoding > 0) {
  293. this._lookBits(1);
  294. this._eatBits(1);
  295. }
  296. if (this.encoding >= 0) {
  297. for (i = 0; i < 4; ++i) {
  298. code1 = this._lookBits(12);
  299. if (code1 !== 1) {
  300. (0, _util.info)("bad rtc code: " + code1);
  301. }
  302. this._eatBits(12);
  303. if (this.encoding > 0) {
  304. this._lookBits(1);
  305. this._eatBits(1);
  306. }
  307. }
  308. }
  309. this.eof = true;
  310. }
  311. } else if (this.err && this.eoline) {
  312. while (true) {
  313. code1 = this._lookBits(13);
  314. if (code1 === ccittEOF) {
  315. this.eof = true;
  316. return -1;
  317. }
  318. if (code1 >> 1 === 1) {
  319. break;
  320. }
  321. this._eatBits(1);
  322. }
  323. this._eatBits(12);
  324. if (this.encoding > 0) {
  325. this._eatBits(1);
  326. this.nextLine2D = !(code1 & 1);
  327. }
  328. }
  329. if (codingLine[0] > 0) {
  330. this.outputBits = codingLine[this.codingPos = 0];
  331. } else {
  332. this.outputBits = codingLine[this.codingPos = 1];
  333. }
  334. this.row++;
  335. }
  336. let c;
  337. if (this.outputBits >= 8) {
  338. c = this.codingPos & 1 ? 0 : 0xff;
  339. this.outputBits -= 8;
  340. if (this.outputBits === 0 && codingLine[this.codingPos] < columns) {
  341. this.codingPos++;
  342. this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
  343. }
  344. } else {
  345. bits = 8;
  346. c = 0;
  347. do {
  348. if (this.outputBits > bits) {
  349. c <<= bits;
  350. if (!(this.codingPos & 1)) {
  351. c |= 0xff >> 8 - bits;
  352. }
  353. this.outputBits -= bits;
  354. bits = 0;
  355. } else {
  356. c <<= this.outputBits;
  357. if (!(this.codingPos & 1)) {
  358. c |= 0xff >> 8 - this.outputBits;
  359. }
  360. bits -= this.outputBits;
  361. this.outputBits = 0;
  362. if (codingLine[this.codingPos] < columns) {
  363. this.codingPos++;
  364. this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
  365. } else if (bits > 0) {
  366. c <<= bits;
  367. bits = 0;
  368. }
  369. }
  370. } while (bits);
  371. }
  372. if (this.black) {
  373. c ^= 0xff;
  374. }
  375. return c;
  376. },
  377. _addPixels(a1, blackPixels) {
  378. const codingLine = this.codingLine;
  379. let codingPos = this.codingPos;
  380. if (a1 > codingLine[codingPos]) {
  381. if (a1 > this.columns) {
  382. (0, _util.info)("row is wrong length");
  383. this.err = true;
  384. a1 = this.columns;
  385. }
  386. if (codingPos & 1 ^ blackPixels) {
  387. ++codingPos;
  388. }
  389. codingLine[codingPos] = a1;
  390. }
  391. this.codingPos = codingPos;
  392. },
  393. _addPixelsNeg(a1, blackPixels) {
  394. const codingLine = this.codingLine;
  395. let codingPos = this.codingPos;
  396. if (a1 > codingLine[codingPos]) {
  397. if (a1 > this.columns) {
  398. (0, _util.info)("row is wrong length");
  399. this.err = true;
  400. a1 = this.columns;
  401. }
  402. if (codingPos & 1 ^ blackPixels) {
  403. ++codingPos;
  404. }
  405. codingLine[codingPos] = a1;
  406. } else if (a1 < codingLine[codingPos]) {
  407. if (a1 < 0) {
  408. (0, _util.info)("invalid code");
  409. this.err = true;
  410. a1 = 0;
  411. }
  412. while (codingPos > 0 && a1 < codingLine[codingPos - 1]) {
  413. --codingPos;
  414. }
  415. codingLine[codingPos] = a1;
  416. }
  417. this.codingPos = codingPos;
  418. },
  419. _findTableCode(start, end, table, limit) {
  420. const limitValue = limit || 0;
  421. for (let i = start; i <= end; ++i) {
  422. let code = this._lookBits(i);
  423. if (code === ccittEOF) {
  424. return [true, 1, false];
  425. }
  426. if (i < end) {
  427. code <<= end - i;
  428. }
  429. if (!limitValue || code >= limitValue) {
  430. const p = table[code - limitValue];
  431. if (p[0] === i) {
  432. this._eatBits(i);
  433. return [true, p[1], true];
  434. }
  435. }
  436. }
  437. return [false, 0, false];
  438. },
  439. _getTwoDimCode() {
  440. let code = 0;
  441. let p;
  442. if (this.eoblock) {
  443. code = this._lookBits(7);
  444. p = twoDimTable[code];
  445. if (p && p[0] > 0) {
  446. this._eatBits(p[0]);
  447. return p[1];
  448. }
  449. } else {
  450. const result = this._findTableCode(1, 7, twoDimTable);
  451. if (result[0] && result[2]) {
  452. return result[1];
  453. }
  454. }
  455. (0, _util.info)("Bad two dim code");
  456. return ccittEOF;
  457. },
  458. _getWhiteCode() {
  459. let code = 0;
  460. let p;
  461. if (this.eoblock) {
  462. code = this._lookBits(12);
  463. if (code === ccittEOF) {
  464. return 1;
  465. }
  466. if (code >> 5 === 0) {
  467. p = whiteTable1[code];
  468. } else {
  469. p = whiteTable2[code >> 3];
  470. }
  471. if (p[0] > 0) {
  472. this._eatBits(p[0]);
  473. return p[1];
  474. }
  475. } else {
  476. let result = this._findTableCode(1, 9, whiteTable2);
  477. if (result[0]) {
  478. return result[1];
  479. }
  480. result = this._findTableCode(11, 12, whiteTable1);
  481. if (result[0]) {
  482. return result[1];
  483. }
  484. }
  485. (0, _util.info)("bad white code");
  486. this._eatBits(1);
  487. return 1;
  488. },
  489. _getBlackCode() {
  490. let code, p;
  491. if (this.eoblock) {
  492. code = this._lookBits(13);
  493. if (code === ccittEOF) {
  494. return 1;
  495. }
  496. if (code >> 7 === 0) {
  497. p = blackTable1[code];
  498. } else if (code >> 9 === 0 && code >> 7 !== 0) {
  499. p = blackTable2[(code >> 1) - 64];
  500. } else {
  501. p = blackTable3[code >> 7];
  502. }
  503. if (p[0] > 0) {
  504. this._eatBits(p[0]);
  505. return p[1];
  506. }
  507. } else {
  508. let result = this._findTableCode(2, 6, blackTable3);
  509. if (result[0]) {
  510. return result[1];
  511. }
  512. result = this._findTableCode(7, 12, blackTable2, 64);
  513. if (result[0]) {
  514. return result[1];
  515. }
  516. result = this._findTableCode(10, 13, blackTable1);
  517. if (result[0]) {
  518. return result[1];
  519. }
  520. }
  521. (0, _util.info)("bad black code");
  522. this._eatBits(1);
  523. return 1;
  524. },
  525. _lookBits(n) {
  526. let c;
  527. while (this.inputBits < n) {
  528. if ((c = this.source.next()) === -1) {
  529. if (this.inputBits === 0) {
  530. return ccittEOF;
  531. }
  532. return this.inputBuf << n - this.inputBits & 0xffff >> 16 - n;
  533. }
  534. this.inputBuf = this.inputBuf << 8 | c;
  535. this.inputBits += 8;
  536. }
  537. return this.inputBuf >> this.inputBits - n & 0xffff >> 16 - n;
  538. },
  539. _eatBits(n) {
  540. if ((this.inputBits -= n) < 0) {
  541. this.inputBits = 0;
  542. }
  543. }
  544. };
  545. return CCITTFaxDecoder;
  546. }();
  547. exports.CCITTFaxDecoder = CCITTFaxDecoder;