Comparing version 7.0.1 to 7.0.2
# CHANGELOG | ||
## v7.0.2 (2018-07-08) | ||
- UMD dist transpiled to ES5. | ||
## v7.0.1 (2018-07-03) | ||
@@ -4,0 +7,0 @@ - Fix TypeScript declaration file. |
@@ -1,180 +0,42 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global.bitDepth = factory()); | ||
}(this, (function () { 'use strict'; | ||
/* | ||
* bitdepth: Change the resolution of samples to and from any bit depth. | ||
* https://github.com/rochars/bitdepth | ||
* | ||
* Copyright (c) 2017-2018 Rafael da Silva Rocha. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
/** | ||
* @fileoverview The bitdepth() function and private helper functions. | ||
*/ | ||
/** @module bitdepth */ | ||
/** @private */ | ||
const f64f32_ = new Float32Array(1); | ||
/** | ||
* Change the bit depth of samples. The input array. | ||
* @param {!TypedArray} input The samples. | ||
* @param {string} original The original bit depth of the data. | ||
* One of "8" ... "53", "32f", "64" | ||
* @param {string} target The desired bit depth for the data. | ||
* One of "8" ... "53", "32f", "64" | ||
* @param {!TypedArray} output The output array. | ||
*/ | ||
function bitDepth(input, original, target, output) { | ||
validateBitDepth_(original); | ||
validateBitDepth_(target); | ||
/** @type {!Function} */ | ||
let toFunction = getBitDepthFunction_(original, target); | ||
/** @type {!Object<string, number>} */ | ||
let options = { | ||
oldMin: Math.pow(2, parseInt(original, 10)) / 2, | ||
newMin: Math.pow(2, parseInt(target, 10)) / 2, | ||
oldMax: (Math.pow(2, parseInt(original, 10)) / 2) - 1, | ||
newMax: (Math.pow(2, parseInt(target, 10)) / 2) - 1, | ||
}; | ||
/** @type {number} */ | ||
const len = input.length; | ||
// sign the samples if original is 8-bit | ||
if (original == "8") { | ||
for (let i=0; i<len; i++) { | ||
output[i] = input[i] -= 128; | ||
} | ||
} | ||
// change the resolution of the samples | ||
for (let i=0; i<len; i++) { | ||
output[i] = toFunction(input[i], options); | ||
} | ||
// unsign the samples if target is 8-bit | ||
if (target == "8") { | ||
for (let i=0; i<len; i++) { | ||
output[i] = output[i] += 128; | ||
} | ||
} | ||
} | ||
/** | ||
* Change the bit depth from int to int. | ||
* @param {number} sample The sample. | ||
* @param {!Object<string, number>} args Data about the original and target bit depths. | ||
* @return {number} | ||
* @private | ||
*/ | ||
function intToInt_(sample, args) { | ||
if (sample > 0) { | ||
sample = parseInt((sample / args.oldMax) * args.newMax, 10); | ||
} else { | ||
sample = parseInt((sample / args.oldMin) * args.newMin, 10); | ||
} | ||
return sample; | ||
} | ||
/** | ||
* Change the bit depth from float to int. | ||
* @param {number} sample The sample. | ||
* @param {!Object<string, number>} args Data about the original and target bit depths. | ||
* @return {number} | ||
* @private | ||
*/ | ||
function floatToInt_(sample, args) { | ||
return parseInt( | ||
sample > 0 ? sample * args.newMax : sample * args.newMin, 10); | ||
} | ||
/** | ||
* Change the bit depth from int to float. | ||
* @param {number} sample The sample. | ||
* @param {!Object<string, number>} args Data about the original and target bit depths. | ||
* @return {number} | ||
* @private | ||
*/ | ||
function intToFloat_(sample, args) { | ||
return sample > 0 ? sample / args.oldMax : sample / args.oldMin; | ||
} | ||
/** | ||
* Change the bit depth from float to float. | ||
* @param {number} sample The sample. | ||
* @return {number} | ||
* @private | ||
*/ | ||
function floatToFloat_(sample) { | ||
f64f32_[0] = sample; | ||
return f64f32_[0]; | ||
} | ||
/** | ||
* Return the function to change the bit depth of a sample. | ||
* @param {string} original The original bit depth of the data. | ||
* One of "8" ... "53", "32f", "64" | ||
* @param {string} target The new bit depth of the data. | ||
* One of "8" ... "53", "32f", "64" | ||
* @return {!Function} | ||
* @private | ||
*/ | ||
function getBitDepthFunction_(original, target) { | ||
/** @type {!Function} */ | ||
let func = function(x) {return x;}; | ||
if (original != target) { | ||
if (["32f", "64"].includes(original)) { | ||
if (["32f", "64"].includes(target)) { | ||
func = floatToFloat_; | ||
} else { | ||
func = floatToInt_; | ||
} | ||
} else { | ||
if (["32f", "64"].includes(target)) { | ||
func = intToFloat_; | ||
} else { | ||
func = intToInt_; | ||
} | ||
} | ||
} | ||
return func; | ||
} | ||
/** | ||
* Validate the bit depth. | ||
* @param {string} bitDepth The original bit depth. | ||
* Should be one of "8" ... "53", "32f" or "64". | ||
* @throws {Error} If any argument does not meet the criteria. | ||
* @private | ||
*/ | ||
function validateBitDepth_(bitDepth) { | ||
if ((bitDepth != "32f" && bitDepth != "64") && | ||
(parseInt(bitDepth, 10) < "8" || parseInt(bitDepth, 10) > "53")) { | ||
throw new Error("Invalid bit depth."); | ||
} | ||
} | ||
return bitDepth; | ||
}))); | ||
(function (global, factory) {typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :typeof define === 'function' && define.amd ? define(factory) :(global.bitDepth = factory());}(this, (function () { 'use strict';var bitDepth=function(){/** @private @const */ var f64f32_=new Float32Array(1);/** | ||
@param {!TypedArray} input | ||
@param {string} original | ||
@param {string} target | ||
@param {!TypedArray} output | ||
*/ | ||
function bitDepth(input,original,target,output){validateBitDepth_(original);validateBitDepth_(target);/** @type {!Function} */ var toFunction=getBitDepthFunction_(original,target);/** @type {!Object<string,number>} */ var options={oldMin:Math.pow(2,parseInt(original,10))/2,newMin:Math.pow(2,parseInt(target,10))/2,oldMax:Math.pow(2,parseInt(original,10))/2-1,newMax:Math.pow(2,parseInt(target,10))/2-1};/** @const @type {number} */ var len=input.length;if(original=="8")for(var i=0;i<len;i++)output[i]= | ||
input[i]-=128;for(var i$0=0;i$0<len;i$0++)output[i$0]=toFunction(input[i$0],options);if(target=="8")for(var i$1=0;i$1<len;i$1++)output[i$1]=output[i$1]+=128}/** | ||
@private | ||
@param {number} sample | ||
@param {!Object<string,number>} args | ||
@return {number} | ||
*/ | ||
function intToInt_(sample,args){if(sample>0)sample=parseInt(sample/args.oldMax*args.newMax,10);else sample=parseInt(sample/args.oldMin*args.newMin,10);return sample}/** | ||
@private | ||
@param {number} sample | ||
@param {!Object<string,number>} args | ||
@return {number} | ||
*/ | ||
function floatToInt_(sample,args){return parseInt(sample>0?sample*args.newMax:sample*args.newMin,10)}/** | ||
@private | ||
@param {number} sample | ||
@param {!Object<string,number>} args | ||
@return {number} | ||
*/ | ||
function intToFloat_(sample,args){return sample>0?sample/args.oldMax:sample/args.oldMin}/** | ||
@private | ||
@param {number} sample | ||
@return {number} | ||
*/ | ||
function floatToFloat_(sample){f64f32_[0]=sample;return f64f32_[0]}/** | ||
@private | ||
@param {string} original | ||
@param {string} target | ||
@return {!Function} | ||
*/ | ||
function getBitDepthFunction_(original,target){/** @type {!Function} */ var func=function(x){return x};if(original!=target)if(["32f","64"].includes(original))if(["32f","64"].includes(target))func=floatToFloat_;else func=floatToInt_;else if(["32f","64"].includes(target))func=intToFloat_;else func=intToInt_;return func}/** | ||
@private | ||
@param {string} bitDepth | ||
@throws {Error} | ||
*/ | ||
function validateBitDepth_(bitDepth){if(bitDepth!="32f"&&bitDepth!="64"&&(parseInt(bitDepth,10)<"8"||parseInt(bitDepth,10)>"53"))throw new Error("Invalid bit depth.");}return bitDepth}();return bitDepth; }))); |
# Distribution | ||
This library is a ES6 module also distributed as a CommonJS module, UMD module and a compiled script for browsers. It works out of the box in Node when installed with ```npm install bitdepth```. | ||
This library is a ES module also distributed as a CommonJS module, UMD module and a compiled script for browsers. It works out of the box in Node when installed with ```npm install bitdepth```. It includes a TypeScript definition file. | ||
If you use the [Closure Compiler](https://github.com/google/closure-compiler), this package includes a externs file: **./externs.js**. | ||
## If you are using this lib in a browser: | ||
You may load both **bitdepth.umd.js** and **bitdepth.min.js** in the browser with ```<script>``` tags. Ideally you should use **bitdepth.min.js**. You can load it via the https://unpkg.com and https://www.jsdelivr.com/ CDNs: | ||
You may load both **./dist/bitdepth.umd.js** and **./dist/bitdepth.min.js** in the browser with ```<script>``` tags. Ideally you should use **bitdepth.min.js**. You can load it via the https://unpkg.com and https://www.jsdelivr.com/ CDNs: | ||
[unpkg](https://www.unpkg.com): | ||
[unpkg](https://unpkg.com/bitdepth): | ||
```html | ||
@@ -13,3 +15,3 @@ <script src="https://unpkg.com/bitdepth"></script> | ||
[jsDelivr](https://www.jsdelivr.com): | ||
[jsDelivr](https://cdn.jsdelivr.net/npm/bitdepth): | ||
```html | ||
@@ -21,11 +23,11 @@ <script src="https://cdn.jsdelivr.net/npm/bitdepth"></script> | ||
- The **CommonJS** is the dist file used by Node. It is served in the "main" field of package.json. It includes all the sources but no dependencies. Dependencies will be imported from the **node_modules** folder. This is the source you are running when you **npm install bitdepth**. | ||
- The **CommonJS** dist is **./dist/bitdepth.cjs.js**. It is the dist file used by Node. It is served in the "main" field of package.json and is the source you are running when you **npm install bitdepth**. It is not compiled or minified. | ||
- The **UMD** module is compatible with Node, AMD and browsers. It is served in the "browser" field of package.json. It includes all dependencies. This file is not compiled/minified as it may be used by module bundlers. Compilation/minification should be up to the bundler consuming this file. | ||
- The **UMD** module is **./dist/bitdepth.umd.js**. It is transpiled to ES5 and compatible with Node, AMD and browsers. It is served in the "browser" field of package.json. | ||
- The **compiled dist** is browser-only and should be the one served by CDNs. It includes all the dependencies. It is used in the "unpkg" and "jsdelivr" fields of package.json. | ||
- The **browser-only** dist is **./dist/bitdepth.min.js**. It is transpiled to ES5 and compiled. It is used in the "unpkg" and "jsdelivr" fields of package.json. | ||
- The **ES6 dist** is **bitdepth.js**, served as "es2015" in package.json. It includes all the dependencies. It is not compiled/minified. | ||
- The **ES6 dist** is **./dist/bitdepth.js**, served as "es2015" in package.json. It is not compiled/minified. | ||
- **./index.js** is served as "module" in package.json. It should be used by systems that support ES modules and are aware of Node's module path resolution (a module bundler, for instance). This should be the entry point for bundlers in most cases - this will avoid code duplication in the case of shared dependencies (as opposed to using "browser" as the entry point). | ||
- **./index.js** is served as "module" in package.json. This should be the entry point for bundlers. | ||
@@ -32,0 +34,0 @@ If your module bundler is using "browser" as the entry point **your dist should work the same** but will be a larger file. |
{ | ||
"name": "bitdepth", | ||
"version": "7.0.1", | ||
"version": "7.0.2", | ||
"description": "Change the resolution of samples.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/rochars/bitdepth", |
@@ -5,3 +5,3 @@ # bitdepth | ||
[![NPM version](https://img.shields.io/npm/v/bitdepth.svg?style=for-the-badge)](https://www.npmjs.com/package/bitdepth) [![Docs](https://img.shields.io/badge/docs-online-blue.svg?style=for-the-badge)](https://rochars.github.io/bitdepth/index.html) | ||
[![NPM version](https://img.shields.io/npm/v/bitdepth.svg?style=for-the-badge)](https://www.npmjs.com/package/bitdepth) [![Docs](https://img.shields.io/badge/docs-online-blue.svg?style=for-the-badge)](https://rochars.github.io/bitdepth/index.html) [![Tests](https://img.shields.io/badge/tests-online-blue.svg?style=for-the-badge)](https://rawgit.com/rochars/bitdepth/master/test/browser.html) | ||
[![Codecov](https://img.shields.io/codecov/c/github/rochars/bitdepth.svg?style=flat-square)](https://codecov.io/gh/rochars/bitdepth) [![Unix Build](https://img.shields.io/travis/rochars/bitdepth.svg?style=flat-square)](https://travis-ci.org/rochars/bitdepth) [![Windows Build](https://img.shields.io/appveyor/ci/rochars/bitdepth.svg?style=flat-square&logo=appveyor)](https://ci.appveyor.com/project/rochars/bitdepth) [![Scrutinizer](https://img.shields.io/scrutinizer/g/rochars/bitdepth.svg?style=flat-square&logo=scrutinizer)](https://scrutinizer-ci.com/g/rochars/bitdepth/) | ||
@@ -22,11 +22,11 @@ | ||
### ES6 | ||
import bitDepth from **bitdepth.js**: | ||
### Node | ||
```javascript | ||
import bitDepth from 'bitdepth.js'; | ||
const bitDepth = require("bitdepth"); | ||
``` | ||
### Node | ||
### ES module | ||
import bitDepth from **./dist/bitdepth.js**: | ||
```javascript | ||
const bitDepth = require("bitdepth"); | ||
import bitDepth from './dist/bitdepth.js'; | ||
``` | ||
@@ -37,6 +37,6 @@ | ||
```html | ||
<script src="bitdepth.min.js"></script> | ||
<script src="./dist/bitdepth.min.js"></script> | ||
``` | ||
Or get it from the [jsDelivr](https://www.jsdelivr.com) CDN: | ||
Or get it from the [jsDelivr](https://cdn.jsdelivr.net/npm/bitdepth) CDN: | ||
```html | ||
@@ -46,3 +46,3 @@ <script src="https://cdn.jsdelivr.net/npm/bitdepth"></script> | ||
Or get it from [unpkg](https://www.unpkg.com): | ||
Or get it from [unpkg](https://unpkg.com/bitdepth): | ||
```html | ||
@@ -74,2 +74,35 @@ <script src="https://unpkg.com/bitdepth"></script> | ||
## Distribution | ||
This library is a ES module also distributed as a CommonJS module, UMD module and a compiled script for browsers. It works out of the box in Node when installed with ```npm install bitdepth```. It includes a TypeScript definition file. | ||
If you use the [Closure Compiler](https://github.com/google/closure-compiler), this package includes a externs file: **./externs.js**. | ||
### If you are using this lib in a browser: | ||
You may load both **./dist/bitdepth.umd.js** and **./dist/bitdepth.min.js** in the browser with ```<script>``` tags. Ideally you should use **bitdepth.min.js**. You can load it via the https://unpkg.com and https://www.jsdelivr.com/ CDNs: | ||
[unpkg](https://unpkg.com/bitdepth): | ||
```html | ||
<script src="https://unpkg.com/bitdepth"></script> | ||
``` | ||
[jsDelivr](https://cdn.jsdelivr.net/npm/bitdepth): | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/bitdepth"></script> | ||
``` | ||
### If you are using this lib as a dependency: | ||
- The **CommonJS** dist is **./dist/bitdepth.cjs.js**. It is the dist file used by Node. It is served in the "main" field of package.json and is the source you are running when you **npm install bitdepth**. It is not compiled or minified. | ||
- The **UMD** module is **./dist/bitdepth.umd.js**. It is transpiled to ES5 and compatible with Node, AMD and browsers. It is served in the "browser" field of package.json. | ||
- The **browser-only** dist is **./dist/bitdepth.min.js**. It is transpiled to ES5 and compiled. It is used in the "unpkg" and "jsdelivr" fields of package.json. | ||
- The **ES6 dist** is **./dist/bitdepth.js**, served as "es2015" in package.json. It is not compiled/minified. | ||
- **./index.js** is served as "module" in package.json. This should be the entry point for bundlers. | ||
If your module bundler is using "browser" as the entry point **your dist should work the same** but will be a larger file. | ||
## LICENSE | ||
@@ -76,0 +109,0 @@ Copyright (c) 2017-2018 Rafael da Silva Rocha. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
124
34344
561