Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

120
LINES

< > BotCompany Repo | #1032535 // IntegralImage in JavaScript!

JavaScript

1  
/* Generated from Java with JSweet 3.1.0 - http://www.jsweet.org */
2  
var quickstart;
3  
(function (quickstart) {
4  
    class main {
5  
        static doubleRatio(x, y) {
6  
            return y === 0 ? 0 : x / y;
7  
        }
8  
    }
9  
    quickstart.main = main;
10  
    main["__class"] = "quickstart.main";
11  
    (function (main) {
12  
        class IntegralImage {
13  
            constructor(w, h, pixels) {
14  
                if (this.w === undefined) {
15  
                    this.w = 0;
16  
                }
17  
                if (this.h === undefined) {
18  
                    this.h = 0;
19  
                }
20  
                if (this.data === undefined) {
21  
                    this.data = null;
22  
                }
23  
                this.h = h;
24  
                this.w = w;
25  
                this.data = (s => { let a = []; while (s-- > 0)
26  
                    a.push(0); return a; })(w * h * 3);
27  
                let i = 0;
28  
                let j = 0;
29  
                let sumR = 0;
30  
                let sumG = 0;
31  
                let sumB = 0;
32  
                for (let x = 0; x < w; x++) {
33  
                    {
34  
                        const rgb = pixels[j++];
35  
                        this.data[i++] = (sumR += (rgb >> 16) & 255);
36  
                        this.data[i++] = (sumG += (rgb >> 8) & 255);
37  
                        this.data[i++] = (sumB += rgb & 255);
38  
                    }
39  
                    ;
40  
                }
41  
                for (let y = 1; y < h; y++) {
42  
                    {
43  
                        sumR = sumG = sumB = 0;
44  
                        for (let x = 0; x < w; x++) {
45  
                            {
46  
                                const rgb = pixels[j++];
47  
                                sumR += (rgb >> 16) & 255;
48  
                                sumG += (rgb >> 8) & 255;
49  
                                sumB += rgb & 255;
50  
                                this.data[i] = sumR + this.data[i - w * 3];
51  
                                this.data[i + 1] = sumG + this.data[i - w * 3 + 1];
52  
                                this.data[i + 2] = sumB + this.data[i - w * 3 + 2];
53  
                                i += 3;
54  
                            }
55  
                            ;
56  
                        }
57  
                    }
58  
                    ;
59  
                }
60  
            }
61  
            getIntegralValue$int$int$int(x, y, channel) {
62  
                return x < 0 || y < 0 || x >= this.w || y >= this.h ? 0 : this.data[(y * this.w + x) * 3 + channel];
63  
            }
64  
            getIntegralValue(x, y, channel) {
65  
                if (((typeof x === 'number') || x === null) && ((typeof y === 'number') || y === null) && ((typeof channel === 'number') || channel === null)) {
66  
                    return this.getIntegralValue$int$int$int(x, y, channel);
67  
                }
68  
                else if (((typeof x === 'number') || x === null) && ((typeof y === 'number') || y === null) && channel === undefined) {
69  
                    return this.getIntegralValue$int$int(x, y);
70  
                }
71  
                else
72  
                    throw new Error('invalid overload');
73  
            }
74  
            getIntegralValue$int$int(x, y) {
75  
                if (x < 0 || y < 0 || x >= this.w || y >= this.h)
76  
                    return 0;
77  
                const i = (y * this.w + x) * 3;
78  
                return this.data[i] + this.data[i + 1] + this.data[i + 2];
79  
            }
80  
            averageBrightness() {
81  
                return main.doubleRatio(this.getIntegralValue$int$int(this.w - 1, this.h - 1), this.w * this.h * 3 * 255.0);
82  
            }
83  
            toString() {
84  
                return "IntegralImage " + this.w + "*" + this.h + ", brightness: " + this.averageBrightness();
85  
            }
86  
            getWidth() {
87  
                return this.w;
88  
            }
89  
            getHeight() {
90  
                return this.h;
91  
            }
92  
            rectSum$int$int$int$int$int(x1, y1, x2, y2, channel) {
93  
                const bottomLeft = this.getIntegralValue$int$int$int(x1 - 1, y2 - 1, channel);
94  
                const bottomRight = this.getIntegralValue$int$int$int(x2 - 1, y2 - 1, channel);
95  
                const topLeft = this.getIntegralValue$int$int$int(x1 - 1, y1 - 1, channel);
96  
                const topRight = this.getIntegralValue$int$int$int(x2 - 1, y1 - 1, channel);
97  
                return bottomRight + topLeft - topRight - bottomLeft;
98  
            }
99  
            rectSum(x1, y1, x2, y2, channel) {
100  
                if (((typeof x1 === 'number') || x1 === null) && ((typeof y1 === 'number') || y1 === null) && ((typeof x2 === 'number') || x2 === null) && ((typeof y2 === 'number') || y2 === null) && ((typeof channel === 'number') || channel === null)) {
101  
                    return this.rectSum$int$int$int$int$int(x1, y1, x2, y2, channel);
102  
                }
103  
                else if (((typeof x1 === 'number') || x1 === null) && ((typeof y1 === 'number') || y1 === null) && ((typeof x2 === 'number') || x2 === null) && ((typeof y2 === 'number') || y2 === null) && channel === undefined) {
104  
                    return this.rectSum$int$int$int$int(x1, y1, x2, y2);
105  
                }
106  
                else
107  
                    throw new Error('invalid overload');
108  
            }
109  
            rectSum$int$int$int$int(x1, y1, x2, y2) {
110  
                const bottomLeft = this.getIntegralValue$int$int(x1 - 1, y2 - 1);
111  
                const bottomRight = this.getIntegralValue$int$int(x2 - 1, y2 - 1);
112  
                const topLeft = this.getIntegralValue$int$int(x1 - 1, y1 - 1);
113  
                const topRight = this.getIntegralValue$int$int(x2 - 1, y1 - 1);
114  
                return bottomRight + topLeft - topRight - bottomLeft;
115  
            }
116  
        }
117  
        main.IntegralImage = IntegralImage;
118  
        IntegralImage["__class"] = "quickstart.main.IntegralImage";
119  
    })(main = quickstart.main || (quickstart.main = {}));
120  
})(quickstart || (quickstart = {}));

Author comment

Began life as a copy of #1032003

download  show line numbers   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032535
Snippet name: IntegralImage in JavaScript!
Eternal ID of this version: #1032535/2
Text MD5: 4019ec9c98e7ff8cf0bf93106957d394
Author: stefan
Category: javax / gui
Type: JavaScript
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-20 17:20:32
Source code size: 5927 bytes / 120 lines
Pitched / IR pitched: No / No
Views / Downloads: 83 / 43
Version history: 1 change(s)
Referenced in: [show references]