<commit_before>goog.provide('ol.source.Zoomify');

goog.require('goog.array');
goog.require('ol.Image');
goog.require('ol.TileCoord');
goog.require('ol.TileUrlFunction');
goog.require('ol.proj');
goog.require('ol.source.TileImage');
goog.require('ol.tilegrid.Zoomify');



/**
 * @constructor
 * @extends {ol.source.TileImage}
 * @param {olx.source.ZoomifyOptions=} opt_options Options.
 * @todo stability experimental
 */
ol.source.Zoomify = function(opt_options) {

  var options = goog.isDef(opt_options) ? opt_options : {};

  var size = options.size;

  var imageSize = size.slice();
  var tiles = [
    Math.ceil(imageSize[0] / ol.DEFAULT_TILE_SIZE),
    Math.ceil(imageSize[1] / ol.DEFAULT_TILE_SIZE)
  ];
  var tierSizeInTiles = [tiles];
  var tierImageSize = [imageSize];

  while (imageSize[0] > ol.DEFAULT_TILE_SIZE ||
      imageSize[1] > ol.DEFAULT_TILE_SIZE) {

    imageSize = [
      Math.floor(imageSize[0] / 2),
      Math.floor(imageSize[1] / 2)
    ];
    tiles = [
      Math.ceil(imageSize[0] / ol.DEFAULT_TILE_SIZE),
      Math.ceil(imageSize[1] / ol.DEFAULT_TILE_SIZE)
    ];
    tierSizeInTiles.push(tiles);
    tierImageSize.push(imageSize);
  }

  tierSizeInTiles.reverse();
  tierImageSize.reverse();
  var numberOfTiers = tierSizeInTiles.length;
  var resolutions = [1];
  var tileCountUpToTier = [0];
  var i;
  for (i = 1; i < numberOfTiers; i++) {
    resolutions.unshift(Math.pow(2, i));
    tileCountUpToTier.push(
        tierSizeInTiles[i - 1][0] * tierSizeInTiles[i - 1][1] +
        tileCountUpToTier[i - 1]
    );
  }

  var createFromUrl = function(url) {
    var template = url + '{tileIndex}/{z}-{x}-{y}.jpg';
    return (
        /**
         * @this {ol.source.TileImage}
         * @param {ol.TileCoord} tileCoord Tile Coordinate.
         * @param {ol.proj.Projection} projection Projection.
         * @return {string|undefined} Tile URL.
         */
        function(tileCoord, projection) {
          if (goog.isNull(tileCoord)) {
            return undefined;
          } else {
            var tileIndex = tileCoord.x +
                (tileCoord.y * tierSizeInTiles[tileCoord.z][0]) +
                tileCountUpToTier[tileCoord.z];
            return template.replace('{tileIndex}', 'TileGroup' +
                Math.floor((tileIndex) / ol.DEFAULT_TILE_SIZE))
                           .replace('{z}', '' + tileCoord.z)
                           .replace('{x}', '' + tileCoord.x)
                           .replace('{y}', '' + tileCoord.y);
          }
        }
    );
  };

  /**
   * Resize small tiles. Warning : needs a good crossOrigin handling.
   *
   * @param  {ol.ImageTile} imageTile Current tile
   * @param  {String} src Src url
   */
  var tileLoadFunction = function(imageTile, src) {
    var image = imageTile.getImage();

    // Bad image size (only if correct crossOrigin handling)
    if (image.crossOrigin) {
      image.onload = function() {
        if (this.width < ol.DEFAULT_TILE_SIZE ||
            this.height < ol.DEFAULT_TILE_SIZE) {
          // Copy image data into the canvas
          var canvas = document.createElement('canvas');
          if (canvas.getContext) {
            canvas.width = ol.DEFAULT_TILE_SIZE;
            canvas.height = ol.DEFAULT_TILE_SIZE;
            var ctx = canvas.getContext('2d');
            ctx.drawImage(this, 0, 0);

            // Change original image
            image = new Image();
            image.src = canvas.toDataURL();
          }
        }
      };
    }
    image.src = src;
  };

  var tileGrid = new ol.tilegrid.Zoomify({
    resolutions: resolutions
  });
  var tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
      tileGrid.createTileCoordTransform({extent: [0, 0, size[0], size[1]]}),
      createFromUrl(options.url));

  goog.base(this, {
    attributions: options.attributions,
    crossOrigin: options.crossOrigin,
    logo: options.logo,
    tileGrid: tileGrid,
    tileUrlFunction: tileUrlFunction,
    tileLoadFunction: tileLoadFunction
  });

};
goog.inherits(ol.source.Zoomify, ol.source.TileImage);
<commit_msg>Refactor ol.source.Zoomify to use a private tile class<commit_after>goog.provide('ol.source.Zoomify');

goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('ol.ImageTile');
goog.require('ol.TileCoord');
goog.require('ol.TileState');
goog.require('ol.TileUrlFunction');
goog.require('ol.proj');
goog.require('ol.source.TileImage');
goog.require('ol.tilegrid.Zoomify');



/**
 * @constructor
 * @extends {ol.source.TileImage}
 * @param {olx.source.ZoomifyOptions=} opt_options Options.
 * @todo stability experimental
 */
ol.source.Zoomify = function(opt_options) {

  var options = goog.isDef(opt_options) ? opt_options : {};

  var size = options.size;

  var imageSize = size.slice();
  var tiles = [
    Math.ceil(imageSize[0] / ol.DEFAULT_TILE_SIZE),
    Math.ceil(imageSize[1] / ol.DEFAULT_TILE_SIZE)
  ];
  var tierSizeInTiles = [tiles];
  var tierImageSize = [imageSize];

  while (imageSize[0] > ol.DEFAULT_TILE_SIZE ||
      imageSize[1] > ol.DEFAULT_TILE_SIZE) {

    imageSize = [
      Math.floor(imageSize[0] / 2),
      Math.floor(imageSize[1] / 2)
    ];
    tiles = [
      Math.ceil(imageSize[0] / ol.DEFAULT_TILE_SIZE),
      Math.ceil(imageSize[1] / ol.DEFAULT_TILE_SIZE)
    ];
    tierSizeInTiles.push(tiles);
    tierImageSize.push(imageSize);
  }

  tierSizeInTiles.reverse();
  tierImageSize.reverse();
  var numberOfTiers = tierSizeInTiles.length;
  var resolutions = [1];
  var tileCountUpToTier = [0];
  var i;
  for (i = 1; i < numberOfTiers; i++) {
    resolutions.unshift(Math.pow(2, i));
    tileCountUpToTier.push(
        tierSizeInTiles[i - 1][0] * tierSizeInTiles[i - 1][1] +
        tileCountUpToTier[i - 1]
    );
  }

  var createFromUrl = function(url) {
    var template = url + '{tileIndex}/{z}-{x}-{y}.jpg';
    return (
        /**
         * @this {ol.source.TileImage}
         * @param {ol.TileCoord} tileCoord Tile Coordinate.
         * @param {ol.proj.Projection} projection Projection.
         * @return {string|undefined} Tile URL.
         */
        function(tileCoord, projection) {
          if (goog.isNull(tileCoord)) {
            return undefined;
          } else {
            var tileIndex = tileCoord.x +
                (tileCoord.y * tierSizeInTiles[tileCoord.z][0]) +
                tileCountUpToTier[tileCoord.z];
            return template.replace('{tileIndex}', 'TileGroup' +
                Math.floor((tileIndex) / ol.DEFAULT_TILE_SIZE))
                           .replace('{z}', '' + tileCoord.z)
                           .replace('{x}', '' + tileCoord.x)
                           .replace('{y}', '' + tileCoord.y);
          }
        }
    );
  };

  var tileGrid = new ol.tilegrid.Zoomify({
    resolutions: resolutions
  });
  var tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
      tileGrid.createTileCoordTransform({extent: [0, 0, size[0], size[1]]}),
      createFromUrl(options.url));

  goog.base(this, {
    attributions: options.attributions,
    crossOrigin: options.crossOrigin,
    logo: options.logo,
    tileGrid: tileGrid,
    tileUrlFunction: tileUrlFunction
  });

};
goog.inherits(ol.source.Zoomify, ol.source.TileImage);


