<commit_before>/*
* BrainBrowser: Web-based Neurological Visualization Tools
* (https://brainbrowser.cbrain.mcgill.ca)
*
* Copyright (C) 2011
* The Royal Institution for the Advancement of Learning
* McGill University
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
* Author: <NAME> <<EMAIL>> (http://tareksherif.ca/)
* Author: <NAME>
*/

(function() {
  "use strict";

  var image_creation_context = document.createElement("canvas").getContext("2d");

  BrainBrowser.VolumeViewer.utils = {
    /**
    * @doc object
    * @name VolumeViewer.utils.axis_to_number
    *
    * @property {xspace} Index of x space (0)
    * @property {yspace} Index of y space (1)
    * @property {zspace} Index of z space (2)
    *
    * @description
    * Convenience object for converting between space names
    * and indices.
    * ```js
    * BrainBrowser.VolumeViewer.utils.axis_to_number["xspace"];
    * ```
    */
    axis_to_number: {
      xspace: 0,
      yspace: 1,
      zspace: 2
    },

    /**
    * @doc function
    * @name VolumeViewer.utils.nearestNeighbor
    *
    * @param {array} original Original image
    * @param {number} new_width Width of scaled image.
    * @param {number} new_height Height of scaled image.
    * @returns {ImageData} The scaled image.
    *
    * @description
    * Scale an image using nearest neighbor interpolation.
    * ```js
    * BrainBrowser.VolumeViewer.utils.nearestNeighbor(image, 512, 512);
    * ```
    */
    nearestNeighbor: function(original, new_width, new_height) {
      var data = original.data;
      var width = original.width;
      var height = original.height;
      var block_size = 4;

      if (new_width < 0 && new_height > 0) {
        data = flipImage(data, width, height, true, false, block_size);
      }

      //Do nothing if height is the same
      if(width === new_width && height === new_height) {
        return original;
      }
      
      new_width = Math.abs(new_width);
      new_height = Math.abs(new_height);
      
      var image     = image_creation_context.createImageData(new_width, new_height);
      var image_data = image.data;
      var x_ratio   = width / new_width;
      var y_ratio   = height / new_height;
      for (var i = 0; i < new_height; i++) {
        for (var j = 0; j < new_width; j++)  {
          var px = Math.floor(j * x_ratio);
          var py = Math.floor(i * y_ratio);
          for (var k = 0; k < block_size; k++) {
            image_data[Math.floor(i * new_width + j) * block_size + k] = data[Math.floor( py * width + px) * block_size + k];
          }
        }
      }
      
      return image;
    },


    /**
    * @doc function
    * @name VolumeViewer.utils.rotateUint16Array90Left<commit_msg>Tightened up nearest neighbor code.<commit_after>/*
* BrainBrowser: Web-based Neurological Visualization Tools
* (https://brainbrowser.cbrain.mcgill.ca)
*
* Copyright (C) 2011
* The Royal Institution for the Advancement of Learning
* McGill University
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
* Author: <NAME> <<EMAIL>> (http://tareksherif.ca/)
* Author: <NAME>
*/

(function() {
  "use strict";

  var image_creation_context = document.createElement("canvas").getContext("2d");

  BrainBrowser.VolumeViewer.utils = {
    /**
    * @doc object
    * @name VolumeViewer.utils.axis_to_number
    *
    * @property {xspace} Index of x space (0)
    * @property {yspace} Index of y space (1)
    * @property {zspace} Index of z space (2)
    *
    * @description
    * Convenience object for converting between space names
    * and indices.
    * ```js
    * BrainBrowser.VolumeViewer.utils.axis_to_number["xspace"];
    * ```
    */
    axis_to_number: {
      xspace: 0,
      yspace: 1,
      zspace: 2
    },

    /**
    * @doc function
    * @name VolumeViewer.utils.nearestNeighbor
    *
    * @param {array} original Original image
    * @param {number} new_width Width of scaled image.
    * @param {number} new_height Height of scaled image.
    * @returns {ImageData} The scaled image.
    *
    * @description
    * Scale an image using nearest neighbor interpolation.
    * ```js
    * BrainBrowser.VolumeViewer.utils.nearestNeighbor(image, 512, 512);
    * ```
    */
    nearestNeighbor: function(original, new_width, new_height) {
      var data = original.data;
      var width = original.width;
      var height = original.height;
      var block_size = 4;
      var image, image_data;
      var x_ratio, y_ratio;
      var source_x, source_y;
      var source_y_offset, source_block_offset;
      var dest_x, dest_y;
      var dest_y_offset, dest_block_offset;
      var k;

      if (new_width < 0 && new_height > 0) {
        data = flipImage(data, width, height, true, false, block_size);
      }

      //Do nothing if size is the same
      if(width === new_width && height === new_height) {
        return original;
      }
      
      new_width = Math.abs(new_width);
      new_height = Math.abs(new_height);
      
      image = image_creation_context.createImageData(new_width, new_height);
      image_data = image.data;
      x_ratio   = width / new_width;
      y_ratio   = height / new_height;
      for (dest_y = 0; dest_y < new_height; dest_y++) {
        source_y_offset = Math.floor(dest_y * y_ratio) * width;
        dest_y_offset = dest_y * new_width;

        for (dest_x = 0; dest_x < new_width; dest_x++)  {
          source_block_offset = (source_y_offset + Math.floor(dest_x * x_ratio)) * block_size;
          dest_block_offset = (dest_y_offset + dest_x) * block_size;

          for (k = 0; k < block_size; k++) {
            image_data[dest_block_offset+ k] = data[source_block_offset + k];
          }
        }
      }
      
      return image;
    },


    /**
    * @doc function
    * @name VolumeViewer.utils.rotateUint16Array90Left
{'fraction_non_alphanumeric': 0.10741687979539642, 'fraction_numerical': 0.007956805910770106, 'mean_word_length': 2.7105956773853452, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '4729349', 'n_tokens_mistral': 2243, 'n_tokens_neox': 2154, 'n_words': 667}