Author Topic: Mosaic Effect  (Read 4178 times)

0 Members and 1 Guest are viewing this topic.

Offline MrFreyer

  • Newbie
  • Posts: 34
    • View Profile
Mosaic Effect
« on: November 06, 2019, 03:31:34 am »
...it's the mosaic effect, like the SNES games used it to pixelate (blur?) the screen.

Code: QB64: [Select]
  1. DEFINT A-Z
  2.  
  3.  
  4. desktopscreen.width = _DESKTOPWIDTH
  5. desktopscreen.height = _DESKTOPHEIGHT
  6.  
  7. screenwindow.width = 800
  8. screenwindow.height = 600
  9.  
  10. SCREEN _NEWIMAGE(screenwindow.width, screenwindow.height, 32)
  11. _SCREENMOVE (desktopscreen.width * 0.5 - screenwindow.width * 0.5), 0
  12.  
  13. CONST pixelbytes = 4 '4 byte per pixel (RGBA)
  14.  
  15. DIM pixelcolorRed AS _UNSIGNED _BYTE
  16. DIM pixelcolorGreen AS _UNSIGNED _BYTE
  17. DIM pixelcolorBlue AS _UNSIGNED _BYTE
  18.  
  19. DIM imageScreenMEMORY AS _MEM
  20.  
  21. mosaicSize = 1
  22.  
  23. 'calculate maximum mosaic tiles
  24. mosaicsVertical = FIX(screenwindow.height / mosaicSize)
  25. mosaicsHorizontal = FIX(screenwindow.width / mosaicSize)
  26.  
  27.     CLS
  28.     LINE (0, 0)-(screenwindow.width - 1, screenwindow.height - 1), _RGB(255, 255, 95), BF
  29.     _FONT 16
  30.     COLOR _RGB(255, 0, 255), _RGB(255, 255, 255)
  31.     _PRINTSTRING (32, 0), "Explanation:"
  32.     COLOR _RGB(255, 191, 0), _RGB(191, 95, 0)
  33.     _PRINTSTRING (32, 64), "This program shows the Mosaic Effect."
  34.     COLOR _RGB(0, 255, 0), _RGB(0, 0, 63)
  35.     _PRINTSTRING (32, 128), "But it's the old version, like the Super Nintendo Entertainment System used it."
  36.     COLOR _RGB(191, 191, 191), _RGB(191, 63, 0)
  37.     _PRINTSTRING (32, 192), "The old version takes just the color of a pixel"
  38.     COLOR _RGB(95, 63, 95), _RGB(0, 255, 193)
  39.     _PRINTSTRING (32, 256), "and overwrites the next few pixels on the x and y axis"
  40.     COLOR _RGB(0, 191, 127), _RGB(255, 0, 0)
  41.     _PRINTSTRING (32, 320), "with the color values of the first pixel."
  42.     COLOR _RGB(0, 255, 123), _RGB(31, 95, 31)
  43.     _PRINTSTRING (32, 384), "There is also a newer, smooth version, but who needs it? :D"
  44.     COLOR _RGB(255, 255, 255), _RGB(0, 0, 0)
  45.     _PRINTSTRING (32, 448), "Just press [+] or [-] to change the mosaic size."
  46.     COLOR _RGB(255, 95, 95), _RGB(0, 0, 0)
  47.     _PRINTSTRING (464, 448), "<<< change mosaic size"
  48.     COLOR _RGB(0, 0, 0), _RGB(63, 127, 63)
  49.     _PRINTSTRING (32, 512), "Press [Escape] to close the program."
  50.     COLOR _RGB(255, 95, 95), _RGB(0, 0, 0)
  51.     _PRINTSTRING (464, 512), "<<< close program"
  52.     COLOR _RGB(255, 0, 0), _RGB(31, 31, 31)
  53.     _PRINTSTRING (32, 576), "X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X"
  54.  
  55.     imageScreenMEMORY = _MEMIMAGE(0)
  56.  
  57.     FOR pixelVertical = 1 TO mosaicsVertical
  58.         FOR pixelHorizontal = 1 TO mosaicsHorizontal
  59.             y = (pixelVertical  - 1) * mosaicSize
  60.             x = (pixelHorizontal  - 1) * mosaicSize
  61.  
  62.             _MEMGET imageScreenMEMORY , imageScreenMEMORY .OFFSET + (x * pixelbytes) + (y * (screenwindow.width * pixelbytes)), pixelcolorRed
  63.             _MEMGET imageScreenMEMORY , imageScreenMEMORY .OFFSET + ((x * pixelbytes) + 1) + (y * (screenwindow.width * pixelbytes)), pixelcolorGreen
  64.             _MEMGET imageScreenMEMORY , imageScreenMEMORY .OFFSET + ((x * pixelbytes) + 2) + (y * (screenwindow.width * pixelbytes)), pixelcolorBlue
  65.  
  66.             FOR y2 = 0 TO mosaicSize - 1
  67.                 FOR x2 = 0 TO mosaicSize - 1
  68.                     _MEMPUT imageScreenMEMORY , imageScreenMEMORY .OFFSET + (x * pixelbytes) + (x2 * pixelbytes) + (y * (screenwindow.width * pixelbytes)) + (y2 * (screenwindow.width * pixelbytes)), pixelcolorRed
  69.                     _MEMPUT imageScreenMEMORY , imageScreenMEMORY .OFFSET + ((x * pixelbytes) + 1) + (x2 * pixelbytes) + (y * (screenwindow.width * pixelbytes)) + (y2 * (screenwindow.width * pixelbytes)), pixelcolorGreen
  70.                     _MEMPUT imageScreenMEMORY , imageScreenMEMORY .OFFSET + ((x * pixelbytes) + 2) + (x2 * pixelbytes) + (y * (screenwindow.width * pixelbytes)) + (y2 * (screenwindow.width * pixelbytes)), pixelcolorBlue
  71.                 NEXT x2
  72.             NEXT y2
  73.  
  74.         NEXT pixelHorizontal
  75.     NEXT pixelVertical
  76.  
  77.     _DISPLAY
  78.  
  79.     WHILE INKEY$ <> "": WEND
  80.     DO
  81.         keypressed$ = INKEY$
  82.         IF keypressed$ = CHR$(43) THEN
  83.             mosaicSize = mosaicSize + 1
  84.             IF mosaicSize > 64 THEN mosaicSize = 64
  85.             mosaicsVertical = FIX(screenwindow.height / mosaicSize)
  86.             mosaicsHorizontal = FIX(screenwindow.width / mosaicSize)
  87.             EXIT DO
  88.         END IF
  89.         IF keypressed$ = CHR$(45) THEN
  90.             mosaicSize = mosaicSize - 1
  91.             IF mosaicSize < 1 THEN mosaicSize = 1
  92.             mosaicsVertical = FIX(screenwindow.height / mosaicSize)
  93.             mosaicsHorizontal = FIX(screenwindow.width / mosaicSize)
  94.             EXIT DO
  95.         END IF
  96.         IF keypressed$ = CHR$(27) THEN GOTO close_program
  97.     LOOP
  98.  
  99.  
  100. close_program:
  101.  


