stream_spec.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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. var _primitives = require("../../core/primitives.js");
  24. var _predictor_stream = require("../../core/predictor_stream.js");
  25. var _stream = require("../../core/stream.js");
  26. describe("stream", function () {
  27. beforeEach(function () {
  28. jasmine.addMatchers({
  29. toMatchTypedArray(util, customEqualityTesters) {
  30. return {
  31. compare(actual, expected) {
  32. const result = {};
  33. if (actual.length !== expected.length) {
  34. result.pass = false;
  35. result.message = "Array length: " + actual.length + ", expected: " + expected.length;
  36. return result;
  37. }
  38. result.pass = true;
  39. for (let i = 0, ii = expected.length; i < ii; i++) {
  40. const a = actual[i],
  41. b = expected[i];
  42. if (a !== b) {
  43. result.pass = false;
  44. break;
  45. }
  46. }
  47. return result;
  48. }
  49. };
  50. }
  51. });
  52. });
  53. describe("PredictorStream", function () {
  54. it("should decode simple predictor data", function () {
  55. const dict = new _primitives.Dict();
  56. dict.set("Predictor", 12);
  57. dict.set("Colors", 1);
  58. dict.set("BitsPerComponent", 8);
  59. dict.set("Columns", 2);
  60. const input = new _stream.Stream(new Uint8Array([2, 100, 3, 2, 1, 255, 2, 1, 255]), 0, 9, dict);
  61. const predictor = new _predictor_stream.PredictorStream(input, 9, dict);
  62. const result = predictor.getBytes(6);
  63. expect(result).toMatchTypedArray(new Uint8Array([100, 3, 101, 2, 102, 1]));
  64. predictor.reset();
  65. const clampedResult = predictor.getBytes(6, true);
  66. expect(clampedResult).toEqual(new Uint8ClampedArray([100, 3, 101, 2, 102, 1]));
  67. });
  68. });
  69. });