Pass data object to operations

This commit is contained in:
Tim Schaub
2015-06-25 17:32:34 -06:00
parent 5267776627
commit 65fee5b7ac
4 changed files with 47 additions and 24 deletions
+5 -2
View File
@@ -21,9 +21,12 @@ ol.raster.OperationType = {
* return an array of the same. For `'image'` type operations, functions will
* be called with an array of {@link ImageData
* https://developer.mozilla.org/en-US/docs/Web/API/ImageData} and should return
* an array of the same.
* an array of the same. The operations are called with a second "data"
* argument, which can be used for storage. The data object is accessible
* from raster events, where it can be initialized in "beforeoperations" and
* accessed again in "afteroperations."
*
* @typedef {function((Array.<ol.raster.Pixel>|Array.<ImageData>)):
* @typedef {function((Array.<ol.raster.Pixel>|Array.<ImageData>), Object):
* (Array.<ol.raster.Pixel>|Array.<ImageData>)}
* @api
*/
+21 -9
View File
@@ -223,8 +223,9 @@ ol.source.Raster.prototype.composeFrame_ = function(frameState) {
this.renderers_[i], frameState, frameState.layerStatesArray[i]);
}
var data = {};
this.dispatchEvent(new ol.source.RasterEvent(
ol.source.RasterEventType.BEFOREOPERATIONS, frameState));
ol.source.RasterEventType.BEFOREOPERATIONS, frameState, data));
var targetImageData = null;
if (this.operationType_ === ol.raster.OperationType.PIXEL) {
@@ -242,20 +243,20 @@ ol.source.Raster.prototype.composeFrame_ = function(frameState) {
pixel[2] = source[j + 2];
pixel[3] = source[j + 3];
}
pixel = this.runPixelOperations_(pixels)[0];
pixel = this.runPixelOperations_(pixels, data)[0];
target[j] = pixel[0];
target[j + 1] = pixel[1];
target[j + 2] = pixel[2];
target[j + 3] = pixel[3];
}
} else if (this.operationType_ === ol.raster.OperationType.IMAGE) {
targetImageData = this.runImageOperations_(imageDatas)[0];
targetImageData = this.runImageOperations_(imageDatas, data)[0];
} else {
goog.asserts.fail('unsupported operation type: ' + this.operationType_);
}
this.dispatchEvent(new ol.source.RasterEvent(
ol.source.RasterEventType.AFTEROPERATIONS, frameState));
ol.source.RasterEventType.AFTEROPERATIONS, frameState, data));
context.putImageData(targetImageData, 0, 0);
@@ -266,12 +267,13 @@ ol.source.Raster.prototype.composeFrame_ = function(frameState) {
/**
* Run pixel-wise operations to transform pixels.
* @param {Array.<ol.raster.Pixel>} pixels The input pixels.
* @param {Object} data User storage.
* @return {Array.<ol.raster.Pixel>} The modified pixels.
* @private
*/
ol.source.Raster.prototype.runPixelOperations_ = function(pixels) {
ol.source.Raster.prototype.runPixelOperations_ = function(pixels, data) {
for (var i = 0, ii = this.operations_.length; i < ii; ++i) {
pixels = this.operations_[i](pixels);
pixels = this.operations_[i](pixels, data);
}
return pixels;
};
@@ -280,12 +282,13 @@ ol.source.Raster.prototype.runPixelOperations_ = function(pixels) {
/**
* Run image operations.
* @param {Array.<ImageData>} imageDatas The input image data.
* @param {Object} data User storage.
* @return {Array.<ImageData>} The output image data.
* @private
*/
ol.source.Raster.prototype.runImageOperations_ = function(imageDatas) {
ol.source.Raster.prototype.runImageOperations_ = function(imageDatas, data) {
for (var i = 0, ii = this.operations_.length; i < ii; ++i) {
imageDatas = this.operations_[i](imageDatas);
imageDatas = this.operations_[i](imageDatas, data);
}
return imageDatas;
};
@@ -396,8 +399,9 @@ ol.source.Raster.createTileRenderer_ = function(source) {
* @implements {oli.source.RasterEvent}
* @param {string} type Type.
* @param {olx.FrameState} frameState The frame state.
* @param {Object} data An object made available to operations.
*/
ol.source.RasterEvent = function(type, frameState) {
ol.source.RasterEvent = function(type, frameState, data) {
goog.base(this, type);
/**
@@ -414,6 +418,14 @@ ol.source.RasterEvent = function(type, frameState) {
*/
this.resolution = frameState.viewState.resolution / frameState.pixelRatio;
/**
* An object made available to all operations. This can be used by operations
* as a storage object (e.g. for calculating statistics).
* @type {Object}
* @api
*/
this.data = data;
};
goog.inherits(ol.source.RasterEvent, goog.events.Event);