Skip to content

Commit

Permalink
moved all color depth representation to floating numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
AnonMiraj committed Aug 14, 2024
1 parent 765690b commit eb9b1dc
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 224 deletions.
4 changes: 2 additions & 2 deletions src/backends/fig_types.f90
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module fig_types
end interface

type :: canvas_size
integer(pixel) :: width
integer(pixel) :: height
integer :: width
integer :: height
end type canvas_size

contains
Expand Down
13 changes: 4 additions & 9 deletions src/backends/generic_cairo/cairo_utils.f90
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@ module fig_cairo_utils
subroutine set_rgba(cr,color)
type(c_ptr), intent(inout) :: cr
type(RGB) :: color
call cairo_set_source_rgba(cr,normalize_ch(color%r),normalize_ch(color%g),normalize_ch(color%b),normalize_ch(color%a))
end subroutine set_rgba
call cairo_set_source_rgba(cr,(color%r),(color%g),(color%b),(color%a))

function normalize_ch(ch) result(res)
integer, intent(in) :: ch
real(kind=8) :: res
res= real(ch,kind=8)/real(2**rgb_bit_depth-1,kind=8)
end function normalize_ch
end subroutine set_rgba

subroutine fill(cr,sh)
type(c_ptr), intent(inout) :: cr
class(shape), intent(in) :: sh
if (sh%fill_color%a .ne. 0) then
if (sh%fill_color%a > 0.0001) then
call set_rgba(cr,sh%fill_color)
call cairo_fill_preserve(cr)
end if
Expand All @@ -40,7 +35,7 @@ subroutine stroke(cr,sh)
deallocate(dash_arr)
end if

if (sh%stroke_color%a .ne. 0) then
if (sh%stroke_color%a > 0.0001) then
call set_rgba(cr,sh%stroke_color)
call cairo_set_line_width(cr,sh%stroke_width)
call cairo_stroke(cr)
Expand Down
4 changes: 0 additions & 4 deletions src/fig_config.f90
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
module fig_config
use iso_fortran_env, only: int32
implicit none
integer, parameter :: rgb_level = int32
!! technically it should be int8 but there are problems with signed integers and bit manipulation
integer, parameter :: pixel = int32 !! 8 bits per every color channel (r,g,b,a)
integer, parameter :: rgb_bit_depth = 8

logical :: FIG_ABSOLUTE_COORDINATES = .false.
end module fig_config

27 changes: 16 additions & 11 deletions src/fig_rgb.f90
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,35 @@ module fig_rgb

type :: RGB
sequence
integer(rgb_level) :: r
integer(rgb_level) :: g
integer(rgb_level) :: b
integer(rgb_level) :: a
real(kind=8) :: r
real(kind=8) :: g
real(kind=8) :: b
real(kind=8) :: a
end type RGB

contains

elemental type(integer(pixel)) function rgb_to_int(color) result(rgb_int)
type(RGB), intent(in) :: color
integer :: r,g,b,a
r=color%r*(2**rgb_bit_depth-1)
b=color%b*(2**rgb_bit_depth-1)
g=color%g*(2**rgb_bit_depth-1)
a=color%a*(2**rgb_bit_depth-1)

rgb_int = ior(ishft(color%a, rgb_bit_depth*3),&
ior(ishft(color%r, rgb_bit_depth*2),&
ior(ishft(color%g, rgb_bit_depth), color%b)))
rgb_int = ior(ishft(a, rgb_bit_depth*3),&
ior(ishft(r, rgb_bit_depth*2),&
ior(ishft(g, rgb_bit_depth), b)))

end function rgb_to_int

elemental type(RGB) function int_to_rgb(rgb_int) result(color)
integer(pixel), intent(in):: rgb_int

