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

120
LINES

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

JavaScript

/* Generated from Java with JSweet 3.1.0 - http://www.jsweet.org */
var quickstart;
(function (quickstart) {
    class main {
        static doubleRatio(x, y) {
            return y === 0 ? 0 : x / y;
        }
    }
    quickstart.main = main;
    main["__class"] = "quickstart.main";
    (function (main) {
        class IntegralImage {
            constructor(w, h, pixels) {
                if (this.w === undefined) {
                    this.w = 0;
                }
                if (this.h === undefined) {
                    this.h = 0;
                }
                if (this.data === undefined) {
                    this.data = null;
                }
                this.h = h;
                this.w = w;
                this.data = (s => { let a = []; while (s-- > 0)
                    a.push(0); return a; })(w * h * 3);
                let i = 0;
                let j = 0;
                let sumR = 0;
                let sumG = 0;
                let sumB = 0;
                for (let x = 0; x < w; x++) {
                    {
                        const rgb = pixels[j++];
                        this.data[i++] = (sumR += (rgb >> 16) & 255);
                        this.data[i++] = (sumG += (rgb >> 8) & 255);
                        this.data[i++] = (sumB += rgb & 255);
                    }
                    ;
                }
                for (let y = 1; y < h; y++) {
                    {
                        sumR = sumG = sumB = 0;
                        for (let x = 0; x < w; x++) {
                            {
                                const rgb = pixels[j++];
                                sumR += (rgb >> 16) & 255;
                                sumG += (rgb >> 8) & 255;
                                sumB += rgb & 255;
                                this.data[i] = sumR + this.data[i - w * 3];
                                this.data[i + 1] = sumG + this.data[i - w * 3 + 1];
                                this.data[i + 2] = sumB + this.data[i - w * 3 + 2];
                                i += 3;
                            }
                            ;
                        }
                    }
                    ;
                }
            }
            getIntegralValue$int$int$int(x, y, channel) {
                return x < 0 || y < 0 || x >= this.w || y >= this.h ? 0 : this.data[(y * this.w + x) * 3 + channel];
            }
            getIntegralValue(x, y, channel) {
                if (((typeof x === 'number') || x === null) && ((typeof y === 'number') || y === null) && ((typeof channel === 'number') || channel === null)) {
                    return this.getIntegralValue$int$int$int(x, y, channel);
                }
                else if (((typeof x === 'number') || x === null) && ((typeof y === 'number') || y === null) && channel === undefined) {
                    return this.getIntegralValue$int$int(x, y);
                }
                else
                    throw new Error('invalid overload');
            }
            getIntegralValue$int$int(x, y) {
                if (x < 0 || y < 0 || x >= this.w || y >= this.h)
                    return 0;
                const i = (y * this.w + x) * 3;
                return this.data[i] + this.data[i + 1] + this.data[i + 2];
            }
            averageBrightness() {
                return main.doubleRatio(this.getIntegralValue$int$int(this.w - 1, this.h - 1), this.w * this.h * 3 * 255.0);
            }
            toString() {
                return "IntegralImage " + this.w + "*" + this.h + ", brightness: " + this.averageBrightness();
            }
            getWidth() {
                return this.w;
            }
            getHeight() {
                return this.h;
            }
            rectSum$int$int$int$int$int(x1, y1, x2, y2, channel) {
                const bottomLeft = this.getIntegralValue$int$int$int(x1 - 1, y2 - 1, channel);
                const bottomRight = this.getIntegralValue$int$int$int(x2 - 1, y2 - 1, channel);
                const topLeft = this.getIntegralValue$int$int$int(x1 - 1, y1 - 1, channel);
                const topRight = this.getIntegralValue$int$int$int(x2 - 1, y1 - 1, channel);
                return bottomRight + topLeft - topRight - bottomLeft;
            }
            rectSum(x1, y1, x2, y2, channel) {
                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)) {
                    return this.rectSum$int$int$int$int$int(x1, y1, x2, y2, channel);
                }
                else if (((typeof x1 === 'number') || x1 === null) && ((typeof y1 === 'number') || y1 === null) && ((typeof x2 === 'number') || x2 === null) && ((typeof y2 === 'number') || y2 === null) && channel === undefined) {
                    return this.rectSum$int$int$int$int(x1, y1, x2, y2);
                }
                else
                    throw new Error('invalid overload');
            }
            rectSum$int$int$int$int(x1, y1, x2, y2) {
                const bottomLeft = this.getIntegralValue$int$int(x1 - 1, y2 - 1);
                const bottomRight = this.getIntegralValue$int$int(x2 - 1, y2 - 1);
                const topLeft = this.getIntegralValue$int$int(x1 - 1, y1 - 1);
                const topRight = this.getIntegralValue$int$int(x2 - 1, y1 - 1);
                return bottomRight + topLeft - topRight - bottomLeft;
            }
        }
        main.IntegralImage = IntegralImage;
        IntegralImage["__class"] = "quickstart.main.IntegralImage";
    })(main = quickstart.main || (quickstart.main = {}));
})(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: 145 / 70
Version history: 1 change(s)
Referenced in: