Author Topic: Paint Image  (Read 11917 times)

0 Members and 1 Guest are viewing this topic.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Paint Image
« Reply #45 on: November 24, 2019, 12:26:37 pm »
Just a note. Each _NEWIMAGE screen has a RGBA32 background 0,0,0,0, this changes to 0,0,0,255 when CLS is used.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Paint Image
« Reply #46 on: November 25, 2019, 10:23:56 am »
I didn't find my original fill program, so wrote it again. It's not fast, but he doesn't mind the alpha. I'll keep doing it. I will add padding support. The program does not need to know the color number of the color border, the boundaries of the filled color can be different.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. CIRCLE (400, 400), 150, &HFFFF0000
  3. LINE (300, 450)-(500, 150), &H4AEEC2F7, B
  4. LINE (370, 370)-(430, 430), &HFFFF0000, BF
  5. CIRCLE (400, 300), 460, &H1744F3FF
  6.  
  7. Fill 400, 200, &H4AEEC2F7
  8. Fill 400, 300, &HFFFFFF00
  9. Fill 1, 1, &HFFFFFFFF
  10. Fill 200, 440, &HE0FFFFFF
  11. Fill 400, 500, &HDF00FF00
  12.  
  13.  
  14.  
  15.  
  16.  
  17. SUB Fill (x AS INTEGER, y AS INTEGER, clr AS _UNSIGNED LONG)
  18.     TYPE border
  19.         X AS INTEGER
  20.         Y AS INTEGER
  21.     END TYPE
  22.     'z bodu plneni jede vpravo a udela caru od bodu plneni k okraji. Tyto body zapise do pole.
  23.     'from fill points X, Y go to right and draw line. This points are writed to array B.
  24.  
  25.     REDIM B(0) AS border
  26.     DIM bck AS _UNSIGNED LONG
  27.     bck = POINT(x, y)
  28.     px = x: py = y
  29.  
  30.  
  31.  
  32.     WHILE bck = POINT(px, py)
  33.         REDIM _PRESERVE B(i) AS border
  34.  
  35.         B(i).X = px
  36.         B(i).Y = py
  37.         i = i + 1
  38.         px = px + 1
  39.     WEND
  40.     ' od x do prava
  41.     '-----------------------------------------------------------------------------------------
  42.     'from fill points X, Y go to left, points are writed to array B, draw line.
  43.  
  44.  
  45.     px = x: py = y
  46.     WHILE bck = POINT(px, py)
  47.         REDIM _PRESERVE B(i) AS border
  48.  
  49.         B(i).X = px
  50.         B(i).Y = py
  51.         i = i + 1
  52.         px = px - 1
  53.     WEND
  54.  
  55.     i = 0
  56.     'od X do leva
  57.     'Start in: REDIM (C)....  from all points in array B fill to bottom. Write this points to array C.
  58.  
  59.  
  60.     REDIM C(0) AS border
  61.     'vyplni vechny body od predchozi linie plneni smerem dolu.
  62.     FOR p = 0 TO UBOUND(b)
  63.         Fx = B(i).X
  64.         Fy = B(i).Y
  65.         WHILE bck = POINT(Fx, Fy)
  66.             PSET (Fx, Fy), clr~&
  67.             C(j).X = Fx
  68.             C(j).Y = Fy
  69.             Fy = Fy + 1
  70.             j = j + 1
  71.             REDIM _PRESERVE C(j) AS border
  72.         WEND
  73.         i = i + 1
  74.     NEXT p
  75.  
  76.  
  77.     'vyplni predchozi linie plneni smerem nahoru
  78.     'from all points in array B fill to ceiling. Write this points to array C.
  79.  
  80.     i = 0
  81.     FOR p = 0 TO UBOUND(b)
  82.         Fx = B(i).X
  83.         Fy = B(i).Y - 1
  84.         WHILE bck = POINT(Fx, Fy)
  85.             PSET (Fx, Fy), clr~&
  86.             C(j).X = Fx
  87.             C(j).Y = Fy
  88.             Fy = Fy - 1
  89.             j = j + 1
  90.             REDIM _PRESERVE C(j) AS border
  91.         WEND
  92.         i = i + 1
  93.     NEXT p
  94.  
  95.  
  96.     'nize: z bodu v poli C jet vlevo a vpravo, nahoru a dolu.
  97.     'do this 4x:
  98.     'look if neigthbour point is BCK. Starting points are contained in Array C. Look to left, right, up and down. If this color is BCK, fill it until
  99.     'is color other than BCK. Write this points to array B. After then is S selected and loop go again. After then write values from array B to array C
  100.     'and search again. Total search: 4x.
  101.  
  102.  
  103.     REDIM B(0) AS border
  104.  
  105.     FOR search = 1 TO 4
  106.         i = 0
  107.         FOR p = 0 TO UBOUND(c)-1
  108.             FOR s = 1 TO 4
  109.                 Fx = C(p).X
  110.                 Fy = C(p).Y
  111.  
  112.                 SELECT CASE s
  113.                     CASE 1: Sx = 1: sy = 0
  114.                     CASE 2: Sx = -1: sy = 0
  115.                     CASE 3: sy = 1: Sx = 0
  116.                     CASE 4: sy = -1: Sx = 0
  117.                 END SELECT
  118.  
  119.                 WHILE bck = POINT(Fx + Sx, Fy + sy)
  120.                     PSET (Fx + Sx, Fy + sy), clr
  121.                     Fx = Fx + Sx
  122.                     Fy = Fy + sy
  123.                     B(i).X = Fx
  124.                     B(i).Y = Fy
  125.                     i = i + 1
  126.                     REDIM _PRESERVE B(i) AS border
  127.                 WEND
  128.             NEXT s
  129.         NEXT p
  130.  
  131.  
  132.         REDIM C(i - 1) AS border
  133.         FOR L = 0 TO i - 1
  134.             C(L).X = B(L).X
  135.             C(L).Y = B(L).Y
  136.         NEXT
  137.     NEXT search
  138.     ERASE C
  139.     ERASE B
  140.  

