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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//create a class called Region
class Region {
    #weight = 1;
    constructor(x, y, width, height, prompt, weight) {
        this.enabled = true;
        this.x = x;
        this.y = y;
        this.width = Math.max(width, 16);
        this.height = Math.max(height, 16);
        this.prompt = prompt;
        this.weight = weight;
        this.mouse_drag_offset = { x: 0, y: 0 };
        this.tool_operation_mode = "none";
        this.selected = false;
    }
    get weight() {
        return this.#weight;
    }
    set weight(value) {
        if (value > 1) {
            value = 1;
        } else if (value < -1) {
            value = -1;
        }
        this.#weight = value;
    }
    snap(canvas_width, canvas_height, rows, columns) {
        this.stayWithinBounds(canvas_width, canvas_height);
        let cell_width = Math.round(canvas_width / columns);
        let cell_height = Math.round(canvas_height / rows);
        //snap the x and y values to the nearest grid point
        this.x = Math.round(this.x / cell_width) * cell_width;
        this.y = Math.round(this.y / cell_height) * cell_height;

        this.width = Math.max(Math.round(this.width / cell_width) * cell_width, 16);
        this.height = Math.max(Math.round(this.height / cell_height) * cell_height, 16);
        
    }

    getRatios(canvas_width, canvas_height) {
        let w = canvas_width / (this.width)
        let h = canvas_height / (this.height)
        return {
            division: [h, w],
            position: [this.y / (canvas_height / h), this.x / (canvas_width / w)]
        };
    }

    onMouseMove(mouse_position, canvas_width, canvas_height) {
        if (this.enabled == false) {
            return
        }
        if (this.selected) {
            if(this.tool_operation_mode == "move") {
                this.move({
                    x: mouse_position.x - this.mouse_drag_offset.x, 
                    y: mouse_position.y - this.mouse_drag_offset.y},
                    canvas_width, canvas_height);
            }
            if(this.tool_operation_mode == "scale") {
                this.scale({
                    x: mouse_position.x, 
                    y: mouse_position.y},
                    canvas_width, canvas_height);
            }
        }
    }

    selectIfMouseOver(mouse_position) {
        if (this.enabled == false) {
            return
        }
        //determine if the mouse is over this region.
        if (this.pointInside(mouse_position)) {
            this.mouse_drag_offset = { x: mouse_position.x - this.x, y: mouse_position.y - this.y };
            this.selected = true;
            this.tool_operation_mode = "move";
        }

        //determine if the mouse is over the scale control
        if (this.pointInsideScaleControl(mouse_position)) {
            this.tool_operation_mode = "scale";
        }

        return this.selected;
    }

    // When the mouse is clicked and released.
    onMouseUp(mouse_position) {
        if (this.enabled == false) {
            return
        }
        if (this.selected) {
            this.selected = false;
            this.tool_operation_mode = "none";
        }
    }

    // When the mouse leaves the canvas element.
    onMouseLeave(canvas_width, canvas_height) {
        if (this.enabled == false) {
            return
        }
        if (this.selected) {
            this.selected = false;
            this.tool_operation_mode = "none";
            //move this region so that is back within the bounds of the canvas
            this.stayWithinBounds(canvas_width, canvas_height);
        }
    }
    
    stayWithinBounds(canvas_width, canvas_height) {
        this.stayInBoundsWhileMoving(canvas_width, canvas_height);
        this.stayWithinBoundsWhileScaling(canvas_width, canvas_height);
    }

    stayWithinBoundsWhileScaling(canvas_width, canvas_height) {
        if (this.x + this.width > canvas_width) {
            this.width = canvas_width - this.x;
        }
        if (this.y + this.height > canvas_height) {
            this.height = canvas_height - this.y;
        }
    }


    stayInBoundsWhileMoving(canvas_width, canvas_height) {
        this.x = Math.max(0, Math.min(this.x, canvas_width - this.width));
        this.y = Math.max(0, Math.min(this.y, canvas_height - this.height));
    }

    move(point, canvas_width, canvas_height) {
        this.x = Math.round(point.x);
        this.y = Math.round(point.y);
        this.stayInBoundsWhileMoving(canvas_width, canvas_height);
    }

