# Translating javascript to Processing ---Uint32Array---

**URL:** <https://discourse.processing.org/t/translating-javascript-to-processing-uint32array/38323>\
**Category:** Coding Questions\
**Created:** [August 12, 2022, 2:07pm UTC](https://discourse.processing.org/t/translating-javascript-to-processing-uint32array/38323 "2022-08-12T14:07:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeykech](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jeykech](https://discourse.processing.org/u/jeykech)\
**Post date:** [August 12, 2022, 2:07pm UTC](https://discourse.processing.org/t/translating-javascript-to-processing-uint32array/38323/1 "2022-08-12T14:07:01Z")

</div>

hi guys,

please I need your help translating chi piece of code from P5 to Processing.  
I am working on a code and came across this one from this Skitch : [QuadTree - OpenProcessing](https://openprocessing.org/sketch/940492)

I am having trouble understanding the this function :this.R = new Uint32Array(256);  
and in this case what type of data is it returning? is R an array or what ?

thanx

```auto
class Histogram{
	constructor(blob){
		this.img = blob;
		this.img.loadPixels();
		this.px = this.img.pixels;
		
		this.R = new Uint32Array(256);
		this.G = new Uint32Array(256);
		this.B = new Uint32Array(256);
		// this.A = new Uint32Array(256);
		this.total = 0;
		this.varianceR = this.varianceG = this.varianceB = 0;
		this.meanR = this.meanG = this.meanB = 0;
		this.stdDeviation = 0;
		this.error = 0;
		this.area_exponent = 0.25
		this.area_error = Math.pow(blob.width*blob.height, this.area_exponent);

		//histogram
		for(let i=0,v=0;i<this.px.length;i+=4){
			this.R[this.px[i + 0] ]++; //red
			this.G[this.px[i + 1] ]++; //green
			this.B[this.px[i + 2] ]++; //blue
			// this.A[this.px[i + 3] ]++; //alpha
		}
	}
	rgb(){
		return [this.R, this.G, this.B];
	}
	weightedAvg(){

		let totalR,totalG,totalB;
		let valueR,valueG,valueB;
		totalR = totalG = totalB = 0;
		valueR = valueG = valueB = 0;

		for(let i=0;i < 256;i++){
			totalR += this.R[I];
			totalG += this.G[I];
			totalB += this.B[I];
			//sum weighted 
			valueR += this.R[i] * I;
			valueG += this.G[i] * I;
			valueB += this.B[i] * I;
		}
		//True mean
		this.meanR = totalR / this.R.length;
		this.meanG = totalG / this.G.length;
		this.meanB = totalB / this.B.length;
		
		//what is this?
		valueR /= totalR;
		valueG /= totalG;
		valueB /= totalB;

	// weightedAvg
		for(let i=0;i < 256;i++){
			this.varianceR += sq( valueR - i ) * this.R[I];
			this.varianceG += sq( valueG - i ) * this.G[I];
			this.varianceB += sq( valueB - i ) * this.B[I];
		}
		this.varianceR = Math.sqrt(this.varianceR / totalR);
		this.varianceB = Math.sqrt(this.varianceB / totalB);
		this.varianceG = Math.sqrt(this.varianceG / totalG);
		
		this.total = (totalR + totalG + totalB) / 3;
		this.variance = this.varianceR * 0.2989 + this.varianceG * 0.5870 + this.varianceB * 0.1140;
		this.stdDeviation = Math.sqrt( this.variance );
		this.error = this.stdDeviation * this.area_error;
		
		return [valueR,valueG,valueB, this.error]; 
	}
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 12, 2022, 2:40pm UTC](https://discourse.processing.org/t/translating-javascript-to-processing-uint32array/38323/2 "2022-08-12T14:40:42Z")

</div>

> [@jeykech](#):
>
> … and in this case what type of data is it returning? is R an array or what?

> **[Uint32Array - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array)**
>
> The Uint32Array typed array represents an array of 32-bit unsigned integers in the platform byte order. If control over byte order is needed, use DataView instead. The contents are initialized to 0. Once established, you can reference elements in the...

Java’s corresponding container for JS’ Uint32Array would be an `int[]` array:

> **[Array / Reference](https://processing.org/reference/Array.html)**
>
> An array is a list of data. It is possible to have an array of any type of data. Each piece of data in an array is identified by an index number representing its position in the array. The first eleme…

Something like this: `final int[] R = new int[256];`

However pay attention if an `int` value bigger than [MAX\_INT](http://processing.github.io/processing-javadocs/core/processing/core/PConstants.html#MAX_INT) is being stored in an `int[]` container, which in case it would become a negative value:

> **[int / Reference](https://processing.org/reference/int.html)**
>
> Datatype for integers, numbers without a decimal point. Integers can be as large as 2,147,483,647 and as low as -2,147,483,648. They are stored as 32 bits of information. The first time a variable is …

> [@jeykech](#):
>
> I am having trouble understanding the `this` function: `this.R = new Uint32Array(256);`

Inside a Java class keyword [`this`](https://processing.org/reference/this.html) can be generally omitted; unless we have a local variable or parameter which matches (overshadows) the name of a field.

In Java classes we have to define all fields outside the constructor.  
So the field _R_ would be declared in a separate declaring block like this:

```auto
class Histogram {
  final int[] R = new int[256];

```

If you need more troubleshooting for the sketch language conversion, just ask again. 😃

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [August 12, 2022, 3:10pm UTC](https://discourse.processing.org/t/translating-javascript-to-processing-uint32array/38323/3 "2022-08-12T15:10:50Z")

</div>

Hi,

Seems not that hard to translate…

As a tipp, as this is maybe not obvious if you’re not that familiar on the java side datatypes …

the rgb function in the end should look like:

```auto
  int[][] rgb(){
    return new int[][] {R,G,B};
  }

```

and the weightedAvg

```auto
float[] weightedAvg() {
//...
  return new float[] {valueR,valueG,valueB, error}; 
}

```

Cheers  
— mnse

PS: As @GoToLoop already says

> [@GoToLoop](#):
>
> If you need more troubleshooting for the sketch language conversion, just ask again. 😃

---

<div class="post-metadata">

**Author:** ![jeykech](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jeykech](https://discourse.processing.org/u/jeykech)\
**Post date:** [August 12, 2022, 4:07pm UTC](https://discourse.processing.org/t/translating-javascript-to-processing-uint32array/38323/4 "2022-08-12T16:07:04Z")

</div>

@GoToLoop and @mnse  
thank you guys, i think this will help a lot for the moment !!  
I’ll come back to you if I need it !