color%a = ibits(rgb_int, 3*rgb_bit_depth, rgb_bit_depth)
color%r = ibits(rgb_int, 2*rgb_bit_depth, rgb_bit_depth)
color%g = ibits(rgb_int, rgb_bit_depth , rgb_bit_depth)
color%b = ibits(rgb_int, 0, rgb_bit_depth)
color%a = real(ibits(rgb_int, 3*rgb_bit_depth, rgb_bit_depth),kind=8) / (2**rgb_bit_depth-1)
color%r = real(ibits(rgb_int, 2*rgb_bit_depth, rgb_bit_depth),kind=8) / (2**rgb_bit_depth-1)
color%g = real(ibits(rgb_int, rgb_bit_depth , rgb_bit_depth),kind=8) / (2**rgb_bit_depth-1)
color%b = real(ibits(rgb_int, 0, rgb_bit_depth),kind=8) / (2**rgb_bit_depth-1)
end function int_to_rgb

end module fig_rgb
296 changes: 148 additions & 148 deletions src/fig_rgb_color_constants.f90

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/chess.f90
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ program chess_checker
call svg_canva%apply_shapes(checker)
call svg_canva%save_to_svg()
call svg_canva%destroy()
call bitmap_canva%init(WIDTH,HEIGHT,file_name)
call bitmap_canva%init(HEIGHT,WIDTH,file_name)
call bitmap_canva%apply_shapes(checker)
call bitmap_canva%save_to_png()
call bitmap_canva%save_to_ppm()
Expand Down
2 changes: 1 addition & 1 deletion test/circle.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ program circle_test

call canva%init()
bg = FIG_COLOR_GREEN
bg%a = 150
bg%a = 150/255.
call canva%set_background(bg)

! Circle parameters
Expand Down
22 changes: 11 additions & 11 deletions test/drawing_test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ program drawing_test_all
c%center%y = 100.0 / CANVAS_HEIGHT
c%r = 50.0
c%fill_color = FIG_COLOR_PINK
c%fill_color%a = 100
c%fill_color%a = .5
c%stroke_color = FIG_COLOR_RED
c%stroke_color%a = 100
c%stroke_color%a = .5
call canva%add_shape(c)

! Ellipse
Expand All @@ -45,10 +45,10 @@ program drawing_test_all
elp%rx = 50.0
elp%ry = 25.0
color = FIG_COLOR_GOLD
color%a = 100
color%a = .5
elp%fill_color = color
elp%stroke_color = FIG_COLOR_RED
elp%stroke_color%a = 100
elp%stroke_color%a = .5
call canva%add_shape(elp)

! Rectangle 1
Expand All @@ -57,7 +57,7 @@ program drawing_test_all
r%width = 100.0
r%height = 50.0
r%fill_color = FIG_COLOR_BLUE
r%fill_color%a = 100
r%fill_color%a = .5
r%stroke_width = 10
r%stroke_color = FIG_COLOR_GOLD
call canva%add_shape(r)
Expand All @@ -68,12 +68,12 @@ program drawing_test_all
r%width = 120.0
r%height = 50.0
r%fill_color = FIG_COLOR_RED
r%fill_color%a = 100
r%fill_color%a = .5
r%rx=5
r%ry=5
r%stroke_width = 6
r%stroke_color = FIG_COLOR_SEAGREEN
r%stroke_color%a=150
r%stroke_color%a=.5
call canva%add_shape(r)

! Line
Expand All @@ -86,7 +86,7 @@ program drawing_test_all
call canva%add_shape(l)
l%stroke_width = 2
l%stroke_color = FIG_COLOR_RED
l%stroke_color%a=100
l%stroke_color%a=.5
call canva%add_shape(l)


Expand All @@ -96,12 +96,12 @@ program drawing_test_all
l%p2%x = 550.0 / CANVAS_WIDTH
l%p2%y = 400.0 / CANVAS_HEIGHT
l%stroke_color = FIG_COLOR_BLACK
l%stroke_color%a=100
l%stroke_color%a=.5
l%stroke_width = 50
call canva%add_shape(l)
l%stroke_width = 2
l%stroke_color = FIG_COLOR_RED
l%stroke_color%a=100
l%stroke_color%a=.5

