ccitt_stream.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* Copyright 2012 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { CCITTFaxDecoder } from "./ccitt.js";
  16. import { DecodeStream } from "./decode_stream.js";
  17. import { Dict } from "./primitives.js";
  18. class CCITTFaxStream extends DecodeStream {
  19. constructor(str, maybeLength, params) {
  20. super(maybeLength);
  21. this.str = str;
  22. this.dict = str.dict;
  23. if (!(params instanceof Dict)) {
  24. params = Dict.empty;
  25. }
  26. const source = {
  27. next() {
  28. return str.getByte();
  29. },
  30. };
  31. this.ccittFaxDecoder = new CCITTFaxDecoder(source, {
  32. K: params.get("K"),
  33. EndOfLine: params.get("EndOfLine"),
  34. EncodedByteAlign: params.get("EncodedByteAlign"),
  35. Columns: params.get("Columns"),
  36. Rows: params.get("Rows"),
  37. EndOfBlock: params.get("EndOfBlock"),
  38. BlackIs1: params.get("BlackIs1"),
  39. });
  40. }
  41. readBlock() {
  42. while (!this.eof) {
  43. const c = this.ccittFaxDecoder.readNextChar();
  44. if (c === -1) {
  45. this.eof = true;
  46. return;
  47. }
  48. this.ensureBuffer(this.bufferLength + 1);
  49. this.buffer[this.bufferLength++] = c;
  50. }
  51. }
  52. }
  53. export { CCITTFaxStream };