/**
 * @inheritDoc
 */
ol.source.Zoomify.prototype.getTile = function(z, x, y, projection) {
  var tileCoordKey = this.getKeyZXY(z, x, y);
  if (this.tileCache.containsKey(tileCoordKey)) {
    return /** @type {!ol.source.ZoomifyTile_} */ (
        this.tileCache.get(tileCoordKey));
  } else {
    goog.asserts.assert(goog.isDef(projection));
    var tileCoord = new ol.TileCoord(z, x, y);
    var tileUrl = this.tileUrlFunction(tileCoord, projection);
    var tile = new ol.source.ZoomifyTile_(
        tileCoord,
        goog.isDef(tileUrl) ? ol.TileState.IDLE : ol.TileState.EMPTY,
        goog.isDef(tileUrl) ? tileUrl : '',
        this.crossOrigin,
        this.tileLoadFunction);
    this.tileCache.set(tileCoordKey, tile);
    return tile;
  }
};



/**
 * @constructor
 * @extends {ol.ImageTile}
 * @param {ol.TileCoord} tileCoord Tile coordinate.
 * @param {ol.TileState} state State.
 * @param {string} src Image source URI.
 * @param {?string} crossOrigin Cross origin.
 * @param {ol.TileLoadFunctionType} tileLoadFunction Tile load function.
 * @private
 */
ol.source.ZoomifyTile_ = function(
    tileCoord, state, src, crossOrigin, tileLoadFunction) {

  goog.base(this, tileCoord, state, src, crossOrigin, tileLoadFunction);

  /**
   * @private
   * @type {Object.<string,
   *                HTMLCanvasElement|HTMLImageElement|HTMLVideoElement>}
   */
  this.zoomifyImageByContext_ = {};

};
goog.inherits(ol.source.ZoomifyTile_, ol.ImageTile);


/**
 * @inheritDoc
 */
ol.source.ZoomifyTile_.prototype.getImage = function(opt_context) {
  var key = goog.isDef(opt_context) ? goog.getUid(opt_context).toString() : '';
  if (key in this.zoomifyImageByContext_) {
    return this.zoomifyImageByContext_[key];
  } else {
    var image = goog.base(this, 'getImage', opt_context);
    if (this.state == ol.TileState.LOADED) {
      if (image.width == ol.DEFAULT_TILE_SIZE &&
          image.height == ol.DEFAULT_TILE_SIZE) {
        this.zoomifyImageByContext_[key] = image;
        return image;
      } else {
        var canvas = /** @type {HTMLCanvasElement} */
            (goog.dom.createElement(goog.dom.TagName.CANVAS));
        canvas.width = ol.DEFAULT_TILE_SIZE;
        canvas.height = ol.DEFAULT_TILE_SIZE;
        var context = /** @type {CanvasRenderingContext2D} */
            (canvas.getContext('2d'));
        context.drawImage(image, 0, 0);
        this.zoomifyImageByContext_[key] = canvas;
        return canvas;
      }
    } else {
      return image;
    }
  }
};

{'fraction_non_alphanumeric': 0.14329454990814453, 'fraction_numerical': 0.005613390487854664, 'mean_word_length': 2.7131489200454717, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5778285', 'n_tokens_mistral': 3290, 'n_tokens_neox': 3202, 'n_words': 562}