summaryrefslogtreecommitdiff
path: root/chromium/extensions/renderer/resources/image_util.js
blob: 4f5eb8333268d3a96315995ebb1592b2b3f3f434 (plain)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var natives = requireNative('setIcon');
var inServiceWorker = natives.IsInServiceWorker();

// Compute a scaling factor for the image based on the supplied
// image specification.
function computeScaleFactor(imageSpec, img) {
  var scaleFactor = 1;
  if (imageSpec.width && imageSpec.width < img.width)
    scaleFactor = imageSpec.width / img.width;

  if (imageSpec.height && imageSpec.height < img.height) {
    var heightScale = imageSpec.height / img.height;
    if (heightScale < scaleFactor)
      scaleFactor = heightScale;
  }
  return scaleFactor;
}

function loadImageDataForServiceWorker(imageSpec, callbacks) {
  var path = imageSpec.path;
  let fetchPromise = fetch(path);

  let blobPromise = $Promise.then(fetchPromise, (response) => {
    if (!response.ok)
      throw $Error.self({ problem: 'could_not_load', path: path });

    return response.blob();
  });

  let imagePromise = $Promise.then(blobPromise, (blob) => {
    return createImageBitmap(blob);
  });

  let imageDataPromise = $Promise.then(imagePromise, (image) => {
    if (image.width <= 0 || image.height <= 0)
      throw $Error.self({ problem: 'image_size_invalid', path: path});

    var scaleFactor = computeScaleFactor(imageSpec, image);
    var canvas = new OffscreenCanvas(image.width * scaleFactor,
                                     image.height * scaleFactor);

    var canvasContext = canvas.getContext('2d');
    canvasContext.clearRect(0, 0, canvas.width, canvas.height);
    canvasContext.drawImage(image, 0, 0, canvas.width, canvas.height);
    try {
      var imageData = canvasContext.getImageData(
          0, 0, canvas.width, canvas.height);
      if (typeof callbacks.oncomplete === 'function') {
        callbacks.oncomplete(
            imageData.width, imageData.height, imageData.data.buffer);
      }
    } catch (e) {
      throw $Error.self({ problem: 'data_url_unavailable', path: path });
    }
  });

  $Promise.catch(imageDataPromise, function(error) {
    if (typeof callbacks.onerror === 'function')
      callbacks.onerror(error);
  });
}

// This function takes an object |imageSpec| with the key |path| -
// corresponding to the internet URL to be translated - and optionally
// |width| and |height| which are the maximum dimensions to be used when
// converting the image.
function loadImageDataForNonServiceWorker(imageSpec, callbacks) {
  var path = imageSpec.path;
  var img = new Image();
  if (typeof callbacks.onerror === 'function') {
    img.onerror = function() {
      callbacks.onerror({ problem: 'could_not_load', path: path });
    };
  }
  img.onload = function() {
    var canvas = document.createElement('canvas');

    if (img.width <= 0 || img.height <= 0) {
      callbacks.onerror({ problem: 'image_size_invalid', path: path});
      return;
    }

    var scaleFactor = computeScaleFactor(imageSpec, img);
    canvas.width = img.width * scaleFactor;
    canvas.height = img.height * scaleFactor;

    var canvas_context = canvas.getContext('2d');
    canvas_context.clearRect(0, 0, canvas.width, canvas.height);
    canvas_context.drawImage(img, 0, 0, canvas.width, canvas.height);
    try {
      var imageData = canvas_context.getImageData(
          0, 0, canvas.width, canvas.height);
      if (typeof callbacks.oncomplete === 'function') {
        callbacks.oncomplete(
            imageData.width, imageData.height, imageData.data.buffer);
      }
    } catch (e) {
      if (typeof callbacks.onerror === 'function') {
        callbacks.onerror({ problem: 'data_url_unavailable', path: path });
      }
    }
  }
  img.src = path;
}

function loadImageData(imageSpec, callbacks) {
  if (inServiceWorker) {
    loadImageDataForServiceWorker(imageSpec, callbacks);
  } else {
    loadImageDataForNonServiceWorker(imageSpec, callbacks);
  }
}

function on_complete_index(index, err, loading, finished, callbacks) {
  return function(width, height, imageData) {
    delete loading[index];
    finished[index] = { width: width, height: height, data: imageData };
    if (err)
      callbacks.onerror(index);
    if ($Object.keys(loading).length == 0)
      callbacks.oncomplete(finished);
  }
}

function loadAllImages(imageSpecs, callbacks) {
  var loading = {}, finished = [],
      index, pathname;

  for (var index = 0; index < imageSpecs.length; index++) {
    loading[index] = imageSpecs[index];
    loadImageData(imageSpecs[index], {
      oncomplete: on_complete_index(index, false, loading, finished, callbacks),
      onerror: on_complete_index(index, true, loading, finished, callbacks)
    });
  }
}

exports.$set('loadImageData', loadImageData);
exports.$set('loadAllImages', loadAllImages);