15832144755
2022-01-06 7b4c8991dca9cf2a809a95e239d144697d3afb56
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { loadImageFromTypedArray } from "../../Source/Cesium.js";
import { Resource } from "../../Source/Cesium.js";
import { when } from "../../Source/Cesium.js";
 
describe("Core/loadImageFromTypedArray", function () {
  var supportsImageBitmapOptions;
 
  beforeAll(function () {
    return Resource.supportsImageBitmapOptions().then(function (result) {
      supportsImageBitmapOptions = result;
    });
  });
 
  it("can load an image", function () {
    return Resource.fetchArrayBuffer("./Data/Images/Blue10x10.png").then(
      function (arrayBuffer) {
        var options = {
          uint8Array: new Uint8Array(arrayBuffer),
          format: "image/png",
        };
 
        return loadImageFromTypedArray(options).then(function (image) {
          expect(image.width).toEqual(10);
          expect(image.height).toEqual(10);
        });
      }
    );
  });
 
  it("flips image only when flipY is true", function () {
    if (!supportsImageBitmapOptions) {
      return;
    }
 
    var options = {
      uint8Array: new Uint8Array([67, 101, 115, 105, 117, 109]), // This is an invalid PNG.
      format: "image/png",
      flipY: true,
    };
    spyOn(window, "createImageBitmap").and.returnValue(when.resolve({}));
    var blob = new Blob([options.uint8Array], {
      type: options.format,
    });
 
    return Resource.supportsImageBitmapOptions().then(function (result) {
      if (!result) {
        return;
      }
 
      return loadImageFromTypedArray(options)
        .then(function () {
          expect(window.createImageBitmap).toHaveBeenCalledWith(blob, {
            imageOrientation: "flipY",
            premultiplyAlpha: "none",
            colorSpaceConversion: "default",
          });
 
          window.createImageBitmap.calls.reset();
          options.flipY = false;
          return loadImageFromTypedArray(options);
        })
        .then(function () {
          expect(window.createImageBitmap).toHaveBeenCalledWith(blob, {
            imageOrientation: "none",
            premultiplyAlpha: "none",
            colorSpaceConversion: "default",
          });
        });
    });
  });
 
  it("stores colorSpaceConversion correctly", function () {
    if (!supportsImageBitmapOptions) {
      return;
    }
 
    var options = {
      uint8Array: new Uint8Array([67, 101, 115, 105, 117, 109]), // This is an invalid PNG.
      format: "image/png",
      flipY: false,
      skipColorSpaceConversion: true,
    };
    spyOn(window, "createImageBitmap").and.returnValue(when.resolve({}));
    var blob = new Blob([options.uint8Array], {
      type: options.format,
    });
 
    return Resource.supportsImageBitmapOptions().then(function (result) {
      if (!result) {
        return;
      }
 
      return loadImageFromTypedArray(options)
        .then(function () {
          expect(window.createImageBitmap).toHaveBeenCalledWith(blob, {
            imageOrientation: "none",
            premultiplyAlpha: "none",
            colorSpaceConversion: "none",
          });
 
          window.createImageBitmap.calls.reset();
          options.skipColorSpaceConversion = false;
          return loadImageFromTypedArray(options);
        })
        .then(function () {
          expect(window.createImageBitmap).toHaveBeenCalledWith(blob, {
            imageOrientation: "none",
            premultiplyAlpha: "none",
            colorSpaceConversion: "default",
          });
        });
    });
  });
 
  it("can load an image when ImageBitmap is not supported", function () {
    if (!supportsImageBitmapOptions) {
      return;
    }
 
    spyOn(Resource, "supportsImageBitmapOptions").and.returnValue(
      when.resolve(false)
    );
    spyOn(window, "createImageBitmap").and.callThrough();
    return Resource.fetchArrayBuffer("./Data/Images/Blue10x10.png").then(
      function (arrayBuffer) {
        var options = {
          uint8Array: new Uint8Array(arrayBuffer),
          format: "image/png",
        };
 
        return loadImageFromTypedArray(options).then(function (image) {
          expect(image.width).toEqual(10);
          expect(image.height).toEqual(10);
          expect(window.createImageBitmap).not.toHaveBeenCalled();
        });
      }
    );
  });
 
  it("can not load an invalid image", function () {
    var options = {
      uint8Array: new Uint8Array([67, 101, 115, 105, 117, 109]), // This is an invalid PNG.
      format: "image/png",
    };
    return loadImageFromTypedArray(options)
      .then(function (image) {
        fail("should not be called");
      })
      .otherwise(function () {});
  });
 
  it("Throws without array", function () {
    expect(function () {
      loadImageFromTypedArray({});
    }).toThrowDeveloperError();
  });
 
  it("Throws without format", function () {
    expect(function () {
      loadImageFromTypedArray({
        uint8Array: new Uint8Array(),
      });
    }).toThrowDeveloperError();
  });
});