MCPcopy Create free account
hub / github.com/LemonOSProject/LemonOS / Paint

Method Paint

Applications/LemonPaint/brush.cpp:6–37  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4#include <stdio.h>
5
6void Brush::Paint(int x, int y, uint8_t r, uint8_t g, uint8_t b, double scale, Canvas* canvas){
7 int _x = x - (data.width / 2 * scale);
8 int _y = y - (data.height / 2 * scale);
9 for(int i = 0; i < data.height * scale; i++){
10 if(_y + i < 0 || _y + i >= canvas->surface.height) continue;
11 for(int j = 0; j < data.width * scale; j++){
12 if(_x + j < 0 || _x + j >= canvas->surface.width) continue;
13
14 double _xval = ((double)j) / scale;
15 double _yval = ((double)i) / scale;
16
17 double val1 = data.buffer[(int)floor(_yval) * data.width + (int)floor(_xval)];
18 double val2 = data.buffer[(int)floor(_yval) * data.width + (int)ceil(_xval)];
19 double x1 = (val1 + (val2 - val1) * (_xval - ((int)_xval)));
20
21 val1 = data.buffer[(int)ceil(_yval) * data.width + (int)floor(_xval)];
22 val2 = data.buffer[(int)ceil(_yval) * data.width + (int)ceil(_xval)];
23 double x2 = (val1 + (val2 - val1) * (_xval - ((int)_xval)));
24
25 double val = (x1 + (x2 - x1) * (_yval - ((int)_yval))) / 255.0;
26
27 uint32_t oldColour = ((uint32_t*)canvas->surface.buffer)[(_y + i) * canvas->surface.width + _x + j];
28 double oldB = oldColour & 0xFF;
29 double oldG = (oldColour >> 8) & 0xFF;
30 double oldR = (oldColour >> 16) & 0xFF;
31
32 uint32_t newColour = (uint32_t)(val * b + oldB * (1 - val)) | (((uint32_t)(val * g + oldG * (1 - val)) << 8)) | (((uint32_t)(val * r + oldR * (1 - val)) << 16));
33
34 ((uint32_t*)canvas->surface.buffer)[(_y + i) * canvas->surface.width +_x + j] = newColour;
35 }
36 }
37}

Callers

nothing calls this directly

Calls 1

floorFunction · 0.85

Tested by

no test coverage detected