    scale(point, canvas_width, canvas_height) {
        console.log("Scaling")
        this.width = point.x - this.x + 8;
        this.height = point.y - this.y + 8;
        //ensure that the width and height of the region are not less than 1
        if (this.width < 16) {
            this.width = 16;
        }
        if (this.height < 16) {
            this.height = 16;
        }
        this.stayWithinBoundsWhileScaling(canvas_width, canvas_height);
    }

    pointInside(point) {
        return (
            point.x >= this.x &&
            point.x <= this.x + this.width &&
            point.y >= this.y &&
            point.y <= this.y + this.height
        );
    }

    pointInsideScaleControl(point) {
        return (
            point.x >= this.x + this.width - 16 &&
            point.x <= this.x + this.width &&
            point.y >= this.y + this.height - 16 &&
            point.y <= this.y + this.height);

    }

    draw(ctx, label) {
        if (this.enabled == false) {
            return
        }
        ctx.beginPath();
        ctx.fillStyle = 'rgba(0,0,0,0.65)';
        ctx.lineWidth = 1;
        ctx.fillRect(this.x, this.y, this.width, this.height);
        ctx.stroke();
        ctx.closePath();
        //draw a red rangle in the bottom-right corner
        ctx.beginPath();
        ctx.fillStyle = 'rgba(255,0,0,0.5)';
        ctx.lineWidth = 1;
        ctx.fillRect(this.x + this.width - 16, this.y + this.height - 16, 16, 16);
        ctx.stroke();
        ctx.closePath();

        //draw a white line around the region
        ctx.beginPath();
        ctx.strokeStyle = 'rgba(255,255,255,1)';
        ctx.lineWidth = 2;
        ctx.strokeRect(this.x, this.y, this.width, this.height);
        ctx.closePath();
        ctx.fill();

        if(this.selected) {
            ctx.beginPath();
            //set color to red
            ctx.strokeStyle = 'rgba(255,0,0,1)';
            ctx.lineWidth = 4;
            ctx.rect(this.x, this.y, this.width, this.height);
            ctx.stroke();
            ctx.closePath();
        }
        //draw the label text inside the rectangle
        ctx.fillStyle = 'rgba(255,255,255,1)';
        ctx.font = '16px Arial';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';
        ctx.fillText(label, this.x + this.width / 2, this.y + this.height / 2);

    }
}

