Author Topic: Xor image pixels with a known value  (Read 1329 times)

0 Members and 1 Guest are viewing this topic.

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
Xor image pixels with a known value
« on: April 08, 2021, 11:26:57 pm »
Say, there is a 400 * 400 pixel image on the screen. It consists of white text and black background.  I want to XOR every pixel in the image with green (0,255,0).

The result should be magenta text  and green background. I've tried GET and PUT but these are way too much coding for such a simple task.  What is the easiest way to do this?

If XNOR was used, you'd get green text and a magenta background
« Last Edit: April 08, 2021, 11:35:39 pm by NOVARSEG »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Xor image pixels with a known value
« Reply #1 on: April 08, 2021, 11:49:56 pm »
Quote
Say, there is a 400 * 400 pixel image on the screen. It consists of white text and black background.  I want to XOR every pixel in the image with green (0,255,0).

The result should be magenta text  and green background. I've tried GET and PUT but these are way too much coding for such a simple task.  What is the easiest way to do this?

Looks like this works:
Code: QB64: [Select]
  1. _Title "Xor Test with Green" 'b+ 2021-04-09
  2. Screen _NewImage(400, 400, 32)
  3. Color &HFFFFFFFF, &HFF000000
  4. Dim As Single r, g, b, a
  5. Print "Testing 1, 2, 3..."
  6. Print "ZZZ... press any to see Xor Green."
  7. For y = 0 To _Height
  8.     For x = 0 To _Width
  9.         c~& = Point(x, y)
  10.         cAnalysis c~&, r, g, b, a
  11.         PSet (x, y), _RGB32(r Xor 0, g Xor 255, b Xor 0)
  12.     Next
  13.  
  14. Sub cAnalysis (c As _Unsigned Long, outRed, outGrn, outBlu, outAlp)
  15.     outRed = _Red32(c): outGrn = _Green32(c): outBlu = _Blue32(c): outAlp = _Alpha32(c)
  16.  
« Last Edit: April 09, 2021, 03:14:23 pm by bplus »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
Re: Xor image pixels with a known value
« Reply #2 on: April 09, 2021, 12:24:53 am »
bplus
 how does cAnalysis  work

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Xor image pixels with a known value
« Reply #3 on: April 09, 2021, 12:39:15 am »
The _Red32, _Green32, _Blue32 functions take a full _RGB32 number and tell what the red, green and blue component values are _Alpha32 is transparency level like the others also returns a number 0-255.

So cAnalysis just does all 3 reports in one sub, pass it a color number (_unsigned Long) and the 4 Holder variables to pass results back to the main code calling then sub. You have to make sure the variables are the same Type (in the above demo, Single) so the main code can get the values from the variables back after the sub runs.

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
Re: Xor image pixels with a known value
« Reply #4 on: April 09, 2021, 01:36:08 am »
Thanks,    yep that makes sense.

I got XNOR to work but had to DIM variables as  _UNSIGNED _BYTE    to make it work.

The NOT would NOT work without the _UNSIGNED _BYTE  types

even had to DIM the SUB arguments as _UNSIGNED _BYTE

yep, green text and a magenta background with XNOR

Code: QB64: [Select]
  1. _TITLE "Xor Test with Green" 'b+ 2021-04-09
  2. SCREEN _NEWIMAGE(400, 400, 32)
  3. COLOR &HFFFFFFFF, &HFF000000
  4. PRINT "Testing 1, 2, 3..."
  5. PRINT "ZZZ... press any to see Xor Green."
  6. FOR y = 0 TO _HEIGHT
  7.     FOR x = 0 TO _WIDTH
  8.         c~& = POINT(x, y)
  9.         cAnalysis c~&, r, g, b, a
  10.  
  11.         r = r XOR 0: r = NOT r
  12.         g = g XOR 255: g = NOT g
  13.         b = b XOR 0: b = NOT b
  14.  
  15.         PSET (x, y), _RGB32(r, g, b, a)
  16.     NEXT
  17.  
  18. SUB cAnalysis (c AS _UNSIGNED LONG, outRed AS _UNSIGNED _BYTE, outGrn AS _UNSIGNED _BYTE, outBlu AS _UNSIGNED _BYTE, outAlp AS _UNSIGNED _BYTE)
  19.     outRed = _RED32(c): outGrn = _GREEN32(c): outBlu = _BLUE32(c): outAlp = _ALPHA32(c)
  20.  
« Last Edit: April 09, 2021, 01:40:40 am by NOVARSEG »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Xor image pixels with a known value
« Reply #5 on: April 09, 2021, 03:12:30 pm »
Hi @NOVARSEG

I don't understand the business with NOT.

It works fine without:

Oh! you reverse with NOT for magenta background and green text, OK? So XNOR is NOT a Typo LOL!
Oh you edited your original post!
« Last Edit: April 09, 2021, 03:18:49 pm by bplus »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
Re: Xor image pixels with a known value
« Reply #6 on: April 09, 2021, 03:18:44 pm »
XNOR <> XOR

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Xor image pixels with a known value
« Reply #7 on: April 09, 2021, 03:22:12 pm »
XNOR <> XOR

Yeah and XNOR is not a built in bit manipulating function, so I thought it a type-o.

You changed your original post in between time I read it and time I posted my code for original question, confusing!!!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
Re: Xor image pixels with a known value
« Reply #8 on: April 09, 2021, 06:38:59 pm »
Example of logic NAND

Code: QB64: [Select]
  1. _TITLE "Xor Test with Green" 'b+ 2021-04-09
  2. SCREEN _NEWIMAGE(500, 400, 32)
  3. COLOR &HFFFFFFFF, &HFF000000
  4.  
  5. PRINT "Testing 1, 2, 3..."
  6. PRINT "original image is white font with black background"
  7. PRINT "ZZZ... press any key to see pixels NANDed with yellow."
  8.  
  9. PRINT "press ESC to exit"
  10. LL1:
  11.  
  12.  
  13. FOR y = 0 TO _HEIGHT
  14.     FOR x = 0 TO _WIDTH
  15.         C = POINT(x, y)
  16.  
  17.         r = _RED32(C): g = _GREEN32(C): b = _BLUE32(C): a = _ALPHA32(C)
  18.  
  19.         r = r AND 255: r = NOT r
  20.         g = g AND 255: g = NOT g
  21.         b = b AND 0: b = NOT b
  22.  
  23.         PSET (x, y), _RGB32(r, g, b, a)
  24.     NEXT
  25.     i$ = INKEY$
  26.     IF i$ = CHR$(27) THEN EXIT DO
  27.     _DELAY .7: GOTO LL1
  28.  



« Last Edit: April 09, 2021, 06:47:03 pm by NOVARSEG »