Author Topic: Bit confused on PALETTE USING & COPYPALETTE  (Read 2917 times)

0 Members and 1 Guest are viewing this topic.

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Bit confused on PALETTE USING & COPYPALETTE
« on: November 10, 2020, 12:07:23 pm »
Maybe I am misunderstanding the Palette commands...

Suppose I have

SCREEN _NEWIMAGE(3200,1800,32)

and on the one and same display draw some graphic lines and boxes and place somewhere on the screen a 8-bit color picture (which has its own 256 color palette (#1) in its header).

I then on this same display load the same 8-bit color picture and graphics somewhere else on the display BUT I want to override the internal header palette and use my own palette (say #2).

I then again load the same image and graphics somewhere else and use another 256 color palette set (#3) and apply it to this image.

So is it possible to have, on the same display,  an image and graphics using three different palettes simultaneously?

From the QB64 help wiki:-

PALETTE USING statement sets all RGB screen color intensities using values from an array PALETTE USING array%(startindex%)
array%    red% + 256*green% + 65536*blue% .  How is it possible to load the array (being integer) when each of red% green% blue% can be of value up to 256?

_COPY PALETTE statement copies the color palette intensities from 8bpp image to another image or a _NEWIMAGE screen page using 256 colors.

If the _NEWIMAGE is only used once, can _COPY PALETTE be used say three times for the effect mentioned above?

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Bit confused on PALETTE USING & COPYPALETTE
« Reply #1 on: November 10, 2020, 12:54:39 pm »
I think it will work. Wait a minute, I'll try to write it. So you want a 32-bit screen and place three 256-color images on it, each with a different palette. Good. I'm on it :)

So really, it works. Two images uses here random colorized palette, one image use own original palette:


Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. img1 = LOADIMAGE256("parrot.png")
  3.  
  4.  
  5. DIM palett(1, 255) AS _UNSIGNED LONG 'first is original palette, second is 0, 255,   third is 1, 255
  6.  
  7. 'generate two rando palettes
  8.  
  9. FOR p = 0 TO 1
  10.     FOR mask = 0 TO 255
  11.         R = 255 * RND
  12.         G = 255 * RND
  13.         B = 255 * RND
  14.         palett(p, mask) = _RGB32(R, G, B)
  15.     NEXT mask
  16.  
  17. img2 = _COPYIMAGE(img1, 256)
  18. img3 = _COPYIMAGE(img1, 256)
  19.  
  20. 'set palette for image img2
  21. FOR set = 0 TO 255
  22.     'set palette for image img2
  23.     _PALETTECOLOR set, palett(0, set), img2
  24.     'set palette for image img3
  25.     _PALETTECOLOR set, palett(1, set), img3
  26.  
  27.  
  28.  
  29. _PUTIMAGE (0, 50), img1 'place image using original palette
  30. _PUTIMAGE (300, 50), img2
  31. _PUTIMAGE (600, 50), img3
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. FUNCTION LOADIMAGE256 (img$)
  39.     DEFLNG A-Z
  40.     image = _LOADIMAGE(img$, 32)
  41.  
  42.     DIM m AS _MEM, clr8(255) AS _UNSIGNED LONG, Clr32 AS _UNSIGNED LONG, test AS LONG, s AS LONG
  43.  
  44.     FOR s = 0 TO 255
  45.         clr8(s) = 99999
  46.     NEXT s
  47.  
  48.     m = _MEMIMAGE(image)
  49.     DO UNTIL p& = m.SIZE
  50.         _MEMGET m, m.OFFSET + p&, Clr32~&
  51.  
  52.         test = 0
  53.         'this block prevent for writing the same color more than 1x to palette array
  54.         DO UNTIL test = 255
  55.             IF clr8(test) = Clr32~& THEN GOTO NextColor
  56.             IF clr8(test) = 99999 THEN EXIT DO
  57.             test = test + 1
  58.         LOOP
  59.  
  60.         'if is empty place in palette, save this color as next palette color
  61.         IF test > 255 THEN PRINT "Image contains more than 256 colors, can not be directly copyed as 8 bit image": END
  62.         clr8(test) = Clr32
  63.  
  64.         'color is saved as palette for 8 bit image
  65.         NextColor: p& = p& + 4
  66.     LOOP
  67.  
  68.     image8 = _NEWIMAGE(_WIDTH(image), _HEIGHT(image), 256)
  69.  
  70.     'set palette
  71.     N = _MEMIMAGE(image8)
  72.     FOR palett = 0 TO 255
  73.         _PALETTECOLOR palett, clr8(palett), image8
  74.     NEXT
  75.  
  76.  
  77.     'create 8 bit mask (set colors 0 to 255 to 8 bit image)
  78.     FOR C = 255 TO 0 STEP -1
  79.         clr~& = clr8(C)
  80.         R& = 0
  81.         R8& = 0
  82.         DO UNTIL R& = m.SIZE
  83.             _MEMGET m, m.OFFSET + R&, Clr32
  84.             IF Clr32 = clr~& THEN _MEMPUT N, N.OFFSET + R8&, C
  85.             R& = R& + 4
  86.             R8& = R8& + 1
  87.         LOOP
  88.     NEXT C
  89.  
  90.     LOADIMAGE256 = _COPYIMAGE(image8, 256)
  91.     _MEMFREE m
  92.     _MEMFREE N
  93.     _FREEIMAGE image
  94.     _FREEIMAGE image8
  95.  

  [ You are not allowed to view this attachment ]  

You can use _PALETTECOLOR also as function for giving current color palette value:     color32Value& = _PALETTECOLOR(attributeNumber%, imgHandle&)
« Last Edit: November 10, 2020, 01:37:40 pm by Petr »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Bit confused on PALETTE USING & COPYPALETTE
« Reply #2 on: November 10, 2020, 02:01:10 pm »
Next example. First image use original palette, second use inverse palette and third use random RED color in RGB32 palette.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. img1 = LOADIMAGE256("parrot.png")
  3.  
  4.  
  5. DIM palett(1, 255) AS _UNSIGNED LONG 'first is original palette, second is 0, 255,   third is 2, 255
  6.  
  7. 'generate two rando palettes
  8.  
  9.  
  10.  
  11.  
  12. FOR mask = 0 TO 255
  13.     R = 255 * RND
  14.     R2 = _RED32(_PALETTECOLOR(mask, img1))
  15.     G = 255 * RND
  16.     G2 = _GREEN32(_PALETTECOLOR(mask, img1))
  17.     B = 255 * RND
  18.     B2 = _BLUE32(_PALETTECOLOR(mask, img1))
  19.     palett(0, mask) = _RGB32(255 - R2, 255 - G2, 255 - B2) 'inverse palette color
  20.     palett(1, mask) = _RGB32(R, G2, B2) 'use original Green and Blue color values in unsigned long palette color value
  21. NEXT mask
  22.  
  23.  
  24. img2 = _COPYIMAGE(img1, 256)
  25. img3 = _COPYIMAGE(img1, 256)
  26.  
  27. 'set palette for image img2
  28. FOR set = 0 TO 255
  29.     'set palette for image img2
  30.     _PALETTECOLOR set, palett(0, set), img2
  31.     'set palette for image img3
  32.     _PALETTECOLOR set, palett(1, set), img3
  33.  
  34.  
  35.  
  36. _PUTIMAGE (0, 50), img1 'place image using original palette
  37. _PUTIMAGE (300, 50), img2
  38. _PUTIMAGE (600, 50), img3
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. FUNCTION LOADIMAGE256 (img$)
  46.     DEFLNG A-Z
  47.     image = _LOADIMAGE(img$, 32)
  48.  
  49.     DIM m AS _MEM, clr8(255) AS _UNSIGNED LONG, Clr32 AS _UNSIGNED LONG, test AS LONG, s AS LONG
  50.  
  51.     FOR s = 0 TO 255
  52.         clr8(s) = 99999
  53.     NEXT s
  54.  
  55.     m = _MEMIMAGE(image)
  56.     DO UNTIL p& = m.SIZE
  57.         _MEMGET m, m.OFFSET + p&, Clr32~&
  58.  
  59.         test = 0
  60.         'this block prevent for writing the same color more than 1x to palette array
  61.         DO UNTIL test = 255
  62.             IF clr8(test) = Clr32~& THEN GOTO NextColor
  63.             IF clr8(test) = 99999 THEN EXIT DO
  64.             test = test + 1
  65.         LOOP
  66.  
  67.         'if is empty place in palette, save this color as next palette color
  68.         IF test > 255 THEN PRINT "Image contains more than 256 colors, can not be directly copyed as 8 bit image": END
  69.         clr8(test) = Clr32
  70.  
  71.         'color is saved as palette for 8 bit image
  72.         NextColor: p& = p& + 4
  73.     LOOP
  74.  
  75.     image8 = _NEWIMAGE(_WIDTH(image), _HEIGHT(image), 256)
  76.  
  77.     'set palette
  78.     N = _MEMIMAGE(image8)
  79.     FOR palett = 0 TO 255
  80.         _PALETTECOLOR palett, clr8(palett), image8
  81.     NEXT
  82.  
  83.  
  84.     'create 8 bit mask (set colors 0 to 255 to 8 bit image)
  85.     FOR C = 255 TO 0 STEP -1
  86.         clr~& = clr8(C)
  87.         R& = 0
  88.         R8& = 0
  89.         DO UNTIL R& = m.SIZE
  90.             _MEMGET m, m.OFFSET + R&, Clr32
  91.             IF Clr32 = clr~& THEN _MEMPUT N, N.OFFSET + R8&, C
  92.             R& = R& + 4
  93.             R8& = R8& + 1
  94.         LOOP
  95.     NEXT C
  96.  
  97.     LOADIMAGE256 = _COPYIMAGE(image8, 256)
  98.     _MEMFREE m
  99.     _MEMFREE N
  100.     _FREEIMAGE image
  101.     _FREEIMAGE image8
  102.  

  [ You are not allowed to view this attachment ]  
« Last Edit: November 10, 2020, 02:07:19 pm by Petr »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Bit confused on PALETTE USING & COPYPALETTE
« Reply #3 on: November 10, 2020, 02:56:28 pm »
Code: QB64: [Select]
  1. 'example for set palette using _NEWIMAGE
  2.  
  3. scr32 = _NEWIMAGE(800, 600, 32)
  4. SCREEN scr32
  5. image1 = _NEWIMAGE(100, 100, 256)
  6. i32 = _DEST
  7. _DEST image1
  8. FOR d = 0 TO 100 STEP 10
  9.     COLOR 20 + d
  10.     LINE (d, 0)-(d + 10, 100), , BF
  11.  
  12. image2 = _COPYIMAGE(image1)
  13. image3 = _COPYIMAGE(image1)
  14. '_COPYPALETTE image1, image2 'is not need, if image 1 is created with standard basic color palette
  15. '_COPYPALETTE image1, image3
  16.  
  17. 'set palette for image 2 and image 3
  18.  
  19. 'for image 2 set color palette 50 (change color 50)
  20. _PALETTECOLOR 50, _RGB32(60, 70, 80), image2
  21.  
  22.  
  23. 'for image 3 set color palette 70 (change color 70)
  24. _PALETTECOLOR 70, _RGB32(222, 2, 22), image3
  25.  
  26.  
  27. _PUTIMAGE (100, 110), image1
  28. _PUTIMAGE (100, 220), image2
  29. _PUTIMAGE (100, 330), image3
  30.