« Last Edit: November 26, 2019, 05:25:58 pm by Petr »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Paint Image
« Reply #47 on: November 25, 2019, 10:52:17 am »
Hi Petr,

Looks like it just does the color it lands on at x, y, also looks pretty fast compared to my effort:
https://www.qb64.org/forum/index.php?topic=1914.msg111421#msg111421

Did you compare times with Steve's Magic filler?

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Paint Image
« Reply #48 on: November 25, 2019, 11:41:55 am »
BPlus, this example show better, what can you do with my fill program, what PAINT can not do:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. CIRCLE (400, 400), 150, &HFFFF0000
  3. LINE (300, 450)-(500, 150), &H4AEEC2F7, BF
  4. LINE (370, 370)-(430, 430), &HFFFF0000, BF
  5. CIRCLE (400, 300), 460, &H1744F3FF
  6. PRINT "How to fill the black area in a circle now, without damaging the square? - others color borders"
  7. PRINT "Press key..."
  8.  
  9.  
  10.  
  11.  
  12. Fill 400, 530, &HADFFFFFF
  13.  
  14.  
  15.  
  16.  
  17.  
  18. SUB Fill (x AS INTEGER, y AS INTEGER, clr AS _UNSIGNED LONG)
  19.     TYPE border
  20.         X AS INTEGER
  21.         Y AS INTEGER
  22.     END TYPE
  23.     'z bodu plneni jede vpravo a udela caru od bodu plneni k okraji. Tyto body zapise do pole.
  24.     'from fill points X, Y go to right and draw line. This points are writed to array B.
  25.  
  26.     REDIM B(0) AS border
  27.     DIM bck AS _UNSIGNED LONG
  28.     bck = POINT(x, y)
  29.     px = x: py = y
  30.  
  31.  
  32.  
  33.     WHILE bck = POINT(px, py)
  34.         REDIM _PRESERVE B(i) AS border
  35.  
  36.         B(i).X = px
  37.         B(i).Y = py
  38.         i = i + 1
  39.         px = px + 1
  40.     WEND
  41.     ' od x do prava
  42.     '-----------------------------------------------------------------------------------------
  43.     'from fill points X, Y go to left, points are writed to array B, draw line.
  44.  
  45.  
  46.     px = x: py = y
  47.     WHILE bck = POINT(px, py)
  48.         REDIM _PRESERVE B(i) AS border
  49.  
  50.         B(i).X = px
  51.         B(i).Y = py
  52.         i = i + 1
  53.         px = px - 1
  54.     WEND
  55.  
  56.     i = 0
  57.     'od X do leva
  58.     'Start in: REDIM (C)....  from all points in array B fill to bottom. Write this points to array C.
  59.  
  60.  
  61.     REDIM C(0) AS border
  62.     'vyplni vechny body od predchozi linie plneni smerem dolu.
  63.     FOR p = 0 TO UBOUND(b)
  64.         Fx = B(i).X
  65.         Fy = B(i).Y
  66.         WHILE bck = POINT(Fx, Fy)
  67.             PSET (Fx, Fy), clr~&
  68.             C(j).X = Fx
  69.             C(j).Y = Fy
  70.             Fy = Fy + 1
  71.             j = j + 1
  72.             REDIM _PRESERVE C(j) AS border
  73.         WEND
  74.         i = i + 1
  75.     NEXT p
  76.  
  77.  
  78.     'vyplni predchozi linie plneni smerem nahoru
  79.     'from all points in array B fill to ceiling. Write this points to array C.
  80.  
  81.     i = 0
  82.     FOR p = 0 TO UBOUND(b)
  83.         Fx = B(i).X
  84.         Fy = B(i).Y - 1
  85.         WHILE bck = POINT(Fx, Fy)
  86.             PSET (Fx, Fy), clr~&
  87.             C(j).X = Fx
  88.             C(j).Y = Fy
  89.             Fy = Fy - 1
  90.             j = j + 1
  91.             REDIM _PRESERVE C(j) AS border
  92.         WEND
  93.         i = i + 1
  94.     NEXT p
  95.  
  96.  
  97.     'nize: z bodu v poli C jet vlevo a vpravo, nahoru a dolu.
  98.     'do this 4x:
  99.     'look if neigthbour point is BCK. Starting points are contained in Array C. Look to left, right, up and down. If this color is BCK, fill it until
  100.     'is color other than BCK. Write this points to array B. After then is S selected and loop go again. After then write values from array B to array C
  101.     'and search again. Total search: 4x.
  102.  
  103.  
  104.     REDIM B(0) AS border
  105.  
  106.     FOR search = 1 TO 4
  107.         i = 0
  108.         FOR p = 0 TO UBOUND(c)-1
  109.             FOR s = 1 TO 4
  110.                 Fx = C(p).X
  111.                 Fy = C(p).Y
  112.  
  113.                 SELECT CASE s
  114.                     CASE 1: Sx = 1: sy = 0
  115.                     CASE 2: Sx = -1: sy = 0
  116.                     CASE 3: sy = 1: Sx = 0
  117.                     CASE 4: sy = -1: Sx = 0
  118.                 END SELECT
  119.  
  120.                 WHILE bck = POINT(Fx + Sx, Fy + sy)
  121.                     PSET (Fx + Sx, Fy + sy), clr
  122.                     Fx = Fx + Sx
  123.                     Fy = Fy + sy
  124.                     B(i).X = Fx
  125.                     B(i).Y = Fy
  126.                     i = i + 1
  127.                     REDIM _PRESERVE B(i) AS border
  128.                 WEND
  129.             NEXT s
  130.         NEXT p
  131.  
  132.  
  133.         REDIM C(i - 1) AS border
  134.         FOR L = 0 TO i - 1
  135.             C(L).X = B(L).X
  136.             C(L).Y = B(L).Y
  137.         NEXT
  138.     NEXT search
  139.     ERASE C
  140.     ERASE B
  141.  

