Skip to content

Commit 624bb78

Browse files
committed
Use STBI; Supports PNG, JPG, BMP, etc
1 parent 4da25ba commit 624bb78

File tree

11 files changed

+166
-95
lines changed

11 files changed

+166
-95
lines changed

brush_fillcan.v

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module main
22

3-
import vpng
43
import gg
54
import iui as ui
65
import gx
@@ -15,7 +14,7 @@ mut:
1514
down_y int
1615
}
1716

18-
fn (brush FillBrush) set_pixels(ptr voidptr, x int, y int, color vpng.TrueColorAlpha, size int) {
17+
fn (brush FillBrush) set_pixels(ptr voidptr, x int, y int, color gx.Color, size int) {
1918
mut pixels := &KA(ptr)
2019

2120
down_color := get_pixel(x, y, mut pixels.file)
@@ -24,7 +23,7 @@ fn (brush FillBrush) set_pixels(ptr voidptr, x int, y int, color vpng.TrueColorA
2423
}
2524

2625
// TODO: Optimize, improve.
27-
fn check_pix(x int, y int, mut storage KA, down_color vpng.Pixel, color vpng.TrueColorAlpha) []int {
26+
fn check_pix(x int, y int, mut storage KA, down_color gx.Color, color gx.Color) []int {
2827
mut arr := []int{}
2928
if x < 0 || y < 0 {
3029
return arr
@@ -60,17 +59,17 @@ fn check_pix(x int, y int, mut storage KA, down_color vpng.Pixel, color vpng.Tru
6059
return arr
6160
}
6261

63-
fn do_y(x int, yy int, mut storage KA, down_color vpng.Pixel, color vpng.TrueColorAlpha) bool {
62+
fn do_y(x int, yy int, mut storage KA, down_color gx.Color, color gx.Color) bool {
6463
main_ := get_pixel(x, yy, mut storage.file)
6564

6665
if main_ == down_color {
67-
storage.file.set_pixel(x, yy, color)
66+
set_pixel(storage.file, x, yy, color)
6867

6968
mut xx := x - 1
7069
for xx > 0 {
7170
mut color_ := get_pixel(xx, yy, mut storage.file)
7271
if color_ == down_color {
73-
storage.file.set_pixel(xx, yy, color)
72+
set_pixel(storage.file, xx, yy, color)
7473
} else {
7574
break
7675
}
@@ -81,7 +80,7 @@ fn do_y(x int, yy int, mut storage KA, down_color vpng.Pixel, color vpng.TrueCol
8180
for xx < storage.width {
8281
mut color_ := get_pixel(xx, yy, mut storage.file)
8382
if color_ == down_color {
84-
storage.file.set_pixel(xx, yy, color)
83+
set_pixel(storage.file, xx, yy, color)
8584
} else {
8685
break
8786
}

brushes.v

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module main
22

3-
import vpng
43
import gg
54
import iui as ui
65
import gx
@@ -11,7 +10,7 @@ import rand
1110
//
1211
interface Brush {
1312
name string
14-
set_pixels(voidptr, int, int, vpng.TrueColorAlpha, int)
13+
set_pixels(voidptr, int, int, gx.Color, int)
1514
draw_hint(voidptr, int, int, int, int, gx.Color, int)
1615
mut:
1716
down_x int
@@ -28,11 +27,11 @@ mut:
2827
down_y int
2928
}
3029

31-
fn (brush &CalligraphyBrushLeft) set_pixels(ptr voidptr, x int, y int, color vpng.TrueColorAlpha, size int) {
30+
fn (brush &CalligraphyBrushLeft) set_pixels(ptr voidptr, x int, y int, color gx.Color, size int) {
3231
mut pixels := &KA(ptr)
3332
for i in 0 .. size {
34-
pixels.file.set_pixel(x + i, y - i, color)
35-
pixels.file.set_pixel(x - i, y + i, color)
33+
set_pixel(pixels.file, x + i, y - i, color)
34+
set_pixel(pixels.file, x - i, y + i, color)
3635
}
3736
}
3837

@@ -57,11 +56,11 @@ mut:
5756
down_y int
5857
}
5958

60-
fn (brush &CalligraphyBrush) set_pixels(ptr voidptr, x int, y int, color vpng.TrueColorAlpha, size int) {
61-
mut pixels := &KA(ptr)
59+
fn (brush &CalligraphyBrush) set_pixels(ptr voidptr, x int, y int, color gx.Color, size int) {
60+
mut data := &KA(ptr)
6261
for i in 0 .. size {
63-
pixels.file.set_pixel(x - i, y - i, color)
64-
pixels.file.set_pixel(x + i, y + i, color)
62+
set_pixel(data.file, x - i, y - i, color)
63+
set_pixel(data.file, x + i, y + i, color)
6564
}
6665
}
6766

@@ -86,19 +85,19 @@ mut:
8685
down_y int
8786
}
8887

89-
fn (brush PencilBrush) set_pixels(ptr voidptr, x int, y int, color vpng.TrueColorAlpha, size int) {
90-
mut pixels := &KA(ptr)
88+
fn (brush PencilBrush) set_pixels(ptr voidptr, x int, y int, color gx.Color, size int) {
89+
mut data := &KA(ptr)
9190
wid := size / 2
9291

9392
for i in 0 .. size {
9493
for j in 0 .. size {
9594
xx := x + i - wid
9695
yy := y + j - wid
97-
if xx >= 0 && yy >= 0 && xx <= pixels.width {
96+
if xx >= 0 && yy >= 0 && xx <= data.width {
9897
if size < 4 || (!(j == size - 1 && i == size - 1) && !(j == size - 1 && i == 0)
9998
&& !(j == 0 && i == 0) && !(j == 0 && i == size - 1)) {
100-
if xx < pixels.width && yy < pixels.height {
101-
pixels.file.set_pixel(xx, yy, color)
99+
if xx < data.width && yy < data.height {
100+
set_pixel(data.file, xx, yy, color)
102101
}
103102
}
104103
}
@@ -131,7 +130,7 @@ mut:
131130
down_y int
132131
}
133132

134-
fn (brush SpraycanBrush) set_pixels(ptr voidptr, x int, y int, color vpng.TrueColorAlpha, size int) {
133+
fn (brush SpraycanBrush) set_pixels(ptr voidptr, x int, y int, color gx.Color, size int) {
135134
mut pixels := &KA(ptr)
136135
wid := size / 2
137136

@@ -146,7 +145,7 @@ fn (brush SpraycanBrush) set_pixels(ptr voidptr, x int, y int, color vpng.TrueCo
146145
if size < 4 || (!(j == size - 1 && i == size - 1) && !(j == size - 1 && i == 0)
147146
&& !(j == 0 && i == 0) && !(j == 0 && i == size - 1)) {
148147
if rand_int == 0 {
149-
pixels.file.set_pixel(xx, yy, color)
148+
set_pixel(pixels.file, xx, yy, color)
150149
}
151150
}
152151
}
@@ -193,7 +192,7 @@ struct Box {
193192
zoom f32
194193
}
195194

196-
fn (brush &SelectionTool) set_pixels(ptr voidptr, x int, y int, color vpng.TrueColorAlpha, size int) {
195+
fn (brush &SelectionTool) set_pixels(ptr voidptr, x int, y int, color gx.Color, size int) {
197196
mut storage := &KA(ptr)
198197
if storage.brush.down_x == -1 {
199198
storage.brush.down_x = x

drawer.v

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module main
22

3-
import vpng
43
import gg
54
import iui as ui
65
import gx
@@ -26,7 +25,7 @@ fn mouse_down_loop(mut win ui.Window, mut this ui.Component) {
2625
if win.mouse_y < (size.height - 25) && cy < this.height && cx < this.width
2726
&& (cy * zoom) >= 0 && (cx * zoom) >= 0 {
2827
gx_color := pixels.color
29-
color := vpng.TrueColorAlpha{gx_color.r, gx_color.g, gx_color.b, gx_color.a}
28+
color := gx.Color{gx_color.r, gx_color.g, gx_color.b, gx_color.a}
3029
dsize := pixels.draw_size
3130
pixels.brush.set_pixels(pixels, cx, cy, color, dsize)
3231

@@ -105,7 +104,7 @@ fn draw_image(mut win ui.Window, com &ui.Component) {
105104
this.width + 1, this.height + 1, gx.rgb(215, 215, 215))
106105

107106
// Draw box-shadow
108-
draw_box_shadow(this, y_slide, x_slide, gg)
107+
draw_box_shadow(this, y_slide, x_slide, gg, win.theme)
109108

110109
// Draw Image
111110
gg.draw_image_with_config(config)
@@ -127,13 +126,19 @@ fn color_from_string(st string) gx.Color {
127126
//
128127
// Draw box shadow around image canvas
129128
//
130-
fn draw_box_shadow(this ui.Component, y_slide &ui.Slider, x_slide &ui.Slider, gg gg.Context) {
129+
fn draw_box_shadow(this ui.Component, y_slide &ui.Slider, x_slide &ui.Slider, gg gg.Context, theme ui.Theme) {
131130
mut shadows := [gx.rgb(171, 183, 203), gx.rgb(176, 188, 207),
132131
gx.rgb(182, 193, 212), gx.rgb(187, 198, 215), gx.rgb(193, 203, 220),
133132
gx.rgb(198, 208, 225), gx.rgb(204, 213, 230), gx.rgb(209, 218, 234)]
134133

134+
tn := theme.name
135+
if tn.contains('Dark') || tn.contains('Black') {
136+
// TODO: Dark shadow
137+
return
138+
}
139+
135140
mut si := this.y + this.height + 2
136-
mut sx := this.x + this.width + 1
141+
mut sx := this.x + this.width + 2
137142
for mut shadow in shadows {
138143
gg.draw_line(this.x + 10 - int(x_slide.cur), si - int(y_slide.cur), this.width + this.x + 1 - int(x_slide.cur),
139144
si - int(y_slide.cur), shadow)
@@ -156,8 +161,7 @@ fn make_gg_image(mut storage KA, mut win ui.Window, first bool) {
156161
})
157162
win.gg.set_bg_color(gx.rgb(210, 220, 240))
158163
}
159-
bytess := storage.file.get_unfiltered()
160-
win.gg.update_pixel_data(storage.ggim, bytess.data)
164+
win.gg.update_pixel_data(storage.ggim, storage.file.data)
161165
}
162166

163167
// Create an new ui.Image
@@ -181,6 +185,10 @@ fn theme_click(mut win ui.Window, com ui.MenuItem) {
181185
background := gx.rgb(25, 42, 77)
182186
win.gg.set_bg_color(gx.rgb(25, 42, 77))
183187
win.id_map['background'] = &background
188+
} else if text.contains('Black') {
189+
win.gg.set_bg_color(gx.rgb(0, 0, 0))
190+
background := gx.rgb(0, 0, 0)
191+
win.id_map['background'] = &background
184192
} else {
185193
win.gg.set_bg_color(gx.rgb(210, 220, 240))
186194
background := gx.rgb(210, 220, 240)

image_file.v

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module main
2+
3+
import stbi
4+
import gx
5+
6+
pub fn read(path string) ?stbi.Image {
7+
return stbi.load(path)
8+
}
9+
10+
pub fn write(img stbi.Image, path string) {
11+
stbi.stbi_write_png(path, img.width, img.height, 4, img.data, img.width * 4) or { panic(err) }
12+
}
13+
14+
// Get RGB value from image loaded with STBI
15+
pub fn get_pixel(x int, y int, mut this stbi.Image) gx.Color {
16+
image := this
17+
unsafe {
18+
data := &byte(image.data)
19+
p := data + (4 * (y * image.width + x))
20+
r := p[0]
21+
g := p[1]
22+
b := p[2]
23+
a := p[3]
24+
return gx.Color{r, g, b, a}
25+
}
26+
}
27+
28+
// Get RGB value from image loaded with STBI
29+
fn set_pixel(image stbi.Image, x int, y int, color gx.Color) bool {
30+
if x < 0 || x >= image.width {
31+
return false
32+
}
33+
34+
if y < 0 || y >= image.height {
35+
return false
36+
}
37+
38+
unsafe {
39+
data := &byte(image.data)
40+
p := data + (4 * (y * image.width + x))
41+
p[0] = color.r
42+
p[1] = color.g
43+
p[2] = color.b
44+
p[3] = color.a
45+
return true
46+
}
47+
}

menus.v

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ fn make_draw_size_menu(mut win ui.Window) {
6969
}
7070

7171
fn draw_size_item(ds int) &ui.MenuItem {
72-
mut item := ui.menuitem(ds.str() + 'px')
73-
item.set_click(draw_size_click)
72+
item := ui.menu_item(
73+
text: ds.str() + 'px'
74+
click_event_fn: draw_size_click
75+
)
76+
7477
return item
7578
}
7679

@@ -81,19 +84,25 @@ fn draw_size_click(mut win ui.Window, com ui.MenuItem) {
8184

8285
fn make_zoom_menu(mut win ui.Window) {
8386
// Zoom menu
84-
mut mz := ui.menuitem('Zoom')
85-
86-
mut zoomm := ui.menuitem('Decrease (-)')
87-
zoomm.set_click(zoom_decrease_click)
88-
mz.add_child(zoomm)
89-
90-
mut zoomp := ui.menuitem('Increase (+)')
91-
zoomp.set_click(zoom_increase_click)
92-
mz.add_child(zoomp)
93-
94-
mut zoom_full := ui.menuitem('Increase by 1000%')
95-
zoom_full.set_click(zoom_increase_big_click)
96-
mz.add_child(zoom_full)
87+
zoomm := ui.menu_item(
88+
text: 'Decrease (-)'
89+
click_event_fn: zoom_decrease_click
90+
)
91+
92+
zoomp := ui.menu_item(
93+
text: 'Increase (+)'
94+
click_event_fn: zoom_increase_click
95+
)
96+
97+
zoom_full := ui.menu_item(
98+
text: 'Increase by 1000%'
99+
click_event_fn: zoom_increase_big_click
100+
)
101+
102+
mz := ui.menu_item(
103+
text: 'Zoom'
104+
children: [zoomm, zoomp, zoom_full]
105+
)
97106

98107
win.bar.add_child(mz)
99108
}

shapes.v

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module main
22

3-
import vpng
43
import gg
54
import iui as ui
65
import gx
@@ -17,7 +16,7 @@ mut:
1716
selected_area Box
1817
}
1918

20-
fn (brush &RectShape) set_pixels(ptr voidptr, x int, y int, color vpng.TrueColorAlpha, size int) {
19+
fn (brush &RectShape) set_pixels(ptr voidptr, x int, y int, color gx.Color, size int) {
2120
mut storage := &KA(ptr)
2221
if storage.brush.down_x == -1 {
2322
storage.brush.down_x = x
@@ -42,7 +41,7 @@ fn (brush &RectShape) draw_hint(ptr voidptr, tx int, ty int, cx int, cy int, col
4241
if zoom == box.zoom && box.x != -1 {
4342
win.gg.draw_rect_empty(box.x, box.y, box.w, box.h, gx.blue)
4443

45-
tcolor := vpng.TrueColorAlpha{color.r, color.g, color.b, color.a}
44+
tcolor := gx.Color{color.r, color.g, color.b, color.a}
4645

4746
base_x := int(box.w / zoom)
4847
base_y := int(box.h / zoom)
@@ -56,7 +55,7 @@ fn (brush &RectShape) draw_hint(ptr voidptr, tx int, ty int, cx int, cy int, col
5655
for i in min_x .. max_x {
5756
for j in min_y .. max_y {
5857
if (i == min_x || i == max_x - 1) || j == min_y || j == max_y - 1 {
59-
storage.file.set_pixel(box.x + i, box.y + j, tcolor)
58+
set_pixel(storage.file, box.x + i, box.y + j, tcolor)
6059
}
6160
}
6261
}
@@ -100,7 +99,7 @@ mut:
10099
selected_area Box
101100
}
102101

103-
fn (brush &SquareShape) set_pixels(ptr voidptr, x int, y int, color vpng.TrueColorAlpha, size int) {
102+
fn (brush &SquareShape) set_pixels(ptr voidptr, x int, y int, color gx.Color, size int) {
104103
mut storage := &KA(ptr)
105104
if storage.brush.down_x == -1 {
106105
storage.brush.down_x = x
@@ -125,7 +124,7 @@ fn (brush &SquareShape) draw_hint(ptr voidptr, tx int, ty int, cx int, cy int, c
125124
if zoom == box.zoom && box.x != -1 {
126125
win.gg.draw_rect_empty(box.x, box.y, box.w, box.h, gx.blue)
127126

128-
tcolor := vpng.TrueColorAlpha{color.r, color.g, color.b, color.a}
127+
tcolor := gx.Color{color.r, color.g, color.b, color.a}
129128

130129
base_x := int(box.w / zoom)
131130
base_y := int(box.h / zoom)
@@ -139,7 +138,7 @@ fn (brush &SquareShape) draw_hint(ptr voidptr, tx int, ty int, cx int, cy int, c
139138
for i in min_x .. max_x {
140139
for j in min_y .. max_y {
141140
if (i == min_x || i == max_x - 1) || j == min_y || j == max_y - 1 {
142-
storage.file.set_pixel(box.x + i, box.y + j, tcolor)
141+
set_pixel(storage.file, box.x + i, box.y + j, tcolor)
143142
}
144143
}
145144
}

0 commit comments

Comments
 (0)