2
0

ccitt.js 28 KB

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