var latent_couple_mapper = new Vue({
    el: '#latent_couple_mapper',
    mounted:function(){
        this.redrawCanvas();
    },
    data: {
        regions: [],
        grid_enabled: true,
        grid_rows: 8,
        grid_columns: 8,
        image_loaded: false,
        image_resource: null,
        canvas_width: 512,
        canvas_height: 512,
        global_region: new Region(0, 0, 512, 512, "", 0.2),
        instructions_visible: false,
        
    },
    watch: {
        canvas_width: function() {
            this.$refs.canvas.width = Math.max(this.canvas_width, 64);
            this.global_region.width = this.$refs.canvas.width;
            for(let i = 0; i < this.regions.length; i++) {
                this.regions[i].stayWithinBounds(this.$refs.canvas.width, this.$refs.canvas.height);
            }
            this.redrawCanvas();
        },
        canvas_height: function() {

            this.$refs.canvas.height = Math.max(this.canvas_height, 64);
            this.global_region.height = this.$refs.canvas.height;
            for(let i = 0; i < this.regions.length; i++) {
                this.regions[i].stayWithinBounds(this.$refs.canvas.width, this.$refs.canvas.height);
            }
            this.redrawCanvas();
        },
        regions: {
            handler(_) {
                this.redrawCanvas();
            },
            deep: true
        },
        grid_enabled: function(enabled) {
            this.redrawCanvas();
            
        },
        grid_rows: function(rows) {
            this.redrawCanvas();
            
        },
        grid_columns: function(columns) {
            this.redrawCanvas();
            
        }
    },
    methods: {
        toggle_instructions: function() {
            this.instructions_visible = !this.instructions_visible;
        },
        canvas_loaded: function() {
            return this.$refs.canvas != null;
        },
        divisions: function() {
            if (this.canvas_loaded() == false) {
                return "???";
            }
            output = ""
            all_regions = [this.global_region].concat(this.regions).filter(function(region) {
                return region.enabled;
            });
            for (var i = 0; i < all_regions.length; i++) {
                d = all_regions[i].getRatios(this.$refs.canvas.width, this.$refs.canvas.height).division
                output += `${d[0].toFixed(2)}:${d[1].toFixed(2)}`
                if (i < all_regions.length - 1) {
                    output += ',';
                }
            }
            return output;
        },
        positions: function() {
            if (this.canvas_loaded() == false) {
                return "???";
            }
            output = ""
            all_regions = [this.global_region].concat(this.regions).filter(function(region) {
                return region.enabled;
            });
            for (var i = 0; i < all_regions.length; i++) {
                p = all_regions[i].getRatios(this.$refs.canvas.width, this.$refs.canvas.height).position
                output += `${p[0].toFixed(2)}:${p[1].toFixed(2)}`
                if (i < all_regions.length - 1) {
                    output += ',';
                }
            }
            return output;
        },
        weights: function() {
            if (this.canvas_loaded() == false) {
                return "???";
            }
            output = ""
            all_regions = [this.global_region].concat(this.regions).filter(function(region) {
                return region.enabled;
            });
            for (var i = 0; i < all_regions.length; i++) {
                output += all_regions[i].weight.toFixed(2).toString()
                if (i < all_regions.length - 1) {
                    output += ',';
                }
            }
            return output;
        },
        prompt: function() {
            output = ""
            all_regions = [this.global_region].concat(this.regions).filter(function(region) {
                return region.enabled;
            });
            for (var i = 0; i < all_regions.length; i++) {
                output += all_regions[i].prompt
                if (i < all_regions.length - 1) {
                    output += '\nAND ';
                }
            }
            return output;
        },
        setCanvasWidth(canvas_width) {
            this.$refs.canvas.width = canvas_width;
            this.redrawCanvas();
        },
        setCanvasHeight(canvas_height) {
            this.$refs.canvas.height = canvas_height;
            this.redrawCanvas();
        },
        uploadImage: function(e) {
            this.image_loaded = false;
            const reader = new FileReader();
            reader.onload = (event) => {
                this.image_resource = new Image();
                this.image_resource.onload = () => {
                    this.canvas_width = this.image_resource.width;
                    this.canvas_height = this.image_resource.height;
                    this.image_loaded = true;
                    for(let i = 0; i < this.regions.length; i++) {
                        this.regions[i].stayWithinBounds(this.$refs.canvas.width, this.$refs.canvas.height);
                    }
                    this.redrawCanvas();
                };
                this.image_resource.src = event.target.result;
            };
            reader.readAsDataURL(e.target.files[0]);
            
        },

        selectNone: function() {
            for (let i = 0; i < this.regions.length; i++) {
                this.regions[i].selected = false;
            }
            this.redrawCanvas();
        },

        selectRegion: function(region) {
            this.selectNone();
            region.selected = true;
            this.redrawCanvas();
        },

        getSelectedRegion: function() {
            for (let i = 0; i < this.regions.length; i++) {
                if(this.regions[i].selected == true) {
                    return this.regions[i];
                }
            }
            return null;
        },

        snapAllRegions: function() {
            for (let i = 0; i < this.regions.length; i++) {
                this.regions[i].snap(this.$refs.canvas.width, this.$refs.canvas.height, this.grid_rows, this.grid_columns);
            }
            this.redrawCanvas();
        },

        redrawCanvas: function () {
            var ctx = this.$refs.canvas.getContext('2d');
            
            //clear the canvas
            ctx.clearRect(0, 0, this.$refs.canvas.width, this.$refs.canvas.height);

            //draw the image if it exists
            if (this.image_loaded) {
                ctx.drawImage(this.image_resource, 0, 0);
            }
            else {
                ctx.fillStyle = "white";
                ctx.fillRect(0, 0, this.$refs.canvas.width, this.$refs.canvas.height);
            }
            
            //draw the grid...\
            if (this.grid_enabled) {
                
            
                var cell_width = this.$refs.canvas.width / this.grid_columns;
                var cell_height = this.$refs.canvas.height / this.grid_rows;
                ctx.strokeStyle = 'rgba(0,0,0,0.6)';
                for (var i = 0; i < this.grid_columns; i++) {
                    ctx.beginPath();
                    ctx.moveTo(i * cell_width, 0);
                    ctx.lineWidth = 1;
                    ctx.lineTo(i * cell_width, this.$refs.canvas.height);
                    ctx.stroke();
                    ctx.closePath();
                }
                for (var j = 0; j < this.grid_rows; j++) {
                    ctx.beginPath();
                    ctx.moveTo(0, j * cell_height);
                    ctx.lineWidth = 1;
                    ctx.lineTo(this.$refs.canvas.width, j * cell_height);
                    ctx.stroke();
                    ctx.closePath();
                }
            }

            //draw the regions
            for (var i = 0; i < this.regions.length; i++) {
                this.regions[i].draw(ctx, (i + 1).toString());
            }

        },
        deleteRegion(region) {
            this.regions.splice(this.regions.indexOf(region), 1);
            this.selectNone();
        },
        onCanvasMouseDown: function(event) {
            var canvas = event.target;
            var x = event.pageX - canvas.offsetLeft;
            var y = event.pageY - canvas.offsetTop;


            for (var i = this.regions.length - 1; i >= 0; i--) {
                if (event.which == 1) { //left click
                    if (this.regions[i].selectIfMouseOver({x: x, y: y})){
                        break;
                    }
                } else if (event.which == 3) { // right click
                    if (this.regions[i].selectIfMouseOver({x: x, y: y})){
                        this.deleteRegion(this.getSelectedRegion());
                        break;
                    }
                }
            }

            if (this.getSelectedRegion() == null) {
                if (event.which == 1) { //left click
                    new_region = new Region(x, y, 16, 16, "", 0.8);
                    this.regions.push(new_region);
                    this.selectRegion(new_region);
                    new_region.tool_operation_mode = "scale";
                }
            }
            
            this.redrawCanvas();
        },

        onCanvasMouseMove: function (event) {
            var canvas = event.target;
            var x = event.pageX - canvas.offsetLeft;
            var y = event.pageY - canvas.offsetTop;


            for (var i = 0; i < this.regions.length; i++) {
                this.regions[i].onMouseMove({x: x, y: y}, this.$refs.canvas.width, this.$refs.canvas.height);
            }

            this.redrawCanvas();
            
        },
        onCanvasMouseUp: function(event) {
            var canvas = event.target;
            var x = event.pageX - canvas.offsetLeft;
            var y = event.pageY - canvas.offsetTop;

            for (var i = 0; i < this.regions.length; i++) {
                this.regions[i].onMouseUp({x: x, y: y});
            }
            this.redrawCanvas();
        },

        onCanvasMouseLeave: function(event) {
            for (var i = 0; i < this.regions.length; i++) {
                this.regions[i].onMouseLeave(this.$refs.canvas.width, this.$refs.canvas.height);
            }
        },
        getRatios: function(region){
            return region.getRatios(this.$refs.canvas.width, this.$refs.canvas.height)
        },
        copyText(text) {
            navigator.clipboard.writeText(text);
        }
    },
    computed: {
        nick: function() {
            //returns a random nickname from the list of nicknames in the variable nicknames
            let nicknames = ["DeepDreamDestroyer", "Artificial Artist", "Synthetic Sorcerer", "The IP Bandit", "Hue Hacker", "AInomaly", "Deep Dream Disaster", "The Property Intellectual", "Content Crook, Let Him Cook", "The Canvas Caper", "Cyber Surrealist", "The Trademark Terrorist", "Infringe This", "NeuralNetNinja", "The Photo Copyer", "GANtastic", "Pixel Pirate", "Oops I Stole Your Art :3"]
            return nicknames[Math.floor(Math.random() * nicknames.length)];
        }
    }
});