2
0

ccitt.js 28 KB

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