I didn't compare speed with Steve. There's no point in proving I'm better :)  (THIS IS JOKE!)
Frankly, I will use this function to draw an image, and then I will put the finished image on the screen in a program. Using color filling while running a program is nonsense. This is an unnecessary load on the processor. I will not solve the speed, I will solve the fill - so as in the  QB4.5. I do not share claim, that working with a program below 256 colors is a waste of time. Not for me.
« Last Edit: November 26, 2019, 05:26:25 pm by Petr »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Paint Image
« Reply #49 on: November 25, 2019, 01:29:57 pm »
about Petr's affirmation
Quote
Just a note. Each _NEWIMAGE screen has a RGBA32 background 0,0,0,0, this changes to 0,0,0,255 when CLS is used.
i'll go on and I try this study of POINT in SCREEN 32bit

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. PRINT " USING COLORS AS VARIABLE _UNSIGNED LONG"
  3. PRINT "LET'S SEE DIFFERENCE BETWEEN POINT ON NATIVE BACKGROUND "
  4. PRINT "  POINT    POINT+BACKGROUND       KOLOR     BLACK BACKGROUND"
  5. C1 = _RGBA32(0, 127, 127, 10)
  6. C2 = _RGBA32(10, 100, 20, 100)
  7. C3 = _RGBA32(10, 10, 127, 150)
  8. C4 = _RGBA32(127, 10, 10, 200)
  9. C5 = _RGBA32(200, 100, 4, 230)
  10. FOR a = 1 TO 5 STEP 1
  11.     IF a = 1 THEN kolor = C1
  12.     IF a = 2 THEN kolor = C2
  13.     IF a = 3 THEN kolor = C3
  14.     IF a = 4 THEN kolor = C4
  15.     IF a = 5 THEN kolor = C5
  16.     CIRCLE (400, 200), a * 10, kolor
  17.     LOCATE a + 3, 1: PRINT POINT(400, 200 + a * 10), POINT(400, 200 + a * 10) + POINT(400, 200), kolor, POINT(400, 200)
  18.     CIRCLE (200, 200), a * 10, POINT(400, 200 + a * 10)
  19. VIEW PRINT 20 TO 29
  20. VIEW SCREEN(1, 300)-(798, 598), , _RGBA32(1, 127, 127, 255)
  21. PRINT " NOW AFTER CLS WITH BLACK BACKGROUND "
  22. PRINT "  POINT      POINT+BACKGROUND       KOLOR     BLACK BACKGROUND"
  23. FOR a = 1 TO 5 STEP 1
  24.     IF a = 1 THEN kolor = C1
  25.     IF a = 2 THEN kolor = C2
  26.     IF a = 3 THEN kolor = C3
  27.     IF a = 4 THEN kolor = C4
  28.     IF a = 5 THEN kolor = C5
  29.     CIRCLE (400, 500), a * 10, kolor
  30.     LOCATE a + 23, 1: PRINT POINT(400, 500 + a * 10), POINT(400, 500 + a * 10) + POINT(400, 500), kolor, POINT(400, 500)
  31.     CIRCLE (200, 500), a * 10, POINT(400, 500 + a * 10)
  32.  

with this results
 
POINT study on 32background.jpg


but tiese results show that in screen 32bit the color are merged to that are yet on the screen so as Petr suggests  before original black background is RGBA 0 0 0 0   while after CLS it becomes 0 0 0 255 getting a different value with POINT!

Good this!
 
Petr Fill routine.jpg


and this one !
 
petr fill routine demo.jpg

Programming isn't difficult, only it's  consuming time and coffee

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Paint Image
« Reply #50 on: November 25, 2019, 02:26:36 pm »
Hint:  Use CLS, 0
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Paint Image
« Reply #51 on: November 25, 2019, 04:52:50 pm »
Steve
Thanks for tip so we can force to use black RGBA 0,0,0,0

result after CLS,0 is the same as in original screen 32bit
 
POINT study on 32bit Steve Tip.jpg



Programming isn't difficult, only it's  consuming time and coffee