call canva%add_shape(l)

Expand All @@ -113,7 +113,7 @@ program drawing_test_all
ar%start_angle= 0
ar%end_angle= pi * 1.2
ar%fill_color = FIG_COLOR_INDIGO
ar%fill_color%a=100
ar%fill_color%a=.5
ar%stroke_color = FIG_COLOR_BROWN
call canva%add_shape(ar)

Expand Down
17 changes: 8 additions & 9 deletions test/int_to_RGB_to_int.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ program int_to_RGB_to_int

do color_counter=1,1000
call random_number(intermediate_real)
rgb_old%r = int(256*intermediate_real)
rgb_old%r = intermediate_real
call random_number(intermediate_real)
rgb_old%g = int(256*intermediate_real)
rgb_old%g = intermediate_real
call random_number(intermediate_real)
rgb_old%b = int(256*intermediate_real)
rgb_old%b = intermediate_real
call random_number(intermediate_real)
rgb_old%a = int(256*intermediate_real)
rgb_old%a = intermediate_real

color_integer = rgb_to_int(rgb_old)

rgb_new=int_to_rgb(color_integer)

if (rgb_old%r /= rgb_new%r .or. &
rgb_old%g /= rgb_new%g .or. &
rgb_old%b /= rgb_new%b .or. &
rgb_old%a /= rgb_new%a ) then
if (abs(rgb_old%r - rgb_new%r) > 1.0e-2 .or. &
abs(rgb_old%g - rgb_new%g) > 1.0e-2 .or. &
abs(rgb_old%b - rgb_new%b) > 1.0e-2 .or. &
abs(rgb_old%a - rgb_new%a) > 1.0e-2) then
error stop "converting between RGB and int or vice versa went wrong"
endif

Expand Down
19 changes: 2 additions & 17 deletions test/line.f90
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ program radial_lines
angle_step = 1.0 * atan(1.0) / NUM_LINES

do i = 0, NUM_LINES - 1
call random_color(color)
angle = 4 * i * angle_step
call draw_radial_line(radial_canvas, cx, cy, radius, angle, color)
call draw_radial_line(radial_canvas, cx, cy, radius, angle)
end do

call svg_canva%init(CANVAS_WIDTH,CANVAS_HEIGHT,file_name)
Expand All @@ -60,11 +59,10 @@ program radial_lines
call test_both(file_name,bitmap_canva)
contains

subroutine draw_radial_line(canva, cx, cy, radius, angle, color)
subroutine draw_radial_line(canva, cx, cy, radius, angle)
type(drawing), intent(inout) :: canva
real, intent(in) :: cx, cy, radius
real, intent(in) :: angle
type(RGB), intent(in) :: color
integer :: x1, y1, x2, y2
real :: cos_angle, sin_angle

Expand All @@ -79,18 +77,5 @@ subroutine draw_radial_line(canva, cx, cy, radius, angle, color)

call canva%add_shape(sh)
end subroutine draw_radial_line

subroutine random_color(color)
type(RGB), intent(out) :: color
real :: r, g, b
call random_number(r)
call random_number(g)
call random_number(b)
color%r = int(r * 255)
color%g = int(g * 255)
color%b = int(b * 255)
color%a = 255
end subroutine random_color

end program radial_lines

11 changes: 0 additions & 11 deletions test/triangle_pattern.f90
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,5 @@ program test_fig_fill_triangle

call test_both(file_name,bitmap_canva)
contains
subroutine random_color(color)
type(RGB) :: color
real :: r, g, b
call random_number(r)
call random_number(g)
call random_number(b)
color%r = int(r * 255)
color%g = int(g * 255)
color%b = int(b * 255)
color%a = 255
end subroutine random_color
end program test_fig_fill_triangle

0 comments on commit eb9b1dc

Please sign in to comment.