EDIT:
...and here the slightly slower version, but with POINT/ PRESET instead of _MEMGET/ _MEMPUT
Code: QB64: [Select]
  1. DEFINT A-Z
  2.  
  3.  
  4. desktopscreen.width = _DESKTOPWIDTH
  5. desktopscreen.height = _DESKTOPHEIGHT
  6.  
  7. screenwindow.width = 800
  8. screenwindow.height = 600
  9.  
  10. SCREEN _NEWIMAGE(screenwindow.width, screenwindow.height, 32)
  11. _SCREENMOVE (desktopscreen.width * 0.5 - screenwindow.width * 0.5), 0
  12.  
  13. DIM pixelcolor AS LONG
  14. DIM pixelcolorRed AS _UNSIGNED _BYTE
  15. DIM pixelcolorGreen AS _UNSIGNED _BYTE
  16. DIM pixelcolorBlue AS _UNSIGNED _BYTE
  17.  
  18. mosaicSize = 1
  19.  
  20. 'calculate maximum mosaic tiles
  21. mosaicsVertical = FIX(screenwindow.height / mosaicSize)
  22. mosaicsHorizontal = FIX(screenwindow.width / mosaicSize)
  23.  
  24.     CLS
  25.     LINE (0, 0)-(screenwindow.width - 1, screenwindow.height - 1), _RGB(255, 255, 95), BF
  26.     _FONT 16
  27.     COLOR _RGB(255, 0, 255), _RGB(255, 255, 255)
  28.     _PRINTSTRING (32, 0), "Explanation:"
  29.     COLOR _RGB(255, 191, 0), _RGB(191, 95, 0)
  30.     _PRINTSTRING (32, 64), "This program shows the Mosaic Effect."
  31.     COLOR _RGB(0, 255, 0), _RGB(0, 0, 63)
  32.     _PRINTSTRING (32, 128), "But it's the old version, like the Super Nintendo Entertainment System used it."
  33.     COLOR _RGB(191, 191, 191), _RGB(191, 63, 0)
  34.     _PRINTSTRING (32, 192), "The old version takes just the color of a pixel"
  35.     COLOR _RGB(95, 63, 95), _RGB(0, 255, 193)
  36.     _PRINTSTRING (32, 256), "and overwrites the next few pixels on the x and y axis"
  37.     COLOR _RGB(0, 191, 127), _RGB(255, 0, 0)
  38.     _PRINTSTRING (32, 320), "with the color values of the first pixel."
  39.     COLOR _RGB(0, 255, 123), _RGB(31, 95, 31)
  40.     _PRINTSTRING (32, 384), "There is also a newer, smooth version, but who needs it? :D"
  41.     COLOR _RGB(255, 255, 255), _RGB(0, 0, 0)
  42.     _PRINTSTRING (32, 448), "Just press [+] or [-] to change the mosaic size."
  43.     COLOR _RGB(255, 95, 95), _RGB(0, 0, 0)
  44.     _PRINTSTRING (464, 448), "<<< change mosaic size"
  45.     COLOR _RGB(0, 0, 0), _RGB(63, 127, 63)
  46.     _PRINTSTRING (32, 512), "Press [Escape] to close the program."
  47.     COLOR _RGB(255, 95, 95), _RGB(0, 0, 0)
  48.     _PRINTSTRING (464, 512), "<<< close program"
  49.     COLOR _RGB(255, 0, 0), _RGB(31, 31, 31)
  50.     _PRINTSTRING (32, 576), "X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X"
  51.    
  52.     FOR pixelVertical  = 1 TO mosaicsVertical
  53.         FOR pixelHorizontal  = 1 TO mosaicsHorizontal
  54.             y = (pixelVertical  - 1) * mosaicSize
  55.             x = (pixelHorizontal   - 1) * mosaicSize
  56.  
  57.             pixelcolor = POINT(x, y)
  58.             pixelcolorRed = _RED32(pixelcolor)
  59.             pixelcolorGreen = _GREEN32(pixelcolor)
  60.             pixelcolorBlue = _BLUE32(pixelcolor)
  61.  
  62.             FOR y2 = 0 TO mosaicSize - 1
  63.                 FOR x2 = 0 TO mosaicSize - 1
  64.                     PRESET (x + x2, y + y2), _RGB(pixelcolorRed, pixelcolorGreen, pixelcolorBlue)
  65.                 NEXT x2
  66.             NEXT y2
  67.  
  68.         NEXT pixelHorizontal  
  69.     NEXT pixelVertical
  70.     _DISPLAY
  71.  
  72.     WHILE INKEY$ <> "": WEND
  73.     DO
  74.         keypressed$ = INKEY$
  75.         IF keypressed$ = CHR$(43) THEN
  76.             mosaicSize = mosaicSize + 1
  77.             IF mosaicSize > 64 THEN mosaicSize = 64
  78.             mosaicsVertical = FIX(screenwindow.height / mosaicSize)
  79.             mosaicsHorizontal = FIX(screenwindow.width / mosaicSize)
  80.             EXIT DO
  81.         END IF
  82.         IF keypressed$ = CHR$(45) THEN
  83.             mosaicSize = mosaicSize - 1
  84.             IF mosaicSize < 1 THEN mosaicSize = 1
  85.             mosaicsVertical = FIX(screenwindow.height / mosaicSize)
  86.             mosaicsHorizontal = FIX(screenwindow.width / mosaicSize)
  87.             EXIT DO
  88.         END IF
  89.         IF keypressed$ = CHR$(27) THEN GOTO close_program
  90.     LOOP
  91.  
  92.  
  93. close_program:
  94.  
« Last Edit: November 06, 2019, 03:57:53 am by MrFreyer »