Skip to content

Commit

Permalink
Added comments to quantize.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnBeckner committed Apr 16, 2020
1 parent 293722b commit 41eb276
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions src/util/quantize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,13 @@ export function quantize(
return { sprite, palette };
}

// Used to find index of colors for building sprite colorArray
// This kills the imageArr (imageArr is destroyed by this function)
/**
* Used to find the index of colors for building Palette of
* quantized image
* NOTE: This function destroys imageArr
* @param imageArr The image to quantize as a 2D array
* @param colorArr List of unique colors in the image
*/
function getColorIndex(imageArr: number[][], colorArr: number[]): number {
for (let i = 0; i < imageArr.length; i++) {
if (
Expand All @@ -122,7 +127,10 @@ function getColorIndex(imageArr: number[][], colorArr: number[]): number {
return -1;
}

//converts imageObject into array of colors for k-means
/**
* converst image into array of RGB color values
* @param image The image to convert to 2d array
*/
function imageToArr(image: Bitmap): number[][] {
let imageArr = [];
for (let y = 0; y < image.dimensions.height; y++) {
Expand All @@ -138,10 +146,14 @@ function imageToArr(image: Bitmap): number[][] {
return imageArr;
}

//used to find clusters of similar points for image depth reduction (quantization)
//arrayToProcess: number array of colors; [[r, g, b], [r, g, b], ...]
//centroids: center point of clusters;
//clusters: number of clusters to generate
/**
* Used to find clusters of similar colors for image color quantization
* @param arrayToProcess 2D array of RGB color values for the image to quantize
* @param centroids List of center points for each color cluster
* 2D array of RGB values
* @param clusters 3D array of clusters for color quantization
* Each centroid is associated with a cluster
*/
function kmeans(
arrayToProcess: number[][],
centroids: number[][],
Expand Down Expand Up @@ -232,15 +244,13 @@ function kmeans(
return { groups: Groups, centers: centroids };
}

//function to determine if there exists a group of centroids with specific
//average distance between them
//points: list of points to search
//midDist: desired average distance
//numCentroids: number of points to find
//
//returns:
//possible (boolean)
//centers (list of points with desired distance)
/**
* Used to determine if there exists a group of points with a at most
* minDist distance between them
* @param points list of points to search
* @param midDist average distance to check for
* @param numCentroids number of points to find
*/
function centroidPossible(
points: number[][],
midDist: number,
Expand Down Expand Up @@ -277,6 +287,14 @@ function centroidPossible(

//binary search to find centroids, reutrn list of centroids with
// average largest distance between them

/**
* Used to find optimal centroids for kmeans
* Uses binary search to find the group of centroids with min average distance
* between them
* @param uniqueColors 2D array of unique RGB color values
* @param depth number of centroids to find
*/
function findCentroids(
uniqueColors: number[][],
depth: number
Expand Down

0 comments on commit 41eb276

Please